blob: 559700172d22fd061385b9d36ea784e45a601f57 [file] [log] [blame]
Gabriel Birenf3262f92022-07-15 23:25:39 +00001/*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "wifi_chip.h"
18
19#include <android-base/logging.h>
20#include <android-base/unique_fd.h>
21#include <cutils/properties.h>
22#include <fcntl.h>
23#include <net/if.h>
24#include <sys/stat.h>
25#include <sys/sysmacros.h>
26
27#include "aidl_return_util.h"
28#include "aidl_struct_util.h"
29#include "wifi_status_util.h"
30
31#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
32
33namespace {
34using aidl::android::hardware::wifi::IfaceType;
35using aidl::android::hardware::wifi::IWifiChip;
36using CoexRestriction = aidl::android::hardware::wifi::IWifiChip::CoexRestriction;
Shuibing Daie5fbcab2022-12-19 15:37:19 -080037using ChannelCategoryMask = aidl::android::hardware::wifi::IWifiChip::ChannelCategoryMask;
Gabriel Birenf3262f92022-07-15 23:25:39 +000038using android::base::unique_fd;
39
40constexpr char kCpioMagic[] = "070701";
41constexpr size_t kMaxBufferSizeBytes = 1024 * 1024 * 3;
42constexpr uint32_t kMaxRingBufferFileAgeSeconds = 60 * 60 * 10;
43constexpr uint32_t kMaxRingBufferFileNum = 20;
44constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
45constexpr char kActiveWlanIfaceNameProperty[] = "wifi.active.interface";
46constexpr char kNoActiveWlanIfaceNamePropertyValue[] = "";
47constexpr unsigned kMaxWlanIfaces = 5;
48constexpr char kApBridgeIfacePrefix[] = "ap_br_";
49
50template <typename Iface>
51void invalidateAndClear(std::vector<std::shared_ptr<Iface>>& ifaces, std::shared_ptr<Iface> iface) {
52 iface->invalidate();
53 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface), ifaces.end());
54}
55
56template <typename Iface>
57void invalidateAndClearAll(std::vector<std::shared_ptr<Iface>>& ifaces) {
58 for (const auto& iface : ifaces) {
59 iface->invalidate();
60 }
61 ifaces.clear();
62}
63
64template <typename Iface>
65std::vector<std::string> getNames(std::vector<std::shared_ptr<Iface>>& ifaces) {
66 std::vector<std::string> names;
67 for (const auto& iface : ifaces) {
68 names.emplace_back(iface->getName());
69 }
70 return names;
71}
72
73template <typename Iface>
74std::shared_ptr<Iface> findUsingName(std::vector<std::shared_ptr<Iface>>& ifaces,
75 const std::string& name) {
76 std::vector<std::string> names;
77 for (const auto& iface : ifaces) {
78 if (name == iface->getName()) {
79 return iface;
80 }
81 }
82 return nullptr;
83}
84
85std::string getWlanIfaceName(unsigned idx) {
86 if (idx >= kMaxWlanIfaces) {
87 CHECK(false) << "Requested interface beyond wlan" << kMaxWlanIfaces;
88 return {};
89 }
90
91 std::array<char, PROPERTY_VALUE_MAX> buffer;
92 if (idx == 0 || idx == 1) {
93 const char* altPropName = (idx == 0) ? "wifi.interface" : "wifi.concurrent.interface";
94 auto res = property_get(altPropName, buffer.data(), nullptr);
95 if (res > 0) return buffer.data();
96 }
97 std::string propName = "wifi.interface." + std::to_string(idx);
98 auto res = property_get(propName.c_str(), buffer.data(), nullptr);
99 if (res > 0) return buffer.data();
100
101 return "wlan" + std::to_string(idx);
102}
103
104// Returns the dedicated iface name if defined.
105// Returns two ifaces in bridged mode.
106std::vector<std::string> getPredefinedApIfaceNames(bool is_bridged) {
107 std::vector<std::string> ifnames;
108 std::array<char, PROPERTY_VALUE_MAX> buffer;
109 buffer.fill(0);
110 if (property_get("ro.vendor.wifi.sap.interface", buffer.data(), nullptr) == 0) {
111 return ifnames;
112 }
113 ifnames.push_back(buffer.data());
114 if (is_bridged) {
115 buffer.fill(0);
116 if (property_get("ro.vendor.wifi.sap.concurrent.iface", buffer.data(), nullptr) == 0) {
117 return ifnames;
118 }
119 ifnames.push_back(buffer.data());
120 }
121 return ifnames;
122}
123
124std::string getPredefinedP2pIfaceName() {
125 std::array<char, PROPERTY_VALUE_MAX> primaryIfaceName;
126 char p2pParentIfname[100];
127 std::string p2pDevIfName = "";
128 std::array<char, PROPERTY_VALUE_MAX> buffer;
129 property_get("wifi.direct.interface", buffer.data(), "p2p0");
130 if (strncmp(buffer.data(), P2P_MGMT_DEVICE_PREFIX, strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
131 /* Get the p2p parent interface name from p2p device interface name set
132 * in property */
133 strlcpy(p2pParentIfname, buffer.data() + strlen(P2P_MGMT_DEVICE_PREFIX),
134 strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX));
135 if (property_get(kActiveWlanIfaceNameProperty, primaryIfaceName.data(), nullptr) == 0) {
136 return buffer.data();
137 }
138 /* Check if the parent interface derived from p2p device interface name
139 * is active */
140 if (strncmp(p2pParentIfname, primaryIfaceName.data(),
141 strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX)) != 0) {
142 /*
143 * Update the predefined p2p device interface parent interface name
144 * with current active wlan interface
145 */
146 p2pDevIfName += P2P_MGMT_DEVICE_PREFIX;
147 p2pDevIfName += primaryIfaceName.data();
148 LOG(INFO) << "update the p2p device interface name to " << p2pDevIfName.c_str();
149 return p2pDevIfName;
150 }
151 }
152 return buffer.data();
153}
154
155// Returns the dedicated iface name if one is defined.
156std::string getPredefinedNanIfaceName() {
157 std::array<char, PROPERTY_VALUE_MAX> buffer;
158 if (property_get("wifi.aware.interface", buffer.data(), nullptr) == 0) {
159 return {};
160 }
161 return buffer.data();
162}
163
164void setActiveWlanIfaceNameProperty(const std::string& ifname) {
165 auto res = property_set(kActiveWlanIfaceNameProperty, ifname.data());
166 if (res != 0) {
167 PLOG(ERROR) << "Failed to set active wlan iface name property";
168 }
169}
170
171// Delete files that meet either condition:
172// 1. Older than a predefined time in the wifi tombstone dir.
173// 2. Files in excess to a predefined amount, starting from the oldest ones
174bool removeOldFilesInternal() {
175 time_t now = time(0);
176 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
177 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(kTombstoneFolderPath), closedir);
178 if (!dir_dump) {
179 PLOG(ERROR) << "Failed to open directory";
180 return false;
181 }
182 struct dirent* dp;
183 bool success = true;
184 std::list<std::pair<const time_t, std::string>> valid_files;
185 while ((dp = readdir(dir_dump.get()))) {
186 if (dp->d_type != DT_REG) {
187 continue;
188 }
189 std::string cur_file_name(dp->d_name);
190 struct stat cur_file_stat;
191 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
192 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
193 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
194 success = false;
195 continue;
196 }
197 const time_t cur_file_time = cur_file_stat.st_mtime;
198 valid_files.push_back(std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
199 }
200 valid_files.sort(); // sort the list of files by last modified time from
201 // small to big.
202 uint32_t cur_file_count = valid_files.size();
203 for (auto cur_file : valid_files) {
204 if (cur_file_count > kMaxRingBufferFileNum || cur_file.first < delete_files_before) {
205 if (unlink(cur_file.second.c_str()) != 0) {
206 PLOG(ERROR) << "Error deleting file";
207 success = false;
208 }
209 cur_file_count--;
210 } else {
211 break;
212 }
213 }
214 return success;
215}
216
217// Helper function for |cpioArchiveFilesInDir|
218bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name, size_t file_name_len) {
219 const int buf_size = 32 * 1024;
220 std::array<char, buf_size> read_buf;
221 ssize_t llen = snprintf(
222 read_buf.data(), buf_size, "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
223 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid, st.st_gid,
224 static_cast<int>(st.st_nlink), static_cast<int>(st.st_mtime),
225 static_cast<int>(st.st_size), major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
226 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
227 if (write(out_fd, read_buf.data(), llen < buf_size ? llen : buf_size - 1) == -1) {
228 PLOG(ERROR) << "Error writing cpio header to file " << file_name;
229 return false;
230 }
231 if (write(out_fd, file_name, file_name_len) == -1) {
232 PLOG(ERROR) << "Error writing filename to file " << file_name;
233 return false;
234 }
235
236 // NUL Pad header up to 4 multiple bytes.
237 llen = (llen + file_name_len) % 4;
238 if (llen != 0) {
239 const uint32_t zero = 0;
240 if (write(out_fd, &zero, 4 - llen) == -1) {
241 PLOG(ERROR) << "Error padding 0s to file " << file_name;
242 return false;
243 }
244 }
245 return true;
246}
247
248// Helper function for |cpioArchiveFilesInDir|
249size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
250 // writing content of file
251 std::array<char, 32 * 1024> read_buf;
252 ssize_t llen = st.st_size;
253 size_t n_error = 0;
254 while (llen > 0) {
255 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
256 if (bytes_read == -1) {
257 PLOG(ERROR) << "Error reading file";
258 return ++n_error;
259 }
260 llen -= bytes_read;
261 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
262 PLOG(ERROR) << "Error writing data to file";
263 return ++n_error;
264 }
265 if (bytes_read == 0) { // this should never happen, but just in case
266 // to unstuck from while loop
267 PLOG(ERROR) << "Unexpected read result";
268 n_error++;
269 break;
270 }
271 }
272 llen = st.st_size % 4;
273 if (llen != 0) {
274 const uint32_t zero = 0;
275 if (write(out_fd, &zero, 4 - llen) == -1) {
276 PLOG(ERROR) << "Error padding 0s to file";
277 return ++n_error;
278 }
279 }
280 return n_error;
281}
282
283// Helper function for |cpioArchiveFilesInDir|
284bool cpioWriteFileTrailer(int out_fd) {
285 const int buf_size = 4096;
286 std::array<char, buf_size> read_buf;
287 read_buf.fill(0);
288 ssize_t llen = snprintf(read_buf.data(), 4096, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0);
289 if (write(out_fd, read_buf.data(), (llen < buf_size ? llen : buf_size - 1) + 4) == -1) {
290 PLOG(ERROR) << "Error writing trailing bytes";
291 return false;
292 }
293 return true;
294}
295
296// Archives all files in |input_dir| and writes result into |out_fd|
297// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
298// portion
299size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
300 struct dirent* dp;
301 size_t n_error = 0;
302 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir), closedir);
303 if (!dir_dump) {
304 PLOG(ERROR) << "Failed to open directory";
305 return ++n_error;
306 }
307 while ((dp = readdir(dir_dump.get()))) {
308 if (dp->d_type != DT_REG) {
309 continue;
310 }
311 std::string cur_file_name(dp->d_name);
312 struct stat st;
313 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
314 if (stat(cur_file_path.c_str(), &st) == -1) {
315 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
316 n_error++;
317 continue;
318 }
319 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
320 if (fd_read == -1) {
321 PLOG(ERROR) << "Failed to open file " << cur_file_path;
322 n_error++;
323 continue;
324 }
325 std::string file_name_with_last_modified_time =
326 cur_file_name + "-" + std::to_string(st.st_mtime);
327 // string.size() does not include the null terminator. The cpio FreeBSD
328 // file header expects the null character to be included in the length.
329 const size_t file_name_len = file_name_with_last_modified_time.size() + 1;
330 unique_fd file_auto_closer(fd_read);
331 if (!cpioWriteHeader(out_fd, st, file_name_with_last_modified_time.c_str(),
332 file_name_len)) {
333 return ++n_error;
334 }
335 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
336 if (write_error) {
337 return n_error + write_error;
338 }
339 }
340 if (!cpioWriteFileTrailer(out_fd)) {
341 return ++n_error;
342 }
343 return n_error;
344}
345
346// Helper function to create a non-const char*.
347std::vector<char> makeCharVec(const std::string& str) {
348 std::vector<char> vec(str.size() + 1);
349 vec.assign(str.begin(), str.end());
350 vec.push_back('\0');
351 return vec;
352}
353
354} // namespace
355
356namespace aidl {
357namespace android {
358namespace hardware {
359namespace wifi {
360using aidl_return_util::validateAndCall;
361using aidl_return_util::validateAndCallWithLock;
362
363WifiChip::WifiChip(int32_t chip_id, bool is_primary,
364 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
365 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
366 const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util,
367 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
368 const std::function<void(const std::string&)>& handler)
369 : chip_id_(chip_id),
370 legacy_hal_(legacy_hal),
371 mode_controller_(mode_controller),
372 iface_util_(iface_util),
373 is_valid_(true),
374 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
375 modes_(feature_flags.lock()->getChipModes(is_primary)),
376 debug_ring_buffer_cb_registered_(false),
377 subsystemCallbackHandler_(handler) {
378 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
379}
380
381std::shared_ptr<WifiChip> WifiChip::create(
382 int32_t chip_id, bool is_primary, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
383 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
384 const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util,
385 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
386 const std::function<void(const std::string&)>& handler) {
387 std::shared_ptr<WifiChip> ptr = ndk::SharedRefBase::make<WifiChip>(
388 chip_id, is_primary, legacy_hal, mode_controller, iface_util, feature_flags, handler);
389 std::weak_ptr<WifiChip> weak_ptr_this(ptr);
390 ptr->setWeakPtr(weak_ptr_this);
391 return ptr;
392}
393
394void WifiChip::invalidate() {
395 if (!writeRingbufferFilesInternal()) {
396 LOG(ERROR) << "Error writing files to flash";
397 }
398 invalidateAndRemoveAllIfaces();
399 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
400 legacy_hal_.reset();
401 event_cb_handler_.invalidate();
402 is_valid_ = false;
403}
404
405void WifiChip::setWeakPtr(std::weak_ptr<WifiChip> ptr) {
406 weak_ptr_this_ = ptr;
407}
408
409bool WifiChip::isValid() {
410 return is_valid_;
411}
412
413std::set<std::shared_ptr<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
414 return event_cb_handler_.getCallbacks();
415}
416
417ndk::ScopedAStatus WifiChip::getId(int32_t* _aidl_return) {
418 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, &WifiChip::getIdInternal,
419 _aidl_return);
420}
421
422ndk::ScopedAStatus WifiChip::registerEventCallback(
423 const std::shared_ptr<IWifiChipEventCallback>& event_callback) {
424 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
425 &WifiChip::registerEventCallbackInternal, event_callback);
426}
427
428ndk::ScopedAStatus WifiChip::getCapabilities(IWifiChip::ChipCapabilityMask* _aidl_return) {
429 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
430 &WifiChip::getCapabilitiesInternal, _aidl_return);
431}
432
433ndk::ScopedAStatus WifiChip::getAvailableModes(std::vector<IWifiChip::ChipMode>* _aidl_return) {
434 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
435 &WifiChip::getAvailableModesInternal, _aidl_return);
436}
437
438ndk::ScopedAStatus WifiChip::configureChip(int32_t in_modeId) {
439 return validateAndCallWithLock(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
440 &WifiChip::configureChipInternal, in_modeId);
441}
442
443ndk::ScopedAStatus WifiChip::getMode(int32_t* _aidl_return) {
444 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
445 &WifiChip::getModeInternal, _aidl_return);
446}
447
448ndk::ScopedAStatus WifiChip::requestChipDebugInfo(IWifiChip::ChipDebugInfo* _aidl_return) {
449 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
450 &WifiChip::requestChipDebugInfoInternal, _aidl_return);
451}
452
453ndk::ScopedAStatus WifiChip::requestDriverDebugDump(std::vector<uint8_t>* _aidl_return) {
454 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
455 &WifiChip::requestDriverDebugDumpInternal, _aidl_return);
456}
457
458ndk::ScopedAStatus WifiChip::requestFirmwareDebugDump(std::vector<uint8_t>* _aidl_return) {
459 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
460 &WifiChip::requestFirmwareDebugDumpInternal, _aidl_return);
461}
462
463ndk::ScopedAStatus WifiChip::createApIface(std::shared_ptr<IWifiApIface>* _aidl_return) {
464 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
465 &WifiChip::createApIfaceInternal, _aidl_return);
466}
467
468ndk::ScopedAStatus WifiChip::createBridgedApIface(std::shared_ptr<IWifiApIface>* _aidl_return) {
469 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
470 &WifiChip::createBridgedApIfaceInternal, _aidl_return);
471}
472
473ndk::ScopedAStatus WifiChip::getApIfaceNames(std::vector<std::string>* _aidl_return) {
474 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
475 &WifiChip::getApIfaceNamesInternal, _aidl_return);
476}
477
478ndk::ScopedAStatus WifiChip::getApIface(const std::string& in_ifname,
479 std::shared_ptr<IWifiApIface>* _aidl_return) {
480 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
481 &WifiChip::getApIfaceInternal, _aidl_return, in_ifname);
482}
483
484ndk::ScopedAStatus WifiChip::removeApIface(const std::string& in_ifname) {
485 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
486 &WifiChip::removeApIfaceInternal, in_ifname);
487}
488
489ndk::ScopedAStatus WifiChip::removeIfaceInstanceFromBridgedApIface(
490 const std::string& in_brIfaceName, const std::string& in_ifaceInstanceName) {
491 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
492 &WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal, in_brIfaceName,
493 in_ifaceInstanceName);
494}
495
496ndk::ScopedAStatus WifiChip::createNanIface(std::shared_ptr<IWifiNanIface>* _aidl_return) {
497 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
498 &WifiChip::createNanIfaceInternal, _aidl_return);
499}
500
501ndk::ScopedAStatus WifiChip::getNanIfaceNames(std::vector<std::string>* _aidl_return) {
502 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
503 &WifiChip::getNanIfaceNamesInternal, _aidl_return);
504}
505
506ndk::ScopedAStatus WifiChip::getNanIface(const std::string& in_ifname,
507 std::shared_ptr<IWifiNanIface>* _aidl_return) {
508 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
509 &WifiChip::getNanIfaceInternal, _aidl_return, in_ifname);
510}
511
512ndk::ScopedAStatus WifiChip::removeNanIface(const std::string& in_ifname) {
513 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
514 &WifiChip::removeNanIfaceInternal, in_ifname);
515}
516
517ndk::ScopedAStatus WifiChip::createP2pIface(std::shared_ptr<IWifiP2pIface>* _aidl_return) {
518 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
519 &WifiChip::createP2pIfaceInternal, _aidl_return);
520}
521
522ndk::ScopedAStatus WifiChip::getP2pIfaceNames(std::vector<std::string>* _aidl_return) {
523 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
524 &WifiChip::getP2pIfaceNamesInternal, _aidl_return);
525}
526
527ndk::ScopedAStatus WifiChip::getP2pIface(const std::string& in_ifname,
528 std::shared_ptr<IWifiP2pIface>* _aidl_return) {
529 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
530 &WifiChip::getP2pIfaceInternal, _aidl_return, in_ifname);
531}
532
533ndk::ScopedAStatus WifiChip::removeP2pIface(const std::string& in_ifname) {
534 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
535 &WifiChip::removeP2pIfaceInternal, in_ifname);
536}
537
538ndk::ScopedAStatus WifiChip::createStaIface(std::shared_ptr<IWifiStaIface>* _aidl_return) {
539 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
540 &WifiChip::createStaIfaceInternal, _aidl_return);
541}
542
543ndk::ScopedAStatus WifiChip::getStaIfaceNames(std::vector<std::string>* _aidl_return) {
544 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
545 &WifiChip::getStaIfaceNamesInternal, _aidl_return);
546}
547
548ndk::ScopedAStatus WifiChip::getStaIface(const std::string& in_ifname,
549 std::shared_ptr<IWifiStaIface>* _aidl_return) {
550 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
551 &WifiChip::getStaIfaceInternal, _aidl_return, in_ifname);
552}
553
554ndk::ScopedAStatus WifiChip::removeStaIface(const std::string& in_ifname) {
555 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
556 &WifiChip::removeStaIfaceInternal, in_ifname);
557}
558
559ndk::ScopedAStatus WifiChip::createRttController(
560 const std::shared_ptr<IWifiStaIface>& in_boundIface,
561 std::shared_ptr<IWifiRttController>* _aidl_return) {
562 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
563 &WifiChip::createRttControllerInternal, _aidl_return, in_boundIface);
564}
565
566ndk::ScopedAStatus WifiChip::getDebugRingBuffersStatus(
567 std::vector<WifiDebugRingBufferStatus>* _aidl_return) {
568 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
569 &WifiChip::getDebugRingBuffersStatusInternal, _aidl_return);
570}
571
572ndk::ScopedAStatus WifiChip::startLoggingToDebugRingBuffer(
573 const std::string& in_ringName, WifiDebugRingBufferVerboseLevel in_verboseLevel,
574 int32_t in_maxIntervalInSec, int32_t in_minDataSizeInBytes) {
575 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
576 &WifiChip::startLoggingToDebugRingBufferInternal, in_ringName,
577 in_verboseLevel, in_maxIntervalInSec, in_minDataSizeInBytes);
578}
579
580ndk::ScopedAStatus WifiChip::forceDumpToDebugRingBuffer(const std::string& in_ringName) {
581 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
582 &WifiChip::forceDumpToDebugRingBufferInternal, in_ringName);
583}
584
585ndk::ScopedAStatus WifiChip::flushRingBufferToFile() {
586 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
587 &WifiChip::flushRingBufferToFileInternal);
588}
589
590ndk::ScopedAStatus WifiChip::stopLoggingToDebugRingBuffer() {
591 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
592 &WifiChip::stopLoggingToDebugRingBufferInternal);
593}
594
595ndk::ScopedAStatus WifiChip::getDebugHostWakeReasonStats(
596 WifiDebugHostWakeReasonStats* _aidl_return) {
597 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
598 &WifiChip::getDebugHostWakeReasonStatsInternal, _aidl_return);
599}
600
601ndk::ScopedAStatus WifiChip::enableDebugErrorAlerts(bool in_enable) {
602 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
603 &WifiChip::enableDebugErrorAlertsInternal, in_enable);
604}
605
606ndk::ScopedAStatus WifiChip::selectTxPowerScenario(IWifiChip::TxPowerScenario in_scenario) {
607 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
608 &WifiChip::selectTxPowerScenarioInternal, in_scenario);
609}
610
611ndk::ScopedAStatus WifiChip::resetTxPowerScenario() {
612 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
613 &WifiChip::resetTxPowerScenarioInternal);
614}
615
616ndk::ScopedAStatus WifiChip::setLatencyMode(IWifiChip::LatencyMode in_mode) {
617 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
618 &WifiChip::setLatencyModeInternal, in_mode);
619}
620
621binder_status_t WifiChip::dump(int fd, const char**, uint32_t) {
622 {
623 std::unique_lock<std::mutex> lk(lock_t);
624 for (const auto& item : ringbuffer_map_) {
625 forceDumpToDebugRingBufferInternal(item.first);
626 }
627 // unique_lock unlocked here
628 }
629 usleep(100 * 1000); // sleep for 100 milliseconds to wait for
630 // ringbuffer updates.
631 if (!writeRingbufferFilesInternal()) {
632 LOG(ERROR) << "Error writing files to flash";
633 }
634 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
635 if (n_error != 0) {
636 LOG(ERROR) << n_error << " errors occurred in cpio function";
637 }
638 fsync(fd);
639 return STATUS_OK;
640}
641
642ndk::ScopedAStatus WifiChip::setMultiStaPrimaryConnection(const std::string& in_ifName) {
643 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
644 &WifiChip::setMultiStaPrimaryConnectionInternal, in_ifName);
645}
646
647ndk::ScopedAStatus WifiChip::setMultiStaUseCase(IWifiChip::MultiStaUseCase in_useCase) {
648 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
649 &WifiChip::setMultiStaUseCaseInternal, in_useCase);
650}
651
652ndk::ScopedAStatus WifiChip::setCoexUnsafeChannels(
653 const std::vector<IWifiChip::CoexUnsafeChannel>& in_unsafeChannels,
654 CoexRestriction in_restrictions) {
655 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
656 &WifiChip::setCoexUnsafeChannelsInternal, in_unsafeChannels,
657 in_restrictions);
658}
659
660ndk::ScopedAStatus WifiChip::setCountryCode(const std::array<uint8_t, 2>& in_code) {
661 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
662 &WifiChip::setCountryCodeInternal, in_code);
663}
664
665ndk::ScopedAStatus WifiChip::getUsableChannels(WifiBand in_band, WifiIfaceMode in_ifaceModeMask,
666 UsableChannelFilter in_filterMask,
667 std::vector<WifiUsableChannel>* _aidl_return) {
668 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
669 &WifiChip::getUsableChannelsInternal, _aidl_return, in_band,
670 in_ifaceModeMask, in_filterMask);
671}
672
673ndk::ScopedAStatus WifiChip::triggerSubsystemRestart() {
674 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
675 &WifiChip::triggerSubsystemRestartInternal);
676}
677
678ndk::ScopedAStatus WifiChip::getSupportedRadioCombinationsMatrix(
679 WifiRadioCombinationMatrix* _aidl_return) {
680 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
681 &WifiChip::getSupportedRadioCombinationsMatrixInternal, _aidl_return);
682}
683
Mahesh KKVc84d3772022-12-02 16:53:28 -0800684ndk::ScopedAStatus WifiChip::getWifiChipCapabilities(WifiChipCapabilities* _aidl_return) {
685 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
686 &WifiChip::getWifiChipCapabilitiesInternal, _aidl_return);
687}
688
Shuibing Daie5fbcab2022-12-19 15:37:19 -0800689ndk::ScopedAStatus WifiChip::enableStaChannelForPeerNetwork(
690 ChannelCategoryMask in_channelCategoryEnableFlag) {
691 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
692 &WifiChip::enableStaChannelForPeerNetworkInternal,
693 in_channelCategoryEnableFlag);
694}
695
Gabriel Birenf3262f92022-07-15 23:25:39 +0000696void WifiChip::invalidateAndRemoveAllIfaces() {
697 invalidateAndClearBridgedApAll();
698 invalidateAndClearAll(ap_ifaces_);
699 invalidateAndClearAll(nan_ifaces_);
700 invalidateAndClearAll(p2p_ifaces_);
701 invalidateAndClearAll(sta_ifaces_);
702 // Since all the ifaces are invalid now, all RTT controller objects
703 // using those ifaces also need to be invalidated.
704 for (const auto& rtt : rtt_controllers_) {
705 rtt->invalidate();
706 }
707 rtt_controllers_.clear();
708}
709
710void WifiChip::invalidateAndRemoveDependencies(const std::string& removed_iface_name) {
711 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
712 auto nan_iface = *it;
713 if (nan_iface->getName() == removed_iface_name) {
714 nan_iface->invalidate();
715 for (const auto& callback : event_cb_handler_.getCallbacks()) {
716 if (!callback->onIfaceRemoved(IfaceType::NAN_IFACE, removed_iface_name).isOk()) {
717 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
718 }
719 }
720 it = nan_ifaces_.erase(it);
721 } else {
722 ++it;
723 }
724 }
725
726 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
727 auto rtt = *it;
728 if (rtt->getIfaceName() == removed_iface_name) {
729 rtt->invalidate();
730 it = rtt_controllers_.erase(it);
731 } else {
732 ++it;
733 }
734 }
735}
736
737std::pair<int32_t, ndk::ScopedAStatus> WifiChip::getIdInternal() {
738 return {chip_id_, ndk::ScopedAStatus::ok()};
739}
740
741ndk::ScopedAStatus WifiChip::registerEventCallbackInternal(
742 const std::shared_ptr<IWifiChipEventCallback>& event_callback) {
743 if (!event_cb_handler_.addCallback(event_callback)) {
744 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
745 }
746 return ndk::ScopedAStatus::ok();
747}
748
749std::pair<IWifiChip::ChipCapabilityMask, ndk::ScopedAStatus> WifiChip::getCapabilitiesInternal() {
750 legacy_hal::wifi_error legacy_status;
751 uint64_t legacy_feature_set;
752 uint32_t legacy_logger_feature_set;
753 const auto ifname = getFirstActiveWlanIfaceName();
754 std::tie(legacy_status, legacy_feature_set) =
755 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
756 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
757 return {IWifiChip::ChipCapabilityMask{}, createWifiStatusFromLegacyError(legacy_status)};
758 }
759 std::tie(legacy_status, legacy_logger_feature_set) =
760 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
761 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
762 // some devices don't support querying logger feature set
763 legacy_logger_feature_set = 0;
764 }
765 uint32_t aidl_caps;
766 if (!aidl_struct_util::convertLegacyFeaturesToAidlChipCapabilities(
767 legacy_feature_set, legacy_logger_feature_set, &aidl_caps)) {
768 return {IWifiChip::ChipCapabilityMask{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
769 }
770 return {static_cast<IWifiChip::ChipCapabilityMask>(aidl_caps), ndk::ScopedAStatus::ok()};
771}
772
773std::pair<std::vector<IWifiChip::ChipMode>, ndk::ScopedAStatus>
774WifiChip::getAvailableModesInternal() {
775 return {modes_, ndk::ScopedAStatus::ok()};
776}
777
778ndk::ScopedAStatus WifiChip::configureChipInternal(
779 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, int32_t mode_id) {
780 if (!isValidModeId(mode_id)) {
781 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
782 }
783 if (mode_id == current_mode_id_) {
784 LOG(DEBUG) << "Already in the specified mode " << mode_id;
785 return ndk::ScopedAStatus::ok();
786 }
787 ndk::ScopedAStatus status = handleChipConfiguration(lock, mode_id);
788 if (!status.isOk()) {
789 WifiStatusCode errorCode = static_cast<WifiStatusCode>(status.getServiceSpecificError());
790 for (const auto& callback : event_cb_handler_.getCallbacks()) {
791 if (!callback->onChipReconfigureFailure(errorCode).isOk()) {
792 LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
793 }
794 }
795 return status;
796 }
797 for (const auto& callback : event_cb_handler_.getCallbacks()) {
798 if (!callback->onChipReconfigured(mode_id).isOk()) {
799 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
800 }
801 }
802 current_mode_id_ = mode_id;
803 LOG(INFO) << "Configured chip in mode " << mode_id;
804 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
805
806 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(subsystemCallbackHandler_);
807
808 return status;
809}
810
811std::pair<int32_t, ndk::ScopedAStatus> WifiChip::getModeInternal() {
812 if (!isValidModeId(current_mode_id_)) {
813 return {current_mode_id_, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
814 }
815 return {current_mode_id_, ndk::ScopedAStatus::ok()};
816}
817
818std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> WifiChip::requestChipDebugInfoInternal() {
819 IWifiChip::ChipDebugInfo result;
820 legacy_hal::wifi_error legacy_status;
821 std::string driver_desc;
822 const auto ifname = getFirstActiveWlanIfaceName();
823 std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion(ifname);
824 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
825 LOG(ERROR) << "Failed to get driver version: " << legacyErrorToString(legacy_status);
826 ndk::ScopedAStatus status =
827 createWifiStatusFromLegacyError(legacy_status, "failed to get driver version");
828 return {std::move(result), std::move(status)};
829 }
830 result.driverDescription = driver_desc.c_str();
831
832 std::string firmware_desc;
833 std::tie(legacy_status, firmware_desc) = legacy_hal_.lock()->getFirmwareVersion(ifname);
834 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
835 LOG(ERROR) << "Failed to get firmware version: " << legacyErrorToString(legacy_status);
836 ndk::ScopedAStatus status =
837 createWifiStatusFromLegacyError(legacy_status, "failed to get firmware version");
838 return {std::move(result), std::move(status)};
839 }
840 result.firmwareDescription = firmware_desc.c_str();
841
842 return {std::move(result), ndk::ScopedAStatus::ok()};
843}
844
845std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> WifiChip::requestDriverDebugDumpInternal() {
846 legacy_hal::wifi_error legacy_status;
847 std::vector<uint8_t> driver_dump;
848 std::tie(legacy_status, driver_dump) =
849 legacy_hal_.lock()->requestDriverMemoryDump(getFirstActiveWlanIfaceName());
850 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
851 LOG(ERROR) << "Failed to get driver debug dump: " << legacyErrorToString(legacy_status);
852 return {std::vector<uint8_t>(), createWifiStatusFromLegacyError(legacy_status)};
853 }
854 return {driver_dump, ndk::ScopedAStatus::ok()};
855}
856
857std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> WifiChip::requestFirmwareDebugDumpInternal() {
858 legacy_hal::wifi_error legacy_status;
859 std::vector<uint8_t> firmware_dump;
860 std::tie(legacy_status, firmware_dump) =
861 legacy_hal_.lock()->requestFirmwareMemoryDump(getFirstActiveWlanIfaceName());
862 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
863 LOG(ERROR) << "Failed to get firmware debug dump: " << legacyErrorToString(legacy_status);
864 return {std::vector<uint8_t>(), createWifiStatusFromLegacyError(legacy_status)};
865 }
866 return {firmware_dump, ndk::ScopedAStatus::ok()};
867}
868
869ndk::ScopedAStatus WifiChip::createVirtualApInterface(const std::string& apVirtIf) {
870 legacy_hal::wifi_error legacy_status;
871 legacy_status = legacy_hal_.lock()->createVirtualInterface(
872 apVirtIf, aidl_struct_util::convertAidlIfaceTypeToLegacy(IfaceType::AP));
873 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
874 LOG(ERROR) << "Failed to add interface: " << apVirtIf << " "
875 << legacyErrorToString(legacy_status);
876 return createWifiStatusFromLegacyError(legacy_status);
877 }
878 return ndk::ScopedAStatus::ok();
879}
880
881std::shared_ptr<WifiApIface> WifiChip::newWifiApIface(std::string& ifname) {
882 std::vector<std::string> ap_instances;
883 for (auto const& it : br_ifaces_ap_instances_) {
884 if (it.first == ifname) {
885 ap_instances = it.second;
886 }
887 }
888 std::shared_ptr<WifiApIface> iface =
889 ndk::SharedRefBase::make<WifiApIface>(ifname, ap_instances, legacy_hal_, iface_util_);
890 ap_ifaces_.push_back(iface);
891 for (const auto& callback : event_cb_handler_.getCallbacks()) {
892 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
893 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
894 }
895 }
896 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
897 return iface;
898}
899
900std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> WifiChip::createApIfaceInternal() {
901 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP)) {
902 return {std::shared_ptr<WifiApIface>(),
903 createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
904 }
905 std::string ifname = allocateApIfaceName();
906 ndk::ScopedAStatus status = createVirtualApInterface(ifname);
907 if (!status.isOk()) {
908 return {std::shared_ptr<WifiApIface>(), std::move(status)};
909 }
910 std::shared_ptr<WifiApIface> iface = newWifiApIface(ifname);
911 return {iface, ndk::ScopedAStatus::ok()};
912}
913
914std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus>
915WifiChip::createBridgedApIfaceInternal() {
916 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP_BRIDGED)) {
917 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
918 }
919 std::vector<std::string> ap_instances = allocateBridgedApInstanceNames();
920 if (ap_instances.size() < 2) {
921 LOG(ERROR) << "Fail to allocate two instances";
922 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
923 }
924 std::string br_ifname = kApBridgeIfacePrefix + ap_instances[0];
925 for (int i = 0; i < 2; i++) {
926 ndk::ScopedAStatus status = createVirtualApInterface(ap_instances[i]);
927 if (!status.isOk()) {
928 if (i != 0) { // The failure happened when creating second virtual
929 // iface.
930 legacy_hal_.lock()->deleteVirtualInterface(
931 ap_instances.front()); // Remove the first virtual iface.
932 }
933 return {nullptr, std::move(status)};
934 }
935 }
936 br_ifaces_ap_instances_[br_ifname] = ap_instances;
937 if (!iface_util_->createBridge(br_ifname)) {
938 LOG(ERROR) << "Failed createBridge - br_name=" << br_ifname.c_str();
939 invalidateAndClearBridgedAp(br_ifname);
940 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
941 }
942 for (auto const& instance : ap_instances) {
943 // Bind ap instance interface to AP bridge
944 if (!iface_util_->addIfaceToBridge(br_ifname, instance)) {
945 LOG(ERROR) << "Failed add if to Bridge - if_name=" << instance.c_str();
946 invalidateAndClearBridgedAp(br_ifname);
947 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
948 }
949 }
950 std::shared_ptr<WifiApIface> iface = newWifiApIface(br_ifname);
951 return {iface, ndk::ScopedAStatus::ok()};
952}
953
954std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getApIfaceNamesInternal() {
955 if (ap_ifaces_.empty()) {
956 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
957 }
958 return {getNames(ap_ifaces_), ndk::ScopedAStatus::ok()};
959}
960
961std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> WifiChip::getApIfaceInternal(
962 const std::string& ifname) {
963 const auto iface = findUsingName(ap_ifaces_, ifname);
964 if (!iface.get()) {
965 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
966 }
967 return {iface, ndk::ScopedAStatus::ok()};
968}
969
970ndk::ScopedAStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
971 const auto iface = findUsingName(ap_ifaces_, ifname);
972 if (!iface.get()) {
973 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
974 }
975 // Invalidate & remove any dependent objects first.
976 // Note: This is probably not required because we never create
977 // nan/rtt objects over AP iface. But, there is no harm to do it
978 // here and not make that assumption all over the place.
979 invalidateAndRemoveDependencies(ifname);
980 // Clear the bridge interface and the iface instance.
981 invalidateAndClearBridgedAp(ifname);
982 invalidateAndClear(ap_ifaces_, iface);
983 for (const auto& callback : event_cb_handler_.getCallbacks()) {
984 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
985 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
986 }
987 }
988 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
989 return ndk::ScopedAStatus::ok();
990}
991
992ndk::ScopedAStatus WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal(
993 const std::string& ifname, const std::string& ifInstanceName) {
994 const auto iface = findUsingName(ap_ifaces_, ifname);
995 if (!iface.get() || ifInstanceName.empty()) {
996 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
997 }
998 // Requires to remove one of the instance in bridge mode
999 for (auto const& it : br_ifaces_ap_instances_) {
1000 if (it.first == ifname) {
1001 std::vector<std::string> ap_instances = it.second;
1002 for (auto const& iface : ap_instances) {
1003 if (iface == ifInstanceName) {
1004 if (!iface_util_->removeIfaceFromBridge(it.first, iface)) {
1005 LOG(ERROR) << "Failed to remove interface: " << ifInstanceName << " from "
1006 << ifname;
1007 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE);
1008 }
1009 legacy_hal::wifi_error legacy_status =
1010 legacy_hal_.lock()->deleteVirtualInterface(iface);
1011 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1012 LOG(ERROR) << "Failed to del interface: " << iface << " "
1013 << legacyErrorToString(legacy_status);
1014 return createWifiStatusFromLegacyError(legacy_status);
1015 }
1016 ap_instances.erase(
1017 std::remove(ap_instances.begin(), ap_instances.end(), ifInstanceName),
1018 ap_instances.end());
1019 br_ifaces_ap_instances_[ifname] = ap_instances;
1020 break;
1021 }
1022 }
1023 break;
1024 }
1025 }
1026 iface->removeInstance(ifInstanceName);
1027 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1028
1029 return ndk::ScopedAStatus::ok();
1030}
1031
1032std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> WifiChip::createNanIfaceInternal() {
1033 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::NAN_IFACE)) {
1034 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1035 }
1036 bool is_dedicated_iface = true;
1037 std::string ifname = getPredefinedNanIfaceName();
1038 if (ifname.empty() || !iface_util_->ifNameToIndex(ifname)) {
1039 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
1040 // not defined.
1041 ifname = getFirstActiveWlanIfaceName();
1042 is_dedicated_iface = false;
1043 }
1044 std::shared_ptr<WifiNanIface> iface =
1045 WifiNanIface::create(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
1046 nan_ifaces_.push_back(iface);
1047 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1048 if (!callback->onIfaceAdded(IfaceType::NAN_IFACE, ifname).isOk()) {
1049 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1050 }
1051 }
1052 return {iface, ndk::ScopedAStatus::ok()};
1053}
1054
1055std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getNanIfaceNamesInternal() {
1056 if (nan_ifaces_.empty()) {
1057 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
1058 }
1059 return {getNames(nan_ifaces_), ndk::ScopedAStatus::ok()};
1060}
1061
1062std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> WifiChip::getNanIfaceInternal(
1063 const std::string& ifname) {
1064 const auto iface = findUsingName(nan_ifaces_, ifname);
1065 if (!iface.get()) {
1066 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1067 }
1068 return {iface, ndk::ScopedAStatus::ok()};
1069}
1070
1071ndk::ScopedAStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
1072 const auto iface = findUsingName(nan_ifaces_, ifname);
1073 if (!iface.get()) {
1074 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1075 }
1076 invalidateAndClear(nan_ifaces_, iface);
1077 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1078 if (!callback->onIfaceRemoved(IfaceType::NAN_IFACE, ifname).isOk()) {
1079 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1080 }
1081 }
1082 return ndk::ScopedAStatus::ok();
1083}
1084
1085std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> WifiChip::createP2pIfaceInternal() {
1086 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::P2P)) {
1087 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1088 }
1089 std::string ifname = getPredefinedP2pIfaceName();
1090 std::shared_ptr<WifiP2pIface> iface =
1091 ndk::SharedRefBase::make<WifiP2pIface>(ifname, legacy_hal_);
1092 p2p_ifaces_.push_back(iface);
1093 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1094 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
1095 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1096 }
1097 }
1098 return {iface, ndk::ScopedAStatus::ok()};
1099}
1100
1101std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getP2pIfaceNamesInternal() {
1102 if (p2p_ifaces_.empty()) {
1103 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
1104 }
1105 return {getNames(p2p_ifaces_), ndk::ScopedAStatus::ok()};
1106}
1107
1108std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> WifiChip::getP2pIfaceInternal(
1109 const std::string& ifname) {
1110 const auto iface = findUsingName(p2p_ifaces_, ifname);
1111 if (!iface.get()) {
1112 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1113 }
1114 return {iface, ndk::ScopedAStatus::ok()};
1115}
1116
1117ndk::ScopedAStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
1118 const auto iface = findUsingName(p2p_ifaces_, ifname);
1119 if (!iface.get()) {
1120 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1121 }
1122 invalidateAndClear(p2p_ifaces_, iface);
1123 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1124 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1125 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1126 }
1127 }
1128 return ndk::ScopedAStatus::ok();
1129}
1130
1131std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> WifiChip::createStaIfaceInternal() {
1132 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
1133 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1134 }
1135 std::string ifname = allocateStaIfaceName();
1136 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->createVirtualInterface(
1137 ifname, aidl_struct_util::convertAidlIfaceTypeToLegacy(IfaceType::STA));
1138 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1139 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1140 << legacyErrorToString(legacy_status);
1141 return {nullptr, createWifiStatusFromLegacyError(legacy_status)};
1142 }
1143 std::shared_ptr<WifiStaIface> iface =
1144 ndk::SharedRefBase::make<WifiStaIface>(ifname, legacy_hal_, iface_util_);
1145 sta_ifaces_.push_back(iface);
1146 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1147 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1148 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1149 }
1150 }
1151 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1152 return {iface, ndk::ScopedAStatus::ok()};
1153}
1154
1155std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getStaIfaceNamesInternal() {
1156 if (sta_ifaces_.empty()) {
1157 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
1158 }
1159 return {getNames(sta_ifaces_), ndk::ScopedAStatus::ok()};
1160}
1161
1162std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> WifiChip::getStaIfaceInternal(
1163 const std::string& ifname) {
1164 const auto iface = findUsingName(sta_ifaces_, ifname);
1165 if (!iface.get()) {
1166 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1167 }
1168 return {iface, ndk::ScopedAStatus::ok()};
1169}
1170
1171ndk::ScopedAStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
1172 const auto iface = findUsingName(sta_ifaces_, ifname);
1173 if (!iface.get()) {
1174 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1175 }
1176 // Invalidate & remove any dependent objects first.
1177 invalidateAndRemoveDependencies(ifname);
1178 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->deleteVirtualInterface(ifname);
1179 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1180 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1181 << legacyErrorToString(legacy_status);
1182 }
1183 invalidateAndClear(sta_ifaces_, iface);
1184 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1185 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1186 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1187 }
1188 }
1189 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1190 return ndk::ScopedAStatus::ok();
1191}
1192
1193std::pair<std::shared_ptr<IWifiRttController>, ndk::ScopedAStatus>
1194WifiChip::createRttControllerInternal(const std::shared_ptr<IWifiStaIface>& bound_iface) {
1195 if (sta_ifaces_.size() == 0 &&
1196 !canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
1197 LOG(ERROR) << "createRttControllerInternal: Chip cannot support STAs "
1198 "(and RTT by extension)";
1199 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1200 }
1201 std::shared_ptr<WifiRttController> rtt =
1202 WifiRttController::create(getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1203 rtt_controllers_.emplace_back(rtt);
1204 return {rtt, ndk::ScopedAStatus::ok()};
1205}
1206
1207std::pair<std::vector<WifiDebugRingBufferStatus>, ndk::ScopedAStatus>
1208WifiChip::getDebugRingBuffersStatusInternal() {
1209 legacy_hal::wifi_error legacy_status;
1210 std::vector<legacy_hal::wifi_ring_buffer_status> legacy_ring_buffer_status_vec;
1211 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
1212 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
1213 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1214 return {std::vector<WifiDebugRingBufferStatus>(),
1215 createWifiStatusFromLegacyError(legacy_status)};
1216 }
1217 std::vector<WifiDebugRingBufferStatus> aidl_ring_buffer_status_vec;
1218 if (!aidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToAidl(
1219 legacy_ring_buffer_status_vec, &aidl_ring_buffer_status_vec)) {
1220 return {std::vector<WifiDebugRingBufferStatus>(),
1221 createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
1222 }
1223 return {aidl_ring_buffer_status_vec, ndk::ScopedAStatus::ok()};
1224}
1225
1226ndk::ScopedAStatus WifiChip::startLoggingToDebugRingBufferInternal(
1227 const std::string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1228 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1229 ndk::ScopedAStatus status = registerDebugRingBufferCallback();
1230 if (!status.isOk()) {
1231 return status;
1232 }
1233 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRingBufferLogging(
1234 getFirstActiveWlanIfaceName(), ring_name,
1235 static_cast<std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(verbose_level),
1236 max_interval_in_sec, min_data_size_in_bytes);
1237 ringbuffer_map_.insert(
1238 std::pair<std::string, Ringbuffer>(ring_name, Ringbuffer(kMaxBufferSizeBytes)));
1239 // if verbose logging enabled, turn up HAL daemon logging as well.
1240 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1241 ::android::base::SetMinimumLogSeverity(::android::base::DEBUG);
1242 } else {
1243 ::android::base::SetMinimumLogSeverity(::android::base::VERBOSE);
1244 }
1245 return createWifiStatusFromLegacyError(legacy_status);
1246}
1247
1248ndk::ScopedAStatus WifiChip::forceDumpToDebugRingBufferInternal(const std::string& ring_name) {
1249 ndk::ScopedAStatus status = registerDebugRingBufferCallback();
1250 if (!status.isOk()) {
1251 return status;
1252 }
1253 legacy_hal::wifi_error legacy_status =
1254 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(), ring_name);
1255
1256 return createWifiStatusFromLegacyError(legacy_status);
1257}
1258
1259ndk::ScopedAStatus WifiChip::flushRingBufferToFileInternal() {
1260 if (!writeRingbufferFilesInternal()) {
1261 LOG(ERROR) << "Error writing files to flash";
1262 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1263 }
1264 return ndk::ScopedAStatus::ok();
1265}
1266
1267ndk::ScopedAStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
1268 legacy_hal::wifi_error legacy_status =
1269 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(getFirstActiveWlanIfaceName());
1270 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1271 debug_ring_buffer_cb_registered_ = false;
1272 }
1273 return createWifiStatusFromLegacyError(legacy_status);
1274}
1275
1276std::pair<WifiDebugHostWakeReasonStats, ndk::ScopedAStatus>
1277WifiChip::getDebugHostWakeReasonStatsInternal() {
1278 legacy_hal::wifi_error legacy_status;
1279 legacy_hal::WakeReasonStats legacy_stats;
1280 std::tie(legacy_status, legacy_stats) =
1281 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
1282 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1283 return {WifiDebugHostWakeReasonStats{}, createWifiStatusFromLegacyError(legacy_status)};
1284 }
1285 WifiDebugHostWakeReasonStats aidl_stats;
1286 if (!aidl_struct_util::convertLegacyWakeReasonStatsToAidl(legacy_stats, &aidl_stats)) {
1287 return {WifiDebugHostWakeReasonStats{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
1288 }
1289 return {aidl_stats, ndk::ScopedAStatus::ok()};
1290}
1291
1292ndk::ScopedAStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
1293 legacy_hal::wifi_error legacy_status;
1294 if (enable) {
1295 std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_;
1296 const auto& on_alert_callback = [weak_ptr_this](int32_t error_code,
1297 std::vector<uint8_t> debug_data) {
1298 const auto shared_ptr_this = weak_ptr_this.lock();
1299 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1300 LOG(ERROR) << "Callback invoked on an invalid object";
1301 return;
1302 }
1303 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1304 if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) {
1305 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1306 }
1307 }
1308 };
1309 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
1310 getFirstActiveWlanIfaceName(), on_alert_callback);
1311 } else {
1312 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
1313 getFirstActiveWlanIfaceName());
1314 }
1315 return createWifiStatusFromLegacyError(legacy_status);
1316}
1317
1318ndk::ScopedAStatus WifiChip::selectTxPowerScenarioInternal(IWifiChip::TxPowerScenario scenario) {
1319 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1320 getFirstActiveWlanIfaceName(),
1321 aidl_struct_util::convertAidlTxPowerScenarioToLegacy(scenario));
1322 return createWifiStatusFromLegacyError(legacy_status);
1323}
1324
1325ndk::ScopedAStatus WifiChip::resetTxPowerScenarioInternal() {
1326 auto legacy_status = legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
1327 return createWifiStatusFromLegacyError(legacy_status);
1328}
1329
1330ndk::ScopedAStatus WifiChip::setLatencyModeInternal(IWifiChip::LatencyMode mode) {
1331 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
1332 getFirstActiveWlanIfaceName(), aidl_struct_util::convertAidlLatencyModeToLegacy(mode));
1333 return createWifiStatusFromLegacyError(legacy_status);
1334}
1335
1336ndk::ScopedAStatus WifiChip::setMultiStaPrimaryConnectionInternal(const std::string& ifname) {
1337 auto legacy_status = legacy_hal_.lock()->multiStaSetPrimaryConnection(ifname);
1338 return createWifiStatusFromLegacyError(legacy_status);
1339}
1340
1341ndk::ScopedAStatus WifiChip::setMultiStaUseCaseInternal(IWifiChip::MultiStaUseCase use_case) {
1342 auto legacy_status = legacy_hal_.lock()->multiStaSetUseCase(
1343 aidl_struct_util::convertAidlMultiStaUseCaseToLegacy(use_case));
1344 return createWifiStatusFromLegacyError(legacy_status);
1345}
1346
1347ndk::ScopedAStatus WifiChip::setCoexUnsafeChannelsInternal(
1348 std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, CoexRestriction restrictions) {
1349 std::vector<legacy_hal::wifi_coex_unsafe_channel> legacy_unsafe_channels;
1350 if (!aidl_struct_util::convertAidlVectorOfCoexUnsafeChannelToLegacy(unsafe_channels,
1351 &legacy_unsafe_channels)) {
1352 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1353 }
1354 uint32_t aidl_restrictions = static_cast<uint32_t>(restrictions);
1355 uint32_t legacy_restrictions = 0;
1356 if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::WIFI_DIRECT)) {
1357 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_DIRECT;
1358 }
1359 if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::SOFTAP)) {
1360 legacy_restrictions |= legacy_hal::wifi_coex_restriction::SOFTAP;
1361 }
1362 if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::WIFI_AWARE)) {
1363 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_AWARE;
1364 }
1365 auto legacy_status =
1366 legacy_hal_.lock()->setCoexUnsafeChannels(legacy_unsafe_channels, legacy_restrictions);
1367 return createWifiStatusFromLegacyError(legacy_status);
1368}
1369
1370ndk::ScopedAStatus WifiChip::setCountryCodeInternal(const std::array<uint8_t, 2>& code) {
1371 auto legacy_status = legacy_hal_.lock()->setCountryCode(getFirstActiveWlanIfaceName(), code);
1372 return createWifiStatusFromLegacyError(legacy_status);
1373}
1374
1375std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> WifiChip::getUsableChannelsInternal(
1376 WifiBand band, WifiIfaceMode ifaceModeMask, UsableChannelFilter filterMask) {
1377 legacy_hal::wifi_error legacy_status;
1378 std::vector<legacy_hal::wifi_usable_channel> legacy_usable_channels;
1379 std::tie(legacy_status, legacy_usable_channels) = legacy_hal_.lock()->getUsableChannels(
1380 aidl_struct_util::convertAidlWifiBandToLegacyMacBand(band),
1381 aidl_struct_util::convertAidlWifiIfaceModeToLegacy(
1382 static_cast<uint32_t>(ifaceModeMask)),
1383 aidl_struct_util::convertAidlUsableChannelFilterToLegacy(
1384 static_cast<uint32_t>(filterMask)));
1385
1386 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1387 return {std::vector<WifiUsableChannel>(), createWifiStatusFromLegacyError(legacy_status)};
1388 }
1389 std::vector<WifiUsableChannel> aidl_usable_channels;
1390 if (!aidl_struct_util::convertLegacyWifiUsableChannelsToAidl(legacy_usable_channels,
1391 &aidl_usable_channels)) {
1392 return {std::vector<WifiUsableChannel>(), createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
1393 }
1394 return {aidl_usable_channels, ndk::ScopedAStatus::ok()};
1395}
1396
1397std::pair<WifiRadioCombinationMatrix, ndk::ScopedAStatus>
1398WifiChip::getSupportedRadioCombinationsMatrixInternal() {
1399 legacy_hal::wifi_error legacy_status;
1400 legacy_hal::wifi_radio_combination_matrix* legacy_matrix;
1401
1402 std::tie(legacy_status, legacy_matrix) =
1403 legacy_hal_.lock()->getSupportedRadioCombinationsMatrix();
1404 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1405 LOG(ERROR) << "Failed to get SupportedRadioCombinations matrix from legacy HAL: "
1406 << legacyErrorToString(legacy_status);
1407 return {WifiRadioCombinationMatrix{}, createWifiStatusFromLegacyError(legacy_status)};
1408 }
1409
1410 WifiRadioCombinationMatrix aidl_matrix;
1411 if (!aidl_struct_util::convertLegacyRadioCombinationsMatrixToAidl(legacy_matrix,
1412 &aidl_matrix)) {
1413 LOG(ERROR) << "Failed convertLegacyRadioCombinationsMatrixToAidl() ";
1414 return {WifiRadioCombinationMatrix(), createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1415 }
1416 return {aidl_matrix, ndk::ScopedAStatus::ok()};
1417}
1418
Mahesh KKVc84d3772022-12-02 16:53:28 -08001419std::pair<WifiChipCapabilities, ndk::ScopedAStatus> WifiChip::getWifiChipCapabilitiesInternal() {
1420 legacy_hal::wifi_error legacy_status;
1421 legacy_hal::wifi_chip_capabilities legacy_chip_capabilities;
1422 std::tie(legacy_status, legacy_chip_capabilities) =
1423 legacy_hal_.lock()->getWifiChipCapabilities();
1424 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1425 LOG(ERROR) << "Failed to get chip capabilities from legacy HAL: "
1426 << legacyErrorToString(legacy_status);
1427 return {WifiChipCapabilities(), createWifiStatusFromLegacyError(legacy_status)};
1428 }
1429 WifiChipCapabilities aidl_chip_capabilities;
1430 if (!aidl_struct_util::convertLegacyWifiChipCapabilitiesToAidl(legacy_chip_capabilities,
1431 aidl_chip_capabilities)) {
1432 LOG(ERROR) << "Failed convertLegacyWifiChipCapabilitiesToAidl() ";
1433 return {WifiChipCapabilities(), createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1434 }
1435
1436 return {aidl_chip_capabilities, ndk::ScopedAStatus::ok()};
1437}
1438
Shuibing Daie5fbcab2022-12-19 15:37:19 -08001439ndk::ScopedAStatus WifiChip::enableStaChannelForPeerNetworkInternal(
1440 ChannelCategoryMask channelCategoryEnableFlag) {
1441 auto legacy_status = legacy_hal_.lock()->enableStaChannelForPeerNetwork(
1442 aidl_struct_util::convertAidlChannelCategoryToLegacy(
1443 static_cast<uint32_t>(channelCategoryEnableFlag)));
1444 return createWifiStatusFromLegacyError(legacy_status);
1445}
1446
Gabriel Birenf3262f92022-07-15 23:25:39 +00001447ndk::ScopedAStatus WifiChip::triggerSubsystemRestartInternal() {
1448 auto legacy_status = legacy_hal_.lock()->triggerSubsystemRestart();
1449 return createWifiStatusFromLegacyError(legacy_status);
1450}
1451
1452ndk::ScopedAStatus WifiChip::handleChipConfiguration(
1453 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, int32_t mode_id) {
1454 // If the chip is already configured in a different mode, stop
1455 // the legacy HAL and then start it after firmware mode change.
1456 if (isValidModeId(current_mode_id_)) {
1457 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_ << " to mode " << mode_id;
1458 invalidateAndRemoveAllIfaces();
1459 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stop(lock, []() {});
1460 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1461 LOG(ERROR) << "Failed to stop legacy HAL: " << legacyErrorToString(legacy_status);
1462 return createWifiStatusFromLegacyError(legacy_status);
1463 }
1464 }
1465 // Firmware mode change not needed for V2 devices.
1466 bool success = true;
1467 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
1468 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
1469 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
1470 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1471 }
1472 if (!success) {
1473 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1474 }
1475 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1476 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1477 LOG(ERROR) << "Failed to start legacy HAL: " << legacyErrorToString(legacy_status);
1478 return createWifiStatusFromLegacyError(legacy_status);
1479 }
1480 // Every time the HAL is restarted, we need to register the
1481 // radio mode change callback.
1482 ndk::ScopedAStatus status = registerRadioModeChangeCallback();
1483 if (!status.isOk()) {
1484 // This is probably not a critical failure?
1485 LOG(ERROR) << "Failed to register radio mode change callback";
1486 }
1487 // Extract and save the version information into property.
1488 std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> version_info;
1489 version_info = WifiChip::requestChipDebugInfoInternal();
1490 if (version_info.second.isOk()) {
1491 property_set("vendor.wlan.firmware.version",
1492 version_info.first.firmwareDescription.c_str());
1493 property_set("vendor.wlan.driver.version", version_info.first.driverDescription.c_str());
1494 }
1495
1496 return ndk::ScopedAStatus::ok();
1497}
1498
1499ndk::ScopedAStatus WifiChip::registerDebugRingBufferCallback() {
1500 if (debug_ring_buffer_cb_registered_) {
1501 return ndk::ScopedAStatus::ok();
1502 }
1503
1504 std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_;
1505 const auto& on_ring_buffer_data_callback =
1506 [weak_ptr_this](const std::string& name, const std::vector<uint8_t>& data,
1507 const legacy_hal::wifi_ring_buffer_status& status) {
1508 const auto shared_ptr_this = weak_ptr_this.lock();
1509 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1510 LOG(ERROR) << "Callback invoked on an invalid object";
1511 return;
1512 }
1513 WifiDebugRingBufferStatus aidl_status;
1514 Ringbuffer::AppendStatus appendstatus;
1515 if (!aidl_struct_util::convertLegacyDebugRingBufferStatusToAidl(status,
1516 &aidl_status)) {
1517 LOG(ERROR) << "Error converting ring buffer status";
1518 return;
1519 }
1520 {
1521 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1522 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1523 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1524 Ringbuffer& cur_buffer = target->second;
1525 appendstatus = cur_buffer.append(data);
1526 } else {
1527 LOG(ERROR) << "Ringname " << name << " not found";
1528 return;
1529 }
1530 // unique_lock unlocked here
1531 }
1532 if (appendstatus == Ringbuffer::AppendStatus::FAIL_RING_BUFFER_CORRUPTED) {
1533 LOG(ERROR) << "Ringname " << name << " is corrupted. Clear the ring buffer";
1534 shared_ptr_this->writeRingbufferFilesInternal();
1535 return;
1536 }
1537 };
1538 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->registerRingBufferCallbackHandler(
1539 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
1540
1541 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1542 debug_ring_buffer_cb_registered_ = true;
1543 }
1544 return createWifiStatusFromLegacyError(legacy_status);
1545}
1546
1547ndk::ScopedAStatus WifiChip::registerRadioModeChangeCallback() {
1548 std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_;
1549 const auto& on_radio_mode_change_callback =
1550 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1551 const auto shared_ptr_this = weak_ptr_this.lock();
1552 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1553 LOG(ERROR) << "Callback invoked on an invalid object";
1554 return;
1555 }
1556 std::vector<IWifiChipEventCallback::RadioModeInfo> aidl_radio_mode_infos;
1557 if (!aidl_struct_util::convertLegacyWifiMacInfosToAidl(mac_infos,
1558 &aidl_radio_mode_infos)) {
1559 LOG(ERROR) << "Error converting wifi mac info";
1560 return;
1561 }
1562 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1563 if (!callback->onRadioModeChange(aidl_radio_mode_infos).isOk()) {
1564 LOG(ERROR) << "Failed to invoke onRadioModeChange callback";
1565 }
1566 }
1567 };
1568 legacy_hal::wifi_error legacy_status =
1569 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1570 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
1571 return createWifiStatusFromLegacyError(legacy_status);
1572}
1573
1574std::vector<IWifiChip::ChipConcurrencyCombination>
1575WifiChip::getCurrentModeConcurrencyCombinations() {
1576 if (!isValidModeId(current_mode_id_)) {
1577 LOG(ERROR) << "Chip not configured in a mode yet";
1578 return std::vector<IWifiChip::ChipConcurrencyCombination>();
1579 }
1580 for (const auto& mode : modes_) {
1581 if (mode.id == current_mode_id_) {
1582 return mode.availableCombinations;
1583 }
1584 }
1585 CHECK(0) << "Expected to find concurrency combinations for current mode!";
1586 return std::vector<IWifiChip::ChipConcurrencyCombination>();
1587}
1588
1589// Returns a map indexed by IfaceConcurrencyType with the number of ifaces currently
1590// created of the corresponding concurrency type.
1591std::map<IfaceConcurrencyType, size_t> WifiChip::getCurrentConcurrencyCombination() {
1592 std::map<IfaceConcurrencyType, size_t> iface_counts;
1593 uint32_t num_ap = 0;
1594 uint32_t num_ap_bridged = 0;
1595 for (const auto& ap_iface : ap_ifaces_) {
1596 std::string ap_iface_name = ap_iface->getName();
1597 if (br_ifaces_ap_instances_.count(ap_iface_name) > 0 &&
1598 br_ifaces_ap_instances_[ap_iface_name].size() > 1) {
1599 num_ap_bridged++;
1600 } else {
1601 num_ap++;
1602 }
1603 }
1604 iface_counts[IfaceConcurrencyType::AP] = num_ap;
1605 iface_counts[IfaceConcurrencyType::AP_BRIDGED] = num_ap_bridged;
1606 iface_counts[IfaceConcurrencyType::NAN_IFACE] = nan_ifaces_.size();
1607 iface_counts[IfaceConcurrencyType::P2P] = p2p_ifaces_.size();
1608 iface_counts[IfaceConcurrencyType::STA] = sta_ifaces_.size();
1609 return iface_counts;
1610}
1611
1612// This expands the provided concurrency combinations to a more parseable
1613// form. Returns a vector of available combinations possible with the number
1614// of each concurrency type in the combination.
1615// This method is a port of HalDeviceManager.expandConcurrencyCombos() from framework.
1616std::vector<std::map<IfaceConcurrencyType, size_t>> WifiChip::expandConcurrencyCombinations(
1617 const IWifiChip::ChipConcurrencyCombination& combination) {
1618 int32_t num_expanded_combos = 1;
1619 for (const auto& limit : combination.limits) {
1620 for (int32_t i = 0; i < limit.maxIfaces; i++) {
1621 num_expanded_combos *= limit.types.size();
1622 }
1623 }
1624
1625 // Allocate the vector of expanded combos and reset all concurrency type counts to 0
1626 // in each combo.
1627 std::vector<std::map<IfaceConcurrencyType, size_t>> expanded_combos;
1628 expanded_combos.resize(num_expanded_combos);
1629 for (auto& expanded_combo : expanded_combos) {
1630 for (const auto type : {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED,
1631 IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P,
1632 IfaceConcurrencyType::STA}) {
1633 expanded_combo[type] = 0;
1634 }
1635 }
1636 int32_t span = num_expanded_combos;
1637 for (const auto& limit : combination.limits) {
1638 for (int32_t i = 0; i < limit.maxIfaces; i++) {
1639 span /= limit.types.size();
1640 for (int32_t k = 0; k < num_expanded_combos; ++k) {
1641 const auto iface_type = limit.types[(k / span) % limit.types.size()];
1642 expanded_combos[k][iface_type]++;
1643 }
1644 }
1645 }
1646 return expanded_combos;
1647}
1648
1649bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(
1650 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1651 IfaceConcurrencyType requested_type) {
1652 const auto current_combo = getCurrentConcurrencyCombination();
1653
1654 // Check if we have space for 1 more iface of |type| in this combo
1655 for (const auto type :
1656 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED,
1657 IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
1658 size_t num_ifaces_needed = current_combo.at(type);
1659 if (type == requested_type) {
1660 num_ifaces_needed++;
1661 }
1662 size_t num_ifaces_allowed = expanded_combo.at(type);
1663 if (num_ifaces_needed > num_ifaces_allowed) {
1664 return false;
1665 }
1666 }
1667 return true;
1668}
1669
1670// This method does the following:
1671// a) Enumerate all possible concurrency combos by expanding the current
1672// ChipConcurrencyCombination.
1673// b) Check if the requested concurrency type can be added to the current mode
1674// with the concurrency combination that is already active.
1675bool WifiChip::canCurrentModeSupportConcurrencyTypeWithCurrentTypes(
1676 IfaceConcurrencyType requested_type) {
1677 if (!isValidModeId(current_mode_id_)) {
1678 LOG(ERROR) << "Chip not configured in a mode yet";
1679 return false;
1680 }
1681 const auto combinations = getCurrentModeConcurrencyCombinations();
1682 for (const auto& combination : combinations) {
1683 const auto expanded_combos = expandConcurrencyCombinations(combination);
1684 for (const auto& expanded_combo : expanded_combos) {
1685 if (canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(expanded_combo,
1686 requested_type)) {
1687 return true;
1688 }
1689 }
1690 }
1691 return false;
1692}
1693
1694// Note: This does not consider concurrency types already active. It only checks if the
1695// provided expanded concurrency combination can support the requested combo.
1696bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyCombo(
1697 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1698 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
1699 // Check if we have space for 1 more |type| in this combo
1700 for (const auto type :
1701 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED,
1702 IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
1703 if (req_combo.count(type) == 0) {
1704 // Concurrency type not in the req_combo.
1705 continue;
1706 }
1707 size_t num_ifaces_needed = req_combo.at(type);
1708 size_t num_ifaces_allowed = expanded_combo.at(type);
1709 if (num_ifaces_needed > num_ifaces_allowed) {
1710 return false;
1711 }
1712 }
1713 return true;
1714}
1715
1716// This method does the following:
1717// a) Enumerate all possible concurrency combos by expanding the current
1718// ChipConcurrencyCombination.
1719// b) Check if the requested concurrency combo can be added to the current mode.
1720// Note: This does not consider concurrency types already active. It only checks if the
1721// current mode can support the requested combo.
1722bool WifiChip::canCurrentModeSupportConcurrencyCombo(
1723 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
1724 if (!isValidModeId(current_mode_id_)) {
1725 LOG(ERROR) << "Chip not configured in a mode yet";
1726 return false;
1727 }
1728 const auto combinations = getCurrentModeConcurrencyCombinations();
1729 for (const auto& combination : combinations) {
1730 const auto expanded_combos = expandConcurrencyCombinations(combination);
1731 for (const auto& expanded_combo : expanded_combos) {
1732 if (canExpandedConcurrencyComboSupportConcurrencyCombo(expanded_combo, req_combo)) {
1733 return true;
1734 }
1735 }
1736 }
1737 return false;
1738}
1739
1740// This method does the following:
1741// a) Enumerate all possible concurrency combos by expanding the current
1742// ChipConcurrencyCombination.
1743// b) Check if the requested concurrency type can be added to the current mode.
1744bool WifiChip::canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type) {
1745 // Check if we can support at least 1 of the requested concurrency type.
1746 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1747 req_iface_combo[requested_type] = 1;
1748 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
1749}
1750
1751bool WifiChip::isValidModeId(int32_t mode_id) {
1752 for (const auto& mode : modes_) {
1753 if (mode.id == mode_id) {
1754 return true;
1755 }
1756 }
1757 return false;
1758}
1759
1760bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1761 // Check if we can support at least 1 STA & 1 AP concurrently.
1762 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1763 req_iface_combo[IfaceConcurrencyType::STA] = 1;
1764 req_iface_combo[IfaceConcurrencyType::AP] = 1;
1765 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
1766}
1767
1768bool WifiChip::isDualStaConcurrencyAllowedInCurrentMode() {
1769 // Check if we can support at least 2 STA concurrently.
1770 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1771 req_iface_combo[IfaceConcurrencyType::STA] = 2;
1772 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
1773}
1774
1775std::string WifiChip::getFirstActiveWlanIfaceName() {
1776 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1777 if (ap_ifaces_.size() > 0) {
1778 // If the first active wlan iface is bridged iface.
1779 // Return first instance name.
1780 for (auto const& it : br_ifaces_ap_instances_) {
1781 if (it.first == ap_ifaces_[0]->getName()) {
1782 return it.second[0];
1783 }
1784 }
1785 return ap_ifaces_[0]->getName();
1786 }
1787 // This could happen if the chip call is made before any STA/AP
1788 // iface is created. Default to wlan0 for such cases.
1789 LOG(WARNING) << "No active wlan interfaces in use! Using default";
1790 return getWlanIfaceNameWithType(IfaceType::STA, 0);
1791}
1792
1793// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1794// not already in use.
1795// Note: This doesn't check the actual presence of these interfaces.
1796std::string WifiChip::allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx) {
1797 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1798 const auto ifname = getWlanIfaceNameWithType(type, idx);
1799 if (findUsingNameFromBridgedApInstances(ifname)) continue;
1800 if (findUsingName(ap_ifaces_, ifname)) continue;
1801 if (findUsingName(sta_ifaces_, ifname)) continue;
1802 return ifname;
1803 }
1804 // This should never happen. We screwed up somewhere if it did.
1805 CHECK(false) << "All wlan interfaces in use already!";
1806 return {};
1807}
1808
1809uint32_t WifiChip::startIdxOfApIface() {
1810 if (isDualStaConcurrencyAllowedInCurrentMode()) {
1811 // When the HAL support dual STAs, AP should start with idx 2.
1812 return 2;
1813 } else if (isStaApConcurrencyAllowedInCurrentMode()) {
1814 // When the HAL support STA + AP but it doesn't support dual STAs.
1815 // AP should start with idx 1.
1816 return 1;
1817 }
1818 // No concurrency support.
1819 return 0;
1820}
1821
1822// AP iface names start with idx 1 for modes supporting
1823// concurrent STA and not dual AP, else start with idx 0.
1824std::string WifiChip::allocateApIfaceName() {
1825 // Check if we have a dedicated iface for AP.
1826 std::vector<std::string> ifnames = getPredefinedApIfaceNames(true);
1827 for (auto const& ifname : ifnames) {
1828 if (findUsingName(ap_ifaces_, ifname)) continue;
1829 return ifname;
1830 }
1831 return allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface());
1832}
1833
1834std::vector<std::string> WifiChip::allocateBridgedApInstanceNames() {
1835 // Check if we have a dedicated iface for AP.
1836 std::vector<std::string> instances = getPredefinedApIfaceNames(true);
1837 if (instances.size() == 2) {
1838 return instances;
1839 } else {
1840 int num_ifaces_need_to_allocate = 2 - instances.size();
1841 for (int i = 0; i < num_ifaces_need_to_allocate; i++) {
1842 std::string instance_name =
1843 allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface() + i);
1844 if (!instance_name.empty()) {
1845 instances.push_back(instance_name);
1846 }
1847 }
1848 }
1849 return instances;
1850}
1851
1852// STA iface names start with idx 0.
1853// Primary STA iface will always be 0.
1854std::string WifiChip::allocateStaIfaceName() {
1855 return allocateApOrStaIfaceName(IfaceType::STA, 0);
1856}
1857
1858bool WifiChip::writeRingbufferFilesInternal() {
1859 if (!removeOldFilesInternal()) {
1860 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1861 return false;
1862 }
1863 // write ringbuffers to file
1864 {
1865 std::unique_lock<std::mutex> lk(lock_t);
1866 for (auto& item : ringbuffer_map_) {
1867 Ringbuffer& cur_buffer = item.second;
1868 if (cur_buffer.getData().empty()) {
1869 continue;
1870 }
1871 const std::string file_path_raw = kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1872 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1873 if (dump_fd == -1) {
1874 PLOG(ERROR) << "create file failed";
1875 return false;
1876 }
1877 unique_fd file_auto_closer(dump_fd);
1878 for (const auto& cur_block : cur_buffer.getData()) {
1879 if (cur_block.size() <= 0 || cur_block.size() > kMaxBufferSizeBytes) {
1880 PLOG(ERROR) << "Ring buffer: " << item.first
1881 << " is corrupted. Invalid block size: " << cur_block.size();
1882 break;
1883 }
1884 if (write(dump_fd, cur_block.data(), sizeof(cur_block[0]) * cur_block.size()) ==
1885 -1) {
1886 PLOG(ERROR) << "Error writing to file";
1887 }
1888 }
1889 cur_buffer.clear();
1890 }
1891 // unique_lock unlocked here
1892 }
1893 return true;
1894}
1895
1896std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1897 std::string ifname;
1898
1899 // let the legacy hal override the interface name
1900 legacy_hal::wifi_error err = legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
1901 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1902
1903 return getWlanIfaceName(idx);
1904}
1905
1906void WifiChip::invalidateAndClearBridgedApAll() {
1907 for (auto const& it : br_ifaces_ap_instances_) {
1908 for (auto const& iface : it.second) {
1909 iface_util_->removeIfaceFromBridge(it.first, iface);
1910 legacy_hal_.lock()->deleteVirtualInterface(iface);
1911 }
1912 iface_util_->deleteBridge(it.first);
1913 }
1914 br_ifaces_ap_instances_.clear();
1915}
1916
1917void WifiChip::invalidateAndClearBridgedAp(const std::string& br_name) {
1918 if (br_name.empty()) return;
1919 // delete managed interfaces
1920 for (auto const& it : br_ifaces_ap_instances_) {
1921 if (it.first == br_name) {
1922 for (auto const& iface : it.second) {
1923 iface_util_->removeIfaceFromBridge(br_name, iface);
1924 legacy_hal_.lock()->deleteVirtualInterface(iface);
1925 }
1926 iface_util_->deleteBridge(br_name);
1927 br_ifaces_ap_instances_.erase(br_name);
1928 break;
1929 }
1930 }
1931 return;
1932}
1933
1934bool WifiChip::findUsingNameFromBridgedApInstances(const std::string& name) {
1935 for (auto const& it : br_ifaces_ap_instances_) {
1936 if (it.first == name) {
1937 return true;
1938 }
1939 for (auto const& iface : it.second) {
1940 if (iface == name) {
1941 return true;
1942 }
1943 }
1944 }
1945 return false;
1946}
1947
1948} // namespace wifi
1949} // namespace hardware
1950} // namespace android
1951} // namespace aidl