blob: 4fff770dd202acabaea5a657f7d3566a26f97f8e [file] [log] [blame]
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001/*
2 * Copyright (C) 2016 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
xshu5899e8e2018-01-09 15:36:03 -080017#include <fcntl.h>
18
Roshan Pius3c4e8a32016-10-03 14:53:58 -070019#include <android-base/logging.h>
xshu5899e8e2018-01-09 15:36:03 -080020#include <android-base/unique_fd.h>
Roshan Pius9377a0d2017-10-06 13:18:54 -070021#include <cutils/properties.h>
lesl94d28242020-11-18 22:17:37 +080022#include <net/if.h>
xshu5899e8e2018-01-09 15:36:03 -080023#include <sys/stat.h>
24#include <sys/sysmacros.h>
Roshan Pius3c4e8a32016-10-03 14:53:58 -070025
Roshan Pius3c868522016-10-27 12:43:49 -070026#include "hidl_return_util.h"
Roshan Piuse2d0ab52016-12-05 15:24:20 -080027#include "hidl_struct_util.h"
Roshan Pius3c868522016-10-27 12:43:49 -070028#include "wifi_chip.h"
Roshan Pius5c055462016-10-11 08:27:27 -070029#include "wifi_status_util.h"
Roshan Pius3c4e8a32016-10-03 14:53:58 -070030
Sunil Ravi7f2822a2021-10-15 16:55:53 -070031#define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
32
Roshan Pius35d958c2016-10-06 16:47:38 -070033namespace {
Jong Wook Kimda830c92018-07-23 15:29:38 -070034using android::sp;
xshu5899e8e2018-01-09 15:36:03 -080035using android::base::unique_fd;
Roshan Pius35d958c2016-10-06 16:47:38 -070036using android::hardware::hidl_string;
Roshan Piusabcf78f2017-10-06 16:30:38 -070037using android::hardware::hidl_vec;
Roshan Pius2c06a3f2016-12-15 17:51:40 -080038using android::hardware::wifi::V1_0::ChipModeId;
Roshan Pius52947fb2016-11-18 11:38:07 -080039using android::hardware::wifi::V1_0::IfaceType;
Roshan Pius675609b2017-10-31 14:24:58 -070040using android::hardware::wifi::V1_0::IWifiChip;
Roshan Pius52947fb2016-11-18 11:38:07 -080041
xshu5899e8e2018-01-09 15:36:03 -080042constexpr char kCpioMagic[] = "070701";
Roger Wangb294c762018-11-02 15:34:39 +080043constexpr size_t kMaxBufferSizeBytes = 1024 * 1024 * 3;
44constexpr uint32_t kMaxRingBufferFileAgeSeconds = 60 * 60 * 10;
xshu37126c92018-04-13 16:24:45 -070045constexpr uint32_t kMaxRingBufferFileNum = 20;
xshu5899e8e2018-01-09 15:36:03 -080046constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
Roshan Pius8574e7f2019-04-01 13:30:40 -070047constexpr char kActiveWlanIfaceNameProperty[] = "wifi.active.interface";
48constexpr char kNoActiveWlanIfaceNamePropertyValue[] = "";
49constexpr unsigned kMaxWlanIfaces = 5;
lesl94d28242020-11-18 22:17:37 +080050constexpr char kApBridgeIfacePrefix[] = "ap_br_";
xshu5899e8e2018-01-09 15:36:03 -080051
Roshan Pius35d958c2016-10-06 16:47:38 -070052template <typename Iface>
Roshan Pius675609b2017-10-31 14:24:58 -070053void invalidateAndClear(std::vector<sp<Iface>>& ifaces, sp<Iface> iface) {
54 iface->invalidate();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080055 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface), ifaces.end());
Roshan Pius675609b2017-10-31 14:24:58 -070056}
57
58template <typename Iface>
59void invalidateAndClearAll(std::vector<sp<Iface>>& ifaces) {
60 for (const auto& iface : ifaces) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070061 iface->invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -070062 }
Roshan Pius675609b2017-10-31 14:24:58 -070063 ifaces.clear();
64}
65
66template <typename Iface>
67std::vector<hidl_string> getNames(std::vector<sp<Iface>>& ifaces) {
68 std::vector<hidl_string> names;
69 for (const auto& iface : ifaces) {
70 names.emplace_back(iface->getName());
71 }
72 return names;
73}
74
75template <typename Iface>
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080076sp<Iface> findUsingName(std::vector<sp<Iface>>& ifaces, const std::string& name) {
Roshan Pius675609b2017-10-31 14:24:58 -070077 std::vector<hidl_string> names;
78 for (const auto& iface : ifaces) {
79 if (name == iface->getName()) {
80 return iface;
81 }
82 }
83 return nullptr;
Roshan Pius35d958c2016-10-06 16:47:38 -070084}
Roshan Pius9377a0d2017-10-06 13:18:54 -070085
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080086std::string getWlanIfaceName(unsigned idx) {
87 if (idx >= kMaxWlanIfaces) {
88 CHECK(false) << "Requested interface beyond wlan" << kMaxWlanIfaces;
89 return {};
90 }
Roshan Pius9377a0d2017-10-06 13:18:54 -070091
Roshan Pius8e3c7ef2017-11-03 09:43:08 -070092 std::array<char, PROPERTY_VALUE_MAX> buffer;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080093 if (idx == 0 || idx == 1) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -080094 const char* altPropName = (idx == 0) ? "wifi.interface" : "wifi.concurrent.interface";
Roshan Pius5b333462019-03-01 14:07:22 -080095 auto res = property_get(altPropName, buffer.data(), nullptr);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080096 if (res > 0) return buffer.data();
97 }
Roshan Pius5b333462019-03-01 14:07:22 -080098 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();
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800101
102 return "wlan" + std::to_string(idx);
Roshan Pius9377a0d2017-10-06 13:18:54 -0700103}
Roshan Pius9377a0d2017-10-06 13:18:54 -0700104
lesl261818b2020-11-27 12:37:35 +0800105// 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;
Roshan Pius78cb5992020-04-30 12:39:21 -0700109 std::array<char, PROPERTY_VALUE_MAX> buffer;
lesl261818b2020-11-27 12:37:35 +0800110 buffer.fill(0);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800111 if (property_get("ro.vendor.wifi.sap.interface", buffer.data(), nullptr) == 0) {
lesl261818b2020-11-27 12:37:35 +0800112 return ifnames;
Roshan Pius78cb5992020-04-30 12:39:21 -0700113 }
lesl261818b2020-11-27 12:37:35 +0800114 ifnames.push_back(buffer.data());
115 if (is_bridged) {
116 buffer.fill(0);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800117 if (property_get("ro.vendor.wifi.sap.concurrent.iface", buffer.data(), nullptr) == 0) {
lesl261818b2020-11-27 12:37:35 +0800118 return ifnames;
119 }
120 ifnames.push_back(buffer.data());
121 }
122 return ifnames;
Roshan Pius78cb5992020-04-30 12:39:21 -0700123}
124
lesl261818b2020-11-27 12:37:35 +0800125std::string getPredefinedP2pIfaceName() {
Sunil Ravi7f2822a2021-10-15 16:55:53 -0700126 std::array<char, PROPERTY_VALUE_MAX> primaryIfaceName;
127 char p2pParentIfname[100];
128 std::string p2pDevIfName = "";
Roshan Piusabcf78f2017-10-06 16:30:38 -0700129 std::array<char, PROPERTY_VALUE_MAX> buffer;
130 property_get("wifi.direct.interface", buffer.data(), "p2p0");
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800131 if (strncmp(buffer.data(), P2P_MGMT_DEVICE_PREFIX, strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
Sunil Ravi7f2822a2021-10-15 16:55:53 -0700132 /* Get the p2p parent interface name from p2p device interface name set
133 * in property */
134 strncpy(p2pParentIfname, buffer.data() + strlen(P2P_MGMT_DEVICE_PREFIX),
135 strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX));
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800136 if (property_get(kActiveWlanIfaceNameProperty, primaryIfaceName.data(), nullptr) == 0) {
Sunil Ravi7f2822a2021-10-15 16:55:53 -0700137 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(),
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800142 strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX)) != 0) {
Sunil Ravi7f2822a2021-10-15 16:55:53 -0700143 /*
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();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800149 LOG(INFO) << "update the p2p device interface name to " << p2pDevIfName.c_str();
Sunil Ravi7f2822a2021-10-15 16:55:53 -0700150 return p2pDevIfName;
151 }
152 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700153 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700154}
155
Roshan Pius5ba0a902020-04-14 11:55:42 -0700156// Returns the dedicated iface name if one is defined.
lesl261818b2020-11-27 12:37:35 +0800157std::string getPredefinedNanIfaceName() {
Roshan Pius5ba0a902020-04-14 11:55:42 -0700158 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
Roshan Pius8574e7f2019-04-01 13:30:40 -0700165void 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
xshu37126c92018-04-13 16:24:45 -0700172// delete files that meet either conditions:
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
xshu5899e8e2018-01-09 15:36:03 -0800175bool removeOldFilesInternal() {
176 time_t now = time(0);
177 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800178 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(kTombstoneFolderPath), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800179 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800180 PLOG(ERROR) << "Failed to open directory";
xshu5899e8e2018-01-09 15:36:03 -0800181 return false;
182 }
xshu5899e8e2018-01-09 15:36:03 -0800183 struct dirent* dp;
184 bool success = true;
xshu37126c92018-04-13 16:24:45 -0700185 std::list<std::pair<const time_t, std::string>> valid_files;
Josh Gaoa568e532018-06-04 18:16:00 -0700186 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800187 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;
xshu4cb33162018-01-24 15:40:06 -0800193 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800194 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800195 success = false;
xshu4cb33162018-01-24 15:40:06 -0800196 continue;
197 }
xshu37126c92018-04-13 16:24:45 -0700198 const time_t cur_file_time = cur_file_stat.st_mtime;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800199 valid_files.push_back(std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
xshu37126c92018-04-13 16:24:45 -0700200 }
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) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800205 if (cur_file_count > kMaxRingBufferFileNum || cur_file.first < delete_files_before) {
xshu37126c92018-04-13 16:24:45 -0700206 if (unlink(cur_file.second.c_str()) != 0) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800207 PLOG(ERROR) << "Error deleting file";
xshu37126c92018-04-13 16:24:45 -0700208 success = false;
209 }
210 cur_file_count--;
211 } else {
212 break;
xshu5899e8e2018-01-09 15:36:03 -0800213 }
214 }
215 return success;
216}
217
xshu4cb33162018-01-24 15:40:06 -0800218// Helper function for |cpioArchiveFilesInDir|
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800219bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name, size_t file_name_len) {
xshu4cb33162018-01-24 15:40:06 -0800220 std::array<char, 32 * 1024> read_buf;
221 ssize_t llen =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800222 sprintf(read_buf.data(), "%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),
226 major(st.st_rdev), minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
xshu4cb33162018-01-24 15:40:06 -0800227 if (write(out_fd, read_buf.data(), llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800228 PLOG(ERROR) << "Error writing cpio header to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800229 return false;
230 }
231 if (write(out_fd, file_name, file_name_len) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800232 PLOG(ERROR) << "Error writing filename to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800233 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) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800241 PLOG(ERROR) << "Error padding 0s to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800242 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) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800257 PLOG(ERROR) << "Error reading file";
xshu4cb33162018-01-24 15:40:06 -0800258 return ++n_error;
259 }
260 llen -= bytes_read;
261 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800262 PLOG(ERROR) << "Error writing data to file";
xshu4cb33162018-01-24 15:40:06 -0800263 return ++n_error;
264 }
265 if (bytes_read == 0) { // this should never happen, but just in case
266 // to unstuck from while loop
Elliott Hughes4db4add2019-03-08 12:42:57 -0800267 PLOG(ERROR) << "Unexpected read result";
xshu4cb33162018-01-24 15:40:06 -0800268 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) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800276 PLOG(ERROR) << "Error padding 0s to file";
xshu4cb33162018-01-24 15:40:06 -0800277 return ++n_error;
278 }
279 }
280 return n_error;
281}
282
283// Helper function for |cpioArchiveFilesInDir|
284bool cpioWriteFileTrailer(int out_fd) {
285 std::array<char, 4096> read_buf;
286 read_buf.fill(0);
287 if (write(out_fd, read_buf.data(),
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800288 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0) + 4) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800289 PLOG(ERROR) << "Error writing trailing bytes";
xshu4cb33162018-01-24 15:40:06 -0800290 return false;
291 }
292 return true;
293}
294
xshu5899e8e2018-01-09 15:36:03 -0800295// Archives all files in |input_dir| and writes result into |out_fd|
296// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
297// portion
xshu4cb33162018-01-24 15:40:06 -0800298size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800299 struct dirent* dp;
300 size_t n_error = 0;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800301 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800302 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800303 PLOG(ERROR) << "Failed to open directory";
xshu4cb33162018-01-24 15:40:06 -0800304 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800305 }
Josh Gaoa568e532018-06-04 18:16:00 -0700306 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800307 if (dp->d_type != DT_REG) {
308 continue;
309 }
310 std::string cur_file_name(dp->d_name);
xshu5899e8e2018-01-09 15:36:03 -0800311 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800312 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800313 if (stat(cur_file_path.c_str(), &st) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800314 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800315 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800316 continue;
317 }
318 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
319 if (fd_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800320 PLOG(ERROR) << "Failed to open file " << cur_file_path;
xshu4cb33162018-01-24 15:40:06 -0800321 n_error++;
322 continue;
323 }
xshuf392fb42020-08-13 16:57:00 -0700324 std::string file_name_with_last_modified_time =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800325 cur_file_name + "-" + std::to_string(st.st_mtime);
xshuf392fb42020-08-13 16:57:00 -0700326 // string.size() does not include the null terminator. The cpio FreeBSD
327 // file header expects the null character to be included in the length.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800328 const size_t file_name_len = file_name_with_last_modified_time.size() + 1;
xshu4cb33162018-01-24 15:40:06 -0800329 unique_fd file_auto_closer(fd_read);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800330 if (!cpioWriteHeader(out_fd, st, file_name_with_last_modified_time.c_str(),
xshu4cb33162018-01-24 15:40:06 -0800331 file_name_len)) {
332 return ++n_error;
333 }
334 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
335 if (write_error) {
336 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800337 }
338 }
xshu4cb33162018-01-24 15:40:06 -0800339 if (!cpioWriteFileTrailer(out_fd)) {
340 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800341 }
342 return n_error;
343}
344
345// Helper function to create a non-const char*.
346std::vector<char> makeCharVec(const std::string& str) {
347 std::vector<char> vec(str.size() + 1);
348 vec.assign(str.begin(), str.end());
349 vec.push_back('\0');
350 return vec;
351}
352
Roshan Piusabcf78f2017-10-06 16:30:38 -0700353} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700354
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700355namespace android {
356namespace hardware {
357namespace wifi {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800358namespace V1_6 {
Roshan Pius79a99752016-10-04 13:03:58 -0700359namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700360using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800361using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700362
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800363WifiChip::WifiChip(ChipId 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)
Roshan Pius52947fb2016-11-18 11:38:07 -0800369 : chip_id_(chip_id),
370 legacy_hal_(legacy_hal),
371 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800372 iface_util_(iface_util),
Roshan Pius52947fb2016-11-18 11:38:07 -0800373 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800374 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
Jimmy Chen2dddd792019-12-23 17:50:39 +0200375 modes_(feature_flags.lock()->getChipModes(is_primary)),
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700376 debug_ring_buffer_cb_registered_(false),
377 subsystemCallbackHandler_(handler) {
Roshan Pius8574e7f2019-04-01 13:30:40 -0700378 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
379}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700380
Roshan Piusaabe5752016-09-29 09:03:59 -0700381void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700382 if (!writeRingbufferFilesInternal()) {
383 LOG(ERROR) << "Error writing files to flash";
384 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700385 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700386 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700387 legacy_hal_.reset();
388 event_cb_handler_.invalidate();
389 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700390}
391
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800392bool WifiChip::isValid() {
393 return is_valid_;
394}
Roshan Pius3c868522016-10-27 12:43:49 -0700395
Jimmy Chend460df32019-11-29 17:31:22 +0200396std::set<sp<V1_4::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700397 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800398}
399
Roshan Pius5c055462016-10-11 08:27:27 -0700400Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800401 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, &WifiChip::getIdInternal,
402 hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700403}
404
Jong Wook Kimda830c92018-07-23 15:29:38 -0700405// Deprecated support for this callback
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800406Return<void> WifiChip::registerEventCallback(const sp<V1_0::IWifiChipEventCallback>& event_callback,
407 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700408 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800409 &WifiChip::registerEventCallbackInternal, hidl_status_cb,
410 event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700411}
412
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700413Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700414 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
415 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700416}
417
Roshan Pius5c055462016-10-11 08:27:27 -0700418Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700419 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800420 &WifiChip::getAvailableModesInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700421}
422
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800423Return<void> WifiChip::configureChip(ChipModeId mode_id, configureChip_cb hidl_status_cb) {
424 return validateAndCallWithLock(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
425 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700426}
427
Roshan Pius5c055462016-10-11 08:27:27 -0700428Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700429 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
430 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700431}
432
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800433Return<void> WifiChip::requestChipDebugInfo(requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700434 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800435 &WifiChip::requestChipDebugInfoInternal, hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700436}
437
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800438Return<void> WifiChip::requestDriverDebugDump(requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700439 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800440 &WifiChip::requestDriverDebugDumpInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700441}
442
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800443Return<void> WifiChip::requestFirmwareDebugDump(requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700444 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800445 &WifiChip::requestFirmwareDebugDumpInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700446}
447
Roshan Pius5c055462016-10-11 08:27:27 -0700448Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700449 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
450 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700451}
452
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800453Return<void> WifiChip::createBridgedApIface(createBridgedApIface_cb hidl_status_cb) {
lesl94d28242020-11-18 22:17:37 +0800454 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800455 &WifiChip::createBridgedApIfaceInternal, hidl_status_cb);
lesl94d28242020-11-18 22:17:37 +0800456}
457
Roshan Pius5c055462016-10-11 08:27:27 -0700458Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700459 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
460 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700461}
462
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800463Return<void> WifiChip::getApIface(const hidl_string& ifname, getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700464 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800465 &WifiChip::getApIfaceInternal, hidl_status_cb, ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700466}
467
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800468Return<void> WifiChip::removeApIface(const hidl_string& ifname, removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700469 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800470 &WifiChip::removeApIfaceInternal, hidl_status_cb, ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800471}
472
lesl94d28242020-11-18 22:17:37 +0800473Return<void> WifiChip::removeIfaceInstanceFromBridgedApIface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800474 const hidl_string& ifname, const hidl_string& ifInstanceName,
475 removeIfaceInstanceFromBridgedApIface_cb hidl_status_cb) {
476 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
477 &WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal, hidl_status_cb,
478 ifname, ifInstanceName);
lesl94d28242020-11-18 22:17:37 +0800479}
480
Roshan Pius5c055462016-10-11 08:27:27 -0700481Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700482 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
483 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700484}
485
Roshan Pius5c055462016-10-11 08:27:27 -0700486Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700487 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
488 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700489}
490
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800491Return<void> WifiChip::getNanIface(const hidl_string& ifname, getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700492 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800493 &WifiChip::getNanIfaceInternal, hidl_status_cb, ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700494}
495
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800496Return<void> WifiChip::removeNanIface(const hidl_string& ifname, removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700497 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800498 &WifiChip::removeNanIfaceInternal, hidl_status_cb, ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800499}
500
Roshan Pius5c055462016-10-11 08:27:27 -0700501Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700502 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
503 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700504}
505
Roshan Pius5c055462016-10-11 08:27:27 -0700506Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700507 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
508 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700509}
510
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800511Return<void> WifiChip::getP2pIface(const hidl_string& ifname, getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700512 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800513 &WifiChip::getP2pIfaceInternal, hidl_status_cb, ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700514}
515
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800516Return<void> WifiChip::removeP2pIface(const hidl_string& ifname, removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700517 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800518 &WifiChip::removeP2pIfaceInternal, hidl_status_cb, ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800519}
520
Roshan Pius5c055462016-10-11 08:27:27 -0700521Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700522 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
523 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700524}
525
Roshan Pius5c055462016-10-11 08:27:27 -0700526Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700527 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
528 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700529}
530
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800531Return<void> WifiChip::getStaIface(const hidl_string& ifname, getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700532 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800533 &WifiChip::getStaIfaceInternal, hidl_status_cb, ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700534}
535
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800536Return<void> WifiChip::removeStaIface(const hidl_string& ifname, removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700537 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800538 &WifiChip::removeStaIfaceInternal, hidl_status_cb, ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800539}
540
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800541Return<void> WifiChip::createRttController(const sp<IWifiIface>& bound_iface,
542 createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700543 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800544 &WifiChip::createRttControllerInternal, hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700545}
546
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800547Return<void> WifiChip::getDebugRingBuffersStatus(getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700548 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800549 &WifiChip::getDebugRingBuffersStatusInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700550}
551
552Return<void> WifiChip::startLoggingToDebugRingBuffer(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800553 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
554 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
555 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700556 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800557 &WifiChip::startLoggingToDebugRingBufferInternal, hidl_status_cb,
558 ring_name, verbose_level, max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700559}
560
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800561Return<void> WifiChip::forceDumpToDebugRingBuffer(const hidl_string& ring_name,
562 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700563 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800564 &WifiChip::forceDumpToDebugRingBufferInternal, hidl_status_cb,
565 ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700566}
567
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800568Return<void> WifiChip::flushRingBufferToFile(flushRingBufferToFile_cb hidl_status_cb) {
Roger Wangb294c762018-11-02 15:34:39 +0800569 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800570 &WifiChip::flushRingBufferToFileInternal, hidl_status_cb);
Roger Wangb294c762018-11-02 15:34:39 +0800571}
572
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800573Return<void> WifiChip::stopLoggingToDebugRingBuffer(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800574 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700575 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800576 &WifiChip::stopLoggingToDebugRingBufferInternal, hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800577}
578
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800579Return<void> WifiChip::getDebugHostWakeReasonStats(getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700580 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800581 &WifiChip::getDebugHostWakeReasonStatsInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700582}
583
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800584Return<void> WifiChip::enableDebugErrorAlerts(bool enable,
585 enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700586 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800587 &WifiChip::enableDebugErrorAlertsInternal, hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800588}
589
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800590Return<void> WifiChip::selectTxPowerScenario(V1_1::IWifiChip::TxPowerScenario scenario,
591 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700592 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800593 &WifiChip::selectTxPowerScenarioInternal, hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700594}
595
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800596Return<void> WifiChip::resetTxPowerScenario(resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700597 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800598 &WifiChip::resetTxPowerScenarioInternal, hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700599}
600
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800601Return<void> WifiChip::setLatencyMode(LatencyMode mode, setLatencyMode_cb hidl_status_cb) {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700602 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800603 &WifiChip::setLatencyModeInternal, hidl_status_cb, mode);
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700604}
605
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800606Return<void> WifiChip::registerEventCallback_1_2(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800607 const sp<V1_2::IWifiChipEventCallback>& event_callback,
608 registerEventCallback_cb hidl_status_cb) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800609 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800610 &WifiChip::registerEventCallbackInternal_1_2, hidl_status_cb,
611 event_callback);
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800612}
613
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800614Return<void> WifiChip::selectTxPowerScenario_1_2(TxPowerScenario scenario,
615 selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800616 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800617 &WifiChip::selectTxPowerScenarioInternal_1_2, hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800618}
619
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700620Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
621 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800622 &WifiChip::getCapabilitiesInternal_1_3, hidl_status_cb);
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700623}
624
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800625Return<void> WifiChip::getCapabilities_1_5(getCapabilities_1_5_cb hidl_status_cb) {
Jimmy Chen1bdf1a72019-12-23 17:53:40 +0200626 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800627 &WifiChip::getCapabilitiesInternal_1_5, hidl_status_cb);
Jimmy Chen1bdf1a72019-12-23 17:53:40 +0200628}
629
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800630Return<void> WifiChip::debug(const hidl_handle& handle, const hidl_vec<hidl_string>&) {
xshu5899e8e2018-01-09 15:36:03 -0800631 if (handle != nullptr && handle->numFds >= 1) {
xshu0a0fe512020-07-22 17:53:37 -0700632 {
633 std::unique_lock<std::mutex> lk(lock_t);
634 for (const auto& item : ringbuffer_map_) {
635 forceDumpToDebugRingBufferInternal(item.first);
636 }
637 // unique_lock unlocked here
638 }
639 usleep(100 * 1000); // sleep for 100 milliseconds to wait for
640 // ringbuffer updates.
xshu5899e8e2018-01-09 15:36:03 -0800641 int fd = handle->data[0];
642 if (!writeRingbufferFilesInternal()) {
643 LOG(ERROR) << "Error writing files to flash";
644 }
xshu4cb33162018-01-24 15:40:06 -0800645 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800646 if (n_error != 0) {
647 LOG(ERROR) << n_error << " errors occured in cpio function";
648 }
649 fsync(fd);
650 } else {
651 LOG(ERROR) << "File handle error";
652 }
653 return Void();
654}
655
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800656Return<void> WifiChip::createRttController_1_4(const sp<IWifiIface>& bound_iface,
657 createRttController_1_4_cb hidl_status_cb) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700658 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800659 &WifiChip::createRttControllerInternal_1_4, hidl_status_cb, bound_iface);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700660}
661
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800662Return<void> WifiChip::registerEventCallback_1_4(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800663 const sp<V1_4::IWifiChipEventCallback>& event_callback,
664 registerEventCallback_cb hidl_status_cb) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800665 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800666 &WifiChip::registerEventCallbackInternal_1_4, hidl_status_cb,
667 event_callback);
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800668}
669
Roshan Piuse9d1e7d2020-11-04 11:44:16 -0800670Return<void> WifiChip::setMultiStaPrimaryConnection(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800671 const hidl_string& ifname, setMultiStaPrimaryConnection_cb hidl_status_cb) {
Roshan Piuse9d1e7d2020-11-04 11:44:16 -0800672 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800673 &WifiChip::setMultiStaPrimaryConnectionInternal, hidl_status_cb, ifname);
Roshan Piuse9d1e7d2020-11-04 11:44:16 -0800674}
675
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800676Return<void> WifiChip::setMultiStaUseCase(MultiStaUseCase use_case,
677 setMultiStaUseCase_cb hidl_status_cb) {
Roshan Piuse9d1e7d2020-11-04 11:44:16 -0800678 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800679 &WifiChip::setMultiStaUseCaseInternal, hidl_status_cb, use_case);
Roshan Piuse9d1e7d2020-11-04 11:44:16 -0800680}
681
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800682Return<void> WifiChip::setCoexUnsafeChannels(const hidl_vec<CoexUnsafeChannel>& unsafeChannels,
683 hidl_bitfield<CoexRestriction> restrictions,
684 setCoexUnsafeChannels_cb hidl_status_cb) {
Quang Luong94bcce52020-11-25 17:52:19 -0800685 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800686 &WifiChip::setCoexUnsafeChannelsInternal, hidl_status_cb, unsafeChannels,
687 restrictions);
Quang Luong94bcce52020-11-25 17:52:19 -0800688}
689
Kumar Anandda62c382020-11-18 17:17:47 -0800690Return<void> WifiChip::setCountryCode(const hidl_array<int8_t, 2>& code,
691 setCountryCode_cb hidl_status_cb) {
692 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800693 &WifiChip::setCountryCodeInternal, hidl_status_cb, code);
Kumar Anandda62c382020-11-18 17:17:47 -0800694}
695
Kumar Anand2a630a32021-01-21 14:09:14 -0800696Return<void> WifiChip::getUsableChannels(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800697 WifiBand band, hidl_bitfield<V1_5::WifiIfaceMode> ifaceModeMask,
698 hidl_bitfield<V1_5::IWifiChip::UsableChannelFilter> filterMask,
699 getUsableChannels_cb _hidl_cb) {
Kumar Anand2a630a32021-01-21 14:09:14 -0800700 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800701 &WifiChip::getUsableChannelsInternal, _hidl_cb, band, ifaceModeMask,
702 filterMask);
Kumar Anand2a630a32021-01-21 14:09:14 -0800703}
704
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800705Return<void> WifiChip::triggerSubsystemRestart(triggerSubsystemRestart_cb hidl_status_cb) {
chenpaulc6f57032021-03-05 17:06:50 +0800706 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800707 &WifiChip::triggerSubsystemRestartInternal, hidl_status_cb);
chenpaulc6f57032021-03-05 17:06:50 +0800708}
709
Ahmed ElArabawy05571e42022-01-19 11:54:11 -0800710Return<void> WifiChip::createRttController_1_6(const sp<IWifiIface>& bound_iface,
711 createRttController_1_6_cb hidl_status_cb) {
712 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
713 &WifiChip::createRttControllerInternal_1_6, hidl_status_cb, bound_iface);
714}
715
716Return<void> WifiChip::getUsableChannels_1_6(
717 WifiBand band, hidl_bitfield<V1_5::WifiIfaceMode> ifaceModeMask,
Nate Jiang6e135992022-01-24 12:14:23 -0800718 hidl_bitfield<V1_6::IWifiChip::UsableChannelFilter> filterMask,
Ahmed ElArabawy05571e42022-01-19 11:54:11 -0800719 getUsableChannels_1_6_cb _hidl_cb) {
720 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
721 &WifiChip::getUsableChannelsInternal_1_6, _hidl_cb, band, ifaceModeMask,
722 filterMask);
723}
724
Sunil Ravief97d232022-01-24 10:39:56 -0800725Return<void> WifiChip::getSupportedRadioCombinationsMatrix(
726 getSupportedRadioCombinationsMatrix_cb hidl_status_cb) {
727 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
728 &WifiChip::getSupportedRadioCombinationsMatrixInternal, hidl_status_cb);
729}
730
Roshan Pius35d958c2016-10-06 16:47:38 -0700731void WifiChip::invalidateAndRemoveAllIfaces() {
lesl94d28242020-11-18 22:17:37 +0800732 invalidateAndClearBridgedApAll();
Roshan Pius675609b2017-10-31 14:24:58 -0700733 invalidateAndClearAll(ap_ifaces_);
734 invalidateAndClearAll(nan_ifaces_);
735 invalidateAndClearAll(p2p_ifaces_);
736 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700737 // Since all the ifaces are invalid now, all RTT controller objects
738 // using those ifaces also need to be invalidated.
739 for (const auto& rtt : rtt_controllers_) {
740 rtt->invalidate();
741 }
742 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700743}
744
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800745void WifiChip::invalidateAndRemoveDependencies(const std::string& removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200746 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
747 auto nan_iface = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700748 if (nan_iface->getName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200749 nan_iface->invalidate();
Roshan Pius82368502019-05-16 12:53:02 -0700750 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800751 if (!callback->onIfaceRemoved(IfaceType::NAN, removed_iface_name).isOk()) {
Roshan Pius82368502019-05-16 12:53:02 -0700752 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
753 }
754 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200755 it = nan_ifaces_.erase(it);
756 } else {
757 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700758 }
759 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200760
761 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
762 auto rtt = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700763 if (rtt->getIfaceName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200764 rtt->invalidate();
765 it = rtt_controllers_.erase(it);
766 } else {
767 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700768 }
769 }
770}
771
Roshan Pius3c868522016-10-27 12:43:49 -0700772std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700773 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700774}
775
776WifiStatus WifiChip::registerEventCallbackInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800777 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800778 // Deprecated support for this callback.
779 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700780}
781
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700782std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700783 // Deprecated support for this callback.
784 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
785}
786
Jimmy Chend460df32019-11-29 17:31:22 +0200787std::pair<WifiStatus, std::vector<V1_4::IWifiChip::ChipMode>>
Roshan Pius3c868522016-10-27 12:43:49 -0700788WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700789 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700790}
791
Roshan Piusba38d9c2017-12-08 07:32:08 -0800792WifiStatus WifiChip::configureChipInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800793 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700794 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700795 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
796 }
797 if (mode_id == current_mode_id_) {
798 LOG(DEBUG) << "Already in the specified mode " << mode_id;
799 return createWifiStatus(WifiStatusCode::SUCCESS);
800 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800801 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700802 if (status.code != WifiStatusCode::SUCCESS) {
803 for (const auto& callback : event_cb_handler_.getCallbacks()) {
804 if (!callback->onChipReconfigureFailure(status).isOk()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800805 LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
Roshan Piusabcf78f2017-10-06 16:30:38 -0700806 }
807 }
808 return status;
809 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800810 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700811 if (!callback->onChipReconfigured(mode_id).isOk()) {
812 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
813 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800814 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700815 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800816 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700817 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700818
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800819 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(subsystemCallbackHandler_);
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700820
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800821 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700822}
823
824std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700825 if (!isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800826 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), current_mode_id_};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700827 }
828 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700829}
830
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800831std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200832 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700833 legacy_hal::wifi_error legacy_status;
834 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700835 const auto ifname = getFirstActiveWlanIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800836 std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700837 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800838 LOG(ERROR) << "Failed to get driver version: " << legacyErrorToString(legacy_status);
839 WifiStatus status =
840 createWifiStatusFromLegacyError(legacy_status, "failed to get driver version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700841 return {status, result};
842 }
843 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700844
Roshan Piusabcf78f2017-10-06 16:30:38 -0700845 std::string firmware_desc;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800846 std::tie(legacy_status, firmware_desc) = legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700847 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800848 LOG(ERROR) << "Failed to get firmware version: " << legacyErrorToString(legacy_status);
849 WifiStatus status =
850 createWifiStatusFromLegacyError(legacy_status, "failed to get firmware version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700851 return {status, result};
852 }
853 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700854
Roshan Piusabcf78f2017-10-06 16:30:38 -0700855 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700856}
857
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800858std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700859 legacy_hal::wifi_error legacy_status;
860 std::vector<uint8_t> driver_dump;
861 std::tie(legacy_status, driver_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800862 legacy_hal_.lock()->requestDriverMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700863 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800864 LOG(ERROR) << "Failed to get driver debug dump: " << legacyErrorToString(legacy_status);
865 return {createWifiStatusFromLegacyError(legacy_status), std::vector<uint8_t>()};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700866 }
867 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700868}
869
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800870std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700871 legacy_hal::wifi_error legacy_status;
872 std::vector<uint8_t> firmware_dump;
873 std::tie(legacy_status, firmware_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800874 legacy_hal_.lock()->requestFirmwareMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700875 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800876 LOG(ERROR) << "Failed to get firmware debug dump: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700877 return {createWifiStatusFromLegacyError(legacy_status), {}};
878 }
879 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700880}
881
lesl94d28242020-11-18 22:17:37 +0800882WifiStatus WifiChip::createVirtualApInterface(const std::string& apVirtIf) {
883 legacy_hal::wifi_error legacy_status;
884 legacy_status = legacy_hal_.lock()->createVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800885 apVirtIf, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
Sunil Raviddab4bb2020-02-03 22:45:19 -0800886 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
lesl94d28242020-11-18 22:17:37 +0800887 LOG(ERROR) << "Failed to add interface: " << apVirtIf << " "
Sunil Raviddab4bb2020-02-03 22:45:19 -0800888 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +0800889 return createWifiStatusFromLegacyError(legacy_status);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800890 }
lesl94d28242020-11-18 22:17:37 +0800891 return createWifiStatus(WifiStatusCode::SUCCESS);
892}
893
894sp<WifiApIface> WifiChip::newWifiApIface(std::string& ifname) {
lesl420c4fc2020-11-23 19:33:04 +0800895 std::vector<std::string> ap_instances;
896 for (auto const& it : br_ifaces_ap_instances_) {
897 if (it.first == ifname) {
898 ap_instances = it.second;
899 }
900 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800901 sp<WifiApIface> iface = new WifiApIface(ifname, ap_instances, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700902 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700903 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 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700908 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
lesl94d28242020-11-18 22:17:37 +0800909 return iface;
910}
911
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800912std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createApIfaceInternal() {
lesl94d28242020-11-18 22:17:37 +0800913 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
914 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
915 }
916 std::string ifname = allocateApIfaceName();
917 WifiStatus status = createVirtualApInterface(ifname);
918 if (status.code != WifiStatusCode::SUCCESS) {
919 return {status, {}};
920 }
921 sp<WifiApIface> iface = newWifiApIface(ifname);
922 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
923}
924
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800925std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createBridgedApIfaceInternal() {
lesl94d28242020-11-18 22:17:37 +0800926 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
927 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
928 }
lesl261818b2020-11-27 12:37:35 +0800929 std::vector<std::string> ap_instances = allocateBridgedApInstanceNames();
930 if (ap_instances.size() < 2) {
931 LOG(ERROR) << "Fail to allocate two instances";
932 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
933 }
934 std::string br_ifname = kApBridgeIfacePrefix + ap_instances[0];
lesl94d28242020-11-18 22:17:37 +0800935 for (int i = 0; i < 2; i++) {
lesl261818b2020-11-27 12:37:35 +0800936 WifiStatus status = createVirtualApInterface(ap_instances[i]);
lesl94d28242020-11-18 22:17:37 +0800937 if (status.code != WifiStatusCode::SUCCESS) {
lesl261818b2020-11-27 12:37:35 +0800938 if (i != 0) { // The failure happened when creating second virtual
939 // iface.
lesl94d28242020-11-18 22:17:37 +0800940 legacy_hal_.lock()->deleteVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800941 ap_instances.front()); // Remove the first virtual iface.
lesl94d28242020-11-18 22:17:37 +0800942 }
943 return {status, {}};
944 }
lesl94d28242020-11-18 22:17:37 +0800945 }
946 br_ifaces_ap_instances_[br_ifname] = ap_instances;
Roshan Pius8c1a67b2021-03-02 10:00:23 -0800947 if (!iface_util_->createBridge(br_ifname)) {
lesl94d28242020-11-18 22:17:37 +0800948 LOG(ERROR) << "Failed createBridge - br_name=" << br_ifname.c_str();
949 invalidateAndClearBridgedAp(br_ifname);
950 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
951 }
952 for (auto const& instance : ap_instances) {
953 // Bind ap instance interface to AP bridge
Roshan Pius8c1a67b2021-03-02 10:00:23 -0800954 if (!iface_util_->addIfaceToBridge(br_ifname, instance)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800955 LOG(ERROR) << "Failed add if to Bridge - if_name=" << instance.c_str();
lesl94d28242020-11-18 22:17:37 +0800956 invalidateAndClearBridgedAp(br_ifname);
957 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
958 }
959 }
960 sp<WifiApIface> iface = newWifiApIface(br_ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700961 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700962}
963
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800964std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700965 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700966 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
967 }
Roshan Pius675609b2017-10-31 14:24:58 -0700968 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700969}
970
lesl420c4fc2020-11-23 19:33:04 +0800971std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::getApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800972 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700973 const auto iface = findUsingName(ap_ifaces_, ifname);
974 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700975 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
976 }
Roshan Pius675609b2017-10-31 14:24:58 -0700977 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700978}
979
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800980WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700981 const auto iface = findUsingName(ap_ifaces_, ifname);
982 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700983 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800984 }
Roshan Pius82368502019-05-16 12:53:02 -0700985 // Invalidate & remove any dependent objects first.
986 // Note: This is probably not required because we never create
987 // nan/rtt objects over AP iface. But, there is no harm to do it
988 // here and not make that assumption all over the place.
989 invalidateAndRemoveDependencies(ifname);
lesl94d28242020-11-18 22:17:37 +0800990 // Clear the bridge interface and the iface instance.
991 invalidateAndClearBridgedAp(ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700992 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700993 for (const auto& callback : event_cb_handler_.getCallbacks()) {
994 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
995 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
996 }
997 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700998 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700999 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001000}
1001
lesl94d28242020-11-18 22:17:37 +08001002WifiStatus WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001003 const std::string& ifname, const std::string& ifInstanceName) {
lesl94d28242020-11-18 22:17:37 +08001004 const auto iface = findUsingName(ap_ifaces_, ifname);
lesl819e3722021-01-07 09:49:04 +08001005 if (!iface.get() || ifInstanceName.empty()) {
lesl94d28242020-11-18 22:17:37 +08001006 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1007 }
1008 // Requires to remove one of the instance in bridge mode
1009 for (auto const& it : br_ifaces_ap_instances_) {
1010 if (it.first == ifname) {
Les Lee03d642f2021-06-21 21:25:20 +08001011 std::vector<std::string> ap_instances = it.second;
1012 for (auto const& iface : ap_instances) {
lesl94d28242020-11-18 22:17:37 +08001013 if (iface == ifInstanceName) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001014 if (!iface_util_->removeIfaceFromBridge(it.first, iface)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001015 LOG(ERROR) << "Failed to remove interface: " << ifInstanceName << " from "
1016 << ifname;
1017 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE);
lesl94d28242020-11-18 22:17:37 +08001018 }
George Burgess IV2c0a47d2021-01-20 21:14:13 -08001019 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001020 legacy_hal_.lock()->deleteVirtualInterface(iface);
lesl94d28242020-11-18 22:17:37 +08001021 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001022 LOG(ERROR) << "Failed to del interface: " << iface << " "
1023 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +08001024 return createWifiStatusFromLegacyError(legacy_status);
1025 }
Les Lee03d642f2021-06-21 21:25:20 +08001026 ap_instances.erase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001027 std::remove(ap_instances.begin(), ap_instances.end(), ifInstanceName),
1028 ap_instances.end());
Les Lee03d642f2021-06-21 21:25:20 +08001029 br_ifaces_ap_instances_[ifname] = ap_instances;
1030 break;
lesl94d28242020-11-18 22:17:37 +08001031 }
1032 }
1033 break;
1034 }
1035 }
lesl669c9062021-01-22 19:37:47 +08001036 iface->removeInstance(ifInstanceName);
Les Lee03d642f2021-06-21 21:25:20 +08001037 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1038
lesl94d28242020-11-18 22:17:37 +08001039 return createWifiStatus(WifiStatusCode::SUCCESS);
1040}
1041
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001042std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001043 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001044 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -08001045 }
Roshan Pius5ba0a902020-04-14 11:55:42 -07001046 bool is_dedicated_iface = true;
lesl261818b2020-11-27 12:37:35 +08001047 std::string ifname = getPredefinedNanIfaceName();
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001048 if (ifname.empty() || !iface_util_->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -07001049 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
1050 // not defined.
1051 ifname = getFirstActiveWlanIfaceName();
1052 is_dedicated_iface = false;
1053 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001054 sp<WifiNanIface> iface = new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -07001055 nan_ifaces_.push_back(iface);
1056 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1057 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
1058 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1059 }
1060 }
1061 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001062}
1063
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001064std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001065 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001066 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1067 }
Roshan Pius675609b2017-10-31 14:24:58 -07001068 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001069}
1070
Jimmy Chend460df32019-11-29 17:31:22 +02001071std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001072 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001073 const auto iface = findUsingName(nan_ifaces_, ifname);
1074 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001075 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1076 }
Roshan Pius675609b2017-10-31 14:24:58 -07001077 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001078}
1079
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001080WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001081 const auto iface = findUsingName(nan_ifaces_, ifname);
1082 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001083 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001084 }
Roshan Pius675609b2017-10-31 14:24:58 -07001085 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001086 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1087 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
1088 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1089 }
1090 }
1091 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001092}
1093
Roshan Pius3c868522016-10-27 12:43:49 -07001094std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001095 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001096 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001097 }
lesl261818b2020-11-27 12:37:35 +08001098 std::string ifname = getPredefinedP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -07001099 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
1100 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001101 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1102 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
1103 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1104 }
1105 }
Roshan Pius675609b2017-10-31 14:24:58 -07001106 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001107}
1108
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001109std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001110 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001111 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1112 }
Roshan Pius675609b2017-10-31 14:24:58 -07001113 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001114}
1115
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001116std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001117 const auto iface = findUsingName(p2p_ifaces_, ifname);
1118 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001119 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1120 }
Roshan Pius675609b2017-10-31 14:24:58 -07001121 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001122}
1123
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001124WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001125 const auto iface = findUsingName(p2p_ifaces_, ifname);
1126 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001127 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001128 }
Roshan Pius675609b2017-10-31 14:24:58 -07001129 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001130 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1131 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1132 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1133 }
1134 }
1135 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001136}
1137
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001138std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001139 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001140 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001141 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001142 std::string ifname = allocateStaIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001143 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->createVirtualInterface(
1144 ifname, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
Sunil Raviddab4bb2020-02-03 22:45:19 -08001145 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1146 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1147 << legacyErrorToString(legacy_status);
1148 return {createWifiStatusFromLegacyError(legacy_status), {}};
1149 }
Roshan Pius99dab382019-02-14 07:57:10 -08001150 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001151 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001152 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 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001157 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001158 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001159}
1160
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001161std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001162 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001163 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1164 }
Roshan Pius675609b2017-10-31 14:24:58 -07001165 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001166}
1167
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001168std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001169 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001170 const auto iface = findUsingName(sta_ifaces_, ifname);
1171 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001172 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1173 }
Roshan Pius675609b2017-10-31 14:24:58 -07001174 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001175}
1176
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001177WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001178 const auto iface = findUsingName(sta_ifaces_, ifname);
1179 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001180 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001181 }
Roshan Pius82368502019-05-16 12:53:02 -07001182 // Invalidate & remove any dependent objects first.
1183 invalidateAndRemoveDependencies(ifname);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001184 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->deleteVirtualInterface(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001185 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1186 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1187 << legacyErrorToString(legacy_status);
1188 }
Roshan Pius675609b2017-10-31 14:24:58 -07001189 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001190 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 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001195 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001196 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001197}
1198
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001199std::pair<WifiStatus, sp<V1_0::IWifiRttController>> WifiChip::createRttControllerInternal(
1200 const sp<IWifiIface>& /*bound_iface*/) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001201 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001202 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001203}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001204
1205std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1206WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001207 legacy_hal::wifi_error legacy_status;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001208 std::vector<legacy_hal::wifi_ring_buffer_status> legacy_ring_buffer_status_vec;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001209 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001210 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001211 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1212 return {createWifiStatusFromLegacyError(legacy_status), {}};
1213 }
1214 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1215 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001216 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001217 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1218 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001219 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001220}
1221
1222WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001223 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1224 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001225 WifiStatus status = registerDebugRingBufferCallback();
1226 if (status.code != WifiStatusCode::SUCCESS) {
1227 return status;
1228 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001229 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001230 getFirstActiveWlanIfaceName(), ring_name,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001231 static_cast<std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(verbose_level),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001232 max_interval_in_sec, min_data_size_in_bytes);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001233 ringbuffer_map_.insert(
1234 std::pair<std::string, Ringbuffer>(ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001235 // if verbose logging enabled, turn up HAL daemon logging as well.
1236 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1237 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1238 } else {
1239 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1240 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001241 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001242}
1243
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001244WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001245 WifiStatus status = registerDebugRingBufferCallback();
1246 if (status.code != WifiStatusCode::SUCCESS) {
1247 return status;
1248 }
1249 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001250 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001251
Roshan Piusabcf78f2017-10-06 16:30:38 -07001252 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001253}
1254
Roger Wangb294c762018-11-02 15:34:39 +08001255WifiStatus WifiChip::flushRingBufferToFileInternal() {
1256 if (!writeRingbufferFilesInternal()) {
1257 LOG(ERROR) << "Error writing files to flash";
1258 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1259 }
1260 return createWifiStatus(WifiStatusCode::SUCCESS);
1261}
1262
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001263WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001264 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001265 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001266 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1267 debug_ring_buffer_cb_registered_ = false;
1268 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001269 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001270}
1271
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001272std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1273WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001274 legacy_hal::wifi_error legacy_status;
1275 legacy_hal::WakeReasonStats legacy_stats;
1276 std::tie(legacy_status, legacy_stats) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001277 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001278 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1279 return {createWifiStatusFromLegacyError(legacy_status), {}};
1280 }
1281 WifiDebugHostWakeReasonStats hidl_stats;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001282 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats, &hidl_stats)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001283 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1284 }
1285 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001286}
1287
Roshan Pius203cb032016-12-14 17:41:20 -08001288WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001289 legacy_hal::wifi_error legacy_status;
1290 if (enable) {
1291 android::wp<WifiChip> weak_ptr_this(this);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001292 const auto& on_alert_callback = [weak_ptr_this](int32_t error_code,
1293 std::vector<uint8_t> debug_data) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001294 const auto shared_ptr_this = weak_ptr_this.promote();
1295 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1296 LOG(ERROR) << "Callback invoked on an invalid object";
1297 return;
1298 }
1299 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001300 if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001301 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1302 }
1303 }
1304 };
1305 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001306 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001307 } else {
1308 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001309 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001310 }
1311 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001312}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001313
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001314WifiStatus WifiChip::selectTxPowerScenarioInternal(V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001315 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001316 getFirstActiveWlanIfaceName(),
1317 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
Roshan Piusabcf78f2017-10-06 16:30:38 -07001318 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001319}
1320
Roshan Pius735ff432017-07-25 08:48:08 -07001321WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001322 auto legacy_status = legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001323 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001324}
1325
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001326WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1327 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001328 getFirstActiveWlanIfaceName(), hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001329 return createWifiStatusFromLegacyError(legacy_status);
1330}
1331
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001332WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001333 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001334 // Deprecated support for this callback.
1335 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001336}
1337
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001338WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001339 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001340 getFirstActiveWlanIfaceName(),
1341 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001342 return createWifiStatusFromLegacyError(legacy_status);
1343}
1344
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001345std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001346 // Deprecated support for this callback.
1347 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
1348}
1349
1350std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_5() {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001351 legacy_hal::wifi_error legacy_status;
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001352 uint64_t legacy_feature_set;
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001353 uint32_t legacy_logger_feature_set;
1354 const auto ifname = getFirstActiveWlanIfaceName();
1355 std::tie(legacy_status, legacy_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001356 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001357 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1358 return {createWifiStatusFromLegacyError(legacy_status), 0};
1359 }
1360 std::tie(legacy_status, legacy_logger_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001361 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001362 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1363 // some devices don't support querying logger feature set
1364 legacy_logger_feature_set = 0;
1365 }
1366 uint32_t hidl_caps;
1367 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001368 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001369 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1370 }
1371 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1372}
1373
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001374std::pair<WifiStatus, sp<V1_4::IWifiRttController>> WifiChip::createRttControllerInternal_1_4(
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001375 const sp<IWifiIface>& /*bound_iface*/) {
1376 LOG(ERROR) << "createRttController_1_4 is not supported on this HAL";
1377 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001378}
1379
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001380WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001381 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001382 if (!event_cb_handler_.addCallback(event_callback)) {
1383 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1384 }
1385 return createWifiStatus(WifiStatusCode::SUCCESS);
1386}
1387
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001388WifiStatus WifiChip::setMultiStaPrimaryConnectionInternal(const std::string& ifname) {
1389 auto legacy_status = legacy_hal_.lock()->multiStaSetPrimaryConnection(ifname);
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001390 return createWifiStatusFromLegacyError(legacy_status);
1391}
1392
1393WifiStatus WifiChip::setMultiStaUseCaseInternal(MultiStaUseCase use_case) {
1394 auto legacy_status = legacy_hal_.lock()->multiStaSetUseCase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001395 hidl_struct_util::convertHidlMultiStaUseCaseToLegacy(use_case));
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001396 return createWifiStatusFromLegacyError(legacy_status);
1397}
1398
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001399WifiStatus WifiChip::setCoexUnsafeChannelsInternal(std::vector<CoexUnsafeChannel> unsafe_channels,
1400 uint32_t restrictions) {
Quang Luong94bcce52020-11-25 17:52:19 -08001401 std::vector<legacy_hal::wifi_coex_unsafe_channel> legacy_unsafe_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001402 if (!hidl_struct_util::convertHidlVectorOfCoexUnsafeChannelToLegacy(unsafe_channels,
1403 &legacy_unsafe_channels)) {
Quang Luong94bcce52020-11-25 17:52:19 -08001404 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1405 }
Quang Luongab70a832020-12-14 13:01:32 -08001406 uint32_t legacy_restrictions = 0;
1407 if (restrictions & CoexRestriction::WIFI_DIRECT) {
1408 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_DIRECT;
1409 }
1410 if (restrictions & CoexRestriction::SOFTAP) {
1411 legacy_restrictions |= legacy_hal::wifi_coex_restriction::SOFTAP;
1412 }
1413 if (restrictions & CoexRestriction::WIFI_AWARE) {
1414 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_AWARE;
1415 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001416 auto legacy_status =
1417 legacy_hal_.lock()->setCoexUnsafeChannels(legacy_unsafe_channels, legacy_restrictions);
Quang Luong94bcce52020-11-25 17:52:19 -08001418 return createWifiStatusFromLegacyError(legacy_status);
1419}
1420
Kumar Anandda62c382020-11-18 17:17:47 -08001421WifiStatus WifiChip::setCountryCodeInternal(const std::array<int8_t, 2>& code) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001422 auto legacy_status = legacy_hal_.lock()->setCountryCode(getFirstActiveWlanIfaceName(), code);
Kumar Anandda62c382020-11-18 17:17:47 -08001423 return createWifiStatusFromLegacyError(legacy_status);
1424}
1425
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001426std::pair<WifiStatus, std::vector<V1_5::WifiUsableChannel>> WifiChip::getUsableChannelsInternal(
1427 WifiBand /*band*/, uint32_t /*ifaceModeMask*/, uint32_t /*filterMask*/) {
1428 LOG(ERROR) << "getUsableChannels is not supported on this HAL";
1429 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
1430}
1431
1432WifiStatus WifiChip::triggerSubsystemRestartInternal() {
1433 auto legacy_status = legacy_hal_.lock()->triggerSubsystemRestart();
1434 return createWifiStatusFromLegacyError(legacy_status);
1435}
1436
1437std::pair<WifiStatus, sp<V1_6::IWifiRttController>> WifiChip::createRttControllerInternal_1_6(
1438 const sp<IWifiIface>& bound_iface) {
1439 if (sta_ifaces_.size() == 0 && !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1440 LOG(ERROR) << "createRttControllerInternal_1_6: Chip cannot support STAs "
1441 "(and RTT by extension)";
1442 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1443 }
1444 sp<WifiRttController> rtt =
1445 new WifiRttController(getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1446 rtt_controllers_.emplace_back(rtt);
1447 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1448}
1449
1450std::pair<WifiStatus, std::vector<V1_6::WifiUsableChannel>> WifiChip::getUsableChannelsInternal_1_6(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001451 WifiBand band, uint32_t ifaceModeMask, uint32_t filterMask) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001452 legacy_hal::wifi_error legacy_status;
1453 std::vector<legacy_hal::wifi_usable_channel> legacy_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001454 std::tie(legacy_status, legacy_usable_channels) = legacy_hal_.lock()->getUsableChannels(
Kumar Anand2a630a32021-01-21 14:09:14 -08001455 hidl_struct_util::convertHidlWifiBandToLegacyMacBand(band),
Kumar Anandaea86e02021-02-10 16:22:31 -08001456 hidl_struct_util::convertHidlWifiIfaceModeToLegacy(ifaceModeMask),
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001457 hidl_struct_util::convertHidlUsableChannelFilterToLegacy(filterMask));
Kumar Anandaea86e02021-02-10 16:22:31 -08001458
Kumar Anand2a630a32021-01-21 14:09:14 -08001459 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1460 return {createWifiStatusFromLegacyError(legacy_status), {}};
1461 }
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001462 std::vector<V1_6::WifiUsableChannel> hidl_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001463 if (!hidl_struct_util::convertLegacyWifiUsableChannelsToHidl(legacy_usable_channels,
1464 &hidl_usable_channels)) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001465 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1466 }
1467 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_usable_channels};
1468}
1469
Sunil Ravief97d232022-01-24 10:39:56 -08001470std::pair<WifiStatus, V1_6::WifiRadioCombinationMatrix>
1471WifiChip::getSupportedRadioCombinationsMatrixInternal() {
1472 legacy_hal::wifi_error legacy_status;
1473 legacy_hal::wifi_radio_combination_matrix* legacy_matrix;
1474
1475 std::tie(legacy_status, legacy_matrix) =
1476 legacy_hal_.lock()->getSupportedRadioCombinationsMatrix();
1477 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1478 LOG(ERROR) << "Failed to get SupportedRadioCombinations matrix from legacy HAL: "
1479 << legacyErrorToString(legacy_status);
1480 return {createWifiStatusFromLegacyError(legacy_status), {}};
1481 }
1482
1483 V1_6::WifiRadioCombinationMatrix hidl_matrix;
1484 if (!hidl_struct_util::convertLegacyRadioCombinationsMatrixToHidl(legacy_matrix,
1485 &hidl_matrix)) {
1486 LOG(ERROR) << "Failed convertLegacyRadioCombinationsMatrixToHidl() ";
1487 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), {}};
1488 }
1489 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_matrix};
1490}
1491
Roshan Piusba38d9c2017-12-08 07:32:08 -08001492WifiStatus WifiChip::handleChipConfiguration(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001493 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001494 // If the chip is already configured in a different mode, stop
1495 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001496 if (isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001497 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_ << " to mode " << mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -08001498 invalidateAndRemoveAllIfaces();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001499 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stop(lock, []() {});
Roshan Piusba38d9c2017-12-08 07:32:08 -08001500 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001501 LOG(ERROR) << "Failed to stop legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusba38d9c2017-12-08 07:32:08 -08001502 return createWifiStatusFromLegacyError(legacy_status);
1503 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001504 }
Roshan Piuscc338202017-11-02 13:54:09 -07001505 // Firmware mode change not needed for V2 devices.
1506 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001507 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001508 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001509 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001510 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1511 }
1512 if (!success) {
1513 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1514 }
1515 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1516 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001517 LOG(ERROR) << "Failed to start legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001518 return createWifiStatusFromLegacyError(legacy_status);
1519 }
Roshan Pius85c64412018-01-22 17:58:40 -08001520 // Every time the HAL is restarted, we need to register the
1521 // radio mode change callback.
1522 WifiStatus status = registerRadioModeChangeCallback();
1523 if (status.code != WifiStatusCode::SUCCESS) {
1524 // This probably is not a critical failure?
1525 LOG(ERROR) << "Failed to register radio mode change callback";
1526 }
chenpaulf5eca292019-03-14 11:08:03 +08001527 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001528 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001529 version_info = WifiChip::requestChipDebugInfoInternal();
1530 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1531 property_set("vendor.wlan.firmware.version",
1532 version_info.second.firmwareDescription.c_str());
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001533 property_set("vendor.wlan.driver.version", version_info.second.driverDescription.c_str());
chenpaulf5eca292019-03-14 11:08:03 +08001534 }
1535
Roshan Piusabcf78f2017-10-06 16:30:38 -07001536 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001537}
Roshan Pius48185b22016-12-15 19:10:30 -08001538
1539WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001540 if (debug_ring_buffer_cb_registered_) {
1541 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001542 }
Roshan Pius3797e182017-03-30 18:01:54 -07001543
Roshan Piusabcf78f2017-10-06 16:30:38 -07001544 android::wp<WifiChip> weak_ptr_this(this);
1545 const auto& on_ring_buffer_data_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001546 [weak_ptr_this](const std::string& name, const std::vector<uint8_t>& data,
1547 const legacy_hal::wifi_ring_buffer_status& status) {
1548 const auto shared_ptr_this = weak_ptr_this.promote();
1549 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1550 LOG(ERROR) << "Callback invoked on an invalid object";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301551 return;
1552 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001553 WifiDebugRingBufferStatus hidl_status;
1554 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(status,
1555 &hidl_status)) {
1556 LOG(ERROR) << "Error converting ring buffer status";
1557 return;
1558 }
1559 {
1560 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1561 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1562 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1563 Ringbuffer& cur_buffer = target->second;
1564 cur_buffer.append(data);
1565 } else {
1566 LOG(ERROR) << "Ringname " << name << " not found";
1567 return;
1568 }
1569 // unique_lock unlocked here
1570 }
1571 };
1572 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001573 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001574
Roshan Piusabcf78f2017-10-06 16:30:38 -07001575 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1576 debug_ring_buffer_cb_registered_ = true;
1577 }
1578 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001579}
1580
Roshan Pius85c64412018-01-22 17:58:40 -08001581WifiStatus WifiChip::registerRadioModeChangeCallback() {
1582 android::wp<WifiChip> weak_ptr_this(this);
1583 const auto& on_radio_mode_change_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001584 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1585 const auto shared_ptr_this = weak_ptr_this.promote();
1586 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1587 LOG(ERROR) << "Callback invoked on an invalid object";
1588 return;
Roshan Pius85c64412018-01-22 17:58:40 -08001589 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001590 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo> hidl_radio_mode_infos;
1591 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(mac_infos,
1592 &hidl_radio_mode_infos)) {
1593 LOG(ERROR) << "Error converting wifi mac info";
1594 return;
1595 }
1596 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1597 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos).isOk()) {
1598 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
1599 << " callback on: " << toString(callback);
1600 }
1601 }
1602 };
Roshan Pius85c64412018-01-22 17:58:40 -08001603 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001604 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1605 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001606 return createWifiStatusFromLegacyError(legacy_status);
1607}
1608
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001609std::vector<V1_4::IWifiChip::ChipIfaceCombination> WifiChip::getCurrentModeIfaceCombinations() {
Roshan Piuscc338202017-11-02 13:54:09 -07001610 if (!isValidModeId(current_mode_id_)) {
1611 LOG(ERROR) << "Chip not configured in a mode yet";
1612 return {};
1613 }
1614 for (const auto& mode : modes_) {
1615 if (mode.id == current_mode_id_) {
1616 return mode.availableCombinations;
1617 }
1618 }
1619 CHECK(0) << "Expected to find iface combinations for current mode!";
1620 return {};
1621}
1622
1623// Returns a map indexed by IfaceType with the number of ifaces currently
1624// created of the corresponding type.
1625std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1626 std::map<IfaceType, size_t> iface_counts;
1627 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1628 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1629 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1630 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1631 return iface_counts;
1632}
1633
1634// This expands the provided iface combinations to a more parseable
1635// form. Returns a vector of available combinations possible with the number
1636// of ifaces of each type in the combination.
1637// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1638std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001639 const V1_4::IWifiChip::ChipIfaceCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001640 uint32_t num_expanded_combos = 1;
1641 for (const auto& limit : combination.limits) {
1642 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1643 num_expanded_combos *= limit.types.size();
1644 }
1645 }
1646
1647 // Allocate the vector of expanded combos and reset all iface counts to 0
1648 // in each combo.
1649 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1650 expanded_combos.resize(num_expanded_combos);
1651 for (auto& expanded_combo : expanded_combos) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001652 for (const auto type : {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001653 expanded_combo[type] = 0;
1654 }
1655 }
1656 uint32_t span = num_expanded_combos;
1657 for (const auto& limit : combination.limits) {
1658 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1659 span /= limit.types.size();
1660 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001661 const auto iface_type = limit.types[(k / span) % limit.types.size()];
Roshan Piuscc338202017-11-02 13:54:09 -07001662 expanded_combos[k][iface_type]++;
1663 }
1664 }
1665 }
1666 return expanded_combos;
1667}
1668
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001669bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001670 const std::map<IfaceType, size_t>& expanded_combo, IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001671 const auto current_combo = getCurrentIfaceCombination();
1672
1673 // Check if we have space for 1 more iface of |type| in this combo
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001674 for (const auto type : {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001675 size_t num_ifaces_needed = current_combo.at(type);
1676 if (type == requested_type) {
1677 num_ifaces_needed++;
1678 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001679 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001680 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 iface combos by expanding the current
1689// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001690// b) Check if the requested iface type can be added to the current mode
1691// with the iface combination that is already active.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001692bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001693 if (!isValidModeId(current_mode_id_)) {
1694 LOG(ERROR) << "Chip not configured in a mode yet";
1695 return false;
1696 }
1697 const auto combinations = getCurrentModeIfaceCombinations();
1698 for (const auto& combination : combinations) {
1699 const auto expanded_combos = expandIfaceCombinations(combination);
1700 for (const auto& expanded_combo : expanded_combos) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001701 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(expanded_combo,
1702 requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001703 return true;
1704 }
1705 }
1706 }
1707 return false;
1708}
1709
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001710// Note: This does not consider ifaces already active. It only checks if the
1711// provided expanded iface combination can support the requested combo.
1712bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001713 const std::map<IfaceType, size_t>& expanded_combo,
1714 const std::map<IfaceType, size_t>& req_combo) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001715 // Check if we have space for 1 more iface of |type| in this combo
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001716 for (const auto type : {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001717 if (req_combo.count(type) == 0) {
1718 // Iface of "type" not in the req_combo.
1719 continue;
1720 }
1721 size_t num_ifaces_needed = req_combo.at(type);
1722 size_t num_ifaces_allowed = expanded_combo.at(type);
1723 if (num_ifaces_needed > num_ifaces_allowed) {
1724 return false;
1725 }
1726 }
1727 return true;
1728}
1729// This method does the following:
1730// a) Enumerate all possible iface combos by expanding the current
1731// ChipIfaceCombination.
1732// b) Check if the requested iface combo can be added to the current mode.
1733// Note: This does not consider ifaces already active. It only checks if the
1734// current mode can support the requested combo.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001735bool WifiChip::canCurrentModeSupportIfaceCombo(const std::map<IfaceType, size_t>& req_combo) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001736 if (!isValidModeId(current_mode_id_)) {
1737 LOG(ERROR) << "Chip not configured in a mode yet";
1738 return false;
1739 }
1740 const auto combinations = getCurrentModeIfaceCombinations();
1741 for (const auto& combination : combinations) {
1742 const auto expanded_combos = expandIfaceCombinations(combination);
1743 for (const auto& expanded_combo : expanded_combos) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001744 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo, req_combo)) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001745 return true;
1746 }
1747 }
1748 }
1749 return false;
1750}
1751
1752// This method does the following:
1753// a) Enumerate all possible iface combos by expanding the current
1754// ChipIfaceCombination.
1755// b) Check if the requested iface type can be added to the current mode.
1756bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
lesl261818b2020-11-27 12:37:35 +08001757 // Check if we can support at least 1 iface of type.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001758 std::map<IfaceType, size_t> req_iface_combo;
1759 req_iface_combo[requested_type] = 1;
1760 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1761}
1762
Roshan Piuscc338202017-11-02 13:54:09 -07001763bool WifiChip::isValidModeId(ChipModeId mode_id) {
1764 for (const auto& mode : modes_) {
1765 if (mode.id == mode_id) {
1766 return true;
1767 }
1768 }
1769 return false;
1770}
1771
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001772bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
lesl261818b2020-11-27 12:37:35 +08001773 // Check if we can support at least 1 STA & 1 AP concurrently.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001774 std::map<IfaceType, size_t> req_iface_combo;
1775 req_iface_combo[IfaceType::AP] = 1;
1776 req_iface_combo[IfaceType::STA] = 1;
1777 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1778}
1779
lesl261818b2020-11-27 12:37:35 +08001780bool WifiChip::isDualStaConcurrencyAllowedInCurrentMode() {
1781 // Check if we can support at least 2 STA concurrently.
James Mattisd2e4c072019-05-22 16:14:48 -07001782 std::map<IfaceType, size_t> req_iface_combo;
lesl261818b2020-11-27 12:37:35 +08001783 req_iface_combo[IfaceType::STA] = 2;
James Mattisd2e4c072019-05-22 16:14:48 -07001784 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1785}
1786
Roshan Pius6036c022019-03-27 10:41:58 -07001787std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001788 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
leslf012b652021-01-08 15:22:49 +08001789 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 }
Roshan Pius6036c022019-03-27 10:41:58 -07001799 // This could happen if the chip call is made before any STA/AP
1800 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001801 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Jimmy Chen2dddd792019-12-23 17:50:39 +02001802 return getWlanIfaceNameWithType(IfaceType::STA, 0);
Roshan Pius6036c022019-03-27 10:41:58 -07001803}
1804
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001805// 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.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001808std::string WifiChip::allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001809 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001810 const auto ifname = getWlanIfaceNameWithType(type, idx);
lesl94d28242020-11-18 22:17:37 +08001811 if (findUsingNameFromBridgedApInstances(ifname)) continue;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001812 if (findUsingName(ap_ifaces_, ifname)) continue;
1813 if (findUsingName(sta_ifaces_, ifname)) continue;
1814 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001815 }
1816 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001817 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001818 return {};
1819}
1820
lesl261818b2020-11-27 12:37:35 +08001821uint32_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
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001834// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001835// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001836std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001837 // Check if we have a dedicated iface for AP.
lesl261818b2020-11-27 12:37:35 +08001838 std::vector<std::string> ifnames = getPredefinedApIfaceNames(false);
1839 if (!ifnames.empty()) {
1840 return ifnames[0];
Roshan Pius78cb5992020-04-30 12:39:21 -07001841 }
lesl261818b2020-11-27 12:37:35 +08001842 return allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface());
1843}
1844
1845std::vector<std::string> WifiChip::allocateBridgedApInstanceNames() {
1846 // Check if we have a dedicated iface for AP.
1847 std::vector<std::string> instances = getPredefinedApIfaceNames(true);
1848 if (instances.size() == 2) {
1849 return instances;
1850 } else {
1851 int num_ifaces_need_to_allocate = 2 - instances.size();
1852 for (int i = 0; i < num_ifaces_need_to_allocate; i++) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001853 std::string instance_name =
1854 allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface() + i);
lesl261818b2020-11-27 12:37:35 +08001855 if (!instance_name.empty()) {
1856 instances.push_back(instance_name);
1857 }
1858 }
1859 }
1860 return instances;
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001861}
1862
1863// STA iface names start with idx 0.
1864// Primary STA iface will always be 0.
1865std::string WifiChip::allocateStaIfaceName() {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001866 return allocateApOrStaIfaceName(IfaceType::STA, 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001867}
1868
xshu5899e8e2018-01-09 15:36:03 -08001869bool WifiChip::writeRingbufferFilesInternal() {
1870 if (!removeOldFilesInternal()) {
1871 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1872 return false;
1873 }
1874 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301875 {
1876 std::unique_lock<std::mutex> lk(lock_t);
xshuc905ea62021-07-11 19:57:02 -07001877 for (auto& item : ringbuffer_map_) {
1878 Ringbuffer& cur_buffer = item.second;
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301879 if (cur_buffer.getData().empty()) {
1880 continue;
1881 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001882 const std::string file_path_raw = kTombstoneFolderPath + item.first + "XXXXXXXXXX";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301883 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1884 if (dump_fd == -1) {
1885 PLOG(ERROR) << "create file failed";
1886 return false;
1887 }
1888 unique_fd file_auto_closer(dump_fd);
1889 for (const auto& cur_block : cur_buffer.getData()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001890 if (write(dump_fd, cur_block.data(), sizeof(cur_block[0]) * cur_block.size()) ==
1891 -1) {
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301892 PLOG(ERROR) << "Error writing to file";
1893 }
xshu5899e8e2018-01-09 15:36:03 -08001894 }
xshuc905ea62021-07-11 19:57:02 -07001895 cur_buffer.clear();
xshu5899e8e2018-01-09 15:36:03 -08001896 }
xshu0a0fe512020-07-22 17:53:37 -07001897 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001898 }
1899 return true;
1900}
1901
Jimmy Chen2dddd792019-12-23 17:50:39 +02001902std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1903 std::string ifname;
1904
1905 // let the legacy hal override the interface name
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001906 legacy_hal::wifi_error err = legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
Jimmy Chen2dddd792019-12-23 17:50:39 +02001907 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1908
1909 return getWlanIfaceName(idx);
1910}
1911
lesl94d28242020-11-18 22:17:37 +08001912void WifiChip::invalidateAndClearBridgedApAll() {
1913 for (auto const& it : br_ifaces_ap_instances_) {
1914 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001915 iface_util_->removeIfaceFromBridge(it.first, iface);
lesl94d28242020-11-18 22:17:37 +08001916 legacy_hal_.lock()->deleteVirtualInterface(iface);
1917 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001918 iface_util_->deleteBridge(it.first);
lesl94d28242020-11-18 22:17:37 +08001919 }
1920 br_ifaces_ap_instances_.clear();
1921}
1922
1923void WifiChip::invalidateAndClearBridgedAp(const std::string& br_name) {
1924 if (br_name.empty()) return;
1925 // delete managed interfaces
1926 for (auto const& it : br_ifaces_ap_instances_) {
1927 if (it.first == br_name) {
1928 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001929 iface_util_->removeIfaceFromBridge(br_name, iface);
lesl94d28242020-11-18 22:17:37 +08001930 legacy_hal_.lock()->deleteVirtualInterface(iface);
1931 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001932 iface_util_->deleteBridge(br_name);
lesl94d28242020-11-18 22:17:37 +08001933 br_ifaces_ap_instances_.erase(br_name);
1934 break;
1935 }
1936 }
1937 return;
1938}
1939
1940bool WifiChip::findUsingNameFromBridgedApInstances(const std::string& name) {
1941 for (auto const& it : br_ifaces_ap_instances_) {
1942 if (it.first == name) {
1943 return true;
1944 }
1945 for (auto const& iface : it.second) {
1946 if (iface == name) {
1947 return true;
1948 }
1949 }
1950 }
1951 return false;
1952}
1953
Roshan Pius79a99752016-10-04 13:03:58 -07001954} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001955} // namespace V1_6
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001956} // namespace wifi
1957} // namespace hardware
1958} // namespace android