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