1 /*
2 * Copyright (C) 2021-2022 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 "wifi_scan_proxy.h"
16 #include "define.h"
17 #include "wifi_logger.h"
18 #include "wifi_scan_callback_stub.h"
19
20 namespace OHOS {
21 DEFINE_WIFILOG_SCAN_LABEL("WifiScanProxy");
22 namespace Wifi {
23 static sptr<WifiScanCallbackStub> g_wifiScanCallbackStub =
24 sptr<WifiScanCallbackStub>(new (std::nothrow) WifiScanCallbackStub());
25
WifiScanProxy(const sptr<IRemoteObject> & remote)26 WifiScanProxy::WifiScanProxy(const sptr<IRemoteObject> &remote) : IRemoteProxy<IWifiScan>(remote), mRemoteDied(false)
27 {
28 if (remote) {
29 if ((remote->IsProxyObject()) && (!remote->AddDeathRecipient(this))) {
30 WIFI_LOGD("AddDeathRecipient!");
31 } else {
32 WIFI_LOGW("no recipient!");
33 }
34 }
35 }
36
~WifiScanProxy()37 WifiScanProxy::~WifiScanProxy()
38 {}
39
SetScanControlInfo(const ScanControlInfo & info)40 ErrCode WifiScanProxy::SetScanControlInfo(const ScanControlInfo &info)
41 {
42 if (mRemoteDied) {
43 WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
44 return WIFI_OPT_FAILED;
45 }
46 MessageOption option;
47 MessageParcel data;
48 MessageParcel reply;
49 if (!data.WriteInterfaceToken(GetDescriptor())) {
50 WIFI_LOGE("Write interface token error: %{public}s", __func__);
51 return WIFI_OPT_FAILED;
52 }
53 data.WriteInt32(0);
54 data.WriteInt32(info.scanForbidList.size());
55 for (auto iter = info.scanForbidList.begin(); iter != info.scanForbidList.end(); iter++) {
56 data.WriteInt32(iter->scanScene);
57 data.WriteInt32(static_cast<int>(iter->scanMode));
58 data.WriteInt32(iter->forbidTime);
59 data.WriteInt32(iter->forbidCount);
60 }
61
62 data.WriteInt32(info.scanIntervalList.size());
63 for (auto iter2 = info.scanIntervalList.begin(); iter2 != info.scanIntervalList.end(); iter2++) {
64 data.WriteInt32(iter2->scanScene);
65 data.WriteInt32(static_cast<int>(iter2->scanMode));
66 data.WriteBool(iter2->isSingle);
67 data.WriteInt32(static_cast<int>(iter2->intervalMode));
68 data.WriteInt32(iter2->interval);
69 data.WriteInt32(iter2->count);
70 }
71
72 int error = Remote()->SendRequest(WIFI_SVR_CMD_SET_SCAN_CONTROL_INFO, data, reply, option);
73 if (error != ERR_NONE) {
74 WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_SET_SCAN_CONTROL_INFO);
75 return WIFI_OPT_FAILED;
76 }
77 int exception = reply.ReadInt32();
78 if (exception) {
79 return WIFI_OPT_FAILED;
80 }
81 int ret = reply.ReadInt32();
82 if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
83 return ErrCode(ret);
84 }
85
86 return WIFI_OPT_SUCCESS;
87 }
88
Scan()89 ErrCode WifiScanProxy::Scan()
90 {
91 if (mRemoteDied) {
92 WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
93 return WIFI_OPT_FAILED;
94 }
95 MessageOption option;
96 MessageParcel data;
97 MessageParcel reply;
98 if (!data.WriteInterfaceToken(GetDescriptor())) {
99 WIFI_LOGE("Write interface token error: %{public}s", __func__);
100 return WIFI_OPT_FAILED;
101 }
102 data.WriteInt32(0);
103 int error = Remote()->SendRequest(WIFI_SVR_CMD_FULL_SCAN, data, reply, option);
104 if (error != ERR_NONE) {
105 WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_FULL_SCAN);
106 return WIFI_OPT_FAILED;
107 }
108 int exception = reply.ReadInt32();
109 if (exception) {
110 return WIFI_OPT_FAILED;
111 }
112 int ret = reply.ReadInt32();
113 if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
114 return ErrCode(ret);
115 }
116
117 return WIFI_OPT_SUCCESS;
118 }
119
AdvanceScan(const WifiScanParams & params)120 ErrCode WifiScanProxy::AdvanceScan(const WifiScanParams ¶ms)
121 {
122 if (mRemoteDied) {
123 WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
124 return WIFI_OPT_FAILED;
125 }
126 MessageOption option;
127 MessageParcel data;
128 MessageParcel reply;
129 if (!data.WriteInterfaceToken(GetDescriptor())) {
130 WIFI_LOGE("Write interface token error: %{public}s", __func__);
131 return WIFI_OPT_FAILED;
132 }
133 data.WriteInt32(0);
134 data.WriteCString(params.ssid.c_str());
135 data.WriteCString(params.bssid.c_str());
136 data.WriteInt32(params.freqs.size());
137 for (std::size_t i = 0; i < params.freqs.size(); i++) {
138 data.WriteInt32(params.freqs[i]);
139 }
140 data.WriteInt32(params.band);
141
142 int error = Remote()->SendRequest(WIFI_SVR_CMD_SPECIFIED_PARAMS_SCAN, data, reply, option);
143 if (error != ERR_NONE) {
144 WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_SPECIFIED_PARAMS_SCAN);
145 return WIFI_OPT_FAILED;
146 }
147 int exception = reply.ReadInt32();
148 if (exception) {
149 return WIFI_OPT_FAILED;
150 }
151 int ret = reply.ReadInt32();
152 if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
153 return ErrCode(ret);
154 }
155
156 return WIFI_OPT_SUCCESS;
157 }
158
IsWifiClosedScan(bool & bOpen)159 ErrCode WifiScanProxy::IsWifiClosedScan(bool &bOpen)
160 {
161 if (mRemoteDied) {
162 WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
163 return WIFI_OPT_FAILED;
164 }
165 MessageOption option;
166 MessageParcel data;
167 MessageParcel reply;
168 if (!data.WriteInterfaceToken(GetDescriptor())) {
169 WIFI_LOGE("Write interface token error: %{public}s", __func__);
170 return WIFI_OPT_FAILED;
171 }
172 data.WriteInt32(0);
173 int error = Remote()->SendRequest(WIFI_SVR_CMD_IS_SCAN_ALWAYS_ACTIVE, data, reply, option);
174 if (error != ERR_NONE) {
175 WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_IS_SCAN_ALWAYS_ACTIVE);
176 return WIFI_OPT_FAILED;
177 }
178 int exception = reply.ReadInt32();
179 if (exception) {
180 return WIFI_OPT_FAILED;
181 }
182 int ret = reply.ReadInt32();
183 if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
184 return ErrCode(ret);
185 }
186 bOpen = reply.ReadBool();
187 return WIFI_OPT_SUCCESS;
188 }
189
GetScanInfoList(std::vector<WifiScanInfo> & result)190 ErrCode WifiScanProxy::GetScanInfoList(std::vector<WifiScanInfo> &result)
191 {
192 if (mRemoteDied) {
193 WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
194 return WIFI_OPT_FAILED;
195 }
196 const char *readStr = nullptr;
197 MessageOption option;
198 MessageParcel data;
199 MessageParcel reply;
200 if (!data.WriteInterfaceToken(GetDescriptor())) {
201 WIFI_LOGE("Write interface token error: %{public}s", __func__);
202 return WIFI_OPT_FAILED;
203 }
204 data.WriteInt32(0);
205 int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_SCAN_INFO_LIST, data, reply, option);
206 if (error != ERR_NONE) {
207 WIFI_LOGW("Set Attr(%{public}d) failed", WIFI_SVR_CMD_GET_SCAN_INFO_LIST);
208 return WIFI_OPT_FAILED;
209 }
210 int exception = reply.ReadInt32();
211 if (exception) {
212 return WIFI_OPT_FAILED;
213 }
214 int ret = reply.ReadInt32();
215 if (ErrCode(ret) != WIFI_OPT_SUCCESS) {
216 return ErrCode(ret);
217 }
218
219 constexpr int MAX_SIZE = 4096;
220 int tmpsize = reply.ReadInt32();
221 if (tmpsize > MAX_SIZE) {
222 WIFI_LOGE("Scan info size exceeds maximum allowed size: %{public}d", tmpsize);
223 return WIFI_OPT_FAILED;
224 }
225 for (int i = 0; i < tmpsize; ++i) {
226 WifiScanInfo info;
227 readStr = reply.ReadCString();
228 info.bssid = (readStr != nullptr) ? readStr : "";
229 readStr = reply.ReadCString();
230 info.ssid = (readStr != nullptr) ? readStr : "";
231 readStr = reply.ReadCString();
232 info.capabilities = (readStr != nullptr) ? readStr : "";
233 info.frequency = reply.ReadInt32();
234 info.rssi = reply.ReadInt32();
235 info.timestamp = reply.ReadInt64();
236 info.band = reply.ReadInt32();
237 info.securityType = static_cast<WifiSecurity>(reply.ReadInt32());
238 info.channelWidth = static_cast<WifiChannelWidth>(reply.ReadInt32());
239 info.centerFrequency0 = reply.ReadInt32();
240 info.centerFrequency1 = reply.ReadInt32();
241 info.features = reply.ReadInt64();
242
243 constexpr int IE_SIZE_MAX = 256;
244 int ieSize = reply.ReadInt32();
245 if (ieSize > IE_SIZE_MAX) {
246 WIFI_LOGE("ie size error: %{public}d", ieSize);
247 return WIFI_OPT_FAILED;
248 }
249 for (int m = 0; m < ieSize; ++m) {
250 WifiInfoElem tempWifiInfoElem;
251 tempWifiInfoElem.id = reply.ReadInt32();
252 int contentSize = reply.ReadInt32();
253 for (int n = 0; n < contentSize; n++) {
254 char tempChar = static_cast<char>(reply.ReadInt8());
255 tempWifiInfoElem.content.emplace_back(tempChar);
256 }
257 info.infoElems.emplace_back(tempWifiInfoElem);
258 }
259 result.emplace_back(info);
260 }
261 return WIFI_OPT_SUCCESS;
262 }
263
RegisterCallBack(const sptr<IWifiScanCallback> & callback)264 ErrCode WifiScanProxy::RegisterCallBack(const sptr<IWifiScanCallback> &callback)
265 {
266 if (mRemoteDied) {
267 WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
268 return WIFI_OPT_FAILED;
269 }
270 WIFI_LOGD("RegisterCallBack start!");
271 MessageParcel data;
272 MessageParcel reply;
273 MessageOption option(MessageOption::TF_ASYNC);
274
275 if (g_wifiScanCallbackStub == nullptr) {
276 WIFI_LOGE("g_wifiScanCallbackStub is nullptr!");
277 return WIFI_OPT_FAILED;
278 }
279 g_wifiScanCallbackStub->RegisterCallBack(callback);
280 if (!data.WriteInterfaceToken(GetDescriptor())) {
281 WIFI_LOGE("Write interface token error: %{public}s", __func__);
282 return WIFI_OPT_FAILED;
283 }
284 data.WriteInt32(0);
285 if (!data.WriteRemoteObject(g_wifiScanCallbackStub->AsObject())) {
286 WIFI_LOGE("RegisterCallBack WriteRemoteObject failed!");
287 return WIFI_OPT_FAILED;
288 }
289
290 int error = Remote()->SendRequest(WIFI_SVR_CMD_REGISTER_SCAN_CALLBACK, data, reply, option);
291 if (error != ERR_NONE) {
292 WIFI_LOGE("RegisterCallBack failed, error code is %{public}d", error);
293 return WIFI_OPT_FAILED;
294 }
295 int32_t ret = reply.ReadInt32();
296 WIFI_LOGD("RegisterCallBack is finished: result=%{public}d", ret);
297 return WIFI_OPT_SUCCESS;
298 }
299
GetSupportedFeatures(long & features)300 ErrCode WifiScanProxy::GetSupportedFeatures(long &features)
301 {
302 if (mRemoteDied) {
303 WIFI_LOGD("failed to `%{public}s`,remote service is died!", __func__);
304 return WIFI_OPT_FAILED;
305 }
306 MessageOption option;
307 MessageParcel data, reply;
308 if (!data.WriteInterfaceToken(GetDescriptor())) {
309 WIFI_LOGE("Write interface token error: %{public}s", __func__);
310 return WIFI_OPT_FAILED;
311 }
312 data.WriteInt32(0);
313 int error = Remote()->SendRequest(WIFI_SVR_CMD_GET_SUPPORTED_FEATURES, data, reply, option);
314 if (error != ERR_NONE) {
315 WIFI_LOGE("Set Attr(%{public}d) failed,error code is %{public}d", WIFI_SVR_CMD_GET_SUPPORTED_FEATURES, error);
316 return ErrCode(error);
317 }
318 int exception = reply.ReadInt32();
319 if (exception) {
320 return WIFI_OPT_FAILED;
321 }
322 int ret = reply.ReadInt32();
323 if (ret != WIFI_OPT_SUCCESS) {
324 return ErrCode(ret);
325 }
326
327 features = reply.ReadInt64();
328 return WIFI_OPT_SUCCESS;
329 }
330
OnRemoteDied(const wptr<IRemoteObject> & remoteObject)331 void WifiScanProxy::OnRemoteDied(const wptr<IRemoteObject>& remoteObject)
332 {
333 WIFI_LOGD("Remote service is died!");
334 mRemoteDied = true;
335 if (g_wifiScanCallbackStub == nullptr) {
336 WIFI_LOGE("g_wifiScanCallbackStub is nullptr!");
337 return;
338 }
339 g_wifiScanCallbackStub->SetRemoteDied(true);
340 }
341 } // namespace Wifi
342 } // namespace OHOS
343