Skip to content

Commit 77b0670

Browse files
authored
Provide signatures to allow BLEUUID as string
Code changes for #31
1 parent 4401dc0 commit 77b0670

19 files changed

+165
-6
lines changed

cpp_utils/BLEAdvertisedDevice.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,14 @@ void BLEAdvertisedDevice::setScan(BLEScan* pScan) {
352352
m_pScan = pScan;
353353
} // setScan
354354

355+
/**
356+
* @brief Set the Service UUID for this device.
357+
* @param [in] serviceUUID The discovered serviceUUID
358+
*/
359+
void BLEAdvertisedDevice::setServiceUUID(const char* serviceUUID) {
360+
return setServiceUUID(BLEUUID(serviceUUID));
361+
} // setRSSI
362+
355363
/**
356364
* @brief Set the Service UUID for this device.
357365
* @param [in] serviceUUID The discovered serviceUUID

cpp_utils/BLEAdvertisedDevice.h

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class BLEAdvertisedDevice {
5959
void setName(std::string name);
6060
void setRSSI(int rssi);
6161
void setScan(BLEScan* pScan);
62+
void setServiceUUID(const char* serviceUUID);
6263
void setServiceUUID(BLEUUID serviceUUID);
6364
void setTXPower(int8_t txPower);
6465

cpp_utils/BLEAdvertising.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
5656
} // setAppearance
5757

5858

59+
/**
60+
* @brief Set the service UUID.
61+
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
62+
* ESP-IDF advertising functions. In this method, we see two fields within that structure
63+
* namely service_uuid_len and p_service_uuid to be the information supplied in the passed
64+
* in service uuid.
65+
* @param [in] uuid The UUID of the service.
66+
* @return N/A.
67+
*/
68+
void BLEAdvertising::setServiceUUID(const char* serviceUUID) {
69+
return setServiceUUID(BLEUUID(serviceUUID));
70+
}
5971
/**
6072
* @brief Set the service UUID.
6173
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the

cpp_utils/BLEAdvertising.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BLEAdvertising {
2323
void start();
2424
void stop();
2525
void setAppearance(uint16_t appearance);
26+
void setServiceUUID(const char* serviceUUID);
2627
void setServiceUUID(BLEUUID serviceUUID);
2728
private:
2829
esp_ble_adv_data_t m_advData;

cpp_utils/BLECharacteristic.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ static const char* LOG_TAG = "BLECharacteristic";
2323

2424
#define NULL_HANDLE (0xffff)
2525

26+
27+
/**
28+
* @brief Construct a characteristic
29+
* @param [in] uuid - UUID (const char*) for the characteristic.
30+
* @param [in] properties - Properties for the characteristic.
31+
*/
32+
BLECharacteristic::BLECharacteristic(const char* uuid, uint32_t properties) {
33+
BLECharacteristic(BLEUUID(uuid), properties);
34+
}
35+
2636
/**
2737
* @brief Construct a characteristic
2838
* @param [in] uuid - UUID for the characteristic.
@@ -122,6 +132,16 @@ void BLECharacteristic::executeCreate(BLEService* pService) {
122132
} // executeCreate
123133

124134

135+
136+
/**
137+
* @brief Return the BLE Descriptor for the given UUID if associated with this characteristic.
138+
* @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.
139+
* @return The BLE Descriptor. If no such descriptor is associated with the characteristic, nullptr is returned.
140+
*/
141+
BLEDescriptor* BLECharacteristic::getDescriptorByUUID(const char* descriptorUUID) {
142+
return m_descriptorMap.getByUUID(BLEUUID(descriptorUUID));
143+
} // getDescriptorByUUID
144+
125145
/**
126146
* @brief Return the BLE Descriptor for the given UUID if associated with this characteristic.
127147
* @param [in] descriptorUUID The UUID of the descriptor that we wish to retrieve.

cpp_utils/BLECharacteristic.h

+4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ class BLECharacteristicCallbacks;
2626
*/
2727
class BLEDescriptorMap {
2828
public:
29+
void setByUUID(const char* uuid, BLEDescriptor *pDescriptor);
2930
void setByUUID(BLEUUID uuid, BLEDescriptor *pDescriptor);
3031
void setByHandle(uint16_t handle, BLEDescriptor *pDescriptor);
32+
BLEDescriptor *getByUUID(const char* uuid);
3133
BLEDescriptor *getByUUID(BLEUUID uuid);
3234
BLEDescriptor *getByHandle(uint16_t handle);
3335
std::string toString();
@@ -52,10 +54,12 @@ class BLEDescriptorMap {
5254
*/
5355
class BLECharacteristic {
5456
public:
57+
BLECharacteristic(const char* uuid, uint32_t properties = 0);
5558
BLECharacteristic(BLEUUID uuid, uint32_t properties = 0);
5659
virtual ~BLECharacteristic();
5760

5861
void addDescriptor(BLEDescriptor* pDescriptor);
62+
BLEDescriptor* getDescriptorByUUID(const char* descriptorUUID);
5963
BLEDescriptor* getDescriptorByUUID(BLEUUID descriptorUUID);
6064
//size_t getLength();
6165
BLEUUID getUUID();

cpp_utils/BLECharacteristicMap.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
#include <iomanip>
1111
#include "BLEService.h"
1212

13+
/**
14+
* @brief Return the characteristic by UUID.
15+
* @param [in] UUID The UUID to look up the characteristic.
16+
* @return The characteristic.
17+
*/
18+
BLECharacteristic* BLECharacteristicMap::getByUUID(const char* uuid) {
19+
return getByUUID(BLEUUID(uuid));
20+
}
1321

1422
/**
1523
* @brief Return the characteristic by UUID.

cpp_utils/BLEClient.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,16 @@ esp_gatt_if_t BLEClient::getGattcIf() {
202202
} // getGattcIf
203203

204204

205+
206+
/**
207+
* @brief Get the service object corresponding to the uuid.
208+
* @param [in] uuid The UUID of the service being sought.
209+
* @return A reference to the Service or nullptr if don't know about it.
210+
*/
211+
BLERemoteService* BLEClient::getService(const char* uuid) {
212+
return getService(BLEUUID(uuid));
213+
}
214+
205215
/**
206216
* @brief Get the service object corresponding to the uuid.
207217
* @param [in] uuid The UUID of the service being sought.

cpp_utils/BLEClient.h

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class BLEClient {
3232
void disconnect();
3333
BLEAddress getPeerAddress();
3434
std::map<std::string, BLERemoteService*>* getServices();
35+
BLERemoteService* getService(const char* uuid);
3536
BLERemoteService* getService(BLEUUID uuid);
3637
void setClientCallbacks(BLEClientCallbacks *pClientCallbacks);
3738
std::string toString();

cpp_utils/BLEDescriptor.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ static const char* LOG_TAG = "BLEDescriptor";
2121

2222

2323
#define NULL_HANDLE (0xffff)
24+
25+
26+
/**
27+
* @brief BLEDescriptor constructor.
28+
*/
29+
BLEDescriptor::BLEDescriptor(const char* uuid) {
30+
BLEDescriptor(BLEUUID(uuid));
31+
}
2432
/**
2533
* @brief BLEDescriptor constructor.
2634
*/

cpp_utils/BLEDescriptor.h

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class BLECharacteristic;
2323
*/
2424
class BLEDescriptor {
2525
public:
26+
BLEDescriptor(const char* uuid);
2627
BLEDescriptor(BLEUUID uuid);
2728
virtual ~BLEDescriptor();
2829

cpp_utils/BLEDescriptorMap.cpp

+23-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
#include "BLEDescriptor.h"
1313
#include <esp_gatts_api.h> // ESP32 BLE
1414

15+
/**
16+
* @brief Return the descriptor by UUID.
17+
* @param [in] UUID The UUID to look up the descriptor.
18+
* @return The descriptor. If not present, then nullptr is returned.
19+
*/
20+
BLEDescriptor* BLEDescriptorMap::getByUUID(const char* uuid) {
21+
return getByUUID(BLEUUID(uuid));
22+
}
23+
24+
1525
/**
1626
* @brief Return the descriptor by UUID.
1727
* @param [in] UUID The UUID to look up the descriptor.
@@ -44,9 +54,19 @@ BLEDescriptor* BLEDescriptorMap::getByHandle(uint16_t handle) {
4454
* @param [in] characteristic The descriptor to cache.
4555
* @return N/A.
4656
*/
47-
void BLEDescriptorMap::setByUUID(
48-
BLEUUID uuid,
49-
BLEDescriptor *pDescriptor) {
57+
void BLEDescriptorMap::setByUUID(const char* uuid, BLEDescriptor *pDescriptor){
58+
m_uuidMap.insert(std::pair<std::string, BLEDescriptor *>(uuid, pDescriptor));
59+
} // setByUUID
60+
61+
62+
63+
/**
64+
* @brief Set the descriptor by UUID.
65+
* @param [in] uuid The uuid of the descriptor.
66+
* @param [in] characteristic The descriptor to cache.
67+
* @return N/A.
68+
*/
69+
void BLEDescriptorMap::setByUUID(BLEUUID uuid, BLEDescriptor *pDescriptor) {
5070
m_uuidMap.insert(std::pair<std::string, BLEDescriptor *>(uuid.toString(), pDescriptor));
5171
} // setByUUID
5272

cpp_utils/BLERemoteService.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ void BLERemoteService::gattClientEventHandler(
101101
} // gattClientEventHandler
102102

103103

104+
/**
105+
* @brief Get the characteristic object for the UUID.
106+
* @param [in] uuid Characteristic uuid.
107+
* @return Reference to the characteristic object.
108+
*/
109+
BLERemoteCharacteristic* BLERemoteService::getCharacteristic(const char* uuid) {
110+
return getCharacteristic(BLEUUID(uuid));
111+
}
112+
113+
104114
/**
105115
* @brief Get the characteristic object for the UUID.
106116
* @param [in] uuid Characteristic uuid.

cpp_utils/BLERemoteService.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class BLERemoteService {
3030
virtual ~BLERemoteService();
3131

3232
// Public methods
33+
BLERemoteCharacteristic* getCharacteristic(const char* uuid);
3334
BLERemoteCharacteristic* getCharacteristic(BLEUUID uuid);
3435
void getCharacteristics(void);
3536
BLEClient* getClient(void);

cpp_utils/BLEServer.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,20 @@ void BLEServer::createApp(uint16_t appId) {
4747
registerApp();
4848
}
4949

50+
51+
/**
52+
* @brief Create a %BLE Service.
53+
*
54+
* With a %BLE server, we can host one or more services. Invoking this function causes the creation of a definition
55+
* of a new service. Every service must have a unique UUID.
56+
* @param [in] uuid The UUID of the new service.
57+
* @return A reference to the new service object.
58+
*/
59+
BLEService* BLEServer::createService(const char* uuid) {
60+
return createService(BLEUUID(uuid));
61+
}
62+
63+
5064
/**
5165
* @brief Create a %BLE Service.
5266
*

cpp_utils/BLEServer.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ class BLEServerCallbacks;
2929
class BLEServiceMap {
3030
public:
3131
BLEService* getByHandle(uint16_t handle);
32+
BLEService* getByUUID(const char* uuid);
3233
BLEService* getByUUID(BLEUUID uuid);
3334
void handleGATTServerEvent(
3435
esp_gatts_cb_event_t event,
3536
esp_gatt_if_t gatts_if,
3637
esp_ble_gatts_cb_param_t* param);
3738
void setByHandle(uint16_t handle, BLEService* service);
39+
void setByUUID(const char* uuid, BLEService* service);
3840
void setByUUID(BLEUUID uuid, BLEService* service);
3941
std::string toString();
4042

@@ -53,6 +55,7 @@ class BLEServer {
5355

5456

5557
uint32_t getConnectedCount();
58+
BLEService* createService(const char* uuid);
5659
BLEService* createService(BLEUUID uuid);
5760
BLEAdvertising* getAdvertising();
5861
void setCallbacks(BLEServerCallbacks *pCallbacks);

cpp_utils/BLEService.cpp

+25-3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626

2727
static const char* LOG_TAG = "BLEService"; // Tag for logging.
2828

29+
/**
30+
* @brief Construct an instance of the BLEService
31+
* @param [in] uuid The UUID of the service.
32+
*/
33+
BLEService::BLEService(const char* uuid) {
34+
BLEService(BLEUUID(uuid));
35+
}
36+
37+
2938
/**
3039
* @brief Construct an instance of the BLEService
3140
* @param [in] uuid The UUID of the service.
@@ -200,9 +209,17 @@ void BLEService::addCharacteristic(BLECharacteristic* pCharacteristic) {
200209
* @param [in] properties - The properties of the characteristic.
201210
* @return The new BLE characteristic.
202211
*/
203-
BLECharacteristic* BLEService::createCharacteristic(
204-
BLEUUID uuid,
205-
uint32_t properties) {
212+
BLECharacteristic* BLEService::createCharacteristic(const char* uuid, uint32_t properties) {
213+
return createCharacteristic(BLEUUID(uuid), properties);
214+
}
215+
216+
/**
217+
* @brief Create a new BLE Characteristic associated with this service.
218+
* @param [in] uuid - The UUID of the characteristic.
219+
* @param [in] properties - The properties of the characteristic.
220+
* @return The new BLE characteristic.
221+
*/
222+
BLECharacteristic* BLEService::createCharacteristic(BLEUUID uuid, uint32_t properties) {
206223
BLECharacteristic *pCharacteristic = new BLECharacteristic(uuid, properties);
207224
addCharacteristic(pCharacteristic);
208225
return pCharacteristic;
@@ -289,6 +306,11 @@ void BLEService::handleGATTServerEvent(
289306
} // handleGATTServerEvent
290307

291308

309+
BLECharacteristic* BLEService::getCharacteristic(const char* uuid) {
310+
return getCharacteristic(BLEUUID(uuid));
311+
}
312+
313+
292314
BLECharacteristic* BLEService::getCharacteristic(BLEUUID uuid) {
293315
return m_characteristicMap.getByUUID(uuid);
294316
}

cpp_utils/BLEService.h

+5
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ class BLEServer;
2424
*/
2525
class BLECharacteristicMap {
2626
public:
27+
void setByUUID(const char* uuid, BLECharacteristic* pCharacteristic);
2728
void setByUUID(BLEUUID uuid, BLECharacteristic* pCharacteristic);
2829
void setByHandle(uint16_t handle, BLECharacteristic* pCharacteristic);
30+
BLECharacteristic* getByUUID(const char* uuid);
2931
BLECharacteristic* getByUUID(BLEUUID uuid);
3032
BLECharacteristic* getByHandle(uint16_t handle);
3133
BLECharacteristic* getFirst();
@@ -50,12 +52,15 @@ class BLECharacteristicMap {
5052
*/
5153
class BLEService {
5254
public:
55+
BLEService(const char* uuid);
5356
BLEService(BLEUUID uuid);
5457

5558
void addCharacteristic(BLECharacteristic* pCharacteristic);
59+
BLECharacteristic* createCharacteristic(const char* uuid, uint32_t properties);
5660
BLECharacteristic* createCharacteristic(BLEUUID uuid, uint32_t properties);
5761
void dump();
5862
void executeCreate(BLEServer* pServer);
63+
BLECharacteristic* getCharacteristic(const char* uuid);
5964
BLECharacteristic* getCharacteristic(BLEUUID uuid);
6065
BLEUUID getUUID();
6166
BLEServer* getServer();

cpp_utils/BLEServiceMap.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@
1010
#include <iomanip>
1111
#include "BLEService.h"
1212

13+
14+
/**
15+
* @brief Return the service by UUID.
16+
* @param [in] UUID The UUID to look up the service.
17+
* @return The characteristic.
18+
*/
19+
BLEService* BLEServiceMap::getByUUID(const char* uuid) {
20+
return getByUUID(BLEUUID(uuid));
21+
}
22+
1323
/**
1424
* @brief Return the service by UUID.
1525
* @param [in] UUID The UUID to look up the service.

0 commit comments

Comments
 (0)