blob: 8b37e614c54a0d42b0e0a8b60e856a2559f64336 [file] [log] [blame]
Aska Wuf1e488a2017-09-05 18:40:39 +08001/*
2 * Copyright (c) 2017 Linaro Limited
Ravi kumar Veeramallyf51cebe2019-01-18 15:52:55 +02003 * Copyright (c) 2019 Intel Corporation
Aska Wuf1e488a2017-09-05 18:40:39 +08004 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
Flavio Ceolin67ca1762018-09-14 10:43:44 -07008#ifndef ZEPHYR_INCLUDE_NET_SNTP_H_
9#define ZEPHYR_INCLUDE_NET_SNTP_H_
Aska Wuf1e488a2017-09-05 18:40:39 +080010
Paul Sokolovskya7bb2852019-09-12 13:33:04 +030011#ifdef CONFIG_NET_SOCKETS_POSIX_NAMES
Ravi kumar Veeramallyf51cebe2019-01-18 15:52:55 +020012#include <net/socket.h>
Paul Sokolovskya7bb2852019-09-12 13:33:04 +030013#else
14#include <posix/sys/socket.h>
15#include <posix/unistd.h>
16#include <posix/poll.h>
17#endif
Aska Wuf1e488a2017-09-05 18:40:39 +080018
Markus Fuchsa928b6d2019-07-16 12:43:06 +020019#ifdef __cplusplus
20extern "C" {
21#endif
22
Jukka Rissanen6346cc12019-03-13 18:45:51 +020023/**
24 * @brief Simple Network Time Protocol API
25 * @defgroup sntp SNTP
26 * @ingroup networking
27 * @{
28 */
29
Aska Wuf1e488a2017-09-05 18:40:39 +080030/** SNTP context */
31struct sntp_ctx {
Ravi kumar Veeramallyf51cebe2019-01-18 15:52:55 +020032 struct {
33 struct pollfd fds[1];
34 int nfds;
35 int fd;
36 } sock;
Aska Wuf1e488a2017-09-05 18:40:39 +080037
Ravi kumar Veeramallyf51cebe2019-01-18 15:52:55 +020038 /** Timestamp when the request was sent from client to server.
Aska Wuf1e488a2017-09-05 18:40:39 +080039 * This is used to check if the originated timestamp in the server
40 * reply matches the one in client request.
41 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -050042 uint32_t expected_orig_ts;
Aska Wuf1e488a2017-09-05 18:40:39 +080043};
44
Paul Sokolovskyf65727a2019-04-22 16:55:57 +030045/** Time as returned by SNTP API, fractional seconds since 1 Jan 1970 */
46struct sntp_time {
Kumar Galaa1b77fd2020-05-27 11:26:57 -050047 uint64_t seconds;
48 uint32_t fraction;
Paul Sokolovskyf65727a2019-04-22 16:55:57 +030049};
50
Aska Wuf1e488a2017-09-05 18:40:39 +080051/**
52 * @brief Initialize SNTP context
53 *
54 * @param ctx Address of sntp context.
Ravi kumar Veeramallyf51cebe2019-01-18 15:52:55 +020055 * @param addr IP address of NTP/SNTP server.
56 * @param addr_len IP address length of NTP/SNTP server.
Aska Wuf1e488a2017-09-05 18:40:39 +080057 *
58 * @return 0 if ok, <0 if error.
59 */
Ravi kumar Veeramallyf51cebe2019-01-18 15:52:55 +020060int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr,
61 socklen_t addr_len);
Aska Wuf1e488a2017-09-05 18:40:39 +080062
63/**
Paul Sokolovskyf65727a2019-04-22 16:55:57 +030064 * @brief Perform SNTP query
65 *
66 * @param ctx Address of sntp context.
67 * @param timeout Timeout of waiting for sntp response (in milliseconds).
68 * @param time Timestamp including integer and fractional seconds since
69 * 1 Jan 1970 (output).
70 *
71 * @return 0 if ok, <0 if error (-ETIMEDOUT if timeout).
72 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -050073int sntp_query(struct sntp_ctx *ctx, uint32_t timeout,
Paul Sokolovskyf65727a2019-04-22 16:55:57 +030074 struct sntp_time *time);
Aska Wuf1e488a2017-09-05 18:40:39 +080075
76/**
77 * @brief Release SNTP context
78 *
79 * @param ctx Address of sntp context.
80 */
81void sntp_close(struct sntp_ctx *ctx);
82
Jukka Rissanen6346cc12019-03-13 18:45:51 +020083/**
Paul Sokolovsky22f1a292019-04-22 17:49:25 +030084 * @brief Convenience function to query SNTP in one-shot fashion
85 *
86 * Convenience wrapper which calls getaddrinfo(), sntp_init(),
87 * sntp_query(), and sntp_close().
88 *
89 * @param server Address of server in format addr[:port]
90 * @param timeout Query timeout
91 * @param time Timestamp including integer and fractional seconds since
92 * 1 Jan 1970 (output).
93 *
94 * @return 0 if ok, <0 if error (-ETIMEDOUT if timeout).
95 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -050096int sntp_simple(const char *server, uint32_t timeout,
Paul Sokolovsky22f1a292019-04-22 17:49:25 +030097 struct sntp_time *time);
98
Markus Fuchsa928b6d2019-07-16 12:43:06 +020099#ifdef __cplusplus
100}
101#endif
102
Paul Sokolovsky22f1a292019-04-22 17:49:25 +0300103/**
Jukka Rissanen6346cc12019-03-13 18:45:51 +0200104 * @}
105 */
106
Aska Wuf1e488a2017-09-05 18:40:39 +0800107#endif