1 /*
2 * Copyright (C) 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
16 #include "wifi_scan_callback_stub.h"
17 #include "define.h"
18 #include "wifi_manager_service_ipc_interface_code.h"
19 #include "wifi_errcode.h"
20 #include "wifi_logger.h"
21 #include "wifi_msg.h"
22
23 DEFINE_WIFILOG_SCAN_LABEL("WifiScanCallbackStubLite");
24 namespace OHOS {
25 namespace Wifi {
WifiScanCallbackStub()26 WifiScanCallbackStub::WifiScanCallbackStub() : userCallback_(nullptr), mRemoteDied(false)
27 {}
28
~WifiScanCallbackStub()29 WifiScanCallbackStub::~WifiScanCallbackStub()
30 {}
31
OnRemoteRequest(uint32_t code,IpcIo * data)32 int WifiScanCallbackStub::OnRemoteRequest(uint32_t code, IpcIo *data)
33 {
34 int ret = WIFI_OPT_FAILED;
35 WIFI_LOGD("OnRemoteRequest code:%{public}u!", code);
36 if (mRemoteDied || data == nullptr) {
37 WIFI_LOGD("Failed to %{public}s,mRemoteDied:%{public}d data:%{public}d!",
38 __func__, mRemoteDied, data == nullptr);
39 return ret;
40 }
41
42 size_t length;
43 uint16_t* interfaceRead = nullptr;
44 interfaceRead = ReadInterfaceToken(data, &length);
45 for (size_t i = 0; i < length; i++) {
46 if (i >= DECLARE_INTERFACE_DESCRIPTOR_L1_LENGTH || interfaceRead[i] != DECLARE_INTERFACE_DESCRIPTOR_L1[i]) {
47 WIFI_LOGE("Scan stub token verification error: %{public}d", code);
48 return WIFI_OPT_FAILED;
49 }
50 }
51
52 int exception = WIFI_OPT_FAILED;
53 (void)ReadInt32(data, &exception);
54 if (exception) {
55 WIFI_LOGD("OnRemoteRequest exception! %{public}d!", exception);
56 return ret;
57 }
58 switch (code) {
59 case static_cast<uint32_t>(ScanInterfaceCode::WIFI_CBK_CMD_SCAN_STATE_CHANGE): {
60 WIFI_LOGD("OnRemoteRequest code:%{public}u", code);
61 ret = RemoteOnWifiScanStateChanged(code, data);
62 break;
63 }
64 default: {
65 ret = WIFI_OPT_FAILED;
66 }
67 }
68 return ret;
69 }
70
RegisterCallBack(const std::shared_ptr<IWifiScanCallback> & userCallback)71 void WifiScanCallbackStub::RegisterCallBack(const std::shared_ptr<IWifiScanCallback> &userCallback)
72 {
73 if (userCallback_ != nullptr) {
74 WIFI_LOGD("Callback has registered!");
75 return;
76 }
77 userCallback_ = userCallback;
78 }
79
IsRemoteDied() const80 bool WifiScanCallbackStub::IsRemoteDied() const
81 {
82 return mRemoteDied;
83 }
84
SetRemoteDied(bool val)85 void WifiScanCallbackStub::SetRemoteDied(bool val)
86 {
87 mRemoteDied = val;
88 }
89
OnWifiScanStateChanged(int state)90 void WifiScanCallbackStub::OnWifiScanStateChanged(int state)
91 {
92 WIFI_LOGD("OnWifiScanStateChanged,state:%{public}d", state);
93
94 if (userCallback_) {
95 userCallback_->OnWifiScanStateChanged(state);
96 }
97 }
98
RemoteOnWifiScanStateChanged(uint32_t code,IpcIo * data)99 int WifiScanCallbackStub::RemoteOnWifiScanStateChanged(uint32_t code, IpcIo *data)
100 {
101 WIFI_LOGD("run %{public}s code %{public}u", __func__, code);
102 int stateCode = 0;
103 (void)ReadInt32(data, &stateCode);
104 OnWifiScanStateChanged(stateCode);
105 return 0;
106 }
107 } // namespace Wifi
108 } // namespace OHOS
109