summaryrefslogtreecommitdiff
path: root/sensor/patchedBLE/src/BLEService.cpp
blob: 340ea560008e15484deaa8b3c8c6994593e02880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * BLEService.cpp
 *
 *  Created on: Mar 25, 2017
 *      Author: kolban
 */
 
// A service is identified by a UUID.  A service is also the container for one or more characteristics. 
 
#include "sdkconfig.h"
#if defined(CONFIG_BT_ENABLED)
#include <esp_err.h>
#include <esp_gatts_api.h>
#include <esp_log.h>
 
#include <iomanip>
#include <sstream>
#include <string>
 
#include "BLEServer.h"
#include "BLEService.h"
#include "BLEUtils.h"
#include "GeneralUtils.h"
 
#ifdef ARDUINO_ARCH_ESP32
#include "esp32-hal-log.h"
#endif
 
#define NULL_HANDLE (0xffff)
 
static const char* LOG_TAG = "BLEService"// Tag for logging. 
 
/**
 * @brief Construct an instance of the BLEService
 * @param [in] uuid The UUID of the service.
 * @param [in] numHandles The maximum number of handles associated with the service.
 */
BLEService::BLEService(const char* uuid, uint32_t numHandles) : BLEService(BLEUUID(uuid), numHandles) {
}
 
 
/**
 * @brief Construct an instance of the BLEService
 * @param [in] uuid The UUID of the service.
 * @param [in] numHandles The maximum number of handles associated with the service.
 */
BLEService::BLEService(BLEUUID uuid, uint32_t numHandles) {
m_uuid      = uuid;
m_handle    = NULL_HANDLE;
m_pServer   = nullptr;
//m_serializeMutex.setName("BLEService"); 
m_lastCreatedCharacteristic = nullptr;
m_numHandles = numHandles;
} // BLEService 
 
 
/**
 * @brief Create the service.
 * Create the service.
 * @param [in] gatts_if The handle of the GATT server interface.
 * @return N/A.
 */
 
void BLEService::executeCreate(BLEServer *pServer) {
//ESP_LOGD(LOG_TAG, ">> executeCreate() - Creating service (esp_ble_gatts_create_service) service uuid: %s", getUUID().toString().c_str()); 
//getUUID(); // Needed for a weird bug fix 
//char x[1000]; 
//memcpy(x, &m_uuid, sizeof(m_uuid)); 
//char x[10]; 
//memcpy(x, &deleteMe, 10); 
m_pServer          = pServer;
m_semaphoreCreateEvt.take("executeCreate"); // Take the mutex and release at event ESP_GATTS_CREATE_EVT 
 
esp_gatt_srvc_id_t srvc_id;
srvc_id.is_primary = true;
srvc_id.id.inst_id = m_id;
srvc_id.id.uuid    = *m_uuid.getNative();
esp_err_t errRc = ::esp_ble_gatts_create_service(
getServer()->getGattsIf(),
&srvc_id,
m_numHandles   // The maximum number of handles associated with the service. 
);
 
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gatts_create_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
 
m_semaphoreCreateEvt.wait("executeCreate");
ESP_LOGD(LOG_TAG, "<< executeCreate");
} // executeCreate 
 
 
/**
 * @brief Delete the service.
 * Delete the service.
 * @return N/A.
 */
 
void BLEService::executeDelete() {
ESP_LOGD(LOG_TAG, ">> executeDelete()");
m_semaphoreDeleteEvt.take("executeDelete"); // Take the mutex and release at event ESP_GATTS_DELETE_EVT 
 
esp_err_t errRc = ::esp_ble_gatts_delete_service( getHandle() );
 
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "esp_ble_gatts_delete_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
 
m_semaphoreDeleteEvt.wait("executeDelete");
ESP_LOGD(LOG_TAG, "<< executeDelete");
} // executeDelete 
 
 
/**
 * @brief Dump details of this BLE GATT service.
 * @return N/A.
 */
void BLEService::dump() {
ESP_LOGD(LOG_TAG, "Service: uuid:%s, handle: 0x%.2x",
m_uuid.toString().c_str(),
m_handle);
ESP_LOGD(LOG_TAG, "Characteristics:\n%s", m_characteristicMap.toString().c_str());
} // dump 
 
 
/**
 * @brief Get the UUID of the service.
 * @return the UUID of the service.
 */
BLEUUID BLEService::getUUID() {
return m_uuid;
} // getUUID 
 
 
/**
 * @brief Start the service.
 * Here we wish to start the service which means that we will respond to partner requests about it.
 * Starting a service also means that we can create the corresponding characteristics.
 * @return Start the service.
 */
void BLEService::start() {
// We ask the BLE runtime to start the service and then create each of the characteristics. 
// We start the service through its local handle which was returned in the ESP_GATTS_CREATE_EVT event 
// obtained as a result of calling esp_ble_gatts_create_service(). 
// 
ESP_LOGD(LOG_TAG, ">> start(): Starting service (esp_ble_gatts_start_service): %s", toString().c_str());
if (m_handle == NULL_HANDLE) {
ESP_LOGE(LOG_TAG, "<< !!! We attempted to start a service but don't know its handle!");
return;
}
 
 
BLECharacteristic *pCharacteristic = m_characteristicMap.getFirst();
 
while(pCharacteristic != nullptr{
m_lastCreatedCharacteristic = pCharacteristic;
pCharacteristic->executeCreate(this);
 
pCharacteristic = m_characteristicMap.getNext();
}
// Start each of the characteristics ... these are found in the m_characteristicMap. 
 
m_semaphoreStartEvt.take("start");
esp_err_t errRc = ::esp_ble_gatts_start_service(m_handle);
 
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_start_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
m_semaphoreStartEvt.wait("start");
 
ESP_LOGD(LOG_TAG, "<< start()");
} // start 
 
 
/**
 * @brief Stop the service.
 * @return Stop the service.
 */
void BLEService::stop() {
// We ask the BLE runtime to start the service and then create each of the characteristics. 
// We start the service through its local handle which was returned in the ESP_GATTS_CREATE_EVT event 
// obtained as a result of calling esp_ble_gatts_create_service(). 
// 
ESP_LOGD(LOG_TAG, ">> stop(): Stopping service (esp_ble_gatts_stop_service): %s", toString().c_str());
if (m_handle == NULL_HANDLE) {
ESP_LOGE(LOG_TAG, "<< !!! We attempted to stop a service but don't know its handle!");
return;
}
 
m_semaphoreStopEvt.take("stop");
esp_err_t errRc = ::esp_ble_gatts_stop_service(m_handle);
 
if (errRc != ESP_OK) {
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_stop_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
return;
}
m_semaphoreStopEvt.wait("stop");
 
ESP_LOGD(LOG_TAG, "<< stop()");
} // start 
 
 
/**
 * @brief Set the handle associated with this service.
 * @param [in] handle The handle associated with the service.
 */
void BLEService::setHandle(uint16_t handle) {
ESP_LOGD(LOG_TAG, ">> setHandle - Handle=0x%.2x, service UUID=%s)", handle, getUUID().toString().c_str());
if (m_handle != NULL_HANDLE) {
ESP_LOGE(LOG_TAG, "!!! Handle is already set %.2x", m_handle);
return;
}
m_handle = handle;
ESP_LOGD(LOG_TAG, "<< setHandle");
} // setHandle 
 
 
/**
 * @brief Get the handle associated with this service.
 * @return The handle associated with this service.
 */
uint16_t BLEService::getHandle() {
return m_handle;
} // getHandle 
 
 
/**
 * @brief Add a characteristic to the service.
 * @param [in] pCharacteristic A pointer to the characteristic to be added.
 */
void BLEService::addCharacteristic(BLECharacteristic* pCharacteristic) {
// We maintain a mapping of characteristics owned by this service.  These are managed by the 
// BLECharacteristicMap class instance found in m_characteristicMap.  We add the characteristic 
// to the map and then ask the service to add the characteristic at the BLE level (ESP-IDF). 
// 
ESP_LOGD(LOG_TAG, ">> addCharacteristic()");
ESP_LOGD(LOG_TAG, "Adding characteristic: uuid=%s to service: %s",
pCharacteristic->getUUID().toString().c_str(),
toString().c_str());
 
// Check that we don't add the same characteristic twice. 
if (m_characteristicMap.getByUUID(pCharacteristic->getUUID()) != nullptr{
ESP_LOGW(LOG_TAG, "<< Adding a new characteristic with the same UUID as a previous one");
//return; 
}
 
// Remember this characteristic in our map of characteristics.  At this point, we can lookup by UUID 
// but not by handle.  The handle is allocated to us on the ESP_GATTS_ADD_CHAR_EVT. 
m_characteristicMap.setByUUID(pCharacteristic, pCharacteristic->getUUID());
 
ESP_LOGD(LOG_TAG, "<< addCharacteristic()");
} // addCharacteristic 
 
 
/**
 * @brief Create a new BLE Characteristic associated with this service.
 * @param [in] uuid - The UUID of the characteristic.
 * @param [in] properties - The properties of the characteristic.
 * @return The new BLE characteristic.
 */
BLECharacteristic* BLEService::createCharacteristic(const char* uuid, uint32_t properties) {
return createCharacteristic(BLEUUID(uuid), properties);
}
 
/**
 * @brief Create a new BLE Characteristic associated with this service.
 * @param [in] uuid - The UUID of the characteristic.
 * @param [in] properties - The properties of the characteristic.
 * @return The new BLE characteristic.
 */
BLECharacteristic* BLEService::createCharacteristic(BLEUUID uuid, uint32_t properties) {
BLECharacteristic *pCharacteristic = new BLECharacteristic(uuid, properties);
addCharacteristic(pCharacteristic);
return pCharacteristic;
} // createCharacteristic 
 
 
/**
 * @brief Handle a GATTS server event.
 */
void BLEService::handleGATTServerEvent(
esp_gatts_cb_event_t      event,
esp_gatt_if_t             gatts_if,
esp_ble_gatts_cb_param_t *param) {
 
 
switch(event) {
// ESP_GATTS_ADD_CHAR_EVT - Indicate that a characteristic was added to the service. 
// add_char: 
// - esp_gatt_status_t status 
// - uint16_t attr_handle 
// - uint16_t service_handle 
// - esp_bt_uuid_t char_uuid 
 
// If we have reached the correct service, then locate the characteristic and remember the handle 
// for that characteristic. 
case ESP_GATTS_ADD_CHAR_EVT: {
if (m_handle == param->add_char.service_handle{
BLECharacteristic *pCharacteristic = getLastCreatedCharacteristic();
if (pCharacteristic == nullptr{
ESP_LOGE(LOG_TAG, "Expected to find characteristic with UUID: %s, but didnt!",
BLEUUID(param->add_char.char_uuid).toString().c_str());
dump();
break;
}
pCharacteristic->setHandle(param->add_char.attr_handle);
m_characteristicMap.setByHandle(param->add_char.attr_handle, pCharacteristic);
//ESP_LOGD(tag, "Characteristic map: %s", m_characteristicMap.toString().c_str()); 
break;
} // Reached the correct service. 
break;
} // ESP_GATTS_ADD_CHAR_EVT 
 
 
// ESP_GATTS_START_EVT 
// 
// start: 
// esp_gatt_status_t status 
// uint16_t service_handle 
case ESP_GATTS_START_EVT: {
if (param->start.service_handle == getHandle()) {
m_semaphoreStartEvt.give();
}
break;
} // ESP_GATTS_START_EVT 
 
// ESP_GATTS_STOP_EVT 
// 
// stop: 
// esp_gatt_status_t status 
// uint16_t service_handle 
// 
case ESP_GATTS_STOP_EVT: {
if (param->stop.service_handle == getHandle()) {
m_semaphoreStopEvt.give();
}
break;
} // ESP_GATTS_STOP_EVT 
 
 
// ESP_GATTS_CREATE_EVT 
// Called when a new service is registered as having been created. 
// 
// create: 
// * esp_gatt_status_t status 
// * uint16_t service_handle 
// * esp_gatt_srvc_id_t service_id 
// * - esp_gatt_id id 
// *   - esp_bt_uuid uuid 
// *   - uint8_t inst_id 
// * - bool is_primary 
// 
case ESP_GATTS_CREATE_EVT: {
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_id == param->create.service_id.id.inst_id{
setHandle(param->create.service_handle);
m_semaphoreCreateEvt.give();
}
break;
} // ESP_GATTS_CREATE_EVT 
 
 
// ESP_GATTS_DELETE_EVT 
// Called when a service is deleted. 
// 
// delete: 
// * esp_gatt_status_t status 
// * uint16_t service_handle 
// 
case ESP_GATTS_DELETE_EVT: {
if (param->del.service_handle == getHandle()) {
m_semaphoreDeleteEvt.give();
}
break;
} // ESP_GATTS_DELETE_EVT 
 
default{
break;
} // Default 
} // Switch 
 
// Invoke the GATTS handler in each of the associated characteristics. 
m_characteristicMap.handleGATTServerEvent(event, gatts_if, param);
} // handleGATTServerEvent 
 
 
BLECharacteristic* BLEService::getCharacteristic(const char* uuid) {
return getCharacteristic(BLEUUID(uuid));
}
 
 
BLECharacteristic* BLEService::getCharacteristic(BLEUUID uuid) {
return m_characteristicMap.getByUUID(uuid);
}
 
 
/**
 * @brief Return a string representation of this service.
 * A service is defined by:
 * * Its UUID
 * * Its handle
 * @return A string representation of this service.
 */
std::string BLEService::toString() {
std::stringstream stringStream;
stringStream << "UUID: " << getUUID().toString() <<
", handle: 0x" << std::hex << std::setfill('0') << std::setw(2) << getHandle();
return stringStream.str();
} // toString 
 
 
/**
 * @brief Get the last created characteristic.
 * It is lamentable that this function has to exist.  It returns the last created characteristic.
 * We need this because the descriptor API is built around the notion that a new descriptor, when created,
 * is associated with the last characteristics created and we need that information.
 * @return The last created characteristic.
 */
BLECharacteristic* BLEService::getLastCreatedCharacteristic() {
return m_lastCreatedCharacteristic;
} // getLastCreatedCharacteristic 
 
 
/**
 * @brief Get the BLE server associated with this service.
 * @return The BLEServer associated with this service.
 */
BLEServer* BLEService::getServer() {
return m_pServer;
} // getServer 
 
#endif // CONFIG_BT_ENABLED