Skip to content

Commit c55f49b

Browse files
authored
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
1 parent d1b70df commit c55f49b

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

cores/esp8266/coredecls.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,21 @@ extern bool timeshift64_is_set;
1717
void esp_yield();
1818
void esp_schedule();
1919
void tune_timeshift64 (uint64_t now_us);
20-
void settimeofday_cb (void (*cb)(void));
2120
void disable_extra4k_at_link_time (void) __attribute__((noinline));
2221

2322
uint32_t sqrt32 (uint32_t n);
2423
uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff);
2524

2625
#ifdef __cplusplus
2726
}
27+
28+
#include <functional>
29+
30+
using TrivialCB = std::function<void()>;
31+
32+
void settimeofday_cb (TrivialCB&& cb);
33+
void settimeofday_cb (const TrivialCB& cb);
34+
2835
#endif
2936

3037
#endif // __COREDECLS_H

cores/esp8266/sntp-lwip2.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,22 @@
4242
#include <osapi.h>
4343
#include <os_type.h>
4444
#include "coredecls.h"
45+
#include "Schedule.h"
4546

46-
extern "C" {
47+
static TrivialCB _settimeofday_cb;
4748

48-
static void (*_settimeofday_cb)(void) = NULL;
49+
void settimeofday_cb (TrivialCB&& cb)
50+
{
51+
_settimeofday_cb = std::move(cb);
52+
}
4953

50-
void settimeofday_cb (void (*cb)(void))
54+
void settimeofday_cb (const TrivialCB& cb)
5155
{
5256
_settimeofday_cb = cb;
5357
}
5458

59+
extern "C" {
60+
5561
#if LWIP_VERSION_MAJOR == 1
5662

5763
#include <pgmspace.h>
@@ -478,7 +484,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
478484
sntp_set_system_time(tv->tv_sec);
479485

480486
if (_settimeofday_cb)
481-
_settimeofday_cb();
487+
schedule_function(_settimeofday_cb);
482488
}
483489
return 0;
484490
}

libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ void printTm(const char* what, const tm* tm) {
152152
void time_is_set_scheduled() {
153153
// everything is allowed in this function
154154

155+
if (time_machine_days == 0) {
156+
time_machine_running = !time_machine_running;
157+
}
158+
155159
// time machine demo
156160
if (time_machine_running) {
157161
if (time_machine_days == 0)
@@ -178,19 +182,6 @@ void time_is_set_scheduled() {
178182
}
179183
}
180184

181-
void time_is_set_callback() {
182-
// As in an ISR,
183-
// it is not allowed to call "heavy" core API
184-
// like yield()/delay()/print()/network/...
185-
// If this is needed, use a scheduled function.
186-
187-
// This scheduled function is used for the demo, it is normaly unneeded
188-
schedule_function(time_is_set_scheduled);
189-
if (time_machine_days == 0) {
190-
time_machine_running = !time_machine_running;
191-
}
192-
}
193-
194185
void setup() {
195186
Serial.begin(115200);
196187
Serial.println("\nStarting...\n");
@@ -204,7 +195,7 @@ void setup() {
204195

205196
// install callback - called when settimeofday is called (by SNTP or us)
206197
// once enabled (by DHCP), SNTP is updated every hour
207-
settimeofday_cb(time_is_set_callback);
198+
settimeofday_cb(time_is_set_scheduled);
208199

209200
// NTP servers may be overriden by your DHCP server for a more local one
210201
// (see below)

0 commit comments

Comments
 (0)