From a77b2363eff14ae5b19c50a7d25b3db4ef6b200c Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Thu, 3 Jan 2019 21:42:42 -0500 Subject: [PATCH] evt handler list bugs; unique evt handler names; remove uuid128_reference --- ports/nrf/bluetooth/ble_drv.c | 23 ++++++++----------- ports/nrf/common-hal/bleio/Characteristic.c | 8 +++---- ports/nrf/common-hal/bleio/Peripheral.c | 4 ++-- shared-bindings/bleio/Characteristic.c | 12 +++++++--- shared-bindings/bleio/UUID.c | 25 ++------------------- 5 files changed, 26 insertions(+), 46 deletions(-) diff --git a/ports/nrf/bluetooth/ble_drv.c b/ports/nrf/bluetooth/ble_drv.c index 62820289b9..7082b1e59c 100644 --- a/ports/nrf/bluetooth/ble_drv.c +++ b/ports/nrf/bluetooth/ble_drv.c @@ -53,27 +53,22 @@ void ble_drv_reset() { } void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param) { - event_handler_t *handler = m_new_ll(event_handler_t, 1); - handler->next = NULL; - handler->param = param; - handler->func = func; - - if (m_event_handlers == NULL) { - m_event_handlers = handler; - return; - } - event_handler_t *it = m_event_handlers; - while (it->next != NULL) { + while (it != NULL) { + // If event handler and its corresponding param are already on the list, don't add again. if ((it->func == func) && (it->param == param)) { - m_free(handler); return; } - it = it->next; } - it->next = handler; + // Add a new handler to the front of the list + event_handler_t *handler = m_new_ll(event_handler_t, 1); + handler->next = m_event_handlers; + handler->param = param; + handler->func = func; + + m_event_handlers = handler; } void SD_EVT_IRQHandler(void) { diff --git a/ports/nrf/common-hal/bleio/Characteristic.c b/ports/nrf/common-hal/bleio/Characteristic.c index 9d7007d7da..a955395bd5 100644 --- a/ports/nrf/common-hal/bleio/Characteristic.c +++ b/ports/nrf/common-hal/bleio/Characteristic.c @@ -55,7 +55,7 @@ STATIC uint16_t get_cccd(bleio_characteristic_obj_t *characteristic) { // CCCD is not set, so say that neither Notify nor Indicate is enabled. cccd = 0; } else if (err_code != NRF_SUCCESS) { - mp_raise_OSError_msg_varg(translate("Failed to read CCD value, err 0x%04x"), err_code); + mp_raise_OSError_msg_varg(translate("Failed to read CCCD value, err 0x%04x"), err_code); } return cccd; @@ -126,7 +126,7 @@ STATIC void gatts_notify_indicate(bleio_characteristic_obj_t *characteristic, mp const uint16_t conn_handle = common_hal_bleio_device_get_conn_handle(characteristic->service->device); const uint32_t err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params); if (err_code != NRF_SUCCESS) { - mp_raise_OSError_msg_varg(translate("Failed to notify attribute value, err %0x04x"), err_code); + mp_raise_OSError_msg_varg(translate("Failed to notify or indicate attribute value, err %0x04x"), err_code); } m_tx_in_progress += 1; @@ -188,7 +188,7 @@ STATIC void gattc_write(bleio_characteristic_obj_t *characteristic, mp_buffer_in } } -STATIC void on_ble_evt(ble_evt_t *ble_evt, void *param) { +STATIC void characteristic_on_ble_evt(ble_evt_t *ble_evt, void *param) { switch (ble_evt->header.evt_id) { case BLE_GATTS_EVT_HVN_TX_COMPLETE: 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) { } void common_hal_bleio_characteristic_construct(bleio_characteristic_obj_t *self) { - ble_drv_add_event_handler(on_ble_evt, NULL); + ble_drv_add_event_handler(characteristic_on_ble_evt, NULL); } void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) { diff --git a/ports/nrf/common-hal/bleio/Peripheral.c b/ports/nrf/common-hal/bleio/Peripheral.c index d2eef596d4..7d5d6a6a6c 100644 --- a/ports/nrf/common-hal/bleio/Peripheral.c +++ b/ports/nrf/common-hal/bleio/Peripheral.c @@ -221,7 +221,7 @@ STATIC uint32_t set_advertisement_data(bleio_peripheral_obj_t *self, bool connec return err_code; } -STATIC void on_ble_evt(ble_evt_t *ble_evt, void *self_in) { +STATIC void peripheral_on_ble_evt(ble_evt_t *ble_evt, void *self_in) { bleio_peripheral_obj_t *self = (bleio_peripheral_obj_t*)self_in; switch (ble_evt->header.evt_id) { @@ -311,7 +311,7 @@ bool common_hal_bleio_peripheral_get_connected(bleio_peripheral_obj_t *self) { void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *self, bool connectable, mp_buffer_info_t *raw_data) { if (connectable) { - ble_drv_add_event_handler(on_ble_evt, self); + ble_drv_add_event_handler(peripheral_on_ble_evt, self); } const uint32_t err_code = set_advertisement_data(self, connectable, raw_data); diff --git a/shared-bindings/bleio/Characteristic.c b/shared-bindings/bleio/Characteristic.c index 7cffd2261b..1d414b6d30 100644 --- a/shared-bindings/bleio/Characteristic.c +++ b/shared-bindings/bleio/Characteristic.c @@ -35,15 +35,21 @@ //| :class:`Characteristic` -- BLE service characteristic //| ========================================================= //| -//| Stores information about a BLE service characteristic and allows to read -//| and write the characteristic's value. +//| Stores information about a BLE service characteristic and allows reading +//| and writing of the characteristic's value. //| //| //| .. class:: Characteristic(uuid, *, broadcast=False, indicate=False, notify=False, read=False, write=False, write_no_response=False) //| //| Create a new Characteristic object identified by the specified UUID. //| -//| :param bleio.UUID uuid: The uuid of the characteristic (read_only) +//| :param bleio.UUID uuid: The uuid of the characteristic +//| :param bool broadcast: Allowed in advertising packets +//| :param bool indicate: Server will indicate to the client when the value is set and wait for a response +//| :param bool notify: Server will notify the client when the value is set +//| :param bool read: Clients may read this characteristic +//| :param bool write: Clients may write this characteristic; a response will be sent back +//| :param bool write_no_response: Clients may write this characteristic; no response will be sent back //| 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) { mp_arg_check_num(n_args, n_kw, 1, 1, true); diff --git a/shared-bindings/bleio/UUID.c b/shared-bindings/bleio/UUID.c index 7cef8c47f8..2b42ab05cc 100644 --- a/shared-bindings/bleio/UUID.c +++ b/shared-bindings/bleio/UUID.c @@ -109,8 +109,8 @@ const mp_obj_property_t bleio_uuid_uuid16_obj = { //| .. attribute:: uuid128 //| -//| The 128-bit value of the UUID, return as a bytes(). -//| Throws AttributeError if this is a 16-bit UUID. (read-only) +//| The 128-bit value of the UUID, returned as bytes. +//| Raises AttributeError if this is a 16-bit UUID. (read-only) //| STATIC mp_obj_t bleio_uuid_get_uuid128(mp_obj_t self_in) { bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -131,26 +131,6 @@ const mp_obj_property_t bleio_uuid_uuid128_obj = { (mp_obj_t)&mp_const_none_obj}, }; -//| .. attribute:: uuid128_reference -//| -//| An opaque reference representing the 128-bit UUID. (read-only) -//| Returns None if this is a 16-bit UUID. -//| -STATIC mp_obj_t bleio_uuid_get_uuid128_reference(mp_obj_t self_in) { - bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(self_in); - uint32_t reference = common_hal_bleio_uuid_get_uuid128_reference(self); - return reference == 0 ? mp_const_none : MP_OBJ_NEW_SMALL_INT(reference); -} - -MP_DEFINE_CONST_FUN_OBJ_1(bleio_uuid_get_uuid128_reference_obj, bleio_uuid_get_uuid128_reference); - -const mp_obj_property_t bleio_uuid_uuid128_reference_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&bleio_uuid_get_uuid128_reference_obj, - (mp_obj_t)&mp_const_none_obj, - (mp_obj_t)&mp_const_none_obj}, -}; - //| .. attribute:: size //| //| 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 = { STATIC const mp_rom_map_elem_t bleio_uuid_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_uuid16), MP_ROM_PTR(&bleio_uuid_uuid16_obj) }, { MP_ROM_QSTR(MP_QSTR_uuid128), MP_ROM_PTR(&bleio_uuid_uuid128_obj) }, - { MP_ROM_QSTR(MP_QSTR_uuid128_reference), MP_ROM_PTR(&bleio_uuid_uuid128_reference_obj) }, { MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&bleio_uuid_size_obj) }, };