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 #ifndef OHOS_P2P_GROUP_MANAGER_H
17 #define OHOS_P2P_GROUP_MANAGER_H
18 
19 #include <vector>
20 #include <mutex>
21 #include "wifi_msg.h"
22 #include "wifi_p2p_msg.h"
23 #include "p2p_macro.h"
24 #include "wifi_p2p_group_info_proxy.h"
25 #include "wifi_common_msg.h"
26 
27 namespace OHOS {
28 namespace Wifi {
29 const int CREDENTIAL_MAX_NUM = 32;
30 class WifiP2pGroupManager {
31 FRIEND_GTEST(WifiP2pGroupManager);
32 public:
33     /**
34      * @Description  Construct a new WifiP2pGroupManager object.
35      * @param None
36      * @return None
37      */
38     WifiP2pGroupManager();
39 
40     /**
41      * @Description  Destroy the WifiP2pGroupManager object.
42      * @param None
43      * @return None
44      */
45     virtual ~WifiP2pGroupManager() = default;
46 
47     /**
48      * @Description - Reads the stored group information during initialization.
49      * @param None
50      * @return None
51      */
52     virtual void Initialize();
53 
54     /**
55      * @Description - Stores and serializes the current group information.
56      */
57     virtual void StashGroups();
58 
59     /**
60      * @Description - Adding a P2P group.
61      * @param group - P2P group to be added
62      * @return true: adding succeeded    false: adding failed
63      */
64     virtual bool AddGroup(const WifiP2pGroupInfo &group);
65 
66     /**
67      * @Description - Adding or Updating a P2P group.
68      * @param group - P2P group to be modified or add
69      * @return true: adding or Updating succeeded    false: adding or Updating failed
70      */
71     virtual bool AddOrUpdateGroup(const WifiP2pGroupInfo &group);
72 
73     /**
74      * @Description - Remove a P2P group.
75      * @param group - P2P group to be removed
76      * @return true: delete successfully    false: delete failed
77      */
78     virtual bool RemoveGroup(const WifiP2pGroupInfo &group);
79 
80     /**
81      * @Description - Clear all P2P groups.
82      * @param None
83      * @return int number of P2P groups to be deleted
84      */
85     virtual int ClearAll();
86 
87     /**
88      * @Description - Update the group information (except the current temporary group)
89                       in the latest WPA to the manager.
90      * @param  group - group recorded in WPA
91      */
92     virtual void UpdateWpaGroup(const WifiP2pGroupInfo &group);
93 
94     /**
95      * @Description - Delete a client from a specified group.
96      * @param  networkId - specified group ID
97      * @param  deviceAddress - client address to be deleted
98      * @return - int number of clients in the group
99      */
100     virtual int RemoveClientFromGroup(int networkId, const std::string &deviceAddress);
101 
102     /**
103      * @Description - Obtaining all groups.
104      * @param None
105      * @return const std::vector<WifiP2pGroupInfo> &
106      */
107     virtual const std::vector<WifiP2pGroupInfo> &GetGroups();
108 
109     /**
110      * @Description Query the first GC of the group to which the device belongs and returns the network ID of the group.
111      * @param  device - device corresponding to the GC of the group
112      * @return - int -1:not found    >=0:network ID of the corresponding group
113      */
114     virtual int GetNetworkIdFromClients(const WifiP2pDevice &device);
115 
116     /**
117      * @Description  Get the network id of the group owner profile with the specified p2p device address.
118      * @param device - P2P devices to be matched
119      * @return int -1: not found    >=0: network ID of the P2P device
120      */
121     virtual int GetGroupNetworkId(const WifiP2pDevice &device);
122 
123     /**
124      * @Description  Get the network id of the group owner profile with the
125                      specified p2p device address and the ssid.
126      * @param device - P2P devices to be matched
127      * @param ssid - specified network name
128      * @return int -1: not found    >=0: network ID of the P2P device
129      */
130     virtual int GetGroupNetworkId(const WifiP2pDevice &device, const std::string &ssid);
131 
132     /**
133      * @Description  Get the group owner mac address with the specified networkId.
134      * @param netId - specifies the network ID of the group
135      * @return std::string not null: MAC address of the group owner    null: not found
136      */
137     virtual std::string GetGroupOwnerAddr(int netId);
138 
139     /**
140      * @Description  Specifies whether the group with networkId is included in the group list.
141      * @param netId - specifies the network ID of a group
142      * @return true:contains    false:not contains
143      */
144     virtual bool IsInclude(int netId);
145 
146     /**
147      * @Description Store P2P group connection information.
148      *
149      * @param linkedInfo - group connection information
150      */
151     virtual void SaveP2pInfo(const WifiP2pLinkedInfo &linkedInfo);
152 
153     /**
154      * @Description Get P2P group connection information.
155      *
156      * @return const WifiP2pLinkedInfo& group connection information
157      */
158     virtual const WifiP2pLinkedInfo &GetP2pInfo() const;
159 
160     /**
161      * @Description Obtain the group information from the WPA, match and update the network ID.
162      *
163      * @param wpaGroups - groups information from WPA
164      */
165     virtual void UpdateGroupsNetwork(std::map<int, WifiP2pGroupInfo> wpaGroups);
166 
167 public:
168     /**
169      * @Description - Update the current group client.
170      * @param  device - clients to be updated
171      */
UpdateCurrGroupClient(const WifiP2pDevice & device)172     inline void UpdateCurrGroupClient(const WifiP2pDevice &device)
173     {
174         currentGroup.AddClientDevice(device);
175         if (currentGroup.IsPersistent()) {
176             currentGroup.AddPersistentDevice(device);
177         }
178     }
179 
RemoveCurrGroupClient(const WifiP2pDevice & device)180     inline void RemoveCurrGroupClient(const WifiP2pDevice &device)
181     {
182         currentGroup.RemoveClientDevice(device);
183     }
184 
IsCurrGroupClientEmpty(void)185     inline bool IsCurrGroupClientEmpty(void)
186     {
187         return currentGroup.IsClientDevicesEmpty();
188     }
189 
190     /**
191      * @Description set current group
192      *
193      * @param type wifi mac addr info type
194      * @param group wifi p2p group info
195      */
196     void SetCurrentGroup(WifiMacAddrInfoType type, const WifiP2pGroupInfo &group);
197 
GetCurrentGroup()198     inline const WifiP2pGroupInfoProxy &GetCurrentGroup() const
199     {
200         return currentGroup;
201     }
202 #ifdef SUPPORT_RANDOM_MAC_ADDR
203     /**
204      * @Description - add mac address information
205      * @param type - the MAC address type
206      * @param group - P2P group information
207      */
208     void AddMacAddrPairInfo(WifiMacAddrInfoType type, const WifiP2pGroupInfo &group);
209     /**
210      * @Description - remove mac address information
211      * @param type - the MAC address type
212      * @param group - P2P group information
213      */
214     void RemoveMacAddrPairInfo(WifiMacAddrInfoType type, const WifiP2pGroupInfo &group);
215 #endif
216     bool IsOldPersistentGroup(int id);
217 private:
218     /**
219      * @Description - Synchronize from the current group to all registry groups.
220      */
221     void RefreshGroupsFromCurrentGroup();
222 
223     /**
224      * @Description - From all record groups, update to the current group in use.
225      */
226     void RefreshCurrentGroupFromGroups();
227 
228 private:
229     std::vector<WifiP2pGroupInfo> groupsInfo;
230     WifiP2pGroupInfoProxy currentGroup;
231     std::mutex groupMutex;
232     WifiP2pLinkedInfo p2pConnInfo;      /* group connection information */
233 };
234 }  // namespace Wifi
235 }  // namespace OHOS
236 
237 #endif  // OHOS_P2P_GROUP_MANAGER_H
238