• 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 
22 #include "ctime"
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 "time_sysevent.h"
31 #include "timer_manager.h"
32 
33 namespace OHOS {
34 namespace MiscServices {
35 enum class ServiceRunningState {
36     STATE_NOT_START,
37     STATE_RUNNING
38 };
39 
40 class TimeSystemAbility : public SystemAbility, public TimeServiceStub {
41     DECLARE_SYSTEM_ABILITY(TimeService);
42 public:
43     DISALLOW_COPY_AND_MOVE(TimeSystemAbility);
44     TimeSystemAbility(int32_t systemAbilityId, bool runOnCreate);
45     TimeSystemAbility();
46     ~TimeSystemAbility();
47     static sptr<TimeSystemAbility> GetInstance();
48     int32_t SetTime(int64_t time, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
49     bool SetRealTime(int64_t time);
50     int32_t SetTimeZone(const std::string &timeZoneId, APIVersion apiVersion = APIVersion::API_VERSION_7) override;
51     int32_t GetTimeZone(std::string &timeZoneId) override;
52     int32_t GetWallTimeMs(int64_t &times) override;
53     int32_t GetWallTimeNs(int64_t &times) override;
54     int32_t GetBootTimeMs(int64_t &times) override;
55     int32_t GetBootTimeNs(int64_t &times) override;
56     int32_t GetMonotonicTimeMs(int64_t &times) override;
57     int32_t GetMonotonicTimeNs(int64_t &times) override;
58     int32_t GetThreadTimeMs(int64_t &times) override;
59     int32_t GetThreadTimeNs(int64_t &times) override;
60 
61     int32_t CreateTimer(const std::shared_ptr<ITimerInfo> &timerOptions, sptr<IRemoteObject> &obj,
62         uint64_t &timerId) override;
63     int32_t CreateTimer(TimerPara &paras, std::function<void(const uint64_t)> Callback, uint64_t &timerId);
64     int32_t StartTimer(uint64_t timerId, uint64_t triggerTimes) override;
65     int32_t StopTimer(uint64_t timerId) override;
66     int32_t DestroyTimer(uint64_t timerId) override;
67     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger) override;
68     bool ResetAllProxy() override;
69     int Dump(int fd, const std::vector<std::u16string> &args) override;
70     void DumpAllTimeInfo(int fd, const std::vector<std::string> &input);
71     void DumpTimerInfo(int fd, const std::vector<std::string> &input);
72     void DumpTimerInfoById(int fd, const std::vector<std::string> &input);
73     void DumpTimerTriggerById(int fd, const std::vector<std::string> &input);
74     void InitDumpCmd();
75     void RegisterSubscriber();
76 
77 protected:
78     void OnStart() override;
79     void OnStop() override;
80     void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
81 
82 private:
83     int32_t Init();
84     void InitServiceHandler();
85     void InitTimerHandler();
86     void ParseTimerPara(std::shared_ptr<ITimerInfo> timerOptions, TimerPara &paras);
87     bool GetTimeByClockid(clockid_t clockId, struct timespec &tv);
88     int SetRtcTime(time_t sec);
89 
90     bool CheckRtc(const std::string &rtcPath, uint64_t rtcId);
91     int GetWallClockRtcId();
92 
93     ServiceRunningState state_;
94     static std::mutex instanceLock_;
95     static sptr<TimeSystemAbility> instance_;
96     const int rtcId;
97     static std::shared_ptr<AppExecFwk::EventHandler> serviceHandler_;
98     static std::shared_ptr<TimerManager> timerManagerHandler_;
99 };
100 } // MiscServices
101 } // OHOS
102 #endif // SERVICES_INCLUDE_TIME_SERVICES_H