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 SNTPClient(); 31 ~SNTPClient(); 32 bool RequestTime(std::string host); 33 int64_t getNtpTIme(); 34 int64_t getNtpTimeReference(); 35 int64_t getRoundTripTime(); 36 int GetClockOffset(void); 37 38 private: 39 struct ntp_timestamp { 40 uint32_t second; 41 uint32_t fraction; 42 }; 43 44 struct date_structure { 45 int hour; 46 int minute; 47 int second; 48 int millisecond; 49 }; 50 51 struct SNTPMessage { 52 unsigned char _leapIndicator; 53 unsigned char _versionNumber; 54 unsigned char _mode; 55 unsigned char _stratum; 56 unsigned char _pollInterval; 57 unsigned char _precision; 58 unsigned int _rootDelay; 59 unsigned int _rootDispersion; 60 unsigned int _referenceIdentifier[4]; 61 uint64_t _referenceTimestamp; 62 uint64_t _originateTimestamp; 63 uint64_t _receiveTimestamp; 64 uint64_t _transmitTimestamp; 65 66 /** 67 * Zero all the values. 68 */ 69 void clear(); 70 }; 71 72 unsigned int GetNtpField32(int offset, const char *buffer); 73 /** 74 * This function returns an array based on the Reference ID 75 * (converted from NTP message), given the offset provided. 76 * 77 * @param offset the offset of the field in the NTP message 78 * @param buffer the received message 79 * 80 * Returns the array of Reference ID 81 */ 82 void GetReferenceId(int offset, char *buffer, int *_outArray); 83 /** 84 * This function sets the clock offset in ms. 85 * Negative value means the local clock is ahead, 86 * positive means the local clock is behind (relative to the NTP server) 87 * 88 * @param clockOffset the clock offset in ms 89 */ 90 void SetClockOffset(int clockOffset); 91 92 /** 93 * This function converts the UNIX time to NTP 94 * 95 * @param ntpTs the structure NTP where the NTP values are stored 96 * @param unixTs the structure UNIX (with the already set tv_sec and tv_usec) 97 */ 98 void ConvertUnixToNtp(struct ntp_timestamp *ntpTs, struct timeval *unixTs); 99 /** 100 * This function converts the NTP time to UNIX 101 * 102 * @param ntpTs the structure NTP where the NTP values are already set 103 * @param unixTs the structure UNIX where the UNIX values are stored 104 */ 105 void ConvertNtpToUnix(struct ntp_timestamp *ntpTs, struct timeval *unixTs); 106 107 /** 108 * This function creates the SNTP message ready for transmission (SNTP Req) 109 * and returns it back. 110 * 111 * @param buffer the message to be sent 112 */ 113 void CreateMessage(char *buffer); 114 115 /** 116 * This function creates the SNTP message ready for transmission (SNTP Req) 117 * and returns it back. 118 * 119 * @param buffer the message to be sent 120 */ 121 void WriteTimeStamp(char *buffer, ntp_timestamp *ntp); 122 123 /** 124 * This function gets the information received from the SNTP response 125 * and prints the results (e.g. offset, round trip delay etc.) 126 * 127 * @param buffer the message received 128 */ 129 void ReceivedMessage(char *buffer); 130 131 /** 132 * This function returns the timestamp (64-bit) from the received 133 * buffer, given the offset provided. 134 * 135 * @param offset the offset of the timestamp in the NTP message 136 * @param buffer the received message 137 * 138 * Returns the ntp timestamp 139 */ 140 uint64_t GetNtpTimestamp64(int offset, const char *buffer); 141 142 /** 143 * This function converts the NTP time to timestamp 144 * 145 * @param _ntpTs the NTP timestamp to be converted 146 * Returns the milliseconds 147 */ 148 int64_t ConvertNtpToStamp(uint64_t _ntpTs); 149 int64_t m_clockOffset; 150 uint64_t m_originateTimestamp; 151 int64_t mNtpTime; 152 int64_t mNtpTimeReference; 153 int64_t mRoundTripTime; 154 }; 155 } // namespace MiscServices 156 } // namespace OHOS 157 #endif // SNTP_CLIENT_SNTP_CLIENT_H