8000 Sync 2017-07-16 · copercini/esp32-snippets@5cc98cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cc98cf

Browse files
committed
Sync 2017-07-16
1 parent ca5f255 commit 5cc98cf

14 files changed

+130
-62
lines changed

cpp_utils/BLEAdvertising.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,35 @@ void BLEAdvertising::setAppearance(uint16_t appearance) {
5858

5959
/**
6060
* @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.
6165
* @param [in] uuid The UUID of the service.
6266
* @return N/A.
6367
*/
64-
void BLEAdvertising::setServiceUUID(BLEUUID uuid) {
65-
ESP_LOGD(LOG_TAG, ">> setServiceUUID(%s)", uuid.toString().c_str());
66-
m_serviceUUID = uuid; // Save the new service UUID
67-
esp_bt_uuid_t espUUID = *m_serviceUUID.getNative();
68-
switch(espUUID.len) {
68+
void BLEAdvertising::setServiceUUID(BLEUUID serviceUUID) {
69+
ESP_LOGD(LOG_TAG, ">> setServiceUUID - %s", serviceUUID.toString().c_str());
70+
m_serviceUUID = serviceUUID; // Save the new service UUID
71+
esp_bt_uuid_t* espUUID = m_serviceUUID.getNative();
72+
switch(espUUID->len) {
6973
case ESP_UUID_LEN_16: {
7074
m_advData.service_uuid_len = 2;
71-
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID.uuid.uuid16);
75+
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid16);
7276
break;
7377
}
7478
case ESP_UUID_LEN_32: {
7579
m_advData.service_uuid_len = 4;
76-
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID.uuid.uuid32);
80+
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid32);
7781
break;
7882
}
7983
case ESP_UUID_LEN_128: {
8084
m_advData.service_uuid_len = 16;
81-
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID.uuid.uuid128);
85+
m_advData.p_service_uuid = reinterpret_cast<uint8_t*>(&espUUID->uuid.uuid128);
8286
break;
8387
}
8488
} // switch
85-
ESP_LOGD(LOG_TAG, "<< setServiceUUID()");
89+
ESP_LOGD(LOG_TAG, "<< setServiceUUID");
8690
} // setServiceUUID
8791

8892

@@ -92,7 +96,7 @@ void BLEAdvertising::setServiceUUID(BLEUUID uuid) {
9296
* @return N/A.
9397
*/
9498
void BLEAdvertising::start() {
95-
ESP_LOGD(LOG_TAG, ">> start()");
99+
ESP_LOGD(LOG_TAG, ">> start");
96100

97101
if (m_advData.service_uuid_len > 0) {
98102
uint8_t hexData[16*2+1];
@@ -118,7 +122,7 @@ void BLEAdvertising::start() {
118122
ESP_LOGE(LOG_TAG, "<< esp_ble_gap_start_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
119123
return;
120124
}
121-
ESP_LOGD(LOG_TAG, "<< start();")
125+
ESP_LOGD(LOG_TAG, "<< start")
122126
} // start
123127

124128

@@ -128,10 +132,12 @@ void BLEAdvertising::start() {
128132
* @return N/A.
129133
*/
130134
void BLEAdvertising::stop() {
135+
ESP_LOGD(LOG_TAG, ">> stop");
131136
esp_err_t errRc = ::esp_ble_gap_stop_advertising();
132137
if (errRc != ESP_OK) {
133138
ESP_LOGE(LOG_TAG, "esp_ble_gap_stop_advertising: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
134139
return;
135140
}
141+
ESP_LOGD(LOG_TAG, "<< stop");
136142
} // stop
137143
#endif /* CONFIG_BT_ENABLED */

cpp_utils/BLEAdvertising.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class BLEAdvertising {
1717
void start();
1818
void stop();
1919
void setAppearance(uint16_t appearance);
20-
void setServiceUUID(BLEUUID uuid);
20+
void setServiceUUID(BLEUUID serviceUUID);
2121
private:
2222
esp_ble_adv_data_t m_advData;
2323
esp_ble_adv_params_t m_advParams;

cpp_utils/BLECharacteristic.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,22 @@ void BLECharacteristic::executeCreate(BLEService* pService) {
8787
esp_attr_control_t control;
8888
control.auto_rsp = ESP_GATT_RSP_BY_APP;
8989

90+
m_semaphoreCreateEvt.take("executeCreate");
9091
esp_err_t errRc = ::esp_ble_gatts_add_char(
9192
m_pService->getHandle(),
9293
getUUID().getNative(),
9394
static_cast<esp_gatt_perm_t>(ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE),
9495
getProperties(),
9596
&m_value,
96-
&control); // Whether to autorespond or not.
97+
&control); // Whether to auto respond or not.
9798

9899
if (errRc != ESP_OK) {
99100
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_add_char: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
100101
return;
101102
}
102103

104+
m_semaphoreCreateEvt.wait("executeCreate");
105+
103106
// Now that we have registered the characteristic, we must also register all the descriptors associated with this
104107
// characteristic. We iterate through each of those and invoke the registration call to register them with the
105108
// ESP environment.
@@ -111,7 +114,7 @@ void BLECharacteristic::executeCreate(BLEService* pService) {
111114
pDescriptor = m_descriptorMap.getNext();
112115
} // End while
113116

114-
ESP_LOGD(LOG_TAG, "<< executeCreate()");
117+
ESP_LOGD(LOG_TAG, "<< executeCreate");
115118
} // executeCreate
116119

117120

@@ -168,6 +171,19 @@ void BLECharacteristic::handleGATTServerEvent(
168171
esp_gatt_if_t gatts_if,
169172
esp_ble_gatts_cb_param_t* param) {
170173
switch(event) {
174+
// ESP_GATTS_ADD_CHAR_EVT - Indicate that a characteristic was added to the service.
175+
// add_char:
176+
// - esp_gatt_status_t status
177+
// - uint16_t attr_handle
178+
// - uint16_t service_handle
179+
// - esp_bt_uuid_t char_uuid
180+
case ESP_GATTS_ADD_CHAR_EVT: {
181+
if (getUUID().equals(BLEUUID(param->add_char.char_uuid)) &&
182+
getService()->getHandle()==param->add_char.service_handle) {
183+
m_semaphoreCreateEvt.give();
184+
}
185+
break;
186+
} // ESP_GATTS_ADD_CHAR_EVT
171187

172188
// ESP_GATTS_WRITE_EVT - A request to write the value of a characteristic has arrived.
173189
//
@@ -357,9 +373,9 @@ void BLECharacteristic::setCallbacks(BLECharacteristicCallbacks* pCallbacks) {
357373

358374

359375
void BLECharacteristic::setHandle(uint16_t handle) {
360-
ESP_LOGD(LOG_TAG, ">> setHandle(0x%.2x): Setting handle to be 0x%.2x", handle, handle);
376+
ESP_LOGD(LOG_TAG, ">> setHandle: handle=0x%.2x, characteristic uuid=%s", handle, getUUID().toString().c_str());
361377
m_handle = handle;
362-
ESP_LOGD(LOG_TAG, "<< setHandle()");
378+
ESP_LOGD(LOG_TAG, "<< setHandle");
363379
} // setHandle
364380

365381

@@ -400,14 +416,15 @@ void BLECharacteristic::setReadProperty(bool value) {
400416
*/
401417
void BLECharacteristic::setValue(uint8_t* data, size_t length) {
402418
char *pHex = BLEUtils::buildHexData(nullptr, data, length);
403-
ESP_LOGD(LOG_TAG, ">> setValue(length: %d, %s)", length, pHex);
419+
ESP_LOGD(LOG_TAG, ">> setValue: length=%d, data=%s, characteristic UUID=%s", length, pHex, getUUID().toString().c_str());
404420
free(pHex);
405421
if (length > ESP_GATT_MAX_ATTR_LEN) {
406422
ESP_LOGE(LOG_TAG, "Size %d too large, must be no bigger than %d", length, ESP_GATT_MAX_ATTR_LEN);
407423
return;
408424
}
409425
m_value.attr_len = length;
410426
memcpy(m_value.attr_value, data, length);
427+
ESP_LOGD(LOG_TAG, "<< setValue");
411428
} // setValue
412429

413430

cpp_utils/BLECharacteristic.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "BLEDescriptor.h"
1616
#include "BLEDescriptorMap.h"
1717
#include "BLECharacteristicCallbacks.h"
18+
#include "FreeRTOS.h"
1819

1920
class BLEService;
2021
class BLEDescriptor;
@@ -75,6 +76,7 @@ class BLECharacteristic {
7576
esp_gatt_char_prop_t getProperties();
7677
BLEService *getService();
7778
void setHandle(uint16_t handle);
79+
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
7880
}; // BLECharacteristic
7981
#endif /* CONFIG_BT_ENABLED */
8082
#endif /* COMPONENTS_CPP_UTILS_BLECHARACTERISTIC_H_ */

cpp_utils/BLERemoteCharacteristic.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ void BLERemoteCharacteristic::writeValue(std::string newValue, bool response) {
286286
m_semaphoreWriteCharEvt.take("writeValue");
287287
m_semaphoreWriteCharEvt.give();
288288

289-
ESP_LOGD(LOG_TAG, "<< writeValue()");
289+
ESP_LOGD(LOG_TAG, "<< writeValue");
290290
} // writeValue
291291

292292

cpp_utils/BLEServer.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ BLEServer::BLEServer() {
3131
m_appId = -1;
3232
m_gatts_if = -1;
3333
m_connId = -1;
34-
m_serializeMutex.setName("BLEServer");
3534
BLE::m_bleServer = this;
3635
m_pServerCallbacks = nullptr;
3736
createApp(0);
@@ -51,21 +50,23 @@ void BLEServer::createApp(uint16_t appId) {
5150
* @return A reference to the new service object.
5251
*/
5352
BLEService *BLEServer::createService(BLEUUID uuid) {
54-
ESP_LOGD(LOG_TAG, ">> createService(%s)", uuid.toString().c_str());
55-
m_serializeMutex.take("createService");
53+
ESP_LOGD(LOG_TAG, ">> createService - %s", uuid.toString().c_str());
54+
m_semaphoreCreateEvt.take("createService");
5655

5756
// Check that a service with the supplied UUID does not already exist.
5857
if (m_serviceMap.getByUUID(uuid) != nullptr) {
5958
ESP_LOGE(LOG_TAG, "<< Attempt to create a new service with uuid %s but a service with that UUID already exists.",
6059
uuid.toString().c_str());
61-
m_serializeMutex.give();
60+
m_semaphoreCreateEvt.give();
6261
return nullptr;
6362
}
6463

6564
BLEService *pService = new BLEService(uuid);
6665
m_serviceMap.setByUUID(uuid, pService); // Save a reference to this service being on this server.
6766
pService->executeCreate(this); // Perform the API calls to actually create the service.
6867

68+
m_semaphoreCreateEvt.wait("createService");
69+
6970
ESP_LOGD(LOG_TAG, "<< createService");
7071
return pService;
7172
} // createService
@@ -160,7 +161,7 @@ void BLEServer::handleGATTServerEvent(
160161
case ESP_GATTS_REG_EVT: {
161162
m_gatts_if = gatts_if;
162163

163-
m_serializeMutex.give();
164+
m_semaphoreRegisterAppEvt.give();
164165
break;
165166
} // ESP_GATTS_REG_EVT
166167

@@ -176,8 +177,8 @@ void BLEServer::handleGATTServerEvent(
176177
case ESP_GATTS_CREATE_EVT: {
177178
BLEService *pService = m_serviceMap.getByUUID(param->create.service_id.id.uuid);
178179
m_serviceMap.setByHandle(param->create.service_handle, pService);
179-
pService->setHandle(param->create.service_handle);
180-
m_serializeMutex.give();
180+
//pService->setHandle(param->create.service_handle);
181+
m_semaphoreCreateEvt.give();
181182
break;
182183
} // ESP_GATTS_CREATE_EVT
183184

@@ -249,10 +250,11 @@ void BLEServer::handleGATTServerEvent(
249250
* @return N/A
250251
*/
251252
void BLEServer::registerApp() {
252-
ESP_LOGD(LOG_TAG, ">> registerApp(%d)", m_appId);
253-
m_serializeMutex.take("registerApp"); // Take the mutex, will be released by ESP_GATTS_REG_EVT event.
253+
ESP_LOGD(LOG_TAG, ">> registerApp - %d", m_appId);
254+
m_semaphoreRegisterAppEvt.take("registerApp"); // Take the mutex, will be released by ESP_GATTS_REG_EVT event.
254255
::esp_ble_gatts_app_register(m_appId);
255-
ESP_LOGD(LOG_TAG, "<< registerApp()");
256+
m_semaphoreRegisterAppEvt.wait("registerApp");
257+
ESP_LOGD(LOG_TAG, "<< registerApp");
256258
} // registerApp
257259

258260

@@ -270,9 +272,9 @@ void BLEServer::setCallbacks(BLEServerCallbacks* pCallbacks) {
270272
* Start the server advertising its existence.
271273
*/
272274
void BLEServer::startAdvertising() {
273-
ESP_LOGD(LOG_TAG, ">> startAdvertising()");
275+
ESP_LOGD(LOG_TAG, ">> startAdvertising");
274276
m_bleAdvertising.start();
275-
ESP_LOGD(LOG_TAG, "<< startAdvertising()");
277+
ESP_LOGD(LOG_TAG, "<< startAdvertising");
276278
} // startAdvertising
277279

278280

cpp_utils/BLEServer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ class BLEServer {
4545
BLEAdvertising m_bleAdvertising;
4646
uint16_t m_gatts_if;
4747
uint16_t m_connId;
48-
FreeRTOS::Semaphore m_serializeMutex;
48+
FreeRTOS::Semaphore m_semaphoreRegisterAppEvt = FreeRTOS::Semaphore("RegisterAppEvt");
49+
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
4950
BLEServiceMap m_serviceMap;
5051
BLEServerCallbacks *m_pServerCallbacks;
5152
}; // BLEServer

0 commit comments

Comments
 (0)
0