Skip to content
Permalink
Browse files

samples: sntp_client: Elaborate sample to workable state

1. Output what steps the app performs, so it doesn't look to user
that it simply hanged.
2. Don't use infinite timeouts, because that will hang.
3. Clearly note which requests are for IPv4 vs IPv6 server.
4. Define IPv4 gateway. This sample is configured to run against
SNTP on local Linux host, but standard distros (e.g. Ubuntu) don't
run SNTP server by default, so usual outcome for running this sample
will be timeout. A realistic way to get successful output would be
to run it against a server on the Internet, for what a gateway is
required.

Signed-off-by: Paul Sokolovsky <paul.sokolovsky@linaro.org>
  • Loading branch information...
pfalcon authored and jukkar committed Apr 20, 2019
1 parent e8e814c commit 901d85bde0f52b536671f46c70a2b07ff8a6b353
Showing with 18 additions and 9 deletions.
  1. +3 −0 samples/net/sockets/sntp_client/prj.conf
  2. +15 −9 samples/net/sockets/sntp_client/src/main.c
@@ -21,8 +21,11 @@ CONFIG_NET_CONFIG_SETTINGS=y
CONFIG_NET_CONFIG_NEED_IPV4=y
CONFIG_NET_CONFIG_NEED_IPV6=y
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
# Address of SNTP IPv4 server
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
# Address of SNTP IPv6 server
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"

# SNTP
@@ -33,17 +33,20 @@ void main(void)
rv = sntp_init(&ctx, (struct sockaddr *) &addr,
sizeof(struct sockaddr_in));
if (rv < 0) {
LOG_ERR("Failed to init sntp ctx: %d", rv);
LOG_ERR("Failed to init SNTP IPv4 ctx: %d", rv);
goto end;
}

rv = sntp_request(&ctx, K_FOREVER, &epoch_time);
LOG_INF("Sending SNTP IPv4 request...");
rv = sntp_request(&ctx, K_SECONDS(4), &epoch_time);
if (rv < 0) {
LOG_ERR("Failed to send sntp request: %d", rv);
LOG_ERR("SNTP IPv4 request failed: %d", rv);
goto end;
}
LOG_DBG("time: %lld", epoch_time);
LOG_DBG("status: %d", rv);

LOG_INF("status: %d", rv);
LOG_INF("time since Epoch: high word: %u, low word: %u",
(u32_t)(epoch_time >> 32), (u32_t)epoch_time);

#if defined(CONFIG_NET_IPV6)
sntp_close(&ctx);
@@ -57,18 +60,21 @@ void main(void)
rv = sntp_init(&ctx, (struct sockaddr *) &addr6,
sizeof(struct sockaddr_in6));
if (rv < 0) {
LOG_ERR("Failed to init sntp ctx: %d", rv);
LOG_ERR("Failed to init SNTP IPv6 ctx: %d", rv);
goto end;
}

LOG_INF("Sending SNTP IPv6 request...");
/* With such a timeout, this is expected to fail. */
rv = sntp_request(&ctx, K_NO_WAIT, &epoch_time);
if (rv < 0) {
LOG_ERR("Failed to send sntp request: %d", rv);
LOG_ERR("SNTP IPv6 request: %d", rv);
goto end;
}

LOG_DBG("time: %lld", epoch_time);
LOG_DBG("status: %d", rv);
LOG_INF("status: %d", rv);
LOG_INF("time since Epoch: high word: %u, low word: %u",
(u32_t)(epoch_time >> 32), (u32_t)epoch_time);
#endif

end:

0 comments on commit 901d85b

Please sign in to comment.
You can’t perform that action at this time.