• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development 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 "wfd_trust_list_manager.h"
17 #include "common/const_def.h"
18 #include "common/sharing_log.h"
19 #include "wfd_utils.h"
20 
21 namespace OHOS {
22 namespace Sharing {
WfdTrustListManager()23 WfdTrustListManager::WfdTrustListManager()
24 {
25     p2pInstance_ = Wifi::WifiP2p::GetInstance(WIFI_P2P_ABILITY_ID);
26     preferencesUtil_ = std::make_shared<PreferencesUtil>(TRUST_DEVICES_PREFERENCES_PATH);
27 }
28 
GetAllBoundDevices()29 std::vector<BoundDeviceInfo> WfdTrustListManager::GetAllBoundDevices()
30 {
31     SHARING_LOGI("trace");
32     std::vector<BoundDeviceInfo> devices;
33     std::vector<Wifi::WifiP2pGroupInfo> groups;
34     if (p2pInstance_ == nullptr || preferencesUtil_ == nullptr) {
35         SHARING_LOGE("GetAllBoundDevices nullptr");
36         return devices;
37     }
38     p2pInstance_->QueryP2pGroups(groups);
39     std::map<std::string, BoundDeviceInfo> deviceMap;
40     for (Wifi::WifiP2pGroupInfo groupInfo : groups) {
41         if (groupInfo.IsGroupOwner()) {
42             std::vector<Wifi::WifiP2pDevice> clientDevices = groupInfo.GetClientDevices();
43             if (clientDevices.empty()) {
44                 SHARING_LOGE("clientDevices empty");
45                 continue;
46             }
47             for (Wifi::WifiP2pDevice device : clientDevices) {
48                 AddBoundDeviceItem(deviceMap, groupInfo, MaskAddress(device.GetDeviceAddress()));
49             }
50         } else {
51             AddBoundDeviceItem(deviceMap, groupInfo, MaskAddress(groupInfo.GetOwner().GetDeviceAddress()));
52         }
53     }
54     for (auto device : deviceMap) {
55         devices.push_back(device.second);
56     }
57     return devices;
58 }
59 
AddBoundDeviceItem(std::map<std::string,BoundDeviceInfo> & deviceMap,const Wifi::WifiP2pGroupInfo group,std::string encryptedMac)60 void WfdTrustListManager::AddBoundDeviceItem(std::map<std::string, BoundDeviceInfo> &deviceMap,
61                                              const Wifi::WifiP2pGroupInfo group, std::string encryptedMac)
62 {
63     if (encryptedMac.empty()) {
64         SHARING_LOGE("encryptedMac empty");
65         return;
66     }
67     if (deviceMap.find(encryptedMac) != deviceMap.end()) {
68         return;
69     }
70     std::string name = preferencesUtil_->GetString(encryptedMac);
71     if (name.empty()) {
72         return;
73     }
74     BoundDeviceInfo deviceInfo;
75     deviceInfo.deviceId = encryptedMac;
76     deviceInfo.deviceAddress = encryptedMac;
77     deviceInfo.deviceName = name;
78     deviceInfo.networkId = group.GetNetworkId();
79     deviceMap.emplace(encryptedMac, deviceInfo);
80 }
81 
AddBoundDevice(const Wifi::WifiP2pGroupInfo & group)82 void WfdTrustListManager::AddBoundDevice(const Wifi::WifiP2pGroupInfo &group)
83 {
84     SHARING_LOGI("trace");
85     if (group.IsGroupOwner()) {
86         std::vector<Wifi::WifiP2pDevice> clientDevices = group.GetClientDevices();
87         if (clientDevices.empty()) {
88             SHARING_LOGE("clientDevices empty");
89             return;
90         }
91         for (Wifi::WifiP2pDevice device : clientDevices) {
92             saveBoundDevice(device);
93         }
94     } else {
95         saveBoundDevice(group.GetOwner());
96     }
97 }
98 
saveBoundDevice(const Wifi::WifiP2pDevice & device)99 void WfdTrustListManager::saveBoundDevice(const Wifi::WifiP2pDevice &device)
100 {
101     std::string deviceName = GetDeviceName(device);
102     if (!deviceName.empty()) {
103         preferencesUtil_->PutString(MaskAddress(device.GetDeviceAddress()), deviceName);
104     }
105 }
106 
GetDeviceName(const Wifi::WifiP2pDevice & device)107 std::string WfdTrustListManager::GetDeviceName(const Wifi::WifiP2pDevice &device)
108 {
109     if (!device.GetDeviceName().empty()) {
110         return device.GetDeviceName();
111     }
112     std::string networkName = device.GetNetworkName();
113     if (networkName.empty()) {
114         return "";
115     }
116     auto splits = Split(networkName, "-");
117     if (splits.size() < MIN_DEVICE_NAME_LEN) {
118         return networkName;
119     }
120     auto index = networkName.find(splits[DEVICE_NAME_INDEX]);
121     if (index != std::string::npos) {
122         return networkName.substr(index);
123     }
124     return "";
125 }
126 
DeleteBoundDeviceGroup(std::string & deviceAddress)127 int32_t WfdTrustListManager::DeleteBoundDeviceGroup(std::string &deviceAddress)
128 {
129     SHARING_LOGI("trace");
130     std::vector<Wifi::WifiP2pGroupInfo> groups;
131     if (p2pInstance_ == nullptr || preferencesUtil_ == nullptr) {
132         SHARING_LOGE("GetAllBoundDevices nullptr");
133         return OPERATE_ERR;
134     }
135     int32_t result = OPERATE_OK;
136     p2pInstance_->QueryP2pGroups(groups);
137     for (Wifi::WifiP2pGroupInfo groupInfo : groups) {
138         if (!groupInfo.IsGroupOwner()) {
139             if (MaskAddress(groupInfo.GetOwner().GetDeviceAddress()) != deviceAddress) {
140                 continue;
141             }
142             if (!DeleteP2pGroup(groupInfo.GetOwner(), deviceAddress, groupInfo)) {
143                 result = OPERATE_ERR;
144             }
145             continue;
146         }
147         std::vector<Wifi::WifiP2pDevice> clientDevices = groupInfo.GetClientDevices();
148         if (clientDevices.empty()) {
149             SHARING_LOGW("clientDevices empty");
150             continue;
151         }
152         for (Wifi::WifiP2pDevice device : clientDevices) {
153             if (MaskAddress(device.GetDeviceAddress()) != deviceAddress) {
154                 continue;
155             }
156             if (!DeleteP2pGroup(device, deviceAddress, groupInfo)) {
157                 result = OPERATE_ERR;
158             }
159         }
160     }
161     return result;
162 }
163 
DeleteP2pGroup(const Wifi::WifiP2pDevice & device,std::string & deviceAddress,const Wifi::WifiP2pGroupInfo & groupInfo)164 bool WfdTrustListManager::DeleteP2pGroup(const Wifi::WifiP2pDevice &device, std::string &deviceAddress,
165                                          const Wifi::WifiP2pGroupInfo &groupInfo)
166 {
167     Wifi::ErrCode errCode = p2pInstance_->DeleteGroup(groupInfo);
168     if (errCode == Wifi::WIFI_OPT_SUCCESS) {
169         preferencesUtil_->DeleteKey(deviceAddress);
170         return true;
171     } else {
172         SHARING_LOGE("DeleteP2pGroup fail, errCode = %{public}d", errCode);
173         return false;
174     }
175 }
176 
Split(const std::string & str,const std::string & delimiter)177 std::vector<std::string> WfdTrustListManager::Split(const std::string &str, const std::string &delimiter)
178 {
179     std::regex reg{delimiter};
180     return {std::sregex_token_iterator(str.begin(), str.end(), reg, -1), std::sregex_token_iterator()};
181 }
182 
MaskAddress(const std::string & address)183 std::string WfdTrustListManager::MaskAddress(const std::string &address)
184 {
185     if (address.empty()) {
186         return "";
187     }
188     std::string addressStr = std::string(address);
189     return addressStr.replace(HASH_ADDRESS_SUB_START, HASH_ADDRESS_SUB_LENGTH, "****");
190 }
191 
192 } // namespace Sharing
193 } // namespace OHOS