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 NetRequest & netRequest)66 int32_t NetSupplierCallbackProxy::ReleaseNetwork(const NetRequest &netRequest)
67 {
68 MessageParcel data;
69 if (!WriteInterfaceToken(data)) {
70 NETMGR_LOG_E("WriteInterfaceToken failed");
71 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
72 }
73 bool result = data.WriteUint32(netRequest.uid) && data.WriteUint32(netRequest.requestId) &&
74 data.WriteUint32(netRequest.registerType) && data.WriteString(netRequest.ident);
75 if (!result) {
76 NETMGR_LOG_E("Write uid, requestId, registerType or ident failed");
77 return NETMANAGER_ERR_WRITE_DATA_FAIL;
78 }
79
80 uint32_t size = static_cast<uint32_t>(netRequest.bearTypes.size());
81 if (!data.WriteUint32(size)) {
82 NETMGR_LOG_E("Write bearTypes size failed");
83 return NETMANAGER_ERR_WRITE_DATA_FAIL;
84 }
85 for (auto netBearType : netRequest.bearTypes) {
86 if (!data.WriteInt32(netBearType)) {
87 NETMGR_LOG_E("Write net BearType failed");
88 return NETMANAGER_ERR_WRITE_DATA_FAIL;
89 }
90 }
91
92 size = static_cast<uint32_t>(netRequest.netCaps.size());
93 if (!data.WriteUint32(size)) {
94 NETMGR_LOG_E("Write net caps size failed");
95 return NETMANAGER_ERR_WRITE_DATA_FAIL;
96 }
97 for (auto netCap : netRequest.netCaps) {
98 if (!data.WriteInt32(netCap)) {
99 NETMGR_LOG_E("Write net cap failed");
100 return NETMANAGER_ERR_WRITE_DATA_FAIL;
101 }
102 }
103 sptr<IRemoteObject> remote = Remote();
104 if (remote == nullptr) {
105 NETMGR_LOG_E("Remote is null");
106 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
107 }
108
109 MessageParcel reply;
110 MessageOption option;
111 int32_t ret = remote->SendRequest(
112 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_RELEASE_NETWORK), data, reply, option);
113 if (ret != ERR_NONE) {
114 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
115 }
116 return ret;
117 }
118
AddRequest(const NetRequest & netRequest)119 int32_t NetSupplierCallbackProxy::AddRequest(const NetRequest &netRequest)
120 {
121 NETMGR_LOG_D("NetSupplierCallbackProxy::AddRequest: uid:[%{public}d]", netRequest.uid);
122 MessageParcel data;
123 if (!WriteInterfaceToken(data)) {
124 NETMGR_LOG_E("WriteInterfaceToken failed");
125 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
126 }
127 bool result = data.WriteUint32(netRequest.uid) && data.WriteUint32(netRequest.requestId) &&
128 data.WriteUint32(netRequest.registerType) && data.WriteString(netRequest.ident);
129 if (!result) {
130 NETMGR_LOG_E("Write uid, requestId, registerType or ident failed");
131 return NETMANAGER_ERR_WRITE_DATA_FAIL;
132 }
133
134 uint32_t size = static_cast<uint32_t>(netRequest.bearTypes.size());
135 if (!data.WriteUint32(size)) {
136 NETMGR_LOG_E("Write bearTypes size failed");
137 return NETMANAGER_ERR_WRITE_DATA_FAIL;
138 }
139 for (auto netBearType : netRequest.bearTypes) {
140 if (!data.WriteInt32(netBearType)) {
141 NETMGR_LOG_E("Write net BearType failed");
142 return NETMANAGER_ERR_WRITE_DATA_FAIL;
143 }
144 }
145
146 size = static_cast<uint32_t>(netRequest.netCaps.size());
147 if (!data.WriteUint32(size)) {
148 NETMGR_LOG_E("Write net caps size failed");
149 return NETMANAGER_ERR_WRITE_DATA_FAIL;
150 }
151 for (auto netCap : netRequest.netCaps) {
152 if (!data.WriteInt32(netCap)) {
153 NETMGR_LOG_E("Write net cap failed");
154 return NETMANAGER_ERR_WRITE_DATA_FAIL;
155 }
156 }
157 sptr<IRemoteObject> remote = Remote();
158 if (remote == nullptr) {
159 NETMGR_LOG_E("Remote is null");
160 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
161 }
162
163 MessageParcel reply;
164 MessageOption option;
165 option.SetFlags(MessageOption::TF_ASYNC);
166 int32_t ret = remote->SendRequest(static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_ADD_REQUEST), data,
167 reply, option);
168 if (ret != ERR_NONE) {
169 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
170 }
171 return ret;
172 }
173
WriteInterfaceToken(MessageParcel & data)174 bool NetSupplierCallbackProxy::WriteInterfaceToken(MessageParcel &data)
175 {
176 if (!data.WriteInterfaceToken(NetSupplierCallbackProxy::GetDescriptor())) {
177 NETMGR_LOG_E("WriteInterfaceToken failed");
178 return false;
179 }
180 return true;
181 }
182 } // namespace NetManagerStandard
183 } // namespace OHOS
184