Skip to content

Commit 901d85b

Browse files
pfalconjukkar
authored andcommitted
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 <[email protected]>
1 parent e8e814c commit 901d85b

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

samples/net/sockets/sntp_client/prj.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ CONFIG_NET_CONFIG_SETTINGS=y
2121
CONFIG_NET_CONFIG_NEED_IPV4=y
2222
CONFIG_NET_CONFIG_NEED_IPV6=y
2323
CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1"
24+
CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2"
25+
# Address of SNTP IPv4 server
2426
CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
2527
CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1"
28+
# Address of SNTP IPv6 server
2629
CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2"
2730

2831
# SNTP

samples/net/sockets/sntp_client/src/main.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,20 @@ void main(void)
3333
rv = sntp_init(&ctx, (struct sockaddr *) &addr,
3434
sizeof(struct sockaddr_in));
3535
if (rv < 0) {
36-
LOG_ERR("Failed to init sntp ctx: %d", rv);
36+
LOG_ERR("Failed to init SNTP IPv4 ctx: %d", rv);
3737
goto end;
3838
}
3939

40-
rv = sntp_request(&ctx, K_FOREVER, &epoch_time);
40+
LOG_INF("Sending SNTP IPv4 request...");
41+
rv = sntp_request(&ctx, K_SECONDS(4), &epoch_time);
4142
if (rv < 0) {
42-
LOG_ERR("Failed to send sntp request: %d", rv);
43+
LOG_ERR("SNTP IPv4 request failed: %d", rv);
4344
goto end;
4445
}
45-
LOG_DBG("time: %lld", epoch_time);
46-
LOG_DBG("status: %d", rv);
46+
47+
LOG_INF("status: %d", rv);
48+
LOG_INF("time since Epoch: high word: %u, low word: %u",
49+
(u32_t)(epoch_time >> 32), (u32_t)epoch_time);
4750

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

67+
LOG_INF("Sending SNTP IPv6 request...");
68+
/* With such a timeout, this is expected to fail. */
6469
rv = sntp_request(&ctx, K_NO_WAIT, &epoch_time);
6570
if (rv < 0) {
66-
LOG_ERR("Failed to send sntp request: %d", rv);
71+
LOG_ERR("SNTP IPv6 request: %d", rv);
6772
goto end;
6873
}
6974

70-
LOG_DBG("time: %lld", epoch_time);
71-
LOG_DBG("status: %d", rv);
75+
LOG_INF("status: %d", rv);
76+
LOG_INF("time since Epoch: high word: %u, low word: %u",
77+
(u32_t)(epoch_time >> 32), (u32_t)epoch_time);
7278
#endif
7379

7480
end:

0 commit comments

Comments
 (0)