Skip to content

Commit 21ece85

Browse files
Kirilkzyapkov
Kiril
authored andcommitted
Add mgos_sntp_get_last_synced_uptime()
1 parent f7f4c04 commit 21ece85

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

include/mgos_sntp.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
/*
10+
* Returns 0 until synced. After that, returns uptime in seconds of the
11+
* last sync event, as returned by `mgos_uptime()`.
12+
*/
13+
double mgos_sntp_get_last_synced_uptime(void);
14+
15+
#ifdef __cplusplus
16+
}
17+
#endif

mos.yml

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ version: 1.0
55

66
sources:
77
- src
8+
includes:
9+
- include
810
filesystem:
911
- fs
1012
config_schema:

src/mgos_sntp.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include "mgos_sntp.h"
19+
1820
#include <stdbool.h>
1921
#include <stdlib.h>
2022

@@ -30,7 +32,7 @@
3032

3133
struct mgos_sntp_state {
3234
struct mg_connection *nc;
33-
bool synced;
35+
double last_synced_uptime;
3436
int retry_timeout_ms;
3537
mgos_timer_id retry_timer_id;
3638
};
@@ -61,7 +63,7 @@ static void mgos_sntp_ev(struct mg_connection *nc, int ev, void *ev_data,
6163
LOG(LL_ERROR, ("Failed to set time"));
6264
}
6365
s_state.retry_timeout_ms = 0;
64-
s_state.synced = true;
66+
s_state.last_synced_uptime = mgos_uptime();
6567
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
6668
if (s_state.retry_timer_id != MGOS_INVALID_TIMER_ID) {
6769
mgos_clear_timer(s_state.retry_timer_id);
@@ -112,7 +114,7 @@ static void mgos_sntp_retry(void) {
112114
if (!mgos_sys_config_get_sntp_enable()) return;
113115
if (s_state.retry_timer_id != MGOS_INVALID_TIMER_ID) return;
114116
int rt_ms = 0;
115-
if (s_state.synced) {
117+
if (s_state.last_synced_uptime != 0) {
116118
rt_ms = mgos_sys_config_get_sntp_update_interval() * 1000;
117119
if (rt_ms == 0) return;
118120
} else {
@@ -151,6 +153,10 @@ static void mgos_sntp_net_ev(int ev, void *evd, void *arg) {
151153
(void) arg;
152154
}
153155

156+
double mgos_sntp_get_last_synced_uptime(void) {
157+
return s_state.last_synced_uptime;
158+
}
159+
154160
bool mgos_sntp_init(void) {
155161
if (!mgos_sys_config_get_sntp_enable()) return true;
156162
if (mgos_sys_config_get_sntp_server() == NULL) {

0 commit comments

Comments
 (0)