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