add Central connect(), seperate Central event handler
This commit is contained in:
1 parent
2583f234e6
commit
1e2b632998
5 files changed
+92
-37
No files matched your search
@@ -52,6 +52,9 @@ BLECentral::BLECentral(void)
|
||||
};
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Scan and Parser
|
||||
*------------------------------------------------------------------*/
|
||||
void BLECentral::setScanCallback(scan_callback_t fp)
|
||||
{
|
||||
_scan_cb = fp;
|
||||
@@ -144,3 +147,43 @@ bool BLECentral::checkUuidInScan(const ble_gap_evt_adv_report_t* report, const u
|
||||
{
|
||||
return _checkUuidInScan(report, uuid128, 16);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
err_t BLECentral::connect(const ble_gap_addr_t* peer_addr, uint16_t min_conn_interval, uint16_t max_conn_interval)
|
||||
{
|
||||
ble_gap_conn_params_t gap_conn_params =
|
||||
{
|
||||
.min_conn_interval = min_conn_interval, // in 1.25ms unit
|
||||
.max_conn_interval = max_conn_interval, // in 1.25ms unit
|
||||
.slave_latency = BLE_GAP_CONN_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
};
|
||||
|
||||
return sd_ble_gap_connect(peer_addr, &_scan_param, &gap_conn_params);
|
||||
}
|
||||
|
||||
err_t BLECentral::connect(const ble_gap_evt_adv_report_t* adv_report, uint16_t min_conn_interval, uint16_t max_conn_interval)
|
||||
{
|
||||
return connect(&adv_report->peer_addr, min_conn_interval, max_conn_interval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Event is forwarded from Bluefruit Poll() method
|
||||
* @param event
|
||||
*/
|
||||
void BLECentral::_event_handler(ble_evt_t* evt)
|
||||
{
|
||||
switch ( evt->header.evt_id )
|
||||
{
|
||||
case BLE_GAP_EVT_ADV_REPORT:
|
||||
{
|
||||
ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report;
|
||||
if (_scan_cb) _scan_cb(adv_report);
|
||||
}
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
@@ -50,6 +50,9 @@ class BLECentral
|
||||
public:
|
||||
BLECentral(void); // Constructor
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/* Scan & Parser
|
||||
*------------------------------------------------------------------*/
|
||||
typedef void (*scan_callback_t) (ble_gap_evt_adv_report_t*);
|
||||
|
||||
void setScanCallback(scan_callback_t fp);
|
||||
@@ -59,14 +62,27 @@ class BLECentral
|
||||
uint8_t* extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len);
|
||||
uint8_t* extractScanData(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len);
|
||||
|
||||
bool checkUuidInScan(const ble_gap_evt_adv_report_t* report, uint16_t uuid16);
|
||||
bool checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid128[]);
|
||||
bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, uint16_t uuid16);
|
||||
bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, const uint8_t uuid128[]);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
*------------------------------------------------------------------*/
|
||||
err_t connect(const ble_gap_evt_adv_report_t* adv_report,
|
||||
uint16_t min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT,
|
||||
uint16_t max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT);
|
||||
|
||||
err_t connect(const ble_gap_addr_t *peer_addr,
|
||||
uint16_t min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT,
|
||||
uint16_t max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT);
|
||||
|
||||
private:
|
||||
ble_gap_scan_params_t _scan_param;
|
||||
scan_callback_t _scan_cb;
|
||||
|
||||
bool _checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len);
|
||||
bool _checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len);
|
||||
|
||||
void _event_handler(ble_evt_t* event);
|
||||
|
||||
friend class AdafruitBluefruit;
|
||||
};
|
||||
|
||||
@@ -48,11 +48,6 @@
|
||||
|
||||
#define SVC_CONTEXT_FLAG (BLE_GATTS_SYS_ATTR_FLAG_SYS_SRVCS | BLE_GATTS_SYS_ATTR_FLAG_USR_SRVCS)
|
||||
|
||||
#define GAP_CONN_SUPERVISION_TIMEOUT_MS 3000
|
||||
#define GAP_CONN_SLAVE_LATENCY 0
|
||||
#define GAP_CONN_MIN_INTERVAL_MS 20
|
||||
#define GAP_CONN_MAX_INTERVAL_MS 40
|
||||
|
||||
#define GAP_ADV_INTERVAL_MS 20
|
||||
#define GAP_ADV_TIMEOUT_S 30
|
||||
|
||||
@@ -60,18 +55,6 @@
|
||||
#define CFG_DEFAULT_NAME "Bluefruit52"
|
||||
#define CFG_ADV_BLINKY_INTERVAL 500
|
||||
|
||||
// Converts an integer of 1.25ms units to msecs
|
||||
#define MS100TO125(ms100) (((ms100)*4)/5)
|
||||
|
||||
// Converts an integer of 1.25ms units to msecs
|
||||
#define MS125TO100(ms125) (((ms125)*5)/4)
|
||||
|
||||
// Converts msec to 0.625 unit
|
||||
#define MS1000TO625(ms1000) (((ms1000)*8)/5)
|
||||
|
||||
// Converts an integer of 625ms units to msecs
|
||||
#define MS625TO1000(u625) ( ((u625)*5) / 8 )
|
||||
|
||||
#define BLUEFRUIT_TASK_STACKSIZE (512*3)
|
||||
|
||||
extern "C"
|
||||
@@ -173,10 +156,10 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
|
||||
// Connection Parameters
|
||||
ble_gap_conn_params_t gap_conn_params =
|
||||
{
|
||||
.min_conn_interval = MS100TO125(GAP_CONN_MIN_INTERVAL_MS) , // in 1.25ms unit
|
||||
.max_conn_interval = MS100TO125(GAP_CONN_MAX_INTERVAL_MS) , // in 1.25ms unit
|
||||
.slave_latency = GAP_CONN_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
.min_conn_interval = BLE_GAP_CONN_MIN_INTERVAL_DFLT, // in 1.25ms unit
|
||||
.max_conn_interval = BLE_GAP_CONN_MAX_INTERVAL_DFLT, // in 1.25ms unit
|
||||
.slave_latency = BLE_GAP_CONN_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
};
|
||||
|
||||
VERIFY_STATUS( sd_ble_gap_ppcp_set(&gap_conn_params) );
|
||||
@@ -214,8 +197,8 @@ err_t AdafruitBluefruit::setConnInterval(uint16_t min, uint16_t max)
|
||||
{
|
||||
.min_conn_interval = min, // in 1.25ms unit
|
||||
.max_conn_interval = max, // in 1.25ms unit
|
||||
.slave_latency = GAP_CONN_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
.slave_latency = BLE_GAP_CONN_SLAVE_LATENCY,
|
||||
.conn_sup_timeout = BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS / 10 // in 10ms unit
|
||||
};
|
||||
|
||||
return sd_ble_gap_ppcp_set(&gap_conn_params);
|
||||
@@ -781,19 +764,14 @@ void AdafruitBluefruit::_poll(void)
|
||||
break;
|
||||
|
||||
|
||||
case BLE_GAP_EVT_ADV_REPORT:
|
||||
{
|
||||
ble_gap_evt_adv_report_t* adv_report = &evt->evt.gap_evt.params.adv_report;
|
||||
if (central._scan_cb) central._scan_cb(adv_report);
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
//PRINT_HEX(evt->header.evt_id);
|
||||
break;
|
||||
}
|
||||
|
||||
// Central Event Handler
|
||||
if (_central_enabled) central._event_handler(evt);
|
||||
|
||||
// GATTs characteristics event handler
|
||||
for(int i=0; i<_chars_count; i++)
|
||||
{
|
||||
|
||||
@@ -100,8 +100,8 @@ class AdafruitBluefruit
|
||||
bool addScanRespUuid(uint16_t uuid16);
|
||||
bool addScanRespUuid(uint8_t const uuid128[]);
|
||||
|
||||
err_t startAdvertising(void);
|
||||
void stopAdvertising(void);
|
||||
err_t startAdvertising(void);
|
||||
void stopAdvertising(void);
|
||||
|
||||
/*------------------------------------------------------------------*/
|
||||
/*
|
||||
@@ -113,7 +113,7 @@ class AdafruitBluefruit
|
||||
void disconnect(void);
|
||||
bool connected(void);
|
||||
|
||||
err_t setConnInterval(uint16_t min, uint16_t max);
|
||||
err_t setConnInterval (uint16_t min, uint16_t max);
|
||||
err_t setConnIntervalMS(uint16_t min_ms, uint16_t max_ms);
|
||||
|
||||
uint16_t connHandle(void);
|
||||
|
||||
@@ -41,4 +41,22 @@
|
||||
#include "ble_hci.h"
|
||||
#include "nrf_sdm.h"
|
||||
|
||||
#define BLE_GAP_CONN_SUPERVISION_TIMEOUT_MS 3000
|
||||
#define BLE_GAP_CONN_SLAVE_LATENCY 0
|
||||
|
||||
#define BLE_GAP_CONN_MIN_INTERVAL_DFLT MS100TO125(20)
|
||||
#define BLE_GAP_CONN_MAX_INTERVAL_DFLT MS100TO125(40)
|
||||
|
||||
// Converts an integer of 1.25ms units to msecs
|
||||
#define MS100TO125(ms100) (((ms100)*4)/5)
|
||||
|
||||
// Converts an integer of 1.25ms units to msecs
|
||||
#define MS125TO100(ms125) (((ms125)*5)/4)
|
||||
|
||||
// Converts msec to 0.625 unit
|
||||
#define MS1000TO625(ms1000) (((ms1000)*8)/5)
|
||||
|
||||
// Converts an integer of 625ms units to msecs
|
||||
#define MS625TO1000(u625) ( ((u625)*5) / 8 )
|
||||
|
||||
#endif /* BLUEFRUIT_COMMON_H_ */
|
||||
Reference in new issue
Block a user