1 /*
2 * Copyright (c) 2021 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 int32_t ret = remote->SendRequest(ON_INTERFACE_ADDRESS_UPDATED, data, reply, option);
103 if (ret != ERR_NONE) {
104 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
105 }
106 return ret;
107 }
108
OnInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)109 int32_t NotifyCallbackProxy::OnInterfaceAddressRemoved(const std::string &addr, const std::string &ifName, int flags,
110 int scope)
111 {
112 NETNATIVE_LOGI("Proxy OnInterfaceAddressRemoved");
113 MessageParcel data;
114 if (!WriteInterfaceAddressData(data, addr, ifName, flags, scope)) {
115 NETNATIVE_LOGE("WriteInterfaceAddressData failed");
116 return ERR_NONE;
117 }
118
119 sptr<IRemoteObject> remote = Remote();
120 if (remote == nullptr) {
121 NETNATIVE_LOGE("Remote is null");
122 return ERR_NULL_OBJECT;
123 }
124
125 MessageParcel reply;
126 MessageOption option;
127 int32_t ret = remote->SendRequest(ON_INTERFACE_ADDRESS_REMOVED, data, reply, option);
128 if (ret != ERR_NONE) {
129 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
130 }
131 return ret;
132 }
133
OnInterfaceAdded(const std::string & ifName)134 int32_t NotifyCallbackProxy::OnInterfaceAdded(const std::string &ifName)
135 {
136 NETNATIVE_LOGI("Proxy OnInterfaceAdded");
137 MessageParcel data;
138 if (!WriteInterfaceStateData(data, ifName)) {
139 return false;
140 }
141
142 sptr<IRemoteObject> remote = Remote();
143 if (remote == nullptr) {
144 NETNATIVE_LOGE("Remote is null");
145 return ERR_NULL_OBJECT;
146 }
147
148 MessageParcel reply;
149 MessageOption option;
150 int32_t ret = remote->SendRequest(ON_INTERFACE_ADDED, data, reply, option);
151 if (ret != ERR_NONE) {
152 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
153 }
154 return ret;
155 }
156
OnInterfaceRemoved(const std::string & ifName)157 int32_t NotifyCallbackProxy::OnInterfaceRemoved(const std::string &ifName)
158 {
159 NETNATIVE_LOGI("Proxy OnInterfaceRemoved");
160 MessageParcel data;
161 if (!WriteInterfaceStateData(data, ifName)) {
162 return false;
163 }
164
165 sptr<IRemoteObject> remote = Remote();
166 if (remote == nullptr) {
167 NETNATIVE_LOGE("Remote is null");
168 return ERR_NULL_OBJECT;
169 }
170
171 MessageParcel reply;
172 MessageOption option;
173 int32_t ret = remote->SendRequest(ON_INTERFACE_REMOVED, data, reply, option);
174 if (ret != ERR_NONE) {
175 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
176 }
177 return ret;
178 }
179
OnInterfaceChanged(const std::string & ifName,bool up)180 int32_t NotifyCallbackProxy::OnInterfaceChanged(const std::string &ifName, bool up)
181 {
182 NETNATIVE_LOGI("Proxy OnInterfaceChanged");
183 MessageParcel data;
184 if (!WriteLinkStateData(data, ifName, up)) {
185 return ERR_NONE;
186 }
187
188 sptr<IRemoteObject> remote = Remote();
189 if (remote == nullptr) {
190 NETNATIVE_LOGE("Remote is null");
191 return ERR_NULL_OBJECT;
192 }
193
194 MessageParcel reply;
195 MessageOption option;
196 int32_t ret = remote->SendRequest(ON_INTERFACE_CHANGED, data, reply, option);
197 if (ret != ERR_NONE) {
198 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
199 }
200 return ret;
201 }
202
OnInterfaceLinkStateChanged(const std::string & ifName,bool up)203 int32_t NotifyCallbackProxy::OnInterfaceLinkStateChanged(const std::string &ifName, bool up)
204 {
205 NETNATIVE_LOGI("Proxy OnInterfaceLinkStateChanged");
206 MessageParcel data;
207 if (!WriteLinkStateData(data, ifName, up)) {
208 return ERR_NONE;
209 }
210
211 sptr<IRemoteObject> remote = Remote();
212 if (remote == nullptr) {
213 NETNATIVE_LOGE("Remote is null");
214 return ERR_NULL_OBJECT;
215 }
216
217 MessageParcel reply;
218 MessageOption option;
219 int32_t ret = remote->SendRequest(ON_INTERFACE_LINK_STATE_CHANGED, data, reply, option);
220 if (ret != ERR_NONE) {
221 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
222 }
223 return ret;
224 }
225
OnRouteChanged(bool updated,const std::string & route,const std::string & gateway,const std::string & ifName)226 int32_t NotifyCallbackProxy::OnRouteChanged(bool updated, const std::string &route, const std::string &gateway,
227 const std::string &ifName)
228 {
229 NETNATIVE_LOGI("Proxy OnRouteChanged");
230 MessageParcel data;
231 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
232 NETNATIVE_LOGE("WriteInterfaceToken failed");
233 return false;
234 }
235
236 if (!data.WriteBool(updated)) {
237 return false;
238 }
239
240 if (!data.WriteString(route)) {
241 return false;
242 }
243
244 if (!data.WriteString(gateway)) {
245 return false;
246 }
247
248 if (!data.WriteString(ifName)) {
249 return false;
250 }
251
252 sptr<IRemoteObject> remote = Remote();
253 if (remote == nullptr) {
254 NETNATIVE_LOGE("Remote is null");
255 return ERR_NULL_OBJECT;
256 }
257
258 MessageParcel reply;
259 MessageOption option;
260 int32_t ret = remote->SendRequest(ON_ROUTE_CHANGED, data, reply, option);
261 if (ret != ERR_NONE) {
262 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
263 }
264 return ret;
265 }
266
OnDhcpSuccess(sptr<DhcpResultParcel> & dhcpResult)267 int32_t NotifyCallbackProxy::OnDhcpSuccess(sptr<DhcpResultParcel> &dhcpResult)
268 {
269 NETNATIVE_LOGI("Proxy OnDhcpSuccess");
270 MessageParcel data;
271 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
272 NETNATIVE_LOGE("WriteInterfaceToken failed");
273 return false;
274 }
275 dhcpResult->Marshalling(data);
276 sptr<IRemoteObject> remote = Remote();
277 if (remote == nullptr) {
278 NETNATIVE_LOGE("Remote is null");
279 return ERR_NULL_OBJECT;
280 }
281
282 MessageParcel reply;
283 MessageOption option;
284 int32_t ret = remote->SendRequest(ON_DHCP_SUCCESS, data, reply, option);
285 if (ret != ERR_NONE) {
286 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
287 }
288 return ret;
289 }
290
OnBandwidthReachedLimit(const std::string & limitName,const std::string & iface)291 int32_t NotifyCallbackProxy::OnBandwidthReachedLimit(const std::string &limitName, const std::string &iface)
292 {
293 NETNATIVE_LOGI("Proxy OnBandwidthReachedLimit");
294 MessageParcel data;
295 if (!data.WriteInterfaceToken(NotifyCallbackProxy::GetDescriptor())) {
296 NETNATIVE_LOGE("WriteInterfaceToken failed");
297 return false;
298 }
299
300 if (!data.WriteString(limitName)) {
301 return false;
302 }
303 if (!data.WriteString(iface)) {
304 return false;
305 }
306
307 sptr<IRemoteObject> remote = Remote();
308 if (remote == nullptr) {
309 NETNATIVE_LOGE("Remote is null");
310 return ERR_NULL_OBJECT;
311 }
312
313 MessageParcel reply;
314 MessageOption option;
315 int32_t ret = remote->SendRequest(ON_BANDWIDTH_REACHED_LIMIT, data, reply, option);
316 if (ret != ERR_NONE) {
317 NETNATIVE_LOGE("Proxy SendRequest failed, ret code:[%{public}d]", ret);
318 }
319 return reply.ReadInt32();
320 }
321 } // namespace NetsysNative
322 } // namespace OHOS
323