1 /*
2  * Copyright (C) 2021-2023 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 #ifndef OHOS_DHCP_SERVER_PROXY_H
16 #define OHOS_DHCP_SERVER_PROXY_H
17 
18 #include <string>
19 #ifdef OHOS_ARCH_LITE
20 #include "iproxy_client.h"
21 #else
22 #include "iremote_proxy.h"
23 #endif
24 #include "i_dhcp_server.h"
25 #include "i_dhcp_server_callback.h"
26 #include "inner_api/dhcp_server.h"
27 
28 namespace OHOS {
29 namespace DHCP {
30 #ifdef OHOS_ARCH_LITE
31 class DhcpServerProxy : public IDhcpServer {
32 public:
33     static DhcpServerProxy *GetInstance(void);
34     static void ReleaseInstance(void);
35     explicit DhcpServerProxy(void);
36     ErrCode Init(void);
37 #else
38 class DhcpServerProxy : public IRemoteProxy<IDhcpServer> {
39 public:
40     explicit DhcpServerProxy(const sptr<IRemoteObject> &impl);
41 #endif
42     ~DhcpServerProxy();
43 #ifdef OHOS_ARCH_LITE
44     ErrCode RegisterDhcpServerCallBack(const std::string& ifname,
45         const std::shared_ptr<IDhcpServerCallBack> &callback) override;
46 #else
47     ErrCode RegisterDhcpServerCallBack(const std::string& ifname, const sptr<IDhcpServerCallBack> &callback) override;
48 #endif
49     ErrCode StartDhcpServer(const std::string& ifname) override;
50     ErrCode StopDhcpServer(const std::string& ifname) override;
51     ErrCode PutDhcpRange(const std::string& tagName, const DhcpRange& range) override;
52     ErrCode RemoveDhcpRange(const std::string& tagName, const DhcpRange& range) override;
53     ErrCode RemoveAllDhcpRange(const std::string& tagName) override;
54     ErrCode SetDhcpRange(const std::string& ifname, const DhcpRange& range) override;
55     ErrCode SetDhcpName(const std::string& ifname, const std::string& tagName) override;
56     ErrCode GetDhcpClientInfos(const std::string& ifname, std::vector<std::string>& dhcpClientInfo) override;
57     ErrCode UpdateLeasesTime(const std::string& leaseTime) override;
58     bool IsRemoteDied() override;
59 
60 #ifdef OHOS_ARCH_LITE
61     void OnRemoteDied(void);
62 private:
63     static DhcpServerProxy *g_instance;
64     IClientProxy *remote_ = nullptr;
65     SvcIdentity svcIdentity_ = { 0 };
66     bool remoteDied_;
67 #else
68 private:
69     class DhcpServerDeathRecipient : public IRemoteObject::DeathRecipient {
70     public:
DhcpServerDeathRecipient(DhcpServerProxy & client)71         explicit DhcpServerDeathRecipient(DhcpServerProxy &client) : client_(client) {}
72         ~DhcpServerDeathRecipient() override = default;
OnRemoteDied(const wptr<IRemoteObject> & remote)73         void OnRemoteDied(const wptr<IRemoteObject> &remote) override
74         {
75             client_.OnRemoteDied(remote);
76         }
77 
78     private:
79         DhcpServerProxy &client_;
80     };
81     void OnRemoteDied(const wptr<IRemoteObject> &remoteObject);
82     void RemoveDeathRecipient(void);
83 
84     static inline BrokerDelegator<DhcpServerProxy> delegator_;
85     sptr<IRemoteObject> remote_ = nullptr;
86     bool mRemoteDied;
87     std::mutex mutex_;
88     sptr<IRemoteObject::DeathRecipient> deathRecipient_ = nullptr;
89 #endif
90 };
91 }  // namespace DHCP
92 }  // namespace OHOS
93 #endif
94