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_client_def.h" 25 #else 26 #ifndef OHOS_ARCH_LITE 27 #include "dhcp_client_def.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 * @submit sync task to Handler 42 * 43 * @param Callback - Input task 44 * @return bool - true: submit success, false: submit failed 45 */ 46 bool PostSyncTask(const Callback &callback); 47 /** 48 * @submit Async task to Handler 49 * 50 * @param Callback - Input task 51 * @param delayTime - Wait delayTime ms excute task 52 * @return bool - true: submit success, false: submit failed 53 */ 54 bool PostAsyncTask(const Callback &callback, int64_t delayTime = 0); 55 /** 56 * @submit Async task to Handler 57 * 58 * @param Callback - Input task 59 * @param name - Describer of task 60 * @param delayTime - Wait delayTime ms excute task 61 * @return bool - true: submit success, false: submit failed 62 */ 63 bool PostAsyncTask(const Callback &callback, const std::string &name, int64_t delayTime = 0); 64 /** 65 * @Remove Async task 66 * 67 * @param name - Describer of task 68 */ 69 void RemoveAsyncTask(const std::string &name); 70 71 private: 72 class DhcpThreadImpl; 73 std::unique_ptr<DhcpThreadImpl> ptr_{nullptr}; 74 }; 75 76 #ifndef OHOS_ARCH_LITE 77 #ifdef DHCP_FFRT_ENABLE 78 class DhcpTimer { 79 public: 80 static constexpr uint32_t DEFAULT_TIMEROUT = 18000; 81 using TimerCallback = std::function<void()>; 82 static DhcpTimer *GetInstance(void); 83 84 DhcpTimer(); 85 ~DhcpTimer(); 86 87 EnumErrCode Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval = DEFAULT_TIMEROUT, 88 bool once = true); 89 void UnRegister(uint32_t timerId); 90 public: 91 std::unique_ptr<DhcpThread> timer_{nullptr}; 92 uint32_t timerIdInit = 0; 93 }; 94 #else 95 class DhcpTimer { 96 public: 97 static constexpr uint32_t DEFAULT_TIMEROUT = 18000; 98 using TimerCallback = std::function<void()>; 99 static DhcpTimer *GetInstance(void); 100 101 DhcpTimer(); 102 ~DhcpTimer(); 103 104 EnumErrCode Register(const TimerCallback &callback, uint32_t &outTimerId, uint32_t interval = DEFAULT_TIMEROUT, 105 bool once = true); 106 void UnRegister(uint32_t timerId); 107 public: 108 std::unique_ptr<Utils::Timer> timer_{nullptr}; 109 }; 110 #endif 111 #endif 112 113 } // namespace DHCP 114 } // namespace OHOS 115 #endif