From aae994d79b6afd8dcbf946e18f5a4d93ec99246b Mon Sep 17 00:00:00 2001 From: plapointe6 Date: Wed, 8 May 2019 14:04:34 -0400 Subject: [PATCH] Added raw BLE peripheral event management This commit add the possibility to subscribe to raw event. Useful if you want to handle specific events that are not handled in BLEPeripheral. --- API.md | 27 +++++++++++++++++++++++++++ src/BLEDevice.h | 2 ++ src/BLEPeripheral.cpp | 10 ++++++++++ src/BLEPeripheral.h | 5 +++++ src/nRF51822.cpp | 4 ++++ src/nRF8001.cpp | 9 +++++++++ 6 files changed, 57 insertions(+) diff --git a/API.md b/API.md index 2524082..196c2e6 100644 --- a/API.md +++ b/API.md @@ -141,6 +141,33 @@ void blePeripheralEventHandler(BLECentral& central) { * event - ```BLEConnected``` or ```BLEDisconnected``` * eventHandler - function callback for event + +### Set raw event handler callbacks +```c +void setRawEventHandler(BLEPeripheralRawEventHandler rawEventHandler); + +// callback signature +void blePeripheralRawEventHandler(int eventId, const void* event); +``` + +Callback example (for a nRF51822) +```c +#include +#include + +// ... + +void blePeripheralRawEventHandler(int eventId, const void* event) { + // Handling a specific event id unandled by BLEPeripheral. + if(eventId == BLE_GAP_EVT_ADV_REPORT) { + // Casting into a BLE event + ble_evt_t* evt = (ble_evt_t*)event; + // ... + } +} +``` + + ## Actions ### Disconnect diff --git a/src/BLEDevice.h b/src/BLEDevice.h index fb38781..070f49c 100644 --- a/src/BLEDevice.h +++ b/src/BLEDevice.h @@ -23,6 +23,8 @@ class BLEDevice; class BLEDeviceEventListener { public: + virtual void BLERawEvent(int eventId, const void* event); + virtual void BLEDeviceConnected(BLEDevice& /*device*/, const unsigned char* /*address*/) { } virtual void BLEDeviceDisconnected(BLEDevice& /*device*/) { } virtual void BLEDeviceBonded(BLEDevice& /*device*/) { } diff --git a/src/BLEPeripheral.cpp b/src/BLEPeripheral.cpp index a730a81..e29351e 100644 --- a/src/BLEPeripheral.cpp +++ b/src/BLEPeripheral.cpp @@ -25,6 +25,7 @@ BLEPeripheral::BLEPeripheral(unsigned char req, unsigned char rdy, unsigned char _manufacturerData(NULL), _manufacturerDataLength(0), _localName(NULL), + _rawEventHandler(NULL), _localAttributes(NULL), _numLocalAttributes(0), @@ -263,6 +264,10 @@ void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEvent } } +void BLEPeripheral::setRawEventHandler(BLEPeripheralRawEventHandler rawEventHandler) { + this->_rawEventHandler = rawEventHandler; +} + bool BLEPeripheral::characteristicValueChanged(BLECharacteristic& characteristic) { return this->_device->updateCharacteristicValue(characteristic); } @@ -311,6 +316,11 @@ bool BLEPeripheral::unsubcribeRemoteCharacteristic(BLERemoteCharacteristic& char return this->_device->unsubcribeRemoteCharacteristic(characteristic); } +void BLEPeripheral::BLERawEvent(int eventId, const void* event) { + if(this->_rawEventHandler) + this->_rawEventHandler(eventId, event); +} + void BLEPeripheral::BLEDeviceConnected(BLEDevice& /*device*/, const unsigned char* address) { this->_central.setAddress(address); diff --git a/src/BLEPeripheral.h b/src/BLEPeripheral.h index e9e8705..e5b0446 100644 --- a/src/BLEPeripheral.h +++ b/src/BLEPeripheral.h @@ -53,6 +53,7 @@ enum BLEPeripheralEvent { }; typedef void (*BLEPeripheralEventHandler)(BLECentral& central); +typedef void (*BLEPeripheralRawEventHandler) (int eventId, const void* event); class BLEPeripheral : public BLEDeviceEventListener, @@ -94,6 +95,7 @@ class BLEPeripheral : public BLEDeviceEventListener, bool connected(); void setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler); + void setRawEventHandler(BLEPeripheralRawEventHandler rawEventHandler); protected: bool characteristicValueChanged(BLECharacteristic& characteristic); @@ -110,6 +112,8 @@ class BLEPeripheral : public BLEDeviceEventListener, bool canUnsubscribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic); bool unsubcribeRemoteCharacteristic(BLERemoteCharacteristic& characteristic); + virtual void BLERawEvent(int eventId, const void* event); + virtual void BLEDeviceConnected(BLEDevice& device, const unsigned char* address); virtual void BLEDeviceDisconnected(BLEDevice& device); virtual void BLEDeviceBonded(BLEDevice& device); @@ -158,6 +162,7 @@ class BLEPeripheral : public BLEDeviceEventListener, BLECentral _central; BLEPeripheralEventHandler _eventHandlers[4]; + BLEPeripheralRawEventHandler _rawEventHandler; }; #endif diff --git a/src/nRF51822.cpp b/src/nRF51822.cpp index 21adf73..1f35686 100644 --- a/src/nRF51822.cpp +++ b/src/nRF51822.cpp @@ -529,6 +529,10 @@ void nRF51822::poll() { ble_evt_t* bleEvt = (ble_evt_t*)evtBuf; if (sd_ble_evt_get((uint8_t*)evtBuf, &evtLen) == NRF_SUCCESS) { + + if(this->_eventListener) + this->_eventListener->BLERawEvent(bleEvt->header.evt_id, bleEvt); + switch (bleEvt->header.evt_id) { case BLE_EVT_TX_COMPLETE: #ifdef NRF_51822_DEBUG diff --git a/src/nRF8001.cpp b/src/nRF8001.cpp index a82d7d2..88a6ae1 100644 --- a/src/nRF8001.cpp +++ b/src/nRF8001.cpp @@ -806,6 +806,9 @@ void nRF8001::poll() { if (lib_aci_event_get(&this->_aciState, &this->_aciData)) { aci_evt_t* aciEvt = &this->_aciData.evt; + if(this->_eventListener) + this->_eventListener->BLERawEvent(aciEvt->evt_opcode, aciEvt); + switch(aciEvt->evt_opcode) { /** As soon as you reset the nRF8001 you will get an ACI Device Started Event @@ -1451,6 +1454,9 @@ void nRF8001::waitForSetupMode() if (lib_aci_event_get(&this->_aciState, &this->_aciData)) { aci_evt_t* aciEvt = &this->_aciData.evt; + if(this->_eventListener) + this->_eventListener->BLERawEvent(aciEvt->evt_opcode, aciEvt); + switch(aciEvt->evt_opcode) { case ACI_EVT_DEVICE_STARTED: { switch(aciEvt->params.device_started.device_mode) { @@ -1503,6 +1509,9 @@ void nRF8001::sendSetupMessage(hal_aci_data_t* data, bool withCrc) if (lib_aci_event_get(&this->_aciState, &this->_aciData)) { aci_evt_t* aciEvt = &this->_aciData.evt; + if(this->_eventListener) + this->_eventListener->BLERawEvent(aciEvt->evt_opcode, aciEvt); + switch(aciEvt->evt_opcode) { case ACI_EVT_CMD_RSP: { switch(aciEvt->params.cmd_rsp.cmd_status) {