Skip to content

Commit a77b236

Browse files
committed
evt handler list bugs; unique evt handler names; remove uuid128_reference
1 parent 8dea6f5 commit a77b236

5 files changed

Lines changed: 26 additions & 46 deletions

File tree

ports/nrf/bluetooth/ble_drv.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,27 +53,22 @@ void ble_drv_reset() {
5353
}
5454

5555
void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param) {
56-
event_handler_t *handler = m_new_ll(event_handler_t, 1);
57-
handler->next = NULL;
58-
handler->param = param;
59-
handler->func = func;
60-
61-
if (m_event_handlers == NULL) {
62-
m_event_handlers = handler;
63-
return;
64-
}
65-
6656
event_handler_t *it = m_event_handlers;
67-
while (it->next != NULL) {
57+
while (it != NULL) {
58+
// If event handler and its corresponding param are already on the list, don't add again.
6859
if ((it->func == func) && (it->param == param)) {
69-
m_free(handler);
7060
return;
7161
}
72-
7362
it = it->next;
7463
}
7564

76-
it->next = handler;
65+
// Add a new handler to the front of the list
66+
event_handler_t *handler = m_new_ll(event_handler_t, 1);
67+
handler->next = m_event_handlers;
68+
handler->param = param;
69+
handler->func = func;
70+
71+
m_event_handlers = handler;
7772
}
7873

7974
void SD_EVT_IRQHandler(void) {

ports/nrf/common-hal/bleio/Characteristic.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ STATIC uint16_t get_cccd(bleio_characteristic_obj_t *characteristic) {
5555
// CCCD is not set, so say that neither Notify nor Indicate is enabled.
5656
cccd = 0;
5757
} else if (err_code != NRF_SUCCESS) {
58-
mp_raise_OSError_msg_varg(translate("Failed to read CCD value, err 0x%04x"), err_code);
58+
mp_raise_OSError_msg_varg(translate("Failed to read CCCD value, err 0x%04x"), err_code);
5959
}
6060

6161
return cccd;
@@ -126,7 +126,7 @@ STATIC void gatts_notify_indicate(bleio_characteristic_obj_t *characteristic, mp
126126
const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device);
127127
const uint32_t err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
128128
if (err_code != NRF_SUCCESS) {
129-
mp_raise_OSError_msg_varg(translate("Failed to notify attribute value, err %0x04x"), err_code);
129+
mp_raise_OSError_msg_varg(translate("Failed to notify or indicate attribute value, err %0x04x"), err_code);
130130
}
131131

132132
m_tx_in_progress += 1;
@@ -188,7 +188,7 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in
188188
}
189189
}
190190

191-
STATIC void on_ble_evt(ble_evt_t *ble_evt, void *param) {
191+
STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) {
192192
switch (ble_evt->header.evt_id) {
193193
case BLE_GATTS_EVT_HVN_TX_COMPLETE:
194194
m_tx_in_progress -= ble_evt->evt.gatts_evt.params.hvn_tx_complete.count;
@@ -211,7 +211,7 @@ STATIC void on_ble_evt(ble_evt_t *ble_evt, void *param) {
211211
}
212212

213213
void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self) {
214-
ble_drv_add_event_handler(on_ble_evt, NULL);
214+
ble_drv_add_event_handler(characteristic_on_ble_evt, NULL);
215215
}
216216

217217
void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) {

ports/nrf/common-hal/bleio/Peripheral.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ STATIC uint32_t set_advertisement_data(bleio_peripheral_obj_t *self, bool connec
221221
return err_code;
222222
}
223223

224-
STATIC void on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
224+
STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) {
225225
bleio_peripheral_obj_t *self = (bleio_peripheral_obj_t*)self_in;
226226

227227
switch (ble_evt->header.evt_id) {
@@ -311,7 +311,7 @@ bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self) {
311311

312312
void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *self, bool connectable, mp_buffer_info_t *raw_data) {
313313
if (connectable) {
314-
ble_drv_add_event_handler(on_ble_evt, self);
314+
ble_drv_add_event_handler(peripheral_on_ble_evt, self);
315315
}
316316

317317
const uint32_t err_code = set_advertisement_data(self, connectable, raw_data);

shared-bindings/bleio/Characteristic.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,21 @@
3535
//| :class:`Characteristic` -- BLE service characteristic
3636
//| =========================================================
3737
//|
38-
//| Stores information about a BLE service characteristic and allows to read
39-
//| and write the characteristic's value.
38+
//| Stores information about a BLE service characteristic and allows reading
39+
//| and writing of the characteristic's value.
4040
//|
4141
//|
4242
//| .. class:: Characteristic(uuid, *, broadcast=False, indicate=False, notify=False, read=False, write=False, write_no_response=False)
4343
//|
4444
//| Create a new Characteristic object identified by the specified UUID.
4545
//|
46-
//| :param bleio.UUID uuid: The uuid of the characteristic (read_only)
46+
//| :param bleio.UUID uuid: The uuid of the characteristic
47+
//| :param bool broadcast: Allowed in advertising packets
48+
//| :param bool indicate: Server will indicate to the client when the value is set and wait for a response
49+
//| :param bool notify: Server will notify the client when the value is set
50+
//| :param bool read: Clients may read this characteristic
51+
//| :param bool write: Clients may write this characteristic; a response will be sent back
52+
//| :param bool write_no_response: Clients may write this characteristic; no response will be sent back
4753
//|
4854
STATIC mp_obj_t bleio_characteristic_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
4955
mp_arg_check_num(n_args, n_kw, 1, 1, true);

shared-bindings/bleio/UUID.c

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ const mp_obj_property_t bleio_uuid_uuid16_obj = {
109109

110110
//| .. attribute:: uuid128
111111
//|
112-
//| The 128-bit value of the UUID, return as a bytes().
113-
//| Throws AttributeError if this is a 16-bit UUID. (read-only)
112+
//| The 128-bit value of the UUID, returned as bytes.
113+
//| Raises AttributeError if this is a 16-bit UUID. (read-only)
114114
//|
115115
STATIC mp_obj_t bleio_uuid_get_uuid128(mp_obj_t self_in) {
116116
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
@@ -131,26 +131,6 @@ const mp_obj_property_t bleio_uuid_uuid128_obj = {
131131
(mp_obj_t)&mp_const_none_obj},
132132
};
133133

134-
//| .. attribute:: uuid128_reference
135-
//|
136-
//| An opaque reference representing the 128-bit UUID. (read-only)
137-
//| Returns None if this is a 16-bit UUID.
138-
//|
139-
STATIC mp_obj_t bleio_uuid_get_uuid128_reference(mp_obj_t self_in) {
140-
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in);
141-
uint32_t reference = common_hal_bleio_uuid_get_uuid128_reference(self);
142-
return reference == 0 ? mp_const_none : MP_OBJ_NEW_SMALL_INT(reference);
143-
}
144-
145-
MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_uuid128_reference_obj, bleio_uuid_get_uuid128_reference);
146-
147-
const mp_obj_property_t bleio_uuid_uuid128_reference_obj = {
148-
.base.type = &mp_type_property,
149-
.proxy = {(mp_obj_t)&bleio_uuid_get_uuid128_reference_obj,
150-
(mp_obj_t)&mp_const_none_obj,
151-
(mp_obj_t)&mp_const_none_obj},
152-
};
153-
154134
//| .. attribute:: size
155135
//|
156136
//| Returns 128 if this UUID represents a 128-bit vendor-specific UUID.
@@ -174,7 +154,6 @@ const mp_obj_property_t bleio_uuid_size_obj = {
174154
STATIC const mp_rom_map_elem_t bleio_uuid_locals_dict_table[] = {
175155
{ MP_ROM_QSTR(MP_QSTR_uuid16), MP_ROM_PTR(&bleio_uuid_uuid16_obj) },
176156
{ MP_ROM_QSTR(MP_QSTR_uuid128), MP_ROM_PTR(&bleio_uuid_uuid128_obj) },
177-
{ MP_ROM_QSTR(MP_QSTR_uuid128_reference), MP_ROM_PTR(&bleio_uuid_uuid128_reference_obj) },
178157
{ MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&bleio_uuid_size_obj) },
179158
};
180159

0 commit comments

Comments
 (0)