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 22 #include "itimer_info.h" 23 #include "refbase.h" 24 #include "time_service_interface.h" 25 #include "visibility.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 DestroyTimer 342 * 343 * Destroy a timer. 344 * 345 * @param indicate timerId. 346 * @return error code. 347 */ 348 TIME_API int32_t DestroyTimerV9(uint64_t timerId); 349 350 /** 351 * @brief ProxyTimer 352 * 353 * Wake up all timers for provided uid by proxy. 354 * 355 * @param uid the uid. 356 * @param true if set proxy, false if remove proxy. 357 * @param true if need retrigger, false if not. 358 * @return true on success, false on failure. 359 */ 360 TIME_API bool ProxyTimer(int32_t uid, bool isProxy, bool needRetrigger); 361 362 /** 363 * @brief ResetAllProxy 364 * 365 * Wake up all timers by proxy. 366 * 367 * @return bool true on success, false on failure. 368 */ 369 TIME_API bool ResetAllProxy(); 370 private: 371 class TimeSaDeathRecipient : public IRemoteObject::DeathRecipient { 372 public: TimeSaDeathRecipient()373 explicit TimeSaDeathRecipient(){}; 374 ~TimeSaDeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & object)375 void OnRemoteDied(const wptr<IRemoteObject> &object) override 376 { 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