8000 Mon Apr 3 08:50:38 CDT 2017 · programmer131/esp32-snippets@a009371 · GitHub
[go: up one dir, main page]

Skip to content

Commit a009371

Browse files
author
kolban
committed
Mon Apr 3 08:50:38 CDT 2017
1 parent 2f35cf8 commit a009371

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+299
-3542
lines changed

cpp_utils/NVS.cpp

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,71 @@
88
#include <stdlib.h>
99
#include "NVS.h"
1010

11+
/**
12+
* @brief Constructor.
13+
*
14+
* @param [in] name The namespace to open for access.
15+
* @param [in] openMode
16+
*/
1117
NVS::NVS(std::string name, nvs_open_mode openMode) {
1218
m_name = name;
1319
nvs_open(name.c_str(), openMode, &m_handle);
14-
}
20+
} // NVS
1521

1622

1723
NVS::~NVS() {
1824
nvs_close(m_handle);
19-
}
25+
} // ~NVS
2026

2127

28+
/**
29+
* @brief Commit any work performed in the namespace.
30+
*/
2231
void NVS::commit() {
2332
nvs_commit(m_handle);
24-
}
33+
} // commit
2534

2635

36+
/**
37+
* @brief Erase ALL the keys in the namespace.
38+
*/
2739
void NVS::erase() {
2840
nvs_erase_all(m_handle);
29-
}
41+
} // erase
3042

3143

44+
/**
45+
* @brief Erase a specific key in the namespace.
46+
*
47+
* @param [in] key The key to erase from the namespace.
48+
*/
3249
void NVS::erase(std::string key) {
3350
nvs_erase_key(m_handle, key.c_str());
34-
}
51+
} // erase
3552

3653

54+
/**
55+
* @brief Retrieve a string value by key.
56+
*
57+
* @param [in] key The key to read from the namespace.
58+
* @param [out] result The string read from the %NVS storage.
59+
*/
3760
void NVS::get(std::string key, std::string* result) {
3861
size_t length;
3962
nvs_get_str(m_handle, key.c_str(), NULL, &length);
4063
char *data = (char *)malloc(length);
4164
nvs_get_str(m_handle, key.c_str(), data, &length);
4265
*result = std::string(data);
4366
free(data);
44-
}
67+
} // get
4568

4669

70+
/**
71+
* @brief Set the string value by key.
72+
*
73+
* @param [in] key The key to set from the namespace.
74+
* @param [in] data The value to set for the key.
75+
*/
4776
void NVS::set(std::string key, std::string data) {
4877
nvs_set_str(m_handle, key.c_str(), data.c_str());
49-
}
78+
} // set

cpp_utils/NVS.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#define COMPONENTS_CPP_UTILS_NVS_H_
1010
#include <nvs.h>
1111
#include <string>
12+
13+
/**
14+
* @brief Provide Non Volatile Storage access.
15+
*/
1216
class NVS {
1317
public:
1418
NVS(std::string name, nvs_open_mode openMode = NVS_READWRITE);

cpp_utils/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@ to create the user guides and programming references.
55

66
##BLE Functions
77
The Bluetooth BLE functions are only compiled if Bluetooth is enabled in `make menuconfig`. This is primarily because
8-
the ESP-IDF build system has chosen to only compile the underlying BLE functions if Bluetooth is enabled.
8+
the ESP-IDF build system has chosen to only compile the underlying BLE functions if Bluetooth is enabled.
9+
10+
##Building the Documentation
11+
The code is commented using the Doxygen tags. As such we can run Doxygen to generate the data. I use `doxywizard` using
12+
the `Doxyfile` located in this directory.

cpp_utils/RESTClient.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ class RESTTimings {
3333

3434
/**
3535
* @brief Encapsulate a REST client call.
36+
*
37+
* REST is the ability to make service calls using TCP and the HTTP protocols. This class provides a
38+
* REST client encapsulation. A REST client is the part of the story that makes calls to a partner
39+
* REST service provider (or server). To make a call to a REST provider we need to specify:
40+
*
41+
* * The endpoint ... i.e. where are we sending the request.
42+
* * The protocol ... i.e. plain HTTP or secure HTTPS.
43+
* * The HTTP command ... i.e. one of GET, PUT, POST etc.
44+
* * The headers to the HTTP request.
45+
* * An optional payload for command types that accept payloads.
46+
*
47+
* Here is a code example:
48+
*
49+
* @code{cpp}
50+
* #include <RESTClient.h>
51+
*
52+
* RESTClient client;
53+
* client.setURL("https://httpbin.org/post");
54+
* client.addHeader("Content-Type", "application/json");
55+
* client.post("{ \"greeting\": \"hello world!\"");
56+
* @endcode
57+
*
58+
* To use this class you **must** define the `ESP_HAVE_CURL` build definition. In your component.mk file
59+
* add:
60+
*
61+
* ```
62+
* CXXFLAGS+=-DESP_HAVE_CURL
63+
* ```
3664
*/
3765
class RESTClient {
3866
public:
@@ -41,10 +69,18 @@ class RESTClient {
4169
void addHeader(std::string name, std::string value);
4270
void get();
4371
std::string getErrorMessage();
72+
/**
73+
* @brief Get the response payload data from the last REST call.
74+
*
75+
* @return The response payload data.
76+
*/
4477
std::string getResponse() {
4578
return m_response;
4679
}
4780

81+
/**
82+
* @brief Get the timing information associated with this REST connection.
83+
*/
4884
RESTTimings *getTimings() {
4985
return m_timings;
5086
}

cpp_utils/WiFi.cpp

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ WiFi::WiFi() {
6565
*/
6666
void WiFi::addDNSServer(std::string ip) {
6767
ip_addr_t dnsserver;
68-
ESP_LOGD(tag, "Setting DNS[%d] to %s", dnsCount, ip.c_str());
68+
ESP_LOGD(tag, "Setting DNS[%d] to %s", m_dnsCount, ip.c_str());
6969
inet_pton(AF_INET, ip.c_str(), &dnsserver);
70-
::dns_setserver(dnsCount, &dnsserver);
71-
dnsCount++;
70+
::dns_setserver(m_dnsCount, &dnsserver);
71+
m_dnsCount++;
7272
} // addDNSServer
7373

7474
/**
@@ -176,11 +176,11 @@ std::vector<WiFiAPRecord> WiFi::scan() {
176176
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&apCount, list));
177177
std::vector<WiFiAPRecord> apRecords;
178178
for (auto i=0; i<apCount; i++) {
179-
WiFiAPRecord apR;
180-
F438 memcpy(apR.bssid, list[i].bssid, 6);
181-
apR.ssid = std::string((char *)list[i].ssid);
182-
apR.authMode = list[i].authmode;
183-
apRecords.push_back(apR);
179+
WiFiAPRecord wifiAPRecord;
180+
memcpy(wifiAPRecord.m_bssid, list[i].bssid, 6);
181+
wifiAPRecord.m_ssid = std::string((char *)list[i].ssid);
182+
wifiAPRecord.m_authMode = list[i].authmode;
183+
apRecords.push_back(wifiAPRecord);
184184
}
185185
free(list);
186186
return apRecords;
@@ -262,6 +262,65 @@ std::string WiFiAPRecord::toString() {
262262
auth = "<unknown>";
263263
break;
264264
}
265-
return "ssid: " + ssid + ", auth: " + auth + ", rssi: " + std::to_string(rssi);
265+
return "ssid: " + m_ssid + ", auth: " + auth + ", rssi: " + std::to_string(m_rssi);
266266
} // toString
267+
268+
MDNS::MDNS() {
269+
ESP_ERROR_CHECK(mdns_init(TCPIP_ADAPTER_IF_STA, &m_mdns_server));
270+
}
271+
272+
MDNS::~MDNS() {
273+
if (m_mdns_server != nullptr) {
274+
mdns_free(m_mdns_server);
275+
}
276+
m_mdns_server = nullptr;
277+
}
278+
279+
/**
280+
* @brief Define the service for mDNS.
281+
*
282+
* @param [in] service
283+
* @param [in] proto
284+
* @param [in] port
285+
*
286+
*/
287+
void MDNS::serviceAdd(std::string service, std::string proto, uint16_t port) {
288+
ESP_ERROR_CHECK(mdns_service_add(m_mdns_server, service.c_str(), proto.c_str(), port));
289+
} // serviceAdd
290+
291+
292+
void MDNS::serviceInstanceSet(std::string service, std::string proto, std::string instance) {
293+
ESP_ERROR_CHECK(mdns_service_instance_set(m_mdns_server, service.c_str(), proto.c_str(), instance.c_str()));
294+
} // serviceInstanceSet
295+
296+
297+
void MDNS::servicePortSet(std::string service, std::string proto, uint16_t port) {
298+
ESP_ERROR_CHECK(mdns_service_port_set(m_mdns_server, service.c_str(), proto.c_str(), port));
299+
} // servicePortSet
300+
301+
302+
void MDNS::serviceRemove(std::string service, std::string proto) {
303+
ESP_ERROR_CHECK(mdns_service_remove(m_mdns_server, service.c_str(), proto.c_str()));
304+
} // serviceRemove
305+
306+
307+
/**
308+
* @brief Set the mDNS hostname.
309+
*
310+
* @param [in] hostname The host name to set against the mDNS.
311+
*/
312+
void MDNS::setHostname(std::string hostname) {
313+
ESP_ERROR_CHECK(mdns_set_hostname(m_mdns_server,hostname.c_str()));
314+
} // setHostname
315+
316+
317+
/**
318+
* @brief Set the mDNS instance.
319+
*
320+
* @param [in] instance The instance name to set against the mDNS.
321+
*/
322+
void MDNS::setInstance(std::string instance) {
323+
ESP_ERROR_CHECK(mdns_set_instance(m_mdns_server, instance.c_str()));
324+
} // setInstance
325+
267326
#endif // CONFIG_WIFI_ENABLED

cpp_utils/WiFi.h

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,47 @@
1111
#if defined(CONFIG_WIFI_ENABLED)
1212
#include <string>
1313
#include <vector>
14+
#include <mdns.h>
1415
#include "WiFiEventHandler.h"
1516

16-
17+
/**
18+
* @brief Manage mDNS server.
19+
*/
20+
class MDNS {
21+
public:
22+
MDNS();
23+
~MDNS();
24+
void serviceAdd(std::string service, std::string proto, uint16_t port);
25+
void serviceInstanceSet(std::string service, std::string proto, std::string instance);
26+
void servicePortSet(std::string service, std::string proto, uint16_t port);
27+
void serviceRemove(std::string service, std::string proto);
28+
void setHostname(std::string hostname);
29+
void setInstance(std::string instance);
30+
private:
31+
mdns_server_t *m_mdns_server = nullptr;
32+
};
1733

1834
class WiFiAPRecord {
1935
public:
2036
friend class WiFi;
2137
wifi_auth_mode_t getAuthMode() {
22-
return authMode;
38+
return m_authMode;
2339
}
2440

2541
int8_t getRSSI() {
26-
return rssi;
42+
return m_rssi;
2743
}
2844

2945
std::string getSSID() {
30-
return ssid;
46+
return m_ssid;
3147
}
3248

3349
std::string toString();
3450
private:
35-
uint8_t bssid[6];
36-
int8_t rssi;
37-
std::string ssid;
38-
wifi_auth_mode_t authMode;
51+
uint8_t m_bssid[6];
52+
int8_t m_rssi;
53+
std::string m_ssid;
54+
wifi_auth_mode_t m_authMode;
3955
};
4056
/**
4157
* @brief %WiFi driver.
@@ -44,6 +60,9 @@ class WiFiAPRecord {
4460
*
4561
* Here is an example fragment that illustrates connecting to an access point.
4662
* @code{.cpp}
63+
* #include <WiFi.h>
64+
* #include <WiFiEventHandler.h>
65+
*
4766
* class TargetWiFiEventHandler: public WiFiEventHandler {
4867
* esp_err_t staGotIp(system_event_sta_got_ip_t event_sta_got_ip) {
4968
* ESP_LOGD(tag, "MyWiFiEventHandler(Class): staGotIp");
@@ -54,7 +73,7 @@ class WiFiAPRecord {
5473
*
5574
* WiFi wifi;
5675
*
57-
* MyWiFiEventHandler *eventHandler = new MyWiFiEventHandler();
76+
* TargetWiFiEventHandler *eventHandler = new TargetWiFiEventHandler();
5877
* wifi.setWifiEventHandler(eventHandler);
5978
* wifi.connectAP("myssid", "mypassword");
6079
* @endcode
@@ -86,8 +105,9 @@ class WiFi {
86105
this->wifiEventHandler = wifiEventHandler;
87106
}
88107
private:
89-
int dnsCount=0;
90-
char *dnsServer = nullptr;
108+
int m_dnsCount=0;
109+
//char *m_dnsServer = nullptr;
110+
91111
};
92112

93113
#endif // CONFIG_WIFI_ENABLED

cpp_utils/latex/Makefile

Lines changed: 0 additions & 21 deletions
This file was deleted.

cpp_utils/latex/annotated.tex

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)
0