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