|
| 1 | +/* |
| 2 | + * time.c - ESP8266-specific functions for SNTP |
| 3 | + * Copyright (c) 2015 Peter Dobler. All rights reserved. |
| 4 | + * This file is part of the esp8266 core for Arduino environment. |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 2.1 of the License, or (at your option) any later version. |
| 9 | + * This library is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU Lesser General Public License for more details. |
| 13 | + * You should have received a copy of the GNU Lesser General Public |
| 14 | + * License along with this library; if not, write to the Free Software |
| 15 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +#include <time.h> |
| 20 | +#include <sntp.h> |
| 21 | + |
| 22 | +extern uint32_t system_get_time(void); |
| 23 | +extern uint64_t system_mktime(uint32_t year, uint32_t mon, uint32_t day, uint32_t hour, uint32_t min, uint32_t sec); |
| 24 | + |
| 25 | +static int errno_var = 0; |
| 26 | + |
| 27 | +int* __errno(void) { |
| 28 | + // DEBUGV("__errno is called last error: %d (not current)\n", errno_var); |
| 29 | + return &errno_var; |
| 30 | +} |
| 31 | + |
| 32 | +unsigned long millis(void) |
| 33 | +{ |
| 34 | + return system_get_time() / 1000UL; |
| 35 | +} |
| 36 | + |
| 37 | +unsigned long micros(void) |
| 38 | +{ |
| 39 | + return system_get_time(); |
| 40 | +} |
| 41 | + |
| 42 | +#ifndef _TIMEVAL_DEFINED |
| 43 | +#define _TIMEVAL_DEFINED |
| 44 | +struct timeval { |
| 45 | + time_t tv_sec; |
| 46 | + suseconds_t tv_usec; |
| 47 | +}; |
| 48 | +#endif |
| 49 | + |
| 50 | +extern char* sntp_asctime(const struct tm *t); |
| 51 | +extern struct tm* sntp_localtime(const time_t *clock); |
| 52 | + |
| 53 | +// time gap in seconds from 01.01.1900 (NTP time) to 01.01.1970 (UNIX time) |
| 54 | +#define DIFF1900TO1970 2208988800UL |
| 55 | + |
| 56 | +static int s_daylightOffset_sec = 0; |
| 57 | +static long s_timezone_sec = 0; |
| 58 | +static time_t s_bootTime = 0; |
| 59 | + |
| 60 | +// calculate offset used in gettimeofday |
| 61 | +static void ensureBootTimeIsSet() |
| 62 | +{ |
| 63 | + if (!s_bootTime) |
| 64 | + { |
| 65 | + time_t now = sntp_get_current_timestamp(); |
| 66 | + if (now) |
| 67 | + { |
| 68 | + s_bootTime = now - millis() / 1000; |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +static void setServer(int id, const char* name_or_ip) |
| 74 | +{ |
| 75 | + if (name_or_ip) |
| 76 | + { |
| 77 | + //TODO: check whether server is given by name or IP |
| 78 | + sntp_setservername(id, (char*) name_or_ip); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) |
| 83 | +{ |
| 84 | + sntp_stop(); |
| 85 | + |
| 86 | + setServer(0, server1); |
| 87 | + setServer(1, server2); |
| 88 | + setServer(2, server3); |
| 89 | + |
| 90 | + s_timezone_sec = timezone; |
| 91 | + s_daylightOffset_sec = daylightOffset_sec; |
| 92 | + sntp_set_timezone(timezone/3600); |
| 93 | + sntp_init(); |
| 94 | +} |
| 95 | + |
| 96 | +int clock_gettime(clockid_t unused, struct timespec *tp) |
| 97 | +{ |
| 98 | + tp->tv_sec = millis() / 1000; |
| 99 | + tp->tv_nsec = micros() * 1000; |
| 100 | + return 0; |
| 101 | +} |
| 102 | + |
| 103 | +// seconds since 1970 |
| 104 | +time_t mktime(struct tm *t) |
| 105 | +{ |
| 106 | + // system_mktime expects month in range 1..12 |
| 107 | + #define START_MONTH 1 |
| 108 | + return DIFF1900TO1970 + system_mktime(t->tm_year, t->tm_mon + START_MONTH, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec); |
| 109 | +} |
| 110 | + |
| 111 | +time_t time(time_t * t) |
| 112 | +{ |
| 113 | + time_t seconds = sntp_get_current_timestamp(); |
| 114 | + if (t) |
| 115 | + { |
| 116 | + *t = seconds; |
| 117 | + } |
| 118 | + return seconds; |
| 119 | +} |
| 120 | + |
| 121 | +char* asctime(const struct tm *t) |
| 122 | +{ |
| 123 | + return sntp_asctime(t); |
| 124 | +} |
| 125 | + |
| 126 | +struct tm* localtime(const time_t *clock) |
| 127 | +{ |
| 128 | + return sntp_localtime(clock); |
| 129 | +} |
| 130 | + |
| 131 | +char* ctime(const time_t *t) |
| 132 | +{ |
| 133 | + struct tm* p_tm = localtime(t); |
| 134 | + char* result = asctime(p_tm); |
| 135 | + return result; |
| 136 | +} |
| 137 | + |
| 138 | +int gettimeofday(struct timeval *tp, void *tzp) |
| 139 | +{ |
| 140 | + if (tp) |
| 141 | + { |
| 142 | + ensureBootTimeIsSet(); |
| 143 | + tp->tv_sec = (s_bootTime + millis()) / 1000; |
| 144 | + tp->tv_usec = micros() * 1000; |
| 145 | + } |
| 146 | + return 0; |
| 147 | +} |
0 commit comments