3636
3737#include " bluefruit.h"
3838
39+ #define BLE_CENTRAL_TIMEOUT 10000
40+
3941/* *
4042 * Constructor
4143 */
4244BLECentral::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}
0 commit comments