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      /**
98      * This function creates the SNTP message ready for transmission (SNTP Req)
99      * and returns it back.
100      *
101      * @param buffer the message to be sent
102      */
103      void CreateMessage(char *buffer);
104  
105      /**
106       * This function gets the information received from the SNTP response
107       * and prints the results (e.g. offset, round trip delay etc.)
108       *
109       * @param buffer the message received
110       */
111      bool ReceivedMessage(char *buffer);
112  
113      /**
114       * This function returns the timestamp (64-bit) from the received
115       * buffer, given the offset provided.
116       *
117       * @param offset the offset of the timestamp in the NTP message
118       * @param buffer the received message
119       *
120       * Returns the ntp timestamp
121       */
122      uint64_t GetNtpTimestamp64(int offset, const char *buffer);
123  
124      /**
125       * This function converts the NTP time to timestamp
126       *
127       * @param _ntpTs the NTP timestamp to be converted
128       * Returns the milliseconds
129       */
130      int64_t ConvertNtpToStamp(uint64_t _ntpTs);
131      int64_t m_clockOffset;
132      int64_t m_originateTimestamp;
133      int64_t mNtpTime;
134      int64_t mNtpTimeReference;
135      int64_t mRoundTripTime;
136  };
137  } // namespace MiscServices
138  } // namespace OHOS
139  #endif // SNTP_CLIENT_SNTP_CLIENT_H