make gatt support both client & server
This commit is contained in:
1 parent
a3cd4104b2
commit
852e100007
8 files changed
+83
-81
No files matched your search
@@ -47,12 +47,6 @@ BLECentral::BLECentral(void)
|
||||
|
||||
_txpacket_sem = NULL;
|
||||
|
||||
_svc_count = 0;
|
||||
for(uint8_t i=0; i<BLE_CENTRAL_MAX_SERVICE; i++) _svc_list[i] = NULL;
|
||||
|
||||
_chars_count = 0;
|
||||
for(uint8_t i=0; i<BLE_CENTRAL_MAX_CHARS; i++) _chars_list[i] = NULL;
|
||||
|
||||
_scan_cb = NULL;
|
||||
_scan_param = (ble_gap_scan_params_t) {
|
||||
.active = 1,
|
||||
@@ -74,22 +68,6 @@ void BLECentral::begin(void)
|
||||
Bluefruit.Discovery.begin();
|
||||
}
|
||||
|
||||
bool BLECentral::_registerService(BLECentralService* svc)
|
||||
{
|
||||
VERIFY( _svc_count < BLE_CENTRAL_MAX_SERVICE );
|
||||
_svc_list[ _svc_count++ ] = svc;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLECentral::_registerCharacteristic(BLECentralCharacteristic* chr)
|
||||
{
|
||||
VERIFY( _chars_count < BLE_CENTRAL_MAX_CHARS );
|
||||
_chars_list[ _chars_count++ ] = chr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLECentral::getTxPacket(uint32_t ms)
|
||||
{
|
||||
VERIFY(_txpacket_sem != NULL);
|
||||
@@ -293,9 +271,6 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
vSemaphoreDelete(_txpacket_sem);
|
||||
_txpacket_sem = NULL;
|
||||
|
||||
// disconnect all registered services
|
||||
for(uint8_t i=0; i<_svc_count; i++) _svc_list[i]->disconnect();
|
||||
|
||||
if ( _disconnect_cb ) _disconnect_cb(evt->evt.gap_evt.params.disconnected.reason);
|
||||
|
||||
startScanning();
|
||||
@@ -323,28 +298,4 @@ void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
// GATT characteristics event handler only if Connection Handle matches
|
||||
if ( evt_conn_hdl == _conn_hdl)
|
||||
{
|
||||
for(int i=0; i<_chars_count; i++)
|
||||
{
|
||||
bool matched = false;
|
||||
|
||||
switch(evt->header.evt_id)
|
||||
{
|
||||
case BLE_GATTC_EVT_HVX:
|
||||
case BLE_GATTC_EVT_WRITE_RSP:
|
||||
case BLE_GATTC_EVT_READ_RSP:
|
||||
// write & read response's handle has same offset.
|
||||
matched = (_chars_list[i]->_chr.handle_value == evt->evt.gattc_evt.params.write_rsp.handle);
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
// invoke charactersistic handler if matched
|
||||
if ( matched ) _chars_list[i]->_eventHandler(evt);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -46,9 +46,6 @@
|
||||
#include "BLECentralCharacteristic.h"
|
||||
#include "BLECentralService.h"
|
||||
|
||||
#define BLE_CENTRAL_MAX_SERVICE 10
|
||||
#define BLE_CENTRAL_MAX_CHARS 30
|
||||
|
||||
|
||||
class AdafruitBluefruit;
|
||||
|
||||
@@ -97,26 +94,12 @@ class BLECentral
|
||||
void setConnectCallback ( connect_callback_t fp);
|
||||
void setDisconnectCallback( disconnect_callback_t fp);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
* code. User should not call these directly
|
||||
*------------------------------------------------------------------*/
|
||||
bool _registerService(BLECentralService* svc);
|
||||
bool _registerCharacteristic(BLECentralCharacteristic* chr);
|
||||
|
||||
private:
|
||||
uint16_t _conn_hdl;
|
||||
|
||||
// Transmission Buffer Count for HVX notification, max is seen at 7
|
||||
SemaphoreHandle_t _txpacket_sem;
|
||||
|
||||
BLECentralService* _svc_list[BLE_CENTRAL_MAX_SERVICE];
|
||||
uint8_t _svc_count;
|
||||
|
||||
BLECentralCharacteristic* _chars_list[BLE_CENTRAL_MAX_CHARS];
|
||||
uint8_t _chars_count;
|
||||
|
||||
ble_gap_scan_params_t _scan_param;
|
||||
scan_callback_t _scan_cb;
|
||||
|
||||
|
||||
@@ -106,7 +106,7 @@ void BLECentralCharacteristic::begin(void)
|
||||
_service = BLECentralService::lastService;
|
||||
|
||||
// Register to Bluefruit (required for callback and write response)
|
||||
(void) Bluefruit.Central._registerCharacteristic(this);
|
||||
(void) Bluefruit.Gatt._addCharacteristic(this);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
|
||||
@@ -88,7 +88,7 @@ class BLECentralCharacteristic
|
||||
void _init (void);
|
||||
void _eventHandler (ble_evt_t* event);
|
||||
|
||||
friend class BLECentral;
|
||||
friend class BLEGatt;
|
||||
};
|
||||
|
||||
#endif /* BLECENTRALCHARACTERISTIC_H_ */
|
||||
@@ -63,7 +63,7 @@ bool BLECentralService::begin(void)
|
||||
uuid.begin();
|
||||
|
||||
lastService = this;
|
||||
(void) Bluefruit.Central._registerService(this);
|
||||
(void) Bluefruit.Gatt._addService(this);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ class BLECentralService
|
||||
virtual bool discover (uint16_t conn_handle);
|
||||
bool discovered(void);
|
||||
|
||||
friend class BLECentral;
|
||||
friend class BLEGatt;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -39,24 +39,68 @@
|
||||
|
||||
BLEGatt::BLEGatt(void)
|
||||
{
|
||||
_chars_count = 0;
|
||||
for(uint8_t i=0; i<BLE_GATT_MAX_SERVER_CHARS; i++) _chars_list[i] = NULL;
|
||||
|
||||
|
||||
varclr(&server);
|
||||
varclr(&client);
|
||||
}
|
||||
|
||||
void BLEGatt::_eventHandler(ble_evt_t* evt)
|
||||
{
|
||||
// Server Characteristics
|
||||
for(int i=0; i<_chars_count; i++) _chars_list[i]->_eventHandler(evt);
|
||||
for(int i=0; i<server._chars_count; i++)
|
||||
{
|
||||
server._chars_list[i]->_eventHandler(evt);
|
||||
}
|
||||
|
||||
// Client Characteristics
|
||||
for(int i=0; i<client._chars_count; i++)
|
||||
{
|
||||
bool matched = false;
|
||||
|
||||
switch(evt->header.evt_id)
|
||||
{
|
||||
case BLE_GATTC_EVT_HVX:
|
||||
case BLE_GATTC_EVT_WRITE_RSP:
|
||||
case BLE_GATTC_EVT_READ_RSP:
|
||||
// write & read & hvc response's handle has same offset from the struct
|
||||
matched = (client._chars_list[i]->_chr.handle_value == evt->evt.gattc_evt.params.write_rsp.handle);
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
// invoke charactersistic handler if matched
|
||||
if ( matched ) client._chars_list[i]->_eventHandler(evt);
|
||||
}
|
||||
|
||||
// disconnect Client Service
|
||||
if ( evt->header.evt_id == BLE_GAP_EVT_DISCONNECTED )
|
||||
{
|
||||
for(int i=0; i<client._svc_count; i++) client._svc_list[i]->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
bool BLEGatt::_addCharacteristic(BLECharacteristic* chars)
|
||||
bool BLEGatt::_addCharacteristic(BLECharacteristic* chr)
|
||||
{
|
||||
if ( _chars_count == BLE_GATT_MAX_SERVER_CHARS ) return false;
|
||||
_chars_list[ _chars_count++ ] = chars;
|
||||
if ( server._chars_count == BLE_GATT_MAX_SERVER_CHARS ) return false;
|
||||
server._chars_list[ server._chars_count++ ] = chr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool BLEGatt::_addCharacteristic(BLECentralCharacteristic* chr)
|
||||
{
|
||||
VERIFY( client._chars_count < BLE_CENTRAL_MAX_CHARS );
|
||||
client._chars_list[ client._chars_count++ ] = chr;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BLEGatt::_addService(BLECentralService* svc)
|
||||
{
|
||||
VERIFY( client._svc_count < BLE_CENTRAL_MAX_SERVICE );
|
||||
client._svc_list[ client._svc_count++ ] = svc;
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -47,18 +47,42 @@
|
||||
#include "BLECentralService.h"
|
||||
|
||||
#define BLE_GATT_MAX_SERVER_CHARS 40
|
||||
#define BLE_CENTRAL_MAX_SERVICE 10
|
||||
#define BLE_CENTRAL_MAX_CHARS 30
|
||||
|
||||
|
||||
class BLEGatt
|
||||
{
|
||||
public:
|
||||
BLEGatt(void);
|
||||
|
||||
bool _addCharacteristic(BLECharacteristic* chars);
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
* code. User should not call these directly
|
||||
*------------------------------------------------------------------*/
|
||||
// Server
|
||||
bool _addCharacteristic(BLECharacteristic* chr);
|
||||
|
||||
// Client
|
||||
bool _addCharacteristic(BLECentralCharacteristic* chr);
|
||||
bool _addService(BLECentralService* svc);
|
||||
|
||||
void _eventHandler(ble_evt_t* evt);
|
||||
|
||||
private:
|
||||
BLECharacteristic* _chars_list[BLE_GATT_MAX_SERVER_CHARS];
|
||||
uint8_t _chars_count;
|
||||
struct {
|
||||
uint8_t _chars_count;
|
||||
BLECharacteristic* _chars_list[BLE_GATT_MAX_SERVER_CHARS];
|
||||
} server;
|
||||
|
||||
struct {
|
||||
uint8_t _svc_count;
|
||||
BLECentralService* _svc_list[BLE_CENTRAL_MAX_SERVICE];
|
||||
|
||||
uint8_t _chars_count;
|
||||
BLECentralCharacteristic* _chars_list[BLE_CENTRAL_MAX_CHARS];
|
||||
}client;
|
||||
};
|
||||
|
||||
#endif /* BLEGATT_H_ */
|
||||
Reference in new issue
Block a user