|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
| 18 | +#include "mgos_sntp.h" |
| 19 | + |
18 | 20 | #include <stdbool.h>
|
19 | 21 | #include <stdlib.h>
|
20 | 22 |
|
|
30 | 32 |
|
31 | 33 | struct mgos_sntp_state {
|
32 | 34 | struct mg_connection *nc;
|
33 |
| - bool synced; |
| 35 | + double last_synced_uptime; |
34 | 36 | int retry_timeout_ms;
|
35 | 37 | mgos_timer_id retry_timer_id;
|
36 | 38 | };
|
@@ -61,7 +63,7 @@ static void mgos_sntp_ev(struct mg_connection *nc, int ev, void *ev_data,
|
61 | 63 | LOG(LL_ERROR, ("Failed to set time"));
|
62 | 64 | }
|
63 | 65 | s_state.retry_timeout_ms = 0;
|
64 |
| - s_state.synced = true; |
| 66 | + s_state.last_synced_uptime = mgos_uptime(); |
65 | 67 | nc->flags |= MG_F_CLOSE_IMMEDIATELY;
|
66 | 68 | if (s_state.retry_timer_id != MGOS_INVALID_TIMER_ID) {
|
67 | 69 | mgos_clear_timer(s_state.retry_timer_id);
|
@@ -112,7 +114,7 @@ static void mgos_sntp_retry(void) {
|
112 | 114 | if (!mgos_sys_config_get_sntp_enable()) return;
|
113 | 115 | if (s_state.retry_timer_id != MGOS_INVALID_TIMER_ID) return;
|
114 | 116 | int rt_ms = 0;
|
115 |
| - if (s_state.synced) { |
| 117 | + if (s_state.last_synced_uptime != 0) { |
116 | 118 | rt_ms = mgos_sys_config_get_sntp_update_interval() * 1000;
|
117 | 119 | if (rt_ms == 0) return;
|
118 | 120 | } else {
|
@@ -151,6 +153,10 @@ static void mgos_sntp_net_ev(int ev, void *evd, void *arg) {
|
151 | 153 | (void) arg;
|
152 | 154 | }
|
153 | 155 |
|
| 156 | +double mgos_sntp_get_last_synced_uptime(void) { |
| 157 | + return s_state.last_synced_uptime; |
| 158 | +} |
| 159 | + |
154 | 160 | bool mgos_sntp_init(void) {
|
155 | 161 | if (!mgos_sys_config_get_sntp_enable()) return true;
|
156 | 162 | if (mgos_sys_config_get_sntp_server() == NULL) {
|
|
0 commit comments