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