diff --git a/nrf5/modules/ubluepy/modubluepy.h b/nrf5/modules/ubluepy/modubluepy.h index f4a585fe24..c6bec69825 100644 --- a/nrf5/modules/ubluepy/modubluepy.h +++ b/nrf5/modules/ubluepy/modubluepy.h @@ -40,8 +40,8 @@ DB setup: from ubluepy import Service, Characteristic, UUID, Peripheral from pyb import LED -def event_handler(id, length, data): - print("BLE event:", id, " length: ", length) +def event_handler(id, conn_handle, length, data): + print("BLE event:", id, "conn_handle:", conn_handle, "length:", length) if id == 16: # connected @@ -105,6 +105,7 @@ typedef struct _ubluepy_delegate_obj_t { typedef struct _ubluepy_peripheral_obj_t { mp_obj_base_t base; + uint16_t conn_handle; mp_obj_t delegate; mp_obj_t notif_handler; mp_obj_t conn_handler; @@ -117,6 +118,6 @@ typedef struct _ubluepy_advertise_data_t { uint8_t num_of_services; } ubluepy_advertise_data_t; -typedef void (*ubluepy_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t length, uint8_t * data); +typedef void (*ubluepy_gap_evt_callback_t)(mp_obj_t self, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data); #endif // UBLUEPY_H__ diff --git a/nrf5/modules/ubluepy/ubluepy_peripheral.c b/nrf5/modules/ubluepy/ubluepy_peripheral.c index c3db8211b2..6c9e2a751c 100644 --- a/nrf5/modules/ubluepy/ubluepy_peripheral.c +++ b/nrf5/modules/ubluepy/ubluepy_peripheral.c @@ -39,18 +39,19 @@ STATIC void ubluepy_peripheral_print(const mp_print_t *print, mp_obj_t o, mp_pri mp_printf(print, "Peripheral"); } -STATIC void event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t length, uint8_t * data) { +STATIC void gap_event_handler(mp_obj_t self_in, uint16_t event_id, uint16_t conn_handle, uint16_t length, uint8_t * data) { ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); if (self->conn_handler != mp_const_none) { - mp_obj_t args[3]; - mp_uint_t num_of_args = 3; + mp_obj_t args[4]; + mp_uint_t num_of_args = 4; args[0] = MP_OBJ_NEW_SMALL_INT(event_id); - args[1] = MP_OBJ_NEW_SMALL_INT(length); + args[1] = MP_OBJ_NEW_SMALL_INT(conn_handle); + args[2] = MP_OBJ_NEW_SMALL_INT(length); if (data != NULL) { - args[2] = mp_obj_new_bytearray_by_ref(length, data); + args[3] = mp_obj_new_bytearray_by_ref(length, data); } else { - args[2] = mp_const_none; + args[3] = mp_const_none; } // for now hard-code all events to conn_handler @@ -78,11 +79,12 @@ STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_ ubluepy_peripheral_obj_t *s = m_new_obj(ubluepy_peripheral_obj_t); s->base.type = type; - sd_event_handler_set(MP_OBJ_FROM_PTR(s), event_handler); + sd_gap_event_handler_set(MP_OBJ_FROM_PTR(s), gap_event_handler); s->delegate = mp_const_none; s->conn_handler = mp_const_none; s->notif_handler = mp_const_none; + s->conn_handle = 0xFFFF; return MP_OBJ_FROM_PTR(s); } diff --git a/nrf5/sdk/softdevice.c b/nrf5/sdk/softdevice.c index d5cfa56ef7..07f8602667 100644 --- a/nrf5/sdk/softdevice.c +++ b/nrf5/sdk/softdevice.c @@ -51,8 +51,8 @@ if (sd_enabled() == 0) { \ static bool m_adv_in_progress = false; -static ubluepy_evt_callback_t ubluepy_event_handler; -static mp_obj_t mp_observer; +static ubluepy_gap_evt_callback_t ubluepy_gap_event_handler; +static mp_obj_t mp_observer; #if (BLUETOOTH_SD != 100) && (BLUETOOTH_SD != 110) #include "nrf_nvic.h" @@ -456,9 +456,9 @@ bool sd_advertise_data(ubluepy_advertise_data_t * p_adv_params) { return true; } -void sd_event_handler_set(mp_obj_t obj, ubluepy_evt_callback_t evt_handler) { +void sd_gap_event_handler_set(mp_obj_t obj, ubluepy_gap_evt_callback_t evt_handler) { mp_observer = obj; - ubluepy_event_handler = evt_handler; + ubluepy_gap_event_handler = evt_handler; } static void ble_evt_handler(ble_evt_t * p_ble_evt) { @@ -468,17 +468,16 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { // GATTC 0x30 -> 0x4F // GATTS 0x50 -> 0x6F // L2CAP 0x70 -> 0x8F - - ubluepy_event_handler(mp_observer, p_ble_evt->header.evt_id, p_ble_evt->header.evt_len - sizeof(uint16_t), NULL); - - switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: BLE_DRIVER_LOG("GAP CONNECT\n"); + m_adv_in_progress = false; + ubluepy_gap_event_handler(mp_observer, p_ble_evt->header.evt_id, p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->header.evt_len - (2 * sizeof(uint16_t)), NULL); break; case BLE_GAP_EVT_DISCONNECTED: BLE_DRIVER_LOG("GAP DISCONNECT\n"); + ubluepy_gap_event_handler(mp_observer, p_ble_evt->header.evt_id, p_ble_evt->evt.gap_evt.conn_handle, p_ble_evt->header.evt_len - (2 * sizeof(uint16_t)), NULL); break; case BLE_GATTS_EVT_WRITE: diff --git a/nrf5/sdk/softdevice.h b/nrf5/sdk/softdevice.h index f17395e517..f3311c8054 100644 --- a/nrf5/sdk/softdevice.h +++ b/nrf5/sdk/softdevice.h @@ -50,6 +50,6 @@ bool sd_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj); bool sd_advertise_data(ubluepy_advertise_data_t * p_adv_params); -void sd_event_handler_set(mp_obj_t obs, ubluepy_evt_callback_t evt_handler); +void sd_gap_event_handler_set(mp_obj_t obs, ubluepy_gap_evt_callback_t evt_handler); #endif // BLUETOOTH_LE_DRIVER_H__