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