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