8000 Merge remote-tracking branch 'origin' · Netoperz/esp32-snippets@14cdfb6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 14cdfb6

Browse files
committed
Merge remote-tracking branch 'origin'
2 parents eead14a + faf2839 commit 14cdfb6

File tree

9 files changed

+142
-33
lines changed

9 files changed

+142
-33
lines changed

cpp_utils/BLEDevice.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "GeneralUtils.h"
3131
#ifdef ARDUINO_ARCH_ESP32
3232
#include "esp32-hal-log.h"
33+
#include "esp32-hal-bt.h"
3334
#endif
3435

3536
static const char* LOG_TAG = "BLEDevice";
@@ -331,14 +332,21 @@ uint16_t BLEDevice::m_localMTU = 23;
331332
if(!initialized){
332333
initialized = true; // Set the initialization flag to ensure we are only initialized once.
333334

334-
esp_err_t errRc = ::nvs_flash_init();
335+
esp_err_t errRc = ESP_OK;
336+
#ifdef ARDUINO_ARCH_ESP32
337+
if (!btStart()) {
338+
errRc = ESP_FAIL;
339+
return;
340+
}
341+
#else
342+
errRc = ::nvs_flash_init();
335343
if (errRc != ESP_OK) {
336344
ESP_LOGE(LOG_TAG, "nvs_flash_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
337345
return;
338346
}
339347

340-
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
341-
errRc = esp_bt_controller_init(&bt_cfg);
348+
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
349+
errRc = esp_bt_controller_init(&bt_cfg);
342350
if (errRc != ESP_OK) {
343351
ESP_LOGE(LOG_TAG, "esp_bt_controller_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
344352
return;
@@ -358,18 +366,24 @@ uint16_t BLEDevice::m_localMTU = 23;
358366
ESP_LOGE(LOG_TAG, "esp_bt_controller_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
359367
return;
360368
}
369+
#endif
361370
#endif
362371

363-
errRc = esp_bluedroid_init();
364-
if (errRc != ESP_OK) {
365-
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
366-
return;
372+
esp_bluedroid_status_t bt_state = esp_bluedroid_get_status();
373+
if (bt_state == ESP_BLUEDROID_STATUS_UNINITIALIZED){
374+
errRc = esp_bluedroid_init();
375+
if (errRc != ESP_OK) {
376+
ESP_LOGE(LOG_TAG, "esp_bluedroid_init: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
377+
return;
378+
}
367379
}
368380

369-
errRc = esp_bluedroid_enable();
370-
if (errRc != ESP_OK) {
371-
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
372-
return;
381+
if (bt_state != ESP_BLUEDROID_STATUS_ENABLED){
382+
errRc = esp_bluedroid_enable();
383+
if (errRc != ESP_OK) {
384+
ESP_LOGE(LOG_TAG, "esp_bluedroid_enable: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
385+
return;
386+
}
373387
}
374388

375389
errRc = esp_ble_gap_register_callback(BLEDevice::gapEventHandler);

cpp_utils/BLEScan.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,14 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
189189
/**
190190
* @brief Start scanning.
191191
* @param [in] duration The duration in seconds for which to scan.
192-
* @param [in] scanCompleteCB A function to be called when scanning has completed. This can
193-
* be supplied as nullptr (the default) in which case the call to start will block until scanning has
194-
* been completed.
195-
* @return The BLEScanResults. Only applicable if we are waiting for results.
192+
* @param [in] scanCompleteCB A function to be called when scanning has completed.
193+
* @return True if scan started or false if there was an error.
196194
*/
197-
BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
195+
bool BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
198196
ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration);
199-
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
200197

201198
m_semaphoreScanEnd.take(std::string("start"));
199+
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
202200

203201
m_scanResults.m_vectorAdvertisedDevices.clear();
204202

@@ -207,24 +205,33 @@ BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanR
207205
if (errRc != ESP_OK) {
208206
ESP_LOGE(LOG_TAG, "esp_ble_gap_set_scan_params: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
209207
m_semaphoreScanEnd.give();
210-
return m_scanResults;
208+
return false;
211209
}
212210

213211
errRc = ::esp_ble_gap_start_scanning(duration);
214212

215213
if (errRc != ESP_OK) {
216214
ESP_LOGE(LOG_TAG, "esp_ble_gap_start_scanning: err: %d, text: %s", errRc, GeneralUtils::errorToString(errRc));
217215
m_semaphoreScanEnd.give();
218-
return m_scanResults;
216+
return false;
219217
}
220218

221219
m_stopped = false;
222220

223-
if (m_scanCompleteCB == nullptr) {
221+
ESP_LOGD(LOG_TAG, "<< start()");
222+
return true;
223+
} // start
224+
225+
226+
/**
227+
* @brief Start scanning and block until scanning has been completed.
228+
* @param [in] duration The duration in seconds for which to scan.
229+
* @return The BLEScanResults.
230+
*/
231+
BLEScanResults BLEScan::start(uint32_t duration) {
232+
if(start(duration, nullptr)) {
224233
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
225234
}
226-
227-
ESP_LOGD(LOG_TAG, "<< start()");
228235
return m_scanResults;
229236
} // start
230237

cpp_utils/BLEScan.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ class BLEScan {
5353
bool wantDuplicates = false);
5454
void setInterval(uint16_t intervalMSecs);
5555
void setWindow(uint16_t windowMSecs);
56-
BLEScanResults start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults) = nullptr);
56+
bool start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults));
57+
BLEScanResults start(uint32_t duration);
5758
void stop();
5859

5960
private:

cpp_utils/BLEServer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ void BLEServer::setCallbacks(BLEServerCallbacks* pCallbacks) {
327327
* Remove service
328328
*/
329329
void BLEServer::removeService(BLEService *service) {
330-
esp_ble_gatts_delete_service(handle);
331-
uint16_t handle = service->getHandle();
332-
m_serviceMap->removeService(service);
330+
service->stop();
331+
service->executeDelete();
332+
m_serviceMap.removeService(service);
333333
}
334334

335335
/**

cpp_utils/BLEService.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ void BLEService::executeCreate(BLEServer *pServer) {
9191
} // executeCreate
9292

9393

94+
/**
95+
* @brief Delete the service.
96+
* Delete the service.
97+
* @return N/A.
98+
*/
99+
100+
void BLEService::executeDelete() {
101+
ESP_LOGD(LOG_TAG, ">> executeDelete()");
102+
m_semaphoreDeleteEvt.take("executeDelete"); // Take the mutex and release at event ESP_GATTS_DELETE_EVT
103+
104+
esp_err_t errRc = ::esp_ble_gatts_delete_service( getHandle() );
105+
106+
if (errRc != ESP_OK) {
107+
ESP_LOGE(LOG_TAG, "esp_ble_gatts_delete_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
108+
return;
109+
}
110+
111+
m_semaphoreDeleteEvt.wait("executeDelete");
112+
ESP_LOGD(LOG_TAG, "<< executeDelete");
113+
} // executeDelete
114+
115+
94116
/**
95117
* @brief Dump details of this BLE GATT service.
96118
* @return N/A.
@@ -153,6 +175,34 @@ void BLEService::start() {
153175
} // start
154176

155177

178+
/**
179+
* @brief Stop the service.
180+
* @return Stop the service.
181+
*/
182+
void BLEService::stop() {
183+
// We ask the BLE runtime to start the service and then create each of the characteristics.
184+
// We start the service through its local handle which was returned in the ESP_GATTS_CREATE_EVT event
185+
// obtained as a result of calling esp_ble_gatts_create_service().
186+
//
187+
ESP_LOGD(LOG_TAG, ">> stop(): Stopping service (esp_ble_gatts_stop_service): %s", toString().c_str());
188+
if (m_handle == NULL_HANDLE) {
189+
ESP_LOGE(LOG_TAG, "<< !!! We attempted to stop a service but don't know its handle!");
190+
return;
191+
}
192+
193+
m_semaphoreStopEvt.take("stop");
194+
esp_err_t errRc = ::esp_ble_gatts_stop_service(m_handle);
195+
196+
if (errRc != ESP_OK) {
197+
ESP_LOGE(LOG_TAG, "<< esp_ble_gatts_stop_service: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
198+
return;
199+
}
200+
m_semaphoreStopEvt.wait("stop");
201+
202+
ESP_LOGD(LOG_TAG, "<< stop()");
203+
} // start
204+
205+
156206
/**
157207
* @brief Set the handle associated with this service.
158208
* @param [in] handle The handle associated with the service.
@@ -278,6 +328,19 @@ void BLEService::handleGATTServerEvent(
278328
break;
279329
} // ESP_GATTS_START_EVT
280330

331+
// ESP_GATTS_STOP_EVT
332+
//
333+
// stop:
334+
// esp_gatt_status_t status
335+
// uint16_t service_handle
336+
//
337+
case ESP_GATTS_STOP_EVT: {
338+
if (param->stop.service_handle == getHandle()) {
339+
m_semaphoreStopEvt.give();
340+
}
341+
break;
342+
} // ESP_GATTS_STOP_EVT
343+
281344

282345
// ESP_GATTS_CREATE_EVT
283346
// Called when a new service is registered as having been created.
@@ -299,6 +362,21 @@ void BLEService::handleGATTServerEvent(
299362
break;
300363
} // ESP_GATTS_CREATE_EVT
301364

365+
366+
// ESP_GATTS_DELETE_EVT
367+
// Called when a service is deleted.
368+
//
369+
// delete:
370+
// * esp_gatt_status_t status
371+
// * uint16_t service_handle
372+
//
373+
case ESP_GATTS_DELETE_EVT: {
374+
if (param->del.service_handle == getHandle()) {
375+
m_semaphoreDeleteEvt.give();
376+
}
377+
break;
378+
} // ESP_GATTS_DELETE_EVT
379+
302380
default: {
303381
break;
304382
} // Default

cpp_utils/BLEService.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@ class BLEService {
5757
BLECharacteristic* createCharacteristic(BLEUUID uuid, uint32_t properties);
5858
void dump();
5959
void executeCreate(BLEServer* pServer);
60+
void executeDelete();
6061
BLECharacteristic* getCharacteristic(const char* uuid);
6162
BLECharacteristic* getCharacteristic(BLEUUID uuid);
6263
BLEUUID getUUID();
6364
BLEServer* getServer();
6465
void start();
66+
void stop();
6567
std::string toString();
6668
uint16_t getHandle();
6769
uint8_t m_id = 0;
@@ -82,7 +84,9 @@ class BLEService {
8284
BLEUUID m_uuid;
8385

8486
FreeRTOS::Semaphore m_semaphoreCreateEvt = FreeRTOS::Semaphore("CreateEvt");
87+
FreeRTOS::Semaphore m_semaphoreDeleteEvt = FreeRTOS::Semaphore("DeleteEvt");
8588
FreeRTOS::Semaphore m_semaphoreStartEvt = FreeRTOS::Semaphore("StartEvt");
89+
FreeRTOS::Semaphore m_semaphoreStopEvt = FreeRTOS::Semaphore("StopEvt");
8690

8791
uint32_t m_numHandles;
8892

cpp_utils/BLEServiceMap.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ BLEService* BLEServiceMap::getNext() {
120120
return pRet;
121121
} // getNext
122122

123+
/**
124+
* @brief Removes service from maps.
125+
* @return N/A.
126+
*/
123127
void BLEServiceMap::removeService(BLEService *service){
124-
m_handleMap->erase(serice->getHandle());
125-
m_uuidMap->erase(service);
126-
}
128+
m_handleMap.erase(service->getHandle());
129+
m_uuidMap.erase(service);
130+
} // removeService
127131

128132
#endif /* CONFIG_BT_ENABLED */

cpp_utils/WiFi.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,10 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
153153
* @param [in] ssid The network SSID of the access point to which we wish to connect.
154154
* @param [in] password The password of the access point to which we wish to connect.
155155
* @param [in] waitForConnection Block until the connection has an outcome.
156-
* @returns ESP_OK if successfully receives a SYSTEM_EVENT_STA_GOT_IP event. Otherwise returns wifi_err_reason_t - use GeneralUtils::wifiErrorToString(uint8_t errCode) to print the error.
156+
* @param [in] mode WIFI_MODE_AP for normal or WIFI_MODE_APSTA if you want to keep an Access Point running while you connect
157+
* @return ESP_OK if we are now connected and wifi_err_reason_t if not.
157158
*/
158-
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
159+
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection, wifi_mode_t mode){
159160
ESP_LOGD(LOG_TAG, ">> connectAP");
160161

161162
m_apConnectionStatus = UINT8_MAX;
@@ -172,7 +173,7 @@ uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bo
172173
::tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_STA, &ipInfo);
173174
}
174175

175-
esp_err_t errRc = ::esp_wifi_set_mode(WIFI_MODE_STA);
176+
esp_err_t errRc = ::esp_wifi_set_mode(mode);
176177
if (errRc != ESP_OK) {
177178
ESP_LOGE(LOG_TAG, "esp_wifi_set_mode: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
178179
abort();

cpp_utils/WiFi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class WiFi {
130130
void setDNSServer(int numdns, ip_addr_t ip);
131131
struct in_addr getHostByName(const std::string& hostName);
132132
struct in_addr getHostByName(const char* hostName);
133-
uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true);
133+
uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true, wifi_mode_t mode=WIFI_MODE_STA);
134134
void dump();
135135
bool isConnectedToAP();
136136
static std::string getApMac();

0 commit comments

Comments
 (0)
0