From f5fd176b35688bef42687eba68ad468ab3851eb7 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sun, 15 May 2022 18:41:01 +0100 Subject: [PATCH] Increate SNTP timeout 3s->5s. Change mg_sntp_send() to mg_sntp_request(), remove one unnecessary parameter --- docs/README.md | 9 ++++----- examples/zephyr/http-client/src/main.c | 2 +- examples/zephyr/http-server/src/main.c | 2 +- examples/zephyr/mqtt-aws-client/src/main.c | 2 +- examples/zephyr/websocket-server/src/main.c | 2 +- mongoose.c | 7 +++---- mongoose.h | 2 +- src/sntp.c | 7 +++---- src/sntp.h | 2 +- test/unit_test.c | 3 +-- 10 files changed, 17 insertions(+), 21 deletions(-) diff --git a/docs/README.md b/docs/README.md index c4f21460..6ee76447 100644 --- a/docs/README.md +++ b/docs/README.md @@ -1481,24 +1481,23 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { mg_sntp_connect(mgr&, NULL /* connect to time.google.com */, sntp_cb, NULL); ``` -### mg_sntp_send() +### mg_sntp_request() ```c -void mg_sntp_send(struct mg_connection *c, unsigned long utc) +void mg_sntp_request(struct mg_connection *c) ``` -Send time request to SNTP server. Note: This app can't send SNTP request more often than every one hour. +Send time request to SNTP server Parameters: - `c` - Connection to use -- `utc` - Current time, used to verify if new request is possible. Return value: None Usage example: ```c -mg_sntp_send(c, (unsigned long) time(NULL)); +mg_sntp_request(c); ``` ## MQTT diff --git a/examples/zephyr/http-client/src/main.c b/examples/zephyr/http-client/src/main.c index 2346a38f..95b0bebd 100644 --- a/examples/zephyr/http-client/src/main.c +++ b/examples/zephyr/http-client/src/main.c @@ -85,7 +85,7 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { 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_boot_timestamp < 9999) mg_sntp_send(s_sntp_conn, time(NULL)); + if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } // Use Zephyr's printk() for Mongooose MG_* logging diff --git a/examples/zephyr/http-server/src/main.c b/examples/zephyr/http-server/src/main.c index 406b1cbe..08408f80 100644 --- a/examples/zephyr/http-server/src/main.c +++ b/examples/zephyr/http-server/src/main.c @@ -66,7 +66,7 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { 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_boot_timestamp < 9999) mg_sntp_send(s_sntp_conn, time(NULL)); + if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } // Use Zephyr's printk() for Mongooose MG_* logging diff --git a/examples/zephyr/mqtt-aws-client/src/main.c b/examples/zephyr/mqtt-aws-client/src/main.c index 1364caea..f8464855 100644 --- a/examples/zephyr/mqtt-aws-client/src/main.c +++ b/examples/zephyr/mqtt-aws-client/src/main.c @@ -83,7 +83,7 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { 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_boot_timestamp < 9999) mg_sntp_send(s_sntp_conn, time(NULL)); + if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } // Use Zephyr's printk() for Mongooose MG_* logging diff --git a/examples/zephyr/websocket-server/src/main.c b/examples/zephyr/websocket-server/src/main.c index ee77a885..95065357 100644 --- a/examples/zephyr/websocket-server/src/main.c +++ b/examples/zephyr/websocket-server/src/main.c @@ -60,7 +60,7 @@ static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { 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_boot_timestamp < 9999) mg_sntp_send(s_sntp_conn, time(NULL)); + if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn); } // Use Zephyr's printk() for Mongooose MG_* logging diff --git a/mongoose.c b/mongoose.c index 2f73e975..7efd6bb4 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3102,23 +3102,22 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { MG_VERBOSE(("%u.%u", (unsigned) (milliseconds / 1000), (unsigned) (milliseconds % 1000))); } - c->recv.len = 0; // Clear receive buffer + mg_iobuf_del(&c->recv, 0, c->recv.len); // Free receive buffer } else if (ev == MG_EV_CONNECT) { - mg_sntp_send(c, (unsigned long) 0); + mg_sntp_request(c); } else if (ev == MG_EV_CLOSE) { } (void) fnd; (void) evd; } -void mg_sntp_send(struct mg_connection *c, unsigned long utc) { +void mg_sntp_request(struct mg_connection *c) { if (c->is_resolving) { MG_ERROR(("%lu wait until resolved", c->id)); } else { uint8_t buf[48] = {0}; buf[0] = (0 << 6) | (4 << 3) | 3; mg_send(c, buf, sizeof(buf)); - (void) utc; } } diff --git a/mongoose.h b/mongoose.h index ac85d8c6..a943b465 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1162,7 +1162,7 @@ size_t mg_ws_wrap(struct mg_connection *, size_t len, int op); struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url, mg_event_handler_t fn, void *fn_data); -void mg_sntp_send(struct mg_connection *c, unsigned long utc); +void mg_sntp_request(struct mg_connection *c); int64_t mg_sntp_parse(const unsigned char *buf, size_t len); diff --git a/src/sntp.c b/src/sntp.c index ebece8b3..3bcdf784 100644 --- a/src/sntp.c +++ b/src/sntp.c @@ -36,23 +36,22 @@ static void sntp_cb(struct mg_connection *c, int ev, void *evd, void *fnd) { MG_VERBOSE(("%u.%u", (unsigned) (milliseconds / 1000), (unsigned) (milliseconds % 1000))); } - c->recv.len = 0; // Clear receive buffer + mg_iobuf_del(&c->recv, 0, c->recv.len); // Free receive buffer } else if (ev == MG_EV_CONNECT) { - mg_sntp_send(c, (unsigned long) 0); + mg_sntp_request(c); } else if (ev == MG_EV_CLOSE) { } (void) fnd; (void) evd; } -void mg_sntp_send(struct mg_connection *c, unsigned long utc) { +void mg_sntp_request(struct mg_connection *c) { if (c->is_resolving) { MG_ERROR(("%lu wait until resolved", c->id)); } else { uint8_t buf[48] = {0}; buf[0] = (0 << 6) | (4 << 3) | 3; mg_send(c, buf, sizeof(buf)); - (void) utc; } } diff --git a/src/sntp.h b/src/sntp.h index c9beb50c..fd20c5f8 100644 --- a/src/sntp.h +++ b/src/sntp.h @@ -4,5 +4,5 @@ struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, const char *url, mg_event_handler_t fn, void *fn_data); -void mg_sntp_send(struct mg_connection *c, unsigned long utc); +void mg_sntp_request(struct mg_connection *c); int64_t mg_sntp_parse(const unsigned char *buf, size_t len); diff --git a/test/unit_test.c b/test/unit_test.c index 17903fba..2f137e2b 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -299,8 +299,7 @@ static void test_sntp(void) { c = mg_sntp_connect(&mgr, NULL, sntp_cb, &ms); ASSERT(c != NULL); ASSERT(c->is_udp == 1); - mg_sntp_send(c, (unsigned long) time(NULL)); - for (i = 0; i < 300 && ms == 0; i++) mg_mgr_poll(&mgr, 10); + for (i = 0; i < 500 && ms == 0; i++) mg_mgr_poll(&mgr, 10); ASSERT(ms > 0); mg_mgr_free(&mgr);