Skip to content

Commit 74ca42f

Browse files
d-a-vdevyte
authored andcommitted
Sketch emulation on host (#5342)
* WIP compile examples on host with 'make examples' * WIP bufferize tcp input * WIP Makefile * WIP network to rework, tcp/udp to factorize, udp addresses broken * minor changes to the core * WIP basic udp working * WIP mdns * WIP mcast receiving, not sending * WIP mdns OK * beta version * SSL + doc * update travis host test command * licenses * typo * doc: arduino builder is not around: declare functions before calling them * fix with latest SSL PR, compile in 32 bits mode * fix make clean * make -m32 optional * 32bits compiler ability tester * WIP * WIP (fix 1 vtable error, still another one to hunt with using spiffs) * example astyle * fix os_printf_plus * load / save mock spiffs * fix style * fix using spiffs/mock * don't mess ram * update doc * remove leftover * optimization -Os except for CI, rename ARCH32 to FORCE32 * revert useless cast (not even compiled) * remove unused function * use proper type for pointer arithmetics * makefile: sketch object and cpp file moved to bin/ directories easier to clean, and IDE don't like them * changes for review * make use of %zd * less verbose makefile by default (option) * update readme
1 parent b504881 commit 74ca42f

Some content is hidden

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

51 files changed

+3760
-96
lines changed

cores/esp8266/Esp.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "flash_utils.h"
2323
#include "eboot_command.h"
2424
#include <memory>
25-
#include "interrupts.h"
25+
#include <interrupts.h>
2626
#include "MD5Builder.h"
2727
#include "umm_malloc/umm_malloc.h"
2828
#include "cont.h"
@@ -165,6 +165,7 @@ void EspClass::restart(void)
165165
uint16_t EspClass::getVcc(void)
166166
{
167167
InterruptLock lock;
168+
(void)lock;
168169
return system_get_vdd33();
169170
}
170171

cores/esp8266/FSImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class DirImpl {
6262

6363
class FSImpl {
6464
public:
65+
virtual ~FSImpl () { }
6566
virtual bool begin() = 0;
6667
virtual void end() = 0;
6768
virtual bool format() = 0;

cores/esp8266/Updater.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "Updater.h"
22
#include "Arduino.h"
33
#include "eboot_command.h"
4-
#include "interrupts.h"
5-
#include "esp8266_peri.h"
4+
#include <interrupts.h>
5+
#include <esp8266_peri.h>
66

77
//#define DEBUG_UPDATER Serial
88

@@ -84,21 +84,21 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
8484

8585
wifi_set_sleep_type(NONE_SLEEP_T);
8686

87-
uint32_t updateStartAddress = 0;
87+
uintptr_t updateStartAddress = 0;
8888
if (command == U_FLASH) {
8989
//size of current sketch rounded to a sector
90-
uint32_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
90+
size_t currentSketchSize = (ESP.getSketchSize() + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
9191
//address of the end of the space available for sketch and update
92-
uint32_t updateEndAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
92+
uintptr_t updateEndAddress = (uintptr_t)&_SPIFFS_start - 0x40200000;
9393
//size of the update rounded to a sector
94-
uint32_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
94+
size_t roundedSize = (size + FLASH_SECTOR_SIZE - 1) & (~(FLASH_SECTOR_SIZE - 1));
9595
//address where we will start writing the update
9696
updateStartAddress = (updateEndAddress > roundedSize)? (updateEndAddress - roundedSize) : 0;
9797

9898
#ifdef DEBUG_UPDATER
99-
DEBUG_UPDATER.printf("[begin] roundedSize: 0x%08X (%d)\n", roundedSize, roundedSize);
100-
DEBUG_UPDATER.printf("[begin] updateEndAddress: 0x%08X (%d)\n", updateEndAddress, updateEndAddress);
101-
DEBUG_UPDATER.printf("[begin] currentSketchSize: 0x%08X (%d)\n", currentSketchSize, currentSketchSize);
99+
DEBUG_UPDATER.printf("[begin] roundedSize: 0x%08zX (%zd)\n", roundedSize, roundedSize);
100+
DEBUG_UPDATER.printf("[begin] updateEndAddress: 0x%08zX (%zd)\n", updateEndAddress, updateEndAddress);
101+
DEBUG_UPDATER.printf("[begin] currentSketchSize: 0x%08zX (%zd)\n", currentSketchSize, currentSketchSize);
102102
#endif
103103

104104
//make sure that the size of both sketches is less than the total space (updateEndAddress)
@@ -108,7 +108,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
108108
}
109109
}
110110
else if (command == U_SPIFFS) {
111-
updateStartAddress = (uint32_t)&_SPIFFS_start - 0x40200000;
111+
updateStartAddress = (uintptr_t)&_SPIFFS_start - 0x40200000;
112112
}
113113
else {
114114
// unknown command
@@ -133,7 +133,7 @@ bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) {
133133
#ifdef DEBUG_UPDATER
134134
DEBUG_UPDATER.printf("[begin] _startAddress: 0x%08X (%d)\n", _startAddress, _startAddress);
135135
DEBUG_UPDATER.printf("[begin] _currentAddress: 0x%08X (%d)\n", _currentAddress, _currentAddress);
136-
DEBUG_UPDATER.printf("[begin] _size: 0x%08X (%d)\n", _size, _size);
136+
DEBUG_UPDATER.printf("[begin] _size: 0x%08zX (%zd)\n", _size, _size);
137137
#endif
138138

139139
_md5.begin();
@@ -159,7 +159,7 @@ bool UpdaterClass::end(bool evenIfRemaining){
159159

160160
if(hasError() || (!isFinished() && !evenIfRemaining)){
161161
#ifdef DEBUG_UPDATER
162-
DEBUG_UPDATER.printf("premature end: res:%u, pos:%u/%u\n", getError(), progress(), _size);
162+
DEBUG_UPDATER.printf("premature end: res:%u, pos:%zu/%zu\n", getError(), progress(), _size);
163163
#endif
164164

165165
_reset();
@@ -199,10 +199,10 @@ bool UpdaterClass::end(bool evenIfRemaining){
199199
eboot_command_write(&ebcmd);
200200

201201
#ifdef DEBUG_UPDATER
202-
DEBUG_UPDATER.printf("Staged: address:0x%08X, size:0x%08X\n", _startAddress, _size);
202+
DEBUG_UPDATER.printf("Staged: address:0x%08X, size:0x%08zX\n", _startAddress, _size);
203203
}
204204
else if (_command == U_SPIFFS) {
205-
DEBUG_UPDATER.printf("SPIFFS: address:0x%08X, size:0x%08X\n", _startAddress, _size);
205+
DEBUG_UPDATER.printf("SPIFFS: address:0x%08X, size:0x%08zX\n", _startAddress, _size);
206206
#endif
207207
}
208208

cores/esp8266/spiffs_api.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ bool isSpiffsFilenameValid(const char* name)
109109
}
110110

111111
// these symbols should be defined in the linker script for each flash layout
112+
#ifndef CORE_MOCK
112113
#ifdef ARDUINO
113114
extern "C" uint32_t _SPIFFS_start;
114115
extern "C" uint32_t _SPIFFS_end;
@@ -131,6 +132,7 @@ FS SPIFFS = FS(FSImplPtr(new SPIFFSImpl(
131132
SPIFFS_PHYS_PAGE,
132133
SPIFFS_PHYS_BLOCK,
133134
SPIFFS_MAX_OPEN_FILES)));
134-
#endif
135+
#endif // ARDUINO
136+
#endif // !CORE_MOCK
135137

136138
#endif

cores/esp8266/spiffs_api.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class SPIFFSImpl : public FSImpl
220220
size_t cacheBufSize = SPIFFS_buffer_bytes_for_cache(&_fs, _maxOpenFds);
221221

222222
if (!_workBuf) {
223-
DEBUGV("SPIFFSImpl: allocating %d+%d+%d=%d bytes\r\n",
223+
DEBUGV("SPIFFSImpl: allocating %zd+%zd+%zd=%zd bytes\r\n",
224224
workBufSize, fdsBufSize, cacheBufSize,
225225
workBufSize + fdsBufSize + cacheBufSize);
226226
_workBuf.reset(new uint8_t[workBufSize]);

libraries/DNSServer/src/DNSServer.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
DNSServer::DNSServer()
1212
{
13-
_ttl = htonl(60);
13+
_ttl = lwip_htonl(60);
1414
_errorReplyCode = DNSReplyCode::NonExistentDomain;
1515
}
1616

@@ -35,7 +35,7 @@ void DNSServer::setErrorReplyCode(const DNSReplyCode &replyCode)
3535

3636
void DNSServer::setTTL(const uint32_t &ttl)
3737
{
38-
_ttl = htonl(ttl);
38+
_ttl = lwip_htonl(ttl);
3939
}
4040

4141
void DNSServer::stop()
@@ -81,7 +81,7 @@ void DNSServer::processNextRequest()
8181

8282
bool DNSServer::requestIncludesOnlyOneQuestion(const DNSHeader* dnsHeader)
8383
{
84-
return ntohs(dnsHeader->QDCount) == 1 &&
84+
return lwip_ntohs(dnsHeader->QDCount) == 1 &&
8585
dnsHeader->ANCount == 0 &&
8686
dnsHeader->NSCount == 0 &&
8787
dnsHeader->ARCount == 0;

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
734734
free(buff);
735735

736736
if(size && (int) size != bytesWritten) {
737-
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size);
737+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %zd mismatch!.\n", bytesWritten, size);
738738
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
739739
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
740740
} else {

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+6-12
Original file line numberDiff line numberDiff line change
@@ -438,12 +438,9 @@ void ESP8266WebServer::sendContent(const String& content) {
438438
const char * footer = "\r\n";
439439
size_t len = content.length();
440440
if(_chunked) {
441-
char * chunkSize = (char *)malloc(11);
442-
if(chunkSize){
443-
sprintf(chunkSize, "%x%s", len, footer);
444-
_currentClientWrite(chunkSize, strlen(chunkSize));
445-
free(chunkSize);
446-
}
441+
char chunkSize[11];
442+
sprintf(chunkSize, "%zx\r\n", len);
443+
_currentClientWrite(chunkSize, strlen(chunkSize));
447444
}
448445
_currentClientWrite(content.c_str(), len);
449446
if(_chunked){
@@ -461,12 +458,9 @@ void ESP8266WebServer::sendContent_P(PGM_P content) {
461458
void ESP8266WebServer::sendContent_P(PGM_P content, size_t size) {
462459
const char * footer = "\r\n";
463460
if(_chunked) {
464-
char * chunkSize = (char *)malloc(11);
465-
if(chunkSize){
466-
sprintf(chunkSize, "%x%s", size, footer);
467-
_currentClientWrite(chunkSize, strlen(chunkSize));
468-
free(chunkSize);
469-
}
461+
char chunkSize[11];
462+
sprintf(chunkSize, "%zx\r\n", size);
463+
_currentClientWrite(chunkSize, strlen(chunkSize));
470464
}
471465
_currentClientWrite_P(content, size);
472466
if(_chunked){
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
UDPSendReceive.pde:
3+
This sketch receives UDP message strings, prints them to the serial port
4+
and sends an "acknowledge" string back to the sender
5+
6+
A Processing sketch is included at the end of file that can be used to send
7+
and received messages for testing with a computer.
8+
9+
created 21 Aug 2010
10+
by Michael Margolis
11+
12+
This code is in the public domain.
13+
14+
adapted from Ethernet library examples
15+
*/
16+
17+
18+
#include <ESP8266WiFi.h>
19+
#include <WiFiUdp.h>
20+
21+
#define SSID "ssid"
22+
#define PSK "psk"
23+
24+
unsigned int localPort = 8888; // local port to listen on
25+
26+
// buffers for receiving and sending data
27+
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
28+
char ReplyBuffer[] = "acknowledged\r\n"; // a string to send back
29+
30+
WiFiUDP Udp;
31+
32+
void setup() {
33+
Serial.begin(115200);
34+
WiFi.mode(WIFI_STA);
35+
WiFi.begin(SSID, PSK);
36+
while (WiFi.status() != WL_CONNECTED) {
37+
Serial.print('.');
38+
delay(500);
39+
}
40+
Serial.print("Connected! IP address: ");
41+
Serial.println(WiFi.localIP());
42+
Serial.printf("UDP server on port %d\n", localPort);
43+
Udp.begin(localPort);
44+
}
45+
46+
void loop() {
47+
// if there's data available, read a packet
48+
int packetSize = Udp.parsePacket();
49+
if (packetSize) {
50+
Serial.print("Received packet of size ");
51+
Serial.println(packetSize);
52+
Serial.print("From ");
53+
IPAddress remote = Udp.remoteIP();
54+
for (int i = 0; i < 4; i++) {
55+
Serial.print(remote[i], DEC);
56+
if (i < 3) {
57+
Serial.print(".");
58+
}
59+
}
60+
Serial.print(", port ");
61+
Serial.println(Udp.remotePort());
62+
63+
// read the packet into packetBufffer
64+
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
65+
Serial.println("Contents:");
66+
Serial.println(packetBuffer);
67+
68+
// send a reply, to the IP address and port that sent us the packet we received
69+
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
70+
Udp.write(ReplyBuffer);
71+
Udp.endPacket();
72+
}
73+
delay(10);
74+
}
75+
76+
/*
77+
test (shell/netcat):
78+
---------------
79+
nc -u 192.168.esp.address 8888
80+
*/

libraries/ESP8266WiFi/src/BearSSLHelpers.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,8 @@ bool X509List::append(const uint8_t *derCert, size_t derLen) {
826826
return true;
827827
}
828828

829+
#if !CORE_MOCK
830+
829831
// Second stack thunked helpers
830832
make_stack_thunk(br_ssl_engine_recvapp_ack);
831833
make_stack_thunk(br_ssl_engine_recvapp_buf);
@@ -836,4 +838,6 @@ make_stack_thunk(br_ssl_engine_sendapp_buf);
836838
make_stack_thunk(br_ssl_engine_sendrec_ack);
837839
make_stack_thunk(br_ssl_engine_sendrec_buf);
838840

841+
#endif
842+
839843
};

libraries/ESP8266WiFi/src/WiFiClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ extern "C"
3838
#include "lwip/tcp.h"
3939
#include "lwip/inet.h"
4040
#include "lwip/netif.h"
41-
#include "include/ClientContext.h"
41+
#include <include/ClientContext.h>
4242
#include "c_types.h"
4343

4444
uint16_t WiFiClient::_localPort = 0;

libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp

+23-2
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ extern "C" {
4040
#include "lwip/tcp.h"
4141
#include "lwip/inet.h"
4242
#include "lwip/netif.h"
43-
#include "include/ClientContext.h"
43+
#include <include/ClientContext.h>
4444
#include "c_types.h"
4545
#include "coredecls.h"
4646

47+
#if !CORE_MOCK
48+
4749
// The BearSSL thunks in use for now
4850
#define br_ssl_engine_recvapp_ack thunk_br_ssl_engine_recvapp_ack
4951
#define br_ssl_engine_recvapp_buf thunk_br_ssl_engine_recvapp_buf
@@ -54,6 +56,8 @@ extern "C" {
5456
#define br_ssl_engine_sendrec_ack thunk_br_ssl_engine_sendrec_ack
5557
#define br_ssl_engine_sendrec_buf thunk_br_ssl_engine_sendrec_buf
5658

59+
#endif
60+
5761
namespace BearSSL {
5862

5963
void WiFiClientSecure::_clear() {
@@ -1377,6 +1381,21 @@ bool WiFiClientSecure::loadPrivateKey(Stream& stream, size_t size) {
13771381
// SSL debugging which should focus on the WiFiClientBearSSL objects.
13781382

13791383
extern "C" {
1384+
1385+
#if CORE_MOCK
1386+
1387+
void br_esp8266_stack_proxy_init(uint8_t *space, uint16_t size) {
1388+
(void)space;
1389+
(void)size;
1390+
}
1391+
void _BearSSLCheckStack(const char *fcn, const char *file, int line) {
1392+
(void)fcn;
1393+
(void)file;
1394+
(void)line;
1395+
}
1396+
1397+
#else // !CORE_MOCK
1398+
13801399
extern size_t br_esp8266_stack_proxy_usage();
13811400

13821401
void _BearSSLCheckStack(const char *fcn, const char *file, int line) {
@@ -1386,7 +1405,7 @@ extern "C" {
13861405
int freeheap = ESP.getFreeHeap();
13871406
static int laststack, lastheap, laststack2;
13881407
if ((laststack != freestack) || (lastheap != freeheap) || (laststack2 != (int)br_esp8266_stack_proxy_usage())) {
1389-
Serial.printf("%s:%s(%d): FREESTACK=%d, STACK2USAGE=%d, FREEHEAP=%d\n", file, fcn, line, freestack, br_esp8266_stack_proxy_usage(), freeheap);
1408+
Serial.printf("%s:%s(%d): FREESTACK=%d, STACK2USAGE=%zd, FREEHEAP=%d\n", file, fcn, line, freestack, br_esp8266_stack_proxy_usage(), freeheap);
13901409
if (freestack < 256) {
13911410
Serial.printf("!!! Out of main stack space\n");
13921411
}
@@ -1405,6 +1424,8 @@ extern "C" {
14051424
}
14061425
}
14071426

1427+
#endif // !CORE_MOCK
1428+
14081429
void _BearSSLSerialPrint(const char *str) {
14091430
static int cnt = 0;
14101431
Serial.printf("%s", str);

libraries/ESP8266WiFi/src/WiFiServer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extern "C" {
3535
#include "lwip/opt.h"
3636
#include "lwip/tcp.h"
3737
#include "lwip/inet.h"
38-
#include "include/ClientContext.h"
38+
#include <include/ClientContext.h>
3939

4040
WiFiServer::WiFiServer(IPAddress addr, uint16_t port)
4141
: _port(port)

libraries/ESP8266WiFi/src/WiFiServerSecureBearSSL.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern "C" {
3434
#include "lwip/opt.h"
3535
#include "lwip/tcp.h"
3636
#include "lwip/inet.h"
37-
#include "include/ClientContext.h"
37+
#include <include/ClientContext.h>
3838
#include "WiFiServerSecureBearSSL.h"
3939

4040
namespace BearSSL {

0 commit comments

Comments
 (0)