1 /*
2  * Copyright (c) 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 
16 #include "net_interface_callback_proxy.h"
17 
18 #include "net_conn_constants.h"
19 #include "net_mgr_log_wrapper.h"
20 
21 namespace OHOS {
22 namespace NetManagerStandard {
NetInterfaceStateCallbackProxy(const sptr<IRemoteObject> & impl)23 NetInterfaceStateCallbackProxy::NetInterfaceStateCallbackProxy(const sptr<IRemoteObject> &impl)
24     : IRemoteProxy<INetInterfaceStateCallback>(impl)
25 {
26 }
27 
WriteInterfaceToken(MessageParcel & data)28 bool NetInterfaceStateCallbackProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30     if (!data.WriteInterfaceToken(NetInterfaceStateCallbackProxy::GetDescriptor())) {
31         NETMGR_LOG_E("WriteInterfaceToken failed");
32         return false;
33     }
34     return true;
35 }
36 
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)37 int32_t NetInterfaceStateCallbackProxy::OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName,
38                                                                   int32_t flags, int32_t scope)
39 {
40     MessageParcel dataParcel;
41     if (!WriteInterfaceToken(dataParcel)) {
42         NETMGR_LOG_E("WriteInterfaceToken failed");
43         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
44     }
45 
46     if (!dataParcel.WriteString(addr)) {
47         return NETMANAGER_ERR_WRITE_DATA_FAIL;
48     }
49 
50     if (!dataParcel.WriteString(ifName)) {
51         return NETMANAGER_ERR_WRITE_DATA_FAIL;
52     }
53 
54     if (!dataParcel.WriteInt32(flags)) {
55         return NETMANAGER_ERR_WRITE_DATA_FAIL;
56     }
57 
58     if (!dataParcel.WriteInt32(scope)) {
59         return NETMANAGER_ERR_WRITE_DATA_FAIL;
60     }
61 
62     sptr<IRemoteObject> remote = Remote();
63     if (remote == nullptr) {
64         NETMGR_LOG_E("Remote is null");
65         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
66     }
67     MessageParcel replyParcel;
68     MessageOption option;
69     int32_t retCode = remote->SendRequest(
70         static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_ADDR_UPDATED),
71         dataParcel, replyParcel, option);
72     if (retCode != ERR_NONE) {
73         NETMGR_LOG_E("proxy SendRequest failed, retCode: [%{public}d]", retCode);
74         return retCode;
75     }
76     return replyParcel.ReadInt32();
77 }
78 
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int32_t flags,int32_t scope)79 int32_t NetInterfaceStateCallbackProxy::OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName,
80                                                                   int32_t flags, int32_t scope)
81 {
82     MessageParcel dataParcel;
83     if (!WriteInterfaceToken(dataParcel)) {
84         NETMGR_LOG_E("WriteInterfaceToken failed");
85         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
86     }
87 
88     if (!dataParcel.WriteString(addr)) {
89         return NETMANAGER_ERR_WRITE_DATA_FAIL;
90     }
91 
92     if (!dataParcel.WriteString(ifName)) {
93         return NETMANAGER_ERR_WRITE_DATA_FAIL;
94     }
95 
96     if (!dataParcel.WriteInt32(flags)) {
97         return NETMANAGER_ERR_WRITE_DATA_FAIL;
98     }
99 
100     if (!dataParcel.WriteInt32(scope)) {
101         return NETMANAGER_ERR_WRITE_DATA_FAIL;
102     }
103 
104     sptr<IRemoteObject> remote = Remote();
105     if (remote == nullptr) {
106         NETMGR_LOG_E("Remote is null");
107         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
108     }
109     MessageParcel replyParcel;
110     MessageOption option;
111     int32_t retCode = remote->SendRequest(
112         static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_ADDR_REMOVED),
113         dataParcel, replyParcel, option);
114     if (retCode != ERR_NONE) {
115         NETMGR_LOG_E("proxy SendRequest failed, retCode: [%{public}d]", retCode);
116         return retCode;
117     }
118     return replyParcel.ReadInt32();
119 }
120 
OnInterfaceAdded(const std::string & ifName)121 int32_t NetInterfaceStateCallbackProxy::OnInterfaceAdded(const std::string &ifName)
122 {
123     MessageParcel dataParcel;
124     if (!WriteInterfaceToken(dataParcel)) {
125         NETMGR_LOG_E("WriteInterfaceToken failed");
126         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
127     }
128 
129     if (!dataParcel.WriteString(ifName)) {
130         return NETMANAGER_ERR_WRITE_DATA_FAIL;
131     }
132 
133     sptr<IRemoteObject> remote = Remote();
134     if (remote == nullptr) {
135         NETMGR_LOG_E("Remote is null");
136         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
137     }
138     MessageParcel replyParcel;
139     MessageOption option;
140     int32_t retCode = remote->SendRequest(static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_ADDED),
141                                           dataParcel, replyParcel, option);
142     if (retCode != ERR_NONE) {
143         NETMGR_LOG_E("proxy SendRequest failed, retCode: [%{public}d]", retCode);
144         return retCode;
145     }
146     return replyParcel.ReadInt32();
147 }
148 
OnInterfaceRemoved(const std::string & ifName)149 int32_t NetInterfaceStateCallbackProxy::OnInterfaceRemoved(const std::string &ifName)
150 {
151     MessageParcel dataParcel;
152     if (!WriteInterfaceToken(dataParcel)) {
153         NETMGR_LOG_E("WriteInterfaceToken failed");
154         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
155     }
156 
157     if (!dataParcel.WriteString(ifName)) {
158         return NETMANAGER_ERR_WRITE_DATA_FAIL;
159     }
160 
161     sptr<IRemoteObject> remote = Remote();
162     if (remote == nullptr) {
163         NETMGR_LOG_E("Remote is null");
164         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
165     }
166     MessageParcel replyParcel;
167     MessageOption option;
168     int32_t retCode = remote->SendRequest(static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_REMOVED),
169                                           dataParcel, replyParcel, option);
170     if (retCode != ERR_NONE) {
171         NETMGR_LOG_E("proxy SendRequest failed, retCode: [%{public}d]", retCode);
172         return retCode;
173     }
174     return replyParcel.ReadInt32();
175 }
176 
OnInterfaceChanged(const std::string & ifName,bool up)177 int32_t NetInterfaceStateCallbackProxy::OnInterfaceChanged(const std::string &ifName, bool up)
178 {
179     MessageParcel dataParcel;
180     if (!WriteInterfaceToken(dataParcel)) {
181         NETMGR_LOG_E("WriteInterfaceToken failed");
182         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
183     }
184 
185     if (!dataParcel.WriteString(ifName)) {
186         return NETMANAGER_ERR_WRITE_DATA_FAIL;
187     }
188 
189     if (!dataParcel.WriteBool(up)) {
190         return NETMANAGER_ERR_WRITE_DATA_FAIL;
191     }
192 
193     sptr<IRemoteObject> remote = Remote();
194     if (remote == nullptr) {
195         NETMGR_LOG_E("Remote is null");
196         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
197     }
198     MessageParcel replyParcel;
199     MessageOption option;
200     int32_t retCode = remote->SendRequest(static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_CHANGED),
201                                           dataParcel, replyParcel, option);
202     if (retCode != ERR_NONE) {
203         NETMGR_LOG_E("proxy SendRequest failed, retCode: [%{public}d]", retCode);
204         return retCode;
205     }
206     return replyParcel.ReadInt32();
207 }
208 
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)209 int32_t NetInterfaceStateCallbackProxy::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
210 {
211     MessageParcel dataParcel;
212     if (!WriteInterfaceToken(dataParcel)) {
213         NETMGR_LOG_E("WriteInterfaceToken failed");
214         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
215     }
216 
217     if (!dataParcel.WriteString(ifName)) {
218         return NETMANAGER_ERR_WRITE_DATA_FAIL;
219     }
220 
221     if (!dataParcel.WriteBool(up)) {
222         return NETMANAGER_ERR_WRITE_DATA_FAIL;
223     }
224 
225     sptr<IRemoteObject> remote = Remote();
226     if (remote == nullptr) {
227         NETMGR_LOG_E("Remote is null");
228         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
229     }
230     MessageParcel replyParcel;
231     MessageOption option;
232     int32_t retCode = remote->SendRequest(
233         static_cast<uint32_t>(InterfaceCallbackInterfaceCode::CMD_ON_IFACE_LINK_STATE_CHANGED),
234         dataParcel, replyParcel, option);
235     if (retCode != ERR_NONE) {
236         NETMGR_LOG_E("proxy SendRequest failed, retCode: [%{public}d]", retCode);
237         return retCode;
238     }
239     return replyParcel.ReadInt32();
240 }
241 } // namespace NetManagerStandard
242 } // namespace OHOS
243