• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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_conn_callback_proxy.h"
17 #include "net_conn_constants.h"
18 #include "net_mgr_log_wrapper.h"
19 
20 namespace OHOS {
21 namespace NetManagerStandard {
NetConnCallbackProxy(const sptr<IRemoteObject> & impl)22 NetConnCallbackProxy::NetConnCallbackProxy(const sptr<IRemoteObject> &impl)
23     : IRemoteProxy<INetConnCallback>(impl)
24 {}
25 
~NetConnCallbackProxy()26 NetConnCallbackProxy::~NetConnCallbackProxy() {}
27 
NetAvailable(sptr<NetHandle> & netHandle)28 int32_t NetConnCallbackProxy::NetAvailable(sptr<NetHandle> &netHandle)
29 {
30     if (netHandle == nullptr) {
31         NETMGR_LOG_E("netHandle is null");
32         return NETMANAGER_ERR_LOCAL_PTR_NULL;
33     }
34 
35     MessageParcel data;
36     if (!WriteInterfaceToken(data)) {
37         NETMGR_LOG_E("WriteInterfaceToken failed");
38         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
39     }
40 
41     if (!data.WriteInt32(netHandle->GetNetId())) {
42         return NETMANAGER_ERR_WRITE_DATA_FAIL;
43     }
44 
45     sptr<IRemoteObject> remote = Remote();
46     if (remote == nullptr) {
47         NETMGR_LOG_E("Remote is null");
48         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
49     }
50 
51     MessageParcel reply;
52     MessageOption option;
53     option.SetFlags(MessageOption::TF_ASYNC);
54     int32_t ret = remote->SendRequest(NET_AVAILABLE, data, reply, option);
55     if (ret != ERR_NONE) {
56         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
57     }
58     return ret;
59 }
60 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)61 int32_t NetConnCallbackProxy::NetCapabilitiesChange(
62     sptr<NetHandle> &netHandle, const sptr<NetAllCapabilities> &netAllCap)
63 {
64     MessageParcel data;
65     if (!WriteInterfaceToken(data)) {
66         NETMGR_LOG_E("WriteInterfaceToken failed");
67         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
68     }
69 
70     if (netHandle == nullptr || netAllCap == nullptr) {
71         return NETMANAGER_ERR_LOCAL_PTR_NULL;
72     }
73 
74     if (!data.WriteInt32(netHandle->GetNetId()) || !data.WriteUint32(netAllCap->linkUpBandwidthKbps_) ||
75         !data.WriteUint32(netAllCap->linkDownBandwidthKbps_)) {
76         return NETMANAGER_ERR_WRITE_DATA_FAIL;
77     }
78     uint32_t size = static_cast<uint32_t>(netAllCap->netCaps_.size());
79     if (!data.WriteUint32(size)) {
80         return NETMANAGER_ERR_WRITE_DATA_FAIL;
81     }
82     for (auto netCap : netAllCap->netCaps_) {
83         if (!data.WriteUint32(static_cast<uint32_t>(netCap))) {
84             return NETMANAGER_ERR_WRITE_DATA_FAIL;
85         }
86     }
87     size = static_cast<uint32_t>(netAllCap->bearerTypes_.size());
88     if (!data.WriteUint32(size)) {
89         return NETMANAGER_ERR_WRITE_DATA_FAIL;
90     }
91     for (auto bearerType : netAllCap->bearerTypes_) {
92         if (!data.WriteUint32(static_cast<uint32_t>(bearerType))) {
93             return NETMANAGER_ERR_WRITE_DATA_FAIL;
94         }
95     }
96 
97     sptr<IRemoteObject> remote = Remote();
98     if (remote == nullptr) {
99         NETMGR_LOG_E("Remote is null");
100         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
101     }
102 
103     MessageParcel reply;
104     MessageOption option;
105     option.SetFlags(MessageOption::TF_ASYNC);
106     int32_t ret = remote->SendRequest(NET_CAPABILITIES_CHANGE, data, reply, option);
107     if (ret != ERR_NONE) {
108         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
109     }
110     return ret;
111 }
112 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)113 int32_t NetConnCallbackProxy::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
114 {
115     if (netHandle == nullptr || info == nullptr) {
116         NETMGR_LOG_E("Input parameter is null");
117         return NETMANAGER_ERR_LOCAL_PTR_NULL;
118     }
119 
120     MessageParcel data;
121     if (!WriteInterfaceToken(data)) {
122         NETMGR_LOG_E("WriteInterfaceToken failed");
123         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
124     }
125 
126     if (!data.WriteInt32(netHandle->GetNetId())) {
127         return NETMANAGER_ERR_WRITE_DATA_FAIL;
128     }
129 
130     if (!info->Marshalling(data)) {
131         NETMGR_LOG_E("proxy Marshalling failed");
132         return NETMANAGER_ERR_WRITE_DATA_FAIL;
133     }
134 
135     sptr<IRemoteObject> remote = Remote();
136     if (remote == nullptr) {
137         NETMGR_LOG_E("Remote is null");
138         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
139     }
140 
141     MessageParcel reply;
142     MessageOption option;
143     option.SetFlags(MessageOption::TF_ASYNC);
144     int32_t ret = remote->SendRequest(NET_CONNECTION_PROPERTIES_CHANGE, data, reply, option);
145     if (ret != ERR_NONE) {
146         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
147     }
148     return ret;
149 }
150 
NetLost(sptr<NetHandle> & netHandle)151 int32_t NetConnCallbackProxy::NetLost(sptr<NetHandle> &netHandle)
152 {
153     if (netHandle == nullptr) {
154         NETMGR_LOG_E("netHandle is null");
155         return NETMANAGER_ERR_LOCAL_PTR_NULL;
156     }
157 
158     MessageParcel data;
159     if (!WriteInterfaceToken(data)) {
160         NETMGR_LOG_E("WriteInterfaceToken failed");
161         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
162     }
163 
164     if (!data.WriteInt32(netHandle->GetNetId())) {
165         return NETMANAGER_ERR_WRITE_DATA_FAIL;
166     }
167 
168     sptr<IRemoteObject> remote = Remote();
169     if (remote == nullptr) {
170         NETMGR_LOG_E("Remote is null");
171         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
172     }
173 
174     MessageParcel reply;
175     MessageOption option;
176     option.SetFlags(MessageOption::TF_ASYNC);
177     int32_t ret = remote->SendRequest(NET_LOST, data, reply, option);
178     if (ret != ERR_NONE) {
179         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
180     }
181     return ret;
182 }
183 
NetUnavailable()184 int32_t NetConnCallbackProxy::NetUnavailable()
185 {
186     MessageParcel data;
187     if (!WriteInterfaceToken(data)) {
188         NETMGR_LOG_E("WriteInterfaceToken failed");
189         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
190     }
191 
192     sptr<IRemoteObject> remote = Remote();
193     if (remote == nullptr) {
194         NETMGR_LOG_E("Remote is null");
195         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
196     }
197 
198     MessageParcel reply;
199     MessageOption option;
200     option.SetFlags(MessageOption::TF_ASYNC);
201     int32_t ret = remote->SendRequest(NET_UNAVAILABLE, data, reply, option);
202     if (ret != ERR_NONE) {
203         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
204     }
205     return ret;
206 }
207 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)208 int32_t NetConnCallbackProxy::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
209 {
210     if (netHandle == nullptr) {
211         NETMGR_LOG_E("netHandle is null");
212         return NETMANAGER_ERR_LOCAL_PTR_NULL;
213     }
214 
215     MessageParcel data;
216     if (!WriteInterfaceToken(data)) {
217         NETMGR_LOG_E("WriteInterfaceToken failed");
218         return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
219     }
220 
221     if (!data.WriteInt32(netHandle->GetNetId())) {
222         return NETMANAGER_ERR_WRITE_DATA_FAIL;
223     }
224     if (!data.WriteBool(blocked)) {
225         return NETMANAGER_ERR_WRITE_DATA_FAIL;
226     }
227 
228     sptr<IRemoteObject> remote = Remote();
229     if (remote == nullptr) {
230         NETMGR_LOG_E("Remote is null");
231         return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
232     }
233     MessageParcel reply;
234     MessageOption option;
235     option.SetFlags(MessageOption::TF_ASYNC);
236     int32_t ret = remote->SendRequest(NET_BLOCK_STATUS_CHANGE, data, reply, option);
237     if (ret != ERR_NONE) {
238         NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
239     }
240     return ret;
241 }
242 
WriteInterfaceToken(MessageParcel & data)243 bool NetConnCallbackProxy::WriteInterfaceToken(MessageParcel &data)
244 {
245     if (!data.WriteInterfaceToken(NetConnCallbackProxy::GetDescriptor())) {
246         NETMGR_LOG_E("WriteInterfaceToken failed");
247         return false;
248     }
249     return true;
250 }
251 } // namespace NetManagerStandard
252 } // namespace OHOS
253