Skip to content

Commit 5fafd44

Browse files
committed
base line for nkolban#226
1 parent 17fbacc commit 5fafd44

11 files changed

+510
-86
lines changed

cpp_utils/ArduinoBLE.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Arduino BLE Support
2-
As part of this Github project, we provide libraries for Bluetooth Low Energy (BLE) for the ESP32 Arduino environment. Support for this capability is still in the process of being cooked (as of September 2017). As such you should not build product based on these functions as changes to the API and implementation are extremely likely over the next couple of months.
2+
As part of this Github project, we provide libraries for Bluetooth Low Energy (BLE) for the ESP32 Arduino environment. Support for this capability is still in the process of being cooked (as of November 2017). As such you should not build product based on these functions as changes to the API and implementation are extremely likely over the next couple of months.
33

44
That said, we now have the ability to produce a driver you can use for testing. This will give you early access to the function while give those who choose to build and maintain the code early feedback on what works, what doesn't and what can be improved from a usage perspective.
55

66
When complete, the BLE support for ESP32 Arduino will be available as a single ZIP file. The file will be called **ESP32_BLE.zip**. It is this file that will be able to be imported into the Arduino IDE from the `Sketch -> Include Library -> Add .ZIP library`. When initial development of the library has been completed, this ZIP will be placed under some form of release control so that an ordinary Arduino IDE user can simply download this as a unit and install.
77

8-
A build of the BLE support for Arduino can be found through the Arduino IDE. Visit Sketch -> Include Library -> Manage Libraries. In the library filter, enter "esp32 ble arduino". The search will narrow and you should see "ESP32 BLE Arduino" available for installation or upgrade.
8+
Update: As of 2017-11, the BLE support has been included with the Arduino ESP32 base package.
9+
10+
A build of the BLE support for Arduino can be found through the Arduino IDE. Visit `Sketch -> Include Library -> Manage Libraries`. In the library filter, enter "esp32 ble arduino". The search will narrow and you should see "ESP32 BLE Arduino" available for installation or upgrade.
911

1012

1113

@@ -32,7 +34,7 @@ From October 2017 onwards, a build of the BLE libraries is supplied with the Ard
3234
4. Extract the `ESP32_BLE.zip` file there
3335

3436
## Switching on debugging
35-
The BLE support contains extensive internal diagnostics which can be switched on through the "Tools > Core Debug Level" setting:
37+
The BLE support contains extensive internal diagnostics which can be switched on through the `Tools > Core Debug Level` setting:
3638

3739
![](../../Documentation/images/arduino_debug.png)
3840

cpp_utils/GeneralUtils.cpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,4 @@ const char* GeneralUtils::errorToString(esp_err_t errCode) {
398398
} // errorToString
399399

400400

401-
/**
402-
* @brief Restart the ESP32.
403-
*/
404-
void GeneralUtils::restart() {
405-
esp_restart();
406-
} // restart
401+

cpp_utils/GeneralUtils.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class GeneralUtils {
2222
static const char* errorToString(esp_err_t errCode);
2323
static void hexDump(const uint8_t* pData, uint32_t length);
2424
static std::string ipToString(uint8_t* ip);
25-
static void restart();
25+
2626
};
2727

2828
#endif /* COMPONENTS_CPP_UTILS_GENERALUTILS_H_ */

cpp_utils/HttpServer.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "FileSystem.h"
1616
#include "WebSocket.h"
1717
#include "GeneralUtils.h"
18+
#include "Memory.h"
1819
static const char* LOG_TAG = "HttpServer";
1920

2021
#undef close
@@ -65,9 +66,9 @@ static void listDirectory(std::string path, HttpResponse& response) {
6566
* Constructor for HTTP Server
6667
*/
6768
HttpServer::HttpServer() {
68-
m_portNumber = 80; // The default port number.
69-
m_rootPath = ""; // The default path.
70-
m_useSSL = false; // Default SSL is no.
69+
m_portNumber = 80; // The default port number.
70+
m_rootPath = ""; // The default path.
71+
m_useSSL = false; // Default SSL is no.
7172
setDirectoryListing(false); // Default directory listing is no.
7273
} // HttpServer
7374

@@ -188,11 +189,12 @@ class HttpServerTask: public Task {
188189
while(1) { // Loop forever.
189190

190191
ESP_LOGD("HttpServerTask", "Waiting for new peer client");
191-
192+
Memory::checkIntegrity();
192193
try {
193194
clientSocket = m_pHttpServer->m_sockServ.waitForNewClient(); // Block waiting for a new external client connection.
194195
}
195-
catch(std::exception e) {
196+
catch(std::exception &e) {
197+
ESP_LOGE("HttpServerTask", "Caught an exception waiting for new client!");
196198
return;
197199
}
198200

cpp_utils/Memory.cpp

+77-9
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,36 @@
1111
#include <stdlib.h>
1212
#include "GeneralUtils.h"
1313
extern "C" {
14-
#include <esp_heap_trace.h>
15-
#include <esp_heap_caps.h>
14+
#include <esp_heap_trace.h>
15+
#include <esp_heap_caps.h>
1616
}
1717
#include <esp_log.h>
18+
#include <esp_err.h>
1819

1920
static const char* LOG_TAG = "Memory";
2021

2122
heap_trace_record_t* Memory::m_pRecords = nullptr;
2223
size_t Memory::m_lastHeapSize = 0;
2324

25+
26+
/**
27+
* @brief Check the integrity of memory.
28+
* @return True if all integrity checks passed
29+
*/
30+
/* STATIC */ bool Memory::checkIntegrity() {
31+
bool rc = ::heap_caps_check_integrity_all(true);
32+
if (rc == false && m_pRecords != nullptr) {
33+
dumpRanges();
34+
abort();
35+
}
36+
return rc;
37+
} // checkIntegrity
38+
39+
2440
/**
2541
* @brief Dump the trace records from the heap.
2642
*/
27-
void Memory::dump() {
43+
/* STATIC */ void Memory::dump() {
2844
::heap_trace_dump();
2945
} // dump
3046

@@ -34,13 +50,53 @@ void Memory::dump() {
3450
int diff = currentUsage - m_lastHeapSize;
3551
ESP_LOGD(LOG_TAG, "%s: Heap changed by %d bytes (%d to %d)", tag.c_str(), diff, m_lastHeapSize, currentUsage);
3652
m_lastHeapSize = currentUsage;
37-
}
53+
} // dumpHeapChange
54+
55+
56+
/**
57+
* @brief Dump the ranges of allocations found.
58+
*/
59+
/* STATIC */ void Memory::dumpRanges() {
60+
// Each record contained in the Heap trace has the following format:
61+
//
62+
// * uint32_t ccount – Timestamp of record.
63+
// * void* address – Address that was allocated or released.
64+
// * size_t size – Size of the block that was requested and allocated.
65+
// * void* alloced_by[CONFIG_HEAP_TRACING_STACK_DEPTH] – Call stack of allocator
66+
// * void* freed_by[CONFIG_HEAP_TRACING_STACK_DEPTH] – Call stack of releasor
67+
//
68+
if (m_pRecords == nullptr) {
69+
return;
70+
}
71+
esp_log_level_set("*", ESP_LOG_NONE);
72+
size_t count = heap_trace_get_count();
73+
heap_trace_record_t record;
74+
printf(">>> dumpRanges\n");
75+
for (size_t i=0; i<count; i++) {
76+
esp_err_t errRc = heap_trace_get(i, &record);
77+
if (errRc != ESP_OK) {
78+
ESP_LOGE(LOG_TAG, "heap_trace_get: %d", errRc);
79+
}
80+
printf("0x%x:0x%x:%d:", (uint32_t)record.address, ((uint32_t)record.address) + record.size, record.size);
81+
for (size_t j=0; j<CONFIG_HEAP_TRACING_STACK_DEPTH; j++) {
82+
printf("%x ", (uint32_t)record.alloced_by[j]);
83+
}
84+
printf(":");
85+
for (size_t j=0; j<CONFIG_HEAP_TRACING_STACK_DEPTH; j++) {
86+
printf("%x ", (uint32_t)record.freed_by[j]);
87+
}
88+
printf("\n");
89+
}
90+
printf("<<< dumpRanges\n");
91+
esp_log_level_set("*", ESP_LOG_VERBOSE);
92+
} // dumpRanges
93+
3894

3995
/**
4096
* @brief Initialize heap recording.
4197
* @param [in] recordCount The maximum number of records to be recorded.
4298
*/
43-
void Memory::init(uint32_t recordCount) {
99+
/* STATIC */ void Memory::init(uint32_t recordCount) {
44100
assert(recordCount > 0);
45101
if (m_pRecords != nullptr) {
46102
ESP_LOGE(LOG_TAG, "Already initialized");
@@ -60,7 +116,10 @@ void Memory::init(uint32_t recordCount) {
60116
} // init
61117

62118

63-
void Memory::resumeTrace() {
119+
/**
120+
* @brief Resume previously paused trace.
121+
*/
122+
/* STATIC */ void Memory::resumeTrace() {
64123
esp_err_t errRc = ::heap_trace_resume();
65124
if (errRc != ESP_OK) {
66125
ESP_LOGE(LOG_TAG, "heap_trace_resume: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -69,7 +128,10 @@ void Memory::resumeTrace() {
69128
} // resumeTrace
70129

71130

72-
void Memory::startTraceAll() {
131+
/**
132+
* @brief Start tracing all allocate and free calls.
133+
*/
134+
/* STATIC */ void Memory::startTraceAll() {
73135
esp_err_t errRc = ::heap_trace_start(HEAP_TRACE_ALL);
74136
if (errRc != ESP_OK) {
75137
ESP_LOGE(LOG_TAG, "heap_trace_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -78,7 +140,10 @@ void Memory::startTraceAll() {
78140
} // startTraceAll
79141

80142

81-
void Memory::startTraceLeaks() {
143+
/**
144+
* Start tracing leaks. Matched allocate and free calls are removed.
145+
*/
146+
/* STATIC */ void Memory::startTraceLeaks() {
82147
esp_err_t errRc = ::heap_trace_start(HEAP_TRACE_LEAKS);
83148
if (errRc != ESP_OK) {
84149
ESP_LOGE(LOG_TAG, "heap_trace_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));
@@ -87,7 +152,10 @@ void Memory::startTraceLeaks() {
87152
} // startTraceLeaks
88153

89154

90-
void Memory::stopTrace() {
155+
/**
156+
* @brief Stop recording heap trace.
157+
*/
158+
/* STATIC */ void Memory::stopTrace() {
91159
esp_err_t errRc = ::heap_trace_stop();
92160
if (errRc != ESP_OK) {
93161
ESP_LOGE(LOG_TAG, "heap_trace_start: rc=%d %s", errRc, GeneralUtils::errorToString(errRc));

cpp_utils/Memory.h

+2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ extern "C" {
1616

1717
class Memory {
1818
public:
19+
static bool checkIntegrity();
1920
static void dump();
21+
static void dumpRanges();
2022
static void dumpHeapChange(std::string tag);
2123
static void init(uint32_t recordCount);
2224
static void resumeTrace();

cpp_utils/SockServ.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ SockServ::~SockServ() {
6464
SockServ* pSockServ = (SockServ*)data;
6565
try {
6666
while(1) {
67+
ESP_LOGD(LOG_TAG, "Waiting on accept")
6768
Socket tempSock = pSockServ->m_serverSocket.accept(pSockServ->getSSL());
6869
if (!tempSock.isValid()) {
6970
continue;

cpp_utils/Socket.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ Socket Socket::accept(bool useSSL) {
7878
newSocket.m_sslSock.fd = clientSockFD;
7979
newSocket.sslHandshake();
8080
ESP_LOGD(LOG_TAG, "DEBUG DEBUG ");
81-
uint8_t x;
81+
uint8_t x; // What is going on here???
8282
newSocket.receive(&x, 1, 0); // FIX FIX FIX
8383
}
8484
ESP_LOGD(LOG_TAG, "<< accept: sockFd: %d", clientSockFD);
@@ -226,7 +226,10 @@ void Socket::getBind(struct sockaddr* pAddr) {
226226
ESP_LOGE(LOG_TAG, "getBind: Socket is not initialized.");
227227
}
228228
socklen_t nameLen = sizeof(struct sockaddr);
229-
::getsockname(m_sock, pAddr, &nameLen);
229+
int rc = ::getsockname(m_sock, pAddr, &nameLen);
230+
if (rc != 0) {
231+
ESP_LOGE(LOG_TAG, "Error with getsockname in getBind: %s", strerror(errno));
232+
}
230233
} // getBind
231234

232235

cpp_utils/System.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,10 @@ std::string System::getIDFVersion() {
5757
size_t System::getMinimumFreeHeapSize() {
5858
return heap_caps_get_minimum_free_size(MALLOC_CAP_8BIT);
5959
} // getMinimumFreeHeapSize
60+
61+
/**
62+
* @brief Restart the ESP32.
63+
*/
64+
void System::restart() {
65+
esp_restart();
66+
} // restart

cpp_utils/System.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class System {
2222
static size_t getFreeHeapSize();
2323
static std::string getIDFVersion();
2424
static size_t getMinimumFreeHeapSize();
25+
static void restart();
2526
};
2627

2728
#endif /* COMPONENTS_CPP_UTILS_SYSTEM_H_ */

0 commit comments

Comments
 (0)