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