• 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 #include "time_sysevent.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     int32_t SetTime(int64_t time, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
55     bool SetRealTime(int64_t time);
56     int32_t SetTimeZone(const std::string &timeZoneId, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
57     int32_t GetTimeZone(std::string &timeZoneId) override;
58     int32_t GetWallTimeMs(int64_t &time);
59     int32_t GetBootTimeMs(int64_t &time);
60     int32_t GetBootTimeNs(int64_t &time);
61     int32_t GetThreadTimeMs(int64_t &time) override;
62     int32_t GetThreadTimeNs(int64_t &time) override;
63     int32_t CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions, sptr<IRemoteObject> &obj,
64         uint64_t &timerId) override;
65     int32_t CreateTimer(TimerPara &paras, std::function<int32_t (const uint64_t)> callback, uint64_t &timerId);
66     int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override;
67     int32_t StopTimer(uint64_t timerId) override;
68     int32_t DestroyTimer(uint64_t timerId, bool isAsync) override;
69     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override;
70     bool ProxyTimer(int32_t uid, std::set<int> pidList, bool isProxy, bool needRetrigger) override;
71     int32_t AdjustTimer(bool isAdjust, uint32_t interval) override;
72     int32_t SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption) override;
73     bool ResetAllProxy() override;
74     int32_t GetNtpTimeMs(int64_t &time) override;
75     int32_t GetRealTimeMs(int64_t &time) override;
76     int Dump(int fd, const std::vector<std::u16string> &args) override;
77     void DumpAllTimeInfo(int fd, const std::vector<std::string> &input);
78     void DumpTimerInfo(int fd, const std::vector<std::string> &input);
79     void DumpTimerInfoById(int fd, const std::vector<std::string> &input);
80     void DumpTimerTriggerById(int fd, const std::vector<std::string> &input);
81     void DumpIdleTimerInfo(int fd, const std::vector<std::string> &input);
82     void DumpProxyTimerInfo(int fd, const std::vector<std::string> &input);
83     void DumpUidTimerMapInfo(int fd, const std::vector<std::string> &input);
84     void DumpPidTimerMapInfo(int fd, const std::vector<std::string> &input);
85     void DumpProxyDelayTime(int fd, const std::vector<std::string> &input);
86     void DumpAdjustTime(int fd, const std::vector<std::string> &input);
87     void InitDumpCmd();
88     void RegisterCommonEventSubscriber();
89     bool RecoverTimer();
90 
91 protected:
92     void OnStart() override;
93     void OnStop() override;
94     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
95 
96 private:
97     class RSSSaDeathRecipient : public IRemoteObject::DeathRecipient {
98     public:
99         explicit RSSSaDeathRecipient()= default;;
100         ~RSSSaDeathRecipient() override = default;
101         void OnRemoteDied(const wptr<IRemoteObject> &object) override;
102     };
103 
104     int32_t Init();
105     void ParseTimerPara(const std::shared_ptr<ITimerInfo> &timerOptions, TimerPara &paras);
106     int32_t CheckTimerPara(const DatabaseType type, const 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     bool IsValidTime(int64_t time);
116     void RecoverTimerInner(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet, bool autoRestore);
117     void SetAutoReboot();
118 
119     ServiceRunningState state_;
120     static std::mutex instanceLock_;
121     static sptr<TimeSystemAbility> instance_;
122     const int rtcId;
123     sptr<RSSSaDeathRecipient> deathRecipient_ {};
124 };
125 } // namespace MiscServices
126 } // namespace OHOS
127 #endif // SERVICES_INCLUDE_TIME_SERVICES_H