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