|
| 1 | +# Send L2CAP data as fast as possible and time it. |
| 2 | + |
| 3 | +from micropython import const |
| 4 | +import time, machine, bluetooth, random |
| 5 | + |
| 6 | +TIMEOUT_MS = 1000 |
| 7 | + |
| 8 | +_IRQ_CENTRAL_CONNECT = const(1) |
| 9 | +_IRQ_CENTRAL_DISCONNECT = const(2) |
| 10 | +_IRQ_PERIPHERAL_CONNECT = const(7) |
| 11 | +_IRQ_PERIPHERAL_DISCONNECT = const(8) |
| 12 | +_IRQ_L2CAP_ACCEPT = const(22) |
| 13 | +_IRQ_L2CAP_CONNECT = const(23) |
| 14 | +_IRQ_L2CAP_DISCONNECT = const(24) |
| 15 | +_IRQ_L2CAP_RECV = const(25) |
| 16 | +_IRQ_L2CAP_SEND_READY = const(26) |
| 17 | + |
| 18 | +_L2CAP_PSM = const(22) |
| 19 | +_L2CAP_MTU = const(512) |
| 20 | + |
| 21 | +_PAYLOAD_LEN = const(_L2CAP_MTU) |
| 22 | +_NUM_PAYLOADS = const(20) |
| 23 | + |
| 24 | +_RANDOM_SEED = 22 |
| 25 | + |
| 26 | + |
| 27 | +waiting_events = {} |
| 28 | + |
| 29 | + |
| 30 | +def irq(event, data): |
| 31 | + if event == _IRQ_CENTRAL_CONNECT: |
| 32 | + conn_handle, addr_type, addr = data |
| 33 | + waiting_events[event] = conn_handle |
| 34 | + elif event == _IRQ_PERIPHERAL_CONNECT: |
| 35 | + conn_handle, addr_type, addr = data |
| 36 | + waiting_events[event] = conn_handle |
| 37 | + elif event == _IRQ_L2CAP_ACCEPT: |
| 38 | + conn_handle, cid, psm, our_mtu, peer_mtu = data |
| 39 | + waiting_events[event] = (conn_handle, cid, psm) |
| 40 | + elif event == _IRQ_L2CAP_CONNECT: |
| 41 | + conn_handle, cid, psm, our_mtu, peer_mtu = data |
| 42 | + waiting_events[event] = (conn_handle, cid, psm, our_mtu, peer_mtu) |
| 43 | + |
| 44 | + if event not in waiting_events: |
| 45 | + waiting_events[event] = None |
| 46 | + |
| 47 | + |
| 48 | +def wait_for_event(event, timeout_ms): |
| 49 | + t0 = time.ticks_ms() |
| 50 | + while time.ticks_diff(time.ticks_ms(), t0) < timeout_ms: |
| 51 | + if event in waiting_events: |
| 52 | + return waiting_events.pop(event) |
| 53 | + machine.idle() |
| 54 | + raise ValueError("Timeout waiting for {}".format(event)) |
| 55 | + |
| 56 | + |
| 57 | +def send_data(ble, conn_handle, cid): |
| 58 | + buf = bytearray(_PAYLOAD_LEN) |
| 59 | + for i in range(_NUM_PAYLOADS): |
| 60 | + for j in range(_PAYLOAD_LEN): |
| 61 | + buf[j] = random.randint(0, 255) |
| 62 | + if not ble.l2cap_send(conn_handle, cid, buf): |
| 63 | + wait_for_event(_IRQ_L2CAP_SEND_READY, TIMEOUT_MS) |
| 64 | + |
| 65 | + |
| 66 | +def recv_data(ble, conn_handle, cid): |
| 67 | + buf = bytearray(_PAYLOAD_LEN) |
| 68 | + recv_bytes = 0 |
| 69 | + recv_correct = 0 |
| 70 | + expected_bytes = _PAYLOAD_LEN * _NUM_PAYLOADS |
| 71 | + ticks_first_byte = 0 |
| 72 | + while recv_bytes < expected_bytes: |
| 73 | + wait_for_event(_IRQ_L2CAP_RECV, TIMEOUT_MS) |
| 74 | + if not ticks_first_byte: |
| 75 | + ticks_first_byte = time.ticks_ms() |
| 76 | + while True: |
| 77 | + n = ble.l2cap_recvinto(conn_handle, cid, buf) |
| 78 | + if n == 0: |
| 79 | + break |
| 80 | + recv_bytes += n |
| 81 | + for i in range(n): |
| 82 | + if buf[i] == random.randint(0, 255): |
| 83 | + recv_correct += 1 |
| 84 | + ticks_end = time.ticks_ms() |
| 85 | + return recv_bytes, recv_correct, time.ticks_diff(ticks_end, ticks_first_byte) |
| 86 | + |
| 87 | + |
| 88 | +# Acting in peripheral role. |
| 89 | +def instance0(): |
| 90 | + multitest.globals(BDADDR=ble.config("mac")) |
| 91 | + ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY") |
| 92 | + multitest.next() |
| 93 | + try: |
| 94 | + # Wait for central to connect to us. |
| 95 | + conn_handle = wait_for_event(_IRQ_CENTRAL_CONNECT, TIMEOUT_MS) |
| 96 | + |
| 97 | + ble.l2cap_listen(_L2CAP_PSM, _L2CAP_MTU) |
| 98 | + |
| 99 | + conn_handle, cid, psm = wait_for_event(_IRQ_L2CAP_ACCEPT, TIMEOUT_MS) |
| 100 | + conn_handle, cid, psm, our_mtu, peer_mtu = wait_for_event(_IRQ_L2CAP_CONNECT, TIMEOUT_MS) |
| 101 | + |
| 102 | + random.seed(_RANDOM_SEED) |
| 103 | + |
| 104 | + send_data(ble, conn_handle, cid) |
| 105 | + |
| 106 | + wait_for_event(_IRQ_L2CAP_DISCONNECT, TIMEOUT_MS) |
| 107 | + |
| 108 | + # Wait for the central to disconnect. |
| 109 | + wait_for_event(_IRQ_CENTRAL_DISCONNECT, TIMEOUT_MS) |
| 110 | + finally: |
| 111 | + ble.active(0) |
| 112 | + |
| 113 | + |
| 114 | +# Acting in central role. |
| 115 | +def instance1(): |
| 116 | + multitest.next() |
| 117 | + try: |
| 118 | + # Connect to peripheral and then disconnect. |
| 119 | + ble.gap_connect(*BDADDR) |
| 120 | + conn_handle = wait_for_event(_IRQ_PERIPHERAL_CONNECT, TIMEOUT_MS) |
| 121 | + |
| 122 | + ble.l2cap_connect(conn_handle, _L2CAP_PSM, _L2CAP_MTU) |
| 123 | + conn_handle, cid, psm, our_mtu, peer_mtu = wait_for_event(_IRQ_L2CAP_CONNECT, TIMEOUT_MS) |
| 124 | + |
| 125 | + random.seed(_RANDOM_SEED) |
| 126 | + |
| 127 | + recv_bytes, recv_correct, total_ticks = recv_data(ble, conn_handle, cid) |
| 128 | + |
| 129 | + # Disconnect channel. |
| 130 | + ble.l2cap_disconnect(conn_handle, cid) |
| 131 | + wait_for_event(_IRQ_L2CAP_DISCONNECT, TIMEOUT_MS) |
| 132 | + |
| 133 | + print( |
| 134 | + "Received {}/{} bytes in {} ms. {} B/s".format( |
| 135 | + recv_bytes, recv_correct, total_ticks, recv_bytes * 1000 // total_ticks |
| 136 | + ) |
| 137 | + ) |
| 138 | + |
| 139 | + # Disconnect from peripheral. |
| 140 | + ble.gap_disconnect(conn_handle) |
| 141 | + wait_for_event(_IRQ_PERIPHERAL_DISCONNECT, TIMEOUT_MS) |
| 142 | + finally: |
| 143 | + ble.active(0) |
| 144 | + |
| 145 | + |
| 146 | +ble = bluetooth.BLE() |
| 147 | +ble.active(1) |
| 148 | +ble.irq(irq) |
0 commit comments