Skip to content

Commit 7390dc7

Browse files
committed
bleio: Move ScanEntry to shared module and add a new AdvertisementData class
1 parent 345334a commit 7390dc7

File tree

14 files changed

+592
-229
lines changed

14 files changed

+592
-229
lines changed

ports/nrf/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\
135135
ubluepy/ubluepy_delegate.c \
136136
ubluepy/ubluepy_constants.c \
137137
ubluepy/ubluepy_scanner.c \
138-
ubluepy/ubluepy_scan_entry.c \
139138
)
140139

141140
SRC_COMMON_HAL += \
@@ -187,6 +186,8 @@ ifneq ($(SD), )
187186
SRC_BINDINGS_ENUMS += \
188187
bleio/Address.c \
189188
bleio/AddressType.c \
189+
bleio/AdvertisementData.c \
190+
bleio/ScanEntry.c \
190191
bleio/UUIDType.c
191192
endif
192193

ports/nrf/drivers/bluetooth/ble_drv.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,18 @@ void ble_drv_scan_start(void) {
785785
}
786786
}
787787

788+
void ble_drv_scan_continue(void) {
789+
SD_TEST_OR_ENABLE();
790+
791+
#if (BLUETOOTH_SD == 140)
792+
uint32_t err_code;
793+
if ((err_code = sd_ble_gap_scan_start(NULL, &m_scan_buffer)) != 0) {
794+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError,
795+
"Can not continue scanning. status: 0x" HEX2_FMT, (uint16_t)err_code));
796+
}
797+
#endif
798+
}
799+
788800
void ble_drv_scan_stop(void) {
789801
sd_ble_gap_scan_stop();
790802
}

ports/nrf/drivers/bluetooth/ble_drv.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ void ble_drv_attr_c_write(uint16_t conn_handle, uint16_t handle, uint16_t len, u
108108

109109
void ble_drv_scan_start(void);
110110

111+
void ble_drv_scan_continue(void);
112+
111113
void ble_drv_scan_stop(void);
112114

113115
void ble_drv_adv_report_handler_set(mp_obj_t obj, ble_drv_adv_evt_callback_t evt_handler);

ports/nrf/modules/ubluepy/modubluepy.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ extern const mp_obj_type_t ubluepy_characteristic_type;
3434
extern const mp_obj_type_t ubluepy_delegate_type;
3535
extern const mp_obj_type_t ubluepy_constants_type;
3636
extern const mp_obj_type_t ubluepy_scanner_type;
37-
extern const mp_obj_type_t ubluepy_scan_entry_type;
3837

3938
STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
4039
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_ubluepy) },
@@ -46,7 +45,6 @@ STATIC const mp_rom_map_elem_t mp_module_ubluepy_globals_table[] = {
4645
#endif
4746
#if MICROPY_PY_UBLUEPY_CENTRAL
4847
{ MP_ROM_QSTR(MP_QSTR_Scanner), MP_ROM_PTR(&ubluepy_scanner_type) },
49-
{ MP_ROM_QSTR(MP_QSTR_ScanEntry), MP_ROM_PTR(&ubluepy_scan_entry_type) },
5048
#endif
5149
{ MP_ROM_QSTR(MP_QSTR_DefaultDelegate), MP_ROM_PTR(&ubluepy_delegate_type) },
5250
{ MP_ROM_QSTR(MP_QSTR_Service), MP_ROM_PTR(&ubluepy_service_type) },

ports/nrf/modules/ubluepy/modubluepy.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ extern const mp_obj_type_t ubluepy_service_type;
7878
extern const mp_obj_type_t ubluepy_characteristic_type;
7979
extern const mp_obj_type_t ubluepy_peripheral_type;
8080
extern const mp_obj_type_t ubluepy_scanner_type;
81-
extern const mp_obj_type_t ubluepy_scan_entry_type;
8281
extern const mp_obj_type_t ubluepy_constants_type;
8382
extern const mp_obj_type_t ubluepy_constants_ad_types_type;
8483

@@ -153,15 +152,6 @@ typedef struct _ubluepy_scanner_obj_t {
153152
mp_obj_t adv_reports;
154153
} ubluepy_scanner_obj_t;
155154

156-
typedef struct _ubluepy_scan_entry_obj_t {
157-
mp_obj_base_t base;
158-
mp_obj_t addr;
159-
uint8_t addr_type;
160-
bool connectable;
161-
int8_t rssi;
162-
mp_obj_t data;
163-
} ubluepy_scan_entry_obj_t;
164-
165155
typedef enum _ubluepy_prop_t {
166156
UBLUEPY_PROP_BROADCAST = 0x01,
167157
UBLUEPY_PROP_READ = 0x02,

ports/nrf/modules/ubluepy/ubluepy_constants.c

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -31,58 +31,11 @@
3131

3232
#include "modubluepy.h"
3333

34-
STATIC const mp_rom_map_elem_t ubluepy_constants_ad_types_locals_dict_table[] = {
35-
// GAP AD Types
36-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_FLAGS), MP_ROM_INT(0x01) },
37-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE), MP_ROM_INT(0x02) },
38-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE), MP_ROM_INT(0x03) },
39-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_32BIT_SERVICE_UUID_MORE_AVAILABLE), MP_ROM_INT(0x04) },
40-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_32BIT_SERVICE_UUID_COMPLETE), MP_ROM_INT(0x05) },
41-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE), MP_ROM_INT(0x06) },
42-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE), MP_ROM_INT(0x07) },
43-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SHORT_LOCAL_NAME), MP_ROM_INT(0x08) },
44-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_COMPLETE_LOCAL_NAME), MP_ROM_INT(0x09) },
45-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_TX_POWER_LEVEL), MP_ROM_INT(0x0A) },
46-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_CLASS_OF_DEVICE), MP_ROM_INT(0x0D) },
47-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SIMPLE_PAIRING_HASH_C), MP_ROM_INT(0x0E) },
48-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SIMPLE_PAIRING_RANDOMIZER_R), MP_ROM_INT(0x0F) },
49-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SECURITY_MANAGER_TK_VALUE), MP_ROM_INT(0x10) },
50-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SECURITY_MANAGER_OOB_FLAGS), MP_ROM_INT(0x11) },
51-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SLAVE_CONNECTION_INTERVAL_RANGE), MP_ROM_INT(0x12) },
52-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SOLICITED_SERVICE_UUIDS_16BIT), MP_ROM_INT(0x14) },
53-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SOLICITED_SERVICE_UUIDS_128BIT), MP_ROM_INT(0x15) },
54-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SERVICE_DATA), MP_ROM_INT(0x16) },
55-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_PUBLIC_TARGET_ADDRESS), MP_ROM_INT(0x17) },
56-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_RANDOM_TARGET_ADDRESS), MP_ROM_INT(0x18) },
57-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_APPEARANCE), MP_ROM_INT(0x19) },
58-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_ADVERTISING_INTERVAL), MP_ROM_INT(0x1A) },
59-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_LE_BLUETOOTH_DEVICE_ADDRESS), MP_ROM_INT(0x1B) },
60-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_LE_ROLE), MP_ROM_INT(0x1C) },
61-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SIMPLE_PAIRING_HASH_C256), MP_ROM_INT(0x1D) },
62-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SIMPLE_PAIRING_RANDOMIZER_R256), MP_ROM_INT(0x1E) },
63-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SERVICE_DATA_32BIT_UUID), MP_ROM_INT(0x20) },
64-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_SERVICE_DATA_128BIT_UUID), MP_ROM_INT(0x21) },
65-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_URI), MP_ROM_INT(0x24) },
66-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_3D_INFORMATION_DATA), MP_ROM_INT(0x3D) },
67-
{ MP_ROM_QSTR(MP_QSTR_AD_TYPE_MANUFACTURER_SPECIFIC_DATA), MP_ROM_INT(0xFF) },
68-
};
69-
70-
STATIC MP_DEFINE_CONST_DICT(ubluepy_constants_ad_types_locals_dict, ubluepy_constants_ad_types_locals_dict_table);
71-
72-
const mp_obj_type_t ubluepy_constants_ad_types_type = {
73-
{ &mp_type_type },
74-
.name = MP_QSTR_ad_types,
75-
.locals_dict = (mp_obj_dict_t*)&ubluepy_constants_ad_types_locals_dict
76-
};
77-
7834
STATIC const mp_rom_map_elem_t ubluepy_constants_locals_dict_table[] = {
7935
// GAP events
8036
{ MP_ROM_QSTR(MP_QSTR_EVT_GAP_CONNECTED), MP_ROM_INT(16) },
8137
{ MP_ROM_QSTR(MP_QSTR_EVT_GAP_DISCONNECTED), MP_ROM_INT(17) },
8238
{ MP_ROM_QSTR(MP_QSTR_EVT_GATTS_WRITE), MP_ROM_INT(80) },
83-
{ MP_ROM_QSTR(MP_QSTR_UUID_CCCD), MP_ROM_INT(0x2902) },
84-
85-
{ MP_ROM_QSTR(MP_QSTR_ad_types), MP_ROM_PTR(&ubluepy_constants_ad_types_type) },
8639
};
8740

8841
STATIC MP_DEFINE_CONST_DICT(ubluepy_constants_locals_dict, ubluepy_constants_locals_dict_table);

ports/nrf/modules/ubluepy/ubluepy_scan_entry.c

Lines changed: 0 additions & 146 deletions
This file was deleted.

ports/nrf/modules/ubluepy/ubluepy_scanner.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,25 @@
3333

3434
#if MICROPY_PY_UBLUEPY_CENTRAL
3535

36+
#include "shared-bindings/bleio/ScanEntry.h"
3637
#include "ble_drv.h"
3738

3839
STATIC void adv_event_handler(mp_obj_t self_in, uint16_t event_id, ble_drv_adv_data_t * data) {
3940
ubluepy_scanner_obj_t *self = MP_OBJ_TO_PTR(self_in);
4041

41-
ubluepy_scan_entry_obj_t * item = m_new_obj(ubluepy_scan_entry_obj_t);
42-
item->base.type = &ubluepy_scan_entry_type;
42+
// TODO: Don't add new entry for each item, group by address and update
43+
bleio_scanentry_obj_t *item = m_new_obj(bleio_scanentry_obj_t);
44+
item->base.type = &bleio_scanentry_type;
4345

44-
vstr_t vstr;
45-
vstr_init(&vstr, 17);
46+
item->rssi = data->rssi;
47+
item->data = mp_obj_new_bytearray(data->data_len, data->p_data);
4648

47-
vstr_printf(&vstr, ""HEX2_FMT":"HEX2_FMT":"HEX2_FMT":" \
48-
HEX2_FMT":"HEX2_FMT":"HEX2_FMT"",
49-
data->p_peer_addr[5], data->p_peer_addr[4], data->p_peer_addr[3],
50-
data->p_peer_addr[2], data->p_peer_addr[1], data->p_peer_addr[0]);
51-
52-
item->addr = mp_obj_new_str(vstr.buf, vstr.len);
53-
54-
vstr_clear(&vstr);
55-
56-
item->addr_type = data->addr_type;
57-
item->rssi = data->rssi;
58-
item->data = mp_obj_new_bytearray(data->data_len, data->p_data);
49+
item->address.type = data->addr_type;
50+
memcpy(item->address.value, data->p_peer_addr, BLEIO_ADDRESS_BYTES);
5951

6052
mp_obj_list_append(self->adv_reports, item);
53+
54+
ble_drv_scan_continue();
6155
}
6256

6357
STATIC void ubluepy_scanner_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {

0 commit comments

Comments
 (0)