add Gap Callbacks
This commit is contained in:
parent
e01f6f5daf
commit
0c91cc32bf
2 changed files with 56 additions and 18 deletions
|
@ -129,6 +129,10 @@ public:
|
|||
|
||||
ble_error_t disconnect(void);
|
||||
|
||||
void onTimeout(Gap::EventCallback_t timeoutCallback);
|
||||
void onConnection(Gap::EventCallback_t connectionCallback);
|
||||
void onDisconnection(Gap::EventCallback_t disconnectionCallback);
|
||||
|
||||
private:
|
||||
/**
|
||||
* Internal helper to udpate the transport backend with advertising data
|
||||
|
@ -300,4 +304,20 @@ BLEDevice::startAdvertising(const GapAdvertisingParams &_advParams) {
|
|||
return transport->getGap().startAdvertising(_advParams);
|
||||
}
|
||||
|
||||
inline void
|
||||
BLEDevice::onTimeout(Gap::EventCallback_t timeoutCallback) {
|
||||
transport->getGap().setOnTimeout(timeoutCallback);
|
||||
}
|
||||
|
||||
inline void
|
||||
BLEDevice::onConnection(Gap::EventCallback_t connectionCallback) {
|
||||
transport->getGap().setOnConnection(connectionCallback);
|
||||
}
|
||||
|
||||
inline void
|
||||
BLEDevice::onDisconnection(Gap::EventCallback_t disconnectionCallback) {
|
||||
transport->getGap().setOnDisconnection(disconnectionCallback);
|
||||
}
|
||||
|
||||
|
||||
#endif // ifndef __BLE_DEVICE_H__
|
||||
|
|
54
hw/Gap.h
54
hw/Gap.h
|
@ -32,9 +32,6 @@
|
|||
/**************************************************************************/
|
||||
class Gap
|
||||
{
|
||||
private:
|
||||
GapEvents *m_pEventHandler;
|
||||
|
||||
public:
|
||||
typedef enum addr_type_e {
|
||||
ADDR_TYPE_PUBLIC = 0,
|
||||
|
@ -53,41 +50,62 @@ public:
|
|||
|
||||
/* Describes the current state of the device (more than one bit can be
|
||||
*set) */
|
||||
typedef struct GapState_s
|
||||
{
|
||||
unsigned advertising : 1; /**< The device is current
|
||||
*advertising */
|
||||
unsigned connected : 1; /**< The peripheral is
|
||||
*connected to a central
|
||||
*device */
|
||||
typedef struct GapState_s {
|
||||
unsigned advertising : 1; /**< peripheral is currently advertising */
|
||||
unsigned connected : 1; /**< peripheral is connected to a central */
|
||||
} GapState_t;
|
||||
|
||||
/* Event callback handlers */
|
||||
void setEventHandler(GapEvents *pEventHandler) {
|
||||
m_pEventHandler = pEventHandler;
|
||||
typedef void (*EventCallback_t)(void);
|
||||
void setOnTimeout(EventCallback_t callback) {
|
||||
onTimeout = callback;
|
||||
}
|
||||
void setOnConnection(EventCallback_t callback) {
|
||||
onConnection = callback;
|
||||
}
|
||||
void setOnDisconnection(EventCallback_t callback) {
|
||||
onDisconnection = callback;
|
||||
}
|
||||
|
||||
void handleEvent(GapEvents::gapEvent_e type) {
|
||||
if (NULL == m_pEventHandler) {
|
||||
return;
|
||||
}
|
||||
switch (type) {
|
||||
case GapEvents::GAP_EVENT_TIMEOUT:
|
||||
state.advertising = 0;
|
||||
m_pEventHandler->onTimeout();
|
||||
if (onTimeout) {
|
||||
onTimeout();
|
||||
}
|
||||
break;
|
||||
case GapEvents::GAP_EVENT_CONNECTED:
|
||||
state.connected = 1;
|
||||
m_pEventHandler->onConnected();
|
||||
if (onConnection) {
|
||||
onConnection();
|
||||
}
|
||||
break;
|
||||
case GapEvents::GAP_EVENT_DISCONNECTED:
|
||||
state.connected = 0;
|
||||
m_pEventHandler->onDisconnected();
|
||||
if (onDisconnection) {
|
||||
onDisconnection();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
Gap() :
|
||||
state(),
|
||||
onTimeout(NULL),
|
||||
onConnection(NULL),
|
||||
onDisconnection(NULL) {
|
||||
/* empty */
|
||||
}
|
||||
|
||||
protected:
|
||||
GapState_t state;
|
||||
|
||||
private:
|
||||
EventCallback_t onTimeout;
|
||||
EventCallback_t onConnection;
|
||||
EventCallback_t onDisconnection;
|
||||
};
|
||||
|
||||
#endif // ifndef __GAP_H__
|
||||
|
|
Loading…
Reference in a new issue