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_SERVICES_H 17 #define SERVICES_INCLUDE_TIME_SERVICES_H 18 19 #include "system_ability.h" 20 #ifdef HIDUMPER_ENABLE 21 #include "time_cmd_dispatcher.h" 22 #include "time_cmd_parse.h" 23 #endif 24 #include "time_service_notify.h" 25 #include "time_service_stub.h" 26 #include "timer_manager.h" 27 #include "shutdown/sync_shutdown_callback_stub.h" 28 #include "shutdown/shutdown_client.h" 29 #include "cjson_helper.h" 30 #include "ipc_skeleton.h" 31 #include "time_permission.h" 32 #include "time_sysevent.h" 33 #include "itimer_info.h" 34 #ifdef RDB_ENABLE 35 #include "rdb_helper.h" 36 #include "timer_database.h" 37 #endif 38 39 namespace OHOS { 40 namespace MiscServices { 41 enum class ServiceRunningState : int8_t { STATE_NOT_START, STATE_RUNNING }; 42 43 class TimeSystemAbility : public SystemAbility, public TimeServiceStub { 44 DECLARE_SYSTEM_ABILITY(TimeSystemAbility); 45 46 public: 47 #ifdef SET_AUTO_REBOOT_ENABLE 48 class TimePowerStateListener : public OHOS::PowerMgr::SyncShutdownCallbackStub { 49 public: 50 ~TimePowerStateListener() override = default; 51 void OnSyncShutdown() override; 52 }; 53 #endif 54 DISALLOW_COPY_AND_MOVE(TimeSystemAbility); 55 TimeSystemAbility(int32_t systemAbilityId, bool runOnCreate); 56 TimeSystemAbility(); 57 ~TimeSystemAbility(); 58 static sptr<TimeSystemAbility> GetInstance(); 59 int32_t SetTime(int64_t time, int8_t apiVersion = APIVersion::API_VERSION_7) override; 60 int32_t SetTimeInner(int64_t time, int8_t apiVersion = APIVersion::API_VERSION_7); 61 bool SetRealTime(int64_t time); 62 int32_t SetTimeZone(const std::string &timeZoneId, int8_t apiVersion = APIVersion::API_VERSION_7) override; 63 int32_t SetTimeZoneInner(const std::string &timeZoneId, int8_t apiVersion = APIVersion::API_VERSION_7); 64 int32_t GetTimeZone(std::string &timeZoneId) override; 65 int32_t GetThreadTimeMs(int64_t &time) override; 66 int32_t GetThreadTimeNs(int64_t &time) override; 67 int32_t CreateTimer(const std::string &name, int type, bool repeat, bool disposable, bool autoRestore, 68 uint64_t interval, const OHOS::AbilityRuntime::WantAgent::WantAgent &wantAgent, 69 const sptr<IRemoteObject> &timerCallback, uint64_t &timerId) override; 70 int32_t CreateTimerWithoutWA(const std::string &name, int type, bool repeat, bool disposable, bool autoRestore, 71 uint64_t interval, const sptr<IRemoteObject> &timerCallback, uint64_t &timerId) override; 72 int32_t CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions, const sptr<IRemoteObject> &obj, 73 uint64_t &timerId); 74 int32_t CreateTimer(TimerPara ¶s, std::function<int32_t (const uint64_t)> callback, uint64_t &timerId); 75 int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override; 76 int32_t StopTimer(uint64_t timerId) override; 77 int32_t DestroyTimer(uint64_t timerId) override; 78 int32_t DestroyTimerAsync(uint64_t timerId) override; 79 int32_t ProxyTimer(int32_t uid, const std::vector<int>& pidList, bool isProxy, bool needRetrigger) override; 80 int32_t AdjustTimer(bool isAdjust, uint32_t interval, uint32_t delta) override; 81 int32_t SetTimerExemption(const std::vector<std::string> &nameArr, bool isExemption) override; 82 int32_t ResetAllProxy() override; 83 int32_t GetNtpTimeMs(int64_t &time) override; 84 int32_t GetRealTimeMs(int64_t &time) override; 85 #ifdef HIDUMPER_ENABLE 86 int Dump(int fd, const std::vector<std::u16string> &args) override; 87 void DumpAllTimeInfo(int fd, const std::vector<std::string> &input); 88 void DumpTimerInfo(int fd, const std::vector<std::string> &input); 89 void DumpTimerInfoById(int fd, const std::vector<std::string> &input); 90 void DumpTimerTriggerById(int fd, const std::vector<std::string> &input); 91 void DumpIdleTimerInfo(int fd, const std::vector<std::string> &input); 92 void DumpProxyTimerInfo(int fd, const std::vector<std::string> &input); 93 void DumpUidTimerMapInfo(int fd, const std::vector<std::string> &input); 94 void DumpPidTimerMapInfo(int fd, const std::vector<std::string> &input); 95 void DumpProxyDelayTime(int fd, const std::vector<std::string> &input); 96 void DumpAdjustTime(int fd, const std::vector<std::string> &input); 97 void InitDumpCmd(); 98 #endif 99 void RegisterCommonEventSubscriber(); 100 bool RecoverTimer(); 101 void RecoverTimerCjson(std::string tableName); 102 103 protected: 104 void OnStart() override; 105 void OnStop() override; 106 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 107 108 private: 109 class RSSSaDeathRecipient : public IRemoteObject::DeathRecipient { 110 public: 111 explicit RSSSaDeathRecipient()= default;; 112 ~RSSSaDeathRecipient() override = default; 113 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 114 }; 115 116 int32_t Init(); 117 void ParseTimerPara(const std::shared_ptr<ITimerInfo> &timerOptions, TimerPara ¶s); 118 int32_t CheckTimerPara(const DatabaseType type, const TimerPara ¶s); 119 bool GetTimeByClockId(clockid_t clockId, struct timespec &tv); 120 int SetRtcTime(time_t sec); 121 bool CheckRtc(const std::string &rtcPath, uint64_t rtcId); 122 int GetWallClockRtcId(); 123 void RegisterRSSDeathCallback(); 124 void RegisterSubscriber(); 125 std::shared_ptr<TimerEntry> GetEntry(cJSON* obj, bool autoRestore); 126 #ifdef MULTI_ACCOUNT_ENABLE 127 void RegisterOsAccountSubscriber(); 128 #endif 129 bool IsValidTime(int64_t time); 130 #ifdef RDB_ENABLE 131 void CjsonIntoDatabase(cJSON* resultSet, bool autoRestore, const std::string &table); 132 void RecoverTimerInner(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, bool autoRestore); 133 #else 134 void RecoverTimerInnerCjson(cJSON* resultSet, bool autoRestore, std::string tableName); 135 #endif 136 #ifdef SET_AUTO_REBOOT_ENABLE 137 void RegisterPowerStateListener(); 138 void SetAutoReboot(); 139 #endif 140 141 ServiceRunningState state_; 142 static std::mutex instanceLock_; 143 static sptr<TimeSystemAbility> instance_; 144 const int rtcId; 145 sptr<RSSSaDeathRecipient> deathRecipient_ {}; 146 }; 147 } // namespace MiscServices 148 } // namespace OHOS 149 #endif // SERVICES_INCLUDE_TIME_SERVICES_H