diff --git a/nrf5/modules/ubluepy/modubluepy.h b/nrf5/modules/ubluepy/modubluepy.h index aef5408542..685a54b5e9 100644 --- a/nrf5/modules/ubluepy/modubluepy.h +++ b/nrf5/modules/ubluepy/modubluepy.h @@ -37,6 +37,9 @@ p.advertise(device_name="MicroPython") DB setup: +def event_handler(id, length, data): + print("BLE event:", id, " length: ", length) + from ubluepy import Service, Characteristic, UUID, Peripheral u0 = UUID("6e400001-b5a3-f393-e0a9-e50e24dcca9e") s = Service(u0) @@ -44,6 +47,7 @@ u1 = UUID("6e400002-b5a3-f393-e0a9-e50e24dcca9e") c = Characteristic(u1) s.addCharacteristic(c) p = Peripheral() +p.setConnectionHandler(event_handler) p.advertise(device_name="micr", services=[s]) */ @@ -104,6 +108,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); #endif // UBLUEPY_H__ diff --git a/nrf5/modules/ubluepy/ubluepy_peripheral.c b/nrf5/modules/ubluepy/ubluepy_peripheral.c index 12d50fe96f..aa55b8ff48 100644 --- a/nrf5/modules/ubluepy/ubluepy_peripheral.c +++ b/nrf5/modules/ubluepy/ubluepy_peripheral.c @@ -39,6 +39,25 @@ 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) { + ubluepy_peripheral_obj_t *self = MP_OBJ_TO_PTR(self_in); + + mp_obj_t args[3]; + mp_uint_t num_of_args = 3; + args[0] = MP_OBJ_NEW_SMALL_INT(event_id); + args[1] = MP_OBJ_NEW_SMALL_INT(length); + if (data != NULL) { + args[2] = mp_obj_new_bytearray_by_ref(length, data); + } else { + args[2] = mp_const_none; + } + + // for now hard-code all events to conn_handler + mp_call_function_n_kw(self->conn_handler, num_of_args, 0, args); + + (void)self; +} + STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { enum { ARG_NEW_DEVICE_ADDR, @@ -46,8 +65,8 @@ STATIC mp_obj_t ubluepy_peripheral_make_new(const mp_obj_type_t *type, size_t n_ }; static const mp_arg_t allowed_args[] = { - { ARG_NEW_DEVICE_ADDR, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, - { ARG_NEW_ADDR_TYPE, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} }, + { ARG_NEW_DEVICE_ADDR, MP_ARG_OBJ, {.u_obj = mp_const_none} }, + { ARG_NEW_ADDR_TYPE, MP_ARG_OBJ, {.u_obj = mp_const_none} }, }; // parse args @@ -57,21 +76,11 @@ 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); + return MP_OBJ_FROM_PTR(s); } -#if 0 -static void peripheral_delegate(void) { - // delegate - mp_obj_t args[3]; - mp_uint_t num_of_args = 3; - args[0] = type; - args[1] = MP_OBJ_NEW_SMALL_INT(size); - args[2] = mp_obj_new_bytearray_by_ref(size, evt_data); - mp_call_function_n_kw(delegate->handleConnection, num_of_args, 0, args); -} -#endif - /// \method withDelegate(DefaultDelegate) /// Set delegate instance for handling Bluetooth LE events. /// diff --git a/nrf5/sdk/softdevice.c b/nrf5/sdk/softdevice.c index 9efe8f8327..0cfd2beb27 100644 --- a/nrf5/sdk/softdevice.c +++ b/nrf5/sdk/softdevice.c @@ -41,7 +41,10 @@ if (sd_enabled() == 0) { \ (void)sd_enable(); \ } -bool m_adv_in_progress = false; +static bool m_adv_in_progress = false; + +static ubluepy_evt_callback_t ubluepy_event_handler; +static mp_obj_t mp_observer; #if (BLUETOOTH_SD != 100) && (BLUETOOTH_SD != 110) #include "nrf_nvic.h" @@ -445,6 +448,11 @@ 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) { + mp_observer = obj; + ubluepy_event_handler = evt_handler; +} + static void ble_evt_handler(ble_evt_t * p_ble_evt) { // S132 event ranges. // Common 0x01 -> 0x0F @@ -455,6 +463,7 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { switch (p_ble_evt->header.evt_id) { case BLE_GAP_EVT_CONNECTED: + ubluepy_event_handler(mp_observer, BLE_GAP_EVT_CONNECTED, p_ble_evt->header.evt_len - sizeof(uint16_t), NULL); printf("GAP CONNECT\n"); break; diff --git a/nrf5/sdk/softdevice.h b/nrf5/sdk/softdevice.h index ba79d89866..f17395e517 100644 --- a/nrf5/sdk/softdevice.h +++ b/nrf5/sdk/softdevice.h @@ -49,4 +49,7 @@ bool sd_service_add(ubluepy_service_obj_t * p_service_obj); 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); + #endif // BLUETOOTH_LE_DRIVER_H__