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);
58     int32_t GetBootTimeMs(int64_t &time);
59     int32_t GetBootTimeNs(int64_t &time);
60     int32_t GetThreadTimeMs(int64_t &time) override;
61     int32_t GetThreadTimeNs(int64_t &time) override;
62     int32_t CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions, sptr<IRemoteObject> &obj,
63         uint64_t &timerId) override;
64     int32_t CreateTimer(TimerPara &paras, std::function<int32_t (const uint64_t)> callback, uint64_t &timerId);
65     int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override;
66     int32_t StopTimer(uint64_t timerId) override;
67     int32_t DestroyTimer(uint64_t timerId, bool isAsync) override;
68     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override;
69     bool ProxyTimer(int32_t uid, std::set<int> pidList, bool isProxy, bool needRetrigger) override;
70     int32_t AdjustTimer(bool isAdjust, uint32_t interval) override;
71     int32_t SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption) override;
72     bool ResetAllProxy() override;
73     int32_t GetNtpTimeMs(int64_t &time) override;
74     int32_t GetRealTimeMs(int64_t &time) override;
75     int Dump(int fd, const std::vector<std::u16string> &args) override;
76     void DumpAllTimeInfo(int fd, const std::vector<std::string> &input);
77     void DumpTimerInfo(int fd, const std::vector<std::string> &input);
78     void DumpTimerInfoById(int fd, const std::vector<std::string> &input);
79     void DumpTimerTriggerById(int fd, const std::vector<std::string> &input);
80     void DumpIdleTimerInfo(int fd, const std::vector<std::string> &input);
81     void DumpProxyTimerInfo(int fd, const std::vector<std::string> &input);
82     void DumpUidTimerMapInfo(int fd, const std::vector<std::string> &input);
83     void DumpPidTimerMapInfo(int fd, const std::vector<std::string> &input);
84     void DumpProxyDelayTime(int fd, const std::vector<std::string> &input);
85     void DumpAdjustTime(int fd, const std::vector<std::string> &input);
86     void InitDumpCmd();
87     void RegisterCommonEventSubscriber();
88     bool RecoverTimer();
89 
90 protected:
91     void OnStart() override;
92     void OnStop() override;
93     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
94 
95 private:
96     class RSSSaDeathRecipient : public IRemoteObject::DeathRecipient {
97     public:
98         explicit RSSSaDeathRecipient()= default;;
99         ~RSSSaDeathRecipient() override = default;
100         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
101     };
102 
103     int32_t Init();
104     void ParseTimerPara(const std::shared_ptr<ITimerInfo> &timerOptions, TimerPara &paras);
105     bool GetTimeByClockId(clockid_t clockId, struct timespec &tv);
106     int SetRtcTime(time_t sec);
107     bool CheckRtc(const std::string &rtcPath, uint64_t rtcId);
108     int GetWallClockRtcId();
109     void RegisterRSSDeathCallback();
110     void RegisterPowerStateListener();
111     void RegisterScreenOnSubscriber();
112     void RegisterNitzTimeSubscriber();
113     bool IsValidTime(int64_t time);
114     void RecoverTimerInner(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet);
115     void SetAutoReboot();
116 
117     ServiceRunningState state_;
118     static std::mutex instanceLock_;
119     static sptr<TimeSystemAbility> instance_;
120     const int rtcId;
121     sptr<RSSSaDeathRecipient> deathRecipient_ {};
122 };
123 } // namespace MiscServices
124 } // namespace OHOS
125 #endif // SERVICES_INCLUDE_TIME_SERVICES_H