Skip to content

Commit

Permalink
Merge pull request #4 from ALLTERCO/add_is_synced
Browse files Browse the repository at this point in the history
Add mgos_sntp_get_last_synced_uptime()
  • Loading branch information
rojer authored Feb 16, 2021
2 parents f7f4c04 + 21ece85 commit d9f7fd7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
17 changes: 17 additions & 0 deletions include/mgos_sntp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
* Returns 0 until synced. After that, returns uptime in seconds of the
* last sync event, as returned by `mgos_uptime()`.
*/
double mgos_sntp_get_last_synced_uptime(void);

#ifdef __cplusplus
}
#endif
2 changes: 2 additions & 0 deletions mos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ version: 1.0

sources:
- src
includes:
- include
filesystem:
- fs
config_schema:
Expand Down
12 changes: 9 additions & 3 deletions src/mgos_sntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

#include "mgos_sntp.h"

#include <stdbool.h>
#include <stdlib.h>

Expand All @@ -30,7 +32,7 @@

struct mgos_sntp_state {
struct mg_connection *nc;
bool synced;
double last_synced_uptime;
int retry_timeout_ms;
mgos_timer_id retry_timer_id;
};
Expand Down Expand Up @@ -61,7 +63,7 @@ static void mgos_sntp_ev(struct mg_connection *nc, int ev, void *ev_data,
LOG(LL_ERROR, ("Failed to set time"));
}
s_state.retry_timeout_ms = 0;
s_state.synced = true;
s_state.last_synced_uptime = mgos_uptime();
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
if (s_state.retry_timer_id != MGOS_INVALID_TIMER_ID) {
mgos_clear_timer(s_state.retry_timer_id);
Expand Down Expand Up @@ -112,7 +114,7 @@ static void mgos_sntp_retry(void) {
if (!mgos_sys_config_get_sntp_enable()) return;
if (s_state.retry_timer_id != MGOS_INVALID_TIMER_ID) return;
int rt_ms = 0;
if (s_state.synced) {
if (s_state.last_synced_uptime != 0) {
rt_ms = mgos_sys_config_get_sntp_update_interval() * 1000;
if (rt_ms == 0) return;
} else {
Expand Down Expand Up @@ -151,6 +153,10 @@ static void mgos_sntp_net_ev(int ev, void *evd, void *arg) {
(void) arg;
}

double mgos_sntp_get_last_synced_uptime(void) {
return s_state.last_synced_uptime;
}

bool mgos_sntp_init(void) {
if (!mgos_sys_config_get_sntp_enable()) return true;
if (mgos_sys_config_get_sntp_server() == NULL) {
Expand Down

0 comments on commit d9f7fd7

Please sign in to comment.