8000 Merge pull request #575 from jim-ber/master · gourryinverse/esp32-snippets@d875fec · GitHub
[go: up one dir, main page]

Skip to content

Commit d875fec

Browse files
authored
Merge pull request nkolban#575 from jim-ber/master
BLE Remove Service Support
2 parents c054b18 + 07c4b82 commit d875fec

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

cpp_utils/BLEServer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,14 @@ void BLEServer::setCallbacks(BLEServerCallbacks* pCallbacks) {
323323
m_pServerCallbacks = pCallbacks;
324324
} // setCallbacks
325325

326+
/*
327+
* Remove service
328+
*/
329+
void BLEServer::removeService(BLEService *service) {
330+
service->stop();
331+
service->executeDelete();
332+
m_serviceMap.removeService(service);
333+
}
326334

327335
/**
328336
* @brief Start advertising.

cpp_utils/BLEServer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class BLEServiceMap {
4242
std::string toString();
4343
BLEService* getFirst();
4444
BLEService* getNext();
45+
void removeService(BLEService *service);
4546

4647
private:
4748
std::map<uint16_t, BLEService*> m_handleMap;
@@ -61,6 +62,7 @@ class BLEServer {
6162
BLEAdvertising* getAdvertising();
6263
void setCallbacks(BLEServerCallbacks* pCallbacks);
6364
void startAdvertising();
65+
void removeService(BLEService *service);
6466

6567

6668
private:

cpp_utils/BLEService.cpp

Lines changed: 78 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-c43fe74fafe29442d32815ccb10a968b2f1f18919c273dd5b7ea7c125486bf40-155-185-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">185
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ void BLEService::executeCreate(BLEServer *pServer) {
9191
} // executeCreate
9292

9393

94+
/**
95+
* @brief Delete the service.
96+
* Delete the service.
97+
* @return N/A.
98+
*/
99+
100+
void BLEService::executeDelete() {
101+
ESP_LOGD(LOG_TAG, ">> executeDelete()");
102+
m_semaphoreDeleteEvt.take("executeDelete"); // Take the mutex and release at event ESP_GATTS_DELETE_EVT
103+
104+
esp_err_t errRc = ::esp_ble_gatts_delete_service( getHandle() );
105+
106+
if (errRc != ESP_OK) {
107+
ESP_LOGE(LOG_TAG, "esp_ble_gatts_delete_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
108+
return;
109+
}
110+
111+
m_semaphoreDeleteEvt.wait("executeDelete");
112+
ESP_LOGD(LOG_TAG, "<< executeDelete");
113+
} // executeDelete
114+
115+
94116
/**
95117
* @brief Dump details of this BLE GATT service.
96118
* @return N/A.
@@ -153,6 +175,34 @@ void BLEService::start() {
153175
} // start
154176

155177

178+
/**
179+
* @brief Stop the service.
180+
* @return Stop the service.
181+
*/
182+
void BLEService::stop() {
183+
// We ask the BLE runtime to start the service and then create each of the characteristics.
184+
// 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().
186+
//
187+
ESP_LOGD(LOG_TAG, ">> stop(): Stopping service (esp_ble_gatts_stop_service): %s", toString().c_str());
188+
if (m_handle == NULL_HANDLE) {
189+
ESP_LOGE(LOG_TAG, "<< !!! We attempted to stop a service but don't know its handle!");
190+
return;
191+
}
192+
193+
m_semaphoreStopEvt.take("stop");
194+
esp_err_t errRc = ::esp_ble_gatts_stop_service(m_handle);
195+
196+
if (errRc != ESP_OK) {
197+
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_stop_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
198+
return;
199+
}
200+
m_semaphoreStopEvt.wait("stop");
201+
202+
ESP_LOGD(LOG_TAG, "<< stop()");
203+
} // start
204+
205+
156206
/**
157207
* @brief Set the handle associated with this service.
158208
* @param [in] handle The handle associated with the service.
@@ -278,6 +328,19 @@ void BLEService::handleGATTServerEvent(
278328
break;
279329
} // ESP_GATTS_START_EVT
280330

331+
// ESP_GATTS_STOP_EVT
332+
//
333+
// stop:
334+
// esp_gatt_status_t status
335+
// uint16_t service_handle
336+
//
337+
case ESP_GATTS_STOP_EVT: {
338+
if (param->stop.service_handle == getHandle()) {
339+
m_semaphoreStopEvt.give();
340+
}
341+
break;
342+
} // ESP_GATTS_STOP_EVT
343+
281344

282345
// ESP_GATTS_CREATE_EVT
283346
// Called when a new service is registered as having been created.
@@ -299,6 +362,21 @@ void BLEService::handleGATTServerEvent(
299362
break;
300363
} // ESP_GATTS_CREATE_EVT
301364

365+
366+
// ESP_GATTS_DELETE_EVT
367+
// Called when a service is deleted.
368+
//
369+
// delete:
370+
// * esp_gatt_status_t status
371+
// * uint16_t service_handle
372+
//
373+
case ESP_GATTS_DELETE_EVT: {
374+
if (param->del.service_handle == getHandle()) {
375+
m_semaphoreDeleteEvt.give();
376+
}
377+
break;
378+
} // ESP_GATTS_DELETE_EVT
379+
302380
default: {
303381
break;
304382
} // Default

cpp_utils/BLEService.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ class BLEService {
5757
BLECharacteristic* createCharacteristic(BLEUUID uuid, uint32_t properties);
5858
void dump();
5959
void executeCreate(BLEServer* pServer);
60+
void executeDelete();
6061
BLECharacteristic* getCharacteristic(const char* uuid);
6162
BLECharacteristic* getCharacteristic(BLEUUID uuid);
6263
BLEUUID getUUID();
6364
BLEServer* getServer();
6465
void start();
66+
void stop();
6567
std::string toString();
6668
uint16_t getHandle();
6769
uint8_t m_id = 0;
@@ -82,7 +84,9 @@ class BLEService {
8284
BLEUUID m_uuid;
8385

8486
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
87+
FreeRTOS::Semaphore m_semaphoreDeleteEvt = FreeRTOS::Semaphore("DeleteEvt");
8588
FreeRTOS::Semaphore m_semaphoreStartEvt = FreeRTOS::Semaphore("StartEvt");
89+
FreeRTOS::Semaphore m_semaphoreStopEvt = FreeRTOS::Semaphore("StopEvt");
8690

8791
uint32_t m_numHandles;
8892

cpp_utils/BLEServiceMap.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,13 @@ BLEService* BLEServiceMap::getNext() {
120120
return pRet;
121121
} // getNext
122122

123+
/**
124+
* @brief Removes service from maps.
125+
* @return N/A.
126+
*/
127+
void BLEServiceMap::removeService(BLEService *service){
128+
m_handleMap.erase(service->getHandle());
129+
m_uuidMap.erase(service);
130+
} // removeService
131+
123132
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)
0