10000 Merge pull request #536 from chegewara/master · robertklep/esp32-snippets@809dbbe · GitHub
[go: up one dir, main page]

Skip to content

Commit 809dbbe

Browse files
authored
Merge pull request nkolban#536 from chegewara/master
multiple services with the same uuid, first try
2 parents 74aa4ae + b059dad commit 809dbbe

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

cpp_utils/BLEServer.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,23 @@ BLEService* BLEServer::createService(const char* uuid) {
6969
* of a new service. Every service must have a unique UUID.
7070
* @param [in] uuid The UUID of the new service.
7171
* @param [in] numHandles The maximum number of handles associated with this service.
72+
* @param [in] inst_id With multiple services with the same UUID we need to provide inst_id value different for each service.
7273
* @return A reference to the new service object.
7374
*/
74-
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles) {
75+
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles, uint8_t inst_id) {
7576
ESP_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
7677
m_semaphoreCreateEvt.take("createService");
7778

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;
8485
}
8586

8687
BLEService* pService = new BLEService(uuid, numHandles);
88+
pService->m_id = inst_id;
8789
m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server.
8890
pService->executeCreate(this); // Perform the API calls to actually create the service.
8991

cpp_utils/BLEServer.h

Lines changed: 5 additions & 2 deletions
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

@@ -54,7 +57,7 @@ class BLEServer {
5457
public:
5558
uint32_t getConnectedCount();
5659
BLEService* createService(const char* uuid);
57-
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15);
60+
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0);
5861
BLEAdvertising* getAdvertising();
5962
void setCallbacks(BLEServerCallbacks* pCallbacks);
6063
void startAdvertising();

cpp_utils/BLEService.cpp

Lines changed: 2 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,7 @@ 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+
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid)) && m_id == param->create.service_id.id.inst_id) {
296296
setHandle(param->create.service_handle);
297297
m_semaphoreCreateEvt.give();
298298
}

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