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