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