diff --git a/cores/esp8266/Esp.cpp b/cores/esp8266/Esp.cpp index 41d0592f31..2ee4a65b5e 100644 --- a/cores/esp8266/Esp.cpp +++ b/cores/esp8266/Esp.cpp @@ -115,20 +115,18 @@ void EspClass::wdtFeed(void) system_soft_wdt_feed(); } -extern "C" void esp_yield(); - void EspClass::deepSleep(uint64_t time_us, WakeMode mode) { system_deep_sleep_set_option(static_cast(mode)); system_deep_sleep(time_us); - esp_yield(); + esp_suspend(); } void EspClass::deepSleepInstant(uint64_t time_us, WakeMode mode) { system_deep_sleep_set_option(static_cast(mode)); system_deep_sleep_instant(time_us); - esp_yield(); + esp_suspend(); } //this calculation was taken verbatim from the SDK api reference for SDK 2.1.0. @@ -200,7 +198,7 @@ void EspClass::reset(void) void EspClass::restart(void) { system_restart(); - esp_yield(); + esp_suspend(); } [[noreturn]] void EspClass::rebootIntoUartDownloadMode() diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 88b47b33f3..4896e27758 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -143,7 +143,7 @@ unsigned long HardwareSerial::detectBaudrate(time_t timeoutMillis) if ((detectedBaudrate = testBaudrate())) { break; } - delay(100); + esp_delay(100); } return detectedBaudrate; } diff --git a/cores/esp8266/LwipIntfDev.h b/cores/esp8266/LwipIntfDev.h index 3354a32563..895e1eb31e 100644 --- a/cores/esp8266/LwipIntfDev.h +++ b/cores/esp8266/LwipIntfDev.h @@ -64,7 +64,7 @@ class LwipIntfDev: public LwipIntf, public RawDev return IPAddress(ip4_addr_get_u32(ip_2_ip4(&_netif.gw))); } - void setDefault(); + void setDefault(bool deflt = true); bool connected() { @@ -187,10 +187,10 @@ boolean LwipIntfDev::begin(const uint8_t* macAddress, const uint16_t mtu return false; } - _netif.flags |= NETIF_FLAG_UP; - if (localIP().v4() == 0) { + // IP not set, starting DHCP + _netif.flags |= NETIF_FLAG_UP; switch (dhcp_start(&_netif)) { case ERR_OK: @@ -204,6 +204,12 @@ boolean LwipIntfDev::begin(const uint8_t* macAddress, const uint16_t mtu return false; } } + else + { + // IP is set, static config + netif_set_link_up(&_netif); + netif_set_up(&_netif); + } _started = true; @@ -386,9 +392,9 @@ err_t LwipIntfDev::handlePackets() } template -void LwipIntfDev::setDefault() +void LwipIntfDev::setDefault(bool deflt) { - _default = true; + _default = deflt; if (connected()) { netif_set_default(&_netif); diff --git a/cores/esp8266/PolledTimeout.h b/cores/esp8266/PolledTimeout.h index 7eec547e48..1252ce6c29 100644 --- a/cores/esp8266/PolledTimeout.h +++ b/cores/esp8266/PolledTimeout.h @@ -27,6 +27,7 @@ #include // std::numeric_limits #include // std::is_unsigned #include +#include namespace esp8266 { @@ -45,7 +46,7 @@ struct DoNothing struct YieldOrSkip { - static void execute() {delay(0);} + static void execute() {esp_yield();} }; template diff --git a/cores/esp8266/Schedule.cpp b/cores/esp8266/Schedule.cpp index b1230ed675..279e78ff6c 100644 --- a/cores/esp8266/Schedule.cpp +++ b/cores/esp8266/Schedule.cpp @@ -135,8 +135,6 @@ bool schedule_recurrent_function_us(const std::function& fn, void run_scheduled_functions() { - esp8266::polledTimeout::periodicFastMs yieldNow(100); // yield every 100ms - // prevent scheduling of new functions during this run auto stop = sLast; bool done = false; @@ -161,13 +159,10 @@ void run_scheduled_functions() recycle_fn_unsafe(to_recycle); } - if (yieldNow) - { - // because scheduled functions might last too long for watchdog etc, - // this is yield() in cont stack: - esp_schedule(); - cont_yield(g_pcont); - } + // scheduled functions might last too long for watchdog etc. + // yield() is allowed in scheduled functions, therefore + // recursion into run_scheduled_recurrent_functions() is permitted + optimistic_yield(100000); } } @@ -241,9 +236,10 @@ void run_scheduled_recurrent_functions() if (yieldNow) { // because scheduled functions might last too long for watchdog etc, - // this is yield() in cont stack: + // this is yield() in cont stack, but need to call cont_suspend directly + // to prevent recursion into run_scheduled_recurrent_functions() esp_schedule(); - cont_yield(g_pcont); + cont_suspend(g_pcont); } } while (current && !done); diff --git a/cores/esp8266/cont.S b/cores/esp8266/cont.S index 00ca017785..4832b39170 100644 --- a/cores/esp8266/cont.S +++ b/cores/esp8266/cont.S @@ -21,9 +21,9 @@ .section .irom0.text .align 4 .literal_position - .global cont_yield - .type cont_yield, @function -cont_yield: + .global cont_suspend + .type cont_suspend, @function +cont_suspend: /* a1: sp */ /* a2: void* cont_ctx */ /* adjust stack and save registers */ @@ -35,10 +35,10 @@ cont_yield: s32i a0, a1, 16 s32i a2, a1, 20 - /* &cont_continue -> cont_ctx.pc_yield */ + /* &cont_continue -> cont_ctx.pc_suspend */ movi a3, cont_continue s32i a3, a2, 8 - /* sp -> cont_ctx.sp_yield */ + /* sp -> cont_ctx.sp_suspend */ s32i a1, a2, 12 /* a0 <- cont_ctx.pc_ret */ @@ -56,7 +56,7 @@ cont_continue: l32i a2, a1, 20 addi a1, a1, 24 ret - .size cont_yield, . - cont_yield + .size cont_suspend, . - cont_suspend //////////////////////////////////////////////////// @@ -108,7 +108,7 @@ cont_run: /* sp -> cont_ctx.sp_ret */ s32i a1, a2, 4 - /* if cont_ctx.pc_yield != 0, goto cont_resume */ + /* if cont_ctx.pc_suspend != 0, goto cont_resume */ l32i a4, a2, 8 bnez a4, cont_resume /* else */ @@ -119,12 +119,12 @@ cont_run: jx a2 cont_resume: - /* a1 <- cont_ctx.sp_yield */ + /* a1 <- cont_ctx.sp_suspend */ l32i a1, a2, 12 - /* reset yield flag, 0 -> cont_ctx.pc_yield */ + /* reset yield flag, 0 -> cont_ctx.pc_suspend */ movi a3, 0 s32i a3, a2, 8 - /* jump to saved cont_ctx.pc_yield */ + /* jump to saved cont_ctx.pc_suspend */ movi a0, cont_ret jx a4 diff --git a/cores/esp8266/cont.h b/cores/esp8266/cont.h index 21ecad2806..6932416b68 100644 --- a/cores/esp8266/cont.h +++ b/cores/esp8266/cont.h @@ -35,8 +35,8 @@ typedef struct cont_ { void (*pc_ret)(void); unsigned* sp_ret; - void (*pc_yield)(void); - unsigned* sp_yield; + void (*pc_suspend)(void); + unsigned* sp_suspend; unsigned* stack_end; unsigned unused1; @@ -55,12 +55,12 @@ extern cont_t* g_pcont; void cont_init(cont_t*); // Run function pfn in a separate stack, or continue execution -// at the point where cont_yield was called +// at the point where cont_suspend was called void cont_run(cont_t*, void (*pfn)(void)); // Return to the point where cont_run was called, saving the // execution state (registers and stack) -void cont_yield(cont_t*); +void cont_suspend(cont_t*); // Check guard bytes around the stack. Return 0 in case everything is ok, // return 1 if guard bytes were overwritten. @@ -70,9 +70,9 @@ int cont_check(cont_t* cont); // and thus weren't used by the user code. i.e. that stack space is free. (high water mark) int cont_get_free_stack(cont_t* cont); -// Check if yield() may be called. Returns true if we are running inside +// Check if cont_suspend() may be called. Returns true if we are running inside // continuation stack -bool cont_can_yield(cont_t* cont); +bool cont_can_suspend(cont_t* cont); // Repaint the stack from the current SP to the end, to allow individual // routines' stack usages to be calculated by re-painting, checking current diff --git a/cores/esp8266/cont_util.cpp b/cores/esp8266/cont_util.cpp index 8fa4421802..fd72ee5d18 100644 --- a/cores/esp8266/cont_util.cpp +++ b/cores/esp8266/cont_util.cpp @@ -62,9 +62,9 @@ int cont_get_free_stack(cont_t* cont) { return freeWords * 4; } -bool IRAM_ATTR cont_can_yield(cont_t* cont) { +bool IRAM_ATTR cont_can_suspend(cont_t* cont) { return !ETS_INTR_WITHINISR() && - cont->pc_ret != 0 && cont->pc_yield == 0; + cont->pc_ret != 0 && cont->pc_suspend == 0; } // No need for this to be in IRAM, not expected to be IRQ called diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index ff95d08cfb..a4be973191 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -62,7 +62,7 @@ cont_t* g_pcont __attribute__((section(".noinit"))); static os_event_t s_loop_queue[LOOP_QUEUE_SIZE]; /* Used to implement optimistic_yield */ -static uint32_t s_cycles_at_yield_start; +static uint32_t s_cycles_at_resume; /* For ets_intr_lock_nest / ets_intr_unlock_nest * Max nesting seen by SDK so far is 2. @@ -80,6 +80,10 @@ const char* core_release = #else NULL; #endif + +static os_timer_t delay_timer; +#define ONCE 0 +#define REPEAT 1 } // extern "C" void initVariant() __attribute__((weak)); @@ -106,32 +110,73 @@ extern "C" void __preloop_update_frequency() { extern "C" void preloop_update_frequency() __attribute__((weak, alias("__preloop_update_frequency"))); extern "C" bool can_yield() { - return cont_can_yield(g_pcont); + return cont_can_suspend(g_pcont); } -static inline void esp_yield_within_cont() __attribute__((always_inline)); -static void esp_yield_within_cont() { - cont_yield(g_pcont); - s_cycles_at_yield_start = ESP.getCycleCount(); +static inline void esp_suspend_within_cont() __attribute__((always_inline)); +static void esp_suspend_within_cont() { + cont_suspend(g_pcont); + s_cycles_at_resume = ESP.getCycleCount(); run_scheduled_recurrent_functions(); } -extern "C" void __esp_yield() { - if (can_yield()) { - esp_yield_within_cont(); +extern "C" void __esp_suspend() { + if (cont_can_suspend(g_pcont)) { + esp_suspend_within_cont(); } } -extern "C" void esp_yield() __attribute__ ((weak, alias("__esp_yield"))); +extern "C" void esp_suspend() __attribute__ ((weak, alias("__esp_suspend"))); extern "C" IRAM_ATTR void esp_schedule() { ets_post(LOOP_TASK_PRIORITY, 0, 0); } +// Replacement for delay(0). In CONT, same as yield(). Whereas yield() panics +// in SYS, esp_yield() is safe to call and only schedules CONT. Use yield() +// whereever only called from CONT, use esp_yield() if code is called from SYS +// or both CONT and SYS. +extern "C" void esp_yield() { + esp_schedule(); + esp_suspend(); +} + +void delay_end(void* arg) { + (void)arg; + esp_schedule(); +} + +extern "C" void __esp_delay(unsigned long ms) { + if (ms) { + os_timer_setfn(&delay_timer, (os_timer_func_t*)&delay_end, 0); + os_timer_arm(&delay_timer, ms, ONCE); + } + else { + esp_schedule(); + } + esp_suspend(); + if (ms) { + os_timer_disarm(&delay_timer); + } +} + +extern "C" void esp_delay(unsigned long ms) __attribute__((weak, alias("__esp_delay"))); + +bool try_esp_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms) { + decltype(millis()) expired; + + if ((expired = millis() - start_ms) >= timeout_ms) { + return true; + } + const auto remaining = timeout_ms - expired; + esp_delay(remaining <= intvl_ms ? remaining : intvl_ms); + return false; +} + extern "C" void __yield() { - if (can_yield()) { + if (cont_can_suspend(g_pcont)) { esp_schedule(); - esp_yield_within_cont(); + esp_suspend_within_cont(); } else { panic(); @@ -140,6 +185,9 @@ extern "C" void __yield() { extern "C" void yield(void) __attribute__ ((weak, alias("__yield"))); +// In CONT, actually performs yield() only once the given time interval +// has elapsed since the last time yield() occured. Whereas yield() panics +// in SYS, optimistic_yield() additionally is safe to call and does nothing. extern "C" void optimistic_yield(uint32_t interval_us) { const uint32_t intvl_cycles = interval_us * #if defined(F_CPU) @@ -147,7 +195,7 @@ extern "C" void optimistic_yield(uint32_t interval_us) { #else ESP.getCpuFreqMHz(); #endif - if ((ESP.getCycleCount() - s_cycles_at_yield_start) > intvl_cycles && + if ((ESP.getCycleCount() - s_cycles_at_resume) > intvl_cycles && can_yield()) { yield(); @@ -207,7 +255,7 @@ static void loop_wrapper() { static void loop_task(os_event_t *events) { (void) events; - s_cycles_at_yield_start = ESP.getCycleCount(); + s_cycles_at_resume = ESP.getCycleCount(); ESP.resetHeap(); cont_run(g_pcont, &loop_wrapper); ESP.setDramHeap(); @@ -215,8 +263,8 @@ static void loop_task(os_event_t *events) { panic(); } } -extern "C" { +extern "C" { struct object { long placeholder[ 10 ]; }; void __register_frame_info (const void *begin, struct object *ob); extern char __eh_frame[]; @@ -253,7 +301,6 @@ static void __unhandled_exception_cpp() } #endif } - } void init_done() { diff --git a/cores/esp8266/core_esp8266_postmortem.cpp b/cores/esp8266/core_esp8266_postmortem.cpp index 3e75342d05..2279401b54 100644 --- a/cores/esp8266/core_esp8266_postmortem.cpp +++ b/cores/esp8266/core_esp8266_postmortem.cpp @@ -245,12 +245,12 @@ static void print_stack(uint32_t start, uint32_t end) { } } -static void uart_write_char_d(char c) { +static void IRAM_ATTR uart_write_char_d(char c) { uart0_write_char_d(c); uart1_write_char_d(c); } -static void uart0_write_char_d(char c) { +static void IRAM_ATTR uart0_write_char_d(char c) { while (((USS(0) >> USTXC) & 0xff)) { } if (c == '\n') { @@ -259,7 +259,7 @@ static void uart0_write_char_d(char c) { USF(0) = c; } -static void uart1_write_char_d(char c) { +static void IRAM_ATTR uart1_write_char_d(char c) { while (((USS(1) >> USTXC) & 0xff) >= 0x7e) { } if (c == '\n') { diff --git a/cores/esp8266/core_esp8266_waveform_phase.cpp b/cores/esp8266/core_esp8266_waveform_phase.cpp index de2f914121..873dadede0 100644 --- a/cores/esp8266/core_esp8266_waveform_phase.cpp +++ b/cores/esp8266/core_esp8266_waveform_phase.cpp @@ -211,7 +211,7 @@ int startWaveformClockCycles_weak(uint8_t pin, uint32_t highCcys, uint32_t lowCc } std::atomic_thread_fence(std::memory_order_acq_rel); while (waveform.toSetBits) { - delay(0); // Wait for waveform to update + yield(); // Wait for waveform to update std::atomic_thread_fence(std::memory_order_acquire); } return true; diff --git a/cores/esp8266/core_esp8266_waveform_pwm.cpp b/cores/esp8266/core_esp8266_waveform_pwm.cpp index 916361e2ca..65e75a0c71 100644 --- a/cores/esp8266/core_esp8266_waveform_pwm.cpp +++ b/cores/esp8266/core_esp8266_waveform_pwm.cpp @@ -40,6 +40,7 @@ #include +#include #include "ets_sys.h" #include "core_esp8266_waveform.h" #include "user_interface.h" @@ -162,7 +163,7 @@ static IRAM_ATTR void _notifyPWM(PWMState *p, bool idle) { forceTimerInterrupt(); while (pwmState.pwmUpdate) { if (idle) { - delay(0); + esp_yield(); } MEMBARRIER(); } @@ -372,7 +373,7 @@ int startWaveformClockCycles_weak(uint8_t pin, uint32_t timeHighCycles, uint32_t if (wvfState.waveformEnabled & mask) { // Make sure no waveform changes are waiting to be applied while (wvfState.waveformToChange) { - delay(0); // Wait for waveform to update + yield(); // Wait for waveform to update // No mem barrier here, the call to a global function implies global state updated } wvfState.waveformNewHigh = timeHighCycles; @@ -392,7 +393,7 @@ int startWaveformClockCycles_weak(uint8_t pin, uint32_t timeHighCycles, uint32_t initTimer(); forceTimerInterrupt(); while (wvfState.waveformToEnable) { - delay(0); // Wait for waveform to update + yield(); // Wait for waveform to update // No mem barrier here, the call to a global function implies global state updated } } diff --git a/cores/esp8266/core_esp8266_wiring.cpp b/cores/esp8266/core_esp8266_wiring.cpp index b956cebe43..40a996d560 100644 --- a/cores/esp8266/core_esp8266_wiring.cpp +++ b/cores/esp8266/core_esp8266_wiring.cpp @@ -23,37 +23,18 @@ #include "ets_sys.h" #include "osapi.h" #include "user_interface.h" -#include "cont.h" +#include "coredecls.h" extern "C" { -extern void ets_delay_us(uint32_t us); -extern void esp_schedule(); -extern void esp_yield(); - -static os_timer_t delay_timer; static os_timer_t micros_overflow_timer; static uint32_t micros_at_last_overflow_tick = 0; static uint32_t micros_overflow_count = 0; #define ONCE 0 #define REPEAT 1 -void delay_end(void* arg) { - (void) arg; - esp_schedule(); -} - void __delay(unsigned long ms) { - if(ms) { - os_timer_setfn(&delay_timer, (os_timer_func_t*) &delay_end, 0); - os_timer_arm(&delay_timer, ms, ONCE); - } else { - esp_schedule(); - } - esp_yield(); - if(ms) { - os_timer_disarm(&delay_timer); - } + esp_delay(ms); } void delay(unsigned long ms) __attribute__ ((weak, alias("__delay"))); diff --git a/cores/esp8266/coredecls.h b/cores/esp8266/coredecls.h index b9c771df77..e1edf8b927 100644 --- a/cores/esp8266/coredecls.h +++ b/cores/esp8266/coredecls.h @@ -2,6 +2,10 @@ #ifndef __COREDECLS_H #define __COREDECLS_H +#define HAVE_ESP_SUSPEND 1 + +#include "core_esp8266_features.h" + #ifdef __cplusplus extern "C" { #endif @@ -13,8 +17,10 @@ extern "C" { #include // g_pcont declaration bool can_yield(); -void esp_yield(); +void esp_suspend(); +void esp_delay(unsigned long ms); void esp_schedule(); +void esp_yield(); void tune_timeshift64 (uint64_t now_us); void disable_extra4k_at_link_time (void) __attribute__((noinline)); bool sntp_set_timezone_in_seconds(int32_t timezone); @@ -32,9 +38,44 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff); using BoolCB = std::function; using TrivialCB = std::function; +void settimeofday_cb (BoolCB&& cb); void settimeofday_cb (const BoolCB& cb); void settimeofday_cb (const TrivialCB& cb); +// This overload of esp_suspend() performs the blocked callback whenever it is resumed, +// and if that returns true, it immediately suspends again. +template +inline void esp_suspend(T&& blocked) { + do { + esp_suspend(); + } while (blocked()); +} + +// Try to delay for timeout_ms relative to start_ms. Returns false if the delayed task +// is asynchronously resumed before the timeout is reached. +// Also returns false if intvl_ms have expired during the active call. +// Otherwise return true to indicate the timeout_ms have completely expired. +bool try_esp_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms); + +// This overload of esp_delay() delays for a duration of at most timeout_ms milliseconds. +// Whenever it is resumed, as well as every intvl_ms millisconds, it performs +// the blocked callback, and if that returns true, it keeps delaying for the remainder +// of the original timeout_ms period. +template +inline void esp_delay(const uint32_t timeout_ms, T&& blocked, const uint32_t intvl_ms) { + const auto start_ms = millis(); + while (!try_esp_delay(start_ms, timeout_ms, intvl_ms) && blocked()) { + } +} + +// This overload of esp_delay() delays for a duration of at most timeout_ms milliseconds. +// Whenever it is resumed, it performs the blocked callback, and if that returns true, +// it keeps delaying for the remainder of the original timeout_ms period. +template +inline void esp_delay(const uint32_t timeout_ms, T&& blocked) { + esp_delay(timeout_ms, std::forward(blocked), timeout_ms); +} + #endif // __cplusplus #endif // __COREDECLS_H diff --git a/cores/esp8266/hwdt_app_entry.cpp b/cores/esp8266/hwdt_app_entry.cpp index 3a657cfcd9..22a91a6bee 100644 --- a/cores/esp8266/hwdt_app_entry.cpp +++ b/cores/esp8266/hwdt_app_entry.cpp @@ -952,13 +952,13 @@ STATIC void IRAM_MAYBE handle_hwdt(void) { /* Print separate ctx: cont stack */ /* Check if cont stack is yielding to SYS */ - if (0 == hwdt_info.cont_integrity && 0 != g_pcont->pc_yield) { - ctx_cont_ptr = (const uint32_t *)((uintptr_t)g_pcont->sp_yield - 8u); + if (0 == hwdt_info.cont_integrity && 0 != g_pcont->pc_suspend) { + ctx_cont_ptr = (const uint32_t *)((uintptr_t)g_pcont->sp_suspend - 8u); } print_stack((uintptr_t)ctx_cont_ptr, (uintptr_t)g_pcont->stack_end, PRINT_STACK::CONT); } else { - if (0 == hwdt_info.cont_integrity && 0 != g_pcont->pc_yield) { - ETS_PRINTF("\nCont stack is yielding. Active stack starts at 0x%08X.\n", (uint32_t)g_pcont->sp_yield - 8u); + if (0 == hwdt_info.cont_integrity && 0 != g_pcont->pc_suspend) { + ETS_PRINTF("\nCont stack is yielding. Active stack starts at 0x%08X.\n", (uint32_t)g_pcont->sp_suspend - 8u); } } diff --git a/cores/esp8266/time.cpp b/cores/esp8266/time.cpp index b9489c330f..8e997c72d5 100644 --- a/cores/esp8266/time.cpp +++ b/cores/esp8266/time.cpp @@ -214,6 +214,11 @@ void settimeofday_cb (const TrivialCB& cb) _settimeofday_cb = [cb](bool sntp) { (void)sntp; cb(); }; } +void settimeofday_cb (BoolCB&& cb) +{ + _settimeofday_cb = std::move(cb); +} + void settimeofday_cb (const BoolCB& cb) { _settimeofday_cb = cb; diff --git a/cores/esp8266/uart.cpp b/cores/esp8266/uart.cpp index de574d0bee..1f657c9403 100644 --- a/cores/esp8266/uart.cpp +++ b/cores/esp8266/uart.cpp @@ -41,6 +41,7 @@ * */ #include "Arduino.h" +#include "coredecls.h" #include #include "gdb_hooks.h" #include "uart.h" @@ -493,13 +494,13 @@ uart_stop_isr(uart_t* uart) -tools/sdk/uart_register.h -cores/esp8266/esp8266_peri.h */ -inline size_t +inline __attribute__((always_inline)) size_t uart_tx_fifo_available(const int uart_nr) { return (USS(uart_nr) >> USTXC) & 0xff; } -inline bool +inline __attribute__((always_inline)) bool uart_tx_fifo_full(const int uart_nr) { return uart_tx_fifo_available(uart_nr) >= 0x7f; @@ -566,7 +567,7 @@ uart_wait_tx_empty(uart_t* uart) return; while(uart_tx_fifo_available(uart->uart_nr) > 0) - delay(0); + esp_yield(); } @@ -943,23 +944,23 @@ uart_ignore_char(char c) (void) c; } -inline void +inline __attribute__((always_inline)) void uart_write_char_delay(const int uart_nr, char c) { while(uart_tx_fifo_full(uart_nr)) - delay(0); + esp_yield(); USF(uart_nr) = c; } -static void +static void IRAM_ATTR uart0_write_char(char c) { uart_write_char_delay(0, c); } -static void +static void IRAM_ATTR uart1_write_char(char c) { uart_write_char_delay(1, c); diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index b98edcccc0..195ade0274 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -22,6 +22,7 @@ * */ #include +#include #include "ESP8266HTTPClient.h" #include @@ -567,6 +568,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) if (transferred != size) { DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] short write, asked for %d but got %d failed.\n", size, transferred); + esp_yield(); return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED); } @@ -709,7 +711,7 @@ int HTTPClient::writeToPrint(Print * print) return returnError(HTTPC_ERROR_READ_TIMEOUT); } - delay(0); + esp_yield(); } } else { return returnError(HTTPC_ERROR_ENCODING); @@ -1074,7 +1076,7 @@ int HTTPClient::handleHeaderResponse() if((millis() - lastDataTime) > _tcpTimeout) { return HTTPC_ERROR_READ_TIMEOUT; } - delay(0); + esp_yield(); } } diff --git a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h index 3357f2ac63..39608934cd 100644 --- a/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h +++ b/libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer-impl.h @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -119,7 +120,7 @@ void ESP8266HTTPUpdateServerTemplate::setup(ESP8266WebServerTemplate Update.end(); if (_serial_output) Serial.println("Update was aborted"); } - delay(0); + esp_yield(); }); } diff --git a/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino b/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino index 84a9befcf2..b546df4a7d 100644 --- a/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino +++ b/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino @@ -49,7 +49,7 @@ void loop() { (encryptionType == ENC_TYPE_NONE) ? ' ' : '*', hidden ? 'H' : 'V', ssid.c_str()); - delay(0); + yield(); } } else { Serial.printf(PSTR("WiFi scan error %d"), scanResult); diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp index 19da592c76..c66f7ad48b 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiGeneric.cpp @@ -49,10 +49,6 @@ extern "C" { #include "debug.h" #include "include/WiFiState.h" -extern "C" void esp_schedule(); -extern "C" void esp_yield(); - - // ----------------------------------------------------------------------------------------------------------------------- // ------------------------------------------------- Generic WiFi function ----------------------------------------------- // ----------------------------------------------------------------------------------------------------------------------- @@ -438,10 +434,9 @@ bool ESP8266WiFiGenericClass::mode(WiFiMode_t m) { //tasks to wait correctly. constexpr unsigned int timeoutValue = 1000; //1 second if(can_yield()) { - using oneShot = esp8266::polledTimeout::oneShotFastMs; - oneShot timeout(timeoutValue); - while(wifi_get_opmode() != (uint8) m && !timeout) - delay(5); + // The final argument, intvl_ms, to esp_delay influences how frequently + // the scheduled recurrent functions (Schedule.h) are probed. + esp_delay(timeoutValue, [m]() { return wifi_get_opmode() != m; }, 5); //if at this point mode still hasn't been reached, give up if(wifi_get_opmode() != (uint8) m) { @@ -518,9 +513,9 @@ bool ESP8266WiFiGenericClass::forceSleepBegin(uint32 sleepUs) { } wifi_fpm_set_sleep_type(MODEM_SLEEP_T); - delay(0); + esp_yield(); wifi_fpm_open(); - delay(0); + esp_yield(); auto ret = wifi_fpm_do_sleep(sleepUs); if (ret != 0) { @@ -621,22 +616,24 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul aResult = IPAddress(&addr); } else if(err == ERR_INPROGRESS) { _dns_lookup_pending = true; - delay(timeout_ms); - // will resume on timeout or when wifi_dns_found_callback fires + // Will resume on timeout or when wifi_dns_found_callback fires. + // The final argument, intvl_ms, to esp_delay influences how frequently + // the scheduled recurrent functions (Schedule.h) are probed; here, to allow + // the ethernet driver perform work. + esp_delay(timeout_ms, []() { return _dns_lookup_pending; }, 1); _dns_lookup_pending = false; - // will return here when dns_found_callback fires if(aResult.isSet()) { err = ERR_OK; } } - if(err != 0) { - DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err); - } else { + if(err == ERR_OK) { DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str()); + return 1; + } else { + DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err); + return 0; } - - return (err == ERR_OK) ? 1 : 0; } #if LWIP_IPV4 && LWIP_IPV6 @@ -671,8 +668,8 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul aResult = IPAddress(&addr); } else if(err == ERR_INPROGRESS) { _dns_lookup_pending = true; - delay(timeout_ms); // will resume on timeout or when wifi_dns_found_callback fires + esp_delay(timeout_ms, []() { return _dns_lookup_pending; }); _dns_lookup_pending = false; // will return here when dns_found_callback fires if(aResult.isSet()) { @@ -680,13 +677,13 @@ int ESP8266WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResul } } - if(err != 0) { - DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err); - } else { + if(err == ERR_OK) { DEBUG_WIFI_GENERIC("[hostByName] Host: %s IP: %s\n", aHostname, aResult.toString().c_str()); + return 1; + } else { + DEBUG_WIFI_GENERIC("[hostByName] Host: %s lookup error: %d!\n", aHostname, (int)err); + return 0; } - - return (err == ERR_OK) ? 1 : 0; } #endif @@ -705,7 +702,8 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca if(ipaddr) { (*reinterpret_cast(callback_arg)) = IPAddress(ipaddr); } - esp_schedule(); // break delay in hostByName + _dns_lookup_pending = false; // resume hostByName + esp_schedule(); } uint32_t ESP8266WiFiGenericClass::shutdownCRC (const WiFiState& state) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp index 8d74f687cb..a4d95383e5 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp @@ -25,6 +25,7 @@ #include "PolledTimeout.h" #include "ESP8266WiFiMulti.h" +#include #include #include @@ -82,37 +83,26 @@ static void printWiFiStatus(wl_status_t status) */ static wl_status_t waitWiFiConnect(uint32_t connectTimeoutMs) { - wl_status_t status; - - // Set WiFi connect timeout - using esp8266::polledTimeout::oneShotMs; - oneShotMs connectTimeout(connectTimeoutMs); - - // Wait for WiFi status change or timeout - do { - // Refresh watchdog - delay(0); - - // Get WiFi status - status = WiFi.status(); + wl_status_t status = WL_CONNECT_FAILED; + // The final argument, intvl_ms, to esp_delay influences how frequently + // the scheduled recurrent functions (Schedule.h) are probed. + esp_delay(connectTimeoutMs, + [&status]() { status = WiFi.status(); return status != WL_CONNECTED && status != WL_CONNECT_FAILED; }, 0); - // Check status - if (status == WL_CONNECTED) { - // Connected, print WiFi status - printWiFiStatus(status); - - // Return WiFi status - return status; - } else if (status == WL_CONNECT_FAILED) { - DEBUG_WIFI_MULTI("[WIFIM] Connect failed\n"); - - // Return WiFi connect failed - return WL_CONNECT_FAILED; - } - } while (!connectTimeout); + // Check status + if (status == WL_CONNECTED) { + // Connected, print WiFi status + printWiFiStatus(status); - DEBUG_WIFI_MULTI("[WIFIM] Connect timeout\n"); + // Return WiFi status + return status; + } else if (status == WL_CONNECT_FAILED) { + DEBUG_WIFI_MULTI("[WIFIM] Connect failed\n"); + } else { + DEBUG_WIFI_MULTI("[WIFIM] Connect timeout\n"); + } + // Return WiFi connect failed return WL_CONNECT_FAILED; } @@ -242,24 +232,16 @@ int8_t ESP8266WiFiMulti::startScan() // Start wifi scan in async mode WiFi.scanNetworks(true); - // Set WiFi scan timeout - using esp8266::polledTimeout::oneShotMs; - oneShotMs scanTimeout(WIFI_SCAN_TIMEOUT_MS); - // Wait for WiFi scan change or timeout - do { - // Refresh watchdog - delay(0); - - // Check scan timeout which may occur when scan does not report completion - if (scanTimeout) { - DEBUG_WIFI_MULTI("[WIFIM] Scan timeout\n"); - return WIFI_SCAN_FAILED; - } - - // Get scan result - scanResult = WiFi.scanComplete(); - } while (scanResult < 0); + // The final argument, intvl_ms, to esp_delay influences how frequently + // the scheduled recurrent functions (Schedule.h) are probed. + esp_delay(WIFI_SCAN_TIMEOUT_MS, + [&scanResult]() { scanResult = WiFi.scanComplete(); return scanResult < 0; }, 0); + // Check for scan timeout which may occur when scan does not report completion + if (scanResult < 0) { + DEBUG_WIFI_MULTI("[WIFIM] Scan timeout\n"); + return WIFI_SCAN_FAILED; + } // Print WiFi scan result printWiFiScan(); @@ -535,7 +517,7 @@ void ESP8266WiFiMulti::printWiFiScan() rssi, (encryptionType == ENC_TYPE_NONE) ? ' ' : '*', ssid.c_str()); - delay(0); + esp_yield(); } #endif } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA-WPS.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA-WPS.cpp index 99d27ba7a3..b93c77da9c 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA-WPS.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA-WPS.cpp @@ -30,6 +30,8 @@ static void wifi_wps_status_cb(wps_cb_status status); +static bool _wps_config_pending = false; + /** * WPS config * so far only WPS_TYPE_PBC is supported (SDK 1.2.0) @@ -70,8 +72,9 @@ bool ESP8266WiFiSTAClass::beginWPSConfig(void) { return false; } - esp_yield(); + _wps_config_pending = true; // will resume when wifi_wps_status_cb fires + esp_suspend([]() { return _wps_config_pending; }); return true; } @@ -107,5 +110,6 @@ void wifi_wps_status_cb(wps_cb_status status) { } // TODO user function to get status - esp_schedule(); // resume beginWPSConfig + _wps_config_pending = false; // resume beginWPSConfig + esp_schedule(); } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp index 618dbfcc86..c1c63996aa 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp @@ -28,6 +28,8 @@ #include "PolledTimeout.h" #include "LwipIntf.h" +#include + #include "c_types.h" #include "ets_sys.h" #include "os_type.h" @@ -44,9 +46,6 @@ extern "C" { #include "debug.h" -extern "C" void esp_schedule(); -extern "C" void esp_yield(); - // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ // ----------------------------------------------------------------------------------------------------------------------- diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp index 65878a3d5b..6aaf432e2d 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp @@ -36,9 +36,7 @@ extern "C" { } #include "debug.h" - -extern "C" void esp_schedule(); -extern "C" void esp_yield(); +#include // ----------------------------------------------------------------------------------------------------------------------- // ---------------------------------------------------- Private functions ------------------------------------------------ @@ -94,11 +92,13 @@ int8_t ESP8266WiFiScanClass::scanNetworks(bool async, bool show_hidden, uint8 ch ESP8266WiFiScanClass::_scanStarted = true; if(ESP8266WiFiScanClass::_scanAsync) { - delay(0); // time for the OS to trigger the scan + esp_yield(); // time for the OS to trigger the scan return WIFI_SCAN_RUNNING; } - esp_yield(); // will resume when _scanDone fires + // will resume when _scanDone fires + esp_suspend([]() { return !ESP8266WiFiScanClass::_scanComplete && ESP8266WiFiScanClass::_scanStarted; }); + return ESP8266WiFiScanClass::_scanCount; } else { return WIFI_SCAN_FAILED; @@ -322,7 +322,7 @@ void ESP8266WiFiScanClass::_scanDone(void* result, int status) { ESP8266WiFiScanClass::_scanStarted = false; ESP8266WiFiScanClass::_scanComplete = true; - if(!ESP8266WiFiScanClass::_scanAsync) { + if (!ESP8266WiFiScanClass::_scanAsync) { esp_schedule(); // resume scanNetworks } else if (ESP8266WiFiScanClass::_onComplete) { ESP8266WiFiScanClass::_onComplete(ESP8266WiFiScanClass::_scanCount); diff --git a/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp b/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp index 6a82ff3b96..9ad357c33a 100644 --- a/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp +++ b/libraries/ESP8266WiFi/src/WiFiClientSecureBearSSL.cpp @@ -44,7 +44,6 @@ extern "C" { #include "lwip/netif.h" #include #include "c_types.h" -#include "coredecls.h" #include #include #include diff --git a/libraries/ESP8266WiFi/src/include/ClientContext.h b/libraries/ESP8266WiFi/src/include/ClientContext.h index a994b5cb1d..7fcad3678a 100644 --- a/libraries/ESP8266WiFi/src/include/ClientContext.h +++ b/libraries/ESP8266WiFi/src/include/ClientContext.h @@ -26,11 +26,9 @@ class WiFiClient; typedef void (*discard_cb_t)(void*, ClientContext*); -extern "C" void esp_yield(); -extern "C" void esp_schedule(); - #include #include +#include bool getDefaultPrivateGlobalSyncValue (); @@ -145,11 +143,9 @@ class ClientContext } _connect_pending = true; _op_start_time = millis(); - for (decltype(_timeout_ms) i = 0; _connect_pending && i < _timeout_ms; i++) { - // Give scheduled functions a chance to run (e.g. Ethernet uses recurrent) - delay(1); - // will resume on timeout or when _connected or _notify_error fires - } + // will resume on timeout or when _connected or _notify_error fires + // give scheduled functions a chance to run (e.g. Ethernet uses recurrent) + esp_delay(_timeout_ms, [this]() { return this->_connect_pending; }, 1); _connect_pending = false; if (!_pcb) { DEBUGV(":cabrt\r\n"); @@ -352,7 +348,7 @@ class ClientContext last_sent = millis(); } - delay(0); // from sys or os context + esp_yield(); // from sys or os context if ((state() != ESTABLISHED) || (sndbuf == TCP_SND_BUF)) { // peer has closed or all bytes are sent and acked @@ -458,9 +454,10 @@ class ClientContext void _notify_error() { if (_connect_pending || _send_waiting) { + // resume connect or _write_from_source _send_waiting = false; _connect_pending = false; - esp_schedule(); // break delay in connect or _write_from_source + esp_schedule(); } } @@ -487,11 +484,9 @@ class ClientContext } _send_waiting = true; - for (decltype(_timeout_ms) i = 0; _send_waiting && i < _timeout_ms; i++) { - // Give scheduled functions a chance to run (e.g. Ethernet uses recurrent) - delay(1); - // will resume on timeout or when _write_some_from_cb or _notify_error fires - } + // will resume on timeout or when _write_some_from_cb or _notify_error fires + // give scheduled functions a chance to run (e.g. Ethernet uses recurrent) + esp_delay(_timeout_ms, [this]() { return this->_send_waiting; }, 1); _send_waiting = false; } while(true); @@ -561,8 +556,9 @@ class ClientContext void _write_some_from_cb() { if (_send_waiting) { + // resume _write_from_source _send_waiting = false; - esp_schedule(); // break delay in _write_from_source + esp_schedule(); } } @@ -649,8 +645,9 @@ class ClientContext (void) pcb; assert(pcb == _pcb); if (_connect_pending) { + // resume connect _connect_pending = false; - esp_schedule(); // break delay in connect + esp_schedule(); } return ERR_OK; } diff --git a/libraries/ESP8266WiFi/src/include/UdpContext.h b/libraries/ESP8266WiFi/src/include/UdpContext.h index 1d3c6e418c..1e7f639e34 100644 --- a/libraries/ESP8266WiFi/src/include/UdpContext.h +++ b/libraries/ESP8266WiFi/src/include/UdpContext.h @@ -24,7 +24,7 @@ class UdpContext; extern "C" { -void esp_yield(); +void esp_suspend(); void esp_schedule(); #include } @@ -177,7 +177,7 @@ class UdpContext } // warning: handler is called from tcp stack context - // esp_yield and non-reentrant functions which depend on it will fail + // esp_suspend and non-reentrant functions which depend on it will fail void onRx(rxhandler_t handler) { _on_rx = handler; } @@ -411,7 +411,7 @@ class UdpContext err_t err; esp8266::polledTimeout::oneShotFastMs timeout(timeoutMs); while (((err = trySend(addr, port, /* keep buffer on error */true)) != ERR_OK) && !timeout) - delay(0); + esp_yield(); if (err != ERR_OK) cancelBuffer(); // get rid of buffer kept on error after timeout return err == ERR_OK; diff --git a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp index 6fc4e3cd01..bf1b1cde26 100644 --- a/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp +++ b/libraries/ESP8266mDNS/src/LEAmDNS_Control.cpp @@ -702,7 +702,7 @@ bool MDNSResponder::_parseResponse(const MDNSResponder::stcMDNS_MsgHeader& p_Msg for (uint16_t qd=0; ((bDumpResult) && (qdflush(); diff --git a/libraries/esp8266/examples/SerialStress/SerialStress.ino b/libraries/esp8266/examples/SerialStress/SerialStress.ino index b06b38e307..a19eb9f270 100644 --- a/libraries/esp8266/examples/SerialStress/SerialStress.ino +++ b/libraries/esp8266/examples/SerialStress/SerialStress.ino @@ -122,7 +122,7 @@ void loop() { if ((out_idx += local_written_size) == BUFFER_SIZE) { out_idx = 0; } - delay(0); + yield(); DEBUG(logger->printf("----------\n")); diff --git a/libraries/lwIP_Ethernet/examples/EthClient/EthClient.ino b/libraries/lwIP_Ethernet/examples/EthClient/EthClient.ino new file mode 100644 index 0000000000..f05fd04081 --- /dev/null +++ b/libraries/lwIP_Ethernet/examples/EthClient/EthClient.ino @@ -0,0 +1,81 @@ +/* + This sketch establishes a TCP connection to a "quote of the day" service. + It sends a "hello" message, and then prints received data. + + This is Ethernet version of: + https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino +*/ + +#include + +Wiznet5500lwIP eth(/*SS*/16); // <== adapt to your hardware + +const char* host = "djxmmx.net"; +const uint16_t port = 17; + +void setup() { + Serial.begin(115200); + + Serial.println("\nEthernet\n"); + + eth.setDefault(true); // default route set through this interface + if (!ethInitDHCP(eth)) { + Serial.printf("no hardware found\n"); + while (1) { + delay(1000); + } + } + + while (!eth.connected()) { + Serial.printf("."); + delay(1000); + } + + Serial.printf("Ethernet: IP Address: %s\n", + eth.localIP().toString().c_str()); +} + +void loop() { + + Serial.print("connecting to "); + Serial.print(host); + Serial.print(':'); + Serial.println(port); + + // Use WiFiClient class to create TCP connections + // (this class could have been named TCPClient) + WiFiClient client; + if (!client.connect(host, port)) { + Serial.println("connection failed"); + delay(5000); + return; + } + + // This will send a string to the server + Serial.println("sending data to server"); + if (client.connected()) { + client.println("hello from ESP8266"); + } + + // wait for data to be available + unsigned long timeout = millis(); + while (client.available() == 0) { + if (millis() - timeout > 5000) { + Serial.println(">>> Client Timeout !"); + client.stop(); + delay(60000); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + Serial.println("receiving from remote server"); + client.sendAll(Serial); // this peer closes once all data are sent + + // Close the connection + Serial.println(); + Serial.println("closing connection"); + client.stop(); + + delay(600000); // execute once every 10 minutes, don't flood remote service +} diff --git a/libraries/lwIP_Ethernet/examples/EthClientStatic/EthClientStatic.ino b/libraries/lwIP_Ethernet/examples/EthClientStatic/EthClientStatic.ino new file mode 100644 index 0000000000..b4516599e3 --- /dev/null +++ b/libraries/lwIP_Ethernet/examples/EthClientStatic/EthClientStatic.ino @@ -0,0 +1,82 @@ +/* + This sketch establishes a TCP connection to a "quote of the day" service. + It sends a "hello" message, and then prints received data. + + This is Ethernet version of: + https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino +*/ + +#include + +#define LOCAL_IP IPAddress(192,168,0,233) +#define LOCAL_GW IPAddress(192,168,0,254) // <== adapt to your network +#define LOCAL_MASK IPAddress(255,255,255,0) +#define DNS IPAddress(8,8,8,8) + +Wiznet5500lwIP eth(/*SS*/16); // <== adapt to your hardware + +const char* host = "djxmmx.net"; +const uint16_t port = 17; + +void setup() { + Serial.begin(115200); + + Serial.println("\nEthernet\n"); + + eth.setDefault(true); // default route set through this interface + if (!ethInitStatic(eth, LOCAL_IP, LOCAL_GW, LOCAL_MASK, DNS)) { + // enabling debug message will show the real cause + Serial.printf("no hardware found or bad network configuration\n"); + while (1) { + delay(1000); + } + } + + Serial.printf("Ethernet: IP Address: %s\n", + eth.localIP().toString().c_str()); +} + +void loop() { + + Serial.print("connecting to "); + Serial.print(host); + Serial.print(':'); + Serial.println(port); + + // Use WiFiClient class to create TCP connections + // (this class could have been named TCPClient) + WiFiClient client; + if (!client.connect(host, port)) { + Serial.println("connection failed"); + delay(5000); + return; + } + + // This will send a string to the server + Serial.println("sending data to server"); + if (client.connected()) { + client.println("hello from ESP8266"); + } + + // wait for data to be available + unsigned long timeout = millis(); + while (client.available() == 0) { + if (millis() - timeout > 5000) { + Serial.println(">>> Client Timeout !"); + client.stop(); + delay(60000); + return; + } + } + + // Read all the lines of the reply from server and print them to Serial + Serial.println("receiving from remote server"); + client.sendAll(Serial); // this peer closes once all data are sent + + // Close the connection + Serial.println(); + Serial.println("closing connection"); + client.stop(); + + delay(600000); // execute once every 10 minutes, don't flood remote service +} diff --git a/libraries/lwIP_Ethernet/examples/EthSSLValidation/EthSSLValidation.ino b/libraries/lwIP_Ethernet/examples/EthSSLValidation/EthSSLValidation.ino new file mode 100644 index 0000000000..e47aae9152 --- /dev/null +++ b/libraries/lwIP_Ethernet/examples/EthSSLValidation/EthSSLValidation.ino @@ -0,0 +1,228 @@ +// Example of the different modes of the X.509 validation options +// in the WiFiClientBearSSL object +// +// Mar 2018 by Earle F. Philhower, III +// Released to the public domain +// +// This is Ethernet version of: +// https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266WiFi/examples/BearSSL_Validation/BearSSL_Validation.ino + +#include + +Wiznet5500lwIP eth(/*SS*/16); // <== adapt to your hardware + +#include +#include +#include +#include "certs.h" + +const char * path = "/"; + +// Set time via NTP, as required for x.509 validation +void setClock() { + configTime(3 * 3600, 0, "pool.ntp.org", "time.nist.gov"); + + Serial.print("Waiting for NTP time sync: "); + time_t now = time(nullptr); + while (now < 8 * 3600 * 2) { + delay(500); + Serial.print("."); + now = time(nullptr); + } + Serial.println(""); + struct tm timeinfo; + gmtime_r(&now, &timeinfo); + Serial.print("Current time: "); + Serial.print(asctime(&timeinfo)); +} + +// Try and connect using a WiFiClientBearSSL to specified host:port and dump HTTP response +void fetchURL(BearSSL::WiFiClientSecure *client, const char *host, const uint16_t port, const char *path) { + if (!path) { + path = "/"; + } + + ESP.resetFreeContStack(); + uint32_t freeStackStart = ESP.getFreeContStack(); + Serial.printf("Trying: %s:443...", host); + client->connect(host, port); + if (!client->connected()) { + Serial.printf("*** Can't connect. ***\n-------\n"); + return; + } + Serial.printf("Connected!\n-------\n"); + client->write("GET "); + client->write(path); + client->write(" HTTP/1.0\r\nHost: "); + client->write(host); + client->write("\r\nUser-Agent: ESP8266\r\n"); + client->write("\r\n"); + uint32_t to = millis() + 5000; + if (client->connected()) { + do { + char tmp[32]; + memset(tmp, 0, 32); + int rlen = client->read((uint8_t*)tmp, sizeof(tmp) - 1); + yield(); + if (rlen < 0) { + break; + } + // Only print out first line up to \r, then abort connection + char *nl = strchr(tmp, '\r'); + if (nl) { + *nl = 0; + Serial.print(tmp); + break; + } + Serial.print(tmp); + } while (millis() < to); + } + client->stop(); + uint32_t freeStackEnd = ESP.getFreeContStack(); + Serial.printf("\nCONT stack used: %d\n", freeStackStart - freeStackEnd); + Serial.printf("BSSL stack used: %d\n-------\n\n", stack_thunk_get_max_usage()); +} + +void fetchNoConfig() { + Serial.printf(R"EOF( +If there are no CAs or insecure options specified, BearSSL will not connect. +Expect the following call to fail as none have been configured. +)EOF"); + BearSSL::WiFiClientSecure client; + fetchURL(&client, gitlab_host, gitlab_port, path); +} + +void fetchInsecure() { + Serial.printf(R"EOF( +This is absolutely *insecure*, but you can tell BearSSL not to check the +certificate of the server. In this mode it will accept ANY certificate, +which is subject to man-in-the-middle (MITM) attacks. +)EOF"); + BearSSL::WiFiClientSecure client; + client.setInsecure(); + fetchURL(&client, gitlab_host, gitlab_port, path); +} + +void fetchFingerprint() { + Serial.printf(R"EOF( +The SHA-1 fingerprint of an X.509 certificate can be used to validate it +instead of the while certificate. This is not nearly as secure as real +X.509 validation, but is better than nothing. Also be aware that these +fingerprints will change if anything changes in the certificate chain +(i.e. re-generating the certificate for a new end date, any updates to +the root authorities, etc.). +)EOF"); + BearSSL::WiFiClientSecure client; + client.setFingerprint(fingerprint_gitlab_com); + fetchURL(&client, gitlab_host, gitlab_port, path); +} + +void fetchSelfSigned() { + Serial.printf(R"EOF( +It is also possible to accept *any* self-signed certificate. This is +absolutely insecure as anyone can make a self-signed certificate. +)EOF"); + BearSSL::WiFiClientSecure client; + Serial.printf("First, try and connect to a badssl.com self-signed website (will fail):\n"); + fetchURL(&client, "self-signed.badssl.com", 443, "/"); + Serial.printf("Now we'll enable self-signed certs (will pass)\n"); + client.allowSelfSignedCerts(); + fetchURL(&client, "self-signed.badssl.com", 443, "/"); +} + +void fetchKnownKey() { + Serial.printf(R"EOF( +The server certificate can be completely ignored and its public key +hardcoded in your application. This should be secure as the public key +needs to be paired with the private key of the site, which is obviously +private and not shared. A MITM without the private key would not be +able to establish communications. +)EOF"); + BearSSL::WiFiClientSecure client; + BearSSL::PublicKey key(pubkey_gitlab_com); + client.setKnownKey(&key); + fetchURL(&client, gitlab_host, gitlab_port, path); +} + +void fetchCertAuthority() { + Serial.printf(R"EOF( +A specific certification authority can be passed in and used to validate +a chain of certificates from a given server. These will be validated +using BearSSL's rules, which do NOT include certificate revocation lists. +A specific server's certificate, or your own self-signed root certificate +can also be used. ESP8266 time needs to be valid for checks to pass as +BearSSL does verify the notValidBefore/After fields. +)EOF"); + + BearSSL::WiFiClientSecure client; + BearSSL::X509List cert(cert_USERTrust_RSA_Certification_Authority); + client.setTrustAnchors(&cert); + Serial.printf("Try validating without setting the time (should fail)\n"); + fetchURL(&client, gitlab_host, gitlab_port, path); + + Serial.printf("Try again after setting NTP time (should pass)\n"); + setClock(); + fetchURL(&client, gitlab_host, gitlab_port, path); +} + +void fetchFaster() { + Serial.printf(R"EOF( +The ciphers used to set up the SSL connection can be configured to +only support faster but less secure ciphers. If you care about security +you won't want to do this. If you need to maximize battery life, these +may make sense +)EOF"); + BearSSL::WiFiClientSecure client; + client.setInsecure(); + uint32_t now = millis(); + fetchURL(&client, gitlab_host, gitlab_port, path); + uint32_t delta = millis() - now; + client.setInsecure(); + client.setCiphersLessSecure(); + now = millis(); + fetchURL(&client, gitlab_host, gitlab_port, path); + uint32_t delta2 = millis() - now; + std::vector myCustomList = { BR_TLS_RSA_WITH_AES_256_CBC_SHA256, BR_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, BR_TLS_RSA_WITH_3DES_EDE_CBC_SHA }; + client.setInsecure(); + client.setCiphers(myCustomList); + now = millis(); + fetchURL(&client, gitlab_host, gitlab_port, path); + uint32_t delta3 = millis() - now; + Serial.printf("Using more secure: %dms\nUsing less secure ciphers: %dms\nUsing custom cipher list: %dms\n", delta, delta2, delta3); +} + +void setup() { + Serial.begin(115200); + Serial.println(); + Serial.println(); + + Serial.println("\nEthernet\n"); + + eth.setDefault(true); // default route set through this interface + if (!ethInitDHCP(eth)) { + Serial.printf("no hardware found\n"); + while (1) + delay(1000); + } + + while (!eth.connected()) { + Serial.printf("."); + delay(1000); + } + + Serial.printf("Ethernet: IP Address: %s\n", + eth.localIP().toString().c_str()); + + fetchNoConfig(); + fetchInsecure(); + fetchFingerprint(); + fetchSelfSigned(); + fetchKnownKey(); + fetchCertAuthority(); + fetchFaster(); +} + + +void loop() { + // Nothing to do here +} diff --git a/libraries/lwIP_Ethernet/examples/EthSSLValidation/certUpdate b/libraries/lwIP_Ethernet/examples/EthSSLValidation/certUpdate new file mode 100755 index 0000000000..2162b78e97 --- /dev/null +++ b/libraries/lwIP_Ethernet/examples/EthSSLValidation/certUpdate @@ -0,0 +1,2 @@ +cd ${0%/*} 2>/dev/null +python3 ../../../../tools/cert.py -s www.gitlab.com -n gitlab > certs.h diff --git a/libraries/lwIP_Ethernet/examples/EthSSLValidation/certs.h b/libraries/lwIP_Ethernet/examples/EthSSLValidation/certs.h new file mode 100644 index 0000000000..0a3956e220 --- /dev/null +++ b/libraries/lwIP_Ethernet/examples/EthSSLValidation/certs.h @@ -0,0 +1,115 @@ + +// this file is autogenerated - any modification will be overwritten +// unused symbols will not be linked in the final binary +// generated on 2021-09-22 14:19:05 +// by ['../../../../tools/cert.py', '-s', 'www.gitlab.com', '-n', 'gitlab'] + +#pragma once + +//////////////////////////////////////////////////////////// +// certificate chain for www.gitlab.com:443 + +const char* gitlab_host = "www.gitlab.com"; +const uint16_t gitlab_port = 443; + +// CN: gitlab.com => name: gitlab_com +// not valid before: 2021-04-12 00:00:00 +// not valid after: 2022-05-11 23:59:59 +const char fingerprint_gitlab_com [] PROGMEM = "71:55:5e:29:68:99:43:98:c8:85:35:bd:4c:10:4c:f5:cf:17:09:e6"; +const char pubkey_gitlab_com [] PROGMEM = R"PUBKEY( +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1eeFy86Xbz3ygyCVprHp +sPP3zyg0yldkIfqwjsXPH0b+KwQ85s3pzI/5+MVrR2/BGY4ed6mTZ6hvNwQJ2B0E +sJrsTb2nuUsXQ0UVO4hvnZ7Dnx8r/bT1cndqa+Mn+bms8/TS4etP72+TLaORBRCz +O4L1Hi8r61+zZLnP3DqqHeHAgl5wKHNYpx7yFFl2I71LuLH/pk2ICDBjaHwCIbRW +u484no9s1c4VROxqMrQQ/wDMl80MiO9YeNQ5rBHfnabh4rFe9eb2Sd0H/DWBj3SO +YBD0kiLI6b5CWYfA76pBSlZg7G3ledvQ+n9FEcS3EOCPKBBZqMDCzEahvHqwJ/r6 +pwIDAQAB +-----END PUBLIC KEY----- +)PUBKEY"; + +// http://crt.sectigo.com/SectigoRSADomainValidationSecureServerCA.crt +// CN: Sectigo RSA Domain Validation Secure Server CA => name: Sectigo_RSA_Domain_Validation_Secure_Server_CA +// not valid before: 2018-11-02 00:00:00 +// not valid after: 2030-12-31 23:59:59 +const char cert_Sectigo_RSA_Domain_Validation_Secure_Server_CA [] PROGMEM = R"CERT( +-----BEGIN CERTIFICATE----- +MIIGEzCCA/ugAwIBAgIQfVtRJrR2uhHbdBYLvFMNpzANBgkqhkiG9w0BAQwFADCB +iDELMAkGA1UEBhMCVVMxEzARBgNVBAgTCk5ldyBKZXJzZXkxFDASBgNVBAcTC0pl +cnNleSBDaXR5MR4wHAYDVQQKExVUaGUgVVNFUlRSVVNUIE5ldHdvcmsxLjAsBgNV +BAMTJVVTRVJUcnVzdCBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTgx +MTAyMDAwMDAwWhcNMzAxMjMxMjM1OTU5WjCBjzELMAkGA1UEBhMCR0IxGzAZBgNV +BAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEYMBYGA1UE +ChMPU2VjdGlnbyBMaW1pdGVkMTcwNQYDVQQDEy5TZWN0aWdvIFJTQSBEb21haW4g +VmFsaWRhdGlvbiBTZWN1cmUgU2VydmVyIENBMIIBIjANBgkqhkiG9w0BAQEFAAOC +AQ8AMIIBCgKCAQEA1nMz1tc8INAA0hdFuNY+B6I/x0HuMjDJsGz99J/LEpgPLT+N +TQEMgg8Xf2Iu6bhIefsWg06t1zIlk7cHv7lQP6lMw0Aq6Tn/2YHKHxYyQdqAJrkj +eocgHuP/IJo8lURvh3UGkEC0MpMWCRAIIz7S3YcPb11RFGoKacVPAXJpz9OTTG0E +oKMbgn6xmrntxZ7FN3ifmgg0+1YuWMQJDgZkW7w33PGfKGioVrCSo1yfu4iYCBsk +Haswha6vsC6eep3BwEIc4gLw6uBK0u+QDrTBQBbwb4VCSmT3pDCg/r8uoydajotY +uK3DGReEY+1vVv2Dy2A0xHS+5p3b4eTlygxfFQIDAQABo4IBbjCCAWowHwYDVR0j +BBgwFoAUU3m/WqorSs9UgOHYm8Cd8rIDZsswHQYDVR0OBBYEFI2MXsRUrYrhd+mb ++ZsF4bgBjWHhMA4GA1UdDwEB/wQEAwIBhjASBgNVHRMBAf8ECDAGAQH/AgEAMB0G +A1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAbBgNVHSAEFDASMAYGBFUdIAAw +CAYGZ4EMAQIBMFAGA1UdHwRJMEcwRaBDoEGGP2h0dHA6Ly9jcmwudXNlcnRydXN0 +LmNvbS9VU0VSVHJ1c3RSU0FDZXJ0aWZpY2F0aW9uQXV0aG9yaXR5LmNybDB2Bggr +BgEFBQcBAQRqMGgwPwYIKwYBBQUHMAKGM2h0dHA6Ly9jcnQudXNlcnRydXN0LmNv +bS9VU0VSVHJ1c3RSU0FBZGRUcnVzdENBLmNydDAlBggrBgEFBQcwAYYZaHR0cDov +L29jc3AudXNlcnRydXN0LmNvbTANBgkqhkiG9w0BAQwFAAOCAgEAMr9hvQ5Iw0/H +ukdN+Jx4GQHcEx2Ab/zDcLRSmjEzmldS+zGea6TvVKqJjUAXaPgREHzSyrHxVYbH +7rM2kYb2OVG/Rr8PoLq0935JxCo2F57kaDl6r5ROVm+yezu/Coa9zcV3HAO4OLGi +H19+24rcRki2aArPsrW04jTkZ6k4Zgle0rj8nSg6F0AnwnJOKf0hPHzPE/uWLMUx +RP0T7dWbqWlod3zu4f+k+TY4CFM5ooQ0nBnzvg6s1SQ36yOoeNDT5++SR2RiOSLv +xvcRviKFxmZEJCaOEDKNyJOuB56DPi/Z+fVGjmO+wea03KbNIaiGCpXZLoUmGv38 +sbZXQm2V0TP2ORQGgkE49Y9Y3IBbpNV9lXj9p5v//cWoaasm56ekBYdbqbe4oyAL +l6lFhd2zi+WJN44pDfwGF/Y4QA5C5BIG+3vzxhFoYt/jmPQT2BVPi7Fp2RBgvGQq +6jG35LWjOhSbJuMLe/0CjraZwTiXWTb2qHSihrZe68Zk6s+go/lunrotEbaGmAhY +LcmsJWTyXnW0OMGuf1pGg+pRyrbxmRE1a6Vqe8YAsOf4vmSyrcjC8azjUeqkk+B5 +yOGBQMkKW+ESPMFgKuOXwIlCypTPRpgSabuY0MLTDXJLR27lk8QyKGOHQ+SwMj4K +00u/I5sUKUErmgQfky3xxzlIPK1aEn8= +-----END CERTIFICATE----- +)CERT"; + +// http://crt.usertrust.com/USERTrustRSAAddTrustCA.crt +// CN: USERTrust RSA Certification Authority => name: USERTrust_RSA_Certification_Authority +// not valid before: 2019-03-12 00:00:00 +// not valid after: 2028-12-31 23:59:59 +const char cert_USERTrust_RSA_Certification_Authority [] PROGMEM = R"CERT( +-----BEGIN CERTIFICATE----- +MIIFgTCCBGmgAwIBAgIQOXJEOvkit1HX02wQ3TE1lTANBgkqhkiG9w0BAQwFADB7 +MQswCQYDVQQGEwJHQjEbMBkGA1UECAwSR3JlYXRlciBNYW5jaGVzdGVyMRAwDgYD +VQQHDAdTYWxmb3JkMRowGAYDVQQKDBFDb21vZG8gQ0EgTGltaXRlZDEhMB8GA1UE +AwwYQUFBIENlcnRpZmljYXRlIFNlcnZpY2VzMB4XDTE5MDMxMjAwMDAwMFoXDTI4 +MTIzMTIzNTk1OVowgYgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpOZXcgSmVyc2V5 +MRQwEgYDVQQHEwtKZXJzZXkgQ2l0eTEeMBwGA1UEChMVVGhlIFVTRVJUUlVTVCBO +ZXR3b3JrMS4wLAYDVQQDEyVVU0VSVHJ1c3QgUlNBIENlcnRpZmljYXRpb24gQXV0 +aG9yaXR5MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAgBJlFzYOw9sI +s9CsVw127c0n00ytUINh4qogTQktZAnczomfzD2p7PbPwdzx07HWezcoEStH2jnG +vDoZtF+mvX2do2NCtnbyqTsrkfjib9DsFiCQCT7i6HTJGLSR1GJk23+jBvGIGGqQ +Ijy8/hPwhxR79uQfjtTkUcYRZ0YIUcuGFFQ/vDP+fmyc/xadGL1RjjWmp2bIcmfb +IWax1Jt4A8BQOujM8Ny8nkz+rwWWNR9XWrf/zvk9tyy29lTdyOcSOk2uTIq3XJq0 +tyA9yn8iNK5+O2hmAUTnAU5GU5szYPeUvlM3kHND8zLDU+/bqv50TmnHa4xgk97E +xwzf4TKuzJM7UXiVZ4vuPVb+DNBpDxsP8yUmazNt925H+nND5X4OpWaxKXwyhGNV +icQNwZNUMBkTrNN9N6frXTpsNVzbQdcS2qlJC9/YgIoJk2KOtWbPJYjNhLixP6Q5 +D9kCnusSTJV882sFqV4Wg8y4Z+LoE53MW4LTTLPtW//e5XOsIzstAL81VXQJSdhJ +WBp/kjbmUZIO8yZ9HE0XvMnsQybQv0FfQKlERPSZ51eHnlAfV1SoPv10Yy+xUGUJ +5lhCLkMaTLTwJUdZ+gQek9QmRkpQgbLevni3/GcV4clXhB4PY9bpYrrWX1Uu6lzG +KAgEJTm4Diup8kyXHAc/DVL17e8vgg8CAwEAAaOB8jCB7zAfBgNVHSMEGDAWgBSg +EQojPpbxB+zirynvgqV/0DCktDAdBgNVHQ4EFgQUU3m/WqorSs9UgOHYm8Cd8rID +ZsswDgYDVR0PAQH/BAQDAgGGMA8GA1UdEwEB/wQFMAMBAf8wEQYDVR0gBAowCDAG +BgRVHSAAMEMGA1UdHwQ8MDowOKA2oDSGMmh0dHA6Ly9jcmwuY29tb2RvY2EuY29t +L0FBQUNlcnRpZmljYXRlU2VydmljZXMuY3JsMDQGCCsGAQUFBwEBBCgwJjAkBggr +BgEFBQcwAYYYaHR0cDovL29jc3AuY29tb2RvY2EuY29tMA0GCSqGSIb3DQEBDAUA +A4IBAQAYh1HcdCE9nIrgJ7cz0C7M7PDmy14R3iJvm3WOnnL+5Nb+qh+cli3vA0p+ +rvSNb3I8QzvAP+u431yqqcau8vzY7qN7Q/aGNnwU4M309z/+3ri0ivCRlv79Q2R+ +/czSAaF9ffgZGclCKxO/WIu6pKJmBHaIkU4MiRTOok3JMrO66BQavHHxW/BBC5gA +CiIDEOUMsfnNkjcZ7Tvx5Dq2+UUTJnWvu6rvP3t3O9LEApE9GQDTF1w52z97GA1F +zZOFli9d31kWTz9RvdVFGD/tSo7oBmF0Ixa1DVBzJ0RHfxBdiSprhTEUxOipakyA +vGp4z7h/jnZymQyd/teRCBaho1+V +-----END CERTIFICATE----- +)CERT"; + + +// end of certificate chain for www.gitlab.com:443 +//////////////////////////////////////////////////////////// + diff --git a/libraries/lwIP_Ethernet/examples/Eth_mDNS_Clock/Eth_mDNS_Clock.ino b/libraries/lwIP_Ethernet/examples/Eth_mDNS_Clock/Eth_mDNS_Clock.ino new file mode 100644 index 0000000000..eadb23cd25 --- /dev/null +++ b/libraries/lwIP_Ethernet/examples/Eth_mDNS_Clock/Eth_mDNS_Clock.ino @@ -0,0 +1,274 @@ +/* + ESP8266 mDNS responder clock + + This example demonstrates two features of the LEA MDNSResponder: + 1. The host and service domain negotiation process that ensures + the uniqueness of the finally chosen host and service domain name. + 2. The dynamic MDNS service TXT feature + + A 'clock' service in announced via the MDNS responder and the current + time is set as a TXT item (eg. 'curtime=Mon Oct 15 19:54:35 2018'). + The time value is updated every second! + + The ESP is initially announced to clients as 'esp8266.local', if this host domain + is already used in the local network, another host domain is negotiated. Keep an + eye to the serial output to learn the final host domain for the clock service. + The service itself is is announced as 'host domain'._espclk._tcp.local. + As the service uses port 80, a very simple HTTP server is installed also to deliver + a small web page containing a greeting and the current time (not updated). + The web server code is taken nearly 1:1 from the 'mDNS_Web_Server.ino' example. + Point your browser to 'host domain'.local to see this web page. + + Instructions: + - Flash the sketch to the ESP8266 board + - Install host software: + - For Linux, install Avahi (http://avahi.org/). + - For Windows, install Bonjour (http://www.apple.com/support/bonjour/). + - For Mac OSX and iOS support is built in through Bonjour already. + - Use a MDNS/Bonjour browser like 'Discovery' to find the clock service in your local + network and see the current time updates. + + This is the Ethernet version of: + https://github.com/esp8266/Arduino/tree/master/libraries/ESP8266mDNS/examples/LEAmDNS/mDNS_Clock +*/ + + +#include +#include +#include +#include +#include +#include +#include + +Wiznet5500lwIP eth(/*SS*/16); // <== adapt to your hardware + +/* + Global defines and vars +*/ + +#define TIMEZONE_OFFSET 1 // CET +#define DST_OFFSET 1 // CEST +#define UPDATE_CYCLE (1 * 1000) // every second + +#define SERVICE_PORT 80 // HTTP port + +#ifndef STASSID +#define STASSID "your-ssid" +#define STAPSK "your-password" +#endif + +const char* ssid = STASSID; +const char* password = STAPSK; + +char* pcHostDomain = 0; // Negotiated host domain +bool bHostDomainConfirmed = false; // Flags the confirmation of the host domain +MDNSResponder::hMDNSService hMDNSService = 0; // The handle of the clock service in the MDNS responder + +// HTTP server at port 'SERVICE_PORT' will respond to HTTP requests +ESP8266WebServer server(SERVICE_PORT); + +/* + getTimeString +*/ +const char* getTimeString(void) { + + static char acTimeString[32]; + time_t now = time(nullptr); + ctime_r(&now, acTimeString); + size_t stLength; + while (((stLength = strlen(acTimeString))) && + ('\n' == acTimeString[stLength - 1])) { + acTimeString[stLength - 1] = 0; // Remove trailing line break... + } + return acTimeString; +} + + +/* + setClock + + Set time via NTP +*/ +void setClock(void) { + configTime((TIMEZONE_OFFSET * 3600), (DST_OFFSET * 3600), "pool.ntp.org", "time.nist.gov", "time.windows.com"); + + Serial.print("Waiting for NTP time sync: "); + time_t now = time(nullptr); // Secs since 01.01.1970 (when uninitialized starts with (8 * 3600 = 28800) + while (now < 8 * 3600 * 2) { // Wait for realistic value + delay(500); + Serial.print("."); + now = time(nullptr); + } + Serial.println(""); + Serial.printf("Current time: %s\n", getTimeString()); +} + + +/* + setStationHostname +*/ +bool setStationHostname(const char* p_pcHostname) { + + if (p_pcHostname) { + WiFi.hostname(p_pcHostname); + Serial.printf("setDeviceHostname: Station hostname is set to '%s'\n", p_pcHostname); + } + return true; +} + + +/* + MDNSDynamicServiceTxtCallback + + Add a dynamic MDNS TXT item 'ct' to the clock service. + The callback function is called every time, the TXT items for the clock service + are needed. + This can be triggered by calling MDNS.announce(). + +*/ +void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) { + Serial.println("MDNSDynamicServiceTxtCallback"); + + if (hMDNSService == p_hService) { + Serial.printf("Updating curtime TXT item to: %s\n", getTimeString()); + MDNS.addDynamicServiceTxt(p_hService, "curtime", getTimeString()); + } +} + + +/* + MDNSProbeResultCallback + + Probe result callback for the host domain. + If the domain is free, the host domain is set and the clock service is + added. + If the domain is already used, a new name is created and the probing is + restarted via p_pMDNSResponder->setHostname(). + +*/ +void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) { + + Serial.println("MDNSProbeResultCallback"); + Serial.printf("MDNSProbeResultCallback: Host domain '%s.local' is %s\n", p_pcDomainName.c_str(), (p_bProbeResult ? "free" : "already USED!")); + if (true == p_bProbeResult) { + // Set station hostname + setStationHostname(pcHostDomain); + + if (!bHostDomainConfirmed) { + // Hostname free -> setup clock service + bHostDomainConfirmed = true; + + if (!hMDNSService) { + // Add a 'clock.tcp' service to port 'SERVICE_PORT', using the host domain as instance domain + hMDNSService = MDNS.addService(0, "espclk", "tcp", SERVICE_PORT); + if (hMDNSService) { + // Add a simple static MDNS service TXT item + MDNS.addServiceTxt(hMDNSService, "port#", SERVICE_PORT); + // Set the callback function for dynamic service TXTs + MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback); + } + } + } + } else { + // Change hostname, use '-' as divider between base name and index + if (MDNSResponder::indexDomain(pcHostDomain, "-", 0)) { + MDNS.setHostname(pcHostDomain); + } else { + Serial.println("MDNSProbeResultCallback: FAILED to update hostname!"); + } + } +} + + +/* + handleHTTPClient +*/ + +void handleHTTPRequest() { + Serial.println(""); + Serial.println("HTTP Request"); + + // Get current time + time_t now = time(nullptr);; + struct tm timeinfo; + gmtime_r(&now, &timeinfo); + + String s; + + s = "\r\nHello from "; + s += WiFi.hostname() + " at " + WiFi.localIP().toString(); + // Simple addition of the current time + s += "\r\nCurrent time is: "; + s += getTimeString(); + // done :-) + s += "\r\n\r\n"; + Serial.println("Sending 200"); + server.send(200, "text/html", s); +} + +/* + setup +*/ +void setup(void) { + Serial.begin(115200); + + Serial.println("\nEthernet\n"); + + eth.setDefault(true); // default route set through this interface + if (!ethInitDHCP(eth)) { + Serial.printf("no hardware found\n"); + while (1) { + delay(1000); + } + } + + while (!eth.connected()) { + Serial.printf("."); + delay(1000); + } + + Serial.printf("Ethernet: IP Address: %s\n", + eth.localIP().toString().c_str()); + + // Sync clock + setClock(); + + // Setup MDNS responder + MDNS.setHostProbeResultCallback(hostProbeResult); + // Init the (currently empty) host domain string with 'esp8266' + if ((!MDNSResponder::indexDomain(pcHostDomain, 0, "esp8266")) || + (!MDNS.begin(pcHostDomain))) { + Serial.println("Error setting up MDNS responder!"); + while (1) { // STOP + delay(1000); + } + } + Serial.println("MDNS responder started"); + + // Setup HTTP server + server.on("/", handleHTTPRequest); + server.begin(); + Serial.println("HTTP server started"); +} + +/* + loop +*/ +void loop(void) { + + // Check if a request has come in + server.handleClient(); + // Allow MDNS processing + MDNS.update(); + + static esp8266::polledTimeout::periodicMs timeout(UPDATE_CYCLE); + if (timeout.expired()) { + + if (hMDNSService) { + // Just trigger a new MDNS announcement, this will lead to a call to + // 'MDNSDynamicServiceTxtCallback', which will update the time TXT item + MDNS.announce(); + } + } +} diff --git a/libraries/lwIP_Ethernet/library.properties b/libraries/lwIP_Ethernet/library.properties new file mode 100644 index 0000000000..38b161c26e --- /dev/null +++ b/libraries/lwIP_Ethernet/library.properties @@ -0,0 +1,10 @@ +name=lwIP-Ethernet +version=1 +author=esp8266/Arduino +maintainer=esp8266/Arduino +sentence=Helper for ethernet drivers +paragraph=Example repository for Ethernet drivers +category=Communication +url=https://github.com/esp8266/Arduino +architectures=esp8266 +dot_a_linkage=true diff --git a/libraries/lwIP_Ethernet/src/LwipEthernet.cpp b/libraries/lwIP_Ethernet/src/LwipEthernet.cpp new file mode 100644 index 0000000000..36e016ae4b --- /dev/null +++ b/libraries/lwIP_Ethernet/src/LwipEthernet.cpp @@ -0,0 +1,14 @@ + +#include + +#ifndef ETHERNET_SPI_CLOCK_DIV +#define ETHERNET_SPI_CLOCK_DIV SPI_CLOCK_DIV4 // 4MHz (SPI.h) +#endif + +void SPI4EthInit() +{ + SPI.begin(); + SPI.setClockDivider(ETHERNET_SPI_CLOCK_DIV); + SPI.setBitOrder(MSBFIRST); + SPI.setDataMode(SPI_MODE0); +} diff --git a/libraries/lwIP_Ethernet/src/LwipEthernet.h b/libraries/lwIP_Ethernet/src/LwipEthernet.h new file mode 100644 index 0000000000..f32165c19e --- /dev/null +++ b/libraries/lwIP_Ethernet/src/LwipEthernet.h @@ -0,0 +1,53 @@ + +#include // tcp API +#include +#include + +#include +#include +#include + +// One of them is to be declared in the main sketch +// and passed to ethInitDHCP() or ethInitStatic(): +//Wiznet5500lwIP eth(CSPIN); +//Wiznet5100lwIP eth(CSPIN); +//ENC28J60lwIP eth(CSPIN); + +void SPI4EthInit(); + +template +bool ethInitDHCP(EthImpl& eth) +{ + SPI4EthInit(); + + if (!eth.begin()) + { + // hardware not responding + DEBUGV("ethInitDHCP: hardware not responding\n"); + return false; + } + + return true; +} + +template +bool ethInitStatic(EthImpl& eth, IPAddress IP, IPAddress gateway, IPAddress netmask, IPAddress dns1, IPAddress dns2 = IPADDR_NONE) +{ + SPI4EthInit(); + + if (!eth.config(IP, gateway, netmask, dns1, dns2)) + { + // invalid arguments + DEBUGV("ethInitStatic: invalid arguments\n"); + return false; + } + + if (!eth.begin()) + { + // hardware not responding + DEBUGV("ethInitStatic: hardware not responding\n"); + return false; + } + + return true; +} diff --git a/tests/host/common/Arduino.cpp b/tests/host/common/Arduino.cpp index 780c4adc14..e934f8acba 100644 --- a/tests/host/common/Arduino.cpp +++ b/tests/host/common/Arduino.cpp @@ -15,6 +15,7 @@ #include #include "Arduino.h" +#include #include @@ -53,10 +54,33 @@ extern "C" void optimistic_yield (uint32_t interval_us) (void)interval_us; } +extern "C" void esp_suspend() +{ +} + +extern "C" void esp_schedule() +{ +} + extern "C" void esp_yield() { } +extern "C" void esp_delay (unsigned long ms) +{ + usleep(ms * 1000); +} + +bool try_esp_delay(const uint32_t start_ms, const uint32_t timeout_ms, const uint32_t intvl_ms) { + decltype(millis()) expired; + + if ((expired = millis() - start_ms) >= timeout_ms) { + return true; + } + const auto remaining = timeout_ms - expired; + esp_delay(remaining <= intvl_ms ? remaining : intvl_ms); + return false; +} extern "C" void __panic_func(const char* file, int line, const char* func) { (void)file; @@ -67,7 +91,7 @@ extern "C" void __panic_func(const char* file, int line, const char* func) { extern "C" void delay(unsigned long ms) { - usleep(ms * 1000); + esp_delay(ms); } extern "C" void delayMicroseconds(unsigned int us) @@ -77,6 +101,6 @@ extern "C" void delayMicroseconds(unsigned int us) #include "cont.h" cont_t* g_pcont = NULL; -extern "C" void cont_yield(cont_t*) +extern "C" void cont_suspend(cont_t*) { } diff --git a/tests/host/common/include/ClientContext.h b/tests/host/common/include/ClientContext.h index 744d7872d4..bd9c9b80a5 100644 --- a/tests/host/common/include/ClientContext.h +++ b/tests/host/common/include/ClientContext.h @@ -24,9 +24,6 @@ class ClientContext; class WiFiClient; -extern "C" void esp_yield(); -extern "C" void esp_schedule(); - #include bool getDefaultPrivateGlobalSyncValue (); diff --git a/tests/host/common/user_interface.cpp b/tests/host/common/user_interface.cpp index b3a302df18..4e03d77786 100644 --- a/tests/host/common/user_interface.cpp +++ b/tests/host/common/user_interface.cpp @@ -498,10 +498,6 @@ extern "C" (void)intr; } - void esp_schedule(void) - { - } - void dns_setserver(u8_t numdns, ip_addr_t *dnsserver) { (void)numdns;