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_p2p_callback_stub.h"
17 #include "define.h"
18 #include "wifi_manager_service_ipc_interface_code.h"
19 #include "wifi_errcode.h"
20 #include "wifi_hisysevent.h"
21 #include "wifi_logger.h"
22 #include "wifi_p2p_msg.h"
23
24 DEFINE_WIFILOG_P2P_LABEL("WifiP2pCallbackStub");
25 namespace OHOS {
26 namespace Wifi {
WifiP2pCallbackStub()27 WifiP2pCallbackStub::WifiP2pCallbackStub() : userCallback_(nullptr), mRemoteDied(false)
28 {
29 InitHandleMap();
30 }
31
~WifiP2pCallbackStub()32 WifiP2pCallbackStub::~WifiP2pCallbackStub()
33 {}
34
InitHandleMap()35 void WifiP2pCallbackStub::InitHandleMap()
36 {
37 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_P2P_STATE_CHANGE)] =
38 &WifiP2pCallbackStub::RemoteOnP2pStateChanged;
39 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_PERSISTENT_GROUPS_CHANGE)] =
40 &WifiP2pCallbackStub::RemoteOnP2pPersistentGroupsChanged;
41 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_THIS_DEVICE_CHANGE)] =
42 &WifiP2pCallbackStub::RemoteOnP2pThisDeviceChanged;
43 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_PEER_CHANGE)] =
44 &WifiP2pCallbackStub::RemoteOnP2pPeersChanged;
45 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_SERVICE_CHANGE)] =
46 &WifiP2pCallbackStub::RemoteOnP2pServicesChanged;
47 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_CONNECT_CHANGE)] =
48 &WifiP2pCallbackStub::RemoteOnP2pConnectionChanged;
49 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_DISCOVERY_CHANGE)] =
50 &WifiP2pCallbackStub::RemoteOnP2pDiscoveryChanged;
51 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_P2P_ACTION_RESULT)] =
52 &WifiP2pCallbackStub::RemoteOnP2pActionResult;
53 handleFuncMap[static_cast<uint32_t>(P2PInterfaceCode::WIFI_CBK_CMD_CFG_CHANGE)] =
54 &WifiP2pCallbackStub::RemoteOnConfigChanged;
55 return;
56 }
57
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)58 int WifiP2pCallbackStub::OnRemoteRequest(
59 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
60 {
61 WIFI_LOGD("WifiP2pCallbackStub::OnRemoteRequest code:%{public}u!", code);
62
63 if (data.ReadInterfaceToken() != GetDescriptor()) {
64 WIFI_LOGE("P2p callback stub token verification error: %{public}d", code);
65 return WIFI_OPT_FAILED;
66 }
67
68 int exception = data.ReadInt32();
69 if (exception) {
70 WIFI_LOGD("WifiP2pCallbackStub::OnRemoteRequest exception! %{public}d!", exception);
71 return WIFI_OPT_FAILED;
72 }
73
74 HandleFuncMap::iterator iter = handleFuncMap.find(code);
75 if (iter == handleFuncMap.end()) {
76 WIFI_LOGI("not find function to deal, code %{public}u", code);
77 reply.WriteInt32(0);
78 } else {
79 (this->*(iter->second))(code, data, reply);
80 }
81 return 0;
82 }
83
RegisterCallBack(const sptr<IWifiP2pCallback> & userCallback)84 void WifiP2pCallbackStub::RegisterCallBack(const sptr<IWifiP2pCallback> &userCallback)
85 {
86 if (userCallback_ != nullptr) {
87 WIFI_LOGD("Callback has registered!");
88 return;
89 }
90 userCallback_ = userCallback;
91 }
92
IsRemoteDied() const93 bool WifiP2pCallbackStub::IsRemoteDied() const
94 {
95 return mRemoteDied;
96 }
97
SetRemoteDied(bool val)98 void WifiP2pCallbackStub::SetRemoteDied(bool val)
99 {
100 WIFI_LOGI("WifiP2pCallbackStub::SetRemoteDied: %{public}d", val);
101 mRemoteDied = val;
102 }
103
OnP2pStateChanged(int state)104 void WifiP2pCallbackStub::OnP2pStateChanged(int state)
105 {
106 WIFI_LOGI("WifiP2pCallbackStub::OnP2pStateChanged: %{public}d", state);
107 if (userCallback_) {
108 userCallback_->OnP2pStateChanged(state);
109 }
110 WriteWifiEventReceivedHiSysEvent(HISYS_P2P_STATE_CHANGE, state);
111 }
112
OnP2pPersistentGroupsChanged(void)113 void WifiP2pCallbackStub::OnP2pPersistentGroupsChanged(void)
114 {
115 WIFI_LOGI("WifiP2pCallbackStub::OnP2pPersistentGroupsChanged");
116 if (userCallback_) {
117 userCallback_->OnP2pPersistentGroupsChanged();
118 }
119 WriteWifiEventReceivedHiSysEvent(HISYS_P2P_PERSISTENT_GROUP_CHANGE, HISYS_EVENT_DEFAULT_VALUE);
120 }
121
OnP2pThisDeviceChanged(const WifiP2pDevice & device)122 void WifiP2pCallbackStub::OnP2pThisDeviceChanged(const WifiP2pDevice &device)
123 {
124 WIFI_LOGI("WifiP2pCallbackStub::OnP2pThisDeviceChanged");
125 if (userCallback_) {
126 userCallback_->OnP2pThisDeviceChanged(device);
127 }
128 WriteWifiEventReceivedHiSysEvent(HISYS_P2P_DEVICE_STATE_CHANGE, HISYS_EVENT_DEFAULT_VALUE);
129 }
130
OnP2pPeersChanged(const std::vector<WifiP2pDevice> & device)131 void WifiP2pCallbackStub::OnP2pPeersChanged(const std::vector<WifiP2pDevice> &device)
132 {
133 WIFI_LOGI("WifiP2pCallbackStub::OnP2pPeersChanged");
134 if (userCallback_) {
135 userCallback_->OnP2pPeersChanged(device);
136 }
137 WriteWifiEventReceivedHiSysEvent(HISYS_P2P_PEER_DEVICE_CHANGE, HISYS_EVENT_DEFAULT_VALUE);
138 }
139
OnP2pServicesChanged(const std::vector<WifiP2pServiceInfo> & srvInfo)140 void WifiP2pCallbackStub::OnP2pServicesChanged(const std::vector<WifiP2pServiceInfo> &srvInfo)
141 {
142 WIFI_LOGI("WifiP2pCallbackStub::OnP2pServicesChanged");
143 if (userCallback_) {
144 userCallback_->OnP2pServicesChanged(srvInfo);
145 }
146 }
147
OnP2pConnectionChanged(const WifiP2pLinkedInfo & info)148 void WifiP2pCallbackStub::OnP2pConnectionChanged(const WifiP2pLinkedInfo &info)
149 {
150 WIFI_LOGI("WifiP2pCallbackStub::OnP2pConnectionChanged: %{public}d", static_cast<int>(info.GetConnectState()));
151 if (userCallback_) {
152 userCallback_->OnP2pConnectionChanged(info);
153 }
154 WriteWifiEventReceivedHiSysEvent(HISYS_P2P_CONN_STATE_CHANGE, static_cast<int>(info.GetConnectState()));
155 }
156
OnP2pDiscoveryChanged(bool isChange)157 void WifiP2pCallbackStub::OnP2pDiscoveryChanged(bool isChange)
158 {
159 WIFI_LOGI("WifiP2pCallbackStub::OnP2pDiscoveryChanged, isChange:%{public}d", isChange);
160 if (userCallback_) {
161 userCallback_->OnP2pDiscoveryChanged(isChange);
162 }
163 WriteWifiEventReceivedHiSysEvent(HISYS_P2P_DISCOVERY_CHANGE, isChange);
164 }
165
OnP2pActionResult(P2pActionCallback action,ErrCode code)166 void WifiP2pCallbackStub::OnP2pActionResult(P2pActionCallback action, ErrCode code)
167 {
168 WIFI_LOGI("WifiP2pCallbackStub::OnP2pActionResult");
169 if (userCallback_) {
170 userCallback_->OnP2pActionResult(action, code);
171 }
172 }
173
OnConfigChanged(CfgType type,char * data,int dataLen)174 void WifiP2pCallbackStub::OnConfigChanged(CfgType type, char* data, int dataLen)
175 {
176 WIFI_LOGI("WifiP2pCallbackStub::OnConfigChanged");
177 if (userCallback_) {
178 userCallback_->OnConfigChanged(type, data, dataLen);
179 }
180 }
181
RemoteOnP2pStateChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)182 void WifiP2pCallbackStub::RemoteOnP2pStateChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
183 {
184 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
185 int state = data.ReadInt32();
186 OnP2pStateChanged(state);
187 reply.WriteInt32(0);
188 }
189
RemoteOnP2pPersistentGroupsChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)190 void WifiP2pCallbackStub::RemoteOnP2pPersistentGroupsChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
191 {
192 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
193 OnP2pPersistentGroupsChanged();
194 reply.WriteInt32(0);
195 }
196
ReadWifiP2pDeviceData(MessageParcel & data,WifiP2pDevice & device)197 void WifiP2pCallbackStub::ReadWifiP2pDeviceData(MessageParcel &data, WifiP2pDevice &device)
198 {
199 device.SetDeviceName(data.ReadString());
200 device.SetDeviceAddress(data.ReadString());
201 device.SetDeviceAddressType(data.ReadInt32());
202 device.SetPrimaryDeviceType(data.ReadString());
203 device.SetSecondaryDeviceType(data.ReadString());
204 device.SetP2pDeviceStatus(static_cast<P2pDeviceStatus>(data.ReadInt32()));
205 WifiP2pWfdInfo wfdInfo;
206 wfdInfo.SetWfdEnabled(data.ReadBool());
207 wfdInfo.SetDeviceInfo(data.ReadInt32());
208 wfdInfo.SetCtrlPort(data.ReadInt32());
209 wfdInfo.SetMaxThroughput(data.ReadInt32());
210 device.SetWfdInfo(wfdInfo);
211 device.SetWpsConfigMethod(data.ReadInt32());
212 device.SetDeviceCapabilitys(data.ReadInt32());
213 device.SetGroupCapabilitys(data.ReadInt32());
214 }
215
RemoteOnP2pThisDeviceChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)216 void WifiP2pCallbackStub::RemoteOnP2pThisDeviceChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
217 {
218 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
219 WifiP2pDevice config;
220 ReadWifiP2pDeviceData(data, config);
221 OnP2pThisDeviceChanged(config);
222 reply.WriteInt32(0);
223 }
224
RemoteOnP2pPeersChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)225 void WifiP2pCallbackStub::RemoteOnP2pPeersChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
226 {
227 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
228 constexpr int MAX_DEVICE_SIZE = 512;
229 std::vector<WifiP2pDevice> device;
230 int size = data.ReadInt32();
231 if (size > MAX_DEVICE_SIZE) {
232 WIFI_LOGE("Peers change list size error: %{public}d", size);
233 reply.WriteInt32(0);
234 return;
235 }
236 for (int i = 0; i < size; ++i) {
237 WifiP2pDevice config;
238 ReadWifiP2pDeviceData(data, config);
239 device.emplace_back(config);
240 }
241 OnP2pPeersChanged(device);
242 reply.WriteInt32(0);
243 }
244
RemoteOnP2pServicesChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)245 void WifiP2pCallbackStub::RemoteOnP2pServicesChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
246 {
247 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
248 const char *readStr = nullptr;
249 constexpr int MAX_SIZE = 512;
250 std::vector<WifiP2pServiceInfo> srvInfo;
251 int size = data.ReadInt32();
252 if (size > MAX_SIZE) {
253 WIFI_LOGE("Service change size error: %{public}d", size);
254 reply.WriteInt32(0);
255 return;
256 }
257 for (int i = 0; i < size; ++i) {
258 WifiP2pServiceInfo info;
259 readStr = data.ReadCString();
260 info.SetServiceName((readStr != nullptr) ? readStr : "");
261 readStr = data.ReadCString();
262 info.SetDeviceAddress((readStr != nullptr) ? readStr : "");
263 info.SetServicerProtocolType(static_cast<P2pServicerProtocolType>(data.ReadInt32()));
264 int length = data.ReadInt32();
265 std::vector<std::string> queryList;
266 for (int j = 0; j < length; j++) {
267 readStr = data.ReadCString();
268 std::string queryStr = (readStr != nullptr) ? readStr : "";
269 queryList.push_back(queryStr);
270 }
271 info.SetQueryList(queryList);
272 srvInfo.emplace_back(info);
273 }
274 OnP2pServicesChanged(srvInfo);
275 reply.WriteInt32(0);
276 }
277
RemoteOnP2pConnectionChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)278 void WifiP2pCallbackStub::RemoteOnP2pConnectionChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
279 {
280 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
281 const char *readStr = nullptr;
282 WifiP2pLinkedInfo info;
283 info.SetConnectState(static_cast<P2pConnectedState>(data.ReadInt32()));
284 info.SetIsGroupOwner(data.ReadBool());
285 readStr = data.ReadCString();
286 info.SetIsGroupOwnerAddress((readStr != nullptr) ? readStr : "");
287 OnP2pConnectionChanged(info);
288 reply.WriteInt32(0);
289 }
290
RemoteOnP2pDiscoveryChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)291 void WifiP2pCallbackStub::RemoteOnP2pDiscoveryChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
292 {
293 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
294 bool isChange = data.ReadBool();
295 OnP2pDiscoveryChanged(isChange);
296 reply.WriteInt32(0);
297 }
298
RemoteOnP2pActionResult(uint32_t code,MessageParcel & data,MessageParcel & reply)299 void WifiP2pCallbackStub::RemoteOnP2pActionResult(uint32_t code, MessageParcel &data, MessageParcel &reply)
300 {
301 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
302 P2pActionCallback action = static_cast<P2pActionCallback>(data.ReadInt32());
303 ErrCode errCode = static_cast<ErrCode>(data.ReadInt32());
304 OnP2pActionResult(action, errCode);
305 reply.WriteInt32(0);
306 }
307
RemoteOnConfigChanged(uint32_t code,MessageParcel & data,MessageParcel & reply)308 void WifiP2pCallbackStub::RemoteOnConfigChanged(uint32_t code, MessageParcel &data, MessageParcel &reply)
309 {
310 WIFI_LOGD("run %{public}s code %{public}u, datasize %{public}zu", __func__, code, data.GetRawDataSize());
311
312 CfgType cfgType = static_cast<CfgType>(data.ReadInt32());
313 int cfgLen = data.ReadInt32();
314 if (cfgLen <= 0) {
315 WIFI_LOGE("Config change size error: %{public}d", cfgLen);
316 reply.WriteInt32(static_cast<int>(WIFI_OPT_INVALID_PARAM));
317 return;
318 }
319
320 const char *dataBuffer = (const char *)data.ReadBuffer(cfgLen);
321 if (dataBuffer == nullptr) {
322 WIFI_LOGE("read buffer error!");
323 reply.WriteInt32(static_cast<int>(WIFI_OPT_FAILED));
324 return;
325 }
326
327 char* cfgData = new (std::nothrow) char[cfgLen];
328 if (cfgData == nullptr) {
329 WIFI_LOGE("new buffer error!");
330 reply.WriteInt32(static_cast<int>(WIFI_OPT_FAILED));
331 return;
332 }
333 if (memcpy_s(cfgData, cfgLen, dataBuffer, cfgLen) != EOK) {
334 WIFI_LOGE("memcpy_s failed!");
335 reply.WriteInt32(static_cast<int>(WIFI_OPT_FAILED));
336 return;
337 }
338 OnConfigChanged(cfgType, cfgData, cfgLen);
339 delete[] cfgData;
340 reply.WriteInt32(0);
341 }
342 } // namespace Wifi
343 } // namespace OHOS