1 /* 2 * Copyright (C) 2021 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 SERVICES_INCLUDE_TIME_COMMON_H 17 #define SERVICES_INCLUDE_TIME_COMMON_H 18 19 #include <string> 20 #include <chrono> 21 22 #include "errors.h" 23 #include "time_hilog.h" 24 25 namespace OHOS { 26 namespace MiscServices { 27 #define TIME_SERVICE_NAME "TimeService" 28 29 struct TimerPara { 30 std::string name; 31 int timerType; 32 int64_t windowLength; 33 uint64_t interval; 34 bool disposable; 35 bool autoRestore; 36 uint32_t flag; 37 }; 38 39 enum TimeModule : uint8_t { 40 TIME_MODULE_SERVICE_ID = 0x04, 41 }; 42 // time error offset, used only in this file. 43 constexpr ErrCode TIME_ERR_OFFSET = ErrCodeOffset(SUBSYS_SMALLSERVICES, TIME_MODULE_SERVICE_ID); 44 45 enum TimeError : int32_t { 46 E_TIME_OK = 0, 47 E_TIME_SA_DIED = TIME_ERR_OFFSET, // 77856768 48 E_TIME_READ_PARCEL_ERROR, // 77856769 49 E_TIME_WRITE_PARCEL_ERROR, // 77856770 50 E_TIME_PUBLISH_FAIL, // 77856771 51 E_TIME_TRANSACT_ERROR, // 77856772 52 E_TIME_DEAL_FAILED, // 77856773 53 E_TIME_PARAMETERS_INVALID, // 77856774 54 E_TIME_SET_RTC_FAILED, // 77856775 55 E_TIME_NOT_FOUND, // 77856776 56 E_TIME_NULLPTR, // 77856777 57 E_TIME_NO_PERMISSION, // 77856778 58 E_TIME_NOT_SYSTEM_APP, // 77856779 59 E_TIME_NO_TIMER_ADJUST, // 77856780 60 E_TIME_NTP_UPDATE_FAILED, // 77856781 61 E_TIME_NTP_NOT_UPDATE, // 77856782 62 #ifdef MULTI_ACCOUNT_ENABLE 63 E_TIME_ACCOUNT_NOT_MATCH, 64 E_TIME_ACCOUNT_ERROR, 65 #endif 66 E_TIME_AUTO_RESTORE_ERROR, 67 }; 68 69 enum DatabaseType : int8_t { 70 NOT_STORE = 0, 71 STORE, 72 }; 73 74 enum APIVersion : int8_t { 75 API_VERSION_7 = 0, 76 API_VERSION_9 = 1, 77 }; 78 79 class TimeUtils { 80 public: 81 static int32_t GetWallTimeMs(int64_t &time); 82 static int32_t GetBootTimeMs(int64_t &time); 83 static int32_t GetBootTimeNs(int64_t &time); 84 static std::chrono::steady_clock::time_point GetBootTimeNs(); 85 static bool GetTimeByClockId(clockid_t clockId, struct timespec &tv); 86 }; 87 } // namespace MiscServices 88 } // namespace OHOS 89 #endif // SERVICES_INCLUDE_TIME_COMMON_H