nrf5/modules: Updating bluetooth driver and ubluepy to use explicit gap event handler. Adding connection handle parameter to the gap handler from ubluepy. Resetting advertisment flag if connection event is recieved, in order to allow for subsequent advertisment if disconnected again. Example in ublupy header updated.
This commit is contained in:
parent
7ce31444a0
commit
bba8221aa2
|
@ -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__
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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__
|
||||
|
|
Loading…
Reference in New Issue