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 "time_common.h" 24 #include "timer_call_back.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(const 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(const 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(const 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 DestroyTimer 341 * 342 * Destroy a timer. 343 * 344 * @param indicate timerId. 345 * @return error code. 346 */ 347 TIME_API int32_t DestroyTimerV9(uint64_t timerId); 348 349 /** 350 * @brief ProxyTimer 351 * 352 * Wake up all timers for provided uid by proxy. 353 * 354 * @param uid the uid. 355 * @param true if set proxy, false if remove proxy. 356 * @param true if need retrigger, false if not. 357 * @return true on success, false on failure. 358 */ 359 TIME_API bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger); 360 361 /** 362 * @brief ResetAllProxy 363 * 364 * Wake up all timers by proxy. 365 * 366 * @return bool true on success, false on failure. 367 */ 368 TIME_API bool ResetAllProxy(); 369 private: 370 class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient { 371 public: TimeSaDeathRecipient()372 explicit TimeSaDeathRecipient(){}; 373 ~TimeSaDeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & object)374 void OnRemoteDied(const wptr<IRemoteObject> &object) override 375 { 376 TIME_HILOGE(TIME_MODULE_CLIENT, "TimeSaDeathRecipient on remote systemAbility died."); 377 TimeServiceClient::GetInstance()->ClearProxy(); 378 TimeServiceClient::GetInstance()->ConnectService(); 379 }; 380 381 private: 382 DISALLOW_COPY_AND_MOVE(TimeSaDeathRecipient); 383 }; 384 385 TimeServiceClient(); 386 ~TimeServiceClient(); 387 bool ConnectService(); 388 void ClearProxy(); 389 sptr<ITimeService> GetProxy(); 390 void SetProxy(sptr<ITimeService> proxy); 391 392 static std::mutex instanceLock_; 393 static sptr<TimeServiceClient> instance_; 394 std::mutex proxyLock_; 395 std::mutex deathLock_; 396 sptr<ITimeService> timeServiceProxy_; 397 sptr<TimeSaDeathRecipient> deathRecipient_ {}; 398 }; 399 } // MiscServices 400 } // OHOS 401 #endif // SERVICES_INCLUDE_TIME_SERVICES_MANAGER_H