Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Linaro Limited |
Ravi kumar Veeramally | f51cebe | 2019-01-18 15:52:55 +0200 | [diff] [blame] | 3 | * Copyright (c) 2019 Intel Corporation |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 4 | * |
| 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | */ |
| 7 | |
Flavio Ceolin | 67ca176 | 2018-09-14 10:43:44 -0700 | [diff] [blame] | 8 | #ifndef ZEPHYR_INCLUDE_NET_SNTP_H_ |
| 9 | #define ZEPHYR_INCLUDE_NET_SNTP_H_ |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 10 | |
Paul Sokolovsky | a7bb285 | 2019-09-12 13:33:04 +0300 | [diff] [blame] | 11 | #ifdef CONFIG_NET_SOCKETS_POSIX_NAMES |
Ravi kumar Veeramally | f51cebe | 2019-01-18 15:52:55 +0200 | [diff] [blame] | 12 | #include <net/socket.h> |
Paul Sokolovsky | a7bb285 | 2019-09-12 13:33:04 +0300 | [diff] [blame] | 13 | #else |
| 14 | #include <posix/sys/socket.h> |
| 15 | #include <posix/unistd.h> |
| 16 | #include <posix/poll.h> |
| 17 | #endif |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 18 | |
Markus Fuchs | a928b6d | 2019-07-16 12:43:06 +0200 | [diff] [blame] | 19 | #ifdef __cplusplus |
| 20 | extern "C" { |
| 21 | #endif |
| 22 | |
Jukka Rissanen | 6346cc1 | 2019-03-13 18:45:51 +0200 | [diff] [blame] | 23 | /** |
| 24 | * @brief Simple Network Time Protocol API |
| 25 | * @defgroup sntp SNTP |
| 26 | * @ingroup networking |
| 27 | * @{ |
| 28 | */ |
| 29 | |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 30 | /** SNTP context */ |
| 31 | struct sntp_ctx { |
Ravi kumar Veeramally | f51cebe | 2019-01-18 15:52:55 +0200 | [diff] [blame] | 32 | struct { |
| 33 | struct pollfd fds[1]; |
| 34 | int nfds; |
| 35 | int fd; |
| 36 | } sock; |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 37 | |
Ravi kumar Veeramally | f51cebe | 2019-01-18 15:52:55 +0200 | [diff] [blame] | 38 | /** Timestamp when the request was sent from client to server. |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 39 | * This is used to check if the originated timestamp in the server |
| 40 | * reply matches the one in client request. |
| 41 | */ |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 42 | uint32_t expected_orig_ts; |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 43 | }; |
| 44 | |
Paul Sokolovsky | f65727a | 2019-04-22 16:55:57 +0300 | [diff] [blame] | 45 | /** Time as returned by SNTP API, fractional seconds since 1 Jan 1970 */ |
| 46 | struct sntp_time { |
Kumar Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 47 | uint64_t seconds; |
| 48 | uint32_t fraction; |
Paul Sokolovsky | f65727a | 2019-04-22 16:55:57 +0300 | [diff] [blame] | 49 | }; |
| 50 | |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 51 | /** |
| 52 | * @brief Initialize SNTP context |
| 53 | * |
| 54 | * @param ctx Address of sntp context. |
Ravi kumar Veeramally | f51cebe | 2019-01-18 15:52:55 +0200 | [diff] [blame] | 55 | * @param addr IP address of NTP/SNTP server. |
| 56 | * @param addr_len IP address length of NTP/SNTP server. |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 57 | * |
| 58 | * @return 0 if ok, <0 if error. |
| 59 | */ |
Ravi kumar Veeramally | f51cebe | 2019-01-18 15:52:55 +0200 | [diff] [blame] | 60 | int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr, |
| 61 | socklen_t addr_len); |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 62 | |
| 63 | /** |
Paul Sokolovsky | f65727a | 2019-04-22 16:55:57 +0300 | [diff] [blame] | 64 | * @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 Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 73 | int sntp_query(struct sntp_ctx *ctx, uint32_t timeout, |
Paul Sokolovsky | f65727a | 2019-04-22 16:55:57 +0300 | [diff] [blame] | 74 | struct sntp_time *time); |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * @brief Release SNTP context |
| 78 | * |
| 79 | * @param ctx Address of sntp context. |
| 80 | */ |
| 81 | void sntp_close(struct sntp_ctx *ctx); |
| 82 | |
Jukka Rissanen | 6346cc1 | 2019-03-13 18:45:51 +0200 | [diff] [blame] | 83 | /** |
Paul Sokolovsky | 22f1a29 | 2019-04-22 17:49:25 +0300 | [diff] [blame] | 84 | * @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 Gala | a1b77fd | 2020-05-27 11:26:57 -0500 | [diff] [blame] | 96 | int sntp_simple(const char *server, uint32_t timeout, |
Paul Sokolovsky | 22f1a29 | 2019-04-22 17:49:25 +0300 | [diff] [blame] | 97 | struct sntp_time *time); |
| 98 | |
Markus Fuchs | a928b6d | 2019-07-16 12:43:06 +0200 | [diff] [blame] | 99 | #ifdef __cplusplus |
| 100 | } |
| 101 | #endif |
| 102 | |
Paul Sokolovsky | 22f1a29 | 2019-04-22 17:49:25 +0300 | [diff] [blame] | 103 | /** |
Jukka Rissanen | 6346cc1 | 2019-03-13 18:45:51 +0200 | [diff] [blame] | 104 | * @} |
| 105 | */ |
| 106 | |
Aska Wu | f1e488a | 2017-09-05 18:40:39 +0800 | [diff] [blame] | 107 | #endif |