1 /*
2 * Copyright (c) 2021 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_conn_callback_stub.h"
16 #include "net_conn_constants.h"
17 #include "net_mgr_log_wrapper.h"
18
19 namespace OHOS {
20 namespace NetManagerStandard {
21 static constexpr uint32_t MAX_NET_CAP_NUM = 32;
22
NetConnCallbackStub()23 NetConnCallbackStub::NetConnCallbackStub()
24 {
25 memberFuncMap_[static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_AVAILABLE)] =
26 &NetConnCallbackStub::OnNetAvailable;
27 memberFuncMap_[static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_CAPABILITIES_CHANGE)] =
28 &NetConnCallbackStub::OnNetCapabilitiesChange;
29 memberFuncMap_[static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_CONNECTION_PROPERTIES_CHANGE)] =
30 &NetConnCallbackStub::OnNetConnectionPropertiesChange;
31 memberFuncMap_[static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_LOST)] = &NetConnCallbackStub::OnNetLost;
32 memberFuncMap_[static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_UNAVAILABLE)] =
33 &NetConnCallbackStub::OnNetUnavailable;
34 memberFuncMap_[static_cast<uint32_t>(ConnCallbackInterfaceCode::NET_BLOCK_STATUS_CHANGE)] =
35 &NetConnCallbackStub::OnNetBlockStatusChange;
36 }
37
~NetConnCallbackStub()38 NetConnCallbackStub::~NetConnCallbackStub() {}
39
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int32_t NetConnCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
41 MessageOption &option)
42 {
43 NETMGR_LOG_D("Stub call start, code:[%{public}d]", code);
44 std::u16string myDescripter = NetConnCallbackStub::GetDescriptor();
45 std::u16string remoteDescripter = data.ReadInterfaceToken();
46 if (myDescripter != remoteDescripter) {
47 NETMGR_LOG_E("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_D("Stub default case, need check");
60 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61 }
62
OnNetAvailable(MessageParcel & data,MessageParcel & reply)63 int32_t NetConnCallbackStub::OnNetAvailable(MessageParcel &data, MessageParcel &reply)
64 {
65 if (!data.ContainFileDescriptors()) {
66 NETMGR_LOG_W("sent raw data is less than 32k");
67 }
68 int32_t netId = 0;
69 if (!data.ReadInt32(netId)) {
70 return NETMANAGER_ERR_READ_DATA_FAIL;
71 }
72 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
73 int32_t result = NetAvailable(netHandle);
74 if (!reply.WriteInt32(result)) {
75 NETMGR_LOG_E("Write parcel failed");
76 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
77 }
78
79 return NETMANAGER_SUCCESS;
80 }
81
OnNetCapabilitiesChange(MessageParcel & data,MessageParcel & reply)82 int32_t NetConnCallbackStub::OnNetCapabilitiesChange(MessageParcel &data, MessageParcel &reply)
83 {
84 if (!data.ContainFileDescriptors()) {
85 NETMGR_LOG_W("sent raw data is less than 32k");
86 }
87
88 int32_t netId = 0;
89 sptr<NetAllCapabilities> netAllCap = std::make_unique<NetAllCapabilities>().release();
90 if (!data.ReadInt32(netId) || !data.ReadUint32(netAllCap->linkUpBandwidthKbps_) ||
91 !data.ReadUint32(netAllCap->linkDownBandwidthKbps_)) {
92 return NETMANAGER_ERR_READ_DATA_FAIL;
93 }
94 uint32_t size = 0;
95 uint32_t value = 0;
96 if (!data.ReadUint32(size)) {
97 return NETMANAGER_ERR_READ_DATA_FAIL;
98 }
99 size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
100 for (uint32_t i = 0; i < size; i++) {
101 if (!data.ReadUint32(value)) {
102 return NETMANAGER_ERR_READ_DATA_FAIL;
103 }
104 netAllCap->netCaps_.insert(static_cast<NetCap>(value));
105 }
106 if (!data.ReadUint32(size)) {
107 return NETMANAGER_ERR_READ_DATA_FAIL;
108 }
109 size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
110 for (uint32_t i = 0; i < size; i++) {
111 if (!data.ReadUint32(value)) {
112 return NETMANAGER_ERR_READ_DATA_FAIL;
113 }
114 netAllCap->bearerTypes_.insert(static_cast<NetBearType>(value));
115 }
116
117 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
118 int32_t result = NetCapabilitiesChange(netHandle, netAllCap);
119 if (!reply.WriteInt32(result)) {
120 NETMGR_LOG_E("Write parcel failed");
121 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
122 }
123
124 return NETMANAGER_SUCCESS;
125 }
126
OnNetConnectionPropertiesChange(MessageParcel & data,MessageParcel & reply)127 int32_t NetConnCallbackStub::OnNetConnectionPropertiesChange(MessageParcel &data, MessageParcel &reply)
128 {
129 if (!data.ContainFileDescriptors()) {
130 NETMGR_LOG_W("sent raw data is less than 32k");
131 }
132
133 int32_t netId;
134 if (!data.ReadInt32(netId)) {
135 return NETMANAGER_ERR_READ_DATA_FAIL;
136 }
137 sptr<NetLinkInfo> info = NetLinkInfo::Unmarshalling(data);
138 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
139 int32_t result = NetConnectionPropertiesChange(netHandle, info);
140 if (!reply.WriteInt32(result)) {
141 NETMGR_LOG_E("Write parcel failed");
142 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
143 }
144
145 return NETMANAGER_SUCCESS;
146 }
147
OnNetLost(MessageParcel & data,MessageParcel & reply)148 int32_t NetConnCallbackStub::OnNetLost(MessageParcel &data, MessageParcel &reply)
149 {
150 if (!data.ContainFileDescriptors()) {
151 NETMGR_LOG_W("sent raw data is less than 32k");
152 }
153
154 int32_t netId;
155 if (!data.ReadInt32(netId)) {
156 return NETMANAGER_ERR_READ_DATA_FAIL;
157 }
158 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
159 int32_t result = NetLost(netHandle);
160 if (!reply.WriteInt32(result)) {
161 NETMGR_LOG_E("Write parcel failed");
162 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
163 }
164
165 return NETMANAGER_SUCCESS;
166 }
167
OnNetUnavailable(MessageParcel & data,MessageParcel & reply)168 int32_t NetConnCallbackStub::OnNetUnavailable(MessageParcel &data, MessageParcel &reply)
169 {
170 if (!data.ContainFileDescriptors()) {
171 NETMGR_LOG_W("sent raw data is less than 32k");
172 }
173
174 int32_t result = NetUnavailable();
175 if (!reply.WriteInt32(result)) {
176 NETMGR_LOG_E("Write parcel failed");
177 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
178 }
179 return NETMANAGER_SUCCESS;
180 }
181
OnNetBlockStatusChange(MessageParcel & data,MessageParcel & reply)182 int32_t NetConnCallbackStub::OnNetBlockStatusChange(MessageParcel &data, MessageParcel &reply)
183 {
184 if (!data.ContainFileDescriptors()) {
185 NETMGR_LOG_W("sent raw data is less than 32k");
186 }
187
188 int32_t netId;
189 if (!data.ReadInt32(netId)) {
190 return NETMANAGER_ERR_READ_DATA_FAIL;
191 }
192 bool blocked;
193 if (!data.ReadBool(blocked)) {
194 return NETMANAGER_ERR_READ_DATA_FAIL;
195 }
196
197 sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
198 int32_t result = NetBlockStatusChange(netHandle, blocked);
199 if (!reply.WriteInt32(result)) {
200 NETMGR_LOG_E("Write parcel failed");
201 return NETMANAGER_ERR_WRITE_REPLY_FAIL;
202 }
203 return NETMANAGER_SUCCESS;
204 }
205
NetAvailable(sptr<NetHandle> & netHandle)206 int32_t NetConnCallbackStub::NetAvailable(sptr<NetHandle> &netHandle)
207 {
208 return NETMANAGER_SUCCESS;
209 }
210
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)211 int32_t NetConnCallbackStub::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
212 const sptr<NetAllCapabilities> &netAllCap)
213 {
214 return NETMANAGER_SUCCESS;
215 }
216
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)217 int32_t NetConnCallbackStub::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
218 {
219 return NETMANAGER_SUCCESS;
220 }
221
NetLost(sptr<NetHandle> & netHandle)222 int32_t NetConnCallbackStub::NetLost(sptr<NetHandle> &netHandle)
223 {
224 return NETMANAGER_SUCCESS;
225 }
226
NetUnavailable()227 int32_t NetConnCallbackStub::NetUnavailable()
228 {
229 return NETMANAGER_SUCCESS;
230 }
231
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)232 int32_t NetConnCallbackStub::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
233 {
234 return NETMANAGER_SUCCESS;
235 }
236 } // namespace NetManagerStandard
237 } // namespace OHOS
238