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 #ifndef OHOS_P2P_DEVICE_MANAGER_H
16 #define OHOS_P2P_DEVICE_MANAGER_H
17 
18 #include <mutex>
19 #include "wifi_p2p_msg.h"
20 
21 namespace OHOS {
22 namespace Wifi {
23 class WifiP2pDeviceManager {
24     friend class P2pStateMachine;
25     friend class AuthorizingNegotiationRequestState;
26     friend class GroupFormedState;
27     friend class GroupNegotiationState;
28     friend class InvitationReceivedState;
29     friend class InvitationRequestState;
30     friend class P2pDefaultState;
31     friend class P2pDisabledState;
32     friend class P2pDisablingState;
33     friend class P2pEnabledState;
34     friend class P2pEnablingState;
35     friend class P2pGroupFormationState;
36     friend class P2pGroupJoinState;
37     friend class P2pGroupOperatingState;
38     friend class P2pIdleState;
39     friend class P2pInvitingState;
40     friend class ProvisionDiscoveryState;
41     friend class WifiP2pService;
42 public:
43     /**
44      * @Description Destroy the Wifi P2p Device Manager object.
45      */
46     virtual ~WifiP2pDeviceManager() = default;
47     /**
48      * @Description - Initialize the device manager.
49      */
50     virtual void Initialize();
51     /**
52      * @Description - Add a P2P device.
53      * @param  device - P2P device to be added
54      * @return - bool true:success   false:failed
55      */
56     virtual bool AddDevice(const WifiP2pDevice &device);
57     /**
58      * @Description - Remove a P2P device.
59      * @param  deviceAddress - MAC of the P2P device to be removed
60      * @return - bool true:success    false:failed
61      */
62     virtual bool RemoveDevice(const std::string &deviceAddress);
63     /**
64      * @Description - Remove a P2P device.
65      * @param  device - P2P device to be removed
66      * @return - bool true:success    false:failed
67      */
68     virtual bool RemoveDevice(const WifiP2pDevice &device);
69     /**
70      * @Description - Clear all P2P devices.
71      * @return - int number of cleared P2P devices
72      */
73     virtual int ClearAll();
74     /**
75      * @Description - Obtain all P2P devices.
76      * @param  devices - object for storing P2P devices
77      * @return - int the number of obtained
78      */
79     virtual int GetDevicesList(std::vector<WifiP2pDevice> &devices);
80     /**
81      * @Description - Updates the supplicant information of a P2P device based on the device address.
82      * @param  devices - device that need to be updated
83      * @return - bool true:success    false:failed
84      */
85     virtual bool UpdateDeviceSupplicantInf(const WifiP2pDevice &device);
86     /**
87      * @Description - Updates all information about the P2P device based on the device address.
88                       If the device does not exist, add the device.
89      * @param  devices - device that need to be updated
90      * @return - bool true:success    false:failed
91      */
92     virtual bool UpdateDevice(const WifiP2pDevice &device);
93     /**
94      * @Description - Update the group capability of a device.
95      * @param  deviceAddress - device address to be updated
96      * @param  cap - group capability
97      * @return - bool
98      */
99     virtual bool UpdateDeviceGroupCap(const std::string &deviceAddress, uint32_t cap);
100     /**
101      * @Description - Update the group capability of a device.
102      * @param  device - device that need to be updated
103      * @return - bool true:success    false:failed
104      */
105     virtual bool UpdateDeviceGroupCap(const WifiP2pDevice &device);
106     /**
107      * @Description - Update the status of a P2P device based on the device address.
108      * @param  deviceAddress - device address to be updated
109      * @param  status - status that needs to be updated
110      * @return - bool true:success    false:failed
111      */
112     virtual bool UpdateDeviceStatus(const std::string &deviceAddress, P2pDeviceStatus status);
113     /**
114      * @Description - Updates the status of a P2P device based on the device information.
115      * @param  devices - device that need to be updated
116      * @return - bool true:success    false:failed
117      */
118     virtual bool UpdateDeviceStatus(const WifiP2pDevice &device);
119     /**
120      * @Description - Updates the status of P2P devices based on the device information.
121      * @param  status - status that needs to be updated
122      * @return - bool true:success    false:failed
123      */
124     virtual bool UpdateAllDeviceStatus(const P2pDeviceStatus status);
125     /**
126      * @Description - Obtain information about a P2P device based on the device address.
127      * @param  devices - MAC of the device
128      * @return - WifiP2pDevice device information that obtained
129      */
130     virtual WifiP2pDevice GetDevices(const std::string &deviceAddress);
131     /**
132      * @Description Obtain the device name based on the device address.
133      * @param  deviceAddress - address of the device
134      * @return - const std::string device name
135      */
136     virtual const std::string GetDeviceName(const std::string &deviceAddress);
137     /**
138      * @Description - Updates the address of a P2P group based on the device information.
139      * @param  device - device that need to get group
140      * @return - bool true:success    false:failed
141      */
142     virtual bool UpdateGroupAddress(const WifiP2pDevice &device);
143     /**
144      * @Description - Updates the address of a P2P group based on the device information.
145      * @param  deviceAddress - device to get group
146      * @param  groupAddress - groupAddress that need to be updated
147      * @return - bool true:success    false:failed
148      */
149     virtual bool UpdateGroupAddress(const std::string &deviceAddress, const std::string &groupAddress);
150 
151 private:
152     /**
153      * @Description Get own device information.
154      * @return - WifiP2pDevice a reference to the own device
155      */
GetThisDevice()156     inline WifiP2pDevice &GetThisDevice()
157     {
158         return thisDevice;
159     }
160 
161 private:
162     WifiP2pDevice thisDevice;            /* own device */
163     std::vector<WifiP2pDevice> p2pDevices;  /* all scanned devices */
164     std::mutex deviceMutex;
165 };
166 }  // namespace Wifi
167 }  // namespace OHOS
168 
169 #endif  // OHOS_P2P_DEVICE_MANAGER_H
170