Skip to content

Commit 660c4c7

Browse files
committed
adding central support
1 parent 0d9df86 commit 660c4c7

File tree

3 files changed

+97
-14
lines changed

3 files changed

+97
-14
lines changed

libraries/Bluefruit52Lib/src/BLECentral.cpp

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,18 @@
3636

3737
#include "bluefruit.h"
3838

39+
#define BLE_CENTRAL_TIMEOUT 10000
40+
3941
/**
4042
* Constructor
4143
*/
4244
BLECentral::BLECentral(void)
4345
{
4446
_conn_hdl = BLE_CONN_HANDLE_INVALID;
4547

48+
_evt_sem = NULL;
49+
_evt_data = NULL;
50+
4651
_scan_cb = NULL;
4752
_scan_param = (ble_gap_scan_params_t) {
4853
.active = 1,
@@ -57,6 +62,11 @@ BLECentral::BLECentral(void)
5762
_discconnect_cb = NULL;
5863
}
5964

65+
void BLECentral::begin(void)
66+
{
67+
_evt_sem = xSemaphoreCreateBinary();
68+
}
69+
6070
/*------------------------------------------------------------------*/
6171
/* Scan and Parser
6272
*------------------------------------------------------------------*/
@@ -106,19 +116,28 @@ uint8_t* BLECentral::extractScanData(const ble_gap_evt_adv_report_t* report, uin
106116
return extractScanData(report->data, report->dlen, type, result_len);
107117
}
108118

109-
bool BLECentral::_checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid[], uint8_t uuid_len)
119+
bool BLECentral::checkUuidInScan(const ble_gap_evt_adv_report_t* report, BLEUuid ble_uuid)
110120
{
121+
const uint8_t* uuid;
122+
uint8_t uuid_len = ble_uuid.size();
123+
111124
uint8_t type_arr[2];
112125

126+
PRINT_INT(uuid_len);
127+
113128
// Check both UUID16 more available and complete list
114129
if ( uuid_len == 2)
115130
{
116131
type_arr[0] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE;
117132
type_arr[1] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE;
133+
134+
uuid = (uint8_t*) &ble_uuid._uuid.uuid;
118135
}else
119136
{
120137
type_arr[0] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE;
121138
type_arr[1] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE;
139+
140+
uuid = ble_uuid._uuid128;
122141
}
123142

124143
for (int i=0; i<2; i++)
@@ -143,16 +162,6 @@ bool BLECentral::_checkUuidInScan(const ble_gap_evt_adv_report_t* report, const
143162
return false;
144163
}
145164

146-
bool BLECentral::checkUuidInScan(const ble_gap_evt_adv_report_t* report, uint16_t uuid16)
147-
{
148-
return _checkUuidInScan(report, (uint8_t*) &uuid16, 2);
149-
}
150-
151-
bool BLECentral::checkUuidInScan(const ble_gap_evt_adv_report_t* report, const uint8_t uuid128[])
152-
{
153-
return _checkUuidInScan(report, uuid128, 16);
154-
}
155-
156165
/*------------------------------------------------------------------*/
157166
/*
158167
*------------------------------------------------------------------*/
@@ -189,6 +198,43 @@ void BLECentral::setDisconnectCallback( disconnect_callback_t fp)
189198
_discconnect_cb = fp;
190199
}
191200

201+
/*------------------------------------------------------------------*/
202+
/* DISCOVERY
203+
*------------------------------------------------------------------*/
204+
bool BLECentral::discoverService(BLEUuid uuid, ble_gattc_handle_range_t* handle_range, uint16_t start_handle)
205+
{
206+
uuid.begin(); // add uuid128 if needed
207+
208+
VERIFY_STATUS( sd_ble_gattc_primary_services_discover(_conn_hdl, start_handle, &uuid._uuid), false );
209+
210+
// wait for discovery event
211+
if ( !xSemaphoreTake(_evt_sem, BLE_CENTRAL_TIMEOUT) || !_evt_data ) return false;
212+
213+
bool result = false;
214+
215+
ble_gattc_evt_prim_srvc_disc_rsp_t* discover_svc = (ble_gattc_evt_prim_srvc_disc_rsp_t*) _evt_data;
216+
217+
if ( (discover_svc->count == 1) && (uuid == discover_svc->services[0].uuid) )
218+
{
219+
(*handle_range) = discover_svc->services[0].handle_range;
220+
result = true;
221+
}
222+
223+
free(_evt_data);
224+
225+
return result;
226+
}
227+
228+
bool BLECentral::discoverCharacteristic(ble_gattc_handle_range_t handle_range)
229+
{
230+
// uuid.begin(); // add uuid128 if needed
231+
232+
VERIFY_STATUS( sd_ble_gattc_characteristics_discover(_conn_hdl, &handle_range), false );
233+
234+
return true;
235+
}
236+
237+
192238
/**
193239
* Event is forwarded from Bluefruit Poll() method
194240
* @param event
@@ -241,6 +287,32 @@ void BLECentral::_event_handler(ble_evt_t* evt)
241287
}
242288
break;
243289

290+
case BLE_GATTC_EVT_PRIM_SRVC_DISC_RSP:
291+
{
292+
ble_gattc_evt_t* gattc = &evt->evt.gattc_evt;
293+
294+
if ( _conn_hdl == gattc->conn_handle )
295+
{
296+
if ( _evt_data ) rtos_free(_evt_data);
297+
_evt_data = NULL;
298+
299+
if (gattc->gatt_status == BLE_GATT_STATUS_SUCCESS)
300+
{
301+
ble_gattc_evt_prim_srvc_disc_rsp_t* svc_rsp = &gattc->params.prim_srvc_disc_rsp;
302+
303+
// len of the discovered services
304+
uint16_t len = sizeof(ble_gattc_evt_prim_srvc_disc_rsp_t) + (svc_rsp->count-1)*sizeof(ble_gattc_service_t);
305+
306+
_evt_data = rtos_malloc( len );
307+
308+
memcpy(_evt_data, svc_rsp, len);
309+
}
310+
311+
xSemaphoreGive(_evt_sem);
312+
}
313+
}
314+
break;
315+
244316
default: break;
245317
}
246318
}

libraries/Bluefruit52Lib/src/BLECentral.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class BLECentral
5050
public:
5151
BLECentral(void); // Constructor
5252

53+
void begin(void);
54+
5355
/*------------------------------------------------------------------*/
5456
/* Scan & Parser
5557
*------------------------------------------------------------------*/
@@ -62,8 +64,7 @@ class BLECentral
6264
uint8_t* extractScanData(uint8_t const* scandata, uint8_t scanlen, uint8_t type, uint8_t* result_len);
6365
uint8_t* extractScanData(const ble_gap_evt_adv_report_t* report, uint8_t type, uint8_t* result_len);
6466

65-
bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, uint16_t uuid16);
66-
bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, const uint8_t uuid128[]);
67+
bool checkUuidInScan(const ble_gap_evt_adv_report_t* adv_report, BLEUuid ble_uuid);
6768

6869
/*------------------------------------------------------------------*/
6970
/*
@@ -81,8 +82,12 @@ class BLECentral
8182
/*------------------------------------------------------------------*/
8283
/* GATTC Discovery
8384
*------------------------------------------------------------------*/
85+
bool discoverService(BLEUuid uuid, ble_gattc_handle_range_t* handle_range, uint16_t start_handle = 1);
86+
bool discoverCharacteristic(ble_gattc_handle_range_t handle_range);
8487

85-
88+
/*------------------------------------------------------------------*/
89+
/* CALLBACKS
90+
*------------------------------------------------------------------*/
8691
typedef void (*connect_callback_t) (void);
8792
typedef void (*disconnect_callback_t) (uint8_t reason);
8893

@@ -92,6 +97,9 @@ class BLECentral
9297
private:
9398
uint16_t _conn_hdl;
9499

100+
SemaphoreHandle_t _evt_sem;
101+
void* _evt_data;
102+
95103
ble_gap_scan_params_t _scan_param;
96104
scan_callback_t _scan_cb;
97105

libraries/Bluefruit52Lib/src/bluefruit.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ err_t AdafruitBluefruit::begin(bool prph_enable, bool central_enable)
191191
/*------------- DFU OTA as built-in service -------------*/
192192
_dfu_svc.begin();
193193

194+
if (_central_enabled) Central.begin(); // Init Central
195+
194196
// Create RTOS Semaphore & Task for BLE Event
195197
_ble_event_sem = xSemaphoreCreateBinary();
196198
VERIFY(_ble_event_sem, NRF_ERROR_NO_MEM);
@@ -648,6 +650,7 @@ COMMENT_OUT(
648650

649651
case BLE_GAP_EVT_AUTH_STATUS:
650652
{
653+
// Bonding process completed
651654
ble_gap_evt_auth_status_t* status = &evt->evt.gap_evt.params.auth_status;
652655

653656
// Pairing/Bonding succeeded --> save encryption keys

0 commit comments

Comments
 (0)