High level Bluetooth Low Energy API and radio abstraction layer
Fork of BLE_API by
Diff: hw/Gap.h
- Revision:
- 31:2c94f0501807
- Parent:
- 30:9614522cf932
- Child:
- 33:6d51a2c69442
--- a/hw/Gap.h Fri Jan 17 14:25:29 2014 +0000 +++ b/hw/Gap.h Tue Apr 01 11:04:56 2014 +0100 @@ -21,6 +21,7 @@ #include "blecommon.h" #include "GapAdvertisingData.h" #include "GapAdvertisingParams.h" +#include "GapEvents.h" /**************************************************************************/ /*! @@ -31,40 +32,45 @@ /**************************************************************************/ class Gap { - protected: - FunctionPointer m_callback_event; - + private: + GapEvents *m_pEventHandler; + public: - /******************************************************************/ - /*! - \brief - Identifies GAP events generated by the radio HW when an event - callback occurs - */ - /******************************************************************/ - typedef enum gap_event_e - { - GAP_EVENT_ADVERTISING_STARTED = 1, - GAP_EVENT_CONNECTED = 2, - GAP_EVENT_DISCONNECTED = 3 - } gapEvent_t; + /* These functions must be defined in the sub-class */ + virtual ble_error_t setAdvertisingData(GapAdvertisingData &, GapAdvertisingData &) = 0; + virtual ble_error_t startAdvertising(GapAdvertisingParams &) = 0; + virtual ble_error_t stopAdvertising(void) = 0; + virtual ble_error_t disconnect(void) = 0; + + /* 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 */ + } GapState_t; - /* These functions must be defined in the sub-class */ - virtual ble_error_t setAdvertising(GapAdvertisingParams &, GapAdvertisingData &, GapAdvertisingData &) = 0; - virtual ble_error_t startAdvertising(void) = 0; - virtual ble_error_t stopAdvertising(void) = 0; - - uint16_t state; /* Initialising, Advertising, Scanning, Connected, etc. ... more than one bit can be set at a time! */ - - /* Event callback */ - void attach(void (*function)(void)) { - m_callback_event.attach( function ); - } - - template<typename T> - void attach(T *object, void (T::*member)(void)) { - m_callback_event.attach( object, member ); - } + /* Event callback handlers */ + void setEventHandler(GapEvents *pEventHandler) {m_pEventHandler = pEventHandler;} + void handleEvent(GapEvents::gapEvent_e type) { + if (NULL == m_pEventHandler) + return; + switch(type) { + case GapEvents::GAP_EVENT_TIMEOUT: + state.advertising = 0; + m_pEventHandler->onTimeout(); + break; + case GapEvents::GAP_EVENT_CONNECTED: + state.connected = 1; + m_pEventHandler->onConnected(); + break; + case GapEvents::GAP_EVENT_DISCONNECTED: + state.connected = 0; + m_pEventHandler->onDisconnected(); + break; + } + } + + GapState_t state; }; #endif