1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-07 06:01:35 +03:00

settimeofday_cb: distinguish from user or sntp (#7637)

* settimeofday_cb: distinguish from user or sntp
This commit is contained in:
david gauchard
2020-10-23 10:43:45 +02:00
committed by GitHub
parent ace5e98bf6
commit 95fb104562
12 changed files with 89 additions and 48 deletions

View File

@ -28,9 +28,10 @@ uint32_t crc32 (const void* data, size_t length, uint32_t crc = 0xffffffff);
#include <functional>
using BoolCB = std::function<void(bool)>;
using TrivialCB = std::function<void()>;
void settimeofday_cb (TrivialCB&& cb);
void settimeofday_cb (const BoolCB& cb);
void settimeofday_cb (const TrivialCB& cb);
#endif

View File

@ -204,14 +204,14 @@ void configTime(const char* tz, const char* server1, const char* server2, const
sntp_init();
}
static TrivialCB _settimeofday_cb;
void settimeofday_cb (TrivialCB&& cb)
{
_settimeofday_cb = std::move(cb);
}
static BoolCB _settimeofday_cb;
void settimeofday_cb (const TrivialCB& cb)
{
_settimeofday_cb = [cb](bool sntp) { (void)sntp; cb(); };
}
void settimeofday_cb (const BoolCB& cb)
{
_settimeofday_cb = cb;
}
@ -222,6 +222,20 @@ extern "C" {
int settimeofday(const struct timeval* tv, const struct timezone* tz)
{
bool from_sntp;
if (tz == (struct timezone*)0xFeedC0de)
{
// This special constant is used by lwip2/SNTP calling
// settimeofday(sntp-time, 0xfeedc0de), secretly using the
// obsolete-but-yet-still-there `tz` field.
// It allows to avoid duplicating this function and inform user
// about the source time change.
tz = nullptr;
from_sntp = true;
}
else
from_sntp = false;
if (tz || !tv)
// tz is obsolete (cf. man settimeofday)
return EINVAL;
@ -230,7 +244,7 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
if (_settimeofday_cb)
schedule_recurrent_function_us([](){ _settimeofday_cb(); return false; }, 0);
schedule_recurrent_function_us([from_sntp](){ _settimeofday_cb(from_sntp); return false; }, 0);
return 0;
}