From c55f49bd6103dab81c6a389470f6d5bbbee399d0 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Sun, 6 Oct 2019 22:50:57 +0200 Subject: [PATCH] use a scheduled function for settimeofday_cb (#6600) * use a scheduled function for settimeofday_cb * per review * use a generic and clear name for trivial functional variable type name used for callbacks --- cores/esp8266/coredecls.h | 9 ++++++++- cores/esp8266/sntp-lwip2.cpp | 14 ++++++++++---- .../examples/NTP-TZ-DST/NTP-TZ-DST.ino | 19 +++++-------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/cores/esp8266/coredecls.h b/cores/esp8266/coredecls.h index 4a70609bf..2496995b0 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,14 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff); #ifdef __cplusplus } + +#include + +using TrivialCB = std::function; + +void settimeofday_cb (TrivialCB&& cb); +void settimeofday_cb (const TrivialCB& cb); + #endif #endif // __COREDECLS_H diff --git a/cores/esp8266/sntp-lwip2.cpp b/cores/esp8266/sntp-lwip2.cpp index 0dba9a7f8..063cc5be2 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" { +static TrivialCB _settimeofday_cb; -static void (*_settimeofday_cb)(void) = NULL; +void settimeofday_cb (TrivialCB&& cb) +{ + _settimeofday_cb = std::move(cb); +} -void settimeofday_cb (void (*cb)(void)) +void settimeofday_cb (const TrivialCB& 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 b3c4e2064..ebfbd112b 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)