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