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