8000 Code changes for #83 · headcloudmonkey/esp32-snippets@c544fbf · GitHub
[go: up one dir, main page]

Skip to content

Commit c544fbf

Browse files
committed
Code changes for nkolban#83
1 parent 01718f8 commit c544fbf

8 files changed

+343
-89
lines changed

cpp_utils/BLEClient.cpp

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -110,45 +110,6 @@ void BLEClient::gattClientEventHandler(
110110

111111
// Execute handler code based on the type of event received.
112112
switch(event) {
113-
//
114-
// ESP_GATTC_NOTIFY_EVT
115-
//
116-
// notify
117-
// - uint16_t conn_id
118-
// - esp_bd_addr_t remote_bda
119-
// - uint16_t handle
120-
// - uint16_t value_len
121-
// - uint8_t* value
122-
// - bool is_notify
123-
//
124-
// We have received a notification event which means that the server wishes us to know about a notification
125-
// piece of data. What we must now do is find the characteristic with the associated handle and then
126-
// invoke its notification callback (if it has one).
127-
//
128-
case ESP_GATTC_NOTIFY_EVT: {
129-
BLERemoteService* pBLERemoteService = getService(evtParam->notify.handle);
130-
if (pBLERemoteService == nullptr) {
131-
ESP_LOGE(LOG_TAG, "Could not find service containing handle %d 0x%.2x for notification",
132-
evtParam->notify.handle, evtParam->notify.handle);
133-
break;
134-
}
135-
BLERemoteCharacteristic* pBLERemoteCharacteristic = pBLERemoteService->getCharacteristic(evtParam->notify.handle);
136-
if (pBLERemoteCharacteristic == nullptr) {
137-
ESP_LOGE(LOG_TAG, "Could not find characteristic with handle %d 0x%.2x for notification",
138-
evtParam->notify.handle, evtParam->notify.handle);
139-
break;
140-
}
141-
if (pBLERemoteCharacteristic->m_notifyCallback != nullptr) {
142-
pBLERemoteCharacteristic->m_notifyCallback(
143-
pBLERemoteCharacteristic,
144-
evtParam->notify.value,
145-
evtParam->notify.value_len,
146-
evtParam->notify.is_notify
147-
);
148-
} // End we have a callback function ...
149-
break;
150-
} // ESP_GATTC_NOTIFY_EVT
151-
152113
//
153114
// ESP_GATTC_OPEN_EVT
154115
//
@@ -222,6 +183,7 @@ void BLEClient::gattClientEventHandler(
222183
}
223184
} // Switch
224185

186+
// Pass the request on to all services.
225187
for (auto &myPair : m_servicesMap) {
226188
myPair.second->gattClientEventHandler(event, gattc_if, evtParam);
227189
}
@@ -232,7 +194,7 @@ void BLEClient::gattClientEventHandler(
232194
/**
233195
* @brief Retrieve the address of the peer.
234196
*
235-
* Returns the address of the %BLE peer to which this client is connected.
197+
* Returns the Bluetooth device address of the %BLE peer to which this client is connected.
236198
*/
237199
BLEAddress BLEClient::getPeerAddress() {
238200
return m_peerAddress;
@@ -248,21 +210,15 @@ esp_gatt_if_t BLEClient::getGattcIf() {
248210
return m_gattc_if;
249211
} // getGattcIf
250212

251-
BLERemoteService* BLEClient::getService(uint16_t handle) {
252-
ESP_LOGD(LOG_TAG, ">> getService: handle: %d", handle);
253-
ESP_LOGE(LOG_TAG, "!!! NOT IMPLEMENTED !!!");
254-
ESP_LOGD(LOG_TAG, "<< getService");
255-
return nullptr;
256-
}
257213

258214
/**
259-
* @brief Get the service object corresponding to the uuid.
215+
* @brief Get the service BLE Remote Service instance corresponding to the uuid.
260216
* @param [in] uuid The UUID of the service being sought.
261217
* @return A reference to the Service or nullptr if don't know about it.
262218
*/
263219
BLERemoteService* BLEClient::getService(const char* uuid) {
264220
return getService(BLEUUID(uuid));
265-
}
221+
} // getService
266222

267223

268224
/**

cpp_utils/BLEClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ class BLEClient {
3434
std::map<std::string, BLERemoteService*>* getServices();
3535
BLERemoteService* getService(const char* uuid);
3636
BLERemoteService* getService(BLEUUID uuid);
37-
BLERemoteService* getService(uint16_t handle);
3837
void setClientCallbacks(BLEClientCallbacks *pClientCallbacks);
3938
std::string toString();
4039

4140
private:
4241
friend class BLEDevice;
43-
friend class BLERemoteCharacteristic;
4442
friend class BLERemoteService;
43+
friend class BLERemoteCharacteristic;
44+
friend class BLERemoteDescriptor;
4545

4646
void gattClientEventHandler(
4747
esp_gattc_cb_event_t event,

cpp_utils/BLERemoteCharacteristic.cpp

Lines changed: 183 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,43 @@
1717
#include <sstream>
1818
#include "BLEUtils.h"
1919
#include "GeneralUtils.h"
20+
#include "BLERemoteDescriptor.h"
2021

2122

22-
static const char* LOG_TAG = "BLERemoteCharacteristic";
23+
static const char* LOG_TAG = "BLERemoteCharacteristic"; // The logging tag for this class.
2324

25+
/**
26+
* @brief Constructor.
27+
* @param [in] handle The BLE server side handle of this characteristic.
28+
* @param [in] uuid The UUID of this characteristic.
29+
* @param [in] charProp The properties of this characteristic.
30+
* @param [in] pRemoteService A reference to the remote service to which this remote characteristic pertains.
31+
*/
2432
BLERemoteCharacteristic::BLERemoteCharacteristic(
2533
uint16_t handle,
2634
BLEUUID uuid,
2735
esp_gatt_char_prop_t charProp,
2836
BLERemoteService* pRemoteService) {
37+
ESP_LOGD(LOG_TAG, ">> BLERemoteCharacteristic: handle: %d 0x%d, uuid: %s", handle, handle, uuid.toString().c_str());
2938
m_handle = handle;
3039
m_uuid = uuid;
3140
m_charProp = charProp;
3241
m_pRemoteService = pRemoteService;
3342
m_notifyCallback = nullptr;
43+
44+
getDescriptors(); // Get the descriptors for this characteristic
45+
ESP_LOGD(LOG_TAG, "<< BLERemoteCharacteristic");
3446
} // BLERemoteCharacteristic
3547

48+
49+
/**
50+
*@brief Destructor.
51+
*/
52+
BLERemoteCharacteristic::~BLERemoteCharacteristic() {
53+
removeDescriptors(); // Release resources for any descriptor information we may have allocated.
54+
} // ~BLERemoteCharacteristic
55+
56+
3657
/*
3758
static bool compareSrvcId(esp_gatt_srvc_id_t id1, esp_gatt_srvc_id_t id2) {
3859
if (id1.id.inst_id != id2.id.inst_id) {
@@ -72,6 +93,36 @@ void BLERemoteCharacteristic::gattClientEventHandler(
7293
esp_gatt_if_t gattc_if,
7394
esp_ble_gattc_cb_param_t* evtParam) {
7495
switch(event) {
96+
//
97+
// ESP_GATTC_NOTIFY_EVT
98+
//
99+
// notify
100+
// - uint16_t conn_id
101+
// - esp_bd_addr_t remote_bda
102+
// - uint16_t handle
103+
// - uint16_t value_len
104+
// - uint8_t* value
105+
// - bool is_notify
106+
//
107+
// We have received a notification event which means that the server wishes us to know about a notification
108+
// piece of data. What we must now do is find the characteristic with the associated handle and then
109+
// invoke its notification callback (if it has one).
110+
//
111+
case ESP_GATTC_NOTIFY_EVT: {
112+
if (evtParam->notify.handle != getHandle()) {
113+
break;
114+
}
115+
if (m_notifyCallback != nullptr) {
116+
m_notifyCallback(
117+
this,
118+
evtParam->notify.value,
119+
evtParam->notify.value_len,
120+
evtParam->notify.is_notify
121+
);
122+
} // End we have a callback function ...
123+
break;
124+
} // ESP_GATTC_NOTIFY_EVT
125+
75126
//
76127
// ESP_GATTC_READ_CHAR_EVT
77128
// This event indicates that the server has responded to the read request.
@@ -148,16 +199,127 @@ void BLERemoteCharacteristic::gattClientEventHandler(
148199
} // End switch
149200
}; // gattClientEventHandler
150201

202+
203+
/**
204+
* @brief Populate the descriptors (if any) for this characteristic.
205+
*/
206+
void BLERemoteCharacteristic::getDescriptors() {
207+
ESP_LOGD(LOG_TAG, ">> getDescriptors() for characteristic: %s", getUUID().toString().c_str());
208+
209+
removeDescriptors(); // Remove any existing descriptors.
210+
211+
/*
212+
uint16_t count;
213+
esp_gatt_status_t status = ::esp_ble_gattc_get_attr_count(
214+
getRemoteService()->getClient()->getGattcIf(),
215+
getRemoteService()->getClient()->getConnId(),
216+
ESP_GATT_DB_DESCRIPTOR,
217+
getRemoteService()->getStartHandle(),
218+
getRemoteService()->getEndHandle(),
219+
getHandle(), // Characteristic handle ... only used for ESP_GATT_DB_DESCRIPTOR
220+
&count
221+
);
222+
if (status != ESP_GATT_OK) {
223+
ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_attr_count: %s", BLEUtils::gattStatusToString(status).c_str());
224+
} else {
225+
ESP_LOGD(LOG_TAG, "Number of descriptors associated with characteristic is %d", count);
226+
}
227+
*/
228+
229+
230+
// Loop over each of the descriptors within the service associated with this characteristic.
231+
// For each descriptor we find, create a BLERemoteDescriptor instance.
232+
uint16_t offset = 0;
233+
esp_gattc_descr_elem_t result;
234+
while(1) {
235+
uint16_t count = 1;
236+
esp_gatt_status_t status = ::esp_ble_gattc_get_all_descr(
237+
getRemoteService()->getClient()->getGattcIf(),
238+
getRemoteService()->getClient()->getConnId(),
239+
getHandle(),
240+
&result,
241+
&count,
242+
offset
243+
);
244+
245+
if (status == ESP_GATT_INVALID_OFFSET) { // We have reached the end of the entries.
246+
break;
247+
}
248+
249+
if (status != ESP_GATT_OK) {
250+
ESP_LOGE(LOG_TAG, "esp_ble_gattc_get_all_descr: %s", BLEUtils::gattStatusToString(status).c_str());
251+
break;
252+
}
253+
254+
if (count == 0) {
255+
break;
256+
}
257+
ESP_LOGD(LOG_TAG, "Found a descriptor: Handle: %d, UUID: %s", result.handle, BLEUUID(result.uuid).toString().c_str());
258+
259+
// We now have a new characteristic ... let us add that to our set of known characteristics
260+
BLERemoteDescriptor *pNewRemoteDescriptor = new BLERemoteDescriptor(
261+
result.handle,
262+
BLEUUID(result.uuid),
263+
this
264+
);
265+
266+
m_descriptorMap.insert(std::pair<std::string, BLERemoteDescriptor*>(pNewRemoteDescriptor->getUUID().toString(), pNewRemoteDescriptor));
267+
268+
offset++;
269+
} // while true
270+
//m_haveCharacteristics = true; // Remember that we have received the characteristics.
271+
ESP_LOGD(LOG_TAG, "<< getDescriptors(): Found %d descriptors.", offset);
272+
} // getDescriptors
273+
274+
275+
276+
/**
277+
* @brief Get the handle for this characteristic.
278+
* @return The handle for this characteristic.
279+
*/
151280
uint16_t BLERemoteCharacteristic::getHandle() {
152-
ESP_LOGD(LOG_TAG, ">> getHandle: Characteristic: %s", getUUID().toString().c_str());
153-
ESP_LOGD(LOG_TAG, "<< getHandle: %d 0x%.2x", m_handle, m_handle);
281+
//ESP_LOGD(LOG_TAG, ">> getHandle: Characteristic: %s", getUUID().toString().c_str());
282+
//ESP_LOGD(LOG_TAG, "<< getHandle: %d 0x%.2x", m_handle, m_handle);
154283
return m_handle;
155-
}
284+
} // getHandle
156285

157286

287+
/**
288+
* @brief Get the descriptor instance with the given UUID that belongs to this characteristic.
289+
* @param [in] uuid The UUID of the descriptor to find.
290+
* @return The Remote descriptor (if present) or null if not present.
291+
*/
292+
BLERemoteDescriptor* BLERemoteCharacteristic::getDescriptor(BLEUUID uuid) {
293+
ESP_LOGD(LOG_TAG, ">> getDescriptor: uuid: %s", uuid.toString().c_str());
294+
std::string v = uuid.toString();
295+
for (auto &myPair : m_descriptorMap) {
296+
if (myPair.first == v) {
297+
ESP_LOGD(LOG_TAG, "<< getDescriptor: found");
298+
return myPair.second;
299+
}
300+
}
301+
ESP_LOGD(LOG_TAG, "<< getDescriptor: Not found");
302+
return nullptr;
303+
} // getDescriptor
304+
305+
306+
/**
307+
* @brief Get the remote service associated with this characteristic.
308+
* @return The remote service associated with this characteristic.
309+
*/
310+
BLERemoteService *BLERemoteCharacteristic::getRemoteService() {
311+
return m_pRemoteService;
312+
} // getRemoteService
313+
314+
315+
/**
316+
* @brief Get the UUID for this characteristic.
317+
* @return The UUID for this characteristic.
318+
*/
158319
BLEUUID BLERemoteCharacteristic::getUUID() {
159320
return m_uuid;
160-
}
321+
} // getUUID
322+
161323

162324
/**
163325
* @brief Read an unsigned 16 bit value
@@ -240,7 +402,7 @@ void BLERemoteCharacteristic::registerForNotify(
240402
uint8_t* pData,
241403
size_t length,
242404
bool isNotify)) {
243-
ESP_LOGD(LOG_TAG, ">> registerForNotify()");
405+
ESP_LOGD(LOG_TAG, ">> registerForNotify(): %s", toString().c_str());
244406

245407
m_notifyCallback = notifyCallback; // Save the notification callback.
246408

@@ -275,6 +437,21 @@ void BLERemoteCharacteristic::registerForNotify(
275437
} // registerForNotify
276438

277439

440+
/**
441+
* @brief Delete the descriptors in the descriptor map.
442+
* We maintain a map called m_descriptorMap that contains pointers to BLERemoteDescriptors
443+
* object references. Since we allocated these in this class, we are also responsible for deleteing
444+
* them. This method does just that.
445+
* @return N/A.
446+
*/
447+
void BLERemoteCharacteristic::removeDescriptors() {
448+
for (auto &myPair : m_descriptorMap) {
449+
delete myPair.second;
450+
}
451+
m_descriptorMap.empty();
452+
} // removeCharacteristics
453+
454+
278455
/**
279456
* @brief Convert a BLERemoteCharacteristic to a string representation;
280457
* @return a String representation.

cpp_utils/BLERemoteCharacteristic.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <esp_gattc_api.h>
1616

1717
#include "BLERemoteService.h"
18+
#include "BLERemoteDescriptor.h"
1819
#include "BLEUUID.h"
1920
#include "FreeRTOS.h"
2021

@@ -26,9 +27,10 @@ class BLERemoteDescriptor;
2627
*/
2728
class BLERemoteCharacteristic {
2829
public:
29-
30+
~BLERemoteCharacteristic();
3031

3132
// Public member functions
33+
BLERemoteDescriptor *getDescriptor(BLEUUID uuid);
3234
BLEUUID getUUID();
3335
std::string readValue(void);
3436
uint8_t readUInt8(void);
@@ -44,14 +46,18 @@ class BLERemoteCharacteristic {
4446
BLERemoteCharacteristic(uint16_t handle, BLEUUID uuid, esp_gatt_char_prop_t charProp, BLERemoteService* pRemoteService);
4547
friend class BLEClient;
4648
friend class BLERemoteService;
49+
friend class BLERemoteDescriptor;
4750

4851
// Private member functions
4952
void gattClientEventHandler(
5053
esp_gattc_cb_event_t event,
5154
esp_gatt_if_t gattc_if,
5255
esp_ble_gattc_cb_param_t* evtParam);
56+
void getDescriptors();
57+
uint16_t getHandle();
58+
BLERemoteService* getRemoteService();
59+
void removeDescriptors();
5360

54-
uint16_t getHandle();
5561
// Private properties
5662
BLEUUID m_uuid;
5763
esp_gatt_char_prop_t m_charProp;

0 commit comments

Comments
 (0)
0