8000 Code changes for #115 · kebot-embedded/esp32-snippets@ff93684 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff93684

Browse files
committed
Code changes for nkolban#115
1 parent 2920d11 commit ff93684

File tree

8 files changed

+537
-82
lines changed

8 files changed

+537
-82
lines changed

cpp_utils/BLEAdvertising.cpp

Lines changed: 49 additions & 56 deletions
< 57AE td data-grid-cell-id="diff-afe08a1d244f18a25298e69ad8717d594997c1384299071286c7452063190250-101-76-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-deletionNum-bgColor, var(--diffBlob-deletion-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ BLEAdvertising::BLEAdvertising() {
3535
m_advData.p_service_uuid = nullptr;
3636
m_advData.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
3737

38-
m_advDataScanResponse.set_scan_rsp = true;
39-
m_advDataScanResponse.include_name = false;
40-
m_advDataScanResponse.include_txpower = false;
41-
m_advDataScanResponse.min_interval = 0x20;
42-
m_advDataScanResponse.max_interval = 0x40;
43-
m_advDataScanResponse.appearance = 0x00;
44-
m_advDataScanResponse.manufacturer_len = 0;
45-
m_advDataScanResponse.p_manufacturer_data = nullptr;
46-
m_advDataScanResponse.service_data_len = 0;
47-
m_advDataScanResponse.p_service_data = nullptr;
48-
m_advDataScanResponse.service_uuid_len = 0;
49-
m_advDataScanResponse.p_service_uuid = nullptr;
50-
m_advDataScanResponse.flag = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
51-
5238
m_advParams.adv_int_min = 0x20;
5339
m_advParams.adv_int_max = 0x40;
5440
m_advParams.adv_type = ADV_TYPE_IND;
@@ -58,6 +44,24 @@ BLEAdvertising::BLEAdvertising() {
5844
} // BLEAdvertising
5945

6046

47+
/**
48+
* @brief Add a service uuid to exposed list of services.
49+
* @param [in] serviceUUID The UUID of the service to expose.
50+
*/
51+
void BLEAdvertising::addServiceUUID(BLEUUID serviceUUID) {
52+
m_serviceUUIDs.push_back(serviceUUID);
53+
} // addServiceUUID
54+
55+
56+
/**
57+
* @brief Add a service uuid to exposed list of services.
58+
* @param [in] serviceUUID The string representation of the service to expose.
59+
*/
60+
void BLEAdvertising::addServiceUUID(const char* serviceUUID) {
61+
addServiceUUID(BLEUUID(serviceUUID));
62+
} // addServiceUUID
63+
64+
6165
/**
6266
* @brief Set the device appearance in the advertising data.
6367
* The appearance attribute is of type 0x19. The codes for distinct appearances can be found here:
@@ -70,38 +74,6 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
7074
} // setAppearance
7175

7276

73-
/**
74-
* @brief Set the service UUID.
75-
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
76-
* ESP-IDF advertising functions. In this method, we see two fields within that structure
77-
* namely service_uuid_len and p_service_uuid to be the information supplied in the passed
78-
* in service uuid.
79-
* @param [in] uuid The UUID of the service.
80-
* @return N/A.
81-
*/
82-
void BLEAdvertising::setServiceUUID(const char* serviceUUID) {
83-
return setServiceUUID(BLEUUID(serviceUUID));
84-
}
85-
/**
86-
* @brief Set the service UUID.
87-
* We maintain a class member called m_advData (esp_ble_adv_data_t) that is passed to the
88-
* ESP-IDF advertising functions. In this method, we see two fields within that structure
89-
* namely service_uuid_len and p_service_uuid to be the information supplied in the passed
90-
* in service uuid.
91-
* @param [in] uuid The UUID of the service.
92-
* @return N/A.
93-
*/
94-
void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
95-
ESP_LOGD(LOG_TAG, ">> setServiceUUID - %s", serviceUUID.toString().c_str());
96-
m_serviceUUID = serviceUUID; // Save the new service UUID
97-
m_serviceUUID128 = serviceUUID.to128();
98-
99-
m_advDataScanResponse.service_uuid_len = 16;
100-
m_advDataScanResponse.p_service_uuid = reinterpret_cast<uint8_t*>(&m_serviceUUID128.getNative()->uuid.uuid128);
101-
ESP_LOGD(LOG_TAG, "<< setServiceUUID");
102-
} // setServiceUUID
103-
104-
10577
/**
10678
* @brief Start advertising.
10779
* Start advertising.
@@ -110,30 +82,49 @@ void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
11082
void BLEAdvertising::start() {
11183
ESP_LOGD(LOG_TAG, ">> start");
11284

113-
if (m_advDataScanResponse.service_uuid_len > 0) {
114-
uint8_t hexData[16*2+1];
115-
BLEUtils::buildHexData(hexData, m_advDataScanResponse.p_service_uuid, m_advDataScanResponse.service_uuid_len);
116-
ESP_LOGD(LOG_TAG, " - Service: service_uuid_len=%d, p_service_uuid=0x%x (data=%s)",
117-
m_advDataScanResponse.service_uuid_len,
118-
(uint32_t)m_advDataScanResponse.p_service_uuid,
119-
(m_advDataScanResponse.service_uuid_len > 0?(char *)hexData:"N/A")
120-
);
121-
} // We have a service to advertise
85+
// We have a vector of service UUIDs that we wish to advertise. In order to use the
86+
// ESP-IDF framework, these must be supplied in a contiguous array of their 128bit (16 byte)
87+
// representations. If we have 1 or more services to advertise then we allocate enough
88+
// storage to host them and then copy them in one at a time into the contiguous storage.
89+
int numServices = m_serviceUUIDs.size();
90+
if (numServices > 0) {
91+
m_advData.service_uuid_len = 16*numServices;
92+
m_advData.p_service_uuid = new uint8_t[m_advData.service_uuid_len];
93+
uint8_t* p = m_advData.p_service_uuid;
94+
for (int i=0; i<numServices; i++) {
95+
ESP_LOGD(LOG_TAG, "- advertising service: %s", m_serviceUUIDs[i].toString().c_str());
96+
BLEUUID serviceUUID128 = m_serviceUUIDs[i].to128();
97+
memcpy(p, serviceUUID128.getNative()->uuid.uuid128, 16);
98+
p+=16;
99+
}
100+
} else {
101+
m_advData.service_uuid_len = 0;
102+
ESP_LOGD(LOG_TAG, "- no services advertised");
103+
}
122104

123105

124106
// Set the configuration for advertising.
107+
m_advData.set_scan_rsp = false;
125108
esp_err_t errRc = ::esp_ble_gap_config_adv_data(&m_advData);
126109
if (errRc != ESP_OK) {
127110
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
128111
return;
129112
}
130113

131-
errRc = ::esp_ble_gap_config_adv_data(&m_advDataScanResponse);
114+
m_advData.set_scan_rsp = true;
115+
errRc = ::esp_ble_gap_config_adv_data(&m_advData);
132116
if (errRc != ESP_OK) {
133117
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_config_adv_data (Scan response): rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
134118
return;
135119
}
136120

121+
// If we had services to advertise then we previously allocated some storage for them.
122+
// Here we release that storage.
123+
if (m_advData.service_uuid_len > 0) {
124+
delete[] m_advData.p_service_uuid;
125+
m_advData.p_service_uuid = nullptr;
126+
}
127+
137128
// Start advertising.
138129
errRc = ::esp_ble_gap_start_advertising(&m_advParams);
139130
if (errRc != ESP_OK) {
@@ -158,4 +149,6 @@ void BLEAdvertising::stop() {
158149
}
159150
ESP_LOGD(LOG_TAG, "<< stop");
160151
} // stop
152+
153+
161154
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEAdvertising.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#if defined(CONFIG_BT_ENABLED)
1212
#include <esp_gap_ble_api.h>
1313
#include "BLEUUID.h"
14+
#include <vector>
1415

1516
/**
1617
* @brief Perform and manage %BLE advertising.
@@ -20,17 +21,15 @@
2021
class BLEAdvertising {
2122
public:
2223
BLEAdvertising();
24+
void addServiceUUID(BLEUUID serviceUUID);
25+
void addServiceUUID(const char* serviceUUID);
2326
void start();
2427
void stop();
2528
void setAppearance(uint16_t appearance);
26-
void setServiceUUID(const char* serviceUUID);
27-
void setServiceUUID(BLEUUID serviceUUID);
2829
private:
2930
esp_ble_adv_data_t m_advData;
30-
esp_ble_adv_data_t m_advDataScanResponse;
3131
esp_ble_adv_params_t m_advParams;
32-
BLEUUID m_serviceUUID;
33-
BLEUUID m_serviceUUID128;
32+
std::vector<BLEUUID> m_serviceUUIDs;
3433
};
3534
#endif /* CONFIG_BT_ENABLED */
3635
#endif /* COMPONENTS_CPP_UTILS_BLEADVERTISING_H_ */

cpp_utils/tests/BLETests/Sample1.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,64 @@
22
#include "BLEServer.h"
33
#include <esp_log.h>
44
#include <string>
5+
#include <stdio.h>
56

67
#include "BLEDevice.h"
78
#include "sdkconfig.h"
89

910
// See the following for generating UUIDs:
1011
// https://www.uuidgenerator.net/
1112

12-
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
13-
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
13+
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
14+
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
15+
#define CHARACTERISTIC_UUID2 "54059634-9448-404f-9af4-7d14556f3ad8"
16+
#define CHARACTERISTIC_UUID3 "78f8a814-7b20-40ca-b970-0aba448c53b1"
17+
#define CHARACTERISTIC_UUID4 "03a55273-c1ef-4eab-a6c0-7ff11509122f"
18+
#define CHARACTERISTIC_UUID5 "0d19566d-2144-4443-9779-19d42e283439"
1419

1520
static void run() {
1621
BLEDevice::init("MYDEVICE");
1722

18-
BLEServer *pServer = BLEDevice::createServer();
23+
BLEServer* pServer = BLEDevice::createServer();
1924

20-
BLEService *pService = pServer->createService(BLEUUID(SERVICE_UUID));
25+
BLEService* pService = pServer->createService(BLEUUID(SERVICE_UUID));
2126

22-
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
27+
BLECharacteristic* pCharacteristic = pService->createCharacteristic(
2328
BLEUUID(CHARACTERISTIC_UUID),
2429
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
2530
);
2631

32+
pCharacteristic = pService->createCharacteristic(
33+
BLEUUID(CHARACTERISTIC_UUID2),
34+
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
35+
);
36+
37+
pCharacteristic = pService->createCharacteristic(
38+
BLEUUID(CHARACTERISTIC_UUID3),
39+
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
40+
);
41+
42+
pCharacteristic = pService->createCharacteristic(
43+
BLEUUID(CHARACTERISTIC_UUID4),
44+
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
45+
);
46+
47+
pCharacteristic = pService->createCharacteristic(
48+
BLEUUID(CHARACTERISTIC_UUID5),
49+
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE
50+
);
51+
52+
2753
pCharacteristic->setValue("Hello World says Neil");
2854

2955
pService->start();
3056

31-
BLEAdvertising *pAdvertising = pServer->getAdvertising();
57+
BLEAdvertising* pAdvertising = pServer->getAdvertising();
3258
pAdvertising->start();
3359
}
3460

3561
void Sample1(void)
3662
{
63+
//esp_log_level_set("*", ESP_LOG_ERROR);
3764
run();
3865
} // app_main

0 commit comments

Comments
 (0)
0