8000 multiple services with the same uuid, first try by chegewara · Pull Request #536 · nkolban/esp32-snippets · GitHub
[go: up one dir, main page]

Skip to content

multiple services with the same uuid, first try #536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cpp_utils/BLEServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,23 @@ BLEService* BLEServer::createService(const char* uuid) {
* of a new service. Every service must have a unique UUID.
* @param [in] uuid The UUID of the new service.
* @param [in] numHandles The maximum number of handles associated with this service.
* @param [in] inst_id With multiple services with the same UUID we need to provide inst_id value different for each service.
* @return A reference to the new service object.
*/
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles) {
BLEService* BLEServer::createService(BLEUUID uuid, uint32_t numHandles, uint8_t inst_id) {
ESP_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
m_semaphoreCreateEvt.take("createService");

// Check that a service with the supplied UUID does not already exist.
if (m_serviceMap.getByUUID(uuid) != nullptr) {
ESP_LOGE(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
ESP_LOGW(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
uuid.toString().c_str());
m_semaphoreCreateEvt.give();
return nullptr;
//m_semaphoreCreateEvt.give();
//return nullptr;
}

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

Expand Down
7 changes: 5 additions & 2 deletions cpp_utils/BLEServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ class BLEServiceMap {
void setByUUID(const char* uuid, BLEService* service);
void setByUUID(BLEUUID uuid, BLEService* service);
std::string toString();
BLEService* getFirst();
BLEService* getNext();

private:
std::map<std::string, BLEService*> m_uuidMap;
std::map<uint16_t, BLEService*> m_handleMap;
std::map<BLEService*, std::string> m_uuidMap;
std::map<BLEService*, std::string>::iterator m_iterator;
};


Expand All @@ -54,7 +57,7 @@ class BLEServer {
public:
uint32_t getConnectedCount();
BLEService* createService(const char* uuid);
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15);
BLEService* createService(BLEUUID uuid, uint32_t numHandles=15, uint8_t inst_id=0);
BLEAdvertising* getAdvertising();
void setCallbacks(BLEServerCallbacks* pCallbacks);
void startAdvertising();
Expand Down
4 changes: 2 additions & 2 deletions cpp_utils/BLEService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void BLEService::executeCreate(BLEServer *pServer) {

esp_gatt_srvc_id_t srvc_id;
srvc_id.is_primary = true;
srvc_id.id.inst_id = 0;
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(),
Expand Down Expand Up @@ -292,7 +292,7 @@ void BLEService::handleGATTServerEvent(
// * - bool is_primary
//
case ESP_GATTS_CREATE_EVT: {
if (getUUID().equals(BLEUUID(param->create.service_id.id.uuid))) {
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();
}
Expand Down
1 change: 1 addition & 0 deletions cpp_utils/BLEService.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class BLEService {
void start();
std::string toString();
uint16_t getHandle();
uint8_t m_id = 0;

private:
BLEService(const char* uuid, uint32_t numHandles);
Expand Down
36 changes: 32 additions & 4 deletions cpp_utils/BLEServiceMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ BLEService* BLEServiceMap::getByUUID(const char* uuid) {
*/
BLEService* BLEServiceMap::getByUUID(BLEUUID uuid) {
for (auto &myPair : m_uuidMap) {
if (myPair.second->getUUID().equals(uuid)) {
return myPair.second;
if (myPair.first->getUUID().equals(uuid)) {
return myPair.first;
}
}
//return m_uuidMap.at(uuid.toString());
Expand All @@ -54,7 +54,7 @@ BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
*/
void BLEServiceMap::setByUUID(BLEUUID uuid,
BLEService *service) {
m_uuidMap.insert(std::pair<std::string, BLEService *>(uuid.toString(), service));
m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString()));
} // setByUUID


Expand Down Expand Up @@ -89,7 +89,35 @@ void BLEServiceMap::handleGATTServerEvent(
esp_ble_gatts_cb_param_t *param) {
// Invoke the handler for every Service we have.
for (auto &myPair : m_uuidMap) {
myPair.second->handleGATTServerEvent(event, gatts_if, param);
myPair.first->handleGATTServerEvent(event, gatts_if, param);
}
}

/**
* @brief Get the first service in the map.
* @return The first service in the map.
*/
BLEService* BLEServiceMap::getFirst() {
m_iterator = m_uuidMap.begin();
if (m_iterator == m_uuidMap.end()) {
return nullptr;
}
BLEService* pRet = m_iterator->first;
m_iterator++;
return pRet;
} // getFirst

/**
* @brief Get the next service in the map.
* @return The next service in the map.
*/
BLEService* BLEServiceMap::getNext() {
if (m_iterator == m_uuidMap.end()) {
return nullptr;
}
BLEService* pRet = m_iterator->first;
m_iterator++;
return pRet;
} // getNext

#endif /* CONFIG_BT_ENABLED */
0