• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SNTP_CLIENT_SNTP_CLIENT_H
17 #define SNTP_CLIENT_SNTP_CLIENT_H
18 
19 #include <cstdlib>
20 #include <ctime>
21 #include <netinet/in.h>
22 #include <string>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 
26 namespace OHOS {
27 namespace MiscServices {
28 class SNTPClient {
29 public:
30     bool RequestTime(const std::string &host);
31     int64_t getNtpTime();
32     int64_t getNtpTimeReference();
33     int64_t getRoundTripTime();
34 
35 private:
36     struct ntp_timestamp {
37         uint64_t second;
38         uint64_t fraction;
39     };
40 
41     struct date_structure {
42         int hour;
43         int minute;
44         int second;
45         int millisecond;
46     };
47 
48     struct SNTPMessage {
49         unsigned char _leapIndicator;
50         unsigned char _versionNumber;
51         unsigned char _mode;
52         unsigned char _stratum;
53         unsigned char _pollInterval;
54         unsigned char _precision;
55         unsigned int _rootDelay;
56         unsigned int _rootDispersion;
57         unsigned int _referenceIdentifier[4];
58         uint64_t _referenceTimestamp;
59         uint64_t _originateTimestamp;
60         uint64_t _receiveTimestamp;
61         uint64_t _transmitTimestamp;
62 
63         /**
64          * Zero all the values.
65          */
66         void clear();
67     };
68 
69     unsigned int GetNtpField32(int offset, const char *buffer);
70     /**
71      * This function returns an array based on the Reference ID
72      * (converted from NTP message), given the offset provided.
73      *
74      * @param offset the offset of the field in the NTP message
75      * @param buffer the received message
76      *
77      * Returns the array of Reference ID
78      */
79     void GetReferenceId(int offset, char *buffer, int *_outArray);
80     /**
81      * This function sets the clock offset in ms.
82      * Negative value means the local clock is ahead,
83      * positive means the local clock is behind (relative to the NTP server)
84      *
85      * @param clockOffset the clock offset in ms
86      */
87     void SetClockOffset(int clockOffset);
88 
89     /**
90      * This function converts the UNIX time to NTP
91      *
92      * @param ntpTs the structure NTP where the NTP values are stored
93      * @param unixTs the structure UNIX (with the already set tv_sec and tv_usec)
94      */
95     void ConvertUnixToNtp(struct ntp_timestamp *ntpTs, struct timeval *unixTs);
96     /**
97      * This function converts the NTP time to UNIX
98      *
99      * @param ntpTs the structure NTP where the NTP values are already set
100      * @param unixTs the structure UNIX where the UNIX values are stored
101      */
102     void ConvertNtpToUnix(struct ntp_timestamp *ntpTs, struct timeval *unixTs);
103 
104     /**
105     * This function creates the SNTP message ready for transmission (SNTP Req)
106     * and returns it back.
107     *
108     * @param buffer the message to be sent
109     */
110     void CreateMessage(char *buffer);
111 
112     /**
113     * This function creates the SNTP message ready for transmission (SNTP Req)
114     * and returns it back.
115     *
116     * @param buffer the message to be sent
117     */
118     void WriteTimeStamp(char *buffer, ntp_timestamp *ntp);
119 
120     /**
121      * This function gets the information received from the SNTP response
122      * and prints the results (e.g. offset, round trip delay etc.)
123      *
124      * @param buffer the message received
125      */
126     void ReceivedMessage(char *buffer);
127 
128     /**
129      * This function returns the timestamp (64-bit) from the received
130      * buffer, given the offset provided.
131      *
132      * @param offset the offset of the timestamp in the NTP message
133      * @param buffer the received message
134      *
135      * Returns the ntp timestamp
136      */
137     uint64_t GetNtpTimestamp64(int offset, const char *buffer);
138 
139     /**
140      * This function converts the NTP time to timestamp
141      *
142      * @param _ntpTs the NTP timestamp to be converted
143      * Returns the milliseconds
144      */
145     int64_t ConvertNtpToStamp(uint64_t _ntpTs);
146     int64_t m_clockOffset;
147     uint64_t m_originateTimestamp;
148     int64_t mNtpTime;
149     int64_t mNtpTimeReference;
150     int64_t mRoundTripTime;
151 };
152 } // namespace MiscServices
153 } // namespace OHOS
154 #endif // SNTP_CLIENT_SNTP_CLIENT_H