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 
WriteInterfaceToken(MessageParcel & data)96 bool NetSupplierCallbackProxy::WriteInterfaceToken(MessageParcel &data)
97 {
98     if (!data.WriteInterfaceToken(NetSupplierCallbackProxy::GetDescriptor())) {
99         NETMGR_LOG_E("WriteInterfaceToken failed");
100         return false;
101     }
102     return true;
103 }
104 } // namespace NetManagerStandard
105 } // namespace OHOS
106