1 /* 2 * Copyright (C) 2024 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 DHCP_THREAD_H 17 #define DHCP_THREAD_H 18 19 #include <string> 20 #include <memory> 21 #ifdef OHOS_EUPDATER 22 #include "common_timer_errors.h" 23 #include "timer.h" 24 #include "dhcp_define.h" 25 #else 26 #ifndef OHOS_ARCH_LITE 27 #include "dhcp_define.h" 28 #include "common_timer_errors.h" 29 #include "timer.h" 30 #endif 31 #endif // OHOS_EUPDATER 32 namespace OHOS { 33 namespace DHCP { 34 class DhcpThread { 35 public: 36 using Callback = std::function<void()>; 37 38 explicit DhcpThread(const std::string &threadName); 39 ~DhcpThread(); 40 41 /** 42 * @submit sync task to Handler 43 * 44 * @param Callback - Input task 45 * @return bool - true: submit success, false: submit failed 46 */ 47 bool PostSyncTask(const Callback &callback); 48 49 /** 50 * @submit Async task to Handler 51 * 52 * @param Callback - Input task 53 * @param delayTime - Wait delayTime ms excute task 54 * @return bool - true: submit success, false: submit failed 55 */ 56 bool PostAsyncTask(const Callback &callback, int64_t delayTime = 0); 57 58 /** 59 * @submit Async task to Handler 60 * 61 * @param Callback - Input task 62 * @param name - Describer of task 63 * @param delayTime - Wait delayTime ms excute task 64 * @return bool - true: submit success, false: submit failed 65 */ 66 bool PostAsyncTask(const Callback &callback, const std::string &name, 67 int64_t delayTime = 0, bool isHighPriority = false); 68 69 /** 70 * @submit sync timeout task to Handler 71 * 72 * @param Callback - Input task 73 * @param waitTime - Wait time(ms) excute task 74 * @param delayTime - Wait delayTime ms excute task 75 * @return bool - true: excute task success, false: execute task timeout 76 */ 77 bool PostSyncTimeOutTask(const std::function<int32_t()> &callback, int32_t waitTime); 78 79 /** 80 * @Remove Async task 81 * 82 * @param name - Describer of task 83 */ 84 void RemoveAsyncTask(const std::string &name); 85 /** 86 * @Check if Has Async Task 87 * 88 * @param name 89 * @param hasTask 90 * @return int - 0: supported, -1: unsupported 91 */ 92 int HasAsyncTask(const std::string &name, bool &hasTask); 93 94 private: 95 class DhcpThreadImpl; 96 std::unique_ptr<DhcpThreadImpl> ptr_; 97 }; 98 99 #ifndef OHOS_ARCH_LITE 100 #ifdef DHCP_FFRT_ENABLE 101 class DhcpTimer { 102 public: 103 static constexpr uint32_t DEFAULT_TIMEROUT = 18000; 104 using TimerCallback = std::function<void()>; 105 static DhcpTimer *GetInstance(void); 106 107 DhcpTimer(); 108 ~DhcpTimer(); 109 110 EnumErrCode Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval = DEFAULT_TIMEROUT, 111 bool once = true); 112 void UnRegister(uint32_t timerId); 113 public: 114 std::unique_ptr<DhcpThread> timer_{nullptr}; 115 uint32_t timerIdInit = 0; 116 }; 117 #else 118 class DhcpTimer { 119 public: 120 static constexpr uint32_t DEFAULT_TIMEROUT = 18000; 121 using TimerCallback = std::function<void()>; 122 static DhcpTimer *GetInstance(void); 123 124 DhcpTimer(); 125 ~DhcpTimer(); 126 127 EnumErrCode Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval = DEFAULT_TIMEROUT, 128 bool once = true); 129 void UnRegister(uint32_t timerId); 130 public: 131 std::unique_ptr<Utils::Timer> timer_{nullptr}; 132 }; 133 #endif 134 #endif 135 136 } // namespace DHCP 137 } // namespace OHOS 138 #endif