• 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_MANAGER_H
17 #define SERVICES_INCLUDE_TIME_SERVICES_MANAGER_H
18 
19 #include <mutex>
20 
21 #include "refbase.h"
22 #include "time_service_interface.h"
23 #include "iremote_object.h"
24 #include "timer_call_back.h"
25 
26 namespace OHOS {
27 namespace MiscServices {
28 constexpr int64_t ERROR_OPREATION_FAILED = -1;
29 
30 class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient {
31 public:
32     explicit TimeSaDeathRecipient();
33     ~TimeSaDeathRecipient() = default;
34     void OnRemoteDied(const wptr<IRemoteObject> &object) override;
35 private:
36     DISALLOW_COPY_AND_MOVE(TimeSaDeathRecipient);
37 };
38 
39 class TimeServiceClient : public RefBase {
40 public:
41     DISALLOW_COPY_AND_MOVE(TimeServiceClient);
42     static sptr<TimeServiceClient> GetInstance();
43 
44     /**
45      * SetTime
46      * @description
47      * @param milliseconds int64_t UTC time in milliseconds.
48      * @return bool true on success, false on failure.
49      */
50     bool SetTime(const int64_t milliseconds);
51 
52     /**
53      * SetTime
54      * @description
55      * @param milliseconds int64_t UTC time in milliseconds.
56      * @param code error code return.
57      * @return bool true on success, false on failure.
58      */
59     bool SetTime(const int64_t milliseconds, int32_t &code);
60     int32_t SetTimeV9(const int64_t &time);
61     /**
62      * SetTimeZone
63      * @description
64      * @param timeZoneId const std::string time zone. example: "Beijing, China".
65      * @return bool true on success, false on failure.
66      */
67     bool SetTimeZone(const std::string timeZoneId);
68 
69     /**
70      * SetTimeZone
71      * @description
72      * @param timeZoneId const std::string time zone. example: "Beijing, China".
73      * @param code error code return.
74      * @return bool true on success, false on failure.
75      */
76     bool SetTimeZone(const std::string timezoneId, int32_t &code);
77     int32_t SetTimeZoneV9(const std::string timezoneId);
78     /**
79      * GetTimeZone
80      * @description
81      * @return std::string, time zone example: "Beijing, China", if result length == 0 on failed.
82      */
83     std::string GetTimeZone();
84     int32_t GetTimeZone(std::string &timezoneId);
85     /**
86      * GetWallTimeMs
87      * @description get the wall time(the UTC time from 1970 0H:0M:0S) in milliseconds
88      * @return int64_t, milliseconds in wall time, ret < 0 on failed.
89      */
90     int64_t GetWallTimeMs();
91     int32_t GetWallTimeMs(int64_t &time);
92 
93     /**
94      * GetWallTimeNs
95      * @description get the wall time(the UTC time from 1970 0H:0M:0S) in nanoseconds
96      * @return int64_t, nanoseconds in wall time, ret < 0 on failed.
97      */
98     int64_t GetWallTimeNs();
99     int32_t GetWallTimeNs(int64_t &time);
100     /**
101      * GetBootTimeMs
102      * @description get the time since boot(include time spent in sleep) in milliseconds.
103      * @return int64_t, milliseconds in boot time, ret < 0 on failed.
104      */
105     int64_t GetBootTimeMs();
106     int32_t GetBootTimeMs(int64_t &time);
107     /**
108      * GetBootTimeNs
109      * @description // get the time since boot(include time spent in sleep) in nanoseconds.
110      * @return int64_t, nanoseconds in boot time, ret < 0 on failed.
111      */
112     int64_t GetBootTimeNs();
113     int32_t GetBootTimeNs(int64_t &time);
114     /**
115      * GetMonotonicTimeMs
116      * @description get the time since boot(exclude time spent in sleep) in milliseconds.
117      * @return int64_t, milliseconds in Monotonic time, ret < 0 on failed.
118      */
119     int64_t GetMonotonicTimeMs();
120     int32_t GetMonotonicTimeMs(int64_t &time);
121     /**
122      * GetMonotonicTimeNs
123      * @description get the time since boot(exclude time spent in sleep) in nanoseconds.
124      * @return int64_t, nanoseconds in Monotonic time, ret < 0 on failed.
125      */
126     int64_t GetMonotonicTimeNs();
127     int32_t GetMonotonicTimeNs(int64_t &time);
128     /**
129      * GetThreadTimeMs
130      * @description get the Thread-specific CPU-time in milliseconds.
131      * @return int64_t, milliseconds in Thread-specific CPU-time, ret < 0 on failed.
132      */
133     int64_t GetThreadTimeMs();
134     int32_t GetThreadTimeMs(int64_t &time);
135     /**
136      * GetThreadTimeNs
137      * @description get the Thread-specific CPU-time in nanoseconds.
138      * @return int64_t, nanoseconds in Thread-specific CPU-time, ret < 0 on failed.
139      */
140     int64_t GetThreadTimeNs();
141     int32_t GetThreadTimeNs(int64_t &time);
142     /**
143      * CreateTimer
144      * @param TimerInfo  timer info
145      * @return uint64_t > 0 on success, == 0 failure.
146      */
147     uint64_t CreateTimer(std::shared_ptr<ITimerInfo> TimerInfo);
148     int32_t CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions, uint64_t &timerId);
149 
150      /**
151      * StartTimer
152      * @param timerId indicate timerId
153      * @param triggerTime  trigger times
154      * @return bool true on success, false on failure.
155      */
156     bool StartTimer(uint64_t timerId, uint64_t triggerTime);
157     int32_t StartTimerV9(uint64_t timerId, uint64_t triggerTime);
158 
159      /**
160      * StopTimer
161      * @param timerId indicate timerId
162      * @return bool true on success, false on failure.
163      */
164     bool StopTimer(uint64_t timerId);
165     int32_t StopTimerV9(uint64_t timerId);
166 
167      /**
168      * DestroyTimer
169      * @param timerId indicate timerId
170      * @return bool true on success, false on failure.
171      */
172     bool DestroyTimer(uint64_t timerId);
173     int32_t DestroyTimerV9(uint64_t timerId);
174 
175      /**
176      * NetworkTimeStatusOff
177      * @return void.
178      */
179     void NetworkTimeStatusOff();
180 
181      /**
182      * NetworkTimeStatusOn
183      * @return void.
184      */
185     void NetworkTimeStatusOn();
186 
187     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
188 
189     /**
190      * ProxyTimer
191      * @param uid the uid
192      * @param isProxy true if set proxy, false if remove proxy.
193      * @param needRetrigger true if need retrigger, false if not.
194      * @return bool true on success, false on failure.
195      */
196     bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger);
197 
198     /**
199      * ResetAllProxy
200      * @return bool true on success, false on failure.
201      */
202     bool ResetAllProxy();
203     bool ConnectService();
204     void ClearProxy();
205 
206 private:
207     TimeServiceClient();
208     ~TimeServiceClient();
209     sptr<ITimeService> GetProxy();
210     void SetProxy(sptr<ITimeService> proxy);
211 
212     static std::mutex instanceLock_;
213     static sptr<TimeServiceClient> instance_;
214     std::mutex proxyLock_;
215     sptr<ITimeService> timeServiceProxy_;
216 };
217 } // MiscServices
218 } // OHOS
219 #endif // SERVICES_INCLUDE_TIME_SERVICES_MANAGER_H