From 0096ee7d9ffcbefc6e60fd0bc21ede66e5a36e0e Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 4 Oct 2019 22:26:34 +0200 Subject: [PATCH 1/3] use a scheduled function for settimeofday_cb --- cores/esp8266/coredecls.h | 6 +++++- cores/esp8266/sntp-lwip2.cpp | 14 ++++++++++---- .../examples/NTP-TZ-DST/NTP-TZ-DST.ino | 19 +++++-------------- 3 files changed, 20 insertions(+), 19 deletions(-) diff --git a/cores/esp8266/coredecls.h b/cores/esp8266/coredecls.h index 4a70609bff..35c9a0d720 100644 --- a/cores/esp8266/coredecls.h +++ b/cores/esp8266/coredecls.h @@ -17,7 +17,6 @@ extern bool timeshift64_is_set; void esp_yield(); void esp_schedule(); void tune_timeshift64 (uint64_t now_us); -void settimeofday_cb (void (*cb)(void)); void disable_extra4k_at_link_time (void) __attribute__((noinline)); uint32_t sqrt32 (uint32_t n); @@ -25,6 +24,11 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff); #ifdef __cplusplus } + +#include +void settimeofday_cb (std::function&& cb); +void settimeofday_cb (const std::function& cb); + #endif #endif // __COREDECLS_H diff --git a/cores/esp8266/sntp-lwip2.cpp b/cores/esp8266/sntp-lwip2.cpp index 0dba9a7f8a..fa5bfe2652 100644 --- a/cores/esp8266/sntp-lwip2.cpp +++ b/cores/esp8266/sntp-lwip2.cpp @@ -42,16 +42,22 @@ #include #include #include "coredecls.h" +#include "Schedule.h" -extern "C" { +std::function _settimeofday_cb; -static void (*_settimeofday_cb)(void) = NULL; +void settimeofday_cb (std::function&& cb) +{ + _settimeofday_cb = std::move(cb); +} -void settimeofday_cb (void (*cb)(void)) +void settimeofday_cb (const std::function& cb) { _settimeofday_cb = cb; } +extern "C" { + #if LWIP_VERSION_MAJOR == 1 #include @@ -478,7 +484,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz) sntp_set_system_time(tv->tv_sec); if (_settimeofday_cb) - _settimeofday_cb(); + schedule_function(_settimeofday_cb); } return 0; } diff --git a/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino index b3c4e2064f..ebfbd112b5 100644 --- a/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino +++ b/libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino @@ -152,6 +152,10 @@ void printTm(const char* what, const tm* tm) { void time_is_set_scheduled() { // everything is allowed in this function + if (time_machine_days == 0) { + time_machine_running = !time_machine_running; + } + // time machine demo if (time_machine_running) { if (time_machine_days == 0) @@ -178,19 +182,6 @@ void time_is_set_scheduled() { } } -void time_is_set_callback() { - // As in an ISR, - // it is not allowed to call "heavy" core API - // like yield()/delay()/print()/network/... - // If this is needed, use a scheduled function. - - // This scheduled function is used for the demo, it is normaly unneeded - schedule_function(time_is_set_scheduled); - if (time_machine_days == 0) { - time_machine_running = !time_machine_running; - } -} - void setup() { Serial.begin(115200); Serial.println("\nStarting...\n"); @@ -204,7 +195,7 @@ void setup() { // install callback - called when settimeofday is called (by SNTP or us) // once enabled (by DHCP), SNTP is updated every hour - settimeofday_cb(time_is_set_callback); + settimeofday_cb(time_is_set_scheduled); // NTP servers may be overriden by your DHCP server for a more local one // (see below) From 61cf089daccb0e12ee58703f93a330dcb0229a10 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sat, 5 Oct 2019 13:14:01 +0200 Subject: [PATCH 2/3] per review --- cores/esp8266/coredecls.h | 7 +++++-- cores/esp8266/sntp-lwip2.cpp | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/cores/esp8266/coredecls.h b/cores/esp8266/coredecls.h index 35c9a0d720..e6d3546bec 100644 --- a/cores/esp8266/coredecls.h +++ b/cores/esp8266/coredecls.h @@ -26,8 +26,11 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff); } #include -void settimeofday_cb (std::function&& cb); -void settimeofday_cb (const std::function& cb); + +using functionalVoidVoid = std::function; + +void settimeofday_cb (functionalVoidVoid&& cb); +void settimeofday_cb (const functionalVoidVoid& cb); #endif diff --git a/cores/esp8266/sntp-lwip2.cpp b/cores/esp8266/sntp-lwip2.cpp index fa5bfe2652..f27520001e 100644 --- a/cores/esp8266/sntp-lwip2.cpp +++ b/cores/esp8266/sntp-lwip2.cpp @@ -44,14 +44,14 @@ #include "coredecls.h" #include "Schedule.h" -std::function _settimeofday_cb; +static functionalVoidVoid _settimeofday_cb; -void settimeofday_cb (std::function&& cb) +void settimeofday_cb (functionalVoidVoid&& cb) { _settimeofday_cb = std::move(cb); } -void settimeofday_cb (const std::function& cb) +void settimeofday_cb (const functionalVoidVoid& cb) { _settimeofday_cb = cb; } From 591269624aae2390ef3a1aeb4771e3729891afb3 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 6 Oct 2019 22:12:55 +0200 Subject: [PATCH 3/3] use a generic and clear name for trivial functional variable type name used for callbacks --- cores/esp8266/coredecls.h | 6 +++--- cores/esp8266/sntp-lwip2.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cores/esp8266/coredecls.h b/cores/esp8266/coredecls.h index e6d3546bec..2496995b08 100644 --- a/cores/esp8266/coredecls.h +++ b/cores/esp8266/coredecls.h @@ -27,10 +27,10 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff); #include -using functionalVoidVoid = std::function; +using TrivialCB = std::function; -void settimeofday_cb (functionalVoidVoid&& cb); -void settimeofday_cb (const functionalVoidVoid& cb); +void settimeofday_cb (TrivialCB&& cb); +void settimeofday_cb (const TrivialCB& cb); #endif diff --git a/cores/esp8266/sntp-lwip2.cpp b/cores/esp8266/sntp-lwip2.cpp index f27520001e..063cc5be2b 100644 --- a/cores/esp8266/sntp-lwip2.cpp +++ b/cores/esp8266/sntp-lwip2.cpp @@ -44,14 +44,14 @@ #include "coredecls.h" #include "Schedule.h" -static functionalVoidVoid _settimeofday_cb; +static TrivialCB _settimeofday_cb; -void settimeofday_cb (functionalVoidVoid&& cb) +void settimeofday_cb (TrivialCB&& cb) { _settimeofday_cb = std::move(cb); } -void settimeofday_cb (const functionalVoidVoid& cb) +void settimeofday_cb (const TrivialCB& cb) { _settimeofday_cb = cb; }