8000 Code changes for #103 · lucadentella/esp32-snippets@7d97376 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d97376

Browse files
committed
Code changes for nkolban#103
1 parent 2f1dc0c commit 7d97376

13 files changed

+230
-220
lines changed

cpp_utils/BLEAdvertisedDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ BLEAddress BLEAdvertisedDevice::getAddress() {
6060
*
6161
* @return The appearance of the advertised device.
6262
*/
63-
uint16_t BLEAdvertisedDevice::getApperance() {
63+
uint16_t BLEAdvertisedDevice::getAppearance() {
6464
return m_appearance;
6565
}
6666

@@ -390,7 +390,7 @@ std::string BLEAdvertisedDevice::toString() {
390390
std::stringstream ss;
391391
ss << "Name: " << getName() << ", Address: " << getAddress().toString();
392392
if (haveAppearance()) {
393-
ss << ", appearance: " << getApperance();
393+
ss << ", appearance: " << getAppearance();
394394
}
395395
if (haveManufacturerData()) {
396396
char *pHex = BLEUtils::buildHexData(nullptr, (uint8_t*)getManufacturerData().data(), getManufacturerData().length());

cpp_utils/BLEAdvertisedDevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class BLEAdvertisedDevice {
3030
BLEAdvertisedDevice();
3131

3232
BLEAddress getAddress();
33-
uint16_t getApperance();
33+
uint16_t getAppearance();
3434
std::string getManufacturerData();
3535
std::string getName();
3636
int getRSSI();

cpp_utils/BLECharacteristicMap.cpp

Lines changed: 63 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@
1010
#include <iomanip>
1111
#include "BLEService.h"
1212

13+
14+
/**
15+
* @brief Return the characteristic by handle.
16+
* @param [in] handle The handle to look up the characteristic.
17+
* @return The characteristic.
18+
*/
19+
BLECharacteristic* BLECharacteristicMap::getByHandle(uint16_t handle) {
20+
return m_handleMap.at(handle);
21+
} // getByHandle
22+
23+
1324
/**
1425
* @brief Return the characteristic by UUID.
1526
* @param [in] UUID The UUID to look up the characteristic.
@@ -19,6 +30,7 @@ BLECharacteristic* BLECharacteristicMap::getByUUID(const char* uuid) {
1930
return getByUUID(BLEUUID(uuid));
2031
}
2132

33+
2234
/**
2335
* @brief Return the characteristic by UUID.
2436
* @param [in] UUID The UUID to look up the characteristic.
@@ -36,26 +48,49 @@ BLECharacteristic* BLECharacteristicMap::getByUUID(BLEUUID uuid) {
3648

3749

3850
/**
39-
* @brief Return the characteristic by handle.
40-
* @param [in] handle The handle to look up the characteristic.
41-
* @return The characteristic.
51+
* @brief Get the first characteristic in the map.
52+
* @return The first characteristic in the map.
4253
*/
43-
BLECharacteristic* BLECharacteristicMap::getByHandle(uint16_t handle) {
44-
return m_handleMap.at(handle);
45-
} // getByHandle
54+
BLECharacteristic* BLECharacteristicMap::getFirst() {
55+
m_iterator = m_uuidMap.begin();
56+
if (m_iterator == m_uuidMap.end()) {
57+
return nullptr;
58+
}
59+
BLECharacteristic* pRet = m_iterator->second;
60+
m_iterator++;
61+
return pRet;
62+
} // getFirst
4663

4764

4865
/**
49-
* @brief Set the characteristic by UUID.
50-
* @param [in] uuid The uuid of the characteristic.
51-
* @param [in] characteristic The characteristic to cache.
52-
* @return N/A.
66+
* @brief Get the next characteristic in the map.
67+
* @return The next characteristic in the map.
5368
*/
54-
void BLECharacteristicMap::setByUUID(
55-
BLEUUID uuid,
56-
BLECharacteristic *pCharacteristic) {
57-
m_uuidMap.insert(std::pair<std::string, BLECharacteristic *>(uuid.toString(), pCharacteristic));
58-
} // setByUUID
69+
BLECharacteristic* BLECharacteristicMap::getNext() {
70+
if (m_iterator == m_uuidMap.end()) {
71+
return nullptr;
72+
}
73+
BLECharacteristic* pRet = m_iterator->second;
74+
m_iterator++;
75+
return pRet;
76+
} // getNext
77+
78+
79+
/**
80+
* @brief Pass the GATT server event onwards to each of the characteristics found in the mapping
81+
* @param [in] event
82+
* @param [in] gatts_if
83+
* @param [in] param
84+
*/
85+
void BLECharacteristicMap::handleGATTServerEvent(
86+
esp_gatts_cb_event_t event,
87+
esp_gatt_if_t gatts_if,
88+
esp_ble_gatts_cb_param_t* param) {
89+
// Invoke the handler for every Service we have.
90+
for (auto &myPair : m_uuidMap) {
91+
myPair.second->handleGATTServerEvent(event, gatts_if, param);
92+
}
93+
} // handleGATTServerEvent
5994

6095

6196
/**
@@ -70,6 +105,19 @@ void BLECharacteristicMap::setByHandle(uint16_t handle,
70105
} // setByHandle
71106

72107

108+
/**
109+
* @brief Set the characteristic by UUID.
110+
* @param [in] uuid The uuid of the characteristic.
111+
* @param [in] characteristic The characteristic to cache.
112+
* @return N/A.
113+
*/
114+
void BLECharacteristicMap::setByUUID(
115+
BLEUUID uuid,
116+
BLECharacteristic *pCharacteristic) {
117+
m_uuidMap.insert(std::pair<std::string, BLECharacteristic *>(uuid.toString(), pCharacteristic));
118+
} // setByUUID
119+
120+
73121
/**
74122
* @brief Return a string representation of the characteristic map.
75123
* @return A string representation of the characteristic map.
@@ -89,48 +137,4 @@ std::string BLECharacteristicMap::toString() {
89137
} // toString
90138

91139

92-
/**
93-
* @breif Pass the GATT server event onwards to each of the characteristics found in the mapping
94-
* @param [in] event
95-
* @param [in] gatts_if
96-
* @param [in] param
97-
*/
98-
void BLECharacteristicMap::handleGATTServerEvent(
99-
esp_gatts_cb_event_t event,
100-
esp_gatt_if_t gatts_if,
101-
esp_ble_gatts_cb_param_t *param) {
102-
// Invoke the handler for every Service we have.
103-
for (auto &myPair : m_uuidMap) {
104-
myPair.second->handleGATTServerEvent(event, gatts_if, param);
105-
}
106-
} // handleGATTServerEvent
107-
108-
109-
/**
110-
* @brief Get the first characteristic in the map.
111-
* @return The first characteristic in the map.
112-
*/
113-
BLECharacteristic* BLECharacteristicMap::getFirst() {
114-
m_iterator = m_uuidMap.begin();
115-
if (m_iterator == m_uuidMap.end()) {
116-
return nullptr;
117-
}
118-
BLECharacteristic *pRet = m_iterator->second;
119-
m_iterator++;
120-
return pRet;
121-
} // getFirst
122-
123-
124-
/**
125-
* @brief Get the next characteristic in the map.
126-
* @return The next characteristic in the map.
127-
*/
128-
BLECharacteristic* BLECharacteristicMap::getNext() {
129-
if (m_iterator == m_uuidMap.end()) {
130-
return nullptr;
131-
}
132-
BLECharacteristic *pRet = m_iterator->second;
133-
m_iterator++;
134-
return pRet;
135-
} // getNext
136140
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEDevice.cpp

Lines changed: 53 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,20 @@
77
#include "sdkconfig.h"
88
#if defined(CONFIG_BT_ENABLED)
99
#include <freertos/FreeRTOS.h>
10+
#include <freertos/event_groups.h>
1011
#include <freertos/task.h>
1112
#include <esp_err.h>
1213
#include <nvs_flash.h>
13-
#include <freertos/FreeRTOS.h>
14-
#include <freertos/event_groups.h>
15-
#include <bt.h> // ESP32 BLE
16-
#include <esp_bt_main.h> // ESP32 BLE
17-
#include <esp_gap_ble_api.h> // ESP32 BLE
18-
// ESP32 BLE
19-
#include <esp_gatts_api.h> // ESP32 BLE
20-
#include <esp_err.h> // ESP32 ESP-IDF
21-
#include <esp_log.h> // ESP32 ESP-IDF
22-
#include <map> // Part of C++ STL
23-
#include <sstream>
24-
#include <iomanip>
14+
#include <bt.h> // ESP32 BLE
15+
#include <esp_bt_main.h> // ESP32 BLE
16+
#include <esp_gap_ble_api.h> // ESP32 BLE
17+
#include <esp_gatts_api.h> // ESP32 BLE
18+
#include <esp_gattc_api.h> // ESP32 BLE
19+
#include <esp_err.h> // ESP32 ESP-IDF
20+
#include <esp_log.h> // ESP32 ESP-IDF
21+
#include <map> // Part of C++ Standard library
22+
#include <sstream> // Part of C++ Standard library
23+
#include <iomanip> // Part of C++ Standard library
2524

2625
#include "BLEDevice.h"
2726
#include "BLEClient.h"
@@ -30,41 +29,54 @@
3029

3130
static const char* LOG_TAG = "BLEDevice";
3231

33-
BLEServer *BLEDevice::m_bleServer = nullptr;
34-
BLEScan *BLEDevice::m_pScan = nullptr;
35-
BLEClient *BLEDevice::m_pClient = nullptr;
36-
37-
#include <esp_gattc_api.h>
3832

33+
/**
34+
* Singletons for the BLEDevice.
35+
*/
36+
BLEServer* BLEDevice::m_pServer = nullptr;
37+
BLEScan* BLEDevice::m_pScan = nullptr;
38+
BLEClient* BLEDevice::m_pClient = nullptr;
3939

40+
/**
41+
* @brief Create a new instance of a client.
42+
* @return A new instance of the client.
43+
*/
4044
BLEClient* BLEDevice::createClient() {
4145
m_pClient = new BLEClient();
4246
return m_pClient;
4347
} // createClient
4448

49+
50+
/**
51+
* @brief Create a new instance of a server.
52+
* @return A new instance of the server.
53+
*/
4554
BLEServer* BLEDevice::createServer() {
46-
return new BLEServer();
47-
}
55+
m_pServer = new BLEServer();
56+
return m_pServer;
57+
} // createServer
4858

4959

5060
/**
5161
* @brief Handle GATT server events.
5262
*
53-
* @param [in] event
54-
* @param [in] gatts_if
55-
* @param [in] param
63+
* @param [in] event The event that has been newly received.
64+
* @param [in] gatts_if The connection to the GATT interface.
65+
* @param [in] param Parameters for the event.
5666
*/
5767
void BLEDevice::gattServerEventHandler(
5868
esp_gatts_cb_event_t event,
5969
esp_gatt_if_t gatts_if,
60-
esp_ble_gatts_cb_param_t *param
70+
esp_ble_gatts_cb_param_t* param
6171
) {
6272
ESP_LOGD(LOG_TAG, "gattServerEventHandler [esp_gatt_if: %d] ... %s",
6373
gatts_if,
6474
BLEUtils::gattServerEventTypeToString(event).c_str());
75+
6576
BLEUtils::dumpGattServerEvent(event, gatts_if, param);
66-
if (BLEDevice::m_bleServer != nullptr) {
67-
BLEDevice::m_bleServer->handleGATTServerEvent(event, gatts_if, param);
77+
78+
if (BLEDevice::m_pServer != nullptr) {
79+
BLEDevice::m_pServer->handleGATTServerEvent(event, gatts_if, param);
6880
}
6981
} // gattServerEventHandler
7082

@@ -73,10 +85,6 @@ void BLEDevice::gattServerEventHandler(
7385
* @brief Handle GATT client events.
7486
*
7587
* Handler for the GATT client events.
76-
* * `ESP_GATTC_OPEN_EVT` – Invoked when a connection is opened.
77-
* * `ESP_GATTC_PREP_WRITE_EVT` – Response to write a characteristic.
78-
* * `ESP_GATTC_READ_CHAR_EVT` – Response to read a characteristic.
79-
* * `ESP_GATTC_REG_EVT` – Invoked when a GATT client has been registered.
8088
*
8189
* @param [in] event
8290
* @param [in] gattc_if
@@ -90,12 +98,13 @@ void BLEDevice::gattClientEventHandler(
9098
ESP_LOGD(LOG_TAG, "gattClientEventHandler [esp_gatt_if: %d] ... %s",
9199
gattc_if, BLEUtils::gattClientEventTypeToString(event).c_str());
92100
BLEUtils::dumpGattClientEvent(event, gattc_if, param);
93-
101+
/*
94102
switch(event) {
95103
default: {
96104
break;
97105
}
98106
} // switch
107+
*/
99108

100109
// If we have a client registered, call it.
101110
if (BLEDevice::m_pClient != nullptr) {
@@ -128,8 +137,8 @@ void BLEDevice::gapEventHandler(
128137
}
129138
} // switch
130139

131-
if (BLEDevice::m_bleServer != nullptr) {
132-
BLEDevice::m_bleServer->handleGAPEvent(event, param);
140+
if (BLEDevice::m_pServer != nullptr) {
141+
BLEDevice::m_pServer->handleGAPEvent(event, param);
133142
}
134143

135144
if (BLEDevice::m_pScan != nullptr) {
@@ -138,6 +147,18 @@ void BLEDevice::gapEventHandler(
138147
} // gapEventHandler
139148

140149

150+
/**
151+
* @brief Retrieve the Scan object that we use for scanning.
152+
* @return The scanning object reference.
153+
*/
154+
BLEScan* BLEDevice::getScan() {
155+
if (m_pScan == nullptr) {
156+
m_pScan = new BLEScan();
157+
}
158+
return m_pScan;
159+
} // getScan
160+
161+
141162
/**
142163
* @brief Initialize the %BLE environment.
143164
* @param deviceName The device name of the device.
@@ -209,18 +230,4 @@ void BLEDevice::init(std::string deviceName) {
209230
} // init
210231

211232

212-
213-
/**
214-
* @brief Retrieve the Scan object that we use for scanning.
215-
* @return The scanning object reference.
216-
*/
217-
BLEScan* BLEDevice::getScan() {
218-
if (m_pScan == nullptr) {
219-
m_pScan = new BLEScan();
220-
}
221-
return m_pScan;
222-
} // getScan
223-
224-
225-
226233
#endif // CONFIG_BT_ENABLED

0 commit comments

Comments
 (0)
0