From c88358d84b26a7c3113bd92e56b3791c0610154f Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Sat, 1 Apr 2017 16:34:26 +0200 Subject: [PATCH] nrf5/bluetooth: Updating bluetooth driver to support GATT client read of a characteristic value. Data passed to caller in interrupt context, and copy has to be performed. The function call is itself blocking. --- nrf5/bluetooth/ble_drv.c | 24 ++++++++++++++++++++++-- nrf5/bluetooth/ble_drv.h | 4 ++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/nrf5/bluetooth/ble_drv.c b/nrf5/bluetooth/ble_drv.c index 20b6284490..71eb09bd3a 100644 --- a/nrf5/bluetooth/ble_drv.c +++ b/nrf5/bluetooth/ble_drv.c @@ -80,6 +80,7 @@ static ble_drv_gatts_evt_callback_t gatts_event_handler; static ble_drv_gattc_evt_callback_t gattc_event_handler; static ble_drv_disc_add_service_callback_t disc_add_service_handler; static ble_drv_disc_add_char_callback_t disc_add_char_handler; +static ble_drv_gattc_char_data_callback_t gattc_char_data_handle; static mp_obj_t mp_gap_observer; static mp_obj_t mp_adv_observer; @@ -87,6 +88,7 @@ static mp_obj_t mp_gatts_observer; static mp_obj_t mp_gattc_observer; static mp_obj_t mp_gattc_disc_service_observer; static mp_obj_t mp_gattc_disc_char_observer; +static mp_obj_t mp_gattc_char_data_observer; #if (BLUETOOTH_SD != 100) && (BLUETOOTH_SD != 110) #include "nrf_nvic.h" @@ -619,7 +621,11 @@ void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, ui } -void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data) { +void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, ble_drv_gattc_char_data_callback_t cb) { + + mp_gattc_char_data_observer = obj; + gattc_char_data_handle = cb; + uint32_t err_code = sd_ble_gattc_read(conn_handle, handle, 0); @@ -627,6 +633,10 @@ void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, uint16_t len, ui nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, "Can not read attribute value. status: 0x" HEX2_FMT, (uint16_t)err_code)); } + + while (gattc_char_data_handle != NULL) { + ; + } } void ble_drv_attr_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data) { @@ -963,7 +973,17 @@ static void ble_evt_handler(ble_evt_t * p_ble_evt) { break; case BLE_GATTC_EVT_READ_RSP: - BLE_DRIVER_LOG("BLE EVT READ RESPONSE\n"); + BLE_DRIVER_LOG("BLE EVT READ RESPONSE, offset: 0x"HEX2_FMT", length: 0x"HEX2_FMT"\n", + p_ble_evt->evt.gattc_evt.params.read_rsp.offset, + p_ble_evt->evt.gattc_evt.params.read_rsp.len); + + gattc_char_data_handle(mp_gattc_char_data_observer, + p_ble_evt->evt.gattc_evt.params.read_rsp.len, + p_ble_evt->evt.gattc_evt.params.read_rsp.data); + + // mark end of read + gattc_char_data_handle = NULL; + break; case BLE_GATTC_EVT_WRITE_RSP: diff --git a/nrf5/bluetooth/ble_drv.h b/nrf5/bluetooth/ble_drv.h index 471b476bfb..9671ab695b 100644 --- a/nrf5/bluetooth/ble_drv.h +++ b/nrf5/bluetooth/ble_drv.h @@ -63,7 +63,7 @@ typedef void (*ble_drv_gattc_evt_callback_t)(mp_obj_t self, uint16_t event_id, u typedef void (*ble_drv_adv_evt_callback_t)(mp_obj_t self, uint16_t event_id, ble_drv_adv_data_t * data); typedef void (*ble_drv_disc_add_service_callback_t)(mp_obj_t self, ble_drv_service_data_t * p_service_data); typedef void (*ble_drv_disc_add_char_callback_t)(mp_obj_t self, ble_drv_char_data_t * p_desc_data); - +typedef void (*ble_drv_gattc_char_data_callback_t)(mp_obj_t self, uint16_t length, uint8_t * p_data); uint32_t ble_drv_stack_enable(void); @@ -91,7 +91,7 @@ void ble_drv_gattc_event_handler_set(mp_obj_t obj, ble_drv_gattc_evt_callback_t void ble_drv_attr_s_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data); -void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data); +void ble_drv_attr_c_read(uint16_t conn_handle, uint16_t handle, mp_obj_t obj, ble_drv_gattc_char_data_callback_t cb); void ble_drv_attr_write(uint16_t conn_handle, uint16_t handle, uint16_t len, uint8_t * p_data);