Merge branch 'feature/use_sntp_in_lwip' into 'master'

feat(lwip): Use sntp in lwip

See merge request sdk/ESP8266_RTOS_SDK!121
This commit is contained in:
Wu Jian Gang
2018-05-04 13:54:12 +08:00
10 changed files with 25 additions and 509 deletions

View File

@ -7,7 +7,7 @@ COMPONENT_SRCDIRS := driver source
LIBS ?=
ifndef CONFIG_NO_BLOBS
LIBS += airkiss cirom crypto espnow gcc hal core minic mirom net80211 \
LIBS += airkiss cirom crypto espnow gcc hal core mirom net80211 \
phy pp pwm smartconfig ssc wpa wps
endif

View File

@ -174,6 +174,11 @@ config LWIP_IP6_DEBUG
depends on LWIP_DEBUG
default n
config LWIP_SNTP_DEBUG
bool "Enable debugging for SNTP."
depends on LWIP_DEBUG
default n
config LWIP_SO_REUSE
bool "Enable SO_REUSEADDR option"
default n

View File

@ -1,268 +0,0 @@
/*
* sntp_time.c
*
* Created on: 2016-11-9
* Author: LiuHan
*/
#include "apps/sntp_opts.h"
#include "lwip/apps/sntp/sntp_time.h"
#include "esp_timer.h"
static s8_t sntp_time_timezone = 8;
static u32_t sntp_time_realtime = 0;
static char sntp_time_result[100];
static sntp_tm_type sntp_time_rule[2];
static sntp_tm sntp_time_result_buf;
static const int sntp_time_mon_lengths[2][12] = {
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
} ;
static const int sntp_time_year_lengths[2] = {
365,
366
};
static os_timer_t sntp_system_timer;
static sntp_tm *sntp_mktm_r(const sntp_time_t *tim_p, sntp_tm *res, int is_gmtime)
{
long days, rem;
sntp_time_t lcltime;
int y;
int yleap;
const int *ip;
/* base decision about std/dst time on current time */
lcltime = *tim_p;
days = ((long)lcltime) / SECSPERDAY;
rem = ((long)lcltime) % SECSPERDAY;
while (rem < 0)
{
rem += SECSPERDAY;
--days;
}
while (rem >= SECSPERDAY)
{
rem -= SECSPERDAY;
++days;
}
/* compute hour, min, and sec */
res->tm_hour = (int) (rem / SECSPERHOUR);
rem %= SECSPERHOUR;
res->tm_min = (int) (rem / SECSPERMIN);
res->tm_sec = (int) (rem % SECSPERMIN);
/* compute day of week */
if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
res->tm_wday += DAYSPERWEEK;
/* compute year & day of year */
y = EPOCH_YEAR;
if (days >= 0)
{
for (;;)
{
yleap = isleap(y);
if (days < sntp_time_year_lengths[yleap])
break;
y++;
days -= sntp_time_year_lengths[yleap];
}
}
else
{
do
{
--y;
yleap = isleap(y);
days += sntp_time_year_lengths[yleap];
} while (days < 0);
}
res->tm_year = y - YEAR_BASE;
res->tm_yday = days;
ip = sntp_time_mon_lengths[yleap];
for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon)
days -= ip[res->tm_mon];
res->tm_mday = days + 1;
if (!is_gmtime)
{
int offset;
int hours, mins, secs;
res->tm_isdst = 0;
offset = (res->tm_isdst == 1 ? sntp_time_rule[1].offset : sntp_time_rule[0].offset);
hours = offset / SECSPERHOUR;
offset = offset % SECSPERHOUR;
mins = offset / SECSPERMIN;
secs = offset % SECSPERMIN;
res->tm_sec -= secs;
res->tm_min -= mins;
res->tm_hour -= hours;
if (res->tm_sec >= SECSPERMIN)
{
res->tm_min += 1;
res->tm_sec -= SECSPERMIN;
}
else if (res->tm_sec < 0)
{
res->tm_min -= 1;
res->tm_sec += SECSPERMIN;
}
if (res->tm_min >= MINSPERHOUR)
{
res->tm_hour += 1;
res->tm_min -= MINSPERHOUR;
}
else if (res->tm_min < 0)
{
res->tm_hour -= 1;
res->tm_min += MINSPERHOUR;
}
if (res->tm_hour >= HOURSPERDAY)
{
++res->tm_yday;
++res->tm_wday;
if (res->tm_wday > 6)
res->tm_wday = 0;
++res->tm_mday;
res->tm_hour -= HOURSPERDAY;
if (res->tm_mday > ip[res->tm_mon])
{
res->tm_mday -= ip[res->tm_mon];
res->tm_mon += 1;
if (res->tm_mon == 12)
{
res->tm_mon = 0;
res->tm_year += 1;
res->tm_yday = 0;
}
}
}
else if (res->tm_hour < 0)
{
res->tm_yday -= 1;
res->tm_wday -= 1;
if (res->tm_wday < 0)
res->tm_wday = 6;
res->tm_mday -= 1;
res->tm_hour += 24;
if (res->tm_mday == 0)
{
res->tm_mon -= 1;
if (res->tm_mon < 0)
{
res->tm_mon = 11;
res->tm_year -= 1;
res->tm_yday = 365 + isleap(res->tm_year);
}
res->tm_mday = ip[res->tm_mon];
}
}
}
else
res->tm_isdst = 0;
return (res);
}
static sntp_tm *sntp_localtime_r(const sntp_time_t *tim_p, sntp_tm *res)
{
return sntp_mktm_r (tim_p, res, 0);
}
static sntp_tm *sntp_localtime(const sntp_time_t *tim_p)
{
return sntp_localtime_r (tim_p, &sntp_time_result_buf);
}
static char *sntp_asctime_r(sntp_tm *tim_p ,char *result)
{
static const char day_name[7][4] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char mon_name[12][4] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
sprintf (result, "%s %s %02d %02d:%02d:%02d %02d\n",
day_name[tim_p->tm_wday],
mon_name[tim_p->tm_mon],
tim_p->tm_mday, tim_p->tm_hour, tim_p->tm_min,
tim_p->tm_sec, 1900 + tim_p->tm_year);
return result;
}
static char *sntp_asctime(sntp_tm *tim_p)
{
return sntp_asctime_r (tim_p, sntp_time_result);
}
u32_t sntp_get_current_timestamp(void)
{
if(sntp_time_realtime == 0){
printf("please start sntp first !\n");
return 0;
} else {
return sntp_time_realtime;
}
}
char* sntp_get_real_time(sntp_time_t t)
{
return sntp_asctime(sntp_localtime (&t));
}
/**
* SNTP get sntp_time_timezone default GMT + 8
*/
s8_t sntp_get_timezone(void)
{
return sntp_time_timezone;
}
/**
* SNTP set sntp_time_timezone default GMT + 8
*/
bool sntp_set_timezone(s8_t timezone)
{
if(timezone >= -11 && timezone <= 13) {
sntp_time_timezone = timezone;
return true;
} else {
return false;
}
}
void sntp_set_update_delay(uint32 ms)
{
// SNTP_UPDATE_DELAY = (ms > 15000) ? ms : 15000;
}
static void sntp_inc_time(void* arg)
{
sntp_time_realtime ++;
}
void sntp_set_system_time(sntp_time_t GMT_Time)
{
sntp_time_t Local_Time = GMT_Time;
Local_Time += sntp_time_timezone * 60 * 60;
sntp_time_realtime = Local_Time;
os_timer_disarm(&sntp_system_timer);
os_timer_setfn(&sntp_system_timer, (os_timer_func_t *)sntp_inc_time, NULL);
os_timer_arm(&sntp_system_timer, 1000, 1);
}

View File

@ -1,115 +0,0 @@
/*
time.c - implementation of Time API for esp8266
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "c_types.h"
#include "esp_timer.h"
#include "esp_system.h"
#include "lwip/apps/sntp.h"
#include "lwip/apps/sntp/time.h"
#include "lwip/apps/sntp/sntp_time.h"
static os_timer_t micros_overflow_timer;
static uint32 micros_at_last_overflow_tick = 0;
static uint32 micros_overflow_count = 0;
extern void sntp_set_update_delay(uint32 ms);
static void micros_overflow_tick(void * arg)
{
uint32 m = system_get_time();
if (m < micros_at_last_overflow_tick)
micros_overflow_count ++;
micros_at_last_overflow_tick = m;
}
static void micros_set_default_time(void)
{
os_timer_disarm(&micros_overflow_timer);
os_timer_setfn(&micros_overflow_timer, (os_timer_func_t *)micros_overflow_tick, 0);
os_timer_arm(&micros_overflow_timer, 60 * 1000, 1);
}
unsigned long millis(void)
{
uint32 m = system_get_time();
uint32 c = micros_overflow_count + ((m < micros_at_last_overflow_tick) ? 1 : 0);
return c * 4294967 + m / 1000000;
}
unsigned long micros(void)
{
return system_get_time();
}
void updateTime(uint32 ms)
{
sntp_set_update_delay(ms);
}
static void setServer(int id, const char* name_or_ip)
{
if (name_or_ip){
sntp_setservername(id, (char*)name_or_ip);
}
}
bool configTime(int timezone, int daylightOffset, char *server1, char *server2, char *server3, bool enable)
{
if (server1 == NULL && server2 == NULL && server3 == NULL)
return false;
sntp_stop();
setServer(0, server1);
setServer(1, server2);
setServer(2, server3);
sntp_set_timezone(timezone);
sntp_init();
return true;
}
int __attribute__((weak))
_gettimeofday_r(void *ptr, struct timeval *ptimeval, void *ptimezone)
{
if (ptimeval){
time_t seconds = sntp_get_current_timestamp();
if (seconds == 0){
micros_set_default_time();
seconds = millis();
}
ptimeval->tv_sec = seconds;
ptimeval->tv_usec = 0;
}
return 0;
}
char* getrealtimeofday(void)
{
struct timeval t;
gettimeofday(&t, NULL);
return sntp_get_real_time(t.tv_sec);
}

View File

@ -5,6 +5,6 @@ COMPONENT_ADD_INCLUDEDIRS += lwip/src/include lwip/src/include/lwip lwip/src/inc
include/lwip/apps include/lwip/port apps
COMPONENT_SRCDIRS += lwip/src/api lwip/src/apps/sntp lwip/src/netif lwip/src/core lwip/src/core/ipv4 lwip/src/core/ipv6 \
port/freertos port/netif apps/dhcpserver apps/sntp apps/multi-threads
port/freertos port/netif apps/dhcpserver apps/multi-threads
CFLAGS += -Wno-address #lots of LWIP source files evaluate macros that check address of stack variables

View File

@ -1,61 +0,0 @@
/*
* sntp_time.h
*
* Created on: 2016-11-9
* Author: LiuHan
*/
#ifndef SNTP_TIME_H_
#define SNTP_TIME_H_
#include "lwip/opt.h"
#include "lwip/timeouts.h"
#define SECSPERMIN 60L
#define MINSPERHOUR 60L
#define HOURSPERDAY 24L
#define SECSPERHOUR (SECSPERMIN * MINSPERHOUR)
#define SECSPERDAY (SECSPERHOUR * HOURSPERDAY)
#define DAYSPERWEEK 7
#define MONSPERYEAR 12
#define YEAR_BASE 1900
#define EPOCH_YEAR 1970
#define EPOCH_WDAY 4
#define EPOCH_YEARS_SINCE_LEAP 2
#define EPOCH_YEARS_SINCE_CENTURY 70
#define EPOCH_YEARS_SINCE_LEAP_CENTURY 370
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
typedef long sntp_time_t;
typedef struct{
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
}sntp_tm;
typedef struct{
char ch;
int m;
int n;
int d;
int s;
sntp_time_t change;
int offset;
}sntp_tm_type;
void sntp_set_system_time(sntp_time_t GMT_Time);
bool sntp_set_timezone(s8_t timezone);
u32_t sntp_get_current_timestamp(void);
char* sntp_get_real_time(sntp_time_t t);
#endif /* SNTP_TIME_H_ */

View File

@ -1,57 +0,0 @@
/*
* time.h
*
* Created on: May 31, 2016
* Author: liuhan
*/
#ifndef TIME_H_
#define TIME_H_
#include "c_types.h"
#include "esp8266/eagle_soc.h"
#if !NO_SYS
#include "lwip/sockets.h"
#else
struct timeval {
unsigned long tv_sec; /* seconds */
unsigned long tv_usec; /* and microseconds */
};
#endif
/***************************RTC TIME OPTION***************************************/
// daylight settings
// Base calculated with value obtained from NTP server (64 bits)
#define sntp_base (*((uint64_t*)RTC_SCRATCH0))
// Timer value when base was obtained
#define TIM_REF_SET(value) WRITE_PERI_REG(RTC_SCRATCH2, value)
#define TIM_REF_GET() READ_PERI_REG(RTC_SCRATCH2)
// Setters and getters for CAL, TZ and DST.
#define RTC_CAL_SET(val) do {uint32 value = READ_PERI_REG(RTC_SCRATCH3);\
value |= ((val) & 0x0000FFFF);\
WRITE_PERI_REG(RTC_SCRATCH3, value);\
}while(0)
#define RTC_DST_SET(val) do {uint32 value = READ_PERI_REG(RTC_SCRATCH3);\
value |= (((val)<<16) & 0x00010000);\
WRITE_PERI_REG(RTC_SCRATCH3, value);\
}while(0)
#define RTC_TZ_SET(val) do {uint32 value = READ_PERI_REG(RTC_SCRATCH3);\
value |= (((val)<<24) & 0xFF000000);\
WRITE_PERI_REG(RTC_SCRATCH3, value);\
}while(0)
#define RTC_CAL_GET() (READ_PERI_REG(RTC_SCRATCH3) & 0x0000FFFF)
#define RTC_DST_GET() ((READ_PERI_REG(RTC_SCRATCH3) & 0x00010000)>>16)
#define RTC_TZ_GET() ((((int)READ_PERI_REG(RTC_SCRATCH3)) & ((int)0xFF000000))>>24)
void system_update_rtc(time_t t, uint32 us);
time_t sntp_get_rtc_time(sint32 *us);
int gettimeofday(struct timeval* t, void* timezone);
void updateTime(uint32 ms);
bool configTime(int timezone, int daylightOffset, char *server1, char *server2, char *server3, bool enable);
time_t time(time_t *t);
unsigned long millis(void);
unsigned long micros(void);
#endif /* TIME_H_ */

View File

@ -86,4 +86,6 @@ typedef int sys_prot_t;
#define LWIP_PLATFORM_HTONS(_n) ((u16_t)((((_n) & 0xff) << 8) | (((_n) >> 8) & 0xff)))
#define LWIP_PLATFORM_HTONL(_n) ((u32_t)( (((_n) & 0xff) << 24) | (((_n) & 0xff00) << 8) | (((_n) >> 8) & 0xff00) | (((_n) >> 24) & 0xff) ))
#define LWIP_TIMEVAL_PRIVATE 0
#endif /* __ARCH_CC_H__ */

View File

@ -712,11 +712,14 @@ extern void vPortFree(void *pv, const char * file, unsigned line);
#define SNTP_SERVER_DNS 1
#ifndef sntp_time_t
typedef long sntp_time_t;
#endif
extern void sntp_set_system_time(sntp_time_t GMT_Time);
#define SNTP_SET_SYSTEM_TIME sntp_set_system_time
#include <sys/time.h>
#define SNTP_SET_SYSTEM_TIME_US(sec, us) \
do { \
struct timeval tv = { .tv_sec = sec, .tv_usec = us }; \
settimeofday(&tv, NULL); \
} while (0);
/*
----------------------------------
----- Multicast/IGMP options -----
@ -2135,6 +2138,14 @@ extern void sntp_set_system_time(sntp_time_t GMT_Time);
#if CONFIG_LWIP_IP6_DEBUG
#define IP6_DEBUG LWIP_DBG_ON
#endif
/**
* SNTP_DEBUG: Enable debugging for SNTP.
*/
#if CONFIG_LWIP_SNTP_DEBUG
#define SNTP_DEBUG LWIP_DBG_ON
#endif
/**
* @}
*/

View File

@ -42,7 +42,6 @@ extern "C" {
#endif
#include "esp_common.h"
#include "lwip/apps/sntp/time.h"
#if 0
#define ssl_printf(fmt, args...) os_printf(fmt,## args)