1 /*
2  * Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
3  *
4  *  Redistribution and use in source and binary forms, with or without
5  *  modification, are permitted provided that the following conditions
6  *  are met:
7  *    1. Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *    2. Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the
12  *       distribution.
13  *    3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
14  *       its contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef _NET_SNTP_H_
31 #define _NET_SNTP_H_
32 
33 #include <stdint.h>
34 #include "lwip/sockets.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /** @defgroup sntp_api sntp
41  * @{
42  */
43 
44 #define SNTP_PORT                     123
45 #define SNTP_SUPPORT_MULTIPLE_SERVERS 1
46 #if SNTP_SUPPORT_MULTIPLE_SERVERS
47 #define SNTP_MAX_SERVERS              3
48 #endif
49 #ifndef SNTP_RECV_TIMEOUT
50 #define SNTP_RECV_TIMEOUT             3000 /* ms */
51 #endif
52 #define SNTP_RETRY_TIMEOUT            SNTP_RECV_TIMEOUT
53 #define SNTP_RETRY_TIMES              3
54 
55 #define SNTP_SERVER_ADDRESS          "pool.ntp.org"
56 
57 typedef struct {
58 	char *server_name;    /* remote server name, if this is not NULL, this will be preferred. */
59 	int recv_timeout;     /* the receive timeout from ntp server */
60 	uint8_t retry_times;  /* the retry times when receiver timeout */
61 } sntp_arg;
62 
63 typedef struct {
64         uint8_t sec;       /**< Seconds after the minute   - [0,59]  */
65         uint8_t min;       /**< Minutes after the hour     - [0,59]  */
66         uint8_t hour;      /**< Hours after the midnight   - [0,23]  */
67         uint8_t day;       /**< Day of the month           - [1,31]  */
68         uint8_t mon;       /**< Months                     - [1,12]  */
69         uint8_t week;      /**< Days in a week             - [0,6]   */
70         uint8_t year;      /**< Years                      - [0,127] */
71 } sntp_time;
72 
73 int sntp_request(void *arg);
74 sntp_time *sntp_obtain_time(void);
75 
76 int sntp_get_time(sntp_arg *arg, struct timeval *ntp_time);
77 #if SNTP_SUPPORT_MULTIPLE_SERVERS
78 int sntp_set_server(uint8_t idx, char *server_name);
79 #endif
80 
81 /*
82 * check if last sntp query is OK
83 * @param: None
84 * @retval: 0 success -1 fail
85 */
86 int sntp_last_query_status(void);
87 
88 /*
89 * set system calender time directly
90 * @param: current time in millisecond
91 * @retval: 0 success -1 fail
92 */
93 int sntp_set_time_direct(long long now_ms);
94 
95 #ifdef USE_RECORD_CALENDAR_TIME
96 /*
97 * update calendar time from file
98 * @param: none
99 * @retval: none
100 */
101 void update_calendar_time_from_file(void);
102 
103 /*
104 * record calendar time to file
105 * @param: current time in millisecond
106 * @retval: none
107 */
108 void record_calendar_time(long long ms);
109 #endif
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #endif /* _NET_SNTP_H_ */
116 
117