Increate SNTP timeout 3s->5s. Change mg_sntp_send() to mg_sntp_request(), remove one unnecessary parameter

This commit is contained in:
Sergey Lyubka 2022-05-15 18:41:01 +01:00
parent 6db1d7925e
commit f5fd176b35
10 changed files with 17 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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);

View File

@ -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);