net: sntp: Refactor because of timeout overhaul

Use 64-bit time in order to avoid overlaps, and do not use K_MSEC()
as that will convert to k_timeout_t which we do not want in this
case.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
This commit is contained in:
Jukka Rissanen 2020-04-02 20:53:26 +03:00
commit 8a9153678a
2 changed files with 13 additions and 8 deletions

View file

@ -41,7 +41,7 @@ void main(void)
}
LOG_INF("Sending SNTP IPv4 request...");
rv = sntp_query(&ctx, K_SECONDS(4), &sntp_time);
rv = sntp_query(&ctx, 4 * MSEC_PER_SEC, &sntp_time);
if (rv < 0) {
LOG_ERR("SNTP IPv4 request failed: %d", rv);
goto end;
@ -69,7 +69,7 @@ void main(void)
LOG_INF("Sending SNTP IPv6 request...");
/* With such a timeout, this is expected to fail. */
rv = sntp_query(&ctx, K_NO_WAIT, &sntp_time);
rv = sntp_query(&ctx, 0, &sntp_time);
if (rv < 0) {
LOG_ERR("SNTP IPv6 request: %d", rv);
goto end;

View file

@ -14,7 +14,7 @@ int sntp_simple(const char *server, u32_t timeout, struct sntp_time *time)
static struct addrinfo hints;
struct addrinfo *addr;
struct sntp_ctx sntp_ctx;
u32_t deadline;
u64_t deadline;
u32_t iter_timeout;
hints.ai_family = AF_INET;
@ -36,11 +36,16 @@ int sntp_simple(const char *server, u32_t timeout, struct sntp_time *time)
goto freeaddr;
}
deadline = k_uptime_get_32() + timeout;
/* Timeout for current iteration */
iter_timeout = K_MSEC(100);
if (timeout == NET_WAIT_FOREVER) {
deadline = (u64_t)timeout;
} else {
deadline = k_uptime_get() + (u64_t)timeout;
}
while ((s32_t)(deadline - k_uptime_get_32()) > 0) {
/* Timeout for current iteration */
iter_timeout = 100;
while (k_uptime_get() < deadline) {
res = sntp_query(&sntp_ctx, iter_timeout, time);
if (res != -ETIMEDOUT) {
@ -48,7 +53,7 @@ int sntp_simple(const char *server, u32_t timeout, struct sntp_time *time)
}
/* Exponential backoff with limit */
if (iter_timeout < K_MSEC(1000)) {
if (iter_timeout < 1000) {
iter_timeout *= 2;
}
}