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 #include <sstream>
21 #include <unordered_set>
22 
23 #include "itimer_info.h"
24 #include "refbase.h"
25 #include "time_service_interface.h"
26 #include "iservice_registry.h"
27 #include "system_ability_definition.h"
28 #include "system_ability_status_change_stub.h"
29 #include "visibility.h"
30 
31 namespace OHOS {
32 namespace MiscServices {
33 class TimeServiceClient : public RefBase {
34 public:
35     DISALLOW_COPY_AND_MOVE(TimeServiceClient);
36     TIME_API static sptr<TimeServiceClient> GetInstance();
37 
38     /**
39      * @brief Set time
40      *
41      * This api is used to set system time.
42      *
43      * @param UTC time in milliseconds.
44      * @return true on success, false on failure.
45      */
46     TIME_API bool SetTime(int64_t milliseconds);
47 
48     /**
49      * @brief Set system time
50      *
51      * This api is used to set system time.
52      *
53      * @param UTC time in milliseconds.
54      * @param error code.
55      * @return true on success, false on failure.
56      */
57     TIME_API bool SetTime(int64_t milliseconds, int32_t &code);
58 
59     /**
60      * @brief Set system time
61      *
62      * This api is used to set system time.
63      *
64      * @param UTC time in milliseconds.
65      * @return error code.
66      */
67     TIME_API int32_t SetTimeV9(int64_t time);
68 
69     /**
70      * @brief Set Timezone
71      *
72      * This api is used to set timezone.
73      *
74      * @param const std::string time zone. example: "Beijing, China".
75      * @return true on success, false on failure.
76      */
77     TIME_API bool SetTimeZone(const std::string &timeZoneId);
78 
79     /**
80      * @brief Set Timezone
81      *
82      * This api is used to set timezone.
83      *
84      * @param const std::string time zone. example: "Beijing, China".
85      * @param error code.
86      * @return true on success, false on failure.
87      */
88     TIME_API bool SetTimeZone(const std::string &timezoneId, int32_t &code);
89 
90     /**
91      * @brief Set Timezone
92      *
93      * This api is used to set timezone.
94      *
95      * @param const std::string time zone. example: "Beijing, China".
96      * @return error code.
97      */
98     TIME_API int32_t SetTimeZoneV9(const std::string &timezoneId);
99 
100     /**
101      * @brief Get Timezone
102      *
103      * This api is used to get current system timezone.
104      *
105      * @return time zone example: "Beijing, China", if result length == 0 on failed.
106      */
107     TIME_API std::string GetTimeZone();
108 
109     /**
110      * @brief Get Timezone
111      *
112      * This api is used to get current system timezone.
113      *
114      * @param The current system time zone, example: "Beijing, China", if failed the value is nullptr.
115      * @return error code.
116      */
117     TIME_API int32_t GetTimeZone(std::string &timezoneId);
118 
119     /**
120      * @brief GetWallTimeMs
121      *
122      * Get the wall time(the UTC time from 1970 0H:0M:0S) in milliseconds
123      *
124      * @return milliseconds in wall time, ret < 0 on failed.
125      */
126     TIME_API int64_t GetWallTimeMs();
127 
128     /**
129      * @brief GetWallTimeMs
130      *
131      * Get the wall time(the UTC time from 1970 0H:0M:0S) in milliseconds.
132      *
133      * @param milliseconds in wall time.
134      * @return error code.
135      */
136     TIME_API int32_t GetWallTimeMs(int64_t &time);
137 
138     /**
139      * @brief GetWallTimeNs
140      *
141      * Get the wall time(the UTC time from 1970 0H:0M:0S) in nanoseconds.
142      *
143      * @return nanoseconds in wall time, ret < 0 on failed.
144      */
145     TIME_API int64_t GetWallTimeNs();
146 
147     /**
148      * @brief GetWallTimeNs
149      *
150      * Get the wall time(the UTC time from 1970 0H:0M:0S) in nanoseconds.
151      *
152      * @param nanoseconds in wall time.
153      * @return error code.
154      */
155     TIME_API int32_t GetWallTimeNs(int64_t &time);
156 
157     /**
158      * @brief GetBootTimeMs
159      *
160      * Get the time since boot(include time spent in sleep) in milliseconds.
161      *
162      * @return milliseconds in boot time, ret < 0 on failed.
163      */
164     TIME_API int64_t GetBootTimeMs();
165 
166     /**
167      * @brief GetBootTimeMs
168      *
169      * Get the time since boot(include time spent in sleep) in milliseconds.
170      *
171      * @param milliseconds in boot time.
172      * @param error code.
173      */
174     TIME_API int32_t GetBootTimeMs(int64_t &time);
175 
176     /**
177      * @brief GetBootTimeNs
178      *
179      * Get the time since boot(include time spent in sleep) in nanoseconds.
180      *
181      * @return nanoseconds in boot time, ret < 0 on failed.
182      */
183     TIME_API int64_t GetBootTimeNs();
184 
185     /**
186      * @brief GetBootTimeNs
187      *
188      * Get the time since boot(include time spent in sleep) in nanoseconds.
189      *
190      * @param nanoseconds in boot time.
191      * @return error code.
192      */
193     TIME_API int32_t GetBootTimeNs(int64_t &time);
194 
195     /**
196      * @brief GetMonotonicTimeMs
197      *
198      * Get the time since boot(exclude time spent in sleep) in milliseconds.
199      *
200      * @return milliseconds in Monotonic time, ret < 0 on failed.
201      */
202     TIME_API int64_t GetMonotonicTimeMs();
203 
204     /**
205      * @brief GetMonotonicTimeMs
206      *
207      * Get the time since boot(exclude time spent in sleep) in milliseconds.
208      *
209      * @param milliseconds in Monotonic time.
210      * @return error code.
211      */
212     TIME_API int32_t GetMonotonicTimeMs(int64_t &time);
213 
214     /**
215      * @brief GetMonotonicTimeNs
216      *
217      * Get the time since boot(exclude time spent in sleep) in nanoseconds.
218      *
219      * @return nanoseconds in Monotonic time, ret < 0 on failed.
220      */
221     TIME_API int64_t GetMonotonicTimeNs();
222 
223     /**
224      * @brief GetMonotonicTimeNs
225      *
226      * Get the time since boot(exclude time spent in sleep) in nanoseconds.
227      *
228      * @param nanoseconds in Monotonic time.
229      * @return error code.
230      */
231     TIME_API int32_t GetMonotonicTimeNs(int64_t &time);
232 
233     /**
234      * @brief GetThreadTimeMs
235      *
236      * Get the thread time in milliseconds.
237      *
238      * @return milliseconds in Thread-specific CPU-time, ret < 0 on failed.
239      */
240     TIME_API int64_t GetThreadTimeMs();
241 
242     /**
243      * @brief GetThreadTimeMs
244      *
245      * Get the thread time in milliseconds.
246      *
247      * @param the Thread-specific CPU-time in milliseconds.
248      * @return error code.
249      */
250     TIME_API int32_t GetThreadTimeMs(int64_t &time);
251 
252     /**
253      * @brief GetThreadTimeNs
254      *
255      * Get the thread time in nanoseconds.
256      *
257      * @return nanoseconds in Thread-specific CPU-time, ret < 0 on failed.
258      */
259     TIME_API int64_t GetThreadTimeNs();
260 
261     /**
262      * @brief GetThreadTimeNs
263      *
264      * Get the thread time in nanoseconds.
265      *
266      * @param get the Thread-specific CPU-time in nanoseconds.
267      * @return error code.
268      */
269     TIME_API int32_t GetThreadTimeNs(int64_t &time);
270 
271     /**
272      * @brief CreateTimer
273      *
274      * Creates a timer.
275      *
276      * @param indicates the timer options.
277      * @return timer id.
278      */
279     TIME_API uint64_t CreateTimer(std::shared_ptr<ITimerInfo> timerInfo);
280 
281     /**
282      * @brief Create Timer
283      *
284      * Creates a timer.
285      *
286      * @param indicates the timer options.
287      * @param timer id.
288      * @return error code.
289      */
290     TIME_API int32_t CreateTimerV9(std::shared_ptr<ITimerInfo> timerOptions, uint64_t &timerId);
291 
292     /**
293      * @brief StartTimer
294      *
295      * Starts a timer.
296      *
297      * @param indicate timerId
298      * @param trigger time
299      * @return true on success, false on failure.
300      */
301     TIME_API bool StartTimer(uint64_t timerId, uint64_t triggerTime);
302 
303     /**
304      * @brief Start Timer
305      *
306      * Starts a timer.
307      *
308      * @param indicate timerId.
309      * @param trigger time.
310      * @return true on success, false on failure.
311      */
312     TIME_API int32_t StartTimerV9(uint64_t timerId, uint64_t triggerTime);
313 
314     /**
315      * @brief Stop Timer
316      *
317      * Starts a timer.
318      *
319      * @param indicate timerId.
320      * @return true on success, false on failure.
321      */
322     TIME_API bool StopTimer(uint64_t timerId);
323 
324     /**
325      * @brief StopTimer
326      *
327      * Stops a timer.
328      *
329      * @param indicate timerId.
330      * @return error code.
331      */
332     TIME_API int32_t StopTimerV9(uint64_t timerId);
333 
334     /**
335      * @brief DestroyTimer
336      *
337      * Destroy a timer.
338      *
339      * @param indicate timerId.
340      * @return true on success, false on failure.
341      */
342     TIME_API bool DestroyTimer(uint64_t timerId);
343 
344     /**
345      * @brief DestroyTimerAsync
346      *
347      * Destroy a timer asynchronously.
348      *
349      * @param indicate timerId.
350      * @return true on success, false on failure.
351      */
352     TIME_API bool DestroyTimerAsync(uint64_t timerId);
353 
354     /**
355      * @brief DestroyTimer
356      *
357      * Destroy a timer.
358      *
359      * @param indicate timerId.
360      * @return error code.
361      */
362     TIME_API int32_t DestroyTimerV9(uint64_t timerId);
363 
364     /**
365      * @brief DestroyTimerAsync
366      *
367      * Destroy a timer asynchronously.
368      *
369      * @param indicate timerId.
370      * @return error code.
371      */
372     TIME_API int32_t DestroyTimerAsyncV9(uint64_t timerId);
373 
374     /**
375      * @brief ProxyTimer
376      *
377      * Wake up all timers for provided uid by proxy.
378      *
379      * @param uid the uid.
380      * @param true if set proxy, false if remove proxy.
381      * @param true if need retrigger, false if not.
382      * @return true on success, false on failure.
383      */
384     TIME_API bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger);
385 
386     /**
387      * @brief ProxyTimer
388      *
389      * Wake up all timers for provided pid by proxy.
390      *
391      * @param pid the pid.
392      * @param true if set proxy, false if remove proxy.
393      * @param true if need retrigger, false if not.
394      * @return true on success, false on failure.
395      */
396     TIME_API bool ProxyTimer(int32_t uid, std::set<int> pidList, bool isProxy, bool needRetrigger);
397 
398     /**
399      * @brief AdjustTimer
400      * adjust bundle or system process timer
401      * @param isAdjust true if adjust, false if not adjust.
402      * @param interval adjust period in seconds.
403      * @return int32_t return error code.
404      */
405     TIME_API int32_t AdjustTimer(bool isAdjust, uint32_t interval);
406 
407     /**
408      * @brief SetTimerExemption
409      * set exemption list for adjust timer
410      * @param nameArr list for bundle name or proccess name.
411      * @param isExemption exemption or ctrl.
412      * @return int32_t return error code.
413      */
414     TIME_API int32_t SetTimerExemption(const std::unordered_set<std::string> &nameArr, bool isExemption);
415 
416     /**
417      * @brief ResetAllProxy
418      *
419      * Wake up all timers by proxy.
420      *
421      * @return bool true on success, false on failure.
422      */
423     TIME_API bool ResetAllProxy();
424 
425     /**
426      * @brief GetNtpTimeMs
427      *
428      * Obtain the wall time through ntp.
429      *
430      * @param time the wall time(the UTC time from 1970 0H:0M:0S) in milliseconds.
431      * @return int32_t return error code.
432      */
433     TIME_API int32_t GetNtpTimeMs(int64_t &time);
434 
435     /**
436      * @brief GetRealTimeMs
437      *
438      * Obtain the wall time based on the last ntp time.
439      *
440      * @param time the wall time(the UTC time from 1970 0H:0M:0S) in milliseconds.
441      * @return int32_t return error code.
442      */
443     TIME_API int32_t GetRealTimeMs(int64_t &time);
444 private:
445     class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient {
446     public:
TimeSaDeathRecipient()447         explicit TimeSaDeathRecipient(){};
448         ~TimeSaDeathRecipient() = default;
OnRemoteDied(const wptr<IRemoteObject> & object)449         void OnRemoteDied(const wptr<IRemoteObject> &object) override
450         {
451             TimeServiceClient::GetInstance()->ClearProxy();
452             TimeServiceClient::GetInstance()->ConnectService();
453         };
454 
455     private:
456         DISALLOW_COPY_AND_MOVE(TimeSaDeathRecipient);
457     };
458 
459     class TimeServiceListener : public OHOS::SystemAbilityStatusChangeStub {
460         public:
461             TimeServiceListener();
462             ~TimeServiceListener() = default;
463             virtual void OnAddSystemAbility(int32_t saId, const std::string &deviceId) override;
464             virtual void OnRemoveSystemAbility(int32_t asId, const std::string &deviceId) override;
465     };
466 
467     struct RecoverTimerInfo {
468         std::shared_ptr<ITimerInfo> timerInfo;
469         uint8_t state;
470         uint64_t triggerTime;
471     };
472 
473     TimeServiceClient();
474     ~TimeServiceClient();
475     bool SubscribeSA(sptr<ISystemAbilityManager> systemAbilityManager);
476     bool ConnectService();
477     bool GetTimeByClockId(clockid_t clockId, struct timespec &tv);
478     void ClearProxy();
479     sptr<ITimeService> GetProxy();
480     void SetProxy(sptr<ITimeService> proxy);
481 
482     sptr<TimeServiceListener> listener_;
483     static std::mutex instanceLock_;
484     static sptr<TimeServiceClient> instance_;
485     TIME_API std::map<uint64_t, std::shared_ptr<RecoverTimerInfo>> recoverTimerInfoMap_;
486     TIME_API std::mutex recoverTimerInfoLock_;
487     std::mutex proxyLock_;
488     std::mutex deathLock_;
489     sptr<ITimeService> timeServiceProxy_;
490     sptr<TimeSaDeathRecipient> deathRecipient_ {};
491 };
492 } // MiscServices
493 } // OHOS
494 #endif // SERVICES_INCLUDE_TIME_SERVICES_MANAGER_H