1 /*
2  * Copyright (C) 2021 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 #ifndef OHOS_P2P_MONITOR_H
16 #define OHOS_P2P_MONITOR_H
17 
18 #include <string>
19 #include <map>
20 #include <set>
21 
22 #include "p2p_macro.h"
23 #include "p2p_define.h"
24 #include "wifi_p2p_event_callback.h"
25 #include "wifi_p2p_service_response_list.h"
26 #include "wifi_p2p_service_request_list.h"
27 #include "wifi_p2p_temp_disc_event.h"
28 
29 namespace OHOS {
30 namespace Wifi {
31 class P2pMonitor {
32     FRIEND_GTEST(P2pMonitor);
33 
34 public:
35     /**
36      * @Description Construct a new P2pMonitor object.
37      */
38     P2pMonitor();
39     /**
40      * @Description Destroy the P2pMonitor object.
41      */
42     virtual ~P2pMonitor();
43     /**
44      * @Description - Initialize monitor.
45      */
46     virtual void Initialize();
47     /**
48      * @Description - Start listening for events.
49      * @param  iface - network interface that needs to start listening for events
50      */
51     virtual void MonitorBegins(const std::string &iface);
52     /**
53      * @Description - Stop listening on events.
54      * @param  iface - network interfaces that need to be stopped from listening to events
55      */
56     virtual void MonitorEnds(const std::string &iface);
57     /**
58      * @Description - Registering events handler function.
59      * @param  iface - name of the network interface corresponding to the event processing function
60      * @param  handler - event processing function
61      */
62     virtual void RegisterIfaceHandler(const std::string &iface, const std::function<HandlerMethod> &handler);
63     /**
64      * @Description - Deregistration event processing function.
65      * @param  iface - network interfaces that need to be deregistered for event handlers
66      */
67     virtual void UnregisterHandler(const std::string &iface);
68 
69 private:
70     /**
71      * @Description - Sending events and related parameters to state machine.
72      * @param  iface - name of the network interface that needs to be processed
73      * @param  msgName - event name
74      * @param  param1 - general parameter 1
75      * @param  param2 - general parameter 1
76      * @param  messageObj - data objects to be transferred
77      */
78     virtual void MessageToStateMachine(const std::string &iface, P2P_STATE_MACHINE_CMD msgName, int param1, int param2,
79         const std::any &messageObj) const;
80     /**
81      * @Description Converting the int type to the P2pStatus type.
82      *
83      * @param status - type parameter to be converted
84      * @return P2pStatus - converted value
85      */
86     virtual P2pStatus IntStatusToP2pStatus(int status) const;
87 
88 private:
89     /**
90      * @Description - Broadcast connection supplicant event.
91      *
92      * @param  iface - network interface for event processing
93      * @param  status - connection result status
94      */
95     virtual void Broadcast2SmConnectSupplicant(const std::string &iface, int status) const;
96     /**
97      * @Description Broadcast device found event.
98      *
99      * @param iface - network interface for event processing
100      * @param device - device information
101      */
102     virtual void Broadcast2SmDeviceFound(const std::string &iface, const WifiP2pDevice &device) const;
103 
104     virtual void Broadcast2SmPrivateDeviceFound(const std::string &iface, const std::string &privateInfo) const;
105 
106     /**
107      * @Description Broadcast device lost event.
108      *
109      * @param iface - network interface for event processing
110      * @param device - device information
111      */
112     virtual void Broadcast2SmDeviceLost(const std::string &iface, const WifiP2pDevice &device) const;
113     /**
114      * @Description Broadcast the event that the GO negotiation request is received.
115      *
116      * @param iface - network interface for event processing
117      * @param config - config of P2P
118      */
119     virtual void Broadcast2SmGoNegRequest(const std::string &iface, const WifiP2pConfigInternal &config) const;
120     /**
121      * @Description Broadcast GO negotiation success event.
122      *
123      * @param iface - network interface for event processing
124      */
125     virtual void Broadcast2SmGoNegSuccess(const std::string &iface) const;
126     /**
127      * @Description Broadcast GO negotiation failure event.
128      *
129      * @param iface - network interface for event processing
130      * @param p2pStatus - cause of GO negotiation failure
131      */
132     virtual void Broadcast2SmGoNegFailure(const std::string &iface, P2pStatus p2pStatus) const;
133     /**
134      * @Description Broadcast P2P invitation request event.
135      *
136      * @param iface - network interface for event processing
137      * @param group - group that has received invitations
138      */
139     virtual void Broadcast2SmInvitationReceived(const std::string &iface, const WifiP2pGroupInfo &group) const;
140     /**
141      * @Description Broadcast the P2P invitation request result.
142      *
143      * @param iface - network interface for event processing
144      * @param p2pStatus - results of the invitation
145      */
146     virtual void Broadcast2SmInvitationResult(const std::string &iface, P2pStatus p2pStatus) const;
147     /**
148      * @Description Broadcast group establishment success event.
149      *
150      * @param iface - network interface for event processing
151      */
152     virtual void Broadcast2SmGroupFormationSuccess(const std::string &iface) const;
153     /**
154      * @Description Broadcast group setup failure event.
155      *
156      * @param iface - network interface for event processing
157      * @param reason - cause of failure
158      */
159     virtual void Broadcast2SmGroupFormationFailure(const std::string &iface, const std::string &reason) const;
160     /**
161      * @Description Broadcast group start event.
162      *
163      * @param iface - network interface for event processing
164      * @param group - group information
165      */
166     virtual void Broadcast2SmGroupStarted(const std::string &iface, const WifiP2pGroupInfo &group) const;
167     /**
168      * @Description Broadcast group removed event.
169      *
170      * @param iface - network interface for event processing
171      * @param group - group information
172      */
173     virtual void Broadcast2SmGroupRemoved(const std::string &iface, const WifiP2pGroupInfo &group) const;
174     /**
175      * @Description Broadcast provision discovery request event.
176      *
177      * @param iface - network interface for event processing
178      * @param event - provision discovery request event
179      */
180     virtual void Broadcast2SmProvDiscPbcReq(const std::string &iface, const WifiP2pTempDiscEvent &event) const;
181     /**
182      * @Description Broadcast provision discovery response event.
183      *
184      * @param iface - network interface for event processing
185      * @param event - provision discovery response event
186      */
187     virtual void Broadcast2SmProvDiscPbcResp(const std::string &iface, const WifiP2pTempDiscEvent &event) const;
188     /**
189      * @Description Broadcast provision discovery PIN input event.
190      *
191      * @param iface - network interface for event processing
192      * @param event - provision discovery request event
193      */
194     virtual void Broadcast2SmProvDiscEnterPin(const std::string &iface, const WifiP2pTempDiscEvent &event) const;
195     /**
196      * @Description Broadcast the provision discovery PIN display event.
197      *
198      * @param iface - network interface for event processing
199      * @param event - provision discovery response event
200      */
201     virtual void Broadcast2SmProvDiscShowPin(const std::string &iface, const WifiP2pTempDiscEvent &event) const;
202     /**
203      * @Description Broadcast provision discovery failure event.
204      *
205      * @param iface - network interface for event processing
206      */
207     virtual void Broadcast2SmProvDiscFailure(const std::string &iface) const;
208     /**
209      * @Description Broadcast device search stop event.
210      *
211      * @param iface - network interface for event processing
212      */
213     virtual void Broadcast2SmFindStopped(const std::string &iface) const;
214     /**
215      * @Description Broadcast service request event.
216      *
217      * @param iface - network interface for event processing
218      * @param reqList discovery service request list
219      */
220     virtual void Broadcast2SmServDiscReq(const std::string &iface, const WifiP2pServiceRequestList &reqList) const;
221     /**
222      * @Description Broadcast service response event.
223      *
224      * @param iface - network interface for event processing
225      * @param respList discovery service response list
226      */
227     virtual void Broadcast2SmServDiscResp(const std::string &iface, const WifiP2pServiceResponseList &respList) const;
228     /**
229      * @Description Broadcast the event that the STA is disconnected from the AP.
230      *
231      * @param iface - network interface for event processing
232      * @param device - device information of the disconnected STA
233      */
234     virtual void Broadcast2SmApStaDisconnected(const std::string &iface, const WifiP2pDevice &device) const;
235     /**
236      * @Description Broadcast STA and AP connection event.
237      *
238      * @param iface - network interface for event processing
239      * @param device - device information of the connected STA
240      */
241     virtual void Broadcast2SmApStaConnected(const std::string &iface, const WifiP2pDevice &device) const;
242     /**
243      * @Description Broadcast suplicant connects failed event.
244      *
245      * @param iface - network interface for event processing
246      */
247     virtual void Broadcast2SmConnectSupplicantFailed(const std::string &iface) const;
248 
249     /**
250      * @Description Broadcast p2p interface created event.
251      *
252      * @param iface - network interface for event processing
253      * @param type - 0: GC, 1: GO
254      * @param event - the name of interface created
255      */
256     virtual void Broadcast2SmP2pIfaceCreated(const std::string &iface, int type, const std::string &event) const;
257 
258     /**
259      * @Description Broadcast p2p connect failed event.
260      *
261      * @param iface - network interface for event processing
262      * @param reason - failed reason
263      * @param device - device information
264      */
265     virtual void Broadcast2SmConnectFailed(const std::string &iface, int reason, const WifiP2pDevice &device) const;
266 
267     /**
268      * @Description Broadcast p2p chr event.
269      *
270      * @param iface - network interface for event processing
271      * @param group - group information
272      */
273     virtual void Broadcast2SmChrEvent(const std::string &iface, const int &errCode) const;
274 
275     /**
276      * @Description Broadcast p2p channel switch event.
277      *
278      * @param iface - network interface for event processing
279      * @param group - group information
280      */
281     virtual void Broadcast2SmChSwitch(const std::string &iface, const WifiP2pGroupInfo &group) const;
282 private:
283     /**
284      * @Description - Register the connection supplicant result callback function.
285      *
286      * @param  status - connection status
287      */
288     void OnConnectSupplicant(int status) const;
289     /**
290      * @Description Register device discovery callback function.
291      *
292      * @param deviceInfo - packets of device discovery
293      */
294     void WpaEventDeviceFound(const HalP2pDeviceFound &deviceInfo) const;
295     /**
296      * @Description Register the callback function for device loss.
297      *
298      * @param p2pDeviceAddress - The address of the device is lost
299      */
300     void WpaEventDeviceLost(const std::string &p2pDeviceAddress) const;
301     /**
302      * @Description Register the GO negotiation request callback function.
303      *
304      * @param srcAddress - the MAC address of the GO negotiation request device
305      * @param passwordId - the password type
306      */
307     void WpaEventGoNegRequest(const std::string &srcAddress, short passwordId) const;
308     /**
309      * @Description Register the callback function for successful GO negotiation.
310      *
311      */
312     void WpaEventGoNegSuccess(void) const;
313     /**
314      * @Description Register the callback function for GO negotiation failure.
315      *
316      * @param status - result of GO negotiation
317      */
318     void WpaEventGoNegFailure(int status) const;
319     /**
320      * @Description Register the callback function of a P2P invite request.
321      *
322      * @param recvInfo - invitation requested packet
323      */
324     void WpaEventInvitationReceived(const HalP2pInvitationInfo &recvInfo) const;
325     /**
326      * @Description Register the P2P invitation result callback function.
327      *
328      * @param bssid - group name
329      * @param status - result of the invitation
330      */
331     void WpaEventInvitationResult(const std::string &bssid, int status) const;
332     /**
333      * @Description Register callback function for group establishment success.
334      *
335      */
336     void WpaEventGroupFormationSuccess(void) const;
337     /**
338      * @Description Register callback function for group establishment failure.
339      *
340      * @param failureReason - cause of failure
341      */
342     void WpaEventGroupFormationFailure(const std::string &failureReason) const;
343     /**
344      * @Description Register group start callback function.
345      *
346      * @param groupInfo - group information data packet
347      */
348     void WpaEventGroupStarted(const HalP2pGroupInfo &groupInfo) const;
349     /**
350      * @Description Register callback function for removing a group.
351      *
352      * @param groupIfName - group interface name
353      * @param isGo - whether the device is GO
354      */
355     void WpaEventGroupRemoved(const std::string &groupIfName, bool isGo) const;
356     /**
357      * @Description Register provision discovery PBC request callback function.
358      *
359      * @param p2pDeviceAddress - MAC of the peer device
360      */
361     void WpaEventProvDiscPbcReq(const std::string &p2pDeviceAddress) const;
362     /**
363      * @Description Register provision discovery PBC response callback function.
364      *
365      * @param p2pDeviceAddress - MAC of the peer device
366      */
367     void WpaEventProvDiscPbcResp(const std::string &p2pDeviceAddress) const;
368     /**
369      * @Description Register the provision discovery PIN input callback function.
370      *
371      * @param p2pDeviceAddress - MAC of the peer device
372      */
373     void WpaEventProvDiscEnterPin(const std::string &p2pDeviceAddress) const;
374     /**
375      * @Description Register the provision discovery PIN display callback function.
376      *
377      * @param p2pDeviceAddress - MAC of the peer device
378      * @param generatedPin - displayed PIN
379      */
380     void WpaEventProvDiscShowPin(const std::string &p2pDeviceAddress, const std::string &generatedPin) const;
381     /**
382      * @Description Register provision discovery failure event.
383      *
384      */
385     void WpaEventProvDiscFailure(void) const;
386     /**
387      * @Description Register device search stop callback function.
388      *
389      */
390     void WpaEventFindStopped(void) const;
391     /**
392      * @Description Register service request callback function.
393      *
394      * @param reqInfo - service request packet
395      */
396     void WpaEventServDiscReq(const HalP2pServDiscReqInfo &reqInfo) const;
397     /**
398      * @Description register service response callback function.
399      *
400      * @param srcAddress - MAC address of the device issuing the service discovery
401      * @param updateIndicator - service update indication
402      * @param tlvList - tlvs packet
403      */
404     void WpaEventServDiscResp(
405         const std::string &srcAddress, short updateIndicator, const std::vector<unsigned char> &tlvList) const;
406     /**
407      * @Description Register the callback function for disconnecting the STA from the AP.
408      *
409      * @param p2pDeviceAddress - the MAC address of the STA disconnected
410      */
411     void WpaEventApStaDisconnected(const std::string &p2pDeviceAddress) const;
412     /**
413      * @Description Register the callback function for connecting STA to AP
414      *
415      * @param p2pDeviceAddress - the MAC address of the STA connected
416      * @param p2pGroupAddress -  the group MAC address of the STA connected
417      */
418     void WpaEventApStaConnected(const std::string &p2pDeviceAddress,
419         const std::string &p2pGroupAddress) const;
420 
421     /**
422      * @Description Register Wpa_supplicant client connection failure callback function
423      *
424      */
425     void OnConnectSupplicantFailed(void) const;
426 
427     /**
428      * @Description Register the callback function for p2p interface created
429      * @param ifName - the name of interface created
430      * @param isGo - 0: GC, 1: GO
431      */
432     void WpaEventP2pIfaceCreated(const std::string &ifName, int isGo) const;
433 
434     /**
435      * @Description Register the callback function for p2p connect failed
436      * @param bssid - group name
437      * @param reason - connect failed reason
438      */
439     void WpaEventP2pConnectFailed(const std::string &bssid, int reason) const;
440 
441     /**
442      * @Description Register the callback function for p2p channel switch
443      * @param freq - channel switch freq
444      */
445     void WpaEventP2pChannelSwitch(int freq) const;
446 
447     /**
448      * @Description Register the callback function for p2p event notify
449      * @param freq - data param
450      */
451     void WpaEventStaNotifyCallBack(const std::string &notifyParam) const;
452 
453     /**
454      * @Description Register the callback function for p2p chr event
455      * @param errCode - chr evnet code
456      */
457     void WpaEventP2pChrReport(int errCode) const;
458 private:
459     /**
460      * The current implementation cannot obtain abundant HAL instances like Andoird and cannot distinguish which
461      * iace is operated on. Therefore, the iace of the current operation is recorded temporarily the same as that
462      * of the HAL operation.
463      */
464     std::string selectIfacName;
465     std::set<std::string> setMonitorIface;
466     std::map<std::string, std::function<HandlerMethod>> mapHandler;
467 };
468 }  // namespace Wifi
469 }  // namespace OHOS
470 #endif
471