Skip to content

lwip2: stability and time management #3808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions cores/esp8266/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

#include <time.h>
#include <sys/time.h>
#include <sys/reent.h>
#include "sntp.h"

Expand All @@ -36,8 +37,6 @@ extern uint64_t micros64();
// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time)
#define DIFF1900TO1970 2208988800UL

static int s_daylightOffset_sec = 0;
static long s_timezone_sec = 0;
static bool s_bootTimeSet = false;
static uint64_t s_bootTime_us = 0;

Expand Down Expand Up @@ -73,12 +72,29 @@ void configTime(int timezone, int daylightOffset_sec, const char* server1, const
setServer(1, server2);
setServer(2, server3);

s_timezone_sec = timezone;
s_daylightOffset_sec = daylightOffset_sec;
//s_timezone_sec = timezone;
//s_daylightOffset_sec = daylightOffset_sec;
sntp_set_timezone(timezone/3600);
sntp_set_daylight(daylightOffset_sec);
sntp_init();
}

int settimeofday(const struct timeval* tv, const struct timezone* tz)
{
if (tz) /*before*/
{
sntp_set_timezone(tz->tz_minuteswest / 60);
// apparently tz->tz_dsttime is a bitfield and should not be further used (cf man)
sntp_set_daylight(0);
}
if (tv) /* after*/
{
sntp_set_system_time(tv->tv_sec);
// ignore tv->usec
}
return 0;
}

int clock_gettime(clockid_t unused, struct timespec *tp)
{
(void) unused;
Expand Down
93 changes: 93 additions & 0 deletions libraries/esp8266/examples/NTP-TZ-DST/NTP-TZ-DST.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@

#include <time.h> // time() ctime()
#include <sys/time.h> // struct timeval
#include <ESP8266WiFi.h>

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

#define SSID "open"
#define SSIDPWD ""
#define TZ 1 // utc+TZ in hours
#define DST_MN 60 // use 60 for summer time in some countries

#define NTP0_OR_LOCAL1 1 // 0:use NTP 1:fake external RTC
#define RTC_TEST 1510592825 // 1510592825 = Monday 13 November 2017 17:07:05 UTC

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

#define TZ_MN ((TZ)*60)
#define TZ_SEC ((TZ)*3600)
#define DST_SEC ((DST_MN)*60)

void setup() {
Serial.begin(115200);

#if NTP0_OR_LOCAL1

// local
ESP.eraseConfig();
time_t rtc = RTC_TEST;
timeval tv = { rtc, 0 };
timezone tz = { TZ_MN + DST_MN, 0 };
settimeofday(&tv, &tz);

#else

// NTP
configTime(TZ_SEC, DST_SEC, "pool.ntp.org");
WiFi.mode(WIFI_STA);
WiFi.begin(SSID, SSIDPWD);
// don't wait

#endif
}

// for testing purpose:
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);

void loop() {

// same result as with time()
timeval tv;
gettimeofday(&tv, NULL);
Serial.print("gtod:");
Serial.print((uint32_t)tv.tv_sec);
#if 0
// but usec is wrong
Serial.print("/");
Serial.print((uint32_t)tv.tv_usec);
#endif

// same result as with millis()/1000
timespec tp;
clock_gettime(0, &tp);
Serial.print(" -- clock:");
Serial.print((uint32_t)tp.tv_sec);
#if 0
// nsec is wrong
Serial.print("/");
Serial.print((uint32_t)tp.tv_nsec/1000000);
Serial.print("ms");
#endif

// gives EPOCH+tz+dst
time_t now = time(nullptr);
Serial.print(" -- time:");
Serial.print((uint32_t)now);

// from boot:
Serial.print(" -- millis:");
Serial.print((uint32_t)millis());
Serial.print(" -- micros:");
Serial.print((uint32_t)micros());

// human readable
Serial.print(" -- ctime:(UTC+");
Serial.print((uint32_t)(TZ*60 + DST_MN));
Serial.print("mn)");
Serial.print(ctime(&now));

// simple drifting loop
delay(1000);
}

Binary file modified tools/sdk/lib/liblwip2.a
Binary file not shown.
12 changes: 9 additions & 3 deletions tools/sdk/lwip2/include/arch/cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,17 @@ extern "C"
#ifdef __cplusplus
}
#endif

void sntp_set_system_time (uint32_t t);

#endif // defined(LWIP_BUILD)

#ifdef __cplusplus
extern "C"
{
#endif
void sntp_set_system_time (uint32_t t); // also provided to user
#ifdef __cplusplus
}
#endif

#include "mem.h" // useful for os_malloc used in esp-arduino's mDNS

typedef uint32_t sys_prot_t; // not really used
Expand Down
5 changes: 4 additions & 1 deletion tools/sdk/lwip2/include/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -2986,7 +2986,10 @@
--------------------------------------------------
*/
#define SNTP_SERVER_DNS 1 // SNTP support DNS names through sntp_setservername / sntp_getservername
#define SNTP_SERVER_ADDRESS "pool.ntp.org" // default
// if SNTP_SERVER_ADDRESS is defined, it always overrides user's config
// so we do not define it. sntp server can come from dhcp server, or by
// user.
//#define SNTP_SERVER_ADDRESS "pool.ntp.org" // default
#define SNTP_GET_SERVERS_FROM_DHCP 1
#define SNTP_SET_SYSTEM_TIME(t) (sntp_set_system_time(t)) // implemented in lwip2-sntp.c

Expand Down