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 #include "notify_callback_proxy.h"
16
17 #include "netnative_log_wrapper.h"
18
19 namespace OHOS {
20 namespace NetsysNative {
21 namespace {
WriteInterfaceStateData(MessageParcel & data,const std::string & ifName)22 bool WriteInterfaceStateData(MessageParcel &data, const std::string &ifName)
23 {
24 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
25 NETNATIVE_LOGE("WriteInterfaceToken failed");
26 return false;
27 }
28
29 if (!data.WriteString(ifName)) {
30 return false;
31 }
32 return true;
33 }
34
WriteInterfaceAddressData(MessageParcel & data,const std::string & addr,const std::string & ifName,int flags,int scope)35 bool WriteInterfaceAddressData(MessageParcel &data, const std::string &addr, const std::string &ifName, int flags,
36 int scope)
37 {
38 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
39 NETNATIVE_LOGE("WriteInterfaceToken failed");
40 return false;
41 }
42
43 if (!data.WriteString(addr)) {
44 return false;
45 }
46
47 if (!data.WriteString(ifName)) {
48 return false;
49 }
50
51 if (!data.WriteInt32(flags)) {
52 return false;
53 }
54
55 if (!data.WriteInt32(scope)) {
56 return false;
57 }
58 return true;
59 }
60
WriteLinkStateData(MessageParcel & data,const std::string & ifName,bool up)61 bool WriteLinkStateData(MessageParcel &data, const std::string &ifName, bool up)
62 {
63 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
64 NETNATIVE_LOGE("WriteInterfaceToken failed");
65 return false;
66 }
67
68 if (!data.WriteString(ifName)) {
69 return false;
70 }
71
72 if (!data.WriteBool(up)) {
73 return false;
74 }
75 return true;
76 }
77 }
NotifyCallbackProxy(const sptr<IRemoteObject> & impl)78 NotifyCallbackProxy::NotifyCallbackProxy(const sptr<IRemoteObject> &impl)
79 : IRemoteProxy<INotifyCallback>(impl)
80 {}
81
~NotifyCallbackProxy()82 NotifyCallbackProxy::~NotifyCallbackProxy() {}
83
OnInterfaceAddressUpdated(const std::string & addr,const std::string & ifName,int flags,int scope)84 int32_t NotifyCallbackProxy::OnInterfaceAddressUpdated(const std::string &addr, const std::string &ifName, int flags,
85 int scope)
86 {
87 NETNATIVE_LOGI("Proxy OnInterfaceAddressUpdated");
88 MessageParcel data;
89 if (!WriteInterfaceAddressData(data, addr, ifName, flags, scope)) {
90 NETNATIVE_LOGE("WriteInterfaceAddressData failed");
91 return ERR_NONE;
92 }
93
94 sptr<IRemoteObject> remote = Remote();
95 if (remote == nullptr) {
96 NETNATIVE_LOGE("Remote is null");
97 return ERR_NULL_OBJECT;
98 }
99
100 MessageParcel reply;
101 MessageOption option;
102 option.SetFlags(MessageOption::TF_ASYNC);
103 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDRESS_UPDATED), data,
104 reply, option);
105 if (ret != ERR_NONE) {
106 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
107 }
108 return ret;
109 }
110
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)111 int32_t NotifyCallbackProxy::OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
112 int scope)
113 {
114 NETNATIVE_LOGI("Proxy OnInterfaceAddressRemoved");
115 MessageParcel data;
116 if (!WriteInterfaceAddressData(data, addr, ifName, flags, scope)) {
117 NETNATIVE_LOGE("WriteInterfaceAddressData failed");
118 return ERR_NONE;
119 }
120
121 sptr<IRemoteObject> remote = Remote();
122 if (remote == nullptr) {
123 NETNATIVE_LOGE("Remote is null");
124 return ERR_NULL_OBJECT;
125 }
126
127 MessageParcel reply;
128 MessageOption option;
129 option.SetFlags(MessageOption::TF_ASYNC);
130 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDRESS_REMOVED), data,
131 reply, option);
132 if (ret != ERR_NONE) {
133 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
134 }
135 return ret;
136 }
137
OnInterfaceAdded(const std::string & ifName)138 int32_t NotifyCallbackProxy::OnInterfaceAdded(const std::string &ifName)
139 {
140 NETNATIVE_LOGI("Proxy OnInterfaceAdded");
141 MessageParcel data;
142 if (!WriteInterfaceStateData(data, ifName)) {
143 return false;
144 }
145
146 sptr<IRemoteObject> remote = Remote();
147 if (remote == nullptr) {
148 NETNATIVE_LOGE("Remote is null");
149 return ERR_NULL_OBJECT;
150 }
151
152 MessageParcel reply;
153 MessageOption option;
154 option.SetFlags(MessageOption::TF_ASYNC);
155 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_ADDED), data,
156 reply, option);
157 if (ret != ERR_NONE) {
158 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
159 }
160 return ret;
161 }
162
OnInterfaceRemoved(const std::string & ifName)163 int32_t NotifyCallbackProxy::OnInterfaceRemoved(const std::string &ifName)
164 {
165 NETNATIVE_LOGI("Proxy OnInterfaceRemoved");
166 MessageParcel data;
167 if (!WriteInterfaceStateData(data, ifName)) {
168 return false;
169 }
170
171 sptr<IRemoteObject> remote = Remote();
172 if (remote == nullptr) {
173 NETNATIVE_LOGE("Remote is null");
174 return ERR_NULL_OBJECT;
175 }
176
177 MessageParcel reply;
178 MessageOption option;
179 option.SetFlags(MessageOption::TF_ASYNC);
180 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_REMOVED), data,
181 reply, option);
182 if (ret != ERR_NONE) {
183 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
184 }
185 return ret;
186 }
187
OnInterfaceChanged(const std::string & ifName,bool up)188 int32_t NotifyCallbackProxy::OnInterfaceChanged(const std::string &ifName, bool up)
189 {
190 NETNATIVE_LOGI("Proxy OnInterfaceChanged");
191 MessageParcel data;
192 if (!WriteLinkStateData(data, ifName, up)) {
193 return ERR_NONE;
194 }
195
196 sptr<IRemoteObject> remote = Remote();
197 if (remote == nullptr) {
198 NETNATIVE_LOGE("Remote is null");
199 return ERR_NULL_OBJECT;
200 }
201
202 MessageParcel reply;
203 MessageOption option;
204 option.SetFlags(MessageOption::TF_ASYNC);
205 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_CHANGED), data,
206 reply, option);
207 if (ret != ERR_NONE) {
208 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
209 }
210 return ret;
211 }
212
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)213 int32_t NotifyCallbackProxy::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
214 {
215 NETNATIVE_LOG_D("Proxy OnInterfaceLinkStateChanged");
216 MessageParcel data;
217 if (!WriteLinkStateData(data, ifName, up)) {
218 return ERR_NONE;
219 }
220
221 sptr<IRemoteObject> remote = Remote();
222 if (remote == nullptr) {
223 NETNATIVE_LOGE("Remote is null");
224 return ERR_NULL_OBJECT;
225 }
226
227 MessageParcel reply;
228 MessageOption option;
229 option.SetFlags(MessageOption::TF_ASYNC);
230 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_INTERFACE_LINK_STATE_CHANGED),
231 data, reply, option);
232 if (ret != ERR_NONE) {
233 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
234 }
235 return ret;
236 }
237
OnRouteChanged(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)238 int32_t NotifyCallbackProxy::OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
239 const std::string &ifName)
240 {
241 NETNATIVE_LOGI("Proxy OnRouteChanged");
242 MessageParcel data;
243 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
244 NETNATIVE_LOGE("WriteInterfaceToken failed");
245 return false;
246 }
247
248 if (!data.WriteBool(updated)) {
249 return false;
250 }
251
252 if (!data.WriteString(route)) {
253 return false;
254 }
255
256 if (!data.WriteString(gateway)) {
257 return false;
258 }
259
260 if (!data.WriteString(ifName)) {
261 return false;
262 }
263
264 sptr<IRemoteObject> remote = Remote();
265 if (remote == nullptr) {
266 NETNATIVE_LOGE("Remote is null");
267 return ERR_NULL_OBJECT;
268 }
269
270 MessageParcel reply;
271 MessageOption option;
272 option.SetFlags(MessageOption::TF_ASYNC);
273 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_ROUTE_CHANGED),
274 data, reply, option);
275 if (ret != ERR_NONE) {
276 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
277 }
278 return ret;
279 }
280
OnDhcpSuccess(sptr<DhcpResultParcel> & dhcpResult)281 int32_t NotifyCallbackProxy::OnDhcpSuccess(sptr<DhcpResultParcel> &dhcpResult)
282 {
283 NETNATIVE_LOGI("Proxy OnDhcpSuccess");
284 MessageParcel data;
285 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
286 NETNATIVE_LOGE("WriteInterfaceToken failed");
287 return false;
288 }
289 dhcpResult->Marshalling(data);
290 sptr<IRemoteObject> remote = Remote();
291 if (remote == nullptr) {
292 NETNATIVE_LOGE("Remote is null");
293 return ERR_NULL_OBJECT;
294 }
295
296 MessageParcel reply;
297 MessageOption option;
298 option.SetFlags(MessageOption::TF_ASYNC);
299 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_DHCP_SUCCESS),
300 data, reply, option);
301 if (ret != ERR_NONE) {
302 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
303 }
304 return ret;
305 }
306
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)307 int32_t NotifyCallbackProxy::OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
308 {
309 NETNATIVE_LOGI("Proxy OnBandwidthReachedLimit");
310 MessageParcel data;
311 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
312 NETNATIVE_LOGE("WriteInterfaceToken failed");
313 return false;
314 }
315
316 if (!data.WriteString(limitName)) {
317 return false;
318 }
319 if (!data.WriteString(iface)) {
320 return false;
321 }
322
323 sptr<IRemoteObject> remote = Remote();
324 if (remote == nullptr) {
325 NETNATIVE_LOGE("Remote is null");
326 return ERR_NULL_OBJECT;
327 }
328
329 MessageParcel reply;
330 MessageOption option;
331 option.SetFlags(MessageOption::TF_ASYNC);
332 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NotifyInterfaceCode::ON_BANDWIDTH_REACHED_LIMIT),
333 data, reply, option);
334 if (ret != ERR_NONE) {
335 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
336 }
337 return reply.ReadInt32();
338 }
339 } // namespace NetsysNative
340 } // namespace OHOS
341