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_D("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_D("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         if (value < NET_CAPABILITY_END) {
105             netAllCap->netCaps_.insert(static_cast<NetCap>(value));
106         }
107     }
108     if (!data.ReadUint32(size)) {
109         return NETMANAGER_ERR_READ_DATA_FAIL;
110     }
111     size = size > MAX_NET_CAP_NUM ? MAX_NET_CAP_NUM : size;
112     for (uint32_t i = 0; i < size; i++) {
113         if (!data.ReadUint32(value)) {
114             return NETMANAGER_ERR_READ_DATA_FAIL;
115         }
116         if (value >= BEARER_DEFAULT) {
117             return NETMANAGER_ERR_INTERNAL;
118         }
119         netAllCap->bearerTypes_.insert(static_cast<NetBearType>(value));
120     }
121 
122     sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
123     int32_t result = NetCapabilitiesChange(netHandle, netAllCap);
124     if (!reply.WriteInt32(result)) {
125         NETMGR_LOG_E("Write parcel failed");
126         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
127     }
128 
129     return NETMANAGER_SUCCESS;
130 }
131 
OnNetConnectionPropertiesChange(MessageParcel & data,MessageParcel & reply)132 int32_t NetConnCallbackStub::OnNetConnectionPropertiesChange(MessageParcel &data, MessageParcel &reply)
133 {
134     if (!data.ContainFileDescriptors()) {
135         NETMGR_LOG_D("sent raw data is less than 32k");
136     }
137 
138     int32_t netId;
139     if (!data.ReadInt32(netId)) {
140         return NETMANAGER_ERR_READ_DATA_FAIL;
141     }
142     sptr<NetLinkInfo> info = NetLinkInfo::Unmarshalling(data);
143     sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
144     int32_t result = NetConnectionPropertiesChange(netHandle, info);
145     if (!reply.WriteInt32(result)) {
146         NETMGR_LOG_E("Write parcel failed");
147         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
148     }
149 
150     return NETMANAGER_SUCCESS;
151 }
152 
OnNetLost(MessageParcel & data,MessageParcel & reply)153 int32_t NetConnCallbackStub::OnNetLost(MessageParcel &data, MessageParcel &reply)
154 {
155     if (!data.ContainFileDescriptors()) {
156         NETMGR_LOG_D("sent raw data is less than 32k");
157     }
158 
159     int32_t netId;
160     if (!data.ReadInt32(netId)) {
161         return NETMANAGER_ERR_READ_DATA_FAIL;
162     }
163     sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
164     int32_t result = NetLost(netHandle);
165     if (!reply.WriteInt32(result)) {
166         NETMGR_LOG_E("Write parcel failed");
167         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
168     }
169 
170     return NETMANAGER_SUCCESS;
171 }
172 
OnNetUnavailable(MessageParcel & data,MessageParcel & reply)173 int32_t NetConnCallbackStub::OnNetUnavailable(MessageParcel &data, MessageParcel &reply)
174 {
175     if (!data.ContainFileDescriptors()) {
176         NETMGR_LOG_D("sent raw data is less than 32k");
177     }
178 
179     int32_t result = NetUnavailable();
180     if (!reply.WriteInt32(result)) {
181         NETMGR_LOG_E("Write parcel failed");
182         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
183     }
184     return NETMANAGER_SUCCESS;
185 }
186 
OnNetBlockStatusChange(MessageParcel & data,MessageParcel & reply)187 int32_t NetConnCallbackStub::OnNetBlockStatusChange(MessageParcel &data, MessageParcel &reply)
188 {
189     if (!data.ContainFileDescriptors()) {
190         NETMGR_LOG_D("sent raw data is less than 32k");
191     }
192 
193     int32_t netId;
194     if (!data.ReadInt32(netId)) {
195         return NETMANAGER_ERR_READ_DATA_FAIL;
196     }
197     bool blocked;
198     if (!data.ReadBool(blocked)) {
199         return NETMANAGER_ERR_READ_DATA_FAIL;
200     }
201 
202     sptr<NetHandle> netHandle = std::make_unique<NetHandle>(netId).release();
203     int32_t result = NetBlockStatusChange(netHandle, blocked);
204     if (!reply.WriteInt32(result)) {
205         NETMGR_LOG_E("Write parcel failed");
206         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
207     }
208     return NETMANAGER_SUCCESS;
209 }
210 
NetAvailable(sptr<NetHandle> & netHandle)211 int32_t NetConnCallbackStub::NetAvailable(sptr<NetHandle> &netHandle)
212 {
213     return NETMANAGER_SUCCESS;
214 }
215 
NetCapabilitiesChange(sptr<NetHandle> & netHandle,const sptr<NetAllCapabilities> & netAllCap)216 int32_t NetConnCallbackStub::NetCapabilitiesChange(sptr<NetHandle> &netHandle,
217                                                    const sptr<NetAllCapabilities> &netAllCap)
218 {
219     return NETMANAGER_SUCCESS;
220 }
221 
NetConnectionPropertiesChange(sptr<NetHandle> & netHandle,const sptr<NetLinkInfo> & info)222 int32_t NetConnCallbackStub::NetConnectionPropertiesChange(sptr<NetHandle> &netHandle, const sptr<NetLinkInfo> &info)
223 {
224     return NETMANAGER_SUCCESS;
225 }
226 
NetLost(sptr<NetHandle> & netHandle)227 int32_t NetConnCallbackStub::NetLost(sptr<NetHandle> &netHandle)
228 {
229     return NETMANAGER_SUCCESS;
230 }
231 
NetUnavailable()232 int32_t NetConnCallbackStub::NetUnavailable()
233 {
234     return NETMANAGER_SUCCESS;
235 }
236 
NetBlockStatusChange(sptr<NetHandle> & netHandle,bool blocked)237 int32_t NetConnCallbackStub::NetBlockStatusChange(sptr<NetHandle> &netHandle, bool blocked)
238 {
239     return NETMANAGER_SUCCESS;
240 }
241 
PreAirplaneCallbackStub()242 PreAirplaneCallbackStub::PreAirplaneCallbackStub()
243 {
244     memberFuncMap_[static_cast<uint32_t>(PreAirplaneCallbackInterfaceCode::PRE_AIRPLANE_START)] =
245         &PreAirplaneCallbackStub::OnPreAirplaneStart;
246 }
247 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)248 int32_t PreAirplaneCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
249                                                  MessageOption &option)
250 {
251     NETMGR_LOG_D("Stub call start, code:[%{public}d]", code);
252     std::u16string myDescripter = PreAirplaneCallbackStub::GetDescriptor();
253     std::u16string remoteDescripter = data.ReadInterfaceToken();
254     if (myDescripter != remoteDescripter) {
255         NETMGR_LOG_E("Descriptor checked failed");
256         return NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
257     }
258 
259     auto itFunc = memberFuncMap_.find(code);
260     if (itFunc != memberFuncMap_.end()) {
261         auto requestFunc = itFunc->second;
262         if (requestFunc != nullptr) {
263             return (this->*requestFunc)(data, reply);
264         }
265     }
266 
267     NETMGR_LOG_D("Stub default case, need check");
268     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
269 }
270 
PreAirplaneStart()271 int32_t PreAirplaneCallbackStub::PreAirplaneStart()
272 {
273     NETMGR_LOG_D("Stub PreAirplaneStart");
274     return NETMANAGER_SUCCESS;
275 }
276 
OnPreAirplaneStart(MessageParcel & data,MessageParcel & reply)277 int32_t PreAirplaneCallbackStub::OnPreAirplaneStart(MessageParcel &data, MessageParcel &reply)
278 {
279     if (!data.ContainFileDescriptors()) {
280         NETMGR_LOG_W("sent raw data is less than 32k");
281     }
282 
283     int32_t result = PreAirplaneStart();
284     if (!reply.WriteInt32(result)) {
285         NETMGR_LOG_E("Write parcel failed");
286         return NETMANAGER_ERR_WRITE_REPLY_FAIL;
287     }
288     return NETMANAGER_SUCCESS;
289 }
290 } // namespace NetManagerStandard
291 } // namespace OHOS
292