36
36
37
37
#include " bluefruit.h"
38
38
39
+ #define BLE_CENTRAL_TIMEOUT 10000
40
+
39
41
/* *
40
42
* Constructor
41
43
*/
42
44
BLECentral::BLECentral (void )
43
45
{
44
46
_conn_hdl = BLE_CONN_HANDLE_INVALID;
45
47
48
+ _evt_sem = NULL ;
49
+ _evt_data = NULL ;
50
+
46
51
_scan_cb = NULL ;
47
52
_scan_param = (ble_gap_scan_params_t ) {
48
53
.active = 1 ,
@@ -57,6 +62,11 @@ BLECentral::BLECentral(void)
57
62
_discconnect_cb = NULL ;
58
63
}
59
64
65
+ void BLECentral::begin (void )
66
+ {
67
+ _evt_sem = xSemaphoreCreateBinary ();
68
+ }
69
+
60
70
/* ------------------------------------------------------------------*/
61
71
/* Scan and Parser
62
72
*------------------------------------------------------------------*/
@@ -106,19 +116,28 @@ uint8_t* BLECentral::extractScanData(const ble_gap_evt_adv_report_t* report, uin
106
116
return extractScanData (report->data , report->dlen , type, result_len);
107
117
}
108
118
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 )
110
120
{
121
+ const uint8_t * uuid;
122
+ uint8_t uuid_len = ble_uuid.size ();
123
+
111
124
uint8_t type_arr[2 ];
112
125
126
+ PRINT_INT (uuid_len);
127
+
113
128
// Check both UUID16 more available and complete list
114
129
if ( uuid_len == 2 )
115
130
{
116
131
type_arr[0 ] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_MORE_AVAILABLE;
117
132
type_arr[1 ] = BLE_GAP_AD_TYPE_16BIT_SERVICE_UUID_COMPLETE;
133
+
134
+ uuid = (uint8_t *) &ble_uuid._uuid .uuid ;
118
135
}else
119
136
{
120
137
type_arr[0 ] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_MORE_AVAILABLE;
121
138
type_arr[1 ] = BLE_GAP_AD_TYPE_128BIT_SERVICE_UUID_COMPLETE;
139
+
140
+ uuid = ble_uuid._uuid128 ;
122
141
}
123
142
124
143
for (int i=0 ; i<2 ; i++)
@@ -143,16 +162,6 @@ bool BLECentral::_checkUuidInScan(const ble_gap_evt_adv_report_t* report, const
143
162
return false ;
144
163
}
145
164
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
-
156
165
/* ------------------------------------------------------------------*/
157
166
/*
158
167
*------------------------------------------------------------------*/
@@ -189,6 +198,43 @@ void BLECentral::setDisconnectCallback( disconnect_callback_t fp)
189
198
_discconnect_cb = fp;
190
199
}
191
200
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
+
192
238
/* *
193
239
* Event is forwarded from Bluefruit Poll() method
194
240
* @param event
@@ -241,6 +287,32 @@ void BLECentral::_event_handler(ble_evt_t* evt)
241
287
}
242
288
break ;
243
289
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
+
244
316
default : break ;
245
317
}
246
318
}
0 commit comments