From 29b283f69745baaf68138a0b26f5e3e8f7a5fe2c Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Sun, 19 Feb 2017 00:08:05 +0100 Subject: [PATCH] nrf5/sdk: Adding support for setting gatts handler in the bluetooth le driver. --- nrf5/sdk/ble_drv.c | 23 +++++++++++++++++------ nrf5/sdk/ble_drv.h | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/nrf5/sdk/ble_drv.c b/nrf5/sdk/ble_drv.c index ecf565c864..09c4b2b379 100644 --- a/nrf5/sdk/ble_drv.c +++ b/nrf5/sdk/ble_drv.c @@ -52,7 +52,10 @@ if (ble_drv_stack_enabled() == 0) { \ static bool m_adv_in_progress = false; static ubluepy_gap_evt_callback_t ubluepy_gap_event_handler; -static mp_obj_t mp_observer; +static ubluepy_gatts_evt_callback_t ubluepy_gatts_event_handler; + +static mp_obj_t mp_gap_observer; +static mp_obj_t mp_gatts_observer; #if (BLUETOOTH_SD != 100) && (BLUETOOTH_SD != 110) #include "nrf_nvic.h" @@ -278,10 +281,8 @@ bool ble_drv_characteristic_add(ubluepy_characteristic_obj_t * p_char_obj) { ble_gatts_attr_md_t attr_md; memset(&cccd_md, 0, sizeof(cccd_md)); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - cccd_md.vloc = BLE_GATTS_VLOC_STACK; memset(&char_md, 0, sizeof(char_md)); @@ -574,10 +575,15 @@ void ble_drv_attr_notif(uint16_t conn_handle, uint16_t handle, uint16_t len, uin } void ble_drv_gap_event_handler_set(mp_obj_t obj, ubluepy_gap_evt_callback_t evt_handler) { - mp_observer = obj; + mp_gap_observer = obj; ubluepy_gap_event_handler = evt_handler; } +void ble_drv_gatts_event_handler_set(mp_obj_t obj, ubluepy_gatts_evt_callback_t evt_handler) { + mp_gatts_observer = obj; + ubluepy_gatts_event_handler = evt_handler; +} + static void ble_evt_handler(ble_evt_t * p_ble_evt) { // S132 event ranges. // Common 0x01 -> 0x0F @@ -589,16 +595,21 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { 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); + ubluepy_gap_event_handler(mp_gap_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); + ubluepy_gap_event_handler(mp_gap_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_HVC: + ubluepy_gatts_event_handler(mp_gatts_observer, p_ble_evt->header.evt_id, p_ble_evt->evt.gatts_evt.params.hvc.handle, p_ble_evt->header.evt_len - (2 * sizeof(uint16_t)), NULL); break; case BLE_GATTS_EVT_WRITE: BLE_DRIVER_LOG("GATTS write\n"); + ubluepy_gatts_event_handler(mp_gatts_observer, p_ble_evt->header.evt_id, p_ble_evt->evt.gatts_evt.params.write.handle, p_ble_evt->header.evt_len - (2 * sizeof(uint16_t)), NULL); break; case BLE_GAP_EVT_CONN_PARAM_UPDATE: diff --git a/nrf5/sdk/ble_drv.h b/nrf5/sdk/ble_drv.h index 7fbef8bd67..8f8075a8c7 100644 --- a/nrf5/sdk/ble_drv.h +++ b/nrf5/sdk/ble_drv.h @@ -52,6 +52,8 @@ bool ble_drv_advertise_data(ubluepy_advertise_data_t * p_adv_params); void ble_drv_gap_event_handler_set(mp_obj_t obs, ubluepy_gap_evt_callback_t evt_handler); +void ble_drv_gatts_event_handler_set(mp_obj_t obj, ubluepy_gatts_evt_callback_t evt_handler); + void ble_drv_attr_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data); void ble_drv_attr_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);