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