1cf69771bSopenharmony_ci/*
2cf69771bSopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd.
3cf69771bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cf69771bSopenharmony_ci * you may not use this file except in compliance with the License.
5cf69771bSopenharmony_ci * You may obtain a copy of the License at
6cf69771bSopenharmony_ci *
7cf69771bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cf69771bSopenharmony_ci *
9cf69771bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cf69771bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cf69771bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cf69771bSopenharmony_ci * See the License for the specific language governing permissions and
13cf69771bSopenharmony_ci * limitations under the License.
14cf69771bSopenharmony_ci */
15cf69771bSopenharmony_ci
16cf69771bSopenharmony_ci#ifndef SERVICES_INCLUDE_TIME_SERVICES_H
17cf69771bSopenharmony_ci#define SERVICES_INCLUDE_TIME_SERVICES_H
18cf69771bSopenharmony_ci
19cf69771bSopenharmony_ci#include <cinttypes>
20cf69771bSopenharmony_ci#include <mutex>
21cf69771bSopenharmony_ci#include <unordered_set>
22cf69771bSopenharmony_ci
23cf69771bSopenharmony_ci#include "event_handler.h"
24cf69771bSopenharmony_ci#include "securec.h"
25cf69771bSopenharmony_ci#include "system_ability.h"
26cf69771bSopenharmony_ci#include "time_cmd_dispatcher.h"
27cf69771bSopenharmony_ci#include "time_cmd_parse.h"
28cf69771bSopenharmony_ci#include "time_service_notify.h"
29cf69771bSopenharmony_ci#include "time_service_stub.h"
30cf69771bSopenharmony_ci#include "timer_manager.h"
31cf69771bSopenharmony_ci#include "shutdown/sync_shutdown_callback_stub.h"
32cf69771bSopenharmony_ci#include "shutdown/shutdown_client.h"
33cf69771bSopenharmony_ci#include "rdb_helper.h"
34cf69771bSopenharmony_ci
35cf69771bSopenharmony_cinamespace OHOS {
36cf69771bSopenharmony_cinamespace MiscServices {
37cf69771bSopenharmony_cienum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
38cf69771bSopenharmony_ci
39cf69771bSopenharmony_ciclass TimeSystemAbility : public SystemAbility, public TimeServiceStub {
40cf69771bSopenharmony_ci    DECLARE_SYSTEM_ABILITY(TimeSystemAbility);
41cf69771bSopenharmony_ci
42cf69771bSopenharmony_cipublic:
43cf69771bSopenharmony_ci    class TimePowerStateListener : public OHOS::PowerMgr::SyncShutdownCallbackStub {
44cf69771bSopenharmony_ci    public:
45cf69771bSopenharmony_ci        ~TimePowerStateListener() override = default;
46cf69771bSopenharmony_ci        void OnSyncShutdown() override;
47cf69771bSopenharmony_ci    };
48cf69771bSopenharmony_ci    DISALLOW_COPY_AND_MOVE(TimeSystemAbility);
49cf69771bSopenharmony_ci    TimeSystemAbility(int32_t systemAbilityId, bool runOnCreate);
50cf69771bSopenharmony_ci    TimeSystemAbility();
51cf69771bSopenharmony_ci    ~TimeSystemAbility();
52cf69771bSopenharmony_ci    static sptr<TimeSystemAbility> GetInstance();
53cf69771bSopenharmony_ci    int32_t SetTime(int64_t time, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
54cf69771bSopenharmony_ci    bool SetRealTime(int64_t time);
55cf69771bSopenharmony_ci    int32_t SetTimeZone(const std::string &timeZoneId, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
56cf69771bSopenharmony_ci    int32_t GetTimeZone(std::string &timeZoneId) override;
57cf69771bSopenharmony_ci    int32_t GetWallTimeMs(int64_t &time);
58cf69771bSopenharmony_ci    int32_t GetBootTimeMs(int64_t &time);
59cf69771bSopenharmony_ci    int32_t GetBootTimeNs(int64_t &time);
60cf69771bSopenharmony_ci    int32_t GetThreadTimeMs(int64_t &time) override;
61cf69771bSopenharmony_ci    int32_t GetThreadTimeNs(int64_t &time) override;
62cf69771bSopenharmony_ci    int32_t CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions, sptr<IRemoteObject> &obj,
63cf69771bSopenharmony_ci        uint64_t &timerId) override;
64cf69771bSopenharmony_ci    int32_t CreateTimer(TimerPara &paras, std::function<int32_t (const uint64_t)> callback, uint64_t &timerId);
65cf69771bSopenharmony_ci    int32_t StartTimer(uint64_t timerId, uint64_t triggerTime) override;
66cf69771bSopenharmony_ci    int32_t StopTimer(uint64_t timerId) override;
67cf69771bSopenharmony_ci    int32_t DestroyTimer(uint64_t timerId, bool isAsync) override;
68cf69771bSopenharmony_ci    bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override;
69cf69771bSopenharmony_ci    bool ProxyTimer(std::set<int> pidList, bool isProxy, bool needRetrigger) override;
70cf69771bSopenharmony_ci    int32_t AdjustTimer(bool isAdjust, uint32_t interval) override;
71cf69771bSopenharmony_ci    int32_t SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption) override;
72cf69771bSopenharmony_ci    bool ResetAllProxy() override;
73cf69771bSopenharmony_ci    int32_t GetNtpTimeMs(int64_t &time) override;
74cf69771bSopenharmony_ci    int32_t GetRealTimeMs(int64_t &time) override;
75cf69771bSopenharmony_ci    int Dump(int fd, const std::vector<std::u16string> &args) override;
76cf69771bSopenharmony_ci    void DumpAllTimeInfo(int fd, const std::vector<std::string> &input);
77cf69771bSopenharmony_ci    void DumpTimerInfo(int fd, const std::vector<std::string> &input);
78cf69771bSopenharmony_ci    void DumpTimerInfoById(int fd, const std::vector<std::string> &input);
79cf69771bSopenharmony_ci    void DumpTimerTriggerById(int fd, const std::vector<std::string> &input);
80cf69771bSopenharmony_ci    void DumpIdleTimerInfo(int fd, const std::vector<std::string> &input);
81cf69771bSopenharmony_ci    void DumpProxyTimerInfo(int fd, const std::vector<std::string> &input);
82cf69771bSopenharmony_ci    void DumpUidTimerMapInfo(int fd, const std::vector<std::string> &input);
83cf69771bSopenharmony_ci    void DumpPidTimerMapInfo(int fd, const std::vector<std::string> &input);
84cf69771bSopenharmony_ci    void DumpProxyDelayTime(int fd, const std::vector<std::string> &input);
85cf69771bSopenharmony_ci    void DumpAdjustTime(int fd, const std::vector<std::string> &input);
86cf69771bSopenharmony_ci    void InitDumpCmd();
87cf69771bSopenharmony_ci    void RegisterCommonEventSubscriber();
88cf69771bSopenharmony_ci    bool RecoverTimer();
89cf69771bSopenharmony_ci
90cf69771bSopenharmony_ciprotected:
91cf69771bSopenharmony_ci    void OnStart() override;
92cf69771bSopenharmony_ci    void OnStop() override;
93cf69771bSopenharmony_ci    void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
94cf69771bSopenharmony_ci
95cf69771bSopenharmony_ciprivate:
96cf69771bSopenharmony_ci    class RSSSaDeathRecipient : public IRemoteObject::DeathRecipient {
97cf69771bSopenharmony_ci    public:
98cf69771bSopenharmony_ci        explicit RSSSaDeathRecipient()= default;;
99cf69771bSopenharmony_ci        ~RSSSaDeathRecipient() override = default;
100cf69771bSopenharmony_ci        void OnRemoteDied(const wptr<IRemoteObject> &object) override;
101cf69771bSopenharmony_ci    };
102cf69771bSopenharmony_ci
103cf69771bSopenharmony_ci    int32_t Init();
104cf69771bSopenharmony_ci    void ParseTimerPara(const std::shared_ptr<ITimerInfo> &timerOptions, TimerPara &paras);
105cf69771bSopenharmony_ci    bool GetTimeByClockId(clockid_t clockId, struct timespec &tv);
106cf69771bSopenharmony_ci    int SetRtcTime(time_t sec);
107cf69771bSopenharmony_ci    bool CheckRtc(const std::string &rtcPath, uint64_t rtcId);
108cf69771bSopenharmony_ci    int GetWallClockRtcId();
109cf69771bSopenharmony_ci    void RegisterRSSDeathCallback();
110cf69771bSopenharmony_ci    void RegisterPowerStateListener();
111cf69771bSopenharmony_ci    void RegisterScreenOnSubscriber();
112cf69771bSopenharmony_ci    void RegisterNitzTimeSubscriber();
113cf69771bSopenharmony_ci    void RegisterOsAccountSubscriber();
114cf69771bSopenharmony_ci    bool IsValidTime(int64_t time);
115cf69771bSopenharmony_ci    void RecoverTimerInner(std::shared_ptr<OHOS::NativeRdb::ResultSet> resultSet);
116cf69771bSopenharmony_ci    void SetAutoReboot();
117cf69771bSopenharmony_ci
118cf69771bSopenharmony_ci    ServiceRunningState state_;
119cf69771bSopenharmony_ci    static std::mutex instanceLock_;
120cf69771bSopenharmony_ci    static sptr<TimeSystemAbility> instance_;
121cf69771bSopenharmony_ci    const int rtcId;
122cf69771bSopenharmony_ci    sptr<RSSSaDeathRecipient> deathRecipient_ {};
123cf69771bSopenharmony_ci};
124cf69771bSopenharmony_ci} // namespace MiscServices
125cf69771bSopenharmony_ci} // namespace OHOS
126cf69771bSopenharmony_ci#endif // SERVICES_INCLUDE_TIME_SERVICES_H