8000 extmod/modbluetooth: Change scan result's "connectable" to "adv_type". · larsks/micropython@dd0bc26 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd0bc26

Browse files
committed
extmod/modbluetooth: Change scan result's "connectable" to "adv_type".
This commit changes the BLE _IRQ_SCAN_RESULT data from: addr_type, addr, connectable, rssi, adv_data to: addr_type, addr, adv_type, rssi, adv_data This allows _IRQ_SCAN_RESULT to handle all scan result types (not just connectable and non-connectable passive scans), and to distinguish between them using adv_type which is an integer taking values 0x00-0x04 per the BT specification. This is a breaking change to the API, albeit a very minor one: the existing connectable value was a boolean and True now becomes 0x00, False becomes 0x02. Documentation is updated and a test added. Fixes micropython#5738.
1 parent bd746a4 commit dd0bc26

File tree

7 files changed

+55
-42
lines changed

7 files changed

+55
-42
lines changed

docs/library/ubluetooth.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Event Handling
9393
conn_handle, attr_handle = data
9494
elif event == _IRQ_SCAN_RESULT:
9595
# A single scan result.
96-
addr_type, addr, connectable, rssi, adv_data = data
96+
addr_type, addr, adv_type, rssi, adv_data = data
9797
elif event == _IRQ_SCAN_COMPLETE:
9898
# Scan duration finished or manually stopped.
9999
pass
@@ -185,7 +185,15 @@ Observer Role (Scanner)
185185
interval and window are 1.28 seconds and 11.25 milliseconds respectively
186186
(background scanning).
187187

188-
For each scan result, the ``_IRQ_SCAN_RESULT`` event will be raised.
188+
For each scan result the ``_IRQ_SCAN_RESULT`` event will be raised, with event
189+
data ``(addr_type, addr, adv_type, rssi, adv_data)``. ``adv_type`` values correspond
190+
to the Bluetooth Specification:
191+
192+
* 0x00 - ADV_IND - connectable and scannable undirected advertising
193+
* 0x01 - ADV_DIRECT_IND - connectable directed advertising
194+
* 0x02 - ADV_SCAN_IND - scannable undirected advertising
195+
* 0x03 - ADV_NONCONN_IND - non-connectable undirected advertising
196+
* 0x04 - SCAN_RSP - scan response
189197

190198
When scanning is stopped (either due to the duration finishing or when
191199
explicitly stopped), the ``_IRQ_SCAN_COMPLETE`` event will be raised.

extmod/btstack/modbluetooth_btstack.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ STATIC void btstack_packet_handler(uint8_t packet_type, uint16_t channel, uint8_
115115
int8_t rssi = gap_event_advertising_report_get_rssi(packet);
116116
uint8_t length = gap_event_advertising_report_get_data_length(packet);
117117
const uint8_t *data = gap_event_advertising_report_get_data(packet);
118-
bool connectable = adv_event_type == 0 || adv_event_type == 1;
119-
if (adv_event_type <= 2) {
120-
mp_bluetooth_gap_on_scan_result(address_type, address, connectable, rssi, data, length);
121-
} else if (adv_event_type == 4) {
122-
// TODO: Scan response.
118+
// Emit an event for all advertising types except SCAN_RSP.
119+
if (adv_event_type < 4) {
120+
mp_bluetooth_gap_on_scan_result(address_type, address, adv_event_type, rssi, data, length);
123121
}
124122
} else if (event_type == HCI_EVENT_DISCONNECTION_COMPLETE) {
125123
DEBUG_EVENT_printf(" --> hci disconnect complete\n");

extmod/modbluetooth.c

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -774,8 +774,8 @@ const mp_obj_module_t mp_module_ubluetooth = {
774774

775775
#include <stdio.h>
776776

777-
STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size_t n_u16, size_t n_u8, mp_obj_str_t *bytes_addr, size_t n_b, size_t n_i8, mp_obj_bluetooth_uuid_t *uuid, mp_obj_str_t *bytes_data) {
778-
assert(ringbuf_avail(ringbuf) >= n_u16 * 2 + n_u8 + (bytes_addr ? 6 : 0) + n_b + n_i8 + (uuid ? 1 : 0) + (bytes_data ? 1 : 0));
777+
STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size_t n_u16, size_t n_u8, mp_obj_str_t *bytes_addr, size_t n_i8, mp_obj_bluetooth_uuid_t *uuid, mp_obj_str_t *bytes_data) {
778+
assert(ringbuf_avail(ringbuf) >= n_u16 * 2 + n_u8 + (bytes_addr ? 6 : 0) + n_i8 + (uuid ? 1 : 0) + (bytes_data ? 1 : 0));
779779
int j = 0;
780780

781781
for (int i = 0; i < n_u16; ++ A3E2 i) {
@@ -792,10 +792,7 @@ STATIC void ringbuf_extract(ringbuf_t *ringbuf, mp_obj_tuple_t *data_tuple, size
792792
}
793793
data_tuple->items[j++] = MP_OBJ_FROM_PTR(bytes_addr);
794794
}
795-
if (n_b) {
796-
data_tuple->items[j++] = mp_obj_new_bool(ringbuf_get(ringbuf));
797-
}
798-
if (n_i8) {
795+
for (int i = 0; i < n_i8; ++i) {
799796
// Note the int8_t got packed into the ringbuf as a uint8_t.
800797
data_tuple->items[j++] = MP_OBJ_NEW_SMALL_INT((int8_t)ringbuf_get(ringbuf));
801798
}
@@ -843,32 +840,32 @@ STATIC mp_obj_t bluetooth_ble_invoke_irq(mp_obj_t none_in) {
843840

844841
if (event == MP_BLUETOOTH_IRQ_CENTRAL_CONNECT || event == MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT || event == MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT || event == MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT) {
845842
// conn_handle, addr_type, addr
846-
ringbuf_extract(&o->ringbuf, data_tuple, 1, 1, &o->irq_data_addr, 0, 0, NULL, NULL);
843+
ringbuf_extract(&o->ringbuf, data_tuple, 1, 1, &o->irq_data_addr, 0, NULL, NULL);
847844
} else if (event == MP_BLUETOOTH_IRQ_GATTS_WRITE) {
848845
// conn_handle, value_handle
849-
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, 0, NULL, NULL);
846+
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, NULL);
850847
#if MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
851848
} else if (event == MP_BLUETOOTH_IRQ_SCAN_RESULT) {
852-
// addr_type, addr, connectable, rssi, adv_data
853-
ringbuf_extract(&o->ringbuf, data_tuple, 0, 1, &o->irq_data_addr, 1, 1, NULL, &o->irq_data_data);
849+
// addr_type, addr, adv_type, rssi, adv_data
850+
ringbuf_extract(&o->ringbuf, data_tuple, 0, 1, &o->irq_data_addr, 2, NULL, &o->irq_data_data);
854851
} else if (event == MP_BLUETOOTH_IRQ_SCAN_COMPLETE) {
855852
// No params required.
856853
data_tuple->len = 0;
857854
} else if (event == MP_BLUETOOTH_IRQ_GATTC_SERVICE_RESULT) {
858855
// conn_handle, start_handle, end_handle, uuid
859-
ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, 0, &o->irq_data_uuid, NULL);
856+
ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, &o->irq_data_uuid, NULL);
860857
} else if (event == MP_BLUETOOTH_IRQ_GATTC_CHARACTERISTIC_RESULT) {
861858
// conn_handle, def_handle, value_handle, properties, uuid
862-
ringbuf_extract(&o->ringbuf, data_tuple, 3, 1, NULL, 0, 0, &o->irq_data_uuid, NULL);
859+
ringbuf_extract(&o->ringbuf, data_tuple, 3, 1, NULL, 0, &o->irq_data_uuid, NULL);
863860
} else if (event == MP_BLUETOOTH_IRQ_GATTC_DESCRIPTOR_RESULT) {
864861
// conn_handle, handle, uuid
865-
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, 0, &o->irq_data_uuid, NULL);
862+
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, &o->irq_data_uuid, NULL);
866863
} else if (event == MP_BLUETOOTH_IRQ_GATTC_READ_RESULT || event == MP_BLUETOOTH_IRQ_GATTC_NOTIFY || event == MP_BLUETOOTH_IRQ_GATTC_INDICATE) {
867864
// conn_handle, value_handle, data
868-
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, 0, NULL, &o->irq_data_data);
865+
ringbuf_extract(&o->ringbuf, data_tuple, 2, 0, NULL, 0, NULL, &o->irq_data_data);
869866
} else if (event == MP_BLUETOOTH_IRQ_GATTC_WRITE_STATUS) {
870867
// conn_handle, value_handle, status
871-
ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, 0, NULL, NULL);
868+
ringbuf_extract(&o->ringbuf, data_tuple, 3, 0, NULL, 0, NULL, NULL);
872869
#endif // MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE
873870
}
874871

@@ -903,7 +900,7 @@ STATIC bool enqueue_irq(mp_obj_bluetooth_ble_t *o, size_t len, uint16_t event) {
903900

904901
// Front of the queue is a scan result, remove it.
905902

906-
// event, addr_type, addr, connectable, rssi
903+
// event, addr_type, addr, adv_type, rssi
907904
int n = 2 + 1 + 6 + 1 + 1;
908905
for (int i = 0; i < n; ++i) {
909906
ringbuf_get(&o->ringbuf);
@@ -963,7 +960,7 @@ void mp_bluetooth_gap_on_scan_complete(void) {
963960
MICROPY_PY_BLUETOOTH_EXIT
96 10000 4961
}
965962

966-
void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, bool connectable, const int8_t rssi, const uint8_t *data, size_t data_len) {
963+
void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len) {
967964
MICROPY_PY_BLUETOOTH_ENTER
968965
mp_obj_bluetooth_ble_t *o = MP_OBJ_TO_PTR(MP_STATE_VM(bluetooth));
969966
data_len = MIN(o->irq_data_data_alloc, data_len);
@@ -972,7 +969,8 @@ void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, boo
972969
for (int i = 0; i < 6; ++i) {
973970
ringbuf_put(&o->ringbuf, addr[i]);
974971
}
975-
ringbuf_put(&o->ringbuf, connectable ? 1 : 0);
972+
// The adv_type will get extracted as an int8_t but that's ok because valid values are 0x00-0x04.
973+
ringbuf_put(&o->ringbuf, adv_type);
976974
// Note conversion of int8_t rssi to uint8_t. Must un-convert on the way out.
977975
ringbuf_put(&o->ringbuf, (uint8_t)rssi);
978976
ringbuf_put(&o->ringbuf, data_len);

extmod/modbluetooth.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ bool mp_bluetooth_gatts_on_read_request(uint16_t conn_handle, uint16_t value_han
248248
void mp_bluetooth_gap_on_scan_complete(void);
249249

250250
// Notify modbluetooth of a scan result.
251-
void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, bool connectable, const int8_t rssi, const uint8_t *data, size_t data_len);
251+
void mp_bluetooth_gap_on_scan_result(uint8_t addr_type, const uint8_t *addr, uint8_t adv_type, const int8_t rssi, const uint8_t *data, size_t data_len);
252252

253253
// Notify modbluetooth that a service was found (either by discover-all, or discover-by-uuid).
254254
void mp_bluetooth_gattc_on_primary_service_result(uint16_t conn_handle, uint16_t start_handle, uint16_t end_handle, mp_obj_bluetooth_uuid_t *service_uuid);

extmod/nimble/modbluetooth_nimble.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -634,16 +634,9 @@ STATIC int gap_scan_cb(struct ble_gap_event *event, void *arg) {
634634
return 0;
635635
}
636636

637-
if (event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_ADV_IND || event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_NONCONN_IND) {
638-
bool connectable = event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_ADV_IND;
639-
uint8_t addr[6];
640-
reverse_addr_byte_order(addr, event->disc.addr.val);
641-
mp_bluetooth_gap_on_scan_result(event->disc.addr.type, addr, connectable, event->disc.rssi, event->disc.data, event->disc.length_data);
642-
} else if (event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_RSP) {
643-
// TODO
644-
} else if (event->disc.event_type == BLE_HCI_ADV_RPT_EVTYPE_SCAN_IND) {
645-
// TODO
646-
}
637+
uint8_t addr[6];
638+
reverse_addr_byte_order(addr, event->disc.addr.val);
639+
mp_bluetooth_gap_on_scan_result(event->disc.addr.type, addr, event->disc.event_type, event->disc.rssi, event->disc.data, event->disc.length_data);
647640

648641
return 0;
649642
}

tests/multi_bluetooth/ble_gap_advertise.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,48 @@
1111

1212
def instance0():
1313
multitest.globals(BDADDR=ble.config("mac"))
14-
print("gap_advertise(20_000)")
15-
ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY")
1614
multitest.next()
15+
16+
print("gap_advertise(100_000, connectable=False)")
17+
ble.gap_advertise(100_000, b"\x02\x01\x06\x04\xffMPY", connectable=False)
18+
time.sleep(ADV_TIME_S)
19+
20+
print("gap_advertise(20_000, connectable=True)")
21+
ble.gap_advertise(20_000, b"\x02\x01\x06\x04\xffMPY", connectable=True)
1722
time.sleep(ADV_TIME_S)
23+
1824
print("gap_advertise(None)")
1925
ble.gap_advertise(None)
26+
2027
ble.active(0)
2128

2229

2330
def instance1():
2431
multitest.next()
2532
finished = False
33+
adv_types = set()
2634
adv_data = None
2735

2836
def irq(ev, data):
29-
nonlocal finished, adv_data
37+
nonlocal finished, adv_types, adv_data
3038
if ev == _IRQ_SCAN_RESULT:
3139
if data[1] == BDADDR:
32-
adv_data = bytes(data[4])
40+
adv_types.add(data[2])
41+
if adv_data is None:
42+
adv_data = bytes(data[4])
43+
else:
44+
if adv_data != data[4]:
45+
adv_data = "MISMATCH"
3346
elif ev == _IRQ_SCAN_COMPLETE:
3447
finished = True
3548

3649
ble.config(rxbuf=2000)
3750
ble.irq(irq)
38-
ble.gap_scan(ADV_TIME_S * 1000, 10000, 10000)
51+
ble.gap_scan(2 * ADV_TIME_S * 1000, 10000, 10000)
3952
while not finished:
4053
machine.idle()
4154
ble.active(0)
55+
print("adv_types:", sorted(adv_types))
4256
print("adv_data:", adv_data)
4357

4458

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
--- instance0 ---
2-
gap_advertise(20_000)
2+
gap_advertise(100_000, connectable=False)
3+
gap_advertise(20_000, connectable=True)
34
gap_advertise(None)
45
--- instance1 ---
6+
adv_types: [0, 2]
57
adv_data: b'\x02\x01\x06\x04\xffMPY'

0 commit comments

Comments
 (0)
0