Skip to content

Add raw BLE peripheral event management #263

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <BLEPeripheral.h>
#include <ble.h>

// ...

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
Expand Down
2 changes: 2 additions & 0 deletions src/BLEDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*/) { }
Expand Down
10 changes: 10 additions & 0 deletions src/BLEPeripheral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions src/BLEPeripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum BLEPeripheralEvent {
};

typedef void (*BLEPeripheralEventHandler)(BLECentral& central);
typedef void (*BLEPeripheralRawEventHandler) (int eventId, const void* event);


class BLEPeripheral : public BLEDeviceEventListener,
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -158,6 +162,7 @@ class BLEPeripheral : public BLEDeviceEventListener,

BLECentral _central;
BLEPeripheralEventHandler _eventHandlers[4];
BLEPeripheralRawEventHandler _rawEventHandler;
};

#endif
4 changes: 4 additions & 0 deletions src/nRF51822.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/nRF8001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down