• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <unordered_set>
22 
23 #include "event_handler.h"
24 #include "securec.h"
25 #include "system_ability.h"
26 #include "ctime"
27 #include "time_cmd_dispatcher.h"
28 #include "time_cmd_parse.h"
29 #include "time_service_notify.h"
30 #include "time_service_stub.h"
31 #include "timer_manager.h"
32 #include "shutdown/sync_shutdown_callback_stub.h"
33 #include "shutdown/shutdown_client.h"
34 #include "rdb_helper.h"
35 
36 namespace OHOS {
37 namespace MiscServices {
38 enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
39 
40 class TimeSystemAbility : public SystemAbility, public TimeServiceStub {
41     DECLARE_SYSTEM_ABILITY(TimeSystemAbility);
42 
43 public:
44     class TimePowerStateListener : public OHOS::PowerMgr::SyncShutdownCallbackStub {
45     public:
46         ~TimePowerStateListener() override = default;
47         void OnSyncShutdown() override;
48     };
49     DISALLOW_COPY_AND_MOVE(TimeSystemAbility);
50     TimeSystemAbility(int32_t systemAbilityId, bool runOnCreate);
51     TimeSystemAbility();
52     ~TimeSystemAbility();
53     static sptr<TimeSystemAbility> GetInstance();
54     static std::shared_ptr<TimerManager> GetManagerHandler();
55     int32_t SetTime(int64_t time, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
56     bool SetRealTime(int64_t time);
57     int32_t SetTimeZone(const std::string &timeZoneId, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
58     int32_t GetTimeZone(std::string &timeZoneId) override;
59     int32_t GetWallTimeMs(int64_t &time) override;
60     int32_t GetWallTimeNs(int64_t &time) override;
61     int32_t GetBootTimeMs(int64_t &time) override;
62     int32_t GetBootTimeNs(int64_t &time) override;
63     int32_t GetMonotonicTimeMs(int64_t &time) override;
64     int32_t GetMonotonicTimeNs(int64_t &time) override;
65     int32_t GetThreadTimeMs(int64_t &time) override;
66     int32_t GetThreadTimeNs(int64_t &time) override;
67     int32_t CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions, sptr<IRemoteObject> &obj,
68         uint64_t &timerId) override;
69     int32_t CreateTimer(TimerPara &paras, std::function<void(const uint64_t)> callback, uint64_t &timerId);
70     int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override;
71     int32_t StopTimer(uint64_t timerId) override;
72     int32_t DestroyTimer(uint64_t timerId) override;
73     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override;
74     bool ProxyTimer(std::set<int> pidList, bool isProxy, bool needRetrigger) override;
75     int32_t AdjustTimer(bool isAdjust, uint32_t interval) override;
76     int32_t SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption) override;
77     bool ResetAllProxy() override;
78     int Dump(int fd, const std::vector<std::u16string> &args) override;
79     void DumpAllTimeInfo(int fd, const std::vector<std::string> &input);
80     void DumpTimerInfo(int fd, const std::vector<std::string> &input);
81     void DumpTimerInfoById(int fd, const std::vector<std::string> &input);
82     void DumpTimerTriggerById(int fd, const std::vector<std::string> &input);
83     void DumpIdleTimerInfo(int fd, const std::vector<std::string> &input);
84     void DumpProxyTimerInfo(int fd, const std::vector<std::string> &input);
85     void DumpUidTimerMapInfo(int fd, const std::vector<std::string> &input);
86     void DumpPidTimerMapInfo(int fd, const std::vector<std::string> &input);
87     void SetProxyDelayTime(int fd, const std::vector<std::string> &input);
88     void DumpProxyDelayTime(int fd, const std::vector<std::string> &input);
89     void DumpAdjustTime(int fd, const std::vector<std::string> &input);
90     void InitDumpCmd();
91     void RegisterCommonEventSubscriber();
92     bool RecoverTimer();
93 
94 protected:
95     void OnStart() override;
96     void OnStop() override;
97     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
98 
99 private:
100     class RSSSaDeathRecipient : public IRemoteObject::DeathRecipient {
101     public:
102         explicit RSSSaDeathRecipient()= default;;
103         ~RSSSaDeathRecipient() override = default;
104         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
105     };
106 
107     int32_t Init();
108     void InitServiceHandler();
109     void InitTimerHandler();
110     void ParseTimerPara(const std::shared_ptr<ITimerInfo> &timerOptions, TimerPara &paras);
111     bool GetTimeByClockId(clockid_t clockId, struct timespec &tv);
112     int SetRtcTime(time_t sec);
113     bool CheckRtc(const std::string &rtcPath, uint64_t rtcId);
114     int GetWallClockRtcId();
115     void RegisterRSSDeathCallback();
116     void RegisterPowerStateListener();
117     void RegisterScreenOnSubscriber();
118     void RegisterNitzTimeSubscriber();
119     bool IsValidTime(int64_t time);
120     void RecoverTimerInner(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet);
121     void SetAutoReboot();
122 
123     ServiceRunningState state_;
124     static std::mutex instanceLock_;
125     static sptr<TimeSystemAbility> instance_;
126     const int rtcId;
127     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
128     static std::shared_ptr<TimerManager> timerManagerHandler_;
129     sptr<RSSSaDeathRecipient> deathRecipient_ {};
130 };
131 } // namespace MiscServices
132 } // namespace OHOS
133 #endif // SERVICES_INCLUDE_TIME_SERVICES_H