blob: 6f43e0669d3c2ff432a53e48aa0b52ebf74fc0f7 [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
Oscar Shuab8313c2022-12-13 00:55:11 +0000673ndk::ScopedAStatus WifiChip::setAfcChannelAllowance(
674 const std::vector<AvailableAfcFrequencyInfo>& availableAfcFrequencyInfo) {
675 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
676 &WifiChip::setAfcChannelAllowanceInternal, availableAfcFrequencyInfo);
677}
678
Gabriel Birenf3262f92022-07-15 23:25:39 +0000679ndk::ScopedAStatus WifiChip::triggerSubsystemRestart() {
680 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
681 &WifiChip::triggerSubsystemRestartInternal);
682}
683
684ndk::ScopedAStatus WifiChip::getSupportedRadioCombinationsMatrix(
685 WifiRadioCombinationMatrix* _aidl_return) {
686 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
687 &WifiChip::getSupportedRadioCombinationsMatrixInternal, _aidl_return);
688}
689
Mahesh KKVc84d3772022-12-02 16:53:28 -0800690ndk::ScopedAStatus WifiChip::getWifiChipCapabilities(WifiChipCapabilities* _aidl_return) {
691 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
692 &WifiChip::getWifiChipCapabilitiesInternal, _aidl_return);
693}
694
Shuibing Daie5fbcab2022-12-19 15:37:19 -0800695ndk::ScopedAStatus WifiChip::enableStaChannelForPeerNetwork(
696 ChannelCategoryMask in_channelCategoryEnableFlag) {
697 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
698 &WifiChip::enableStaChannelForPeerNetworkInternal,
699 in_channelCategoryEnableFlag);
700}
701
Gabriel Birenf3262f92022-07-15 23:25:39 +0000702void WifiChip::invalidateAndRemoveAllIfaces() {
703 invalidateAndClearBridgedApAll();
704 invalidateAndClearAll(ap_ifaces_);
705 invalidateAndClearAll(nan_ifaces_);
706 invalidateAndClearAll(p2p_ifaces_);
707 invalidateAndClearAll(sta_ifaces_);
708 // Since all the ifaces are invalid now, all RTT controller objects
709 // using those ifaces also need to be invalidated.
710 for (const auto& rtt : rtt_controllers_) {
711 rtt->invalidate();
712 }
713 rtt_controllers_.clear();
714}
715
716void WifiChip::invalidateAndRemoveDependencies(const std::string& removed_iface_name) {
717 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
718 auto nan_iface = *it;
719 if (nan_iface->getName() == removed_iface_name) {
720 nan_iface->invalidate();
721 for (const auto& callback : event_cb_handler_.getCallbacks()) {
722 if (!callback->onIfaceRemoved(IfaceType::NAN_IFACE, removed_iface_name).isOk()) {
723 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
724 }
725 }
726 it = nan_ifaces_.erase(it);
727 } else {
728 ++it;
729 }
730 }
731
732 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
733 auto rtt = *it;
734 if (rtt->getIfaceName() == removed_iface_name) {
735 rtt->invalidate();
736 it = rtt_controllers_.erase(it);
737 } else {
738 ++it;
739 }
740 }
741}
742
743std::pair<int32_t, ndk::ScopedAStatus> WifiChip::getIdInternal() {
744 return {chip_id_, ndk::ScopedAStatus::ok()};
745}
746
747ndk::ScopedAStatus WifiChip::registerEventCallbackInternal(
748 const std::shared_ptr<IWifiChipEventCallback>& event_callback) {
749 if (!event_cb_handler_.addCallback(event_callback)) {
750 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
751 }
752 return ndk::ScopedAStatus::ok();
753}
754
755std::pair<IWifiChip::ChipCapabilityMask, ndk::ScopedAStatus> WifiChip::getCapabilitiesInternal() {
756 legacy_hal::wifi_error legacy_status;
757 uint64_t legacy_feature_set;
758 uint32_t legacy_logger_feature_set;
759 const auto ifname = getFirstActiveWlanIfaceName();
760 std::tie(legacy_status, legacy_feature_set) =
761 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
762 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
763 return {IWifiChip::ChipCapabilityMask{}, createWifiStatusFromLegacyError(legacy_status)};
764 }
765 std::tie(legacy_status, legacy_logger_feature_set) =
766 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
767 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
768 // some devices don't support querying logger feature set
769 legacy_logger_feature_set = 0;
770 }
771 uint32_t aidl_caps;
772 if (!aidl_struct_util::convertLegacyFeaturesToAidlChipCapabilities(
773 legacy_feature_set, legacy_logger_feature_set, &aidl_caps)) {
774 return {IWifiChip::ChipCapabilityMask{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
775 }
776 return {static_cast<IWifiChip::ChipCapabilityMask>(aidl_caps), ndk::ScopedAStatus::ok()};
777}
778
779std::pair<std::vector<IWifiChip::ChipMode>, ndk::ScopedAStatus>
780WifiChip::getAvailableModesInternal() {
781 return {modes_, ndk::ScopedAStatus::ok()};
782}
783
784ndk::ScopedAStatus WifiChip::configureChipInternal(
785 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, int32_t mode_id) {
786 if (!isValidModeId(mode_id)) {
787 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
788 }
789 if (mode_id == current_mode_id_) {
790 LOG(DEBUG) << "Already in the specified mode " << mode_id;
791 return ndk::ScopedAStatus::ok();
792 }
793 ndk::ScopedAStatus status = handleChipConfiguration(lock, mode_id);
794 if (!status.isOk()) {
795 WifiStatusCode errorCode = static_cast<WifiStatusCode>(status.getServiceSpecificError());
796 for (const auto& callback : event_cb_handler_.getCallbacks()) {
797 if (!callback->onChipReconfigureFailure(errorCode).isOk()) {
798 LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
799 }
800 }
801 return status;
802 }
803 for (const auto& callback : event_cb_handler_.getCallbacks()) {
804 if (!callback->onChipReconfigured(mode_id).isOk()) {
805 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
806 }
807 }
808 current_mode_id_ = mode_id;
809 LOG(INFO) << "Configured chip in mode " << mode_id;
810 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
811
812 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(subsystemCallbackHandler_);
813
814 return status;
815}
816
817std::pair<int32_t, ndk::ScopedAStatus> WifiChip::getModeInternal() {
818 if (!isValidModeId(current_mode_id_)) {
819 return {current_mode_id_, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
820 }
821 return {current_mode_id_, ndk::ScopedAStatus::ok()};
822}
823
824std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> WifiChip::requestChipDebugInfoInternal() {
825 IWifiChip::ChipDebugInfo result;
826 legacy_hal::wifi_error legacy_status;
827 std::string driver_desc;
828 const auto ifname = getFirstActiveWlanIfaceName();
829 std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion(ifname);
830 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
831 LOG(ERROR) << "Failed to get driver version: " << legacyErrorToString(legacy_status);
832 ndk::ScopedAStatus status =
833 createWifiStatusFromLegacyError(legacy_status, "failed to get driver version");
834 return {std::move(result), std::move(status)};
835 }
836 result.driverDescription = driver_desc.c_str();
837
838 std::string firmware_desc;
839 std::tie(legacy_status, firmware_desc) = legacy_hal_.lock()->getFirmwareVersion(ifname);
840 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
841 LOG(ERROR) << "Failed to get firmware version: " << legacyErrorToString(legacy_status);
842 ndk::ScopedAStatus status =
843 createWifiStatusFromLegacyError(legacy_status, "failed to get firmware version");
844 return {std::move(result), std::move(status)};
845 }
846 result.firmwareDescription = firmware_desc.c_str();
847
848 return {std::move(result), ndk::ScopedAStatus::ok()};
849}
850
851std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> WifiChip::requestDriverDebugDumpInternal() {
852 legacy_hal::wifi_error legacy_status;
853 std::vector<uint8_t> driver_dump;
854 std::tie(legacy_status, driver_dump) =
855 legacy_hal_.lock()->requestDriverMemoryDump(getFirstActiveWlanIfaceName());
856 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
857 LOG(ERROR) << "Failed to get driver debug dump: " << legacyErrorToString(legacy_status);
858 return {std::vector<uint8_t>(), createWifiStatusFromLegacyError(legacy_status)};
859 }
860 return {driver_dump, ndk::ScopedAStatus::ok()};
861}
862
863std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> WifiChip::requestFirmwareDebugDumpInternal() {
864 legacy_hal::wifi_error legacy_status;
865 std::vector<uint8_t> firmware_dump;
866 std::tie(legacy_status, firmware_dump) =
867 legacy_hal_.lock()->requestFirmwareMemoryDump(getFirstActiveWlanIfaceName());
868 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
869 LOG(ERROR) << "Failed to get firmware debug dump: " << legacyErrorToString(legacy_status);
870 return {std::vector<uint8_t>(), createWifiStatusFromLegacyError(legacy_status)};
871 }
872 return {firmware_dump, ndk::ScopedAStatus::ok()};
873}
874
875ndk::ScopedAStatus WifiChip::createVirtualApInterface(const std::string& apVirtIf) {
876 legacy_hal::wifi_error legacy_status;
877 legacy_status = legacy_hal_.lock()->createVirtualInterface(
878 apVirtIf, aidl_struct_util::convertAidlIfaceTypeToLegacy(IfaceType::AP));
879 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
880 LOG(ERROR) << "Failed to add interface: " << apVirtIf << " "
881 << legacyErrorToString(legacy_status);
882 return createWifiStatusFromLegacyError(legacy_status);
883 }
884 return ndk::ScopedAStatus::ok();
885}
886
887std::shared_ptr<WifiApIface> WifiChip::newWifiApIface(std::string& ifname) {
888 std::vector<std::string> ap_instances;
889 for (auto const& it : br_ifaces_ap_instances_) {
890 if (it.first == ifname) {
891 ap_instances = it.second;
892 }
893 }
894 std::shared_ptr<WifiApIface> iface =
895 ndk::SharedRefBase::make<WifiApIface>(ifname, ap_instances, legacy_hal_, iface_util_);
896 ap_ifaces_.push_back(iface);
897 for (const auto& callback : event_cb_handler_.getCallbacks()) {
898 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
899 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
900 }
901 }
902 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
903 return iface;
904}
905
906std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> WifiChip::createApIfaceInternal() {
907 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP)) {
908 return {std::shared_ptr<WifiApIface>(),
909 createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
910 }
911 std::string ifname = allocateApIfaceName();
912 ndk::ScopedAStatus status = createVirtualApInterface(ifname);
913 if (!status.isOk()) {
914 return {std::shared_ptr<WifiApIface>(), std::move(status)};
915 }
916 std::shared_ptr<WifiApIface> iface = newWifiApIface(ifname);
917 return {iface, ndk::ScopedAStatus::ok()};
918}
919
920std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus>
921WifiChip::createBridgedApIfaceInternal() {
922 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP_BRIDGED)) {
923 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
924 }
925 std::vector<std::string> ap_instances = allocateBridgedApInstanceNames();
926 if (ap_instances.size() < 2) {
927 LOG(ERROR) << "Fail to allocate two instances";
928 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
929 }
930 std::string br_ifname = kApBridgeIfacePrefix + ap_instances[0];
931 for (int i = 0; i < 2; i++) {
932 ndk::ScopedAStatus status = createVirtualApInterface(ap_instances[i]);
933 if (!status.isOk()) {
934 if (i != 0) { // The failure happened when creating second virtual
935 // iface.
936 legacy_hal_.lock()->deleteVirtualInterface(
937 ap_instances.front()); // Remove the first virtual iface.
938 }
939 return {nullptr, std::move(status)};
940 }
941 }
942 br_ifaces_ap_instances_[br_ifname] = ap_instances;
943 if (!iface_util_->createBridge(br_ifname)) {
944 LOG(ERROR) << "Failed createBridge - br_name=" << br_ifname.c_str();
945 invalidateAndClearBridgedAp(br_ifname);
946 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
947 }
948 for (auto const& instance : ap_instances) {
949 // Bind ap instance interface to AP bridge
950 if (!iface_util_->addIfaceToBridge(br_ifname, instance)) {
951 LOG(ERROR) << "Failed add if to Bridge - if_name=" << instance.c_str();
952 invalidateAndClearBridgedAp(br_ifname);
953 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
954 }
955 }
956 std::shared_ptr<WifiApIface> iface = newWifiApIface(br_ifname);
957 return {iface, ndk::ScopedAStatus::ok()};
958}
959
960std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getApIfaceNamesInternal() {
961 if (ap_ifaces_.empty()) {
962 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
963 }
964 return {getNames(ap_ifaces_), ndk::ScopedAStatus::ok()};
965}
966
967std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> WifiChip::getApIfaceInternal(
968 const std::string& ifname) {
969 const auto iface = findUsingName(ap_ifaces_, ifname);
970 if (!iface.get()) {
971 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
972 }
973 return {iface, ndk::ScopedAStatus::ok()};
974}
975
976ndk::ScopedAStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
977 const auto iface = findUsingName(ap_ifaces_, ifname);
978 if (!iface.get()) {
979 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
980 }
981 // Invalidate & remove any dependent objects first.
982 // Note: This is probably not required because we never create
983 // nan/rtt objects over AP iface. But, there is no harm to do it
984 // here and not make that assumption all over the place.
985 invalidateAndRemoveDependencies(ifname);
986 // Clear the bridge interface and the iface instance.
987 invalidateAndClearBridgedAp(ifname);
988 invalidateAndClear(ap_ifaces_, iface);
989 for (const auto& callback : event_cb_handler_.getCallbacks()) {
990 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
991 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
992 }
993 }
994 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
995 return ndk::ScopedAStatus::ok();
996}
997
998ndk::ScopedAStatus WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal(
999 const std::string& ifname, const std::string& ifInstanceName) {
1000 const auto iface = findUsingName(ap_ifaces_, ifname);
1001 if (!iface.get() || ifInstanceName.empty()) {
1002 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1003 }
1004 // Requires to remove one of the instance in bridge mode
1005 for (auto const& it : br_ifaces_ap_instances_) {
1006 if (it.first == ifname) {
1007 std::vector<std::string> ap_instances = it.second;
1008 for (auto const& iface : ap_instances) {
1009 if (iface == ifInstanceName) {
1010 if (!iface_util_->removeIfaceFromBridge(it.first, iface)) {
1011 LOG(ERROR) << "Failed to remove interface: " << ifInstanceName << " from "
1012 << ifname;
1013 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE);
1014 }
1015 legacy_hal::wifi_error legacy_status =
1016 legacy_hal_.lock()->deleteVirtualInterface(iface);
1017 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1018 LOG(ERROR) << "Failed to del interface: " << iface << " "
1019 << legacyErrorToString(legacy_status);
1020 return createWifiStatusFromLegacyError(legacy_status);
1021 }
1022 ap_instances.erase(
1023 std::remove(ap_instances.begin(), ap_instances.end(), ifInstanceName),
1024 ap_instances.end());
1025 br_ifaces_ap_instances_[ifname] = ap_instances;
1026 break;
1027 }
1028 }
1029 break;
1030 }
1031 }
1032 iface->removeInstance(ifInstanceName);
1033 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1034
1035 return ndk::ScopedAStatus::ok();
1036}
1037
1038std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> WifiChip::createNanIfaceInternal() {
1039 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::NAN_IFACE)) {
1040 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1041 }
1042 bool is_dedicated_iface = true;
1043 std::string ifname = getPredefinedNanIfaceName();
1044 if (ifname.empty() || !iface_util_->ifNameToIndex(ifname)) {
1045 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
1046 // not defined.
1047 ifname = getFirstActiveWlanIfaceName();
1048 is_dedicated_iface = false;
1049 }
1050 std::shared_ptr<WifiNanIface> iface =
1051 WifiNanIface::create(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
1052 nan_ifaces_.push_back(iface);
1053 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1054 if (!callback->onIfaceAdded(IfaceType::NAN_IFACE, ifname).isOk()) {
1055 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1056 }
1057 }
1058 return {iface, ndk::ScopedAStatus::ok()};
1059}
1060
1061std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getNanIfaceNamesInternal() {
1062 if (nan_ifaces_.empty()) {
1063 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
1064 }
1065 return {getNames(nan_ifaces_), ndk::ScopedAStatus::ok()};
1066}
1067
1068std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> WifiChip::getNanIfaceInternal(
1069 const std::string& ifname) {
1070 const auto iface = findUsingName(nan_ifaces_, ifname);
1071 if (!iface.get()) {
1072 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1073 }
1074 return {iface, ndk::ScopedAStatus::ok()};
1075}
1076
1077ndk::ScopedAStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
1078 const auto iface = findUsingName(nan_ifaces_, ifname);
1079 if (!iface.get()) {
1080 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1081 }
1082 invalidateAndClear(nan_ifaces_, iface);
1083 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1084 if (!callback->onIfaceRemoved(IfaceType::NAN_IFACE, ifname).isOk()) {
1085 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1086 }
1087 }
1088 return ndk::ScopedAStatus::ok();
1089}
1090
1091std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> WifiChip::createP2pIfaceInternal() {
1092 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::P2P)) {
1093 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1094 }
1095 std::string ifname = getPredefinedP2pIfaceName();
1096 std::shared_ptr<WifiP2pIface> iface =
1097 ndk::SharedRefBase::make<WifiP2pIface>(ifname, legacy_hal_);
1098 p2p_ifaces_.push_back(iface);
1099 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1100 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
1101 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1102 }
1103 }
1104 return {iface, ndk::ScopedAStatus::ok()};
1105}
1106
1107std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getP2pIfaceNamesInternal() {
1108 if (p2p_ifaces_.empty()) {
1109 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
1110 }
1111 return {getNames(p2p_ifaces_), ndk::ScopedAStatus::ok()};
1112}
1113
1114std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> WifiChip::getP2pIfaceInternal(
1115 const std::string& ifname) {
1116 const auto iface = findUsingName(p2p_ifaces_, ifname);
1117 if (!iface.get()) {
1118 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1119 }
1120 return {iface, ndk::ScopedAStatus::ok()};
1121}
1122
1123ndk::ScopedAStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
1124 const auto iface = findUsingName(p2p_ifaces_, ifname);
1125 if (!iface.get()) {
1126 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1127 }
1128 invalidateAndClear(p2p_ifaces_, iface);
1129 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1130 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1131 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1132 }
1133 }
1134 return ndk::ScopedAStatus::ok();
1135}
1136
1137std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> WifiChip::createStaIfaceInternal() {
1138 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
1139 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1140 }
1141 std::string ifname = allocateStaIfaceName();
1142 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->createVirtualInterface(
1143 ifname, aidl_struct_util::convertAidlIfaceTypeToLegacy(IfaceType::STA));
1144 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1145 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1146 << legacyErrorToString(legacy_status);
1147 return {nullptr, createWifiStatusFromLegacyError(legacy_status)};
1148 }
1149 std::shared_ptr<WifiStaIface> iface =
1150 ndk::SharedRefBase::make<WifiStaIface>(ifname, legacy_hal_, iface_util_);
1151 sta_ifaces_.push_back(iface);
1152 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1153 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1154 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1155 }
1156 }
1157 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1158 return {iface, ndk::ScopedAStatus::ok()};
1159}
1160
1161std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getStaIfaceNamesInternal() {
1162 if (sta_ifaces_.empty()) {
1163 return {std::vector<std::string>(), ndk::ScopedAStatus::ok()};
1164 }
1165 return {getNames(sta_ifaces_), ndk::ScopedAStatus::ok()};
1166}
1167
1168std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> WifiChip::getStaIfaceInternal(
1169 const std::string& ifname) {
1170 const auto iface = findUsingName(sta_ifaces_, ifname);
1171 if (!iface.get()) {
1172 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1173 }
1174 return {iface, ndk::ScopedAStatus::ok()};
1175}
1176
1177ndk::ScopedAStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
1178 const auto iface = findUsingName(sta_ifaces_, ifname);
1179 if (!iface.get()) {
1180 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1181 }
1182 // Invalidate & remove any dependent objects first.
1183 invalidateAndRemoveDependencies(ifname);
1184 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->deleteVirtualInterface(ifname);
1185 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1186 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1187 << legacyErrorToString(legacy_status);
1188 }
1189 invalidateAndClear(sta_ifaces_, iface);
1190 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1191 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1192 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1193 }
1194 }
1195 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1196 return ndk::ScopedAStatus::ok();
1197}
1198
1199std::pair<std::shared_ptr<IWifiRttController>, ndk::ScopedAStatus>
1200WifiChip::createRttControllerInternal(const std::shared_ptr<IWifiStaIface>& bound_iface) {
1201 if (sta_ifaces_.size() == 0 &&
1202 !canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
1203 LOG(ERROR) << "createRttControllerInternal: Chip cannot support STAs "
1204 "(and RTT by extension)";
1205 return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)};
1206 }
1207 std::shared_ptr<WifiRttController> rtt =
1208 WifiRttController::create(getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1209 rtt_controllers_.emplace_back(rtt);
1210 return {rtt, ndk::ScopedAStatus::ok()};
1211}
1212
1213std::pair<std::vector<WifiDebugRingBufferStatus>, ndk::ScopedAStatus>
1214WifiChip::getDebugRingBuffersStatusInternal() {
1215 legacy_hal::wifi_error legacy_status;
1216 std::vector<legacy_hal::wifi_ring_buffer_status> legacy_ring_buffer_status_vec;
1217 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
1218 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
1219 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1220 return {std::vector<WifiDebugRingBufferStatus>(),
1221 createWifiStatusFromLegacyError(legacy_status)};
1222 }
1223 std::vector<WifiDebugRingBufferStatus> aidl_ring_buffer_status_vec;
1224 if (!aidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToAidl(
1225 legacy_ring_buffer_status_vec, &aidl_ring_buffer_status_vec)) {
1226 return {std::vector<WifiDebugRingBufferStatus>(),
1227 createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
1228 }
1229 return {aidl_ring_buffer_status_vec, ndk::ScopedAStatus::ok()};
1230}
1231
1232ndk::ScopedAStatus WifiChip::startLoggingToDebugRingBufferInternal(
1233 const std::string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1234 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1235 ndk::ScopedAStatus status = registerDebugRingBufferCallback();
1236 if (!status.isOk()) {
1237 return status;
1238 }
1239 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRingBufferLogging(
1240 getFirstActiveWlanIfaceName(), ring_name,
1241 static_cast<std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(verbose_level),
1242 max_interval_in_sec, min_data_size_in_bytes);
1243 ringbuffer_map_.insert(
1244 std::pair<std::string, Ringbuffer>(ring_name, Ringbuffer(kMaxBufferSizeBytes)));
1245 // if verbose logging enabled, turn up HAL daemon logging as well.
1246 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1247 ::android::base::SetMinimumLogSeverity(::android::base::DEBUG);
1248 } else {
1249 ::android::base::SetMinimumLogSeverity(::android::base::VERBOSE);
1250 }
1251 return createWifiStatusFromLegacyError(legacy_status);
1252}
1253
1254ndk::ScopedAStatus WifiChip::forceDumpToDebugRingBufferInternal(const std::string& ring_name) {
1255 ndk::ScopedAStatus status = registerDebugRingBufferCallback();
1256 if (!status.isOk()) {
1257 return status;
1258 }
1259 legacy_hal::wifi_error legacy_status =
1260 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(), ring_name);
1261
1262 return createWifiStatusFromLegacyError(legacy_status);
1263}
1264
1265ndk::ScopedAStatus WifiChip::flushRingBufferToFileInternal() {
1266 if (!writeRingbufferFilesInternal()) {
1267 LOG(ERROR) << "Error writing files to flash";
1268 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1269 }
1270 return ndk::ScopedAStatus::ok();
1271}
1272
1273ndk::ScopedAStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
1274 legacy_hal::wifi_error legacy_status =
1275 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(getFirstActiveWlanIfaceName());
1276 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1277 debug_ring_buffer_cb_registered_ = false;
1278 }
1279 return createWifiStatusFromLegacyError(legacy_status);
1280}
1281
1282std::pair<WifiDebugHostWakeReasonStats, ndk::ScopedAStatus>
1283WifiChip::getDebugHostWakeReasonStatsInternal() {
1284 legacy_hal::wifi_error legacy_status;
1285 legacy_hal::WakeReasonStats legacy_stats;
1286 std::tie(legacy_status, legacy_stats) =
1287 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
1288 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1289 return {WifiDebugHostWakeReasonStats{}, createWifiStatusFromLegacyError(legacy_status)};
1290 }
1291 WifiDebugHostWakeReasonStats aidl_stats;
1292 if (!aidl_struct_util::convertLegacyWakeReasonStatsToAidl(legacy_stats, &aidl_stats)) {
1293 return {WifiDebugHostWakeReasonStats{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
1294 }
1295 return {aidl_stats, ndk::ScopedAStatus::ok()};
1296}
1297
1298ndk::ScopedAStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
1299 legacy_hal::wifi_error legacy_status;
1300 if (enable) {
1301 std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_;
1302 const auto& on_alert_callback = [weak_ptr_this](int32_t error_code,
1303 std::vector<uint8_t> debug_data) {
1304 const auto shared_ptr_this = weak_ptr_this.lock();
1305 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1306 LOG(ERROR) << "Callback invoked on an invalid object";
1307 return;
1308 }
1309 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1310 if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) {
1311 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1312 }
1313 }
1314 };
1315 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
1316 getFirstActiveWlanIfaceName(), on_alert_callback);
1317 } else {
1318 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
1319 getFirstActiveWlanIfaceName());
1320 }
1321 return createWifiStatusFromLegacyError(legacy_status);
1322}
1323
1324ndk::ScopedAStatus WifiChip::selectTxPowerScenarioInternal(IWifiChip::TxPowerScenario scenario) {
1325 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1326 getFirstActiveWlanIfaceName(),
1327 aidl_struct_util::convertAidlTxPowerScenarioToLegacy(scenario));
1328 return createWifiStatusFromLegacyError(legacy_status);
1329}
1330
1331ndk::ScopedAStatus WifiChip::resetTxPowerScenarioInternal() {
1332 auto legacy_status = legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
1333 return createWifiStatusFromLegacyError(legacy_status);
1334}
1335
1336ndk::ScopedAStatus WifiChip::setLatencyModeInternal(IWifiChip::LatencyMode mode) {
1337 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
1338 getFirstActiveWlanIfaceName(), aidl_struct_util::convertAidlLatencyModeToLegacy(mode));
1339 return createWifiStatusFromLegacyError(legacy_status);
1340}
1341
1342ndk::ScopedAStatus WifiChip::setMultiStaPrimaryConnectionInternal(const std::string& ifname) {
1343 auto legacy_status = legacy_hal_.lock()->multiStaSetPrimaryConnection(ifname);
1344 return createWifiStatusFromLegacyError(legacy_status);
1345}
1346
1347ndk::ScopedAStatus WifiChip::setMultiStaUseCaseInternal(IWifiChip::MultiStaUseCase use_case) {
1348 auto legacy_status = legacy_hal_.lock()->multiStaSetUseCase(
1349 aidl_struct_util::convertAidlMultiStaUseCaseToLegacy(use_case));
1350 return createWifiStatusFromLegacyError(legacy_status);
1351}
1352
1353ndk::ScopedAStatus WifiChip::setCoexUnsafeChannelsInternal(
1354 std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, CoexRestriction restrictions) {
1355 std::vector<legacy_hal::wifi_coex_unsafe_channel> legacy_unsafe_channels;
1356 if (!aidl_struct_util::convertAidlVectorOfCoexUnsafeChannelToLegacy(unsafe_channels,
1357 &legacy_unsafe_channels)) {
1358 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1359 }
1360 uint32_t aidl_restrictions = static_cast<uint32_t>(restrictions);
1361 uint32_t legacy_restrictions = 0;
1362 if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::WIFI_DIRECT)) {
1363 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_DIRECT;
1364 }
1365 if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::SOFTAP)) {
1366 legacy_restrictions |= legacy_hal::wifi_coex_restriction::SOFTAP;
1367 }
1368 if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::WIFI_AWARE)) {
1369 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_AWARE;
1370 }
1371 auto legacy_status =
1372 legacy_hal_.lock()->setCoexUnsafeChannels(legacy_unsafe_channels, legacy_restrictions);
1373 return createWifiStatusFromLegacyError(legacy_status);
1374}
1375
1376ndk::ScopedAStatus WifiChip::setCountryCodeInternal(const std::array<uint8_t, 2>& code) {
1377 auto legacy_status = legacy_hal_.lock()->setCountryCode(getFirstActiveWlanIfaceName(), code);
1378 return createWifiStatusFromLegacyError(legacy_status);
1379}
1380
1381std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> WifiChip::getUsableChannelsInternal(
1382 WifiBand band, WifiIfaceMode ifaceModeMask, UsableChannelFilter filterMask) {
1383 legacy_hal::wifi_error legacy_status;
1384 std::vector<legacy_hal::wifi_usable_channel> legacy_usable_channels;
1385 std::tie(legacy_status, legacy_usable_channels) = legacy_hal_.lock()->getUsableChannels(
1386 aidl_struct_util::convertAidlWifiBandToLegacyMacBand(band),
1387 aidl_struct_util::convertAidlWifiIfaceModeToLegacy(
1388 static_cast<uint32_t>(ifaceModeMask)),
1389 aidl_struct_util::convertAidlUsableChannelFilterToLegacy(
1390 static_cast<uint32_t>(filterMask)));
1391
1392 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1393 return {std::vector<WifiUsableChannel>(), createWifiStatusFromLegacyError(legacy_status)};
1394 }
1395 std::vector<WifiUsableChannel> aidl_usable_channels;
1396 if (!aidl_struct_util::convertLegacyWifiUsableChannelsToAidl(legacy_usable_channels,
1397 &aidl_usable_channels)) {
1398 return {std::vector<WifiUsableChannel>(), createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
1399 }
1400 return {aidl_usable_channels, ndk::ScopedAStatus::ok()};
1401}
1402
Oscar Shuab8313c2022-12-13 00:55:11 +00001403ndk::ScopedAStatus WifiChip::setAfcChannelAllowanceInternal(
1404 const std::vector<AvailableAfcFrequencyInfo>& availableAfcFrequencyInfo) {
1405 LOG(INFO) << "setAfcChannelAllowance is not yet supported " << availableAfcFrequencyInfo.size();
1406 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
1407}
1408
Gabriel Birenf3262f92022-07-15 23:25:39 +00001409std::pair<WifiRadioCombinationMatrix, ndk::ScopedAStatus>
1410WifiChip::getSupportedRadioCombinationsMatrixInternal() {
1411 legacy_hal::wifi_error legacy_status;
1412 legacy_hal::wifi_radio_combination_matrix* legacy_matrix;
1413
1414 std::tie(legacy_status, legacy_matrix) =
1415 legacy_hal_.lock()->getSupportedRadioCombinationsMatrix();
1416 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1417 LOG(ERROR) << "Failed to get SupportedRadioCombinations matrix from legacy HAL: "
1418 << legacyErrorToString(legacy_status);
1419 return {WifiRadioCombinationMatrix{}, createWifiStatusFromLegacyError(legacy_status)};
1420 }
1421
1422 WifiRadioCombinationMatrix aidl_matrix;
1423 if (!aidl_struct_util::convertLegacyRadioCombinationsMatrixToAidl(legacy_matrix,
1424 &aidl_matrix)) {
1425 LOG(ERROR) << "Failed convertLegacyRadioCombinationsMatrixToAidl() ";
1426 return {WifiRadioCombinationMatrix(), createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1427 }
1428 return {aidl_matrix, ndk::ScopedAStatus::ok()};
1429}
1430
Mahesh KKVc84d3772022-12-02 16:53:28 -08001431std::pair<WifiChipCapabilities, ndk::ScopedAStatus> WifiChip::getWifiChipCapabilitiesInternal() {
1432 legacy_hal::wifi_error legacy_status;
1433 legacy_hal::wifi_chip_capabilities legacy_chip_capabilities;
1434 std::tie(legacy_status, legacy_chip_capabilities) =
1435 legacy_hal_.lock()->getWifiChipCapabilities();
1436 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1437 LOG(ERROR) << "Failed to get chip capabilities from legacy HAL: "
1438 << legacyErrorToString(legacy_status);
1439 return {WifiChipCapabilities(), createWifiStatusFromLegacyError(legacy_status)};
1440 }
1441 WifiChipCapabilities aidl_chip_capabilities;
1442 if (!aidl_struct_util::convertLegacyWifiChipCapabilitiesToAidl(legacy_chip_capabilities,
1443 aidl_chip_capabilities)) {
1444 LOG(ERROR) << "Failed convertLegacyWifiChipCapabilitiesToAidl() ";
1445 return {WifiChipCapabilities(), createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
1446 }
1447
1448 return {aidl_chip_capabilities, ndk::ScopedAStatus::ok()};
1449}
1450
Shuibing Daie5fbcab2022-12-19 15:37:19 -08001451ndk::ScopedAStatus WifiChip::enableStaChannelForPeerNetworkInternal(
1452 ChannelCategoryMask channelCategoryEnableFlag) {
1453 auto legacy_status = legacy_hal_.lock()->enableStaChannelForPeerNetwork(
1454 aidl_struct_util::convertAidlChannelCategoryToLegacy(
1455 static_cast<uint32_t>(channelCategoryEnableFlag)));
1456 return createWifiStatusFromLegacyError(legacy_status);
1457}
1458
Gabriel Birenf3262f92022-07-15 23:25:39 +00001459ndk::ScopedAStatus WifiChip::triggerSubsystemRestartInternal() {
1460 auto legacy_status = legacy_hal_.lock()->triggerSubsystemRestart();
1461 return createWifiStatusFromLegacyError(legacy_status);
1462}
1463
1464ndk::ScopedAStatus WifiChip::handleChipConfiguration(
1465 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, int32_t mode_id) {
1466 // If the chip is already configured in a different mode, stop
1467 // the legacy HAL and then start it after firmware mode change.
1468 if (isValidModeId(current_mode_id_)) {
1469 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_ << " to mode " << mode_id;
1470 invalidateAndRemoveAllIfaces();
1471 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stop(lock, []() {});
1472 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1473 LOG(ERROR) << "Failed to stop legacy HAL: " << legacyErrorToString(legacy_status);
1474 return createWifiStatusFromLegacyError(legacy_status);
1475 }
1476 }
1477 // Firmware mode change not needed for V2 devices.
1478 bool success = true;
1479 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
1480 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
1481 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
1482 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1483 }
1484 if (!success) {
1485 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1486 }
1487 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1488 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1489 LOG(ERROR) << "Failed to start legacy HAL: " << legacyErrorToString(legacy_status);
1490 return createWifiStatusFromLegacyError(legacy_status);
1491 }
1492 // Every time the HAL is restarted, we need to register the
1493 // radio mode change callback.
1494 ndk::ScopedAStatus status = registerRadioModeChangeCallback();
1495 if (!status.isOk()) {
1496 // This is probably not a critical failure?
1497 LOG(ERROR) << "Failed to register radio mode change callback";
1498 }
1499 // Extract and save the version information into property.
1500 std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> version_info;
1501 version_info = WifiChip::requestChipDebugInfoInternal();
1502 if (version_info.second.isOk()) {
1503 property_set("vendor.wlan.firmware.version",
1504 version_info.first.firmwareDescription.c_str());
1505 property_set("vendor.wlan.driver.version", version_info.first.driverDescription.c_str());
1506 }
1507
1508 return ndk::ScopedAStatus::ok();
1509}
1510
1511ndk::ScopedAStatus WifiChip::registerDebugRingBufferCallback() {
1512 if (debug_ring_buffer_cb_registered_) {
1513 return ndk::ScopedAStatus::ok();
1514 }
1515
1516 std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_;
1517 const auto& on_ring_buffer_data_callback =
1518 [weak_ptr_this](const std::string& name, const std::vector<uint8_t>& data,
1519 const legacy_hal::wifi_ring_buffer_status& status) {
1520 const auto shared_ptr_this = weak_ptr_this.lock();
1521 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1522 LOG(ERROR) << "Callback invoked on an invalid object";
1523 return;
1524 }
1525 WifiDebugRingBufferStatus aidl_status;
1526 Ringbuffer::AppendStatus appendstatus;
1527 if (!aidl_struct_util::convertLegacyDebugRingBufferStatusToAidl(status,
1528 &aidl_status)) {
1529 LOG(ERROR) << "Error converting ring buffer status";
1530 return;
1531 }
1532 {
1533 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1534 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1535 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1536 Ringbuffer& cur_buffer = target->second;
1537 appendstatus = cur_buffer.append(data);
1538 } else {
1539 LOG(ERROR) << "Ringname " << name << " not found";
1540 return;
1541 }
1542 // unique_lock unlocked here
1543 }
1544 if (appendstatus == Ringbuffer::AppendStatus::FAIL_RING_BUFFER_CORRUPTED) {
1545 LOG(ERROR) << "Ringname " << name << " is corrupted. Clear the ring buffer";
1546 shared_ptr_this->writeRingbufferFilesInternal();
1547 return;
1548 }
1549 };
1550 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->registerRingBufferCallbackHandler(
1551 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
1552
1553 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1554 debug_ring_buffer_cb_registered_ = true;
1555 }
1556 return createWifiStatusFromLegacyError(legacy_status);
1557}
1558
1559ndk::ScopedAStatus WifiChip::registerRadioModeChangeCallback() {
1560 std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_;
1561 const auto& on_radio_mode_change_callback =
1562 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1563 const auto shared_ptr_this = weak_ptr_this.lock();
1564 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1565 LOG(ERROR) << "Callback invoked on an invalid object";
1566 return;
1567 }
1568 std::vector<IWifiChipEventCallback::RadioModeInfo> aidl_radio_mode_infos;
1569 if (!aidl_struct_util::convertLegacyWifiMacInfosToAidl(mac_infos,
1570 &aidl_radio_mode_infos)) {
1571 LOG(ERROR) << "Error converting wifi mac info";
1572 return;
1573 }
1574 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1575 if (!callback->onRadioModeChange(aidl_radio_mode_infos).isOk()) {
1576 LOG(ERROR) << "Failed to invoke onRadioModeChange callback";
1577 }
1578 }
1579 };
1580 legacy_hal::wifi_error legacy_status =
1581 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1582 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
1583 return createWifiStatusFromLegacyError(legacy_status);
1584}
1585
1586std::vector<IWifiChip::ChipConcurrencyCombination>
1587WifiChip::getCurrentModeConcurrencyCombinations() {
1588 if (!isValidModeId(current_mode_id_)) {
1589 LOG(ERROR) << "Chip not configured in a mode yet";
1590 return std::vector<IWifiChip::ChipConcurrencyCombination>();
1591 }
1592 for (const auto& mode : modes_) {
1593 if (mode.id == current_mode_id_) {
1594 return mode.availableCombinations;
1595 }
1596 }
1597 CHECK(0) << "Expected to find concurrency combinations for current mode!";
1598 return std::vector<IWifiChip::ChipConcurrencyCombination>();
1599}
1600
1601// Returns a map indexed by IfaceConcurrencyType with the number of ifaces currently
1602// created of the corresponding concurrency type.
1603std::map<IfaceConcurrencyType, size_t> WifiChip::getCurrentConcurrencyCombination() {
1604 std::map<IfaceConcurrencyType, size_t> iface_counts;
1605 uint32_t num_ap = 0;
1606 uint32_t num_ap_bridged = 0;
1607 for (const auto& ap_iface : ap_ifaces_) {
1608 std::string ap_iface_name = ap_iface->getName();
1609 if (br_ifaces_ap_instances_.count(ap_iface_name) > 0 &&
1610 br_ifaces_ap_instances_[ap_iface_name].size() > 1) {
1611 num_ap_bridged++;
1612 } else {
1613 num_ap++;
1614 }
1615 }
1616 iface_counts[IfaceConcurrencyType::AP] = num_ap;
1617 iface_counts[IfaceConcurrencyType::AP_BRIDGED] = num_ap_bridged;
1618 iface_counts[IfaceConcurrencyType::NAN_IFACE] = nan_ifaces_.size();
1619 iface_counts[IfaceConcurrencyType::P2P] = p2p_ifaces_.size();
1620 iface_counts[IfaceConcurrencyType::STA] = sta_ifaces_.size();
1621 return iface_counts;
1622}
1623
1624// This expands the provided concurrency combinations to a more parseable
1625// form. Returns a vector of available combinations possible with the number
1626// of each concurrency type in the combination.
1627// This method is a port of HalDeviceManager.expandConcurrencyCombos() from framework.
1628std::vector<std::map<IfaceConcurrencyType, size_t>> WifiChip::expandConcurrencyCombinations(
1629 const IWifiChip::ChipConcurrencyCombination& combination) {
1630 int32_t num_expanded_combos = 1;
1631 for (const auto& limit : combination.limits) {
1632 for (int32_t i = 0; i < limit.maxIfaces; i++) {
1633 num_expanded_combos *= limit.types.size();
1634 }
1635 }
1636
1637 // Allocate the vector of expanded combos and reset all concurrency type counts to 0
1638 // in each combo.
1639 std::vector<std::map<IfaceConcurrencyType, size_t>> expanded_combos;
1640 expanded_combos.resize(num_expanded_combos);
1641 for (auto& expanded_combo : expanded_combos) {
1642 for (const auto type : {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED,
1643 IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P,
1644 IfaceConcurrencyType::STA}) {
1645 expanded_combo[type] = 0;
1646 }
1647 }
1648 int32_t span = num_expanded_combos;
1649 for (const auto& limit : combination.limits) {
1650 for (int32_t i = 0; i < limit.maxIfaces; i++) {
1651 span /= limit.types.size();
1652 for (int32_t k = 0; k < num_expanded_combos; ++k) {
1653 const auto iface_type = limit.types[(k / span) % limit.types.size()];
1654 expanded_combos[k][iface_type]++;
1655 }
1656 }
1657 }
1658 return expanded_combos;
1659}
1660
1661bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(
1662 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1663 IfaceConcurrencyType requested_type) {
1664 const auto current_combo = getCurrentConcurrencyCombination();
1665
1666 // Check if we have space for 1 more iface of |type| in this combo
1667 for (const auto type :
1668 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED,
1669 IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
1670 size_t num_ifaces_needed = current_combo.at(type);
1671 if (type == requested_type) {
1672 num_ifaces_needed++;
1673 }
1674 size_t num_ifaces_allowed = expanded_combo.at(type);
1675 if (num_ifaces_needed > num_ifaces_allowed) {
1676 return false;
1677 }
1678 }
1679 return true;
1680}
1681
1682// This method does the following:
1683// a) Enumerate all possible concurrency combos by expanding the current
1684// ChipConcurrencyCombination.
1685// b) Check if the requested concurrency type can be added to the current mode
1686// with the concurrency combination that is already active.
1687bool WifiChip::canCurrentModeSupportConcurrencyTypeWithCurrentTypes(
1688 IfaceConcurrencyType requested_type) {
1689 if (!isValidModeId(current_mode_id_)) {
1690 LOG(ERROR) << "Chip not configured in a mode yet";
1691 return false;
1692 }
1693 const auto combinations = getCurrentModeConcurrencyCombinations();
1694 for (const auto& combination : combinations) {
1695 const auto expanded_combos = expandConcurrencyCombinations(combination);
1696 for (const auto& expanded_combo : expanded_combos) {
1697 if (canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(expanded_combo,
1698 requested_type)) {
1699 return true;
1700 }
1701 }
1702 }
1703 return false;
1704}
1705
1706// Note: This does not consider concurrency types already active. It only checks if the
1707// provided expanded concurrency combination can support the requested combo.
1708bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyCombo(
1709 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1710 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
1711 // Check if we have space for 1 more |type| in this combo
1712 for (const auto type :
1713 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED,
1714 IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
1715 if (req_combo.count(type) == 0) {
1716 // Concurrency type not in the req_combo.
1717 continue;
1718 }
1719 size_t num_ifaces_needed = req_combo.at(type);
1720 size_t num_ifaces_allowed = expanded_combo.at(type);
1721 if (num_ifaces_needed > num_ifaces_allowed) {
1722 return false;
1723 }
1724 }
1725 return true;
1726}
1727
1728// This method does the following:
1729// a) Enumerate all possible concurrency combos by expanding the current
1730// ChipConcurrencyCombination.
1731// b) Check if the requested concurrency combo can be added to the current mode.
1732// Note: This does not consider concurrency types already active. It only checks if the
1733// current mode can support the requested combo.
1734bool WifiChip::canCurrentModeSupportConcurrencyCombo(
1735 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
1736 if (!isValidModeId(current_mode_id_)) {
1737 LOG(ERROR) << "Chip not configured in a mode yet";
1738 return false;
1739 }
1740 const auto combinations = getCurrentModeConcurrencyCombinations();
1741 for (const auto& combination : combinations) {
1742 const auto expanded_combos = expandConcurrencyCombinations(combination);
1743 for (const auto& expanded_combo : expanded_combos) {
1744 if (canExpandedConcurrencyComboSupportConcurrencyCombo(expanded_combo, req_combo)) {
1745 return true;
1746 }
1747 }
1748 }
1749 return false;
1750}
1751
1752// This method does the following:
1753// a) Enumerate all possible concurrency combos by expanding the current
1754// ChipConcurrencyCombination.
1755// b) Check if the requested concurrency type can be added to the current mode.
1756bool WifiChip::canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type) {
1757 // Check if we can support at least 1 of the requested concurrency type.
1758 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1759 req_iface_combo[requested_type] = 1;
1760 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
1761}
1762
1763bool WifiChip::isValidModeId(int32_t mode_id) {
1764 for (const auto& mode : modes_) {
1765 if (mode.id == mode_id) {
1766 return true;
1767 }
1768 }
1769 return false;
1770}
1771
1772bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1773 // Check if we can support at least 1 STA & 1 AP concurrently.
1774 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1775 req_iface_combo[IfaceConcurrencyType::STA] = 1;
1776 req_iface_combo[IfaceConcurrencyType::AP] = 1;
1777 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
1778}
1779
1780bool WifiChip::isDualStaConcurrencyAllowedInCurrentMode() {
1781 // Check if we can support at least 2 STA concurrently.
1782 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1783 req_iface_combo[IfaceConcurrencyType::STA] = 2;
1784 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
1785}
1786
1787std::string WifiChip::getFirstActiveWlanIfaceName() {
1788 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1789 if (ap_ifaces_.size() > 0) {
1790 // If the first active wlan iface is bridged iface.
1791 // Return first instance name.
1792 for (auto const& it : br_ifaces_ap_instances_) {
1793 if (it.first == ap_ifaces_[0]->getName()) {
1794 return it.second[0];
1795 }
1796 }
1797 return ap_ifaces_[0]->getName();
1798 }
1799 // This could happen if the chip call is made before any STA/AP
1800 // iface is created. Default to wlan0 for such cases.
1801 LOG(WARNING) << "No active wlan interfaces in use! Using default";
1802 return getWlanIfaceNameWithType(IfaceType::STA, 0);
1803}
1804
1805// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1806// not already in use.
1807// Note: This doesn't check the actual presence of these interfaces.
1808std::string WifiChip::allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx) {
1809 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1810 const auto ifname = getWlanIfaceNameWithType(type, idx);
1811 if (findUsingNameFromBridgedApInstances(ifname)) continue;
1812 if (findUsingName(ap_ifaces_, ifname)) continue;
1813 if (findUsingName(sta_ifaces_, ifname)) continue;
1814 return ifname;
1815 }
1816 // This should never happen. We screwed up somewhere if it did.
1817 CHECK(false) << "All wlan interfaces in use already!";
1818 return {};
1819}
1820
1821uint32_t WifiChip::startIdxOfApIface() {
1822 if (isDualStaConcurrencyAllowedInCurrentMode()) {
1823 // When the HAL support dual STAs, AP should start with idx 2.
1824 return 2;
1825 } else if (isStaApConcurrencyAllowedInCurrentMode()) {
1826 // When the HAL support STA + AP but it doesn't support dual STAs.
1827 // AP should start with idx 1.
1828 return 1;
1829 }
1830 // No concurrency support.
1831 return 0;
1832}
1833
1834// AP iface names start with idx 1 for modes supporting
1835// concurrent STA and not dual AP, else start with idx 0.
1836std::string WifiChip::allocateApIfaceName() {
1837 // Check if we have a dedicated iface for AP.
1838 std::vector<std::string> ifnames = getPredefinedApIfaceNames(true);
1839 for (auto const& ifname : ifnames) {
1840 if (findUsingName(ap_ifaces_, ifname)) continue;
1841 return ifname;
1842 }
1843 return allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface());
1844}
1845
1846std::vector<std::string> WifiChip::allocateBridgedApInstanceNames() {
1847 // Check if we have a dedicated iface for AP.
1848 std::vector<std::string> instances = getPredefinedApIfaceNames(true);
1849 if (instances.size() == 2) {
1850 return instances;
1851 } else {
1852 int num_ifaces_need_to_allocate = 2 - instances.size();
1853 for (int i = 0; i < num_ifaces_need_to_allocate; i++) {
1854 std::string instance_name =
1855 allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface() + i);
1856 if (!instance_name.empty()) {
1857 instances.push_back(instance_name);
1858 }
1859 }
1860 }
1861 return instances;
1862}
1863
1864// STA iface names start with idx 0.
1865// Primary STA iface will always be 0.
1866std::string WifiChip::allocateStaIfaceName() {
1867 return allocateApOrStaIfaceName(IfaceType::STA, 0);
1868}
1869
1870bool WifiChip::writeRingbufferFilesInternal() {
1871 if (!removeOldFilesInternal()) {
1872 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1873 return false;
1874 }
1875 // write ringbuffers to file
1876 {
1877 std::unique_lock<std::mutex> lk(lock_t);
1878 for (auto& item : ringbuffer_map_) {
1879 Ringbuffer& cur_buffer = item.second;
1880 if (cur_buffer.getData().empty()) {
1881 continue;
1882 }
1883 const std::string file_path_raw = kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1884 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1885 if (dump_fd == -1) {
1886 PLOG(ERROR) << "create file failed";
1887 return false;
1888 }
1889 unique_fd file_auto_closer(dump_fd);
1890 for (const auto& cur_block : cur_buffer.getData()) {
1891 if (cur_block.size() <= 0 || cur_block.size() > kMaxBufferSizeBytes) {
1892 PLOG(ERROR) << "Ring buffer: " << item.first
1893 << " is corrupted. Invalid block size: " << cur_block.size();
1894 break;
1895 }
1896 if (write(dump_fd, cur_block.data(), sizeof(cur_block[0]) * cur_block.size()) ==
1897 -1) {
1898 PLOG(ERROR) << "Error writing to file";
1899 }
1900 }
1901 cur_buffer.clear();
1902 }
1903 // unique_lock unlocked here
1904 }
1905 return true;
1906}
1907
1908std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1909 std::string ifname;
1910
1911 // let the legacy hal override the interface name
1912 legacy_hal::wifi_error err = legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
1913 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1914
1915 return getWlanIfaceName(idx);
1916}
1917
1918void WifiChip::invalidateAndClearBridgedApAll() {
1919 for (auto const& it : br_ifaces_ap_instances_) {
1920 for (auto const& iface : it.second) {
1921 iface_util_->removeIfaceFromBridge(it.first, iface);
1922 legacy_hal_.lock()->deleteVirtualInterface(iface);
1923 }
1924 iface_util_->deleteBridge(it.first);
1925 }
1926 br_ifaces_ap_instances_.clear();
1927}
1928
1929void WifiChip::invalidateAndClearBridgedAp(const std::string& br_name) {
1930 if (br_name.empty()) return;
1931 // delete managed interfaces
1932 for (auto const& it : br_ifaces_ap_instances_) {
1933 if (it.first == br_name) {
1934 for (auto const& iface : it.second) {
1935 iface_util_->removeIfaceFromBridge(br_name, iface);
1936 legacy_hal_.lock()->deleteVirtualInterface(iface);
1937 }
1938 iface_util_->deleteBridge(br_name);
1939 br_ifaces_ap_instances_.erase(br_name);
1940 break;
1941 }
1942 }
1943 return;
1944}
1945
1946bool WifiChip::findUsingNameFromBridgedApInstances(const std::string& name) {
1947 for (auto const& it : br_ifaces_ap_instances_) {
1948 if (it.first == name) {
1949 return true;
1950 }
1951 for (auto const& iface : it.second) {
1952 if (iface == name) {
1953 return true;
1954 }
1955 }
1956 }
1957 return false;
1958}
1959
1960} // namespace wifi
1961} // namespace hardware
1962} // namespace android
1963} // namespace aidl