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 <cinttypes> 20 #include <mutex> 21 22 #include "event_handler.h" 23 #include "securec.h" 24 #include "system_ability.h" 25 #include "ctime" 26 #include "time_cmd_dispatcher.h" 27 #include "time_cmd_parse.h" 28 #include "time_service_notify.h" 29 #include "time_service_stub.h" 30 #include "time_sysevent.h" 31 #include "timer_manager.h" 32 33 namespace OHOS { 34 namespace MiscServices { 35 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING }; 36 37 class TimeSystemAbility : public SystemAbility, public TimeServiceStub { 38 DECLARE_SYSTEM_ABILITY(TimeSystemAbility); 39 40 public: 41 DISALLOW_COPY_AND_MOVE(TimeSystemAbility); 42 TimeSystemAbility(int32_t systemAbilityId, bool runOnCreate); 43 TimeSystemAbility(); 44 ~TimeSystemAbility(); 45 static sptr<TimeSystemAbility> GetInstance(); 46 int32_t SetTime(int64_t time, APIVersion apiVersion = APIVersion::API_VERSION_7) override; 47 bool SetRealTime(int64_t time); 48 int32_t SetTimeZone(const std::string &timeZoneId, APIVersion apiVersion = APIVersion::API_VERSION_7) override; 49 int32_t GetTimeZone(std::string &timeZoneId) override; 50 int32_t GetWallTimeMs(int64_t ×) override; 51 int32_t GetWallTimeNs(int64_t ×) override; 52 int32_t GetBootTimeMs(int64_t ×) override; 53 int32_t GetBootTimeNs(int64_t ×) override; 54 int32_t GetMonotonicTimeMs(int64_t ×) override; 55 int32_t GetMonotonicTimeNs(int64_t ×) override; 56 int32_t GetThreadTimeMs(int64_t ×) override; 57 int32_t GetThreadTimeNs(int64_t ×) override; 58 59 int32_t CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions, sptr<IRemoteObject> &obj, 60 uint64_t &timerId) override; 61 int32_t CreateTimer(TimerPara ¶s, std::function<void(const uint64_t)> Callback, uint64_t &timerId); 62 int32_t StartTimer(uint64_t timerId, uint64_t triggerTimes) override; 63 int32_t StopTimer(uint64_t timerId) override; 64 int32_t DestroyTimer(uint64_t timerId) override; 65 bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override; 66 bool ResetAllProxy() override; 67 int Dump(int fd, const std::vector<std::u16string> &args) override; 68 void DumpAllTimeInfo(int fd, const std::vector<std::string> &input); 69 void DumpTimerInfo(int fd, const std::vector<std::string> &input); 70 void DumpTimerInfoById(int fd, const std::vector<std::string> &input); 71 void DumpTimerTriggerById(int fd, const std::vector<std::string> &input); 72 void DumpIdleTimerInfo(int fd, const std::vector<std::string> &input); 73 void InitDumpCmd(); 74 void RegisterSubscriber(); 75 76 protected: 77 void OnStart() override; 78 void OnStop() override; 79 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 80 81 private: 82 class RSSSaDeathRecipient : public IRemoteObject::DeathRecipient { 83 public: 84 explicit RSSSaDeathRecipient()= default;; 85 ~RSSSaDeathRecipient() override = default; 86 void OnRemoteDied(const wptr<IRemoteObject> &object) override; 87 }; 88 int32_t Init(); 89 void InitServiceHandler(); 90 void InitTimerHandler(); 91 void ParseTimerPara(const std::shared_ptr<ITimerInfo> &timerOptions, TimerPara ¶s); 92 bool GetTimeByClockid(clockid_t clockId, struct timespec &tv); 93 int SetRtcTime(time_t sec); 94 bool CheckRtc(const std::string &rtcPath, uint64_t rtcId); 95 int GetWallClockRtcId(); 96 void RegisterRSSDeathCallback(); 97 98 ServiceRunningState state_; 99 static std::mutex instanceLock_; 100 static sptr<TimeSystemAbility> instance_; 101 const int rtcId; 102 static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_; 103 static std::shared_ptr<TimerManager> timerManagerHandler_; 104 sptr<RSSSaDeathRecipient> deathRecipient_ {}; 105 }; 106 } // namespace MiscServices 107 } // namespace OHOS 108 #endif // SERVICES_INCLUDE_TIME_SERVICES_H