8000 Merge branch 'master' into poc-cache-config · esp8266/Arduino@d5dac93 · GitHub
[go: up one dir, main page]

Skip to content

Commit d5dac93

Browse files
authored
Merge branch 'master' into poc-cache-config
2 parents 69fdd5b + 6cb1699 commit d5dac93

File tree

15 files changed

+162
-41
lines changed

15 files changed

+162
-41
lines changed

cores/esp8266/Esp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,12 @@ uint8_t EspClass::getBootMode(void)
266266
return system_get_boot_mode();
267267
}
268268

269+
#ifndef F_CPU
269270
uint8_t EspClass::getCpuFreqMHz(void)
270271
{
271272
return system_get_cpu_freq();
272273
}
273-
274+
#endif
274275

275276
uint32_t EspClass::getFlashChipId(void)
276277
{

cores/esp8266/Esp.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ class EspClass {
157157
uint8_t getBootVersion();
158158
uint8_t getBootMode();
159159

160+
#if defined(F_CPU) || defined(CORE_MOCK)
161+
constexpr uint8_t getCpuFreqMHz() const
162+
{
163+
return clockCyclesPerMicrosecond();
164+
}
165+
#else
160166
uint8_t getCpuFreqMHz();
167+
#endif
161168

162169
uint32_t getFlashChipId();
163170
uint8_t getFlashChipVendorId();
@@ -212,6 +219,7 @@ class EspClass {
212219
};
213220

214221
#ifndef CORE_MOCK
222+
215223
uint32_t EspClass::getCycleCount()
216224
{
217225
return esp_get_cycle_count();

cores/esp8266/MD5Builder.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include <Arduino.h>
22
#include <MD5Builder.h>
3+
#include <memory>
34

4-
uint8_t hex_char_to_byte(uint8_t c){
5-
return (c >= 'a' && c <= 'f') ? (c - ((uint8_t)'a' - 0xa)) :
6-
(c >= 'A' && c <= 'F') ? (c - ((uint8_t)'A' - 0xA)) :
7-
(c >= '0' && c<= '9') ? (c - (uint8_t)'0') : 0;
5+
uint8_t hex_char_to_byte(uint8_t c) {
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;
89
}
910

1011
void MD5Builder::begin(void){
@@ -18,25 +19,27 @@ void MD5Builder::add(const uint8_t * data, const uint16_t len){
1819

1920
void MD5Builder::addHexString(const char * data){
2021
uint16_t i, len = strlen(data);
21-
uint8_t * tmp = (uint8_t*)malloc(len/2);
22-
if(tmp == NULL) {
22+
auto tmp = std::unique_ptr<uint8_t[]>{new(std::nothrow) uint8_t[len / 2]};
23+
24+
if (!tmp) {
2325
return;
2426
}
27+
2528
for(i=0; i<len; i+=2) {
2629
uint8_t high = hex_char_to_byte(data[i]);
2730
uint8_t low = hex_char_to_byte(data[i+1]);
2831
tmp[i/2] = (high & 0x0F) << 4 | (low & 0x0F);
2932
}
30-
add(tmp, len/2);
31-
free(tmp);
33+
add(tmp.get(), len/2);
3234
}
3335

34-
bool MD5Builder::addStream(Stream & stream, const size_t maxLen){
36+
bool MD5Builder::addStream(Stream &stream, const size_t maxLen) {
3537
const int buf_size = 512;
3638
int maxLengthLeft = maxLen;
37-
uint8_t * buf = (uint8_t*) malloc(buf_size);
3839

39-
if(!buf) {
40+
auto buf = std::unique_ptr<uint8_t[]>{new(std::nothrow) uint8_t[buf_size]};
41+
42+
if (!buf) {
4043
return false;
4144
}
4245

@@ -45,47 +48,47 @@ bool MD5Builder::addStream(Stream & stream, const size_t maxLen){
4548

4649
// determine number of bytes to read
4750
int readBytes = bytesAvailable;
48-
if(readBytes > maxLengthLeft) {
49-
readBytes = maxLengthLeft ; // read only until max_len
51+
if (readBytes > maxLengthLeft){
52+
readBytes = maxLengthLeft; // read only until max_len
5053
}
51-
if(readBytes > buf_size) {
54+
if (readBytes > buf_size){
5255
readBytes = buf_size; // not read more the buffer can handle
5356
}
5457

5558
// read data and check if we got something
56-
int numBytesRead = stream.readBytes(buf, readBytes);
57-
if(numBytesRead< 1) {
59+
int numBytesRead = stream.readBytes(buf.get(), readBytes);
60+
if (numBytesRead < 1) {
5861
return false;
5962
}
6063

6164
// Update MD5 with buffer payload
62-
MD5Update(&_ctx, buf, numBytesRead);
65+
MD5Update(&_ctx, buf.get(), numBytesRead);
6366

6467
yield(); // time for network streams
6568

6669
// update available number of bytes
6770
maxLengthLeft -= numBytesRead;
6871
bytesAvailable = stream.available();
6972
}
70-
free(buf);
73+
7174
return true;
7275
}
7376

7477
void MD5Builder::calculate(void){
7578
MD5Final(_buf, &_ctx);
7679
}
7780

78-
void MD5Builder::getBytes(uint8_t * output){
81+
void MD5Builder::getBytes(uint8_t * output) const {
7982
memcpy(output, _buf, 16);
8083
}
8184

82-
void MD5Builder::getChars(char * output){
83-
for(uint8_t i = 0; i < 16; i++) {
85+
void MD5Builder::getChars(char * output) const {
86+
for (uint8_t i=0; i<16; i++){
8487
sprintf(output + (i * 2), "%02x", _buf[i]);
8588
}
8689
}
8790

88-
String MD5Builder::toString(void){
91+
String MD5Builder::toString(void) const {
8992
char out[33];
9093
getChars(out);
9194
return String(out);

cores/esp8266/MD5Builder.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ class MD5Builder {
4040
void addHexString(const String& data){ addHexString(data.c_str()); }
4141
bool addStream(Stream & stream, const size_t maxLen);
4242
void calculate(void);
43-
void getBytes(uint8_t * output);
44-
void getChars(char * output);
45-
String toString(void);
43+
void getBytes(uint8_t * output) const;
44+
void getChars(char * output) const;
45+
String toString(void) const;
4646
};
4747

4848

cores/esp8266/PolledTimeout.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct TimeSourceCycles
7676

7777
using timeType = decltype(ESP.getCycleCount());
7878
static timeType time() {return ESP.getCycleCount();}
79-
static constexpr timeType ticksPerSecond = F_CPU; // 80'000'000 or 160'000'000 Hz
79+
static constexpr timeType ticksPerSecond = ESP.getCpuFreqMHz() * 1000000UL; // 80'000'000 or 160'000'000 Hz
8080
static constexpr timeType ticksPerSecondMax = 160000000; // 160MHz
8181
};
8282

cores/esp8266/core_esp8266_main.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,25 @@ void initVariant() __attribute__((weak));
8484
void initVariant() {
8585
}
8686

87-
void preloop_update_frequency() __attribute__((weak));
88-
void preloop_update_frequency() {
87+
extern "C" void __preloop_update_frequency() {
8988
#if defined(F_CPU) && (F_CPU == 160000000L)
90-
REG_SET_BIT(0x3ff00014, BIT(0));
9189
ets_update_cpu_frequency(160);
90+
CPU2X |= 1UL;
91+
#elif defined(F_CPU)
92+
ets_update_cpu_frequency(80);
93+
CPU2X &= ~1UL;
94+
#elif !defined(F_CPU)
95+
if (system_get_cpu_freq() == 160) {
96+
CPU2X |= 1UL;
97+
}
98+
else {
99+
CPU2X &= ~1UL;
100+
}
92101
#endif
93102
}
94103

104+
extern "C" void preloop_update_frequency() __attribute__((weak, alias("__preloop_update_frequency")));
105+
95106
extern "C" bool can_yield() {
96107
return cont_can_yield(g_pcont);
97108
}

libraries/ESP8266WiFi/examples/IPv6/IPv6.ino

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
/*
32
arduino IPv6 example
43
released to public domain
@@ -27,7 +26,8 @@
2726
#define STAPSK "your-password"
2827
#endif
2928

30-
#define FQDN F("www.google.com") // with both IPv4 & IPv6 addresses
29+
#define FQDN F("www.google.com") // with both IPv4 & IPv6 addresses
30+
#define FQDN2 F("www.yahoo.com") // with both IPv4 & IPv6 addresses
3131
#define FQDN6 F("ipv6.google.com") // does not resolve in IPv4
3232
#define STATUSDELAY_MS 10000
3333
#define TCP_PORT 23
@@ -50,6 +50,21 @@ void fqdn(Print& out, const String& fqdn) {
5050
}
5151
}
5252

53+
#if LWIP_IPV4 && LWIP_IPV6
54+
void fqdn_rt(Print& out, const String& fqdn, DNSResolveType resolveType) {
55+
out.print(F("resolving "));
56+
out.print(fqdn);
57+
out.print(F(": "));
58+
IPAddress result;
59+
if (WiFi.hostByName(fqdn.c_str(), result, 10000, resolveType)) {
60+
result.printTo(out);
61+
out.println();
62+
} else {
63+
out.println(F("timeout or not found"));
64+
}
65+
}
66+
#endif
67+
5368
void status(Print& out) {
5469
out.println(F("------------------------------"));
5570
out.println(ESP.getFullVersion());
@@ -85,7 +100,10 @@ void status(Print& out) {
85100
// an example is provided with a fqdn which does not resolve with IPv4
86101
fqdn(out, FQDN);
87102
fqdn(out, FQDN6);
88-
103+
#if LWIP_IPV4 && LWIP_IPV6
104+
fqdn_rt(out, FQDN, DNSResolveType::DNS_AddrType_IPv4_IPv6); // IPv4 before IPv6
105+
fqdn_rt(out, FQDN2, DNSResolveType::DNS_AddrType_IPv6_IPv4); // IPv6 before IPv4
106+
#endif
89107
out.println(F("------------------------------"));
90108
}
91109

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
608608
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms)
609609
{
610610
ip_addr_t addr;
611-
aResult = static_cast<uint32_t>(0);
611+
aResult = static_cast<uint32_t>(INADDR_NONE);
612612

613613
if(aResult.fromString(aHostname)) {
614614
// Host name is a IP address use it!
@@ -617,7 +617,61 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
617617
}
618618

619619
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
620+
#if LWIP_IPV4 && LWIP_IPV6
621+
err_t err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult,LWIP_DNS_ADDRTYPE_DEFAULT);
622+
#else
620623
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
624+
#endif
625+
if(err == ERR_OK) {
626+
aResult = IPAddress(&addr);
627+
} else if(err == ERR_INPROGRESS) {
628+
_dns_lookup_pending = true;
629+
delay(timeout_ms);
630+
// will resume on timeout or when wifi_dns_found_callback fires
631+
_dns_lookup_pending = false;
632+
// will return here when dns_found_callback fires
633+
if(aResult.isSet()) {
634+
err = ERR_OK;
635+
}
636+
}
637+
638+
if(err != 0) {
639+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err);
640+
} else {
641+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str());
642+
}
643+
644+
return (err == ERR_OK) ? 1 : 0;
645+
}
646+
647+
#if LWIP_IPV4 && LWIP_IPV6
648+
int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms, DNSResolveType resolveType)
649+
{
650+
ip_addr_t addr;
651+
err_t err;
652+
aResult = static_cast<uint32_t>(INADDR_NONE);
653+
654+
if(aResult.fromString(aHostname)) {
655+
// Host name is a IP address use it!
656+
DEBUG_WIFI_GENERIC("[hostByName] Host: %s is a IP!\n", aHostname);
657+
return 1;
658+
}
659+
660+
DEBUG_WIFI_GENERIC("[hostByName] request IP for: %s\n", aHostname);
661+
switch(resolveType)
662+
{
663+
// Use selected addrtype
664+
case DNSResolveType::DNS_AddrType_IPv4:
665+
case DNSResolveType::DNS_AddrType_IPv6:
666+
case DNSResolveType::DNS_AddrType_IPv4_IPv6:
667+
case DNSResolveType::DNS_AddrType_IPv6_IPv4:
668+
err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult, (uint8_t) resolveType);
669+
break;
670+
default:
671+
err = dns_gethostbyname_addrtype(aHostname, &addr, &wifi_dns_found_callback, &aResult, LWIP_DNS_ADDRTYPE_DEFAULT); // If illegal type, use default.
672+
break;
673+
}
674+
621675
if(err == ERR_OK) {
622676
aResult = IPAddress(&addr);
623677
} else if(err == ERR_INPROGRESS) {
@@ -639,6 +693,7 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul
639693

640694
return (err == ERR_OK) ? 1 : 0;
641695
}
696+
#endif
642697

643698
/**
644699
* DNS callback

libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ typedef std::shared_ptr<WiFiEventHandlerOpaque> WiFiEventHandler;
4242

4343
typedef void (*WiFiEventCb)(WiFiEvent_t);
4444

45+
enum class DNSResolveType: uint8_t
46+
{
47+
DNS_AddrType_IPv4 = 0, // LWIP_DNS_ADDRTYPE_IPV4 = 0
48+
DNS_AddrType_IPv6, // LWIP_DNS_ADDRTYPE_IPV6 = 1
49+
DNS_AddrType_IPv4_IPv6, // LWIP_DNS_ADDRTYPE_IPV4_IPV6 = 2
50+
DNS_AddrType_IPv6_IPv4 // LWIP_DNS_ADDRTYPE_IPV6_IPV4 = 3
51+
};
52+
4553
struct WiFiState;
4654

4755
class ESP8266WiFiGenericClass {
@@ -113,6 +121,9 @@ class ESP8266WiFiGenericClass {
113121
public:
114122
int hostByName(const char* aHostname, IPAddress& aResult);
115123
int hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms);
124+
#if LWIP_IPV4 && LWIP_IPV6
125+
int hostByName(const char* aHostname, IPAddress& aResult, uint32_t timeout_ms, DNSResolveType resolveType);
126+
#endif
116127
bool getPersistent();
117128

118129
protected:

libraries/ESP8266WiFi/src/ESP8266WiFiGratuitous.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ bool ESP8266WiFiGratuitous::stationKeepAliveSetIntervalMs (uint32_t ms)
7272
if (_timer)
7373
{
7474
os_timer_disarm(_timer);
75+
free(_timer);
7576
_timer = nullptr;
7677
}
7778

libraries/ESP8266WiFi/src/include/ClientContext.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,14 @@ class ClientContext
127127
}
128128
}
129129

130-
int connect(CONST ip_addr_t* addr, uint16_t port)
130+
int connect(ip_addr_t* addr, uint16_t port)
131131
{
132+
#if LWIP_IPV6
133+
// Set zone so that link local addresses use the default interface
134+
if (IP_IS_V6(addr) && ip6_addr_lacks_zone(ip_2_ip6(addr), IP6_UNKNOWN)) {
135+
ip6_addr_assign_zone(ip_2_ip6(addr), IP6_UNKNOWN, netif_default);
136+
}
137+
#endif
132138
err_t err = tcp_connect(_pcb, addr, port, &ClientContext::_s_connected);
133139
if (err != ERR_OK) {
134140
return 0;

libraries/ESP8266WiFi/src/include/UdpContext.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ class UdpContext
114114
{
115115
_pcb->remote_ip = addr;
116116
_pcb->remote_port = port;
117+
#if LWIP_IPV6
118+
// Set zone so that link local addresses use the default interface
119+
if (IP_IS_V6(&_pcb->remote_ip) && ip6_addr_lacks_zone(ip_2_ip6(&_pcb->remote_ip), IP6_UNKNOWN)) {
120+
ip6_addr_assign_zone(ip_2_ip6(&_pcb->remote_ip), IP6_UNKNOWN, netif_default);
121+
}
122+
#endif
117123
return true;
118124
}
119125

0 commit comments

Comments
 (0)
0