Skip to content

Commit f9a8d09

Browse files
committed
pbio/drv/bluetooth: Pass user object to scan filters.
This was still using part of the singleton code, but we need to generalize this for multi-peripheral support too. This was missed when upgrading the object handling for notifications.
1 parent fcfdf4e commit f9a8d09

File tree

6 files changed

+17
-24
lines changed

6 files changed

+17
-24
lines changed

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
432432
// Match advertisement data against context-specific filter.
433433
pbdrv_bluetooth_ad_match_result_flags_t adv_flags = PBDRV_BLUETOOTH_AD_MATCH_NONE;
434434
if (peri->config->match_adv) {
435-
adv_flags = peri->config->match_adv(event_type, data, NULL, address, peri->bdaddr);
435+
adv_flags = peri->config->match_adv(peri->user, event_type, data, NULL, address, peri->bdaddr);
436436
}
437437

438438
if (adv_flags & PBDRV_BLUETOOTH_AD_MATCH_VALUE) {
@@ -454,7 +454,7 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packe
454454

455455
pbdrv_bluetooth_ad_match_result_flags_t rsp_flags = PBDRV_BLUETOOTH_AD_MATCH_NONE;
456456
if (peri->config->match_adv_rsp) {
457-
rsp_flags = peri->config->match_adv_rsp(event_type, NULL, detected_name, address, peri->bdaddr);
457+
rsp_flags = peri->config->match_adv_rsp(peri->user, event_type, NULL, detected_name, address, peri->bdaddr);
458458
}
459459
if ((rsp_flags & PBDRV_BLUETOOTH_AD_MATCH_VALUE) && (rsp_flags & PBDRV_BLUETOOTH_AD_MATCH_ADDRESS)) {
460460

lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
372372
le_advertising_info *subevt = (void *)&read_buf[5];
373373

374374
// Context specific advertisement filter.
375-
pbdrv_bluetooth_ad_match_result_flags_t adv_flags = peri->config->match_adv(subevt->evt_type, subevt->data_RSSI, NULL, subevt->bdaddr, peri->bdaddr);
375+
pbdrv_bluetooth_ad_match_result_flags_t adv_flags = peri->config->match_adv(peri->user, subevt->evt_type, subevt->data_RSSI, NULL, subevt->bdaddr, peri->bdaddr);
376376

377377
// If it doesn't match context-specific filter, keep scanning.
378378
if (!(adv_flags & PBDRV_BLUETOOTH_AD_MATCH_VALUE)) {
@@ -409,7 +409,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
409409
// If the response data is not right or if the address doesn't match advertisement, keep scanning.
410410
le_advertising_info *subevt = (void *)&read_buf[5];
411411
const char *detected_name = (char *)&subevt->data_RSSI[2];
412-
pbdrv_bluetooth_ad_match_result_flags_t rsp_flags = peri->config->match_adv_rsp(subevt->evt_type, NULL, detected_name, subevt->bdaddr, peri->bdaddr);
412+
pbdrv_bluetooth_ad_match_result_flags_t rsp_flags = peri->config->match_adv_rsp(peri->user, subevt->evt_type, NULL, detected_name, subevt->bdaddr, peri->bdaddr);
413413
if (!(rsp_flags & PBDRV_BLUETOOTH_AD_MATCH_VALUE) || !(rsp_flags & PBDRV_BLUETOOTH_AD_MATCH_ADDRESS)) {
414414
continue;
415415
}

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
448448
}
449449

450450
// Context specific advertisement filter.
451-
pbdrv_bluetooth_ad_match_result_flags_t adv_flags = peri->config->match_adv(read_buf[9], &read_buf[19], NULL, &read_buf[11], peri->bdaddr);
451+
pbdrv_bluetooth_ad_match_result_flags_t adv_flags = peri->config->match_adv(peri->user, read_buf[9], &read_buf[19], NULL, &read_buf[11], peri->bdaddr);
452452

453453
// If it doesn't match context-specific filter, keep scanning.
454454
if (!(adv_flags & PBDRV_BLUETOOTH_AD_MATCH_VALUE)) {
@@ -495,7 +495,7 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
495495

496496
const char *detected_name = (const char *)&read_buf[21];
497497
const uint8_t *response_address = &read_buf[11];
498-
pbdrv_bluetooth_ad_match_result_flags_t rsp_flags = peri->config->match_adv_rsp(read_buf[9], NULL, detected_name, response_address, peri->bdaddr);
498+
pbdrv_bluetooth_ad_match_result_flags_t rsp_flags = peri->config->match_adv_rsp(peri->user, read_buf[9], NULL, detected_name, response_address, peri->bdaddr);
499499

500500
// If the response data is not right or if the address doesn't match advertisement, keep scanning.
501501
if (!(rsp_flags & PBDRV_BLUETOOTH_AD_MATCH_VALUE) || !(rsp_flags & PBDRV_BLUETOOTH_AD_MATCH_ADDRESS)) {

lib/pbio/include/pbdrv/bluetooth.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ typedef enum {
6565
/**
6666
* Callback to match an advertisement or scan response.
6767
*
68+
* @param [in] user The user of this peripheral, usually a high-level object.
6869
* @param [in] event_type The type of advertisement.
6970
* @param [in] data The advertisement data.
7071
* @param [in] name The name to match. If NULL, no name filter is applied.
@@ -73,7 +74,7 @@ typedef enum {
7374
* @return True if the advertisement matches, false otherwise.
7475
*/
7576
typedef pbdrv_bluetooth_ad_match_result_flags_t (*pbdrv_bluetooth_ad_match_t)
76-
(uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr);
77+
(void *user, uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr);
7778

7879
struct _pbdrv_bluetooth_send_context_t {
7980
/** Callback that is called when the data has been sent. */
@@ -137,7 +138,7 @@ typedef struct _pbdrv_bluetooth_peripheral_t pbdrv_bluetooth_peripheral_t;
137138
/**
138139
* Callback that is called when a peripheral sends a notification.
139140
*
140-
* @param [in] user The addressee, usually a high-level object.
141+
* @param [in] user The user of this peripheral, usually a high-level object.
141142
* @param [in] data The data that was received.
142143
* @param [in] size The size of @p data in bytes.
143144
*/

pybricks/iodevices/pb_type_iodevices_lwp3device.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ static pbdrv_bluetooth_peripheral_char_t pb_lwp3device_char = {
8585
.request_notification = true,
8686
};
8787

88-
// Needed for global notification callback. This is cleared when the finalizer
89-
// runs.
90-
static mp_obj_t self_obj;
91-
9288
typedef struct {
9389
mp_obj_base_t base;
9490
/**
@@ -172,16 +168,15 @@ static void handle_remote_notification(void *user, const uint8_t *value, uint32_
172168
}
173169
}
174170

175-
static pbdrv_bluetooth_ad_match_result_flags_t lwp3_advertisement_matches(uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
171+
static pbdrv_bluetooth_ad_match_result_flags_t lwp3_advertisement_matches(void *user, uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
176172

177173
pbdrv_bluetooth_ad_match_result_flags_t flags = PBDRV_BLUETOOTH_AD_MATCH_NONE;
178174

179-
if (self_obj == MP_OBJ_NULL) {
175+
pb_lwp3device_obj_t *self = user;
176+
if (!self) {
180177
return flags;
181178
}
182179

183-
pb_lwp3device_obj_t *self = MP_OBJ_TO_PTR(self_obj);
184-
185180
// Whether this looks like a LWP3 advertisement of the correct hub kind.
186181
if (event_type == PBDRV_BLUETOOTH_AD_TYPE_ADV_IND
187182
&& data[3] == 17 /* length */
@@ -199,16 +194,15 @@ static pbdrv_bluetooth_ad_match_result_flags_t lwp3_advertisement_matches(uint8_
199194
return flags;
200195
}
201196

202-
static pbdrv_bluetooth_ad_match_result_flags_t lwp3_advertisement_response_matches(uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
197+
static pbdrv_bluetooth_ad_match_result_flags_t lwp3_advertisement_response_matches(void *user, uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
203198

204199
pbdrv_bluetooth_ad_match_result_flags_t flags = PBDRV_BLUETOOTH_AD_MATCH_NONE;
205200

206-
if (self_obj == MP_OBJ_NULL) {
201+
pb_lwp3device_obj_t *self = user;
202+
if (!self) {
207203
return flags;
208204
}
209205

210-
pb_lwp3device_obj_t *self = MP_OBJ_TO_PTR(self_obj);
211-
212206
// This is the only value check we do on LWP3 response messages.
213207
if (event_type == PBDRV_BLUETOOTH_AD_TYPE_SCAN_RSP) {
214208
flags |= PBDRV_BLUETOOTH_AD_MATCH_VALUE;
@@ -493,7 +487,6 @@ static mp_obj_t pb_type_pupdevices_Remote_make_new(const mp_obj_type_t *type, si
493487
pb_module_tools_assert_blocking();
494488

495489
pb_lwp3device_obj_t *self = mp_obj_malloc_with_finaliser(pb_lwp3device_obj_t, type);
496-
self_obj = MP_OBJ_FROM_PTR(self);
497490

498491
self->buttons = pb_type_Keypad_obj_new(MP_OBJ_FROM_PTR(self), pb_type_remote_button_pressed);
499492
self->light = pb_type_ColorLight_external_obj_new(MP_OBJ_FROM_PTR(self), pb_type_pupdevices_Remote_light_on);
@@ -624,7 +617,6 @@ static mp_obj_t pb_type_iodevices_LWP3Device_make_new(const mp_obj_type_t *type,
624617
}
625618

626619
pb_lwp3device_obj_t *self = mp_obj_malloc_var_with_finaliser(pb_lwp3device_obj_t, uint8_t, LWP3_MAX_MESSAGE_SIZE * noti_num, type);
627-
self_obj = MP_OBJ_FROM_PTR(self);
628620

629621
memset(self->notification_buffer, 0, LWP3_MAX_MESSAGE_SIZE * noti_num);
630622
self->noti_num = noti_num;

pybricks/iodevices/pb_type_iodevices_xbox_controller.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static void handle_notification(void *user, const uint8_t *value, uint32_t size)
114114

115115
#define _16BIT_AS_LE(x) ((x) & 0xff), (((x) >> 8) & 0xff)
116116

117-
static pbdrv_bluetooth_ad_match_result_flags_t xbox_advertisement_matches(uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
117+
static pbdrv_bluetooth_ad_match_result_flags_t xbox_advertisement_matches(void *user, uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
118118

119119
// The controller seems to advertise three different packets, so allow all.
120120

@@ -165,7 +165,7 @@ static pbdrv_bluetooth_ad_match_result_flags_t xbox_advertisement_matches(uint8_
165165
return flags;
166166
}
167167

168-
static pbdrv_bluetooth_ad_match_result_flags_t xbox_advertisement_response_matches(uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
168+
static pbdrv_bluetooth_ad_match_result_flags_t xbox_advertisement_response_matches(void *user, uint8_t event_type, const uint8_t *data, const char *name, const uint8_t *addr, const uint8_t *match_addr) {
169169

170170
pbdrv_bluetooth_ad_match_result_flags_t flags = PBDRV_BLUETOOTH_AD_MATCH_NONE;
171171

0 commit comments

Comments
 (0)