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)27 int32_t NetSupplierCallbackProxy::RequestNetwork(const std::string &ident, const std::set<NetCap> &netCaps)
28 {
29 MessageParcel data;
30 if (!WriteInterfaceToken(data)) {
31 NETMGR_LOG_E("WriteInterfaceToken failed");
32 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
33 }
34 data.WriteString(ident);
35 uint32_t size = static_cast<uint32_t>(netCaps.size());
36 data.WriteUint32(size);
37 for (auto netCap : netCaps) {
38 data.WriteUint32(static_cast<uint32_t>(netCap));
39 }
40
41 sptr<IRemoteObject> remote = Remote();
42 if (remote == nullptr) {
43 NETMGR_LOG_E("Remote is null");
44 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
45 }
46
47 MessageParcel reply;
48 MessageOption option;
49 int32_t ret = remote->SendRequest(
50 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REQUEST_NETWORK), data, reply, option);
51 if (ret != ERR_NONE) {
52 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
53 }
54 return ret;
55 }
56
ReleaseNetwork(const std::string & ident,const std::set<NetCap> & netCaps)57 int32_t NetSupplierCallbackProxy::ReleaseNetwork(const std::string &ident, const std::set<NetCap> &netCaps)
58 {
59 MessageParcel data;
60 if (!WriteInterfaceToken(data)) {
61 NETMGR_LOG_E("WriteInterfaceToken failed");
62 return NETMANAGER_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
63 }
64 data.WriteString(ident);
65 uint32_t size = static_cast<uint32_t>(netCaps.size());
66 data.WriteUint32(size);
67 for (auto netCap : netCaps) {
68 data.WriteInt32(static_cast<uint32_t>(netCap));
69 }
70
71 sptr<IRemoteObject> remote = Remote();
72 if (remote == nullptr) {
73 NETMGR_LOG_E("Remote is null");
74 return NETMANAGER_ERR_IPC_CONNECT_STUB_FAIL;
75 }
76
77 MessageParcel reply;
78 MessageOption option;
79 int32_t ret = remote->SendRequest(
80 static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_RELEASE_NETWORK), data, reply, option);
81 if (ret != ERR_NONE) {
82 NETMGR_LOG_E("Proxy SendRequest failed, ret code:[%{public}d]", ret);
83 }
84 return ret;
85 }
86
WriteInterfaceToken(MessageParcel & data)87 bool NetSupplierCallbackProxy::WriteInterfaceToken(MessageParcel &data)
88 {
89 if (!data.WriteInterfaceToken(NetSupplierCallbackProxy::GetDescriptor())) {
90 NETMGR_LOG_E("WriteInterfaceToken failed");
91 return false;
92 }
93 return true;
94 }
95 } // namespace NetManagerStandard
96 } // namespace OHOS
97