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
16 #include "wifi_scan_callback_stub.h"
17 #include "define.h"
18 #include "wifi_manager_service_ipc_interface_code.h"
19 #include "wifi_hisysevent.h"
20 #include "wifi_logger.h"
21 #include "wifi_msg.h"
22
23 DEFINE_WIFILOG_SCAN_LABEL("WifiScanCallbackStub");
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,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int WifiScanCallbackStub::OnRemoteRequest(
33 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
34 {
35 WIFI_LOGD("WifiScanCallbackStub::OnRemoteRequest code:%{public}u!", code);
36
37 if (data.ReadInterfaceToken() != GetDescriptor()) {
38 WIFI_LOGE("Scan callback stub token verification error: %{public}d", code);
39 return WIFI_OPT_FAILED;
40 }
41
42 int exception = data.ReadInt32();
43 if (exception) {
44 WIFI_LOGE("WifiScanCallbackStub::OnRemoteRequest exception! %{public}d!", exception);
45 return WIFI_OPT_FAILED;
46 }
47 int ret = -1;
48 switch (code) {
49 case static_cast<uint32_t>(ScanInterfaceCode::WIFI_CBK_CMD_SCAN_STATE_CHANGE): {
50 WIFI_LOGD("WifiScanCallbackStub::OnRemoteRequest code:%{public}u line:%{public}d!", code, __LINE__);
51 ret = RemoteOnWifiScanStateChanged(code, data, reply);
52 break;
53 }
54 default: {
55 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
56 }
57 }
58 return ret;
59 }
60
RegisterCallBack(const sptr<IWifiScanCallback> & userCallback)61 void WifiScanCallbackStub::RegisterCallBack(const sptr<IWifiScanCallback> &userCallback)
62 {
63 if (userCallback_ != nullptr) {
64 WIFI_LOGE("Callback has registered!");
65 return;
66 }
67 userCallback_ = userCallback;
68 }
69
IsRemoteDied() const70 bool WifiScanCallbackStub::IsRemoteDied() const
71 {
72 return mRemoteDied;
73 }
74
SetRemoteDied(bool val)75 void WifiScanCallbackStub::SetRemoteDied(bool val)
76 {
77 WIFI_LOGI("WifiScanCallbackStub::SetRemoteDied,state:%{public}d", val);
78 mRemoteDied = val;
79 }
80
OnWifiScanStateChanged(int state)81 void WifiScanCallbackStub::OnWifiScanStateChanged(int state)
82 {
83 WIFI_LOGI("WifiScanCallbackStub::OnWifiScanStateChanged,state:%{public}d", state);
84
85 if (userCallback_) {
86 userCallback_->OnWifiScanStateChanged(state);
87 }
88 WriteWifiEventReceivedHiSysEvent(HISYS_STA_SCAN_STATE_CHANGE, state);
89 }
90
RemoteOnWifiScanStateChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)91 int WifiScanCallbackStub::RemoteOnWifiScanStateChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
92 {
93 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
94 int stateCode = data.ReadInt32();
95 OnWifiScanStateChanged(stateCode);
96 return 0;
97 }
98 } // namespace Wifi
99 } // namespace OHOS