Skip to content

Commit e742546

Browse files
authored
Merge pull request nkolban#700 from Donderda/master
added getServiceCount() to BLEServer.cpp
2 parents a338ca1 + 96f76c0 commit e742546

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

cpp_utils/BLEServer.cpp

100644100755
+13
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ BLEService* BLEServer::getServiceByUUID(BLEUUID uuid) {
115115
}
116116

117117

118+
/**
119+
* @brief Returns the amount of services registered to this server
120+
* @param [in] includeDefaultServices Add the amount of default BluetoothLE services defined by the BLE standard
121+
* @return The amount of registered services
122+
*/
123+
int BLEServer::getServiceCount(bool includeDefaultServices) {
124+
if(includeDefaultServices){
125+
return m_serviceMap.getRegisteredServiceCount() + 2;
126+
}
127+
return m_serviceMap.getRegisteredServiceCount();
128+
}
129+
130+
118131
/**
119132
* @brief Retrieve the advertising object that can be used to advertise the existence of the server.
120133
*

cpp_utils/BLEServer.h

100644100755
+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class BLEServiceMap {
4343
BLEService* getFirst();
4444
BLEService* getNext();
4545
void removeService(BLEService* service);
46+
int getRegisteredServiceCount();
47+
4648

4749
private:
4850
std::map<uint16_t, BLEService*> m_handleMap;
@@ -66,6 +68,7 @@ class BLEServer {
6668
BLEService* getServiceByUUID(const char* uuid);
6769
BLEService* getServiceByUUID(BLEUUID uuid);
6870
void removeService(BLEService* service);
71+
int getServiceCount(bool includeDefaultServices);
6972

7073
private:
7174
BLEServer();

cpp_utils/BLEServiceMap.cpp

100644100755
+21-9
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
* @return The characteristic.
1818
*/
1919
BLEService* BLEServiceMap::getByUUID(const char* uuid) {
20-
return getByUUID(BLEUUID(uuid));
20+
return getByUUID(BLEUUID(uuid));
2121
}
22-
22+
2323
/**
2424
* @brief Return the service by UUID.
2525
* @param [in] UUID The UUID to look up the service.
@@ -53,8 +53,8 @@ BLEService* BLEServiceMap::getByHandle(uint16_t handle) {
5353
* @return N/A.
5454
*/
5555
void BLEServiceMap::setByUUID(BLEUUID uuid,
56-
BLEService* service) {
57-
m_uuidMap.insert(std::pair<BLEService*, std::string>(service, uuid.toString()));
56+
BLEService *service) {
57+
m_uuidMap.insert(std::pair<BLEService *, std::string>(service, uuid.toString()));
5858
} // setByUUID
5959

6060

@@ -66,7 +66,7 @@ void BLEServiceMap::setByUUID(BLEUUID uuid,
6666
*/
6767
void BLEServiceMap::setByHandle(uint16_t handle,
6868
BLEService* service) {
69-
m_handleMap.insert(std::pair<uint16_t, BLEService*>(handle, service));
69+
m_handleMap.insert(std::pair<uint16_t, BLEService *>(handle, service));
7070
} // setByHandle
7171

7272

@@ -86,7 +86,7 @@ std::string BLEServiceMap::toString() {
8686
void BLEServiceMap::handleGATTServerEvent(
8787
esp_gatts_cb_event_t event,
8888
esp_gatt_if_t gatts_if,
89-
esp_ble_gatts_cb_param_t* param) {
89+
esp_ble_gatts_cb_param_t *param) {
9090
// Invoke the handler for every Service we have.
9191
for (auto &myPair : m_uuidMap) {
9292
myPair.first->handleGATTServerEvent(event, gatts_if, param);
@@ -99,7 +99,9 @@ void BLEServiceMap::handleGATTServerEvent(
9999
*/
100100
BLEService* BLEServiceMap::getFirst() {
101101
m_iterator = m_uuidMap.begin();
102-
if (m_iterator == m_uuidMap.end()) return nullptr;
102+
if (m_iterator == m_uuidMap.end()) {
103+
return nullptr;
104+
}
103105
BLEService* pRet = m_iterator->first;
104106
m_iterator++;
105107
return pRet;
@@ -110,7 +112,9 @@ BLEService* BLEServiceMap::getFirst() {
110112
* @return The next service in the map.
111113
*/
112114
BLEService* BLEServiceMap::getNext() {
113-
if (m_iterator == m_uuidMap.end()) return nullptr;
115+
if (m_iterator == m_uuidMap.end()) {
116+
return nullptr;
117+
}
114118
BLEService* pRet = m_iterator->first;
115119
m_iterator++;
116120
return pRet;
@@ -120,9 +124,17 @@ BLEService* BLEServiceMap::getNext() {
120124
* @brief Removes service from maps.
121125
* @return N/A.
122126
*/
123-
void BLEServiceMap::removeService(BLEService* service) {
127+
void BLEServiceMap::removeService(BLEService *service){
124128
m_handleMap.erase(service->getHandle());
125129
m_uuidMap.erase(service);
126130
} // removeService
127131

132+
/**
133+
* @brief Returns the amount of registered services
134+
* @return amount of registered services
135+
*/
136+
int BLEServiceMap::getRegisteredServiceCount(){
137+
return m_handleMap.size();
138+
}
139+
128140
#endif /* CONFIG_BT_ENABLED */

0 commit comments

Comments
 (0)