Skip to content

Commit 3dba795

Browse files
author
Me No Dev
committed
Merge remote-tracking branch 'esp8266/master'
2 parents db26ef4 + 44d2722 commit 3dba795

32 files changed

+871
-142
lines changed

cores/esp8266/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ void noTone(uint8_t _pin);
271271
long random(long);
272272
long random(long, long);
273273
void randomSeed(unsigned long);
274+
long secureRandom(long);
275+
long secureRandom(long, long);
274276
long map(long, long, long, long, long);
275277

276278
extern "C" void configTime(long timezone, int daylightOffset_sec,

cores/esp8266/HardwareSerial.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,13 @@ size_t HardwareSerial::write(uint8_t c)
180180
return 1;
181181
}
182182

183+
int HardwareSerial::baudRate(void)
184+
{
185+
// Null pointer on _uart is checked by SDK
186+
return uart_get_baudrate(_uart);
187+
}
188+
189+
183190
HardwareSerial::operator bool() const
184191
{
185192
return _uart != 0;

cores/esp8266/HardwareSerial.h

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class HardwareSerial: public Stream
133133
void setDebugOutput(bool);
134134
bool isTxEnabled(void);
135135
bool isRxEnabled(void);
136+
int baudRate(void);
136137

137138
protected:
138139
int _uart_nr;

cores/esp8266/MD5Builder.cpp

+78-59
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,101 @@
1-
#include "Arduino.h"
2-
#include "md5.h"
3-
#include "MD5Builder.h"
1+
#include <Arduino.h>
2+
#include <MD5Builder.h>
43

5-
#define hex_char_to_byte(c) (((c)>='a'&&(c)<='f')?((c)-87):((c)>='A'&&(c)<='F')?((c)-55):((c)>='0'&&(c)<='9')?((c)-48):0)
4+
uint8_t hex_char_to_byte(uint8_t c)
5+
{
6+
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
7+
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
8+
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0;
9+
}
610

7-
void MD5Builder::begin(void){
8-
memset(_buf, 0x00, 16);
9-
MD5Init(&_ctx);
11+
void MD5Builder::begin(void)
12+
{
13+
memset(_buf, 0x00, 16);
14+
MD5Init(&_ctx);
1015
}
1116

12-
void MD5Builder::add(uint8_t * data, uint16_t len){
13-
MD5Update(&_ctx, data, len);
17+
void MD5Builder::add(uint8_t * data, uint16_t len)
18+
{
19+
MD5Update(&_ctx, data, len);
1420
}
1521

16-
void MD5Builder::addHexString(const char * data){
17-
uint16_t i, len = strlen(data);
18-
uint8_t * tmp = (uint8_t*)malloc(len/2);
19-
if(tmp == NULL)
20-
return;
21-
for(i=0; i<len; i+=2) tmp[i/2] = (hex_char_to_byte(data[i]) & 0x0F) << 4 | (hex_char_to_byte(data[i+1]) & 0x0F);
22-
add(tmp, len/2);
23-
free(tmp);
22+
void MD5Builder::addHexString(const char * data)
23+
{
24+
uint16_t i, len = strlen(data);
25+
uint8_t * tmp = (uint8_t*)malloc(len/2);
26+
if(tmp == NULL) {
27+
return;
28+
}
29+
for(i=0; i<len; i+=2) {
30+
uint8_t high = hex_char_to_byte(data[i]);
31+
uint8_t low = hex_char_to_byte(data[i+1]);
32+
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
33+
}
34+
add(tmp, len/2);
35+
free(tmp);
2436
}
2537

26-
bool MD5Builder::addStream(Stream & stream, const size_t total_len) {
27-
const int buf_size = 512;
28-
int bytesleft = total_len;
29-
uint8_t * buf = (uint8_t*) malloc(buf_size);
30-
if(buf) {
31-
while((stream.available() > -1) && (bytesleft > 0)) {
32-
// get available data size
33-
int sizeAvailable = stream.available();
34-
if(sizeAvailable) {
35-
int readBytes = sizeAvailable;
36-
37-
// read only the asked bytes
38-
if(readBytes > bytesleft) {
39-
readBytes = bytesleft ;
40-
}
38+
bool MD5Builder::addStream(Stream & stream, const size_t maxLen)
39+
{
40+
const int buf_size = 512;
41+
int maxLengthLeft = maxLen;
42+
uint8_t * buf = (uint8_t*) malloc(buf_size);
43+
44+
if(!buf) {
45+
return false;
46+
}
47+
48+
int bytesAvailable = stream.available();
49+
while((bytesAvailable > 0) && (maxLengthLeft > 0)) {
4150

42-
// not read more the buffer can handle
51+
// determine number of bytes to read
52+
int readBytes = bytesAvailable;
53+
if(readBytes > maxLengthLeft) {
54+
readBytes = maxLengthLeft ; // read only until max_len
55+
}
4356
if(readBytes > buf_size) {
44-
readBytes = buf_size;
57+
readBytes = buf_size; // not read more the buffer can handle
4558
}
4659

47-
// read data
48-
int bytesread = stream.readBytes(buf, readBytes);
49-
bytesleft -= bytesread;
50-
if(bytesread > 0) {
51-
MD5Update(&_ctx, buf, bytesread);
60+
// read data and check if we got something
61+
int numBytesRead = stream.readBytes(buf, readBytes);
62+
if(numBytesRead< 1) {
63+
return false;
5264
}
53-
}
54-
// time for network streams
55-
delay(0);
65+
66+
// Update MD5 with buffer payload
67+
MD5Update(&_ctx, buf, numBytesRead);
68+
69+
yield(); // time for network streams
70+
71+
// update available number of bytes
72+
maxLengthLeft -= numBytesRead;
73+
bytesAvailable = stream.available();
5674
}
57-
// guaranteed not null
5875
free(buf);
59-
return (bytesleft == 0);
60-
} else {
61-
return false;
62-
}
76+
return true;
6377
}
6478

65-
void MD5Builder::calculate(void){
66-
MD5Final(_buf, &_ctx);
79+
void MD5Builder::calculate(void)
80+
{
81+
MD5Final(_buf, &_ctx);
6782
}
6883

69-
void MD5Builder::getBytes(uint8_t * output){
70-
memcpy(output, _buf, 16);
84+
void MD5Builder::getBytes(uint8_t * output)
85+
{
86+
memcpy(output, _buf, 16);
7187
}
7288

73-
void MD5Builder::getChars(char * output){
74-
for(uint8_t i = 0; i < 16; i++)
75-
sprintf(output + (i * 2), "%02x", _buf[i]);
89+
void MD5Builder::getChars(char * output)
90+
{
91+
for(uint8_t i = 0; i < 16; i++) {
92+
sprintf(output + (i * 2), "%02x", _buf[i]);
93+
}
7694
}
7795

78-
String MD5Builder::toString(void){
79-
char out[33];
80-
getChars(out);
81-
return String(out);
82-
}
96+
String MD5Builder::toString(void)
97+
{
98+
char out[33];
99+
getChars(out);
100+
return String(out);
101+
}

cores/esp8266/MD5Builder.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#ifndef __ESP8266_MD5_BUILDER__
2222
#define __ESP8266_MD5_BUILDER__
2323

24-
#include "Arduino.h"
24+
#include <WString.h>
25+
#include <Stream.h>
2526
#include "md5.h"
2627

2728
class MD5Builder {
@@ -37,7 +38,7 @@ class MD5Builder {
3738
void addHexString(const char * data);
3839
void addHexString(char * data){ addHexString((const char*)data); }
3940
void addHexString(String data){ addHexString(data.c_str()); }
40-
bool addStream(Stream & stream, const size_t total_len);
41+
bool addStream(Stream & stream, const size_t maxLen);
4142
void calculate(void);
4243
void getBytes(uint8_t * output);
4344
void getChars(char * output);

cores/esp8266/StreamString.h

+8-9
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525

2626

2727
class StreamString: public Stream, public String {
28-
29-
size_t write(const uint8_t *buffer, size_t size);
30-
size_t write(uint8_t data);
31-
32-
int available();
33-
int read();
34-
int peek();
35-
void flush();
36-
28+
public:
29+
size_t write(const uint8_t *buffer, size_t size) override;
30+
size_t write(uint8_t data) override;
31+
32+
int available() override;
33+
int read() override;
34+
int peek() override;
35+
void flush() override;
3736
};
3837

3938

cores/esp8266/Updater.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#ifndef ESP8266UPDATER_H
22
#define ESP8266UPDATER_H
33

4-
#include "Arduino.h"
5-
#include "flash_utils.h"
6-
#include "MD5Builder.h"
4+
#include <Arduino.h>
5+
#include <flash_utils.h>
6+
#include <MD5Builder.h>
77

88
#define UPDATE_ERROR_OK (0)
99
#define UPDATE_ERROR_WRITE (1)

cores/esp8266/WMath.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,22 @@ extern "C" {
2828
}
2929
#include "esp8266_peri.h"
3030

31+
static bool s_randomSeedCalled = false;
32+
3133
void randomSeed(unsigned long seed) {
3234
if(seed != 0) {
33-
srand((seed ^ RANDOM_REG32));
35+
srand(seed);
36+
s_randomSeedCalled = true;
3437
}
3538
}
3639

3740
long random(long howbig) {
3841
if(howbig == 0) {
3942
return 0;
4043
}
41-
return (rand() ^ RANDOM_REG32) % howbig;
44+
// if randomSeed was called, fall back to software PRNG
45+
uint32_t val = (s_randomSeedCalled) ? rand() : RANDOM_REG32;
46+
return val % howbig;
4247
}
4348

4449
long random(long howsmall, long howbig) {
@@ -49,6 +54,21 @@ long random(long howsmall, long howbig) {
4954
return random(diff) + howsmall;
5055
}
5156

57+
long secureRandom(long howbig) {
58+
if(howbig == 0) {
59+
return 0;
60+
}
61+
return RANDOM_REG32 % howbig;
62+
}
63+
64+
long secureRandom(long howsmall, long howbig) {
65+
if(howsmall >= howbig) {
66+
return howsmall;
67+
}
68+
long diff = howbig - howsmall;
69+
return secureRandom(diff) + howsmall;
70+
}
71+
5272
long map(long x, long in_min, long in_max, long out_min, long out_max) {
5373
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
5474
}

cores/esp8266/spiffs_api.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ bool SPIFFSImpl::exists(const char* path)
6363
return rc == SPIFFS_OK;
6464
}
6565

66-
DirImplPtr SPIFFSImpl::openDir(const char* path)
66+
DirImplPtr SPIFFSImpl::openDir(const char* path)
6767
{
68-
if (!isSpiffsFilenameValid(path)) {
68+
if (strlen(path) > 0 && !isSpiffsFilenameValid(path)) {
6969
DEBUGV("SPIFFSImpl::openDir: invalid path=`%s` \r\n", path);
7070
return DirImplPtr();
7171
}

doc/changes.md

+39
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,26 @@ title: Change Log
99
- Update SDK to 1.5.3
1010
- umm_malloc: print block start address before heap corruption callback is triggered
1111
- If GDBStub library is used, break into gdb on assert and panic
12+
- Add option to keep FS classes in namespace (#2030)
13+
- Add SPIFFS::end (#1657)
14+
- Add ArduinoOTA::getHostname() interface
15+
- Add __throw_out_of_range
16+
- Add support for RTC user memory in ESP-specific APIs. (#1836)
17+
- Expose RTC_USER_MEM in esp8266_peri.h
18+
- Remove DISABLED macro (#2072)
19+
- Execute global constructors in correct order (#2074)
20+
- Real board name available in Sketch/MDNS/OTA (#2054)
21+
- Add DOUT/QOUT flash modes
22+
- Add ESP8285 entry in boards menu
23+
- Move timer detachInterrupt functions into IRAM (#2083)
24+
- Make Updater be able to run inside async callbacks (#2096)
25+
- Add new boards Phoenix 1.0 & Phoenix 2.0 (#2088)
26+
- Store git version of the core in the compiled binary (#2099)
27+
- Rebuild libstdc++ with mlongcalls and link against it (#1983)
28+
- Add mechanism for posting functions to the main loop (#2082)
29+
- MD5Builder::addStream: fixed falsy calculated hash for len > filelength (#2126)
30+
- Fix SPIFFS.openDir("") (#2143)
31+
- Bring back old semantics to random and randomSeed, add secureRandom (#1710) (#2142)
1232

1333
### Libraries
1434

@@ -25,6 +45,25 @@ title: Change Log
2545
- HTTPClient: include non-standard ports in Host: header
2646
- ESP8266WiFi: Prevent WiFi config corruption (#1997 #1856 #1699 #1675)
2747
- GDBStub: fix section attribute for core gdbstub functions
48+
- Wire: I2C bus reset with info to user
49+
- ESP8266HTTPClient: allow HTTP header value without LWS
50+
- ESP8266mDNS: Fix mDNS doesn't accept queryService responses from avahi-daemon (#2015)
51+
- Add MFRC522 to supported libraries (#2044)
52+
- Update axTLS to ab516f7 (1.5.3+)
53+
- Mention ESP8266Ping library
54+
- ESP8266HTTPClient: fix duplicate Content-Length headers (#1902)
55+
- ESP8266HTTPUpdateServer: make HTTP Update Server more secure (#2104)
56+
- ESP8266WiFi: add virtual destructor to WiFiServer class (#2116)
57+
- ESP8266WiFi: fix error when calling `WiFiServer::close` more than once
58+
- ESP8266WiFi: WiFi event handling refactoring (#2119)
59+
- ESP8266mDNS: restart listening when WiFi STA is connected/disconnected (#1828)
60+
- ESP8266WiFi: allow DHCP client to be re-enabled using WiFi.config(0U, 0U, 0U) (#1896)
61+
- ESP8266WiFi: enable SO_REUSE in LwIP and WiFiServer (#1431)
62+
- ESP8266WebServer: make ESP8266WebServer::urlDecode public (#1419)
63+
- LwIP: sntp_localtime: return -1 in tm_isdst field (#2010)
64+
- ESP8266WiFi: fix for crash in WiFiClientSecure when WiFi is disconnected (#2139)
65+
- SD: Prevent WDT resets in SD library (#1815)
66+
- ESP8266WiFi: Fix issue when WiFi.begin(ssid, pass) is called right after WiFi.mode(WIFI_OFF)
2867

2968
### Tools
3069

doc/reference.md

+15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ You also need to use `Serial.setDebugOutput(true)` to enable output from `printf
8484
8585
Both `Serial` and `Serial1` objects support 5, 6, 7, 8 data bits, odd (O), even (E), and no (N) parity, and 1 or 2 stop bits. To set the desired mode, call `Serial.begin(baudrate, SERIAL_8N1)`, `Serial.begin(baudrate, SERIAL_6E2)`, etc.
8686
87+
A new method has been implemented on both `Serial` and `Serial1` to get current baud rate setting. To get the current baud rate, call `Serial.baudRate()`, `Serial1.baudRate()`. Return a `int` of current speed. For example
88+
```cpp
89+
// Set Baud rate to 57600
90+
Serial.begin(57600);
91+
92+
// Get current baud rate
93+
int br = Serial.baudRate();
94+
95+
// Will print "Serial is 57600 bps"
96+
Serial.printf("Serial is %d bps", br);
97+
```
98+
99+
I've done this also for official ESP8266 [Software Serial](https://github.com/esp8266/Arduino/blob/master/doc/libraries.md#softwareserial) library, see this [pull request](https://github.com/plerup/espsoftwareserial/pull/22).
100+
Note that this implementation is **only for ESP8266 based boards**, and will not works with other Arduino boards.
101+
87102
## Progmem
88103

89104
The Program memory features work much the same way as on a regular Arduino; placing read only data and strings in read only memory and freeing heap for your application.

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ class ESP8266WebServer
119119
void sendContent_P(PGM_P content);
120120
void sendContent_P(PGM_P content, size_t size);
121121

122+
static String urlDecode(const String& text);
123+
122124
template<typename T> size_t streamFile(T &file, const String& contentType){
123125
setContentLength(file.size());
124126
if (String(file.name()).endsWith(".gz") &&
@@ -142,7 +144,6 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
142144
uint8_t _uploadReadByte(WiFiClient& client);
143145
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
144146
bool _collectHeader(const char* headerName, const char* headerValue);
145-
String urlDecode(const String& text);
146147

147148
struct RequestArgument {
148149
String key;

0 commit comments

Comments
 (0)