8000 Merge branch 'master' of https://github.com/chegewara/esp32-snippets · breadchris/esp32-snippets@cebbc6c · GitHub
[go: up one dir, main page]

Skip to content

Commit cebbc6c

Browse files
committed
2 parents 5ae9b98 + 756e8d8 commit cebbc6c

File tree

23 files changed

+1369
-30
lines changed

23 files changed

+1369
-30
lines changed

cpp_utils/BLEAdvertising.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@ class BLEAdvertisementData {
2929
void setPartialServices(BLEUUID uuid);
3030
void setServiceData(BLEUUID uuid, std::string data);
3131
void setShortName(std::string name);
32+
void addData(std::string data); // Add data to the payload.
33+
std::string getPayload(); // Retrieve the current advert payload.
3234

3335
private:
3436
friend class BLEAdvertising;
3537
std::string m_payload; // The payload of the advertisement.
36-
37-
void addData(std::string data); // Add data to the payload.
38-
std::string getPayload(); // Retrieve the current advert payload.
3938
}; // BLEAdvertisementData
4039

4140

cpp_utils/BLECharacteristic.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,43 @@ void BLECharacteristic::setValue(std::string value) {
683683
setValue((uint8_t*)(value.data()), value.length());
684684
} // setValue
685685

686+
void BLECharacteristic::setValue(uint16_t& data16) {
687+
uint8_t temp[2];
688+
temp[0]=data16;
689+
temp[1]=data16>>8;
690+
setValue(temp, 2);
691+
} // setValue
692+
693+
void BLECharacteristic::setValue(uint32_t& data32) {
694+
uint8_t temp[4];
695+
temp[0]=data32;
696+
temp[1]=data32>>8;
697+
temp[2]=data32>>16;
698+
temp[3]=data32>>24;
699+
setValue(temp, 4);
700+
} // setValue
701+
702+
void BLECharacteristic::setValue(int& data32) {
703+
uint8_t temp[4];
704+
temp[0]=data32;
705+
temp[1]=data32>>8;
706+
temp[2]=data32>>16;
707+
temp[3]=data32>>24;
708+
setValue(temp, 4);
709+
} // setValue
710+
711+
void BLECharacteristic::setValue(float& data32) {
712+
uint8_t temp[4];
713+
*((float *)temp) = data32;
714+
setValue(temp, 4);
715+
} // setValue
716+
717+
void BLECharacteristic::setValue(double& data64) {
718+
uint8_t temp[8];
719+
*((double *)temp) = data64;
720+
setValue(temp, 8);
721+
} // setValue
722+
686723

687724
/**
688725
* @brief Set the Write No Response property value.

cpp_utils/BLECharacteristic.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,11 @@ class BLECharacteristic {
7575
void setReadProperty(bool value);
7676
void setValue(uint8_t* data, size_t size);
7777
void setValue(std::string value);
78+
void setValue(uint16_t& data16);
79+
void setValue(uint32_t& data32);
80+
void setValue(int& data32);
81+
void setValue(float& data32);
82+
void setValue(double& data64);
7883
void setWriteProperty(bool value);
7984
void setWriteNoResponseProperty(bool value);
8085
std::string toString();

cpp_utils/BLEScan.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ void BLEScan::handleGAPEvent(
7474
// asked to stop.
7575
case ESP_GAP_SEARCH_INQ_CMPL_EVT: {
7676
m_stopped = true;
77+
if (m_scanCompleteCB != nullptr) {
78+
m_scanCompleteCB(m_scanResults);
79+
}
7780
m_semaphoreScanEnd.give();
7881
break;
7982
} // ESP_GAP_SEARCH_INQ_CMPL_EVT
@@ -186,10 +189,14 @@ void BLEScan::setWindow(uint16_t windowMSecs) {
186189
/**
187190
* @brief Start scanning.
188191
* @param [in] duration The duration in seconds for which to scan.
189-
* @return N/A.
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.
190196
*/
191-
BLEScanResults BLEScan::start(uint32_t duration) {
197+
BLEScanResults BLEScan::start(uint32_t duration, void (*scanCompleteCB)(BLEScanResults)) {
192198
ESP_LOGD(LOG_TAG, ">> start(duration=%d)", duration);
199+
m_scanCompleteCB = scanCompleteCB; // Save the callback to be invoked when the scan completes.
193200

194201
m_semaphoreScanEnd.take(std::string("start"));
195202

@@ -213,7 +220,9 @@ BLEScanResults BLEScan::start(uint32_t duration) {
213220

214221
m_stopped = false;
215222

216-
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
223+
if (m_scanCompleteCB == nullptr) {
224+
m_semaphoreScanEnd.wait("start"); // Wait for the semaphore to release.
225+
}
217226

218227
ESP_LOGD(LOG_TAG, "<< start()");
219228
return m_scanResults;

cpp_utils/BLEScan.h

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

5959
private:
@@ -71,6 +71,7 @@ class BLEScan {
7171
FreeRTOS::Semaphore m_semaphoreScanEnd = FreeRTOS::Semaphore("ScanEnd");
7272
BLEScanResults m_scanResults;
7373
bool m_wantDuplicates;
74+
void (*m_scanCompleteCB)(BLEScanResults scanResults);
7475
}; // BLEScan
7576

7677
#endif /* CONFIG_BT_ENABLED */

cpp_utils/GeneralUtils.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,82 @@ const char* GeneralUtils::errorToString(esp_err_t errCode) {
434434
return "Unknown ESP_ERR error";
435435
} // errorToString
436436

437+
/**
438+
* @brief Convert a wifi_err_reason_t code to a string.
439+
* @param [in] errCode The errCode to be converted.
440+
* @return A string representation of the error code.
441+
*
442+
* @note: wifi_err_reason_t values as of April 2018 are: (1-24, 200-204) and are defined in ~/esp-idf/components/esp32/include/esp_wifi_types.h.
443+
*/
444+
const char* GeneralUtils::wifiErrorToString(uint8_t errCode) {
445+
if (errCode == ESP_OK)
446+
return "ESP_OK (received SYSTEM_EVENT_STA_GOT_IP event)";
447+
if (errCode == UINT8_MAX)
448+
return "Not Connected (default value)";
449+
450+
switch((wifi_err_reason_t) errCode) {
451+
case WIFI_REASON_UNSPECIFIED:
452+
return "WIFI_REASON_UNSPECIFIED";
453+
case WIFI_REASON_AUTH_EXPIRE:
454+
return "WIFI_REASON_AUTH_EXPIRE";
455+
case WIFI_REASON_AUTH_LEAVE:
456+
return "WIFI_REASON_AUTH_LEAVE";
457+
case WIFI_REASON_ASSOC_EXPIRE:
458+
return "WIFI_REASON_ASSOC_EXPIRE";
459+
case WIFI_REASON_ASSOC_TOOMANY:
460+
return "WIFI_REASON_ASSOC_TOOMANY";
461+
case WIFI_REASON_NOT_AUTHED:
462+
return "WIFI_REASON_NOT_AUTHED";
463+
case WIFI_REASON_NOT_ASSOCED:
464+
return "WIFI_REASON_NOT_ASSOCED";
465+
case WIFI_REASON_ASSOC_LEAVE:
466+
return "WIFI_REASON_ASSOC_LEAVE";
467+
case WIFI_REASON_ASSOC_NOT_AUTHED:
468+
return "WIFI_REASON_ASSOC_NOT_AUTHED";
469+
case WIFI_REASON_DISASSOC_PWRCAP_BAD:
470+
return "WIFI_REASON_DISASSOC_PWRCAP_BAD";
471+
case WIFI_REASON_DISASSOC_SUPCHAN_BAD:
472+
return "WIFI_REASON_DISASSOC_SUPCHAN_BAD";
473+
case WIFI_REASON_IE_INVALID:
474+
return "WIFI_REASON_IE_INVALID";
475+
case WIFI_REASON_MIC_FAILURE:
476+
return "WIFI_REASON_MIC_FAILURE";
477+
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
478+
return "WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT";
479+
case WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT:
480+
return "WIFI_REASON_GROUP_KEY_UPDATE_TIMEOUT";
481+
case WIFI_REASON_IE_IN_4WAY_DIFFERS:
482+
return "WIFI_REASON_IE_IN_4WAY_DIFFERS";
483+
case WIFI_REASON_GROUP_CIPHER_INVALID:
484+
return "WIFI_REASON_GROUP_CIPHER_INVALID";
485+
case WIFI_REASON_PAIRWISE_CIPHER_INVALID:
486+
return "WIFI_REASON_PAIRWISE_CIPHER_INVALID";
487+
case WIFI_REASON_AKMP_INVALID:
488+
return "WIFI_REASON_AKMP_INVALID";
489+
case WIFI_REASON_UNSUPP_RSN_IE_VERSION:
490+
return "WIFI_REASON_UNSUPP_RSN_IE_VERSION";
491+
case WIFI_REASON_INVALID_RSN_IE_CAP:
492+
return "WIFI_REASON_INVALID_RSN_IE_CAP";
493+
case WIFI_REASON_802_1X_AUTH_FAILED:
494+
return "WIFI_REASON_802_1X_AUTH_FAILED";
495+
case WIFI_REASON_CIPHER_SUITE_REJECTED:
496+
return "WIFI_REASON_CIPHER_SUITE_REJECTED";
497+
498+
case WIFI_REASON_BEACON_TIMEOUT:
499+
return "WIFI_REASON_BEACON_TIMEOUT";
500+
case WIFI_REASON_NO_AP_FOUND:
501+
return "WIFI_REASON_NO_AP_FOUND";
502+
case WIFI_REASON_AUTH_FAIL:
503+
return "WIFI_REASON_AUTH_FAIL";
504+
case WIFI_REASON_ASSOC_FAIL:
505+
return "WIFI_REASON_ASSOC_FAIL";
506+
case WIFI_REASON_HANDSHAKE_TIMEOUT:
507+
return "WIFI_REASON_HANDSHAKE_TIMEOUT";
508+
}
509+
return "Unknown ESP_ERR error";
510+
} // wifiErrorToString
511+
512+
437513

438514
/**
439515
* @brief Convert a string to lower case.

cpp_utils/GeneralUtils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class GeneralUtils {
2323
static void dumpInfo();
2424
static bool endsWith(std::string str, char c);
2525
static const char* errorToString(esp_err_t errCode);
26+
static const char* wifiErrorToString(uint8_t value);
2627
static void hexDump(const uint8_t* pData, uint32_t length);
2728
static std::string ipToString(uint8_t* ip);
2829
static std::vector<std::string> split(std::string source, char delimiter);

cpp_utils/HttpParser.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,9 @@ void HttpParser::parse(Socket s) {
220220
} else {
221221
uint8_t data[512];
222222
int rc = s.receive(data, sizeof(data));
223-
m_body = std::string((char *)data, rc);
223+
if (rc > 0) {
224+
m_body = std::string((char *)data, rc);
225+
}
224226
}
225227
ESP_LOGD(LOG_TAG, "<< parse: Size of body: %d", m_body.length());
226228
} // parse

cpp_utils/RESTClient.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,16 @@ RESTClient::~RESTClient() {
3636
/**
3737
* @brief Perform an HTTP GET request.
3838
*/
39-
void RESTClient::get() {
39+
long RESTClient::get() {
40+
long response_code; // Added return response_code 2018_4_12
4041
prepForCall();
4142
::curl_easy_setopt(m_curlHandle, CURLOPT_HTTPGET, 1);
4243
int rc = ::curl_easy_perform(m_curlHandle);
4344
if (rc != CURLE_OK) {
4445
ESP_LOGE(tag, "get(): %s", getErrorMessage().c_str());
4546
}
47+
curl_easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &response_code); // Added return response_code 2018_4_12
48+
return response_code; // Added return response_code 2018_4_12
4649
} // get
4750

4851

@@ -52,13 +55,16 @@ void RESTClient::get() {
5255
* @param [in] body The body of the payload to send with the post request.
5356
*
5457
*/
55-
void RESTClient::post(std::string body) {
58+
long RESTClient::post(std::string body) {
59+
long response_code; // Added return response_code 2018_4_12
5660
prepForCall();
5761
::curl_easy_setopt(m_curlHandle, CURLOPT_POSTFIELDS, body.c_str());
5862
int rc = ::curl_easy_perform(m_curlHandle);
5963
if (rc != CURLE_OK) {
6064
ESP_LOGE(tag, "post(): %s", getErrorMessage().c_str());
6165
}
66+
curl_easy_getinfo(m_curlHandle, CURLINFO_RESPONSE_CODE, &response_code);// Added return response_code 2018_4_12
67+
return response_code;// Added return response_code 2018_4_12
6268
} // post
6369

6470

cpp_utils/RESTClient.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class RESTClient {
6868
RESTClient();
6969
virtual ~RESTClient();
7070
void addHeader(std::string name, std::string value);
71-
void get();
71+
long get(); // Added return response_code 2018_4_12
7272
std::string getErrorMessage();
7373
/**
7474
* @brief Get the response payload data from the last REST call.
@@ -86,7 +86,7 @@ class RESTClient {
8686
return m_timings;
8787
}
8888

89-
void post(std::string body);
89+
long post(std::string body); // Added return response_code 2018_4_12
9090

9191
/**
9292
* @brief Set the URL for the target.

cpp_utils/WiFi.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ WiFi::WiFi()
5555
m_eventLoopStarted = false;
5656
m_initCalled = false;
5757
//m_pWifiEventHandler = new WiFiEventHandler();
58-
m_apConnected = false; // Are we connected to an access point?
58+
m_apConnectionStatus = UINT8_MAX; // Are we connected to an access point?
5959
} // WiFi
6060

6161

@@ -153,12 +153,12 @@ 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-
* @return N/A.
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.
157157
*/
158-
bool WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
158+
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
159159
ESP_LOGD(LOG_TAG, ">> connectAP");
160160

161-
m_apConnected = false;
161+
m_apConnectionStatus = UINT8_MAX;
162162
init();
163163

164164
if (ip != 0 && gw != 0 && netmask != 0) {
@@ -206,7 +206,7 @@ bool WiFi::connectAP(const std::string& ssid, const std::string& password, bool
206206
m_connectFinished.give();
207207

208208
ESP_LOGD(LOG_TAG, "<< connectAP");
209-
return m_apConnected; // Return true if we are now connected and false if not.
209+
return m_apConnectionStatus; // Return ESP_OK if we are now connected and wifi_err_reason_t if not.
210210
} // connectAP
211211

212212

@@ -228,7 +228,7 @@ void WiFi::dump() {
228228
* @brief Returns whether wifi is connected to an access point
229229
*/
230230
bool WiFi::isConnectedToAP() {
231-
return m_apConnected;
231+
return m_apConnectionStatus;
232232
} // isConnected
233233

234234

@@ -255,10 +255,11 @@ bool WiFi::isConnectedToAP() {
255255
// If the event we received indicates that we now have an IP address or that a connection was disconnected then unlock the mutex that
256256
// indicates we are waiting for a connection complete.
257257
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP || event->event_id == SYSTEM_EVENT_STA_DISCONNECTED) {
258-
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { // If we connected and have an IP, change the flag.
259-
pWiFi->m_apConnected = true;
258+
259+
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { // If we connected and have an IP, change the status to ESP_OK. Otherwise, change it to the reason code.
260+
pWiFi->m_apConnectionStatus = ESP_OK;
260261
} else {
261-
pWiFi->m_apConnected = false;
262+
pWiFi->m_apConnectionStatus = event->event_info.disconnected.reason;
262263
}
263264
pWiFi->m_connectFinished.give();
264265
}

cpp_utils/WiFi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class WiFi {
116116
uint8_t m_dnsCount=0;
117117
bool m_eventLoopStarted;
118118
bool m_initCalled;
119-
bool m_apConnected; // Are we connected to an access point?
119+
uint8_t m_apConnectionStatus; // ESP_OK = we are connected to an access point. Otherwise receives wifi_err_reason_t.
120120
FreeRTOS::Semaphore m_connectFinished = FreeRTOS::Semaphore("ConnectFinished");
121121

122122
public:
@@ -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-
bool 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);
134134
void dump();
135135
bool isConnectedToAP();
136136
static std::string getApMac();

cpp_utils/tests/BLETests/Arduino/BLE_notify/BLE_notify.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
#include <BLEUtils.h>
2424
#include <BLE2902.h>
2525

26-
BLEServer *pServer = NULL;
26+
BLEServer* pServer = NULL;
27+
BLECharacteristic* pCharacteristic = NULL;
2728
bool deviceConnected = false;
2829
bool oldDeviceConnected = false;
2930
uint8_t value = 0;
@@ -61,7 +62,7 @@ void setup() {
6162
BLEService *pService = pServer->createService(SERVICE_UUID);
6263

6364
// Create a BLE Characteristic
64-
BLECharacteristic * pCharacteristic = pService->createCharacteristic(
65+
pCharacteristic = pService->createCharacteristic(
6566
CHARACTERISTIC_UUID,
6667
BLECharacteristic::PROPERTY_READ |
6768
BLECharacteristic::PROPERTY_WRITE |

0 commit comments

Comments
 (0)
0