1 /*
2  * Copyright (c) 2024 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 #include <fcntl.h>
17 #include <net/if.h>
18 #include <sys/stat.h>
19 #include <sys/sysmacros.h>
20 #include <cstring>
21 #include "wifi_chip.h"
22 #include "parameter.h"
23 #include "wifi_hal.h"
24 #include <hdf_log.h>
25 
26 namespace OHOS {
27 namespace HDI {
28 namespace Wlan {
29 namespace Chip {
30 namespace V1_0 {
31 constexpr int IFACE_TYPE_STA = 2;
32 constexpr char K_ACTIVE_WLAN_IFACE_NAME_PROPERTY[] = "wifi.active.interface";
33 constexpr char K_NO_ACTIVE_WLAN_IFACE_NAME_PROPERTY_VALUE[] = "";
34 constexpr unsigned K_MAX_WLAN_IFACES = 5;
35 const std::string AP_CODEX_DEFAULT_IFACENAME = "wlan1";
36 
InvalidateAndClearApIface(std::vector<sptr<WifiApIface>> & ifaces)37 void InvalidateAndClearApIface(std::vector<sptr<WifiApIface>>& ifaces)
38 {
39     for (const auto& iface : ifaces) {
40         iface->Invalidate();
41     }
42     ifaces.clear();
43 }
44 
InvalidateAndClearStaIface(std::vector<sptr<WifiStaIface>> & ifaces)45 void InvalidateAndClearStaIface(std::vector<sptr<WifiStaIface>>& ifaces)
46 {
47     for (const auto& iface : ifaces) {
48         iface->Invalidate();
49     }
50     ifaces.clear();
51 }
52 
InvalidateAndClearP2pIface(std::vector<sptr<WifiP2pIface>> & ifaces)53 void InvalidateAndClearP2pIface(std::vector<sptr<WifiP2pIface>>& ifaces)
54 {
55     for (const auto& iface : ifaces) {
56         iface->Invalidate();
57     }
58     ifaces.clear();
59 }
60 
WifiChip(int32_t chipId,bool isPrimary,const std::weak_ptr<WifiVendorHal> vendorHal,const std::shared_ptr<IfaceUtil> ifaceUtil,const std::function<void (const std::string &)> & handler)61 WifiChip::WifiChip(
62     int32_t chipId, bool isPrimary,
63     const std::weak_ptr<WifiVendorHal> vendorHal,
64     const std::shared_ptr<IfaceUtil> ifaceUtil,
65     const std::function<void(const std::string&)>& handler)
66     : chipId_(chipId),
67     vendorHal_(vendorHal),
68     isValid_(true),
69     currentModeId_(chip_mode_ids::K_INVALID),
70     ifaceUtil_(ifaceUtil),
71     subsystemCallbackHandler_(handler)
72 {}
73 
~WifiChip()74 WifiChip::~WifiChip()
75 {}
76 
Invalidate()77 void WifiChip::Invalidate()
78 {
79     InvalidateAndClearApIface(apIfaces_);
80     InvalidateAndClearP2pIface(p2pIfaces_);
81     InvalidateAndClearStaIface(staIfaces_);
82     SetParameter(K_ACTIVE_WLAN_IFACE_NAME_PROPERTY, K_NO_ACTIVE_WLAN_IFACE_NAME_PROPERTY_VALUE);
83     vendorHal_.reset();
84     cbHandler_.Invalidate();
85     isValid_ = false;
86 }
87 
GetChipId(int32_t & id)88 int32_t WifiChip::GetChipId(int32_t& id)
89 {
90     id = chipId_;
91     return HDF_SUCCESS;
92 }
93 
RegisterChipEventCallback(const sptr<IConcreteChipCallback> & chipEventcallback)94 int32_t WifiChip::RegisterChipEventCallback(const sptr<IConcreteChipCallback>& chipEventcallback)
95 {
96     if (!cbHandler_.AddCallback(chipEventcallback)) {
97         return HDF_FAILURE;
98     }
99     return HDF_SUCCESS;
100 }
101 
GetWlanIfaceName(unsigned idx)102 std::string GetWlanIfaceName(unsigned idx)
103 {
104     if (idx >= K_MAX_WLAN_IFACES) {
105         HDF_LOGI("Requested interface beyond wlan%{public}d", K_MAX_WLAN_IFACES);
106         return "";
107     }
108     return "wlan" + std::to_string(idx);
109 }
110 
GetIfaceName(IfaceType type,unsigned idx)111 std::string WifiChip::GetIfaceName(IfaceType type, unsigned idx)
112 {
113     return GetWlanIfaceName(idx);
114 }
115 
GetUsedIfaceName()116 std::string WifiChip::GetUsedIfaceName()
117 {
118     if (staIfaces_.size() > 0) return staIfaces_[0]->GetName();
119     if (apIfaces_.size() > 0) {
120         return apIfaces_[0]->GetName();
121     }
122     HDF_LOGI("No active wlan interfaces in use! Using default");
123     return GetIfaceName(IfaceType::STA, 0);
124 }
125 
GetChipCaps(uint32_t & capabilities)126 int32_t WifiChip::GetChipCaps(uint32_t& capabilities)
127 {
128     WifiError status;
129 
130     const auto ifname = GetUsedIfaceName();
131     status = vendorHal_.lock()->GetChipCaps(ifname, capabilities);
132     if (status != HAL_SUCCESS) {
133         return HDF_FAILURE;
134     } else {
135         return HDF_SUCCESS;
136     }
137 }
138 
GetChipModes(std::vector<UsableMode> & modes)139 int32_t WifiChip::GetChipModes(std::vector<UsableMode>& modes)
140 {
141     auto chipModes = std::make_shared<WifiChipModes>(vendorHal_);
142     modes = chipModes->GetChipModes(true);
143     return HDF_SUCCESS;
144 }
145 
IsValidModeId(uint32_t modeId)146 bool WifiChip::IsValidModeId(uint32_t modeId)
147 {
148     std::vector<UsableMode> modes;
149     auto chipModes = std::make_shared<WifiChipModes>(vendorHal_);
150     modes = chipModes->GetChipModes(true);
151     for (const auto& mode : modes) {
152         if (mode.modeId == modeId) {
153             return true;
154         }
155     }
156     return false;
157 }
158 
GetCurrentMode(uint32_t & modeId)159 int32_t WifiChip::GetCurrentMode(uint32_t& modeId)
160 {
161     if (!IsValidModeId(currentModeId_)) {
162         return HDF_ERR_INVALID_PARAM;
163     }
164     modeId = currentModeId_;
165     return HDF_SUCCESS;
166 }
167 
SetChipMode(uint32_t modeId)168 int32_t WifiChip::SetChipMode(uint32_t modeId)
169 {
170     if (!IsValidModeId(modeId)) {
171         return HDF_FAILURE;
172     }
173     if (modeId == currentModeId_) {
174         HDF_LOGI("Already in the specified mode, modeId: %{public}d", modeId);
175         return HDF_SUCCESS;
176     }
177     int32_t status = HandleChipConfiguration(modeId);
178     if (status != ErrorCode::SUCCESS) {
179         return HDF_FAILURE;
180     }
181     currentModeId_ = modeId;
182     HDF_LOGI("Configured chip in mode, modeId: %{public}d", modeId);
183     SetUsedIfaceNameProperty(GetUsedIfaceName());
184     vendorHal_.lock()->RegisterRestartCallback(subsystemCallbackHandler_);
185     return HDF_SUCCESS;
186 }
187 
HandleChipConfiguration(int32_t modeId)188 int32_t WifiChip::HandleChipConfiguration(int32_t modeId)
189 {
190     std::unique_lock<std::recursive_mutex> lock = AcquireGlobalLock();
191     if (IsValidModeId(currentModeId_)) {
192         HDF_LOGI("Reconfiguring chip from mode %{public}d to mode %{public}d", currentModeId_, modeId);
193         InvalidateAndClearApIface(apIfaces_);
194         InvalidateAndClearP2pIface(p2pIfaces_);
195         InvalidateAndClearStaIface(staIfaces_);
196         WifiError status = vendorHal_.lock()->Stop(&lock, []() {});
197         if (status != HAL_SUCCESS) {
198             HDF_LOGE("Failed to stop vendor HAL: %{public}d", status);
199             return HDF_FAILURE;
200         }
201     }
202     WifiError status = vendorHal_.lock()->Start();
203     if (status != HAL_SUCCESS) {
204         HDF_LOGE("Failed to start vendor HAL: %{public}d", status);
205         return HDF_FAILURE;
206     }
207     return HDF_SUCCESS;
208 }
209 
ExpandIfaceCombinations(const ComboIface & combination)210 std::vector<std::map<IfaceType, size_t>> WifiChip::ExpandIfaceCombinations(
211     const ComboIface& combination)
212 {
213     uint32_t numExpandedCombos = 1;
214     for (const auto& limit : combination.limits) {
215         for (uint32_t i = 0; i < limit.ifaceNum; i++) {
216             numExpandedCombos *= limit.types.size();
217         }
218     }
219 
220     std::vector<std::map<IfaceType, size_t>> expandedCombos;
221     expandedCombos.resize(numExpandedCombos);
222     for (auto& expandedCombo : expandedCombos) {
223         for (const auto type : {
224             IfaceType::AP, IfaceType::P2P, IfaceType::STA
225         }) {
226             expandedCombo[type] = 0;
227         }
228     }
229     uint32_t span = numExpandedCombos;
230     for (const auto& limit : combination.limits) {
231         for (uint32_t i = 0; i < limit.ifaceNum; i++) {
232             span /= limit.types.size();
233             for (uint32_t k = 0; k < numExpandedCombos; ++k) {
234                 const auto ifaceType =
235                     limit.types[(k / span) % limit.types.size()];
236                 expandedCombos[k][ifaceType]++;
237             }
238         }
239     }
240     return expandedCombos;
241 }
242 
GetCurrentIfaceCombo()243 std::map<IfaceType, size_t> WifiChip::GetCurrentIfaceCombo()
244 {
245     std::map<IfaceType, size_t> iface_counts;
246     iface_counts[IfaceType::AP] = apIfaces_.size();
247     iface_counts[IfaceType::P2P] = p2pIfaces_.size();
248     iface_counts[IfaceType::STA] = staIfaces_.size();
249     return iface_counts;
250 }
251 
GetCurrentCombinations()252 std::vector<ComboIface> WifiChip::GetCurrentCombinations()
253 {
254     if (!IsValidModeId(currentModeId_)) {
255         HDF_LOGE("Chip not configured in a mode yet");
256         return {};
257     }
258     std::vector<UsableMode> modes;
259     auto chipModes = std::make_shared<WifiChipModes>(vendorHal_);
260     modes = chipModes->GetChipModes(true);
261     for (const auto& mode : modes) {
262         if (mode.modeId == currentModeId_) {
263             return mode.usableCombo;
264         }
265     }
266     HDF_LOGE("not find iface combinations for current mode!");
267     return {};
268 }
269 
270 
CanExpandedIfaceSupportIfaceType(const std::map<IfaceType,size_t> & expandedCombo,IfaceType type)271 bool WifiChip::CanExpandedIfaceSupportIfaceType(
272     const std::map<IfaceType, size_t>& expandedCombo, IfaceType type)
273 {
274     const auto currentCombo = GetCurrentIfaceCombo();
275     for (const auto ifaceType : {IfaceType::AP, IfaceType::P2P, IfaceType::STA}) {
276         size_t numIfacesNeeded = currentCombo.at(ifaceType);
277         if (ifaceType == type) {
278             numIfacesNeeded++;
279         }
280         size_t numIfacesAllowed = expandedCombo.at(ifaceType);
281         if (numIfacesNeeded > numIfacesAllowed) {
282             return false;
283         }
284     }
285     return true;
286 }
287 
CanSupportIfaceType(IfaceType type)288 bool WifiChip::CanSupportIfaceType(IfaceType type)
289 {
290     if (!IsValidModeId(currentModeId_)) {
291         HDF_LOGE("Chip not configured in a mode yet");
292         return false;
293     }
294     const auto combinations = GetCurrentCombinations();
295     for (const auto& combination : combinations) {
296         const auto expandedCombos = ExpandIfaceCombinations(combination);
297         for (const auto& expandedCombo : expandedCombos) {
298             if (CanExpandedIfaceSupportIfaceType(expandedCombo, type)) {
299                 return true;
300             }
301         }
302     }
303     return false;
304 }
305 
GetApNames(std::vector<sptr<WifiApIface>> & ifaces)306 std::vector<std::string> GetApNames(std::vector<sptr<WifiApIface>>& ifaces)
307 {
308     std::vector<std::string> names;
309     for (const auto& iface : ifaces) {
310         names.emplace_back(iface->GetName());
311     }
312     return names;
313 }
314 
FindApUsingName(std::vector<sptr<WifiApIface>> & ifaces,const std::string & name)315 sptr<WifiApIface> FindApUsingName(std::vector<sptr<WifiApIface>>& ifaces, const std::string& name)
316 {
317     std::vector<std::string> names;
318     for (const auto& iface : ifaces) {
319         if (name == iface->GetName()) {
320             return iface;
321         }
322     }
323     return nullptr;
324 }
325 
GetP2pNames(std::vector<sptr<WifiP2pIface>> & ifaces)326 std::vector<std::string> GetP2pNames(std::vector<sptr<WifiP2pIface>>& ifaces)
327 {
328     std::vector<std::string> names;
329     for (const auto& iface : ifaces) {
330         names.emplace_back(iface->GetName());
331     }
332     return names;
333 }
334 
FindP2pUsingName(std::vector<sptr<WifiP2pIface>> & ifaces,const std::string & name)335 sptr<WifiP2pIface> FindP2pUsingName(std::vector<sptr<WifiP2pIface>>& ifaces, const std::string& name)
336 {
337     std::vector<std::string> names;
338     for (const auto& iface : ifaces) {
339         if (name == iface->GetName()) {
340             return iface;
341         }
342     }
343     return nullptr;
344 }
345 
GetStaNames(std::vector<sptr<WifiStaIface>> & ifaces)346 std::vector<std::string> GetStaNames(std::vector<sptr<WifiStaIface>>& ifaces)
347 {
348     std::vector<std::string> names;
349     for (const auto& iface : ifaces) {
350         names.emplace_back(iface->GetName());
351     }
352     return names;
353 }
354 
FindStaUsingName(std::vector<sptr<WifiStaIface>> & ifaces,const std::string & name)355 sptr<WifiStaIface> FindStaUsingName(std::vector<sptr<WifiStaIface>>& ifaces, const std::string& name)
356 {
357     std::vector<std::string> names;
358     for (const auto& iface : ifaces) {
359         if (name == iface->GetName()) {
360             return iface;
361         }
362     }
363     return nullptr;
364 }
365 
AllocIfaceName(IfaceType type,uint32_t startIdx)366 std::string WifiChip::AllocIfaceName(IfaceType type, uint32_t startIdx)
367 {
368     HDF_LOGI("%{public}s: enter AllocIfaceName", __FUNCTION__);
369     for (unsigned idx = startIdx; idx < K_MAX_WLAN_IFACES; idx++) {
370         const auto ifname = GetIfaceName(type, idx);
371         if (FindApUsingName(apIfaces_, ifname)) {
372             continue;
373         }
374         if (FindStaUsingName(staIfaces_, ifname)) {
375             continue;
376         }
377         return ifname;
378     }
379     HDF_LOGE("All wlan interfaces in use already!");
380     return {};
381 }
382 
CanExpandedIfaceComboSupportIfaceCombo(const std::map<IfaceType,size_t> & expandedCombo,const std::map<IfaceType,size_t> & reqCombo)383 bool WifiChip::CanExpandedIfaceComboSupportIfaceCombo(
384     const std::map<IfaceType, size_t>& expandedCombo,
385     const std::map<IfaceType, size_t>& reqCombo)
386 {
387     for (const auto type : {
388         IfaceType::AP, IfaceType::P2P, IfaceType::STA
389     }) {
390         if (reqCombo.count(type) == 0) {
391             continue;
392         }
393         size_t numIfacesNeeded = reqCombo.at(type);
394         size_t numIfacesAllowed = expandedCombo.at(type);
395         if (numIfacesNeeded > numIfacesAllowed) {
396             return false;
397         }
398     }
399     return true;
400 }
401 
CanCurrentModeSupportIfaceCombo(const std::map<IfaceType,size_t> & reqCombo)402 bool WifiChip::CanCurrentModeSupportIfaceCombo(
403     const std::map<IfaceType, size_t>& reqCombo)
404 {
405     if (!IsValidModeId(currentModeId_)) {
406         HDF_LOGE("Chip not configured in a mode yet");
407         return false;
408     }
409     const auto combinations = GetCurrentCombinations();
410     for (const auto& combination : combinations) {
411         const auto expandedCombos = ExpandIfaceCombinations(combination);
412         for (const auto& expandedCombo : expandedCombos) {
413             if (CanExpandedIfaceComboSupportIfaceCombo(expandedCombo, reqCombo)) {
414                 return true;
415             }
416         }
417     }
418     return false;
419 }
420 
IsDualStaSupportInCurrentMode()421 bool WifiChip::IsDualStaSupportInCurrentMode()
422 {
423     std::map<IfaceType, size_t> reqIfaceCombo;
424     reqIfaceCombo[IfaceType::STA] = IFACE_TYPE_STA;
425     return CanCurrentModeSupportIfaceCombo(reqIfaceCombo);
426 }
427 
IsStaApCoexInCurrentMode()428 bool WifiChip::IsStaApCoexInCurrentMode()
429 {
430     std::map<IfaceType, size_t> reqIfaceCombo;
431     reqIfaceCombo[IfaceType::AP] = 1;
432     reqIfaceCombo[IfaceType::STA] = 1;
433     return CanCurrentModeSupportIfaceCombo(reqIfaceCombo);
434 }
435 
IdxOfApIface()436 uint32_t WifiChip::IdxOfApIface()
437 {
438     if (IsDualStaSupportInCurrentMode()) {
439         return IFACE_TYPE_STA;
440     } else if (IsStaApCoexInCurrentMode()) {
441         return 1;
442     }
443     return 0;
444 }
445 
AllocateApIfaceName()446 std::string WifiChip::AllocateApIfaceName()
447 {
448     bool isCoex;
449     vendorHal_.lock()->IsSupportCoex(isCoex);
450     if (isCoex) {
451         return AP_CODEX_DEFAULT_IFACENAME;
452     }
453     return AllocIfaceName(IfaceType::AP, IdxOfApIface());
454 }
455 
SetUsedIfaceNameProperty(const std::string & ifname)456 void WifiChip::SetUsedIfaceNameProperty(const std::string& ifname)
457 {
458     int res = SetParameter(K_ACTIVE_WLAN_IFACE_NAME_PROPERTY, ifname.c_str());
459     if (res != 0) {
460         HDF_LOGE("Failed to set active wlan iface name property");
461     }
462 }
463 
NewApIface(std::string & ifname)464 sptr<WifiApIface> WifiChip::NewApIface(std::string& ifname)
465 {
466     std::vector<std::string> ap_instances;
467     sptr<WifiApIface> iface =
468         new WifiApIface(ifname, ap_instances, vendorHal_, ifaceUtil_);
469     apIfaces_.push_back(iface);
470     SetUsedIfaceNameProperty(GetUsedIfaceName());
471     return iface;
472 }
473 
CreateVirtualApInterface(const std::string & apVirtIf)474 int32_t WifiChip::CreateVirtualApInterface(const std::string& apVirtIf)
475 {
476     WifiError status = vendorHal_.lock()->CreateVirtualInterface(
477         apVirtIf, HalIfaceType::HAL_TYPE_AP);
478     if (status != WifiError::HAL_SUCCESS) {
479         HDF_LOGE("Failed to add interface: %{public}s, error: %{public}d", apVirtIf.c_str(), status);
480         return HDF_FAILURE;
481     }
482     return HDF_SUCCESS;
483 }
484 
CreateApService(sptr<OHOS::HDI::Wlan::Chip::V1_0::IChipIface> & iface)485 int32_t WifiChip::CreateApService(sptr<OHOS::HDI::Wlan::Chip::V1_0::IChipIface>& iface)
486 {
487     if (!CanSupportIfaceType(IfaceType::AP)) {
488         return HDF_FAILURE;
489     }
490     std::string ifname = AllocateApIfaceName();
491     int32_t status = CreateVirtualApInterface(ifname);
492     if (status != HDF_SUCCESS) {
493         return HDF_FAILURE;
494     }
495     iface = NewApIface(ifname);
496     return HDF_SUCCESS;
497 }
498 
GetApServiceIfNames(std::vector<std::string> & ifnames)499 int32_t WifiChip::GetApServiceIfNames(std::vector<std::string>& ifnames)
500 {
501     if (apIfaces_.empty()) {
502         return HDF_FAILURE;
503     }
504     ifnames = GetApNames(apIfaces_);
505     return HDF_SUCCESS;
506 }
507 
GetApService(const std::string & ifname,sptr<IChipIface> & iface)508 int32_t WifiChip::GetApService(const std::string& ifname, sptr<IChipIface>& iface)
509 {
510     iface = FindApUsingName(apIfaces_, ifname);
511     if (iface == nullptr) {
512         return HDF_FAILURE;
513     }
514     return HDF_SUCCESS;
515 }
516 
RemoveApService(const std::string & ifname)517 int32_t WifiChip::RemoveApService(const std::string& ifname)
518 {
519     const auto iface = FindApUsingName(apIfaces_, ifname);
520     if (iface == nullptr) {
521         return HDF_FAILURE;
522     }
523     InvalidateAndClearApIface(apIfaces_);
524     SetUsedIfaceNameProperty(GetUsedIfaceName());
525     return HDF_SUCCESS;
526 }
527 
CreateP2pService(sptr<IChipIface> & iface)528 int32_t WifiChip::CreateP2pService(sptr<IChipIface>& iface)
529 {
530     if (!CanSupportIfaceType(IfaceType::P2P)) {
531         return HDF_FAILURE;
532     }
533     std::string ifname = GetDefaultP2pIfaceName();
534     sptr<WifiP2pIface> ifa = new WifiP2pIface(ifname, vendorHal_, ifaceUtil_);
535     p2pIfaces_.push_back(ifa);
536     iface = ifa;
537     return HDF_SUCCESS;
538 }
539 
GetDefaultP2pIfaceName()540 std::string WifiChip::GetDefaultP2pIfaceName()
541 {
542     return "p2p0";
543 }
544 
GetP2pServiceIfNames(std::vector<std::string> & ifnames)545 int32_t WifiChip::GetP2pServiceIfNames(std::vector<std::string>& ifnames)
546 {
547     if (p2pIfaces_.empty()) {
548         return HDF_FAILURE;
549     }
550     ifnames = GetP2pNames(p2pIfaces_);
551     return HDF_SUCCESS;
552 }
553 
GetP2pService(const std::string & ifname,sptr<IChipIface> & iface)554 int32_t WifiChip::GetP2pService(const std::string& ifname, sptr<IChipIface>& iface)
555 {
556     iface = FindP2pUsingName(p2pIfaces_, ifname);
557     if (iface == nullptr) {
558         return HDF_FAILURE;
559     }
560     return HDF_SUCCESS;
561 }
562 
RemoveP2pService(const std::string & ifname)563 int32_t WifiChip::RemoveP2pService(const std::string& ifname)
564 {
565     const auto iface = FindP2pUsingName(p2pIfaces_, ifname);
566     if (iface == nullptr) {
567         return HDF_FAILURE;
568     }
569     InvalidateAndClearP2pIface(p2pIfaces_);
570     return HDF_SUCCESS;
571 }
572 
CreateStaService(sptr<IChipIface> & iface)573 int32_t WifiChip::CreateStaService(sptr<IChipIface>& iface)
574 {
575     HDF_LOGI("enter CreateStaService");
576     if (!CanSupportIfaceType(IfaceType::STA)) {
577         HDF_LOGE("%{public}s: Current Mode Not Support Iface Of Type With Current Ifaces", __FUNCTION__);
578         return HDF_FAILURE;
579     }
580     WifiError status;
581     std::string ifname = AllocateStaIfaceName();
582     status = vendorHal_.lock()->CreateVirtualInterface(ifname,
583         HalIfaceType::HAL_TYPE_STA);
584     if (status != WifiError::HAL_SUCCESS) {
585         HDF_LOGE("Failed to add interface: %{public}s, error: %{public}d", ifname.c_str(), status);
586         return HDF_FAILURE;
587     }
588     sptr<WifiStaIface> ifa = new WifiStaIface(ifname, vendorHal_, ifaceUtil_);
589     staIfaces_.push_back(ifa);
590     iface = ifa;
591     SetUsedIfaceNameProperty(GetUsedIfaceName());
592     return HDF_SUCCESS;
593 }
594 
AllocateStaIfaceName()595 std::string WifiChip::AllocateStaIfaceName()
596 {
597     return AllocIfaceName(IfaceType::STA, 0);
598 }
599 
GetStaServiceIfNames(std::vector<std::string> & ifnames)600 int32_t WifiChip::GetStaServiceIfNames(std::vector<std::string>& ifnames)
601 {
602     if (staIfaces_.empty()) {
603         return HDF_FAILURE;
604     }
605     ifnames = GetStaNames(staIfaces_);
606     return HDF_SUCCESS;
607 }
608 
GetStaService(const std::string & ifname,sptr<IChipIface> & iface)609 int32_t WifiChip::GetStaService(const std::string& ifname, sptr<IChipIface>& iface)
610 {
611     iface = FindStaUsingName(staIfaces_, ifname);
612     if (iface == nullptr) {
613         return HDF_FAILURE;
614     }
615     return HDF_SUCCESS;
616 }
617 
RemoveStaService(const std::string & ifname)618 int32_t WifiChip::RemoveStaService(const std::string& ifname)
619 {
620     const auto iface = FindStaUsingName(staIfaces_, ifname);
621     if (iface == nullptr) {
622         return HDF_FAILURE;
623     }
624     WifiError status =
625         vendorHal_.lock()->DeleteVirtualInterface(ifname);
626     if (status != WifiError::HAL_SUCCESS) {
627         HDF_LOGE("Failed to remove interface: %{public}s, error: %{public}d", ifname.c_str(), status);
628     }
629     HDF_LOGI("RemoveStaService Invalidate and erase iface:%{public}s", ifname.c_str());
630     iface->Invalidate();
631     staIfaces_.erase(std::remove(staIfaces_.begin(), staIfaces_.end(), iface), staIfaces_.end());
632     SetUsedIfaceNameProperty(GetUsedIfaceName());
633     return HDF_SUCCESS;
634 }
635 
636 }
637 }
638 }
639 }
640 }