mirror of
https://github.com/cesanta/mongoose.git
synced 2025-01-18 15:13:00 +08:00
Add sntp-time-sync example
This commit is contained in:
parent
0763146254
commit
c48349ba56
10
examples/sntp-time-sync/Makefile
Normal file
10
examples/sntp-time-sync/Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
PROG ?= example
|
||||
|
||||
all: $(PROG)
|
||||
$(RUN) ./$(PROG) $(ARGS)
|
||||
|
||||
$(PROG): main.c
|
||||
$(CC) ../../mongoose.c -I../.. -W -Wall -DMG_ENABLE_LINES=1 $(CFLAGS) -o $(PROG) main.c
|
||||
|
||||
clean:
|
||||
rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb
|
52
examples/sntp-time-sync/main.c
Normal file
52
examples/sntp-time-sync/main.c
Normal file
@ -0,0 +1,52 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Synchronize time with remote SNTP server
|
||||
// A working time() call is required by TLS, so an embedded device without
|
||||
// a clock that wants to use TLS, must sync time via SNTP.
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
// The UNIX epoch of the boot time. Initially, we set it to 0. But then after
|
||||
// SNTP response, we update it to the correct value, which will allow us to
|
||||
// use time(). Uptime in milliseconds is returned by mg_millis().
|
||||
static time_t s_boot_timestamp = 0;
|
||||
|
||||
// SNTP client connection
|
||||
static struct mg_connection *s_sntp_conn = NULL;
|
||||
|
||||
// On embedded systems, rename to time()
|
||||
time_t my_time(time_t *tp) {
|
||||
time_t t = s_boot_timestamp + mg_millis() / 1000;
|
||||
if (tp != NULL) *tp = t;
|
||||
return t;
|
||||
}
|
||||
|
||||
// SNTP client callback
|
||||
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
if (ev == MG_EV_SNTP_TIME) {
|
||||
int64_t t = *(int64_t *) ev_data;
|
||||
MG_INFO(("Got SNTP time: %lld ms from epoch", t));
|
||||
s_boot_timestamp = (time_t) ((t - mg_millis()) / 1000);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
s_sntp_conn = NULL;
|
||||
}
|
||||
(void) fn_data, (void) c;
|
||||
}
|
||||
|
||||
// Called every 5 seconds. Increase that for production case.
|
||||
static void timer_fn(void *arg) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
||||
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
|
||||
if (s_sntp_conn != NULL) mg_sntp_request(s_sntp_conn);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_log_set("3"); // Set debug log level
|
||||
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr);
|
||||
for (;;) mg_mgr_poll(&mgr, 300); // Infinite event loop
|
||||
mg_mgr_free(&mgr); // Free manager resources
|
||||
return 0;
|
||||
}
|
@ -342,7 +342,7 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
|
||||
dnsc->c = mg_connect(c->mgr, dnsc->url, NULL, NULL);
|
||||
if (dnsc->c != NULL) {
|
||||
dnsc->c->pfn = dns_cb;
|
||||
dnsc->c->is_hexdumping = 1;
|
||||
// dnsc->c->is_hexdumping = 1;
|
||||
}
|
||||
}
|
||||
if (dnsc->c == NULL) {
|
||||
|
@ -230,7 +230,7 @@ static void mg_sendnsreq(struct mg_connection *c, struct mg_str *name, int ms,
|
||||
dnsc->c = mg_connect(c->mgr, dnsc->url, NULL, NULL);
|
||||
if (dnsc->c != NULL) {
|
||||
dnsc->c->pfn = dns_cb;
|
||||
dnsc->c->is_hexdumping = 1;
|
||||
// dnsc->c->is_hexdumping = 1;
|
||||
}
|
||||
}
|
||||
if (dnsc->c == NULL) {
|
||||
|
Loading…
Reference in New Issue
Block a user