• 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      * @descrition
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      * SetTimeZone
54      * @descrition
55      * @param timeZoneId const std::string time zone. example: "Beijing, China".
56      * @return bool true on success, false on failure.
57      */
58     bool SetTimeZone(const std::string timeZoneId);
59 
60     /**
61      * GetTimeZone
62      * @descpriton
63      * @return std::string, time zone example: "Beijing, China", if result length == 0 on failed.
64      */
65     std::string GetTimeZone();
66     /**
67      * GetWallTimeMs
68      * @descpriton get the wall time(the UTC time from 1970 0H:0M:0S) in milliseconds
69      * @return int64_t, milliseconds in wall time, ret < 0 on failed.
70      */
71     int64_t GetWallTimeMs();
72 
73     /**
74      * GetWallTimeNs
75      * @descpriton get the wall time(the UTC time from 1970 0H:0M:0S) in nanoseconds
76      * @return int64_t, nanoseconds in wall time, ret < 0 on failed.
77      */
78     int64_t GetWallTimeNs();
79 
80     /**
81      * GetBootTimeMs
82      * @descpriton get the time since boot(include time spent in sleep) in milliseconds.
83      * @return int64_t, milliseconds in boot time, ret < 0 on failed.
84      */
85     int64_t GetBootTimeMs();
86 
87     /**
88      * GetBootTimeNs
89      * @descpriton // get the time since boot(include time spent in sleep) in nanoseconds.
90      * @return int64_t, nanoseconds in boot time, ret < 0 on failed.
91      */
92     int64_t GetBootTimeNs();
93 
94     /**
95      * GetMonotonicTimeMs
96      * @descpriton get the time since boot(exclude time spent in sleep) in milliseconds.
97      * @return int64_t, milliseconds in Monotonic time, ret < 0 on failed.
98      */
99     int64_t GetMonotonicTimeMs();
100 
101     /**
102      * GetMonotonicTimeNs
103      * @descpriton get the time since boot(exclude time spent in sleep) in nanoseconds.
104      * @return int64_t, nanoseconds in Monotonic time, ret < 0 on failed.
105      */
106     int64_t GetMonotonicTimeNs();
107 
108     /**
109      * GetThreadTimeMs
110      * @descpriton get the Thread-specific CPU-time in milliseconds.
111      * @return int64_t, milliseconds in Thread-specific CPU-time, ret < 0 on failed.
112      */
113     int64_t GetThreadTimeMs();
114 
115     /**
116      * GetThreadTimeNs
117      * @descpriton get the Thread-specific CPU-time in nanoseconds.
118      * @return int64_t, nanoseconds in Thread-specific CPU-time, ret < 0 on failed.
119      */
120     int64_t GetThreadTimeNs();
121 
122     /**
123      * CreateTimer
124      * @param TimerInfo  timer info
125      * @return uint64_t > 0 on success, == 0 failure.
126      */
127     uint64_t CreateTimer(std::shared_ptr<ITimerInfo> TimerInfo);
128 
129      /**
130      * StartTimer
131      * @param timerId indicate timerId
132      * @param treggerTime  trigger times
133      * @return bool true on success, false on failure.
134      */
135     bool StartTimer(uint64_t timerId, uint64_t triggerTime);
136 
137      /**
138      * StopTimer
139      * @param timerId indicate timerId
140      * @return bool true on success, false on failure.
141      */
142     bool StopTimer(uint64_t timerId);
143 
144      /**
145      * DestroyTimer
146      * @param timerId indicate timerId
147      * @return bool true on success, false on failure.
148      */
149     bool DestroyTimer(uint64_t timerId);
150 
151      /**
152      * NetworkTimeStatusOff
153      * @return void.
154      */
155     void NetworkTimeStatusOff();
156 
157      /**
158      * NetworkTimeStatusOn
159      * @return void.
160      */
161     void NetworkTimeStatusOn();
162 
163     void OnRemoteSaDied(const wptr<IRemoteObject> &object);
164 
165 private:
166     TimeServiceClient();
167     ~TimeServiceClient();
168     static sptr<ITimeService> ConnectService();
169 
170     static std::mutex instanceLock_;
171     static sptr<TimeServiceClient> instance_;
172     static sptr<ITimeService> timeServiceProxy_;
173     static sptr<TimeSaDeathRecipient> deathRecipient_;
174 };
175 } // MiscServices
176 } // OHOS
177 #endif // SERVICES_INCLUDE_TIME_SERVICES_MANAGER_H