10000 #152 · norfanos/esp32-snippets@b600ac4 · GitHub
[go: up one dir, main page]

Skip to content

Commit b600ac4

Browse files
committed
1 parent 6153b32 commit b600ac4

File tree

7 files changed

+197
-35
lines changed

7 files changed

+197
-35
lines changed

cpp_utils/GeneralUtils.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,6 @@ static const char kBase64Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2424
"abcdefghijklmnopqrstuvwxyz"
2525
"0123456789+/";
2626

27-
GeneralUtils::GeneralUtils() {
28-
// TODO Auto-generated constructor stub
29-
30-
}
31-
32-
GeneralUtils::~GeneralUtils() {
33-
// TODO Auto-generated destructor stub
34-
}
35-
3627
static int base64EncodedLength(size_t length) {
3728
return (length + 2 - ((length + 2) % 3)) / 3 * 4;
3829
} // base64EncodedLength
@@ -272,6 +263,7 @@ void GeneralUtils::hexDump(uint8_t* pData, uint32_t length) {
272263
}
273264
*/
274265

266+
275267
/**
276268
* @brief Dump a representation of binary data to the console.
277269
*
@@ -315,6 +307,7 @@ void GeneralUtils::hexDump(const uint8_t* pData, uint32_t length) {
315307
}
316308
} // hexDump
317309

310+
318311
/**
319312
* @brief Convert an IP address to string.
320313
* @param ip The 4 byte IP address.
@@ -404,6 +397,7 @@ const char* GeneralUtils::errorToString(esp_err_t errCode) {
404397
return "Unknown ESP_ERR error";
405398
} // errorToString
406399

400+
407401
/**
408402
* @brief Restart the ESP32.
409403
*/

cpp_utils/GeneralUtils.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616
*/
1717
class GeneralUtils {
1818
public:
19-
GeneralUtils();
20-
virtual ~GeneralUtils();
21-
static bool base64Encode(const std::string &in, std::string *out);
22-
static bool base64Decode(const std::string &in, std::string *out);
23-
static bool endsWith(std::string str, char c);
24-
static const char *errorToString(esp_err_t errCode);
25-
static void hexDump(const uint8_t *pData, uint32_t length);
26-
static std::string ipToString(uint8_t *ip);
27-
static void restart();
19+
static bool base64Decode(const std::string& in, std::string* out);
20+
static bool base64Encode(const std::string& in, std::string* out);
21+
static bool endsWith(std::string str, char c);
22+
static const char* errorToString(esp_err_t errCode);
23+
static void hexDump(const uint8_t* pData, uint32_t length);
24+
static std::string ipToString(uint8_t* ip);
25+
static void restart();
2826
};
2927

3028
#endif /* COMPONENTS_CPP_UTILS_GENERALUTILS_H_ */

cpp_utils/HttpServer.cpp

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <esp_log.h>
1313
#include "HttpRequest.h"
1414
#include "HttpResponse.h"
15+
#include "Memory.h"
1516
#include "FileSystem.h"
1617
#include "WebSocket.h"
1718
#include "GeneralUtils.h"
@@ -232,14 +233,44 @@ class HttpServerTask: public Task {
232233
*/
233234
void HttpServer::addPathHandler(
234235
std::string method,
235-
std::string pathExpr,
236+
std::regex* pathExpr,
236237
void (*handler)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse)) {
237238

238239
// We are maintaining a C++ vector of PathHandler objects. We add a new entry into that vector.
239240
m_pathHandlers.push_back(PathHandler(method, pathExpr, handler));
240241
} // addPathHandler
241242

242243

244+
/**
245+
* @brief Register a handler for a path.
246+
*
247+
* When a browser request arrives, the request will contain a method (GET, POST, etc) and a path
248+
* to be accessed. Using this method we can register a regular expression and, if the incoming method
249+
* and path match the expression, the corresponding handler will be called.
250+
*
251+
* Example:
252+
* @code{.cpp}
253+
* static void handle_REST_WiFi(WebServer::HttpRequest *pRequest, WebServer::HttpResponse *pResponse) {
254+
* ...
255+
* }
256+
*
257+
* webServer.addPathHandler("GET", "/ESP32/WiFi", handle_REST_WiFi);
258+
* @endcode
259+
*
260+
* @param [in] method The method being used for access ("GET", "POST" etc).
261+
* @param [in] path The plain path being accessed.
262+
* @param [in] handler The callback function to be invoked when a request arrives.
263+
*/
264+
void HttpServer::addPathHandler(
265+
std::string method,
266+
std::string path,
267+
void (*handler)(HttpRequest *pHttpRequest, HttpResponse *pHttpResponse)) {
268+
269+
// We are maintaining a C++ vector of PathHandler objects. We add a new entry into that vector.
270+
m_pathHandlers.push_back(PathHandler(method, path, handler));
271+
} // addPathHandler
272+
273+
243274
/**
244275
* @brief Get the port number on which the HTTP Server is listening.
245276
* @return The port number on which the HTTP server is listening.
@@ -333,15 +364,36 @@ void HttpServer::stop() {
333364
* @param [in] pathPattern The path pattern to be matched.
334365
* @param [in] webServerRequestHandler The request handler to be called.
335366
*/
336-
PathHandler::PathHandler(std::string method, std::string pathPattern,
367+
PathHandler::PathHandler(std::string method, std::regex *pRegex,
368+
void (*pWebServerRequestHandler)
369+
(
370+
HttpRequest* pHttpRequest,
371+
HttpResponse* pHttpResponse)
372+
) {
373+
m_method = method; // Save the method we are looking for.
374+
m_pRegex = pRegex; // Save the Regex
375+
m_textPattern = "<Regex>"; // The plain text of the regex pattern.
376+
m_isRegex = true;
377+
m_pRequestHandler = pWebServerRequestHandler; // The handler to be invoked if the pattern matches.
378+
} // PathHandler
379+
380+
381+
/**
382+
* @brief Construct an instance of a PathHandler.
383+
*
384+
* @param [in] method The method to be matched.
385+
* @param [in] pathPattern The path to be matched. Must be an exact match.
386+
* @param [in] webServerRequestHandler The request handler to be called.
387+
*/
388+
PathHandler::PathHandler(std::string method, std::string matchPath,
337389
void (*pWebServerRequestHandler)
338390
(
339391
HttpRequest* pHttpRequest,
340392
HttpResponse* pHttpResponse)
341393
) {
342394
m_method = method; // Save the method we are looking for.
343-
m_pattern = std::regex(pathPattern); // Create the Regex pattern.
344-
m_textPattern = pathPattern; // The plain text of the regex pattern.
395+
m_textPattern = matchPath;
396+
m_isRegex = false;
345397
m_pRequestHandler = pWebServerRequestHandler; // The handler to be invoked if the pattern matches.
346398
} // PathHandler
347399

@@ -354,11 +406,16 @@ PathHandler::PathHandler(std::string method, std::string pathPattern,
354406
* @return True if the path matches.
355407
*/
356408
bool PathHandler::match(std::string method, std::string path) {
357-
ESP_LOGD("PathHandler", "matching: %s with %s", m_textPattern.c_str(), path.c_str());
358409
if (method != m_method) {
359410
return false;
360411
}
361-
return std::regex_search(path, m_pattern);
412+
if (m_isRegex) {
413+
ESP_LOGD("PathHandler", "regex matching: %s with %s", m_textPattern.c_str(), path.c_str());
414+
415+
return std::regex_search(path, *m_pRegex);
416+
}
417+
ESP_LOGD("PathHandler", "plain matching: %s with %s", m_textPattern.c_str(), path.c_str());
418+
return m_textPattern.compare(0, m_textPattern.length(), path) ==0;
362419
} // match
363420

364421

cpp_utils/HttpServer.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,20 @@ class PathHandler {
3434
HttpRequest* pHttpRequest,
3535
HttpResponse* pHttpResponse)
3636
);
37+
PathHandler(
38+
std::string method, // The method in the request to be matched.
39+
std::regex* pathPattern, // The pattern in the request to be matched (regex)
40+
void (*pWebServerRequestHandler) // The handler function to be invoked upon a match.
41+
(
42+
HttpRequest* pHttpRequest,
43+
HttpResponse* pHttpResponse)
44+
);
3745
bool match(std::string method, std::string path); // Does the request method and pattern match?
3846
void invokePathHandler(HttpRequest* request, HttpResponse* response);
3947
private:
4048
std::string m_method;
41-
std::regex m_pattern;
49+
std::regex* m_pRegex;
50+
bool m_isRegex;
4251
std::string m_textPattern;
4352
void (*m_pRequestHandler)(HttpRequest* pHttpRequest, HttpResponse* pHttpResponse);
4453
}; // PathHandler
@@ -57,6 +66,14 @@ class HttpServer {
5766
HttpRequest* pHttpRequest,
5867
HttpResponse* pHttpResponse)
5968
);
69+
void addPathHandler(
70+
std::string method,
71+
std::regex* pRegex,
72+
void (*webServerRequestHandler)
73+
(
74+
HttpRequest* pHttpRequest,
75+
HttpResponse* pHttpResponse)
76+
);
6077
uint16_t getPort(); // Get the port on which the Http server is listening.
6178
std::string getRootPath(); // Get the root of the file system path.
6279
bool getSSL(); // Are we using SSL?

cpp_utils/Memory.cpp

Lines changed: 86 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,95 @@
44
* Created on: Oct 24, 2017
55
* Author: kolban
66
*/
7-
7+
#include "sdkconfig.h"
8+
#ifdef CONFIG_HEAP_TRACING
89
#include "Memory.h"
910

10-
Memory::Memory() {
11-
// TODO Auto-generated constructor stub
12-
11+
#include <stdlib.h>
12+
#include "GeneralUtils.h"
13+
extern "C" {
14+
#include <esp_heap_trace.h>
15+
#include <esp_heap_caps.h>
1316
}
17+
#include <esp_log.h>
18+
19+
static const char* LOG_TAG = "Memory";
20+
21+
heap_trace_record_t* Memory::m_pRecords = nullptr;
22+
size_t Memory::m_lastHeapSize = 0;
23+
24+
/**
25+
* @brief Dump the trace records from the heap.
26+
*/
27+
void Memory::dump() {
28+
::heap_trace_dump();
29+
} // dump
30+
1431

15-
Memory::~Memory() {
16-
// TODO Auto-generated destructor stub
32+
/* STATIC */ void Memory::dumpHeapChange(std::string tag) {
33+
size_t currentUsage = heap_caps_get_free_size(MALLOC_CAP_8BIT);
34+
int diff = currentUsage - m_lastHeapSize;
35+
ESP_LOGD(LOG_TAG, "%s: Heap changed by %d bytes (%d to %d)", tag.c_str(), diff, m_lastHeapSize, currentUsage);
36+
m_lastHeapSize = currentUsage;
1737
}
1838

39+
/**
40+
* @brief Initialize heap recording.
41+
* @param [in] recordCount The maximum number of records to be recorded.
42+
*/
43+
void Memory::init(uint32_t recordCount) {
44+
assert(recordCount > 0);
45+
if (m_pRecords != nullptr) {
46+
ESP_LOGE(LOG_TAG, "Already initialized");
47+
return;
48+
}
49+
50+
m_pRecords = new heap_trace_record_t[recordCount]; // Allocate the maximum number of records to be recorded.
51+
if (m_pRecords == nullptr) {
52+
ESP_LOGE(LOG_TAG, "Unable to create %d heap records", recordCount);
53+
}
54+
55+
esp_err_t errRc = ::heap_trace_init_standalone(m_pRecords, recordCount);
56+
if (errRc != ESP_OK) {
57+
ESP_LOGE(LOG_TAG, "heap_trace_init_standalone: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
58+
abort();
59+
}
60+
} // init
61+
62+
63+
void Memory::resumeTrace() {
64+
esp_err_t errRc = ::heap_trace_resume();
65+
if (errRc != ESP_OK) {
66+
ESP_LOGE(LOG_TAG, "heap_trace_resume: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
67+
abort();
68+
}
69+
} // resumeTrace
70+
71+
72+
void Memory::startTraceAll() {
73+
esp_err_t errRc = ::heap_trace_start(HEAP_TRACE_ALL);
74+
if (errRc != ESP_OK) {
75+
ESP_LOGE(LOG_TAG, "heap_trace_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
76+
abort();
77+
}
78+
} // startTraceAll
79+
80+
81+
void Memory::startTraceLeaks() {
82+
esp_err_t errRc = ::heap_trace_start(HEAP_TRACE_LEAKS);
83+
if (errRc != ESP_OK) {
84+
ESP_LOGE(LOG_TAG, "heap_trace_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
85+
abort();
86+
}
87+
} // startTraceLeaks
88+
89+
90+
void Memory::stopTrace() {
91+
esp_err_t errRc = ::heap_trace_stop();
92+
if (errRc != ESP_OK) {
93+
ESP_LOGE(LOG_TAG, "heap_trace_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
94+
abort();
95+
}
96+
} // stopTrace
97+
98+
#endif

cpp_utils/Memory.h

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,27 @@
77

88
#ifndef COMPONENTS_CPP_UTILS_MEMORY_H_
99
#define COMPONENTS_CPP_UTILS_MEMORY_H_
10+
#include "sdkconfig.h"
11+
#ifdef CONFIG_HEAP_TRACING
12+
#include <string>
13+
extern "C" {
14+
#include <esp_heap_trace.h>
15+
}
1016

1117
class Memory {
1218
public:
13-
Memory();
14-
virtual ~Memory();
15-
};
19+
static void dump();
20+
static void dumpHeapChange(std::string tag);
21+
static void init(uint32_t recordCount);
22+
static void resumeTrace();
23+
static void startTraceAll();
24+
static void startTraceLeaks();
25+
static void stopTrace();
26+
1627

28+
private:
29+
static heap_trace_record_t* m_pRecords;
30+
static size_t m_lastHeapSize; // Size of last heap recorded.
31+
};
32+
#endif
1733
#endif /* COMPONENTS_CPP_UTILS_MEMORY_H_ */

cpp_utils/WiFi.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ void WiFi::dump() {
242242
tcpip_adapter_ip_info_t WiFi::getApIpInfo() {
243243
//init();
244244
tcpip_adapter_ip_info_t ipInfo;
245-
tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ipInfo);
245+
::tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_AP, &ipInfo);
246246
return ipInfo;
247247
} // getApIpInfo
248248

0 commit comments

Comments
 (0)
0