From a79c5ca7ab91e9fabee0768eece3cca30abbc52a Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Mon, 13 Feb 2017 00:18:47 +0100 Subject: [PATCH] nrf5/sdk: Implementing simple event handler for bluetooth stack driver. --- nrf5/sdk/softdevice.c | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/nrf5/sdk/softdevice.c b/nrf5/sdk/softdevice.c index 8a6400080f..ce14c3e852 100644 --- a/nrf5/sdk/softdevice.c +++ b/nrf5/sdk/softdevice.c @@ -35,6 +35,7 @@ #include "ble_gap.h" #include "ble.h" // sd_ble_uuid_encode + #define SD_TEST_OR_ENABLE() \ if (sd_enabled() == 0) { \ (void)sd_enable(); \ @@ -440,3 +441,46 @@ bool sd_advertise_data(ubluepy_advertise_data_t * p_adv_params) { return true; } + +static void ble_evt_handler(ble_evt_t * p_ble_evt) { + switch (p_ble_evt->header.evt_id) { + case BLE_GAP_EVT_CONNECTED: + printf(">>> GAP CONNECT\n"); + break; + + case BLE_GAP_EVT_DISCONNECTED: + printf(">>> GAP DISCONNECT\n"); + break; + + case BLE_GATTS_EVT_WRITE: + printf(">>> GATTS write\n"); + break; + + default: + printf(">>> unhandled evt: 0x" HEX2_FMT, p_ble_evt->header.evt_id); + break; + } +} + +static uint8_t m_ble_evt_buf[sizeof(ble_evt_t) + (GATT_MTU_SIZE_DEFAULT)] __attribute__ ((aligned (4))); + +#ifdef NRF51 +void SWI2_IRQHandler(void) { +#else +void SWI2_EGU2_IRQHandler(void) { +#endif + + printf("SWI2 IRQ\n"); + uint32_t evt_id; + uint32_t err_code; + do { + err_code = sd_evt_get(&evt_id); + // TODO: handle non ble events + } while (err_code != NRF_ERROR_NOT_FOUND && err_code != NRF_SUCCESS); + + uint16_t evt_len = sizeof(m_ble_evt_buf); + do { + err_code = sd_ble_evt_get(m_ble_evt_buf, &evt_len); + ble_evt_handler((ble_evt_t *)m_ble_evt_buf); + } while (err_code != NRF_ERROR_NOT_FOUND && err_code != NRF_SUCCESS); +}