8000 multiple services with the same uuid · breadchris/esp32-snippets@0d67442 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d67442

Browse files
committed
multiple services with the same uuid
1 parent 74aa4ae commit 0d67442

File tree

5 files changed

+45
-11
lines changed

5 files changed

+45
-11
lines changed

cpp_utils/BLEServer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,16 @@ BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles) {
7575
ESP_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
7676
m_semaphoreCreateEvt.take("createService");
7777

78+
BLEService* pService = new BLEService(uuid, numHandles);
7879
// Check that a service with the supplied UUID does not already exist.
7980
if (m_serviceMap.getByUUID(uuid) != nullptr) {
80-
ESP_LOGE(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
81+
ESP_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
8182
uuid.toString().c_str());
82-
m_semaphoreCreateEvt.give();
83-
return nullptr;
83+
//m_semaphoreCreateEvt.give();
84+
//return nullptr;
85+
pService->m_id = 1;
8486
}
8587

86-
BLEService* pService = new BLEService(uuid, numHandles);
8788
m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server.
8889
pService->executeCreate(this); // Perform the API calls to actually create the service.
8990

cpp_utils/BLEServer.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ class BLEServiceMap {
4040
void setByUUID(const char* uuid, BLEService* service);
4141
void setByUUID(BLEUUID uuid, BLEService* service);
4242
std::string toString();
43+
BLEService* getFirst();
44+
BLEService* getNext();
4345

4446
private:
45-
std::map<std::string, BLEService*> m_uuidMap;
4647
std::map<uint16_t, BLEService*> m_handleMap;
48+
std::map<BLEService*, std::string> m_uuidMap;
49+
std::map<BLEService*, std::string>::iterator m_iterator;
4750
};
4851

4952

cpp_utils/BLEService.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void BLEService::executeCreate(BLEServer *pServer) {
7373

7474
esp_gatt_srvc_id_t srvc_id;
7575
srvc_id.is_primary = true;
76-
srvc_id.id.inst_id = 0;
76+
srvc_id.id.inst_id = m_id;
7777
srvc_id.id.uuid = *m_uuid.getNative();
7878
esp_err_t errRc = ::esp_ble_gatts_create_service(
7979
getServer()->getGattsIf(),
@@ -292,7 +292,8 @@ void BLEService::handleGATTServerEvent(
292292
// * - bool is_primary
293293
//
294294
case ESP_GATTS_CREATE_EVT: {
295-
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid))) {
295+
ESP_LOGE(LOG_TAG, "%d", param->create.service_handle);
296+
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_id == param->create.service_id.id.inst_id) {
296297
setHandle(param->create.service_handle);
297298
m_semaphoreCreateEvt.give();
298299
}

cpp_utils/BLEService.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class BLEService {
6464
void start();
6565
std::string toString();
6666
uint16_t getHandle();
67+
uint8_t m_id = 0;
6768

6869
private:
6970
BLEService(const char* uuid, uint32_t numHandles);

cpp_utils/BLEServiceMap.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ BLEService* BLEServiceMap::getByUUID(const char* uuid) {
2727
*/
2828
BLEService* BLEServiceMap::getByUUID(BLEUUID uuid) {
2929
for (auto &myPair : m_uuidMap) {
30-
if (myPair.second->getUUID().equals(uuid)) {
31-
return myPair.second;
30+
if (myPair.first->getUUID().equals(uuid)) {
31+
return myPair.first;
3232
}
3333
}
3434
//return m_uuidMap.at(uuid.toString());
@@ -54,7 +54,7 @@ BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
5454
*/
5555
void BLEServiceMap::setByUUID(BLEUUID uuid,
5656
BLEService *service) {
57-
m_uuidMap.insert(std::pair<std::string, BLEService *>(uuid.toString(), service));
57+
m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString()));
5858
} // setByUUID
5959

6060

@@ -89,7 +89,35 @@ void BLEServiceMap::handleGATTServerEvent(
8989
esp_ble_gatts_cb_param_t *param) {
9090
// Invoke the handler for every Service we have.
9191
for (auto &myPair : m_uuidMap) {
92-
myPair.second->handleGATTServerEvent(event, gatts_if, param);
92+
myPair.first->handleGATTServerEvent(event, gatts_if, param);
9393
}
9494
}
95+
96+
/**
97+
* @brief Get the first service in the map.
98+
* @return The first service in the map.
99+
*/
100+
BLEService* BLEServiceMap::getFirst() {
101+
m_iterator = m_uuidMap.begin();
102+
if (m_iterator == m_uuidMap.end()) {
103+
return nullptr;
104+
}
105+
BLEService* pRet = m_iterator->first;
106+
m_iterator++;
107+
return pRet;
108+
} // getFirst
109+
110+
/**
111+
* @brief Get the next service in the map.
112+
* @return The next service in the map.
113+
*/
114+
BLEService* BLEServiceMap::getNext() {
115+
if (m_iterator == m_uuidMap.end()) {
116+
return nullptr;
117+
}
118+
BLEService* pRet = m_iterator->first;
119+
m_iterator++;
120+
return pRet;
121+
} // getNext
122+
95123
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)
0