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 #include "wifi_p2p_device_manager.h"
16 #include "wifi_logger.h"
17 #include "wifi_config_center.h"
18 #include "wifi_permission_utils.h"
19 #include "wifi_log.h"
20
21 namespace OHOS {
22 namespace Wifi {
Initialize()23 void WifiP2pDeviceManager::Initialize()
24 {}
25
AddDevice(const WifiP2pDevice & device)26 bool WifiP2pDeviceManager::AddDevice(const WifiP2pDevice &device)
27 {
28 if (!device.IsValid()) {
29 LOGE("WifiP2pDeviceManager::AddDevice: invalid address");
30 return false;
31 }
32 std::unique_lock<std::mutex> lock(deviceMutex);
33 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
34 if (*it == device) {
35 LOGE("WifiP2pDeviceManager::AddDevice: device is existed");
36 return false;
37 }
38 }
39 LOGI("add a device: name:%{private}s, address:%{private}s, addressType:%{public}d",
40 device.GetDeviceName().c_str(), device.GetDeviceAddress().c_str(),
41 device.GetDeviceAddressType());
42 #ifdef SUPPORT_RANDOM_MAC_ADDR
43 LOGD("%{public}s: add a device, deviceName: %{private}s, address: %{private}s",
44 __func__, device.GetDeviceName().c_str(), device.GetDeviceAddress().c_str());
45 WifiConfigCenter::GetInstance().StoreWifiMacAddrPairInfo(WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO,
46 device.GetDeviceAddress(), "");
47 #endif
48 p2pDevices.push_back(device);
49 return true;
50 }
51
RemoveDevice(const std::string & deviceAddress)52 bool WifiP2pDeviceManager::RemoveDevice(const std::string &deviceAddress)
53 {
54 std::unique_lock<std::mutex> lock(deviceMutex);
55 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
56 if (it->GetDeviceAddress() == deviceAddress) {
57 p2pDevices.erase(it);
58 LOGI("remove a device: address:%{private}s", deviceAddress.c_str());
59 return true;
60 }
61 }
62 return false;
63 }
64
RemoveDevice(const WifiP2pDevice & device)65 bool WifiP2pDeviceManager::RemoveDevice(const WifiP2pDevice &device)
66 {
67 return RemoveDevice(device.GetDeviceAddress());
68 }
69
ClearAll()70 int WifiP2pDeviceManager::ClearAll()
71 {
72 std::unique_lock<std::mutex> lock(deviceMutex);
73 int num = static_cast<int>(p2pDevices.size());
74 #ifdef SUPPORT_RANDOM_MAC_ADDR
75 WifiConfigCenter::GetInstance().ClearMacAddrPairs(WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO);
76 #endif
77 p2pDevices.clear();
78 LOGI("WifiP2pDeviceManager::ClearAll: clear all address");
79 return num;
80 }
81
GetDevicesList(std::vector<WifiP2pDevice> & devices)82 int WifiP2pDeviceManager::GetDevicesList(std::vector<WifiP2pDevice> &devices)
83 {
84 std::unique_lock<std::mutex> lock(deviceMutex);
85 devices.assign(p2pDevices.begin(), p2pDevices.end());
86 return p2pDevices.size();
87 }
88
UpdateDevice(const WifiP2pDevice & device)89 bool WifiP2pDeviceManager::UpdateDevice(const WifiP2pDevice &device)
90 {
91 if (!device.IsValid()) {
92 LOGE("WifiP2pDeviceManager::UpdateDevice: invalid address");
93 return false;
94 }
95 std::unique_lock<std::mutex> lock(deviceMutex);
96 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
97 if (*it == device) {
98 *it = device;
99 return true;
100 }
101 }
102 #ifdef SUPPORT_RANDOM_MAC_ADDR
103 LOGD("%{public}s: update a device, deviceName: %{private}s, address: %{private}s",
104 __func__, device.GetDeviceName().c_str(), device.GetDeviceAddress().c_str());
105 WifiConfigCenter::GetInstance().StoreWifiMacAddrPairInfo(WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO,
106 device.GetDeviceAddress(), "");
107 #endif
108 p2pDevices.push_back(device);
109 return true;
110 }
111
UpdateDeviceSupplicantInf(const WifiP2pDevice & device)112 bool WifiP2pDeviceManager::UpdateDeviceSupplicantInf(const WifiP2pDevice &device)
113 {
114 if (!device.IsValid()) {
115 LOGE("UpdateDeviceSupplicantInf: invalid address");
116 return false;
117 }
118 std::unique_lock<std::mutex> lock(deviceMutex);
119 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
120 if (*it == device) {
121 it->SetDeviceName(device.GetDeviceName());
122 it->SetDeviceAddressType(REAL_DEVICE_ADDRESS);
123 it->SetPrimaryDeviceType(device.GetPrimaryDeviceType());
124 it->SetSecondaryDeviceType(device.GetSecondaryDeviceType());
125 it->SetWpsConfigMethod(device.GetWpsConfigMethod());
126 it->SetDeviceCapabilitys(device.GetDeviceCapabilitys());
127 it->SetGroupCapabilitys(device.GetGroupCapabilitys());
128 return true;
129 }
130 }
131 WifiP2pDevice updateDevice = device;
132 #ifdef SUPPORT_RANDOM_MAC_ADDR
133 LOGD("%{public}s: receive a event, deviceName: %{private}s, address: %{private}s",
134 __func__, device.GetDeviceName().c_str(), device.GetDeviceAddress().c_str());
135 WifiConfigCenter::GetInstance().StoreWifiMacAddrPairInfo(WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO,
136 device.GetDeviceAddress(), "");
137 #endif
138 /* add its if not found . be careful of the return value */
139 p2pDevices.push_back(updateDevice);
140 return true;
141 }
142
UpdateDeviceGroupCap(const std::string & deviceAddress,uint32_t cap)143 bool WifiP2pDeviceManager::UpdateDeviceGroupCap(const std::string &deviceAddress, uint32_t cap)
144 {
145 if (deviceAddress.empty()) {
146 LOGE("WifiP2pDeviceManager::UpdateDeviceGroupCap: invalid address");
147 return false;
148 }
149 std::unique_lock<std::mutex> lock(deviceMutex);
150 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
151 if (it->GetDeviceAddress() == deviceAddress) {
152 it->SetGroupCapabilitys(cap);
153 return true;
154 }
155 }
156 return false;
157 }
158
UpdateDeviceGroupCap(const WifiP2pDevice & device)159 bool WifiP2pDeviceManager::UpdateDeviceGroupCap(const WifiP2pDevice &device)
160 {
161 if (!device.IsValid()) {
162 LOGE("WifiP2pDeviceManager::UpdateDeviceGroupCap: invalid address");
163 return false;
164 }
165
166 return UpdateDeviceGroupCap(device.GetDeviceAddress(), device.GetGroupCapabilitys());
167 }
168
UpdateDeviceStatus(const std::string & deviceAddress,P2pDeviceStatus status)169 bool WifiP2pDeviceManager::UpdateDeviceStatus(const std::string &deviceAddress, P2pDeviceStatus status)
170 {
171 if (deviceAddress.empty()) {
172 LOGE("WifiP2pDeviceManager::UpdateDeviceStatus: invalid address");
173 return false;
174 }
175
176 std::unique_lock<std::mutex> lock(deviceMutex);
177 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
178 if (it->GetDeviceAddress() == deviceAddress) {
179 it->SetP2pDeviceStatus(status);
180 return true;
181 }
182 }
183 return false;
184 }
185
UpdateDeviceStatus(const WifiP2pDevice & device)186 bool WifiP2pDeviceManager::UpdateDeviceStatus(const WifiP2pDevice &device)
187 {
188 if (!device.IsValid()) {
189 LOGE("WifiP2pDeviceManager::UpdateDeviceStatus: invalid address");
190 return false;
191 }
192
193 return UpdateDeviceStatus(device.GetDeviceAddress(), device.GetP2pDeviceStatus());
194 }
195
UpdateAllDeviceStatus(const P2pDeviceStatus status)196 bool WifiP2pDeviceManager::UpdateAllDeviceStatus(const P2pDeviceStatus status)
197 {
198 std::unique_lock<std::mutex> lock(deviceMutex);
199 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
200 it->SetP2pDeviceStatus(status);
201 }
202 return true;
203 }
204
UpdateGroupAddress(const std::string & deviceAddress,const std::string & groupAddress)205 bool WifiP2pDeviceManager::UpdateGroupAddress(const std::string &deviceAddress, const std::string &groupAddress)
206 {
207 if (deviceAddress.empty() || groupAddress.empty()) {
208 return false;
209 }
210
211 std::unique_lock<std::mutex> lock(deviceMutex);
212 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
213 if (it->GetDeviceAddress() == deviceAddress) {
214 it->SetGroupAddress(groupAddress);
215 return true;
216 }
217 }
218 return false;
219 }
220
UpdateGroupAddress(const WifiP2pDevice & device)221 bool WifiP2pDeviceManager::UpdateGroupAddress(const WifiP2pDevice &device)
222 {
223 if (!device.IsValid()) {
224 return false;
225 }
226
227 return UpdateGroupAddress(device.GetDeviceAddress(), device.GetGroupAddress());
228 }
229
GetDevices(const std::string & deviceAddress)230 WifiP2pDevice WifiP2pDeviceManager::GetDevices(const std::string &deviceAddress)
231 {
232 std::unique_lock<std::mutex> lock(deviceMutex);
233 for (auto it = p2pDevices.begin(); it != p2pDevices.end(); it++) {
234 if (it->GetDeviceAddress() == deviceAddress) {
235 return *it;
236 }
237 }
238 WifiP2pDevice ret;
239 return ret;
240 }
241
GetDeviceName(const std::string & deviceAddress)242 const std::string WifiP2pDeviceManager::GetDeviceName(const std::string &deviceAddress)
243 {
244 WifiP2pDevice device = GetDevices(deviceAddress);
245 return device.IsValid() ? device.GetDeviceName() : deviceAddress;
246 }
247 } // namespace Wifi
248 } // namespace OHOS
249