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
16 #include "net_mgr_log_wrapper.h"
17 #include "net_supplier_callback_proxy.h"
18 #include "net_manager_constants.h"
19 namespace OHOS {
20 namespace NetManagerStandard {
NetSupplierCallbackProxy(const sptr<IRemoteObject> &impl)21 NetSupplierCallbackProxy::NetSupplierCallbackProxy(const sptr<IRemoteObject> &impl)
22 : IRemoteProxy<INetSupplierCallback>(impl)
23 {}
24
~NetSupplierCallbackProxy()25 NetSupplierCallbackProxy::~NetSupplierCallbackProxy() {}
26
RequestNetwork(const std::string &ident, const std::set<NetCap> &netCaps, const NetRequest &netrequest)27 int32_t NetSupplierCallbackProxy::RequestNetwork(const std::string &ident, const std::set<NetCap> &netCaps,
28 const NetRequest &netrequest)
29 {
30 MessageParcel data;
31 if (!WriteInterfaceToken(data)) {
32 NETMGR_LOG_E("WriteInterfaceToken failed");
33 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
34 }
35 data.WriteString(ident);
36 uint32_t size = static_cast<uint32_t>(netCaps.size());
37 data.WriteUint32(size);
38 for (auto netCap : netCaps) {
39 data.WriteUint32(static_cast<uint32_t>(netCap));
40 }
41 data.WriteInt32(netrequest.registerType);
42 uint32_t bearTypeSize = static_cast<uint32_t>(netrequest.bearTypes.size());
43 data.WriteUint32(bearTypeSize);
44 for (auto bearType : netrequest.bearTypes) {
45 data.WriteUint32(static_cast<uint32_t>(bearType));
46 }
47 data.WriteUint32(netrequest.uid);
48 data.WriteUint32(netrequest.requestId);
49 data.WriteString(netrequest.ident);
50 sptr<IRemoteObject> remote = Remote();
51 if (remote == nullptr) {
52 NETMGR_LOG_E("Remote is null");
53 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
54 }
55
56 MessageParcel reply;
57 MessageOption option;
58 int32_t ret = remote->SendRequest(
59 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REQUEST_NETWORK), data, reply, option);
60 if (ret != ERR_NONE) {
61 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
62 }
63 return ret;
64 }
65
ReleaseNetwork(const std::string &ident, const std::set<NetCap> &netCaps)66 int32_t NetSupplierCallbackProxy::ReleaseNetwork(const std::string &ident, const std::set<NetCap> &netCaps)
67 {
68 MessageParcel data;
69 if (!WriteInterfaceToken(data)) {
70 NETMGR_LOG_E("WriteInterfaceToken failed");
71 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
72 }
73 data.WriteString(ident);
74 uint32_t size = static_cast<uint32_t>(netCaps.size());
75 data.WriteUint32(size);
76 for (auto netCap : netCaps) {
77 data.WriteInt32(static_cast<uint32_t>(netCap));
78 }
79
80 sptr<IRemoteObject> remote = Remote();
81 if (remote == nullptr) {
82 NETMGR_LOG_E("Remote is null");
83 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
84 }
85
86 MessageParcel reply;
87 MessageOption option;
88 int32_t ret = remote->SendRequest(
89 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_RELEASE_NETWORK), data, reply, option);
90 if (ret != ERR_NONE) {
91 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
92 }
93 return ret;
94 }
95
AddRequest(const NetRequest &netRequest)96 int32_t NetSupplierCallbackProxy::AddRequest(const NetRequest &netRequest)
97 {
98 NETMGR_LOG_D("NetSupplierCallbackProxy::AddRequest: uid:[%{public}d]", netRequest.uid);
99 MessageParcel data;
100 if (!WriteInterfaceToken(data)) {
101 NETMGR_LOG_E("WriteInterfaceToken failed");
102 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
103 }
104 bool result = data.WriteUint32(netRequest.uid) && data.WriteUint32(netRequest.requestId) &&
105 data.WriteUint32(netRequest.registerType) && data.WriteString(netRequest.ident);
106 if (!result) {
107 NETMGR_LOG_E("Write uid, requestId, registerType or ident failed");
108 return NETMANAGER_ERR_WRITE_DATA_FAIL;
109 }
110
111 uint32_t size = static_cast<uint32_t>(netRequest.bearTypes.size());
112 if (!data.WriteUint32(size)) {
113 NETMGR_LOG_E("Write bearTypes size failed");
114 return NETMANAGER_ERR_WRITE_DATA_FAIL;
115 }
116 for (auto netBearType : netRequest.bearTypes) {
117 if (!data.WriteInt32(netBearType)) {
118 NETMGR_LOG_E("Write net BearType failed");
119 return NETMANAGER_ERR_WRITE_DATA_FAIL;
120 }
121 }
122
123 size = static_cast<uint32_t>(netRequest.netCaps.size());
124 if (!data.WriteUint32(size)) {
125 NETMGR_LOG_E("Write net caps size failed");
126 return NETMANAGER_ERR_WRITE_DATA_FAIL;
127 }
128 for (auto netCap : netRequest.netCaps) {
129 if (!data.WriteInt32(netCap)) {
130 NETMGR_LOG_E("Write net cap failed");
131 return NETMANAGER_ERR_WRITE_DATA_FAIL;
132 }
133 }
134 sptr<IRemoteObject> remote = Remote();
135 if (remote == nullptr) {
136 NETMGR_LOG_E("Remote is null");
137 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
138 }
139
140 MessageParcel reply;
141 MessageOption option;
142 option.SetFlags(MessageOption::TF_ASYNC);
143 int32_t ret = remote->SendRequest(static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_ADD_REQUEST), data,
144 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
RemoveRequest(const NetRequest &netRequest)151 int32_t NetSupplierCallbackProxy::RemoveRequest(const NetRequest &netRequest)
152 {
153 NETMGR_LOG_D("NetSupplierCallbackProxy::RemoveRequest: uid:[%{public}d]", netRequest.uid);
154 MessageParcel data;
155 if (!WriteInterfaceToken(data)) {
156 NETMGR_LOG_E("WriteInterfaceToken failed");
157 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
158 }
159 bool result = data.WriteUint32(netRequest.uid) && data.WriteUint32(netRequest.requestId) &&
160 data.WriteUint32(netRequest.registerType) && data.WriteString(netRequest.ident);
161 if (!result) {
162 NETMGR_LOG_E("Write uid, requestId,P registerType or ident failed");
163 return NETMANAGER_ERR_WRITE_DATA_FAIL;
164 }
165
166 uint32_t size = static_cast<uint32_t>(netRequest.bearTypes.size());
167 if (!data.WriteUint32(size)) {
168 NETMGR_LOG_E("Write bearTypes size failed");
169 return NETMANAGER_ERR_WRITE_DATA_FAIL;
170 }
171 for (auto netBearType : netRequest.bearTypes) {
172 if (!data.WriteInt32(netBearType)) {
173 NETMGR_LOG_E("Write net BearType failed");
174 return NETMANAGER_ERR_WRITE_DATA_FAIL;
175 }
176 }
177
178 size = static_cast<uint32_t>(netRequest.netCaps.size());
179 if (!data.WriteUint32(size)) {
180 NETMGR_LOG_E("Write net caps size failed");
181 return NETMANAGER_ERR_WRITE_DATA_FAIL;
182 }
183 for (auto netCap : netRequest.netCaps) {
184 if (!data.WriteInt32(netCap)) {
185 NETMGR_LOG_E("Write net cap failed");
186 return NETMANAGER_ERR_WRITE_DATA_FAIL;
187 }
188 }
189 sptr<IRemoteObject> remote = Remote();
190 if (remote == nullptr) {
191 NETMGR_LOG_E("Remote is null");
192 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
193 }
194
195 MessageParcel reply;
196 MessageOption option;
197 option.SetFlags(MessageOption::TF_ASYNC);
198 int32_t ret = remote->SendRequest(static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REMOVE_REQUEST), data,
199 reply, option);
200 if (ret != ERR_NONE) {
201 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
202 }
203 return ret;
204 }
205
WriteInterfaceToken(MessageParcel &data)206 bool NetSupplierCallbackProxy::WriteInterfaceToken(MessageParcel &data)
207 {
208 if (!data.WriteInterfaceToken(NetSupplierCallbackProxy::GetDescriptor())) {
209 NETMGR_LOG_E("WriteInterfaceToken failed");
210 return false;
211 }
212 return true;
213 }
214 } // namespace NetManagerStandard
215 } // namespace OHOS
216