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_device_callback_stub.h"
16 #include "define.h"
17 #include "wifi_hisysevent.h"
18 #include "wifi_logger.h"
19 #include "wifi_msg.h"
20 #include "wifi_errcode.h"
21
22 DEFINE_WIFILOG_LABEL("WifiDeviceCallBackStub");
23 namespace OHOS {
24 namespace Wifi {
WifiDeviceCallBackStub()25 WifiDeviceCallBackStub::WifiDeviceCallBackStub() : callback_(nullptr), mRemoteDied(false)
26 {}
27
~WifiDeviceCallBackStub()28 WifiDeviceCallBackStub::~WifiDeviceCallBackStub()
29 {}
30
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)31 int WifiDeviceCallBackStub::OnRemoteRequest(
32 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
33 {
34 WIFI_LOGD("WifiDeviceCallBackStub::OnRemoteRequest!");
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("Sta callback stub token verification error");
42 return WIFI_OPT_FAILED;
43 }
44
45 int exception = data.ReadInt32();
46 if (exception) {
47 WIFI_LOGE("WifiDeviceCallBackStub::OnRemoteRequest, got exception: %{public}d!", exception);
48 return WIFI_OPT_FAILED;
49 }
50 int ret = -1;
51 switch (code) {
52 case WIFI_CBK_CMD_STATE_CHANGE: {
53 ret = RemoteOnWifiStateChanged(code, data, reply);
54 break;
55 }
56 case WIFI_CBK_CMD_CONNECTION_CHANGE: {
57 ret = RemoteOnWifiConnectionChanged(code, data, reply);
58 break;
59 }
60 case WIFI_CBK_CMD_RSSI_CHANGE: {
61 ret = RemoteOnWifiRssiChanged(code, data, reply);
62 break;
63 }
64 case WIFI_CBK_CMD_WPS_STATE_CHANGE: {
65 ret = RemoteOnWifiWpsStateChanged(code, data, reply);
66 break;
67 }
68 case WIFI_CBK_CMD_STREAM_DIRECTION: {
69 ret = RemoteOnStreamChanged(code, data, reply);
70 break;
71 }
72 default: {
73 ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 break;
75 }
76 }
77 return ret;
78 }
79
RegisterUserCallBack(const sptr<IWifiDeviceCallBack> & callBack)80 void WifiDeviceCallBackStub::RegisterUserCallBack(const sptr<IWifiDeviceCallBack> &callBack)
81 {
82 if (callBack == nullptr) {
83 WIFI_LOGD("RegisterUserCallBack:callBack is nullptr!");
84 return;
85 }
86 callback_ = callBack;
87 }
88
IsRemoteDied() const89 bool WifiDeviceCallBackStub::IsRemoteDied() const
90 {
91 return mRemoteDied;
92 }
93
SetRemoteDied(bool val)94 void WifiDeviceCallBackStub::SetRemoteDied(bool val)
95 {
96 mRemoteDied = val;
97 }
98
OnWifiStateChanged(int state)99 void WifiDeviceCallBackStub::OnWifiStateChanged(int state)
100 {
101 WIFI_LOGD("WifiDeviceCallBackStub::OnWifiStateChanged");
102
103 if (callback_) {
104 callback_->OnWifiStateChanged(state);
105 }
106 WriteWifiEventReceivedHiSysEvent(HISYS_STA_POWER_STATE_CHANGE, state);
107 }
108
OnWifiConnectionChanged(int state,const WifiLinkedInfo & info)109 void WifiDeviceCallBackStub::OnWifiConnectionChanged(int state, const WifiLinkedInfo &info)
110 {
111 WIFI_LOGD("WifiDeviceCallBackStub::OnWifiConnectionChanged");
112 if (callback_) {
113 callback_->OnWifiConnectionChanged(state, info);
114 }
115 WriteWifiEventReceivedHiSysEvent(HISYS_STA_CONN_STATE_CHANGE, state);
116 }
117
OnWifiRssiChanged(int rssi)118 void WifiDeviceCallBackStub::OnWifiRssiChanged(int rssi)
119 {
120 WIFI_LOGD("WifiDeviceCallBackStub::OnWifiRssiChanged");
121 if (callback_) {
122 callback_->OnWifiRssiChanged(rssi);
123 }
124 WriteWifiEventReceivedHiSysEvent(HISYS_STA_RSSI_STATE_CHANGE, rssi);
125 }
126
OnWifiWpsStateChanged(int state,const std::string & pinCode)127 void WifiDeviceCallBackStub::OnWifiWpsStateChanged(int state, const std::string &pinCode)
128 {
129 WIFI_LOGD("WifiDeviceCallBackStub::OnWifiWpsStateChanged");
130 if (callback_) {
131 callback_->OnWifiWpsStateChanged(state, pinCode);
132 }
133 }
134
OnStreamChanged(int direction)135 void WifiDeviceCallBackStub::OnStreamChanged(int direction)
136 {
137 WIFI_LOGD("WifiDeviceCallBackStub::OnStreamChanged");
138 if (callback_) {
139 callback_->OnStreamChanged(direction);
140 }
141 }
142
RemoteOnWifiStateChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)143 int WifiDeviceCallBackStub::RemoteOnWifiStateChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
144 {
145 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
146 int state = data.ReadInt32();
147 OnWifiStateChanged(state);
148 reply.WriteInt32(0); /* Reply 0 to indicate that no exception occurs. */
149 return 0;
150 }
151
RemoteOnWifiConnectionChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)152 int WifiDeviceCallBackStub::RemoteOnWifiConnectionChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
153 {
154 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
155 const char *readStr = nullptr;
156 int state = data.ReadInt32();
157 WifiLinkedInfo info;
158 info.networkId = data.ReadInt32();
159 readStr = data.ReadCString();
160 info.ssid = (readStr != nullptr) ? readStr : "";
161 readStr = data.ReadCString();
162 info.bssid = (readStr != nullptr) ? readStr : "";
163 info.rssi = data.ReadInt32();
164 info.band = data.ReadInt32();
165 info.frequency = data.ReadInt32();
166 info.linkSpeed = data.ReadInt32();
167 readStr = data.ReadCString();
168 info.macAddress = (readStr != nullptr) ? readStr : "";
169 info.ipAddress = data.ReadInt32();
170 int tmpConnState = data.ReadInt32();
171 if (tmpConnState >= 0 && tmpConnState <= int(ConnState::UNKNOWN)) {
172 info.connState = ConnState(tmpConnState);
173 } else {
174 info.connState = ConnState::UNKNOWN;
175 }
176 info.ifHiddenSSID = data.ReadBool();
177 info.rxLinkSpeed = data.ReadInt32();
178 info.txLinkSpeed = data.ReadInt32();
179 info.chload = data.ReadInt32();
180 info.snr = data.ReadInt32();
181 info.isDataRestricted = data.ReadInt32();
182 readStr = data.ReadCString();
183 info.portalUrl = (readStr != nullptr) ? readStr : "";
184 int tmpState = data.ReadInt32();
185 if (tmpState >= 0 && tmpState <= int(SupplicantState::INVALID)) {
186 info.supplicantState = SupplicantState(tmpState);
187 } else {
188 info.supplicantState = SupplicantState::INVALID;
189 }
190
191 int tmpDetailState = data.ReadInt32();
192 if (tmpDetailState >= 0 && tmpDetailState <= int(DetailedState::INVALID)) {
193 info.detailedState = DetailedState(tmpDetailState);
194 } else {
195 info.detailedState = DetailedState::INVALID;
196 }
197 OnWifiConnectionChanged(state, info);
198 reply.WriteInt32(0); /* Reply 0 to indicate that no exception occurs. */
199 return 0;
200 }
201
RemoteOnWifiRssiChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)202 int WifiDeviceCallBackStub::RemoteOnWifiRssiChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
203 {
204 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
205 int rssi = data.ReadInt32();
206 OnWifiRssiChanged(rssi);
207 reply.WriteInt32(0); /* Reply 0 to indicate that no exception occurs. */
208 return 0;
209 }
210
RemoteOnWifiWpsStateChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)211 int WifiDeviceCallBackStub::RemoteOnWifiWpsStateChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
212 {
213 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
214 const char *readStr = nullptr;
215 int state = data.ReadInt32();
216 readStr = data.ReadCString();
217 std::string pinCode = (readStr != nullptr) ? readStr : "";
218 OnWifiWpsStateChanged(state, pinCode);
219 reply.WriteInt32(0); /* Reply 0 to indicate that no exception occurs. */
220 return 0;
221 }
222
RemoteOnStreamChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)223 int WifiDeviceCallBackStub::RemoteOnStreamChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
224 {
225 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
226 int direction = data.ReadInt32();
227 OnStreamChanged(direction);
228 reply.WriteInt32(0); /* Reply 0 to indicate that no exception occurs. */
229 return 0;
230 }
231 } // namespace Wifi
232 } // namespace OHOS