Skip to content

Commit 9913e52

Browse files
d-a-vdevyte
authored andcommitted
handle tv.tv_usec in settimeofday() (#4001)
optional settimeofday()'s callback fix #1679
1 parent d5bb4a9 commit 9913e52

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed

cores/esp8266/coredecls.h

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@
22
#ifndef __COREDECLS_H
33
#define __COREDECLS_H
44

5-
extern bool s_bootTimeSet;
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
68

79
// TODO: put declarations here, get rid of -Wno-implicit-function-declaration
810

11+
extern bool timeshift64_is_set;
12+
13+
void tune_timeshift64 (uint64_t now_us);
14+
void settimeofday_cb (void (*cb)(void));
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
920
#endif // __COREDECLS_H

cores/esp8266/sntp-lwip2.c

+16-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* TODOs:
3535
* settimeofday(): handle tv->tv_usec
3636
* sntp_mktm_r(): review, fix DST handling (this one is currently untouched from lwip-1.4)
37+
* implement adjtime()
3738
*/
3839

3940
#include <lwip/init.h>
@@ -42,6 +43,13 @@
4243
#include <os_type.h>
4344
#include "coredecls.h"
4445

46+
static void (*_settimeofday_cb)(void) = NULL;
47+
48+
void settimeofday_cb (void (*cb)(void))
49+
{
50+
_settimeofday_cb = cb;
51+
}
52+
4553
#if LWIP_VERSION_MAJOR == 1
4654

4755
#include <pgmspace.h>
@@ -58,10 +66,11 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
5866
}
5967
if (tv) /* after*/
6068
{
69+
// can't call lwip1.4's static sntp_set_system_time()
6170
os_printf(stod14);
6271

6372
// reset time subsystem
64-
s_bootTimeSet = false;
73+
timeshift64_is_set = false;
6574

6675
return -1;
6776
}
@@ -440,11 +449,13 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
440449
}
441450
if (tv) /* after*/
442451
{
443-
sntp_set_system_time(tv->tv_sec);
444-
// XXX FIXME TODO: efficiently use provided tv->tv_sec
445-
446452
// reset time subsystem
447-
s_bootTimeSet = false;
453+
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
454+
455+
sntp_set_system_time(tv->tv_sec);
456+
457+
if (_settimeofday_cb)
458+
_settimeofday_cb();
448459
}
449460
return 0;
450461
}

cores/esp8266/time.c

+8-16
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,13 @@ extern uint64_t micros64();
3737
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
3838
#define DIFF1900TO1970 2208988800UL
3939

40-
bool s_bootTimeSet = false;
41-
static uint64_t s_bootTime_us = 0;
40+
bool timeshift64_is_set = false;
41+
static uint64_t timeshift64 = 0;
4242

43-
// calculate offset used in gettimeofday
44-
static void ensureBootTimeIsSet()
43+
void tune_timeshift64 (uint64_t now_us)
4544
{
46-
// Check just a bool flag instead of the full 64-bit s_bootTime for zero.
47-
if (!s_bootTimeSet)
48-
{
49-
time_t now_s = sntp_get_current_timestamp();
50-
if (now_s)
51-
{
52-
s_bootTime_us = now_s * 1000000ULL - micros64();
53-
s_bootTimeSet = true;
54-
}
55-
}
45+
timeshift64 = now_us - micros64();
46+
timeshift64_is_set = true;
5647
}
5748

5849
static void setServer(int id, const char* name_or_ip)
@@ -102,8 +93,9 @@ int _gettimeofday_r(struct _reent* unused, struct timeval *tp, void *tzp)
10293
(void) tzp;
10394
if (tp)
10495
{
105-
ensureBootTimeIsSet();
106-
uint64_t currentTime_us = s_bootTime_us + micros64();
96+
if (!timeshift64_is_set)
97+
tune_timeshift64(sntp_get_current_timestamp() * 1000000ULL);
98+
uint64_t currentTime_us = timeshift64 + micros64();
10799
tp->tv_sec = currentTime_us / 1000000ULL;
108100
tp->tv_usec = currentTime_us % 1000000ULL;
109101
}

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@
1212
This example code is in the public domain.
1313
*/
1414

15+
#include <ESP8266WiFi.h>
1516
#include <time.h> // time() ctime()
1617
#include <sys/time.h> // struct timeval
17-
#include <ESP8266WiFi.h>
18+
#include <coredecls.h> // settimeofday_cb()
1819

1920
////////////////////////////////////////////////////////
2021

@@ -32,8 +33,19 @@
3233
#define TZ_SEC ((TZ)*3600)
3334
#define DST_SEC ((DST_MN)*60)
3435

36+
timeval cbtime; // time set in callback
37+
bool cbtime_set = false;
38+
39+
void time_is_set (void)
40+
{
41+
gettimeofday(&cbtime, NULL);
42+
cbtime_set = true;
43+
Serial.println("------------------ settimeofday() was called ------------------");
44+
}
45+
3546
void setup() {
3647
Serial.begin(115200);
48+
settimeofday_cb(time_is_set);
3749

3850
#if NTP0_OR_LOCAL1
3951
// local

0 commit comments

Comments
 (0)