Skip to content

Commit

Permalink
handle tv.tv_usec in settimeofday() (#4001)
Browse files Browse the repository at this point in the history
optional settimeofday()'s callback
fix #1679
  • Loading branch information
d-a-v authored and devyte committed Dec 24, 2017
1 parent d5bb4a9 commit 9913e52
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
13 changes: 12 additions & 1 deletion cores/esp8266/coredecls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
21 changes: 16 additions & 5 deletions cores/esp8266/sntp-lwip2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
24 changes: 8 additions & 16 deletions cores/esp8266/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
14 changes: 13 additions & 1 deletion libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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()

////////////////////////////////////////////////////////

Expand All @@ -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
Expand Down

0 comments on commit 9913e52

Please sign in to comment.