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