1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-25 06:22:11 +03:00

handle tv.tv_usec in settimeofday() (#4001)

optional settimeofday()'s callback
fix #1679
This commit is contained in:
david gauchard
2017-12-24 21:48:25 +01:00
committed by Develo
parent d5bb4a99f6
commit 9913e52107
4 changed files with 49 additions and 23 deletions

View File

@ -2,8 +2,19 @@
#ifndef __COREDECLS_H
#define __COREDECLS_H
extern bool s_bootTimeSet;
#ifdef __cplusplus
extern "C" {
#endif
// TODO: put declarations here, get rid of -Wno-implicit-function-declaration
extern bool timeshift64_is_set;
void tune_timeshift64 (uint64_t now_us);
void settimeofday_cb (void (*cb)(void));
#ifdef __cplusplus
}
#endif
#endif // __COREDECLS_H

View File

@ -34,6 +34,7 @@
* TODOs:
* settimeofday(): handle tv->tv_usec
* sntp_mktm_r(): review, fix DST handling (this one is currently untouched from lwip-1.4)
* implement adjtime()
*/
#include <lwip/init.h>
@ -42,6 +43,13 @@
#include <os_type.h>
#include "coredecls.h"
static void (*_settimeofday_cb)(void) = NULL;
void settimeofday_cb (void (*cb)(void))
{
_settimeofday_cb = cb;
}
#if LWIP_VERSION_MAJOR == 1
#include <pgmspace.h>
@ -58,10 +66,11 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
}
if (tv) /* after*/
{
// can't call lwip1.4's static sntp_set_system_time()
os_printf(stod14);
// reset time subsystem
s_bootTimeSet = false;
timeshift64_is_set = false;
return -1;
}
@ -440,11 +449,13 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
}
if (tv) /* after*/
{
sntp_set_system_time(tv->tv_sec);
// XXX FIXME TODO: efficiently use provided tv->tv_sec
// reset time subsystem
s_bootTimeSet = false;
tune_timeshift64(tv->tv_sec * 1000000ULL + tv->tv_usec);
sntp_set_system_time(tv->tv_sec);
if (_settimeofday_cb)
_settimeofday_cb();
}
return 0;
}

View File

@ -37,22 +37,13 @@ extern uint64_t micros64();
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
#define DIFF1900TO1970 2208988800UL
bool s_bootTimeSet = false;
static uint64_t s_bootTime_us = 0;
bool timeshift64_is_set = false;
static uint64_t timeshift64 = 0;
// calculate offset used in gettimeofday
static void ensureBootTimeIsSet()
void tune_timeshift64 (uint64_t now_us)
{
// Check just a bool flag instead of the full 64-bit s_bootTime for zero.
if (!s_bootTimeSet)
{
time_t now_s = sntp_get_current_timestamp();
if (now_s)
{
s_bootTime_us = now_s * 1000000ULL - micros64();
s_bootTimeSet = true;
}
}
timeshift64 = now_us - micros64();
timeshift64_is_set = true;
}
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)
(void) tzp;
if (tp)
{
ensureBootTimeIsSet();
uint64_t currentTime_us = s_bootTime_us + micros64();
if (!timeshift64_is_set)
tune_timeshift64(sntp_get_current_timestamp() * 1000000ULL);
uint64_t currentTime_us = timeshift64 + micros64();
tp->tv_sec = currentTime_us / 1000000ULL;
tp->tv_usec = currentTime_us % 1000000ULL;
}

View File

@ -12,9 +12,10 @@
This example code is in the public domain.
*/
#include <ESP8266WiFi.h>
#include <time.h> // time() ctime()
#include <sys/time.h> // struct timeval
#include <ESP8266WiFi.h>
#include <coredecls.h> // settimeofday_cb()
////////////////////////////////////////////////////////
@ -32,8 +33,19 @@
#define TZ_SEC ((TZ)*3600)
#define DST_SEC ((DST_MN)*60)
timeval cbtime; // time set in callback
bool cbtime_set = false;
void time_is_set (void)
{
gettimeofday(&cbtime, NULL);
cbtime_set = true;
Serial.println("------------------ settimeofday() was called ------------------");
}
void setup() {
Serial.begin(115200);
settimeofday_cb(time_is_set);
#if NTP0_OR_LOCAL1
// local