1 // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <stdlib.h>
16 #include <time.h>
17 #include <unistd.h>
18 #include <sys/time.h>
19 #include "esp_log.h"
20 #include "sntp.h"
21 #include "lwip/apps/sntp.h"
22 
23 static const char *TAG = "sntp";
24 
25 static volatile sntp_sync_mode_t sntp_sync_mode = SNTP_SYNC_MODE_IMMED;
26 static volatile sntp_sync_status_t sntp_sync_status = SNTP_SYNC_STATUS_RESET;
27 static sntp_sync_time_cb_t time_sync_notification_cb = NULL;
28 static uint32_t s_sync_interval = CONFIG_LWIP_SNTP_UPDATE_DELAY;
29 
sntp_set_sync_status(sntp_sync_status_t sync_status)30 inline void sntp_set_sync_status(sntp_sync_status_t sync_status)
31 {
32     sntp_sync_status = sync_status;
33 }
34 
sntp_sync_time(struct timeval * tv)35 void __attribute__((weak)) sntp_sync_time(struct timeval *tv)
36 {
37     if (sntp_sync_mode == SNTP_SYNC_MODE_IMMED) {
38         settimeofday(tv, NULL);
39         sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED);
40     } else if (sntp_sync_mode == SNTP_SYNC_MODE_SMOOTH) {
41         struct timeval tv_now;
42         gettimeofday(&tv_now, NULL);
43         int64_t cpu_time = (int64_t)tv_now.tv_sec * 1000000L + (int64_t)tv_now.tv_usec;
44         int64_t sntp_time = (int64_t)tv->tv_sec * 1000000L + (int64_t)tv->tv_usec;
45         int64_t delta = sntp_time - cpu_time;
46         struct timeval tv_delta = { .tv_sec = delta / 1000000L, .tv_usec = delta % 1000000L };
47         if (adjtime(&tv_delta, NULL) == -1) {
48             ESP_LOGD(TAG, "Function adjtime don't update time because the error is very big");
49             settimeofday(tv, NULL);
50             ESP_LOGD(TAG, "Time was synchronized through settimeofday");
51             sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED);
52         } else {
53             sntp_set_sync_status(SNTP_SYNC_STATUS_IN_PROGRESS);
54         }
55     }
56     if (time_sync_notification_cb) {
57         time_sync_notification_cb(tv);
58     }
59 }
60 
sntp_set_sync_mode(sntp_sync_mode_t sync_mode)61 void sntp_set_sync_mode(sntp_sync_mode_t sync_mode)
62 {
63     sntp_sync_mode = sync_mode;
64 }
65 
sntp_get_sync_mode(void)66 sntp_sync_mode_t sntp_get_sync_mode(void)
67 {
68     return sntp_sync_mode;
69 }
70 
71 // set a callback function for time synchronization notification
sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback)72 void sntp_set_time_sync_notification_cb(sntp_sync_time_cb_t callback)
73 {
74     time_sync_notification_cb = callback;
75 }
76 
sntp_get_sync_status(void)77 sntp_sync_status_t sntp_get_sync_status(void)
78 {
79     sntp_sync_status_t ret_sync_status = SNTP_SYNC_STATUS_RESET;
80     sntp_sync_status_t sync_status = sntp_sync_status;
81     if (sync_status == SNTP_SYNC_STATUS_COMPLETED) {
82         sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
83         ret_sync_status = SNTP_SYNC_STATUS_COMPLETED;
84     } else if (sync_status == SNTP_SYNC_STATUS_IN_PROGRESS) {
85         struct timeval outdelta;
86         adjtime(NULL, &outdelta);
87         if (outdelta.tv_sec == 0 && outdelta.tv_usec == 0) {
88             sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
89             ret_sync_status = SNTP_SYNC_STATUS_COMPLETED;
90         } else {
91             ret_sync_status = SNTP_SYNC_STATUS_IN_PROGRESS;
92         }
93     }
94     return ret_sync_status;
95 }
96 
sntp_set_sync_interval(uint32_t interval_ms)97 void sntp_set_sync_interval(uint32_t interval_ms)
98 {
99     if (interval_ms < 15000) {
100         // SNTPv4 RFC 4330 enforces a minimum update time of 15 seconds
101         interval_ms = 15000;
102     }
103     s_sync_interval = interval_ms;
104 }
105 
sntp_get_sync_interval(void)106 uint32_t sntp_get_sync_interval(void)
107 {
108     return s_sync_interval;
109 }
110 
sntp_restart(void)111 bool sntp_restart(void)
112 {
113     if (sntp_enabled()) {
114         sntp_stop();
115         sntp_init();
116         return true;
117     }
118     return false;
119 }
120 
sntp_set_system_time(uint32_t sec,uint32_t us)121 void sntp_set_system_time(uint32_t sec, uint32_t us)
122 {
123     struct timeval tv = { .tv_sec = sec, .tv_usec = us };
124     sntp_sync_time(&tv);
125 }
126 
sntp_get_system_time(uint32_t * sec,uint32_t * us)127 void sntp_get_system_time(uint32_t *sec, uint32_t *us)
128 {
129     struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
130     gettimeofday(&tv, NULL);
131     *(sec) = tv.tv_sec;
132     *(us) = tv.tv_usec;
133     sntp_set_sync_status(SNTP_SYNC_STATUS_RESET);
134 }
135