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:
@ -2,8 +2,19 @@
|
|||||||
#ifndef __COREDECLS_H
|
#ifndef __COREDECLS_H
|
||||||
#define __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
|
// 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
|
#endif // __COREDECLS_H
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
* TODOs:
|
* TODOs:
|
||||||
* settimeofday(): handle tv->tv_usec
|
* settimeofday(): handle tv->tv_usec
|
||||||
* sntp_mktm_r(): review, fix DST handling (this one is currently untouched from lwip-1.4)
|
* sntp_mktm_r(): review, fix DST handling (this one is currently untouched from lwip-1.4)
|
||||||
|
* implement adjtime()
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <lwip/init.h>
|
#include <lwip/init.h>
|
||||||
@ -42,6 +43,13 @@
|
|||||||
#include <os_type.h>
|
#include <os_type.h>
|
||||||
#include "coredecls.h"
|
#include "coredecls.h"
|
||||||
|
|
||||||
|
static void (*_settimeofday_cb)(void) = NULL;
|
||||||
|
|
||||||
|
void settimeofday_cb (void (*cb)(void))
|
||||||
|
{
|
||||||
|
_settimeofday_cb = cb;
|
||||||
|
}
|
||||||
|
|
||||||
#if LWIP_VERSION_MAJOR == 1
|
#if LWIP_VERSION_MAJOR == 1
|
||||||
|
|
||||||
#include <pgmspace.h>
|
#include <pgmspace.h>
|
||||||
@ -58,10 +66,11 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
|
|||||||
}
|
}
|
||||||
if (tv) /* after*/
|
if (tv) /* after*/
|
||||||
{
|
{
|
||||||
|
// can't call lwip1.4's static sntp_set_system_time()
|
||||||
os_printf(stod14);
|
os_printf(stod14);
|
||||||
|
|
||||||
// reset time subsystem
|
// reset time subsystem
|
||||||
s_bootTimeSet = false;
|
timeshift64_is_set = false;
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -440,11 +449,13 @@ int settimeofday(const struct timeval* tv, const struct timezone* tz)
|
|||||||
}
|
}
|
||||||
if (tv) /* after*/
|
if (tv) /* after*/
|
||||||
{
|
{
|
||||||
sntp_set_system_time(tv->tv_sec);
|
|
||||||
// XXX FIXME TODO: efficiently use provided tv->tv_sec
|
|
||||||
|
|
||||||
// reset time subsystem
|
// 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -37,22 +37,13 @@ extern uint64_t micros64();
|
|||||||
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
|
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
|
||||||
#define DIFF1900TO1970 2208988800UL
|
#define DIFF1900TO1970 2208988800UL
|
||||||
|
|
||||||
bool s_bootTimeSet = false;
|
bool timeshift64_is_set = false;
|
||||||
static uint64_t s_bootTime_us = 0;
|
static uint64_t timeshift64 = 0;
|
||||||
|
|
||||||
// calculate offset used in gettimeofday
|
void tune_timeshift64 (uint64_t now_us)
|
||||||
static void ensureBootTimeIsSet()
|
|
||||||
{
|
{
|
||||||
// Check just a bool flag instead of the full 64-bit s_bootTime for zero.
|
timeshift64 = now_us - micros64();
|
||||||
if (!s_bootTimeSet)
|
timeshift64_is_set = true;
|
||||||
{
|
|
||||||
time_t now_s = sntp_get_current_timestamp();
|
|
||||||
if (now_s)
|
|
||||||
{
|
|
||||||
s_bootTime_us = now_s * 1000000ULL - micros64();
|
|
||||||
s_bootTimeSet = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void setServer(int id, const char* name_or_ip)
|
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;
|
(void) tzp;
|
||||||
if (tp)
|
if (tp)
|
||||||
{
|
{
|
||||||
ensureBootTimeIsSet();
|
if (!timeshift64_is_set)
|
||||||
uint64_t currentTime_us = s_bootTime_us + micros64();
|
tune_timeshift64(sntp_get_current_timestamp() * 1000000ULL);
|
||||||
|
uint64_t currentTime_us = timeshift64 + micros64();
|
||||||
tp->tv_sec = currentTime_us / 1000000ULL;
|
tp->tv_sec = currentTime_us / 1000000ULL;
|
||||||
tp->tv_usec = currentTime_us % 1000000ULL;
|
tp->tv_usec = currentTime_us % 1000000ULL;
|
||||||
}
|
}
|
||||||
|
@ -12,9 +12,10 @@
|
|||||||
This example code is in the public domain.
|
This example code is in the public domain.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
#include <time.h> // time() ctime()
|
#include <time.h> // time() ctime()
|
||||||
#include <sys/time.h> // struct timeval
|
#include <sys/time.h> // struct timeval
|
||||||
#include <ESP8266WiFi.h>
|
#include <coredecls.h> // settimeofday_cb()
|
||||||
|
|
||||||
////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////
|
||||||
|
|
||||||
@ -32,8 +33,19 @@
|
|||||||
#define TZ_SEC ((TZ)*3600)
|
#define TZ_SEC ((TZ)*3600)
|
||||||
#define DST_SEC ((DST_MN)*60)
|
#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() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
settimeofday_cb(time_is_set);
|
||||||
|
|
||||||
#if NTP0_OR_LOCAL1
|
#if NTP0_OR_LOCAL1
|
||||||
// local
|
// local
|
||||||
|
Reference in New Issue
Block a user