evt handler list bugs; unique evt handler names; remove uuid128_reference

This commit is contained in:
Dan Halbert 2019-01-03 21:42:42 -05:00
parent 8dea6f53bb
commit a77b2363ef
5 changed files with 26 additions and 46 deletions

View File

@ -53,27 +53,22 @@ void ble_drv_reset() {
} }
void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param) { 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; 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)) { if ((it->func == func) && (it->param == param)) {
m_free(handler);
return; return;
} }
it = it->next; 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) { void SD_EVT_IRQHandler(void) {

View File

@ -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 is not set, so say that neither Notify nor Indicate is enabled.
cccd = 0; cccd = 0;
} else if (err_code != NRF_SUCCESS) { } 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; 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 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); const uint32_t err_code = sd_ble_gatts_hvx(conn_handle, &hvx_params);
if (err_code != NRF_SUCCESS) { 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; 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) { switch (ble_evt->header.evt_id) {
case BLE_GATTS_EVT_HVN_TX_COMPLETE: case BLE_GATTS_EVT_HVN_TX_COMPLETE:
m_tx_in_progress -= ble_evt->evt.gatts_evt.params.hvn_tx_complete.count; 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) { 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) { void common_hal_bleio_characteristic_get_value(bleio_characteristic_obj_t *self) {

View File

@ -221,7 +221,7 @@ STATIC uint32_t set_advertisement_data(bleio_peripheral_obj_t *self, bool connec
return err_code; 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; bleio_peripheral_obj_t *self = (bleio_peripheral_obj_t*)self_in;
switch (ble_evt->header.evt_id) { 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) { void common_hal_bleio_peripheral_start_advertising(bleio_peripheral_obj_t *self, bool connectable, mp_buffer_info_t *raw_data) {
if (connectable) { 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); const uint32_t err_code = set_advertisement_data(self, connectable, raw_data);

View File

@ -35,15 +35,21 @@
//| :class:`Characteristic` -- BLE service characteristic //| :class:`Characteristic` -- BLE service characteristic
//| ========================================================= //| =========================================================
//| //|
//| Stores information about a BLE service characteristic and allows to read //| Stores information about a BLE service characteristic and allows reading
//| and write the characteristic's value. //| and writing of the characteristic's value.
//| //|
//| //|
//| .. class:: Characteristic(uuid, *, broadcast=False, indicate=False, notify=False, read=False, write=False, write_no_response=False) //| .. 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. //| 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) { 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); mp_arg_check_num(n_args, n_kw, 1, 1, true);

View File

@ -109,8 +109,8 @@ const mp_obj_property_t bleio_uuid_uuid16_obj = {
//| .. attribute:: uuid128 //| .. attribute:: uuid128
//| //|
//| The 128-bit value of the UUID, return as a bytes(). //| The 128-bit value of the UUID, returned as bytes.
//| Throws AttributeError if this is a 16-bit UUID. (read-only) //| Raises AttributeError if this is a 16-bit UUID. (read-only)
//| //|
STATIC mp_obj_t bleio_uuid_get_uuid128(mp_obj_t self_in) { STATIC mp_obj_t bleio_uuid_get_uuid128(mp_obj_t self_in) {
bleio_uuid_obj_t *self = MP_OBJ_TO_PTR(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}, (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 //| .. attribute:: size
//| //|
//| Returns 128 if this UUID represents a 128-bit vendor-specific UUID. //| 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[] = { 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_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), 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) }, { MP_ROM_QSTR(MP_QSTR_size), MP_ROM_PTR(&bleio_uuid_size_obj) },
}; };