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 #include "net_supplier_callback_stub.h"
16
17 #include "net_manager_constants.h"
18 #include "net_mgr_log_wrapper.h"
19
20 static constexpr uint32_t MAX_NET_CAP_NUM = 32;
21 static constexpr uint32_t MAX_NET_BEARTYPE_NUM = 7;
22
23 namespace OHOS {
24 namespace NetManagerStandard {
NetSupplierCallbackStub()25 NetSupplierCallbackStub::NetSupplierCallbackStub()
26 {
27 memberFuncMap_[static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_REQUEST_NETWORK)] =
28 &NetSupplierCallbackStub::OnRequestNetwork;
29 memberFuncMap_[static_cast<uint32_t>(SupplierInterfaceCode::NET_SUPPLIER_RELEASE_NETWORK)] =
30 &NetSupplierCallbackStub::OnReleaseNetwork;
31 }
32
~NetSupplierCallbackStub()33 NetSupplierCallbackStub::~NetSupplierCallbackStub() {}
34
RegisterSupplierCallbackImpl(const sptr<NetSupplierCallbackBase> & callback)35 void NetSupplierCallbackStub::RegisterSupplierCallbackImpl(const sptr<NetSupplierCallbackBase> &callback)
36 {
37 callback_ = callback;
38 }
39
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int32_t NetSupplierCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
41 MessageOption &option)
42 {
43 NETMGR_LOG_D("Net supplier callback stub call start, code:[%{public}d]", code);
44 std::u16string myDescripter = NetSupplierCallbackStub::GetDescriptor();
45 std::u16string remoteDescripter = data.ReadInterfaceToken();
46 if (myDescripter != remoteDescripter) {
47 NETMGR_LOG_I("Descriptor checked failed");
48 return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
49 }
50
51 auto itFunc = memberFuncMap_.find(code);
52 if (itFunc != memberFuncMap_.end()) {
53 auto requestFunc = itFunc->second;
54 if (requestFunc != nullptr) {
55 return (this->*requestFunc)(data, reply);
56 }
57 }
58
59 NETMGR_LOG_I("Stub default case, need check");
60 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61 }
62
OnRequestNetwork(MessageParcel & data,MessageParcel & reply)63 int32_t NetSupplierCallbackStub::OnRequestNetwork(MessageParcel &data, MessageParcel &reply)
64 {
65 std::string ident;
66 std::set<NetCap> netCaps;
67
68 data.ReadString(ident);
69 uint32_t size = 0;
70 uint32_t value = 0;
71 data.ReadUint32(size);
72 if (size > MAX_NET_CAP_NUM) {
73 NETMGR_LOG_E("Net cap size is too large");
74 return NETMANAGER_ERR_INVALID_PARAMETER;
75 }
76 for (uint32_t i = 0; i < size; i++) {
77 data.ReadUint32(value);
78 if (value < NET_CAPABILITY_END) {
79 netCaps.insert(static_cast<NetCap>(value));
80 }
81 }
82 int32_t registerType = 0;
83 data.ReadInt32(registerType);
84 std::set<NetBearType> netBearTypes;
85 uint32_t bearTypeSize = 0;
86 data.ReadUint32(bearTypeSize);
87 if (bearTypeSize > MAX_NET_BEARTYPE_NUM) {
88 NETMGR_LOG_E("Net beartype size is too large");
89 return NETMANAGER_ERR_INVALID_PARAMETER;
90 }
91 for (uint32_t i = 0; i < bearTypeSize; i++) {
92 data.ReadUint32(value);
93 if (value <= BEARER_DEFAULT) {
94 netBearTypes.insert(static_cast<NetBearType>(value));
95 }
96 }
97 uint32_t uid = 0;
98 data.ReadUint32(uid);
99 uint32_t requestId = 0;
100 data.ReadUint32(requestId);
101 std::string requestIdent;
102 data.ReadString(requestIdent);
103 NetRequest netRequest(registerType, netBearTypes, uid, requestId, requestIdent);
104 RequestNetwork(ident, netCaps, netRequest);
105
106 reply.WriteInt32(0);
107 return NETMANAGER_SUCCESS;
108 }
109
OnReleaseNetwork(MessageParcel & data,MessageParcel & reply)110 int32_t NetSupplierCallbackStub::OnReleaseNetwork(MessageParcel &data, MessageParcel &reply)
111 {
112 std::string ident;
113 std::set<NetCap> netCaps;
114
115 data.ReadString(ident);
116 uint32_t size = 0;
117 uint32_t value = 0;
118 data.ReadUint32(size);
119 size = (size > MAX_NET_CAP_NUM) ? MAX_NET_CAP_NUM : size;
120 for (uint32_t i = 0; i < size; i++) {
121 data.ReadUint32(value);
122 if (value < NET_CAPABILITY_END) {
123 netCaps.insert(static_cast<NetCap>(value));
124 }
125 }
126
127 ReleaseNetwork(ident, netCaps);
128
129 reply.WriteInt32(0);
130 return NETMANAGER_SUCCESS;
131 }
132
RequestNetwork(const std::string & ident,const std::set<NetCap> & netCaps,const NetRequest & netrequest)133 int32_t NetSupplierCallbackStub::RequestNetwork(const std::string &ident, const std::set<NetCap> &netCaps,
134 const NetRequest &netrequest)
135 {
136 if (callback_ != nullptr) {
137 auto startTime = std::chrono::steady_clock::now();
138 callback_->RequestNetwork(ident, netCaps, netrequest);
139 auto endTime = std::chrono::steady_clock::now();
140 auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime);
141 NETMGR_LOG_I("RequestNetwork[%{public}s], cost=%{public}lld", ident.c_str(), durationNs.count());
142 }
143 return 0;
144 }
145
ReleaseNetwork(const std::string & ident,const std::set<NetCap> & netCaps)146 int32_t NetSupplierCallbackStub::ReleaseNetwork(const std::string &ident, const std::set<NetCap> &netCaps)
147 {
148 if (callback_ != nullptr) {
149 auto startTime = std::chrono::steady_clock::now();
150 callback_->ReleaseNetwork(ident, netCaps);
151 auto endTime = std::chrono::steady_clock::now();
152 auto durationNs = std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime);
153 NETMGR_LOG_I("ReleaseNetwork[%{public}s], cost=%{public}lld", ident.c_str(), durationNs.count());
154 }
155 return 0;
156 }
157 } // namespace NetManagerStandard
158 } // namespace OHOS
159