Skip to content

Commit 7d3a93a

Browse files
authored
Merge pull request esp8266#10 from esp8266/master
Update
2 parents 1be1d73 + 901410f commit 7d3a93a

Some content is hidden

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

54 files changed

+1063
-193
lines changed

boards.txt

+166-76
Large diffs are not rendered by default.

cores/esp8266/HardwareSerial.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,9 @@ unsigned long HardwareSerial::testBaudrate()
121121

122122
unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis)
123123
{
124-
time_t startMillis = millis();
124+
esp8266::polledTimeout::oneShotFastMs timeOut(timeoutMillis);
125125
unsigned long detectedBaudrate;
126-
while ((time_t) millis() - startMillis < timeoutMillis) {
126+
while (!timeOut) {
127127
if ((detectedBaudrate = testBaudrate())) {
128128
break;
129129
}

cores/esp8266/Print.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ size_t Print::write(const uint8_t *buffer, size_t size) {
4545

4646
size_t n = 0;
4747
while (size--) {
48-
size_t ret = write(*buffer++);
48+
size_t ret = write(pgm_read_byte(buffer++));
4949
if (ret == 0) {
5050
// Write of last byte didn't complete, abort additional processing
5151
break;

cores/esp8266/Print.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Print {
5656
size_t write(const char *str) {
5757
if(str == NULL)
5858
return 0;
59-
return write((const uint8_t *) str, strlen(str));
59+
return write((const uint8_t *) str, strlen_P(str));
6060
}
6161
virtual size_t write(const uint8_t *buffer, size_t size);
6262
size_t write(const char *buffer, size_t size) {

cores/esp8266/Updater.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "Updater.h"
2-
#include "Arduino.h"
32
#include "eboot_command.h"
43
#include <esp8266_peri.h>
54

@@ -11,10 +10,10 @@
1110
#endif
1211

1312
#if ARDUINO_SIGNING
14-
#include "../../libraries/ESP8266WiFi/src/BearSSLHelpers.h"
15-
static BearSSL::PublicKey signPubKey(signing_pubkey);
16-
static BearSSL::HashSHA256 hash;
17-
static BearSSL::SigningVerifier sign(&signPubKey);
13+
namespace esp8266 {
14+
extern UpdaterHashClass& updaterSigningHash;
15+
extern UpdaterVerifyClass& updaterSigningVerifier;
16+
}
1817
#endif
1918

2019
extern "C" {
@@ -39,7 +38,7 @@ UpdaterClass::UpdaterClass()
3938
, _progress_callback(nullptr)
4039
{
4140
#if ARDUINO_SIGNING
42-
installSignature(&hash, &sign);
41+
installSignature(&esp8266::updaterSigningHash, &esp8266::updaterSigningVerifier);
4342
#endif
4443
}
4544

cores/esp8266/abi.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ extern "C" void __cxa_pure_virtual(void) __attribute__ ((__noreturn__));
3232
extern "C" void __cxa_deleted_virtual(void) __attribute__ ((__noreturn__));
3333

3434

35-
#ifndef __cpp_exceptions
35+
#if !defined(__cpp_exceptions) && !defined(NEW_OOM_ABORT)
3636
void *operator new(size_t size)
3737
{
3838
void *ret = malloc(size);
@@ -52,7 +52,7 @@ void *operator new[](size_t size)
5252
}
5353
return ret;
5454
}
55-
#endif
55+
#endif // arduino's std::new legacy
5656

5757
void __cxa_pure_virtual(void)
5858
{

cores/esp8266/core_esp8266_features.h

+32-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,37 @@
3232

3333
#define WIFI_HAS_EVENT_CALLBACK
3434

35+
#ifdef __cplusplus
36+
37+
#include <stdlib.h> // malloc()
38+
#include <stddef.h> // size_t
39+
40+
namespace arduino
41+
{
42+
extern "C++"
43+
template <typename T, typename ...TConstructorArgs>
44+
T* new0 (size_t n, TConstructorArgs... TconstructorArgs)
45+
{
46+
// n==0: single allocation, otherwise it is an array
47+
size_t offset = n? sizeof(size_t): 0;
48+
size_t arraysize = n? n: 1;
49+
T* ptr = (T*)malloc(offset + (arraysize * sizeof(T)));
50+
if (ptr)
51+
{
52+
if (n)
53+
*(size_t*)(ptr) = n;
54+
for (size_t i = 0; i < arraysize; i++)
55+
new (ptr + offset + i * sizeof(T)) T(TconstructorArgs...);
56+
return ptr + offset;
57+
}
58+
return nullptr;
59+
}
60+
}
61+
62+
#define arduino_new(Type, ...) arduino::new0<Type>(0, ##__VA_ARGS__)
63+
#define arduino_newarray(Type, n, ...) arduino::new0<Type>(n, ##__VA_ARGS__)
64+
65+
#endif // __cplusplus
3566

3667
#ifndef __STRINGIFY
3768
#define __STRINGIFY(a) #a
@@ -61,4 +92,4 @@ inline uint32_t esp_get_cycle_count() {
6192
}
6293
#endif // not CORE_MOCK
6394

64-
#endif
95+
#endif // CORE_ESP8266_FEATURES_H

cores/esp8266/uart.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ uart_write(uart_t* uart, const char* buf, size_t size)
495495
size_t ret = size;
496496
const int uart_nr = uart->uart_nr;
497497
while (size--)
498-
uart_do_write_char(uart_nr, *buf++);
498+
uart_do_write_char(uart_nr, pgm_read_byte(buf++));
499499

500500
return ret;
501501
}

doc/eclipse/makefile.init

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ ESP8266_BASE = $(ARDUINO_BASE)/hardware/esp8266com/esp8266
1414
ESP8266_TOOLS = $(ESP8266_BASE)/tools
1515
XTENSA_TOOLS_ROOT = $(ESP8266_TOOLS)/xtensa-lx106-elf/bin
1616

17-
PYTHON_BIN = python
17+
PYTHON_BIN = python3
1818
ESPTOOL_PY_BIN = $(ESP8266_TOOLS)/esptool.py
1919
ESPOTA_PY_BIN = $(ESP8266_TOOLS)/espota.py
2020
ESPTOOL_BIN = $(ESP8266_TOOLS)/esptool/esptool.exe

doc/installing.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Prerequisites
4343

4444
- Arduino 1.6.8 (or newer, current working version is 1.8.5)
4545
- git
46-
- Python 2.7 (https://python.org)
46+
- Python 3.x (https://python.org)
4747
- terminal, console, or command prompt (depending on your OS)
4848
- Internet connection
4949

@@ -110,7 +110,7 @@ Instructions - Windows 10
110110
.. code:: bash
111111
112112
cd esp8266/tools
113-
python get.py
113+
python3 get.py
114114
115115
- Restart Arduino
116116
@@ -184,7 +184,7 @@ Instructions - Other OS
184184
.. code:: bash
185185
186186
cd esp8266/tools
187-
python get.py
187+
python3 get.py
188188
189189
- Restart Arduino
190190

doc/ota_updates/readme.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Instructions below show configuration of OTA on NodeMCU 1.0 (ESP-12E Module) boa
193193
- esp8266/Arduino platform package 2.0.0 or newer - for instructions
194194
follow
195195
https://github.com/esp8266/Arduino#installing-with-boards-manager
196-
- Python 2.7 - https://www.python.org/
196+
- Python 3.x - https://www.python.org/
197197

198198
**Note:** Windows users should select “Add python.exe to Path”
199199
(see below – this option is not selected by default).

doc/reference.rst

+72
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,75 @@ using FPSTR would become...
215215
String response2;
216216
response2 += FPSTR(HTTP);
217217
}
218+
219+
C++
220+
----
221+
222+
- About C++ exceptions, ``operator new``, and Exceptions menu option
223+
224+
The C++ standard says the following about the ``new`` operator behavior when encountering heap shortage (memory full):
225+
226+
- has to throw a ``std::bad_alloc`` C++ exception when they are enabled
227+
228+
- will ``abort()`` otherwise
229+
230+
There are several reasons for the first point above, among which are:
231+
232+
- guarantee that the return of new is never a ``nullptr``
233+
234+
- guarantee full construction of the top level object plus all member subobjects
235+
236+
- guarantee that any subobjects partially constructed get destroyed, and in the correct order, if oom is encountered midway through construction
237+
238+
When C++ exceptions are disabled, or when using ``new(nothrow)``, the above guarantees can't be upheld, so the second point (``abort()``) above is the only ``std::c++`` viable solution.
239+
240+
Historically in Arduino environments, ``new`` is overloaded to simply return the equivalent ``malloc()`` which in turn can return ``nullptr``.
241+
242+
This behavior is not C++ standard, and there is good reason for that: there are hidden and very bad side effects. The *class and member constructors are always called, even when memory is full* (``this == nullptr``).
243+
In addition, the memory allocation for the top object could succeed, but allocation required for some member object could fail, leaving construction in an undefined state.
244+
So the historical behavior of Ardudino's ``new``, when faced with insufficient memory, will lead to bad crashes sooner or later, sometimes unexplainable, generally due to memory corruption even when the returned value is checked and managed.
245+
Luckily on esp8266, trying to update RAM near address 0 will immediately raise an hardware exception, unlike on other uC like avr on which that memory can be accessible.
246+
247+
As of core 2.6.0, there are 3 options: legacy (default) and two clear cases when ``new`` encounters oom:
248+
249+
- ``new`` returns ``nullptr``, with possible bad effects or immediate crash when constructors (called anyway) initialize members (exceptions are disabled in this case)
250+
251+
- C++ exceptions are disabled: ``new`` calls ``abort()`` and will "cleanly" crash, because there is no way to honor memory allocation or to recover gracefully.
252+
253+
- C++ exceptions are enabled: ``new`` throws a ``std::bad_alloc`` C++ exception, which can be caught and handled gracefully.
254+
This assures correct behavior, including handling of all subobjects, which guarantees stability.
255+
256+
History: `#6269 <https://github.com/esp8266/Arduino/issues/6269>`__ `#6309 <https://github.com/esp8266/Arduino/pull/6309>`__ `#6312 <https://github.com/esp8266/Arduino/pull/6312>`__
257+
258+
- New optional allocator ``arduino_new``
259+
260+
A new optional global allocator is introduced with a different semantic:
261+
262+
- never throws exceptions on oom
263+
264+
- never calls constructors on oom
265+
266+
- returns nullptr on oom
267+
268+
It is similar to arduino ``new`` semantic without side effects
269+
(except when parent constructors, or member constructors use ``new``).
270+
271+
Syntax is slightly different, the following shows the different usages:
272+
273+
.. code:: cpp
274+
275+
// with new:
276+
277+
SomeClass* sc = new SomeClass(arg1, arg2, ...);
278+
delete sc;
279+
280+
SomeClass* scs = new SomeClass[42];
281+
delete [] scs;
282+
283+
// with arduino_new:
284+
285+
SomeClass* sc = arduino_new(SomeClass, arg1, arg2, ...);
286+
delete sc;
287+
288+
SomeClass* scs = arduino_newarray(SomeClass, 42);
289+
delete [] scs;

libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
#include <ESP8266HTTPClient.h>
1414

1515
#include <WiFiClientSecureBearSSL.h>
16-
// Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date
17-
const uint8_t fingerprint[20] = {0x5A, 0xCF, 0xFE, 0xF0, 0xF1, 0xA6, 0xF4, 0x5F, 0xD2, 0x11, 0x11, 0xC6, 0x1D, 0x2F, 0x0E, 0xBC, 0x39, 0x8D, 0x50, 0xE0};
16+
// Fingerprint for demo URL, expires on June 2, 2021, needs to be updated well before this date
17+
const uint8_t fingerprint[20] = {0x40, 0xaf, 0x00, 0x6b, 0xec, 0x90, 0x22, 0x41, 0x8e, 0xa3, 0xad, 0xfa, 0x1a, 0xe8, 0x25, 0x41, 0x1d, 0x1a, 0x54, 0xb3};
1818

1919
ESP8266WiFiMulti WiFiMulti;
2020

libraries/ESP8266WiFi/examples/BearSSL_CertStore/certs-from-mozilla.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/python
1+
#!/usr/bin/python3
22

33
# This script pulls the list of Mozilla trusted certificate authorities
44
# from the web at the "mozurl" below, parses the file to grab the PEM
@@ -7,16 +7,18 @@
77
# and use them for your outgoing SSL connections.
88
#
99
# Script by Earle F. Philhower, III. Released to the public domain.
10-
10+
from __future__ import print_function
1111
import csv
1212
import os
13+
import sys
1314
from subprocess import Popen, PIPE, call
14-
import urllib2
1515
try:
16-
# for Python 2.x
16+
from urllib.request import urlopen
17+
except:
18+
from urllib2 import urlopen
19+
try:
1720
from StringIO import StringIO
18-
except ImportError:
19-
# for Python 3.x
21+
except:
2022
from io import StringIO
2123

2224
# Mozilla's URL for the CSV file with included PEM certs
@@ -25,9 +27,12 @@
2527
# Load the manes[] and pems[] array from the URL
2628
names = []
2729
pems = []
28-
response = urllib2.urlopen(mozurl)
30+
response = urlopen(mozurl)
2931
csvData = response.read()
30-
csvReader = csv.reader(StringIO(csvData))
32+
if sys.version_info[0] > 2:
33+
csvData = csvData.decode('utf-8')
34+
csvFile = StringIO(csvData)
35+
csvReader = csv.reader(csvFile)
3136
for row in csvReader:
3237
names.append(row[0]+":"+row[1]+":"+row[2])
3338
pems.append(row[30])
@@ -46,10 +51,10 @@
4651
for i in range(0, len(pems)):
4752
certName = "data/ca_%03d.der" % (idx);
4853
thisPem = pems[i].replace("'", "")
49-
print names[i] + " -> " + certName
54+
print(names[i] + " -> " + certName)
5055
ssl = Popen(['openssl','x509','-inform','PEM','-outform','DER','-out', certName], shell = False, stdin = PIPE)
5156
pipe = ssl.stdin
52-
pipe.write(thisPem)
57+
pipe.write(thisPem.encode('utf-8'))
5358
pipe.close()
5459
ssl.wait()
5560
if os.path.exists(certName):

0 commit comments

Comments
 (0)