1 /*
2  * Copyright (C) 2022 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 ETHERNET_DHCP_CONTROLLER_H
17 #define ETHERNET_DHCP_CONTROLLER_H
18 
19 #include <cstdint>
20 #include <iosfwd>
21 #include <memory>
22 
23 #include "ethernet_dhcp_callback.h"
24 #include "refbase.h"
25 #include "dhcp_c_api.h"
26 
27 namespace OHOS {
28 namespace NetManagerStandard {
29 class EthernetDhcpController {
30 public:
31     class EthernetDhcpControllerResultNotify {
32     public:
EthernetDhcpControllerResultNotify()33         EthernetDhcpControllerResultNotify()
34         {
35         }
36         ~EthernetDhcpControllerResultNotify()  = default;
37         static void OnSuccess(int status, const char *ifname, DhcpResult *result);
38         static void OnFailed(int status, const char *ifname, const char *reason);
39         static void SetEthernetDhcpController(EthernetDhcpController *ethDhcpController);
40     private:
41         static EthernetDhcpController *ethDhcpController_;
42     };
43 public:
EthernetDhcpController()44     EthernetDhcpController() : dhcpResultNotify_(std::make_unique<EthernetDhcpControllerResultNotify>())
45     {
46         clientEvent.OnIpSuccessChanged = EthernetDhcpControllerResultNotify::OnSuccess;
47         clientEvent.OnIpFailChanged = EthernetDhcpControllerResultNotify::OnFailed;
48     }
49     ~EthernetDhcpController() = default;
50     void RegisterDhcpCallback(sptr<EthernetDhcpCallback> callback);
51     void StartClient(const std::string &iface, bool bIpv6);
52     void StopClient(const std::string &iface, bool bIpv6);
53 private:
54     void OnDhcpSuccess(const std::string &iface, DhcpResult *result);
55     void OnDhcpFailed(int status, const std::string &ifname, const char *reason);
56 private:
57     ClientCallBack clientEvent;
58     std::unique_ptr<EthernetDhcpControllerResultNotify> dhcpResultNotify_ = nullptr;
59     sptr<EthernetDhcpCallback> cbObject_ = nullptr;
60 };
61 } // namespace NetManagerStandard
62 } // namespace OHOS
63 #endif // ETHERNET_DHCP_CONTROLLER_H