Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/net/sntp.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr,
*
* @param ctx Address of sntp context.
* @param timeout Timeout of waiting for sntp response (in milliseconds).
* @param epoch_time Seconds since 1 January 1970.
* @param epoch_time Seconds since 1 January 1970 (output).
*
* @return 0 if ok, <0 if error.
* @return 0 if ok, <0 if error (-ETIMEDOUT if timeout).
*/
int sntp_request(struct sntp_ctx *ctx, u32_t timeout, u64_t *epoch_time);

Expand Down
3 changes: 3 additions & 0 deletions samples/net/sockets/sntp_client/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions samples/net/sockets/sntp_client/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion subsys/net/lib/sntp/sntp.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,16 @@ static int sntp_recv_response(struct sntp_ctx *sntp, u32_t timeout,
int status;
int rcvd;

if (poll(sntp->sock.fds, sntp->sock.nfds, timeout) < 0) {
status = poll(sntp->sock.fds, sntp->sock.nfds, timeout);
if (status < 0) {
NET_ERR("Error in poll:%d", errno);
return -errno;
}

if (status == 0) {
return -ETIMEDOUT;
}

rcvd = recv(sntp->sock.fd, (u8_t *)&buf, sizeof(buf), 0);
if (rcvd < 0) {
return -errno;
Expand Down