物联网操作系统AliOS Things 3.3
sntp.h
浏览该文件的文档.
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 
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 #define SNTP_RECV_TIMEOUT 3000 /* ms */
50 #define SNTP_RETRY_TIMEOUT SNTP_RECV_TIMEOUT
51 #define SNTP_RETRY_TIMES 3
52 
53 #define SNTP_SERVER_ADDRESS "pool.ntp.org"
54 
55 typedef struct {
56  char *server_name; /* remote server name, if this is not NULL, this will be preferred. */
57  int recv_timeout; /* the receive timeout from ntp server */
58  uint8_t retry_times; /* the retry times when receiver timeout */
59 } sntp_arg;
60 
61 typedef struct {
62  uint8_t sec;
63  uint8_t min;
64  uint8_t hour;
65  uint8_t day;
66  uint8_t mon;
67  uint8_t week;
68  uint8_t year;
69 } sntp_time;
70 
71 int sntp_request(void *arg);
73 
74 int sntp_get_time(sntp_arg *arg, struct timeval *ntp_time);
75 #if SNTP_SUPPORT_MULTIPLE_SERVERS
76 int sntp_set_server(uint8_t idx, char *server_name);
77 #endif
78 
79 /*
80 * check if last sntp query is OK
81 * @param: None
82 * @retval: 0 success -1 fail
83 */
85 
86 /*
87 * set system calender time directly
88 * @param: current time in millisecond
89 * @retval: 0 success -1 fail
90 */
91 int sntp_set_time_direct(long long now_ms);
92 
93 #ifdef USE_RECORD_CALENDAR_TIME
94 /*
95 * update calendar time from file
96 * @param: none
97 * @retval: none
98 */
99 void update_calendar_time_from_file(void);
100 
101 /*
102 * record calendar time to file
103 * @param: current time in millisecond
104 * @retval: none
105 */
106 void record_calendar_time(long long ms);
107 #endif
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif /* _NET_SNTP_H_ */
114 
sntp_time * sntp_obtain_time(void)
uint8_t retry_times
Definition: sntp.h:58
char * server_name
Definition: sntp.h:56
uint8_t week
Definition: sntp.h:67
int sntp_last_query_status(void)
uint8_t day
Definition: sntp.h:65
int sntp_set_server(uint8_t idx, char *server_name)
uint8_t year
Definition: sntp.h:68
int sntp_request(void *arg)
int recv_timeout
Definition: sntp.h:57
int sntp_get_time(sntp_arg *arg, struct timeval *ntp_time)
uint8_t mon
Definition: sntp.h:66
uint8_t min
Definition: sntp.h:63
uint8_t sec
Definition: sntp.h:62
int sntp_set_time_direct(long long now_ms)
uint8_t hour
Definition: sntp.h:64
Definition: sntp.h:55
Definition: sntp.h:61