blob: 0e2accf51006074459635fc3fca4eecda733bf14 [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
Quang Luong5d8805e2022-01-28 15:46:40 -0800731Return<void> WifiChip::getAvailableModes_1_6(getAvailableModes_1_6_cb hidl_status_cb) {
732 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
733 &WifiChip::getAvailableModesInternal_1_6, hidl_status_cb);
734}
735
Roshan Pius35d958c2016-10-06 16:47:38 -0700736void WifiChip::invalidateAndRemoveAllIfaces() {
lesl94d28242020-11-18 22:17:37 +0800737 invalidateAndClearBridgedApAll();
Roshan Pius675609b2017-10-31 14:24:58 -0700738 invalidateAndClearAll(ap_ifaces_);
739 invalidateAndClearAll(nan_ifaces_);
740 invalidateAndClearAll(p2p_ifaces_);
741 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700742 // Since all the ifaces are invalid now, all RTT controller objects
743 // using those ifaces also need to be invalidated.
744 for (const auto& rtt : rtt_controllers_) {
745 rtt->invalidate();
746 }
747 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700748}
749
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800750void WifiChip::invalidateAndRemoveDependencies(const std::string& removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200751 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
752 auto nan_iface = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700753 if (nan_iface->getName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200754 nan_iface->invalidate();
Roshan Pius82368502019-05-16 12:53:02 -0700755 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800756 if (!callback->onIfaceRemoved(IfaceType::NAN, removed_iface_name).isOk()) {
Roshan Pius82368502019-05-16 12:53:02 -0700757 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
758 }
759 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200760 it = nan_ifaces_.erase(it);
761 } else {
762 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700763 }
764 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200765
766 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
767 auto rtt = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700768 if (rtt->getIfaceName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200769 rtt->invalidate();
770 it = rtt_controllers_.erase(it);
771 } else {
772 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700773 }
774 }
775}
776
Roshan Pius3c868522016-10-27 12:43:49 -0700777std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700778 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700779}
780
781WifiStatus WifiChip::registerEventCallbackInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800782 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800783 // Deprecated support for this callback.
784 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700785}
786
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700787std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700788 // Deprecated support for this callback.
789 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
790}
791
Quang Luong5d8805e2022-01-28 15:46:40 -0800792std::pair<WifiStatus, std::vector<V1_0::IWifiChip::ChipMode>>
Roshan Pius3c868522016-10-27 12:43:49 -0700793WifiChip::getAvailableModesInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -0800794 // Deprecated support -- use getAvailableModes_1_6.
795 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -0700796}
797
Roshan Piusba38d9c2017-12-08 07:32:08 -0800798WifiStatus WifiChip::configureChipInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800799 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700800 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700801 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
802 }
803 if (mode_id == current_mode_id_) {
804 LOG(DEBUG) << "Already in the specified mode " << mode_id;
805 return createWifiStatus(WifiStatusCode::SUCCESS);
806 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800807 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700808 if (status.code != WifiStatusCode::SUCCESS) {
809 for (const auto& callback : event_cb_handler_.getCallbacks()) {
810 if (!callback->onChipReconfigureFailure(status).isOk()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800811 LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
Roshan Piusabcf78f2017-10-06 16:30:38 -0700812 }
813 }
814 return status;
815 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800816 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700817 if (!callback->onChipReconfigured(mode_id).isOk()) {
818 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
819 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800820 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700821 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800822 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700823 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700824
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800825 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(subsystemCallbackHandler_);
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700826
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800827 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700828}
829
830std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700831 if (!isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800832 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), current_mode_id_};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700833 }
834 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700835}
836
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800837std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200838 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700839 legacy_hal::wifi_error legacy_status;
840 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700841 const auto ifname = getFirstActiveWlanIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800842 std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700843 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800844 LOG(ERROR) << "Failed to get driver version: " << legacyErrorToString(legacy_status);
845 WifiStatus status =
846 createWifiStatusFromLegacyError(legacy_status, "failed to get driver version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700847 return {status, result};
848 }
849 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700850
Roshan Piusabcf78f2017-10-06 16:30:38 -0700851 std::string firmware_desc;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800852 std::tie(legacy_status, firmware_desc) = legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700853 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800854 LOG(ERROR) << "Failed to get firmware version: " << legacyErrorToString(legacy_status);
855 WifiStatus status =
856 createWifiStatusFromLegacyError(legacy_status, "failed to get firmware version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700857 return {status, result};
858 }
859 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700860
Roshan Piusabcf78f2017-10-06 16:30:38 -0700861 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700862}
863
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800864std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700865 legacy_hal::wifi_error legacy_status;
866 std::vector<uint8_t> driver_dump;
867 std::tie(legacy_status, driver_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800868 legacy_hal_.lock()->requestDriverMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700869 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800870 LOG(ERROR) << "Failed to get driver debug dump: " << legacyErrorToString(legacy_status);
871 return {createWifiStatusFromLegacyError(legacy_status), std::vector<uint8_t>()};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700872 }
873 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700874}
875
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800876std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700877 legacy_hal::wifi_error legacy_status;
878 std::vector<uint8_t> firmware_dump;
879 std::tie(legacy_status, firmware_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800880 legacy_hal_.lock()->requestFirmwareMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700881 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800882 LOG(ERROR) << "Failed to get firmware debug dump: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700883 return {createWifiStatusFromLegacyError(legacy_status), {}};
884 }
885 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700886}
887
lesl94d28242020-11-18 22:17:37 +0800888WifiStatus WifiChip::createVirtualApInterface(const std::string& apVirtIf) {
889 legacy_hal::wifi_error legacy_status;
890 legacy_status = legacy_hal_.lock()->createVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800891 apVirtIf, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
Sunil Raviddab4bb2020-02-03 22:45:19 -0800892 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
lesl94d28242020-11-18 22:17:37 +0800893 LOG(ERROR) << "Failed to add interface: " << apVirtIf << " "
Sunil Raviddab4bb2020-02-03 22:45:19 -0800894 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +0800895 return createWifiStatusFromLegacyError(legacy_status);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800896 }
lesl94d28242020-11-18 22:17:37 +0800897 return createWifiStatus(WifiStatusCode::SUCCESS);
898}
899
900sp<WifiApIface> WifiChip::newWifiApIface(std::string& ifname) {
lesl420c4fc2020-11-23 19:33:04 +0800901 std::vector<std::string> ap_instances;
902 for (auto const& it : br_ifaces_ap_instances_) {
903 if (it.first == ifname) {
904 ap_instances = it.second;
905 }
906 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800907 sp<WifiApIface> iface = new WifiApIface(ifname, ap_instances, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700908 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700909 for (const auto& callback : event_cb_handler_.getCallbacks()) {
910 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
911 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
912 }
913 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700914 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
lesl94d28242020-11-18 22:17:37 +0800915 return iface;
916}
917
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800918std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createApIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -0800919 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP)) {
lesl94d28242020-11-18 22:17:37 +0800920 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
921 }
922 std::string ifname = allocateApIfaceName();
923 WifiStatus status = createVirtualApInterface(ifname);
924 if (status.code != WifiStatusCode::SUCCESS) {
925 return {status, {}};
926 }
927 sp<WifiApIface> iface = newWifiApIface(ifname);
928 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
929}
930
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800931std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createBridgedApIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -0800932 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP_BRIDGED)) {
lesl94d28242020-11-18 22:17:37 +0800933 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
934 }
lesl261818b2020-11-27 12:37:35 +0800935 std::vector<std::string> ap_instances = allocateBridgedApInstanceNames();
936 if (ap_instances.size() < 2) {
937 LOG(ERROR) << "Fail to allocate two instances";
938 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
939 }
940 std::string br_ifname = kApBridgeIfacePrefix + ap_instances[0];
lesl94d28242020-11-18 22:17:37 +0800941 for (int i = 0; i < 2; i++) {
lesl261818b2020-11-27 12:37:35 +0800942 WifiStatus status = createVirtualApInterface(ap_instances[i]);
lesl94d28242020-11-18 22:17:37 +0800943 if (status.code != WifiStatusCode::SUCCESS) {
lesl261818b2020-11-27 12:37:35 +0800944 if (i != 0) { // The failure happened when creating second virtual
945 // iface.
lesl94d28242020-11-18 22:17:37 +0800946 legacy_hal_.lock()->deleteVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800947 ap_instances.front()); // Remove the first virtual iface.
lesl94d28242020-11-18 22:17:37 +0800948 }
949 return {status, {}};
950 }
lesl94d28242020-11-18 22:17:37 +0800951 }
952 br_ifaces_ap_instances_[br_ifname] = ap_instances;
Roshan Pius8c1a67b2021-03-02 10:00:23 -0800953 if (!iface_util_->createBridge(br_ifname)) {
lesl94d28242020-11-18 22:17:37 +0800954 LOG(ERROR) << "Failed createBridge - br_name=" << br_ifname.c_str();
955 invalidateAndClearBridgedAp(br_ifname);
956 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
957 }
958 for (auto const& instance : ap_instances) {
959 // Bind ap instance interface to AP bridge
Roshan Pius8c1a67b2021-03-02 10:00:23 -0800960 if (!iface_util_->addIfaceToBridge(br_ifname, instance)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800961 LOG(ERROR) << "Failed add if to Bridge - if_name=" << instance.c_str();
lesl94d28242020-11-18 22:17:37 +0800962 invalidateAndClearBridgedAp(br_ifname);
963 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
964 }
965 }
966 sp<WifiApIface> iface = newWifiApIface(br_ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700967 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700968}
969
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800970std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700971 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700972 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
973 }
Roshan Pius675609b2017-10-31 14:24:58 -0700974 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700975}
976
lesl420c4fc2020-11-23 19:33:04 +0800977std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::getApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800978 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700979 const auto iface = findUsingName(ap_ifaces_, ifname);
980 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700981 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
982 }
Roshan Pius675609b2017-10-31 14:24:58 -0700983 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700984}
985
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800986WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700987 const auto iface = findUsingName(ap_ifaces_, ifname);
988 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700989 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800990 }
Roshan Pius82368502019-05-16 12:53:02 -0700991 // Invalidate & remove any dependent objects first.
992 // Note: This is probably not required because we never create
993 // nan/rtt objects over AP iface. But, there is no harm to do it
994 // here and not make that assumption all over the place.
995 invalidateAndRemoveDependencies(ifname);
lesl94d28242020-11-18 22:17:37 +0800996 // Clear the bridge interface and the iface instance.
997 invalidateAndClearBridgedAp(ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700998 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700999 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1000 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
1001 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1002 }
1003 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001004 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001005 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001006}
1007
lesl94d28242020-11-18 22:17:37 +08001008WifiStatus WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001009 const std::string& ifname, const std::string& ifInstanceName) {
lesl94d28242020-11-18 22:17:37 +08001010 const auto iface = findUsingName(ap_ifaces_, ifname);
lesl819e3722021-01-07 09:49:04 +08001011 if (!iface.get() || ifInstanceName.empty()) {
lesl94d28242020-11-18 22:17:37 +08001012 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1013 }
1014 // Requires to remove one of the instance in bridge mode
1015 for (auto const& it : br_ifaces_ap_instances_) {
1016 if (it.first == ifname) {
Les Lee03d642f2021-06-21 21:25:20 +08001017 std::vector<std::string> ap_instances = it.second;
1018 for (auto const& iface : ap_instances) {
lesl94d28242020-11-18 22:17:37 +08001019 if (iface == ifInstanceName) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001020 if (!iface_util_->removeIfaceFromBridge(it.first, iface)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001021 LOG(ERROR) << "Failed to remove interface: " << ifInstanceName << " from "
1022 << ifname;
1023 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE);
lesl94d28242020-11-18 22:17:37 +08001024 }
George Burgess IV2c0a47d2021-01-20 21:14:13 -08001025 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001026 legacy_hal_.lock()->deleteVirtualInterface(iface);
lesl94d28242020-11-18 22:17:37 +08001027 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001028 LOG(ERROR) << "Failed to del interface: " << iface << " "
1029 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +08001030 return createWifiStatusFromLegacyError(legacy_status);
1031 }
Les Lee03d642f2021-06-21 21:25:20 +08001032 ap_instances.erase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001033 std::remove(ap_instances.begin(), ap_instances.end(), ifInstanceName),
1034 ap_instances.end());
Les Lee03d642f2021-06-21 21:25:20 +08001035 br_ifaces_ap_instances_[ifname] = ap_instances;
1036 break;
lesl94d28242020-11-18 22:17:37 +08001037 }
1038 }
1039 break;
1040 }
1041 }
lesl669c9062021-01-22 19:37:47 +08001042 iface->removeInstance(ifInstanceName);
Les Lee03d642f2021-06-21 21:25:20 +08001043 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1044
lesl94d28242020-11-18 22:17:37 +08001045 return createWifiStatus(WifiStatusCode::SUCCESS);
1046}
1047
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001048std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -08001049 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001050 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -08001051 }
Roshan Pius5ba0a902020-04-14 11:55:42 -07001052 bool is_dedicated_iface = true;
lesl261818b2020-11-27 12:37:35 +08001053 std::string ifname = getPredefinedNanIfaceName();
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001054 if (ifname.empty() || !iface_util_->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -07001055 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
1056 // not defined.
1057 ifname = getFirstActiveWlanIfaceName();
1058 is_dedicated_iface = false;
1059 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001060 sp<WifiNanIface> iface = new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -07001061 nan_ifaces_.push_back(iface);
1062 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1063 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
1064 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1065 }
1066 }
1067 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001068}
1069
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001070std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001071 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001072 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1073 }
Roshan Pius675609b2017-10-31 14:24:58 -07001074 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001075}
1076
Jimmy Chend460df32019-11-29 17:31:22 +02001077std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001078 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001079 const auto iface = findUsingName(nan_ifaces_, ifname);
1080 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001081 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1082 }
Roshan Pius675609b2017-10-31 14:24:58 -07001083 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001084}
1085
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001086WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001087 const auto iface = findUsingName(nan_ifaces_, ifname);
1088 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001089 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001090 }
Roshan Pius675609b2017-10-31 14:24:58 -07001091 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001092 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1093 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
1094 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1095 }
1096 }
1097 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001098}
1099
Roshan Pius3c868522016-10-27 12:43:49 -07001100std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -08001101 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001102 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001103 }
lesl261818b2020-11-27 12:37:35 +08001104 std::string ifname = getPredefinedP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -07001105 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
1106 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001107 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1108 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
1109 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1110 }
1111 }
Roshan Pius675609b2017-10-31 14:24:58 -07001112 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001113}
1114
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001115std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001116 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001117 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1118 }
Roshan Pius675609b2017-10-31 14:24:58 -07001119 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001120}
1121
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001122std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001123 const auto iface = findUsingName(p2p_ifaces_, ifname);
1124 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001125 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1126 }
Roshan Pius675609b2017-10-31 14:24:58 -07001127 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001128}
1129
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001130WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001131 const auto iface = findUsingName(p2p_ifaces_, ifname);
1132 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001133 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001134 }
Roshan Pius675609b2017-10-31 14:24:58 -07001135 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001136 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1137 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1138 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1139 }
1140 }
1141 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001142}
1143
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001144std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -08001145 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001146 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001147 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001148 std::string ifname = allocateStaIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001149 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->createVirtualInterface(
1150 ifname, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
Sunil Raviddab4bb2020-02-03 22:45:19 -08001151 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1152 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1153 << legacyErrorToString(legacy_status);
1154 return {createWifiStatusFromLegacyError(legacy_status), {}};
1155 }
Roshan Pius99dab382019-02-14 07:57:10 -08001156 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001157 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001158 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1159 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1160 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1161 }
1162 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001163 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001164 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001165}
1166
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001167std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001168 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001169 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1170 }
Roshan Pius675609b2017-10-31 14:24:58 -07001171 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001172}
1173
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001174std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001175 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001176 const auto iface = findUsingName(sta_ifaces_, ifname);
1177 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001178 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1179 }
Roshan Pius675609b2017-10-31 14:24:58 -07001180 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001181}
1182
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001183WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001184 const auto iface = findUsingName(sta_ifaces_, ifname);
1185 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001186 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001187 }
Roshan Pius82368502019-05-16 12:53:02 -07001188 // Invalidate & remove any dependent objects first.
1189 invalidateAndRemoveDependencies(ifname);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001190 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->deleteVirtualInterface(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001191 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1192 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1193 << legacyErrorToString(legacy_status);
1194 }
Roshan Pius675609b2017-10-31 14:24:58 -07001195 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001196 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1197 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1198 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1199 }
1200 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001201 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001202 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001203}
1204
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001205std::pair<WifiStatus, sp<V1_0::IWifiRttController>> WifiChip::createRttControllerInternal(
1206 const sp<IWifiIface>& /*bound_iface*/) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001207 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001208 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001209}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001210
1211std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1212WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001213 legacy_hal::wifi_error legacy_status;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001214 std::vector<legacy_hal::wifi_ring_buffer_status> legacy_ring_buffer_status_vec;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001215 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001216 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001217 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1218 return {createWifiStatusFromLegacyError(legacy_status), {}};
1219 }
1220 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1221 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001222 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001223 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1224 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001225 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001226}
1227
1228WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001229 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1230 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001231 WifiStatus status = registerDebugRingBufferCallback();
1232 if (status.code != WifiStatusCode::SUCCESS) {
1233 return status;
1234 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001235 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001236 getFirstActiveWlanIfaceName(), ring_name,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001237 static_cast<std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(verbose_level),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001238 max_interval_in_sec, min_data_size_in_bytes);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001239 ringbuffer_map_.insert(
1240 std::pair<std::string, Ringbuffer>(ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001241 // if verbose logging enabled, turn up HAL daemon logging as well.
1242 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1243 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1244 } else {
1245 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1246 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001247 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001248}
1249
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001250WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001251 WifiStatus status = registerDebugRingBufferCallback();
1252 if (status.code != WifiStatusCode::SUCCESS) {
1253 return status;
1254 }
1255 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001256 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001257
Roshan Piusabcf78f2017-10-06 16:30:38 -07001258 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001259}
1260
Roger Wangb294c762018-11-02 15:34:39 +08001261WifiStatus WifiChip::flushRingBufferToFileInternal() {
1262 if (!writeRingbufferFilesInternal()) {
1263 LOG(ERROR) << "Error writing files to flash";
1264 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1265 }
1266 return createWifiStatus(WifiStatusCode::SUCCESS);
1267}
1268
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001269WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001270 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001271 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001272 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1273 debug_ring_buffer_cb_registered_ = false;
1274 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001275 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001276}
1277
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001278std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1279WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001280 legacy_hal::wifi_error legacy_status;
1281 legacy_hal::WakeReasonStats legacy_stats;
1282 std::tie(legacy_status, legacy_stats) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001283 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001284 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1285 return {createWifiStatusFromLegacyError(legacy_status), {}};
1286 }
1287 WifiDebugHostWakeReasonStats hidl_stats;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001288 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats, &hidl_stats)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001289 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1290 }
1291 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001292}
1293
Roshan Pius203cb032016-12-14 17:41:20 -08001294WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001295 legacy_hal::wifi_error legacy_status;
1296 if (enable) {
1297 android::wp<WifiChip> weak_ptr_this(this);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001298 const auto& on_alert_callback = [weak_ptr_this](int32_t error_code,
1299 std::vector<uint8_t> debug_data) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001300 const auto shared_ptr_this = weak_ptr_this.promote();
1301 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1302 LOG(ERROR) << "Callback invoked on an invalid object";
1303 return;
1304 }
1305 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001306 if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001307 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1308 }
1309 }
1310 };
1311 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001312 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001313 } else {
1314 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001315 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001316 }
1317 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001318}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001319
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001320WifiStatus WifiChip::selectTxPowerScenarioInternal(V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001321 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001322 getFirstActiveWlanIfaceName(),
1323 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
Roshan Piusabcf78f2017-10-06 16:30:38 -07001324 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001325}
1326
Roshan Pius735ff432017-07-25 08:48:08 -07001327WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001328 auto legacy_status = legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001329 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001330}
1331
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001332WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1333 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001334 getFirstActiveWlanIfaceName(), hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001335 return createWifiStatusFromLegacyError(legacy_status);
1336}
1337
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001338WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001339 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001340 // Deprecated support for this callback.
1341 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001342}
1343
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001344WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001345 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001346 getFirstActiveWlanIfaceName(),
1347 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001348 return createWifiStatusFromLegacyError(legacy_status);
1349}
1350
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001351std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001352 // Deprecated support for this callback.
1353 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
1354}
1355
1356std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_5() {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001357 legacy_hal::wifi_error legacy_status;
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001358 uint64_t legacy_feature_set;
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001359 uint32_t legacy_logger_feature_set;
1360 const auto ifname = getFirstActiveWlanIfaceName();
1361 std::tie(legacy_status, legacy_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001362 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001363 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1364 return {createWifiStatusFromLegacyError(legacy_status), 0};
1365 }
1366 std::tie(legacy_status, legacy_logger_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001367 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001368 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1369 // some devices don't support querying logger feature set
1370 legacy_logger_feature_set = 0;
1371 }
1372 uint32_t hidl_caps;
1373 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001374 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001375 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1376 }
1377 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1378}
1379
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001380std::pair<WifiStatus, sp<V1_4::IWifiRttController>> WifiChip::createRttControllerInternal_1_4(
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001381 const sp<IWifiIface>& /*bound_iface*/) {
1382 LOG(ERROR) << "createRttController_1_4 is not supported on this HAL";
1383 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001384}
1385
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001386WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001387 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001388 if (!event_cb_handler_.addCallback(event_callback)) {
1389 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1390 }
1391 return createWifiStatus(WifiStatusCode::SUCCESS);
1392}
1393
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001394WifiStatus WifiChip::setMultiStaPrimaryConnectionInternal(const std::string& ifname) {
1395 auto legacy_status = legacy_hal_.lock()->multiStaSetPrimaryConnection(ifname);
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001396 return createWifiStatusFromLegacyError(legacy_status);
1397}
1398
1399WifiStatus WifiChip::setMultiStaUseCaseInternal(MultiStaUseCase use_case) {
1400 auto legacy_status = legacy_hal_.lock()->multiStaSetUseCase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001401 hidl_struct_util::convertHidlMultiStaUseCaseToLegacy(use_case));
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001402 return createWifiStatusFromLegacyError(legacy_status);
1403}
1404
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001405WifiStatus WifiChip::setCoexUnsafeChannelsInternal(std::vector<CoexUnsafeChannel> unsafe_channels,
1406 uint32_t restrictions) {
Quang Luong94bcce52020-11-25 17:52:19 -08001407 std::vector<legacy_hal::wifi_coex_unsafe_channel> legacy_unsafe_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001408 if (!hidl_struct_util::convertHidlVectorOfCoexUnsafeChannelToLegacy(unsafe_channels,
1409 &legacy_unsafe_channels)) {
Quang Luong94bcce52020-11-25 17:52:19 -08001410 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1411 }
Quang Luongab70a832020-12-14 13:01:32 -08001412 uint32_t legacy_restrictions = 0;
1413 if (restrictions & CoexRestriction::WIFI_DIRECT) {
1414 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_DIRECT;
1415 }
1416 if (restrictions & CoexRestriction::SOFTAP) {
1417 legacy_restrictions |= legacy_hal::wifi_coex_restriction::SOFTAP;
1418 }
1419 if (restrictions & CoexRestriction::WIFI_AWARE) {
1420 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_AWARE;
1421 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001422 auto legacy_status =
1423 legacy_hal_.lock()->setCoexUnsafeChannels(legacy_unsafe_channels, legacy_restrictions);
Quang Luong94bcce52020-11-25 17:52:19 -08001424 return createWifiStatusFromLegacyError(legacy_status);
1425}
1426
Kumar Anandda62c382020-11-18 17:17:47 -08001427WifiStatus WifiChip::setCountryCodeInternal(const std::array<int8_t, 2>& code) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001428 auto legacy_status = legacy_hal_.lock()->setCountryCode(getFirstActiveWlanIfaceName(), code);
Kumar Anandda62c382020-11-18 17:17:47 -08001429 return createWifiStatusFromLegacyError(legacy_status);
1430}
1431
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001432std::pair<WifiStatus, std::vector<V1_5::WifiUsableChannel>> WifiChip::getUsableChannelsInternal(
1433 WifiBand /*band*/, uint32_t /*ifaceModeMask*/, uint32_t /*filterMask*/) {
1434 LOG(ERROR) << "getUsableChannels is not supported on this HAL";
1435 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
1436}
1437
1438WifiStatus WifiChip::triggerSubsystemRestartInternal() {
1439 auto legacy_status = legacy_hal_.lock()->triggerSubsystemRestart();
1440 return createWifiStatusFromLegacyError(legacy_status);
1441}
1442
1443std::pair<WifiStatus, sp<V1_6::IWifiRttController>> WifiChip::createRttControllerInternal_1_6(
1444 const sp<IWifiIface>& bound_iface) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001445 if (sta_ifaces_.size() == 0 &&
1446 !canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001447 LOG(ERROR) << "createRttControllerInternal_1_6: Chip cannot support STAs "
1448 "(and RTT by extension)";
1449 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1450 }
1451 sp<WifiRttController> rtt =
1452 new WifiRttController(getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1453 rtt_controllers_.emplace_back(rtt);
1454 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1455}
1456
1457std::pair<WifiStatus, std::vector<V1_6::WifiUsableChannel>> WifiChip::getUsableChannelsInternal_1_6(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001458 WifiBand band, uint32_t ifaceModeMask, uint32_t filterMask) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001459 legacy_hal::wifi_error legacy_status;
1460 std::vector<legacy_hal::wifi_usable_channel> legacy_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001461 std::tie(legacy_status, legacy_usable_channels) = legacy_hal_.lock()->getUsableChannels(
Kumar Anand2a630a32021-01-21 14:09:14 -08001462 hidl_struct_util::convertHidlWifiBandToLegacyMacBand(band),
Kumar Anandaea86e02021-02-10 16:22:31 -08001463 hidl_struct_util::convertHidlWifiIfaceModeToLegacy(ifaceModeMask),
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001464 hidl_struct_util::convertHidlUsableChannelFilterToLegacy(filterMask));
Kumar Anandaea86e02021-02-10 16:22:31 -08001465
Kumar Anand2a630a32021-01-21 14:09:14 -08001466 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1467 return {createWifiStatusFromLegacyError(legacy_status), {}};
1468 }
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001469 std::vector<V1_6::WifiUsableChannel> hidl_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001470 if (!hidl_struct_util::convertLegacyWifiUsableChannelsToHidl(legacy_usable_channels,
1471 &hidl_usable_channels)) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001472 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1473 }
1474 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_usable_channels};
1475}
1476
Sunil Ravief97d232022-01-24 10:39:56 -08001477std::pair<WifiStatus, V1_6::WifiRadioCombinationMatrix>
1478WifiChip::getSupportedRadioCombinationsMatrixInternal() {
1479 legacy_hal::wifi_error legacy_status;
1480 legacy_hal::wifi_radio_combination_matrix* legacy_matrix;
1481
1482 std::tie(legacy_status, legacy_matrix) =
1483 legacy_hal_.lock()->getSupportedRadioCombinationsMatrix();
1484 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1485 LOG(ERROR) << "Failed to get SupportedRadioCombinations matrix from legacy HAL: "
1486 << legacyErrorToString(legacy_status);
1487 return {createWifiStatusFromLegacyError(legacy_status), {}};
1488 }
1489
1490 V1_6::WifiRadioCombinationMatrix hidl_matrix;
1491 if (!hidl_struct_util::convertLegacyRadioCombinationsMatrixToHidl(legacy_matrix,
1492 &hidl_matrix)) {
1493 LOG(ERROR) << "Failed convertLegacyRadioCombinationsMatrixToHidl() ";
1494 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), {}};
1495 }
1496 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_matrix};
1497}
1498
Quang Luong5d8805e2022-01-28 15:46:40 -08001499std::pair<WifiStatus, std::vector<V1_6::IWifiChip::ChipMode>>
1500WifiChip::getAvailableModesInternal_1_6() {
1501 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
1502}
1503
Roshan Piusba38d9c2017-12-08 07:32:08 -08001504WifiStatus WifiChip::handleChipConfiguration(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001505 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001506 // If the chip is already configured in a different mode, stop
1507 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001508 if (isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001509 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_ << " to mode " << mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -08001510 invalidateAndRemoveAllIfaces();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001511 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stop(lock, []() {});
Roshan Piusba38d9c2017-12-08 07:32:08 -08001512 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001513 LOG(ERROR) << "Failed to stop legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusba38d9c2017-12-08 07:32:08 -08001514 return createWifiStatusFromLegacyError(legacy_status);
1515 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001516 }
Roshan Piuscc338202017-11-02 13:54:09 -07001517 // Firmware mode change not needed for V2 devices.
1518 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001519 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001520 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001521 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001522 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1523 }
1524 if (!success) {
1525 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1526 }
1527 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1528 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001529 LOG(ERROR) << "Failed to start legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001530 return createWifiStatusFromLegacyError(legacy_status);
1531 }
Roshan Pius85c64412018-01-22 17:58:40 -08001532 // Every time the HAL is restarted, we need to register the
1533 // radio mode change callback.
1534 WifiStatus status = registerRadioModeChangeCallback();
1535 if (status.code != WifiStatusCode::SUCCESS) {
1536 // This probably is not a critical failure?
1537 LOG(ERROR) << "Failed to register radio mode change callback";
1538 }
chenpaulf5eca292019-03-14 11:08:03 +08001539 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001540 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001541 version_info = WifiChip::requestChipDebugInfoInternal();
1542 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1543 property_set("vendor.wlan.firmware.version",
1544 version_info.second.firmwareDescription.c_str());
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001545 property_set("vendor.wlan.driver.version", version_info.second.driverDescription.c_str());
chenpaulf5eca292019-03-14 11:08:03 +08001546 }
1547
Roshan Piusabcf78f2017-10-06 16:30:38 -07001548 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001549}
Roshan Pius48185b22016-12-15 19:10:30 -08001550
1551WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001552 if (debug_ring_buffer_cb_registered_) {
1553 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001554 }
Roshan Pius3797e182017-03-30 18:01:54 -07001555
Roshan Piusabcf78f2017-10-06 16:30:38 -07001556 android::wp<WifiChip> weak_ptr_this(this);
1557 const auto& on_ring_buffer_data_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001558 [weak_ptr_this](const std::string& name, const std::vector<uint8_t>& data,
1559 const legacy_hal::wifi_ring_buffer_status& status) {
1560 const auto shared_ptr_this = weak_ptr_this.promote();
1561 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1562 LOG(ERROR) << "Callback invoked on an invalid object";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301563 return;
1564 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001565 WifiDebugRingBufferStatus hidl_status;
1566 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(status,
1567 &hidl_status)) {
1568 LOG(ERROR) << "Error converting ring buffer status";
1569 return;
1570 }
1571 {
1572 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1573 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1574 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1575 Ringbuffer& cur_buffer = target->second;
1576 cur_buffer.append(data);
1577 } else {
1578 LOG(ERROR) << "Ringname " << name << " not found";
1579 return;
1580 }
1581 // unique_lock unlocked here
1582 }
1583 };
1584 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001585 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001586
Roshan Piusabcf78f2017-10-06 16:30:38 -07001587 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1588 debug_ring_buffer_cb_registered_ = true;
1589 }
1590 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001591}
1592
Roshan Pius85c64412018-01-22 17:58:40 -08001593WifiStatus WifiChip::registerRadioModeChangeCallback() {
1594 android::wp<WifiChip> weak_ptr_this(this);
1595 const auto& on_radio_mode_change_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001596 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1597 const auto shared_ptr_this = weak_ptr_this.promote();
1598 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1599 LOG(ERROR) << "Callback invoked on an invalid object";
1600 return;
Roshan Pius85c64412018-01-22 17:58:40 -08001601 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001602 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo> hidl_radio_mode_infos;
1603 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(mac_infos,
1604 &hidl_radio_mode_infos)) {
1605 LOG(ERROR) << "Error converting wifi mac info";
1606 return;
1607 }
1608 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1609 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos).isOk()) {
1610 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
1611 << " callback on: " << toString(callback);
1612 }
1613 }
1614 };
Roshan Pius85c64412018-01-22 17:58:40 -08001615 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001616 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1617 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001618 return createWifiStatusFromLegacyError(legacy_status);
1619}
1620
Quang Luong5d8805e2022-01-28 15:46:40 -08001621std::vector<V1_6::IWifiChip::ChipConcurrencyCombination>
1622WifiChip::getCurrentModeConcurrencyCombinations() {
Roshan Piuscc338202017-11-02 13:54:09 -07001623 if (!isValidModeId(current_mode_id_)) {
1624 LOG(ERROR) << "Chip not configured in a mode yet";
1625 return {};
1626 }
1627 for (const auto& mode : modes_) {
1628 if (mode.id == current_mode_id_) {
1629 return mode.availableCombinations;
1630 }
1631 }
Quang Luong5d8805e2022-01-28 15:46:40 -08001632 CHECK(0) << "Expected to find concurrency combinations for current mode!";
Roshan Piuscc338202017-11-02 13:54:09 -07001633 return {};
1634}
1635
Quang Luong5d8805e2022-01-28 15:46:40 -08001636// Returns a map indexed by IfaceConcurrencyType with the number of ifaces currently
1637// created of the corresponding concurrency type.
1638std::map<IfaceConcurrencyType, size_t> WifiChip::getCurrentConcurrencyCombination() {
1639 std::map<IfaceConcurrencyType, size_t> iface_counts;
1640 uint32_t num_ap = 0;
1641 uint32_t num_ap_bridged = 0;
1642 for (const auto& ap_iface : ap_ifaces_) {
1643 std::string ap_iface_name = ap_iface->getName();
1644 if (br_ifaces_ap_instances_.count(ap_iface_name) > 0 &&
1645 br_ifaces_ap_instances_[ap_iface_name].size() > 1) {
1646 num_ap_bridged++;
1647 } else {
1648 num_ap++;
1649 }
1650 }
1651 iface_counts[IfaceConcurrencyType::AP] = num_ap;
1652 iface_counts[IfaceConcurrencyType::AP_BRIDGED] = num_ap_bridged;
1653 iface_counts[IfaceConcurrencyType::NAN] = nan_ifaces_.size();
1654 iface_counts[IfaceConcurrencyType::P2P] = p2p_ifaces_.size();
1655 iface_counts[IfaceConcurrencyType::STA] = sta_ifaces_.size();
Roshan Piuscc338202017-11-02 13:54:09 -07001656 return iface_counts;
1657}
1658
Quang Luong5d8805e2022-01-28 15:46:40 -08001659// This expands the provided concurrency combinations to a more parseable
Roshan Piuscc338202017-11-02 13:54:09 -07001660// form. Returns a vector of available combinations possible with the number
Quang Luong5d8805e2022-01-28 15:46:40 -08001661// of each concurrency type in the combination.
1662// This method is a port of HalDeviceManager.expandConcurrencyCombos() from framework.
1663std::vector<std::map<IfaceConcurrencyType, size_t>> WifiChip::expandConcurrencyCombinations(
1664 const V1_6::IWifiChip::ChipConcurrencyCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001665 uint32_t num_expanded_combos = 1;
1666 for (const auto& limit : combination.limits) {
1667 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1668 num_expanded_combos *= limit.types.size();
1669 }
1670 }
1671
Quang Luong5d8805e2022-01-28 15:46:40 -08001672 // Allocate the vector of expanded combos and reset all concurrency type counts to 0
Roshan Piuscc338202017-11-02 13:54:09 -07001673 // in each combo.
Quang Luong5d8805e2022-01-28 15:46:40 -08001674 std::vector<std::map<IfaceConcurrencyType, size_t>> expanded_combos;
Roshan Piuscc338202017-11-02 13:54:09 -07001675 expanded_combos.resize(num_expanded_combos);
1676 for (auto& expanded_combo : expanded_combos) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001677 for (const auto type :
1678 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, IfaceConcurrencyType::NAN,
1679 IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001680 expanded_combo[type] = 0;
1681 }
1682 }
1683 uint32_t span = num_expanded_combos;
1684 for (const auto& limit : combination.limits) {
1685 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1686 span /= limit.types.size();
1687 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001688 const auto iface_type = limit.types[(k / span) % limit.types.size()];
Roshan Piuscc338202017-11-02 13:54:09 -07001689 expanded_combos[k][iface_type]++;
1690 }
1691 }
1692 }
1693 return expanded_combos;
1694}
1695
Quang Luong5d8805e2022-01-28 15:46:40 -08001696bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(
1697 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1698 IfaceConcurrencyType requested_type) {
1699 const auto current_combo = getCurrentConcurrencyCombination();
Roshan Piuscc338202017-11-02 13:54:09 -07001700
1701 // Check if we have space for 1 more iface of |type| in this combo
Quang Luong5d8805e2022-01-28 15:46:40 -08001702 for (const auto type :
1703 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, IfaceConcurrencyType::NAN,
1704 IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001705 size_t num_ifaces_needed = current_combo.at(type);
1706 if (type == requested_type) {
1707 num_ifaces_needed++;
1708 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001709 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001710 if (num_ifaces_needed > num_ifaces_allowed) {
1711 return false;
1712 }
1713 }
1714 return true;
1715}
1716
1717// This method does the following:
Quang Luong5d8805e2022-01-28 15:46:40 -08001718// a) Enumerate all possible concurrency combos by expanding the current
1719// ChipConcurrencyCombination.
1720// b) Check if the requested concurrency type can be added to the current mode
1721// with the concurrency combination that is already active.
1722bool WifiChip::canCurrentModeSupportConcurrencyTypeWithCurrentTypes(
1723 IfaceConcurrencyType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001724 if (!isValidModeId(current_mode_id_)) {
1725 LOG(ERROR) << "Chip not configured in a mode yet";
1726 return false;
1727 }
Quang Luong5d8805e2022-01-28 15:46:40 -08001728 const auto combinations = getCurrentModeConcurrencyCombinations();
Roshan Piuscc338202017-11-02 13:54:09 -07001729 for (const auto& combination : combinations) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001730 const auto expanded_combos = expandConcurrencyCombinations(combination);
Roshan Piuscc338202017-11-02 13:54:09 -07001731 for (const auto& expanded_combo : expanded_combos) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001732 if (canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(expanded_combo,
1733 requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001734 return true;
1735 }
1736 }
1737 }
1738 return false;
1739}
1740
Quang Luong5d8805e2022-01-28 15:46:40 -08001741// Note: This does not consider concurrency types already active. It only checks if the
1742// provided expanded concurrency combination can support the requested combo.
1743bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyCombo(
1744 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1745 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
1746 // Check if we have space for 1 more |type| in this combo
1747 for (const auto type :
1748 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, IfaceConcurrencyType::NAN,
1749 IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001750 if (req_combo.count(type) == 0) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001751 // Concurrency type not in the req_combo.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001752 continue;
1753 }
1754 size_t num_ifaces_needed = req_combo.at(type);
1755 size_t num_ifaces_allowed = expanded_combo.at(type);
1756 if (num_ifaces_needed > num_ifaces_allowed) {
1757 return false;
1758 }
1759 }
1760 return true;
1761}
1762// This method does the following:
Quang Luong5d8805e2022-01-28 15:46:40 -08001763// a) Enumerate all possible concurrency combos by expanding the current
1764// ChipConcurrencyCombination.
1765// b) Check if the requested concurrency combo can be added to the current mode.
1766// Note: This does not consider concurrency types already active. It only checks if the
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001767// current mode can support the requested combo.
Quang Luong5d8805e2022-01-28 15:46:40 -08001768bool WifiChip::canCurrentModeSupportConcurrencyCombo(
1769 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001770 if (!isValidModeId(current_mode_id_)) {
1771 LOG(ERROR) << "Chip not configured in a mode yet";
1772 return false;
1773 }
Quang Luong5d8805e2022-01-28 15:46:40 -08001774 const auto combinations = getCurrentModeConcurrencyCombinations();
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001775 for (const auto& combination : combinations) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001776 const auto expanded_combos = expandConcurrencyCombinations(combination);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001777 for (const auto& expanded_combo : expanded_combos) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001778 if (canExpandedConcurrencyComboSupportConcurrencyCombo(expanded_combo, req_combo)) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001779 return true;
1780 }
1781 }
1782 }
1783 return false;
1784}
1785
1786// This method does the following:
Quang Luong5d8805e2022-01-28 15:46:40 -08001787// a) Enumerate all possible concurrency combos by expanding the current
1788// ChipConcurrencyCombination.
1789// b) Check if the requested concurrency type can be added to the current mode.
1790bool WifiChip::canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type) {
1791 // Check if we can support at least 1 of the requested concurrency type.
1792 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001793 req_iface_combo[requested_type] = 1;
Quang Luong5d8805e2022-01-28 15:46:40 -08001794 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001795}
1796
Roshan Piuscc338202017-11-02 13:54:09 -07001797bool WifiChip::isValidModeId(ChipModeId mode_id) {
1798 for (const auto& mode : modes_) {
1799 if (mode.id == mode_id) {
1800 return true;
1801 }
1802 }
1803 return false;
1804}
1805
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001806bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
lesl261818b2020-11-27 12:37:35 +08001807 // Check if we can support at least 1 STA & 1 AP concurrently.
Quang Luong5d8805e2022-01-28 15:46:40 -08001808 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1809 req_iface_combo[IfaceConcurrencyType::STA] = 1;
1810 req_iface_combo[IfaceConcurrencyType::AP] = 1;
1811 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001812}
1813
lesl261818b2020-11-27 12:37:35 +08001814bool WifiChip::isDualStaConcurrencyAllowedInCurrentMode() {
1815 // Check if we can support at least 2 STA concurrently.
Quang Luong5d8805e2022-01-28 15:46:40 -08001816 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1817 req_iface_combo[IfaceConcurrencyType::STA] = 2;
1818 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
James Mattisd2e4c072019-05-22 16:14:48 -07001819}
1820
Roshan Pius6036c022019-03-27 10:41:58 -07001821std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001822 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
leslf012b652021-01-08 15:22:49 +08001823 if (ap_ifaces_.size() > 0) {
1824 // If the first active wlan iface is bridged iface.
1825 // Return first instance name.
1826 for (auto const& it : br_ifaces_ap_instances_) {
1827 if (it.first == ap_ifaces_[0]->getName()) {
1828 return it.second[0];
1829 }
1830 }
1831 return ap_ifaces_[0]->getName();
1832 }
Roshan Pius6036c022019-03-27 10:41:58 -07001833 // This could happen if the chip call is made before any STA/AP
1834 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001835 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Jimmy Chen2dddd792019-12-23 17:50:39 +02001836 return getWlanIfaceNameWithType(IfaceType::STA, 0);
Roshan Pius6036c022019-03-27 10:41:58 -07001837}
1838
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001839// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1840// not already in use.
1841// Note: This doesn't check the actual presence of these interfaces.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001842std::string WifiChip::allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001843 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001844 const auto ifname = getWlanIfaceNameWithType(type, idx);
lesl94d28242020-11-18 22:17:37 +08001845 if (findUsingNameFromBridgedApInstances(ifname)) continue;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001846 if (findUsingName(ap_ifaces_, ifname)) continue;
1847 if (findUsingName(sta_ifaces_, ifname)) continue;
1848 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001849 }
1850 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001851 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001852 return {};
1853}
1854
lesl261818b2020-11-27 12:37:35 +08001855uint32_t WifiChip::startIdxOfApIface() {
1856 if (isDualStaConcurrencyAllowedInCurrentMode()) {
1857 // When the HAL support dual STAs, AP should start with idx 2.
1858 return 2;
1859 } else if (isStaApConcurrencyAllowedInCurrentMode()) {
1860 // When the HAL support STA + AP but it doesn't support dual STAs.
1861 // AP should start with idx 1.
1862 return 1;
1863 }
1864 // No concurrency support.
1865 return 0;
1866}
1867
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001868// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001869// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001870std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001871 // Check if we have a dedicated iface for AP.
lesl261818b2020-11-27 12:37:35 +08001872 std::vector<std::string> ifnames = getPredefinedApIfaceNames(false);
1873 if (!ifnames.empty()) {
1874 return ifnames[0];
Roshan Pius78cb5992020-04-30 12:39:21 -07001875 }
lesl261818b2020-11-27 12:37:35 +08001876 return allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface());
1877}
1878
1879std::vector<std::string> WifiChip::allocateBridgedApInstanceNames() {
1880 // Check if we have a dedicated iface for AP.
1881 std::vector<std::string> instances = getPredefinedApIfaceNames(true);
1882 if (instances.size() == 2) {
1883 return instances;
1884 } else {
1885 int num_ifaces_need_to_allocate = 2 - instances.size();
1886 for (int i = 0; i < num_ifaces_need_to_allocate; i++) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001887 std::string instance_name =
1888 allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface() + i);
lesl261818b2020-11-27 12:37:35 +08001889 if (!instance_name.empty()) {
1890 instances.push_back(instance_name);
1891 }
1892 }
1893 }
1894 return instances;
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001895}
1896
1897// STA iface names start with idx 0.
1898// Primary STA iface will always be 0.
1899std::string WifiChip::allocateStaIfaceName() {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001900 return allocateApOrStaIfaceName(IfaceType::STA, 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001901}
1902
xshu5899e8e2018-01-09 15:36:03 -08001903bool WifiChip::writeRingbufferFilesInternal() {
1904 if (!removeOldFilesInternal()) {
1905 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1906 return false;
1907 }
1908 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301909 {
1910 std::unique_lock<std::mutex> lk(lock_t);
xshuc905ea62021-07-11 19:57:02 -07001911 for (auto& item : ringbuffer_map_) {
1912 Ringbuffer& cur_buffer = item.second;
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301913 if (cur_buffer.getData().empty()) {
1914 continue;
1915 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001916 const std::string file_path_raw = kTombstoneFolderPath + item.first + "XXXXXXXXXX";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301917 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1918 if (dump_fd == -1) {
1919 PLOG(ERROR) << "create file failed";
1920 return false;
1921 }
1922 unique_fd file_auto_closer(dump_fd);
1923 for (const auto& cur_block : cur_buffer.getData()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001924 if (write(dump_fd, cur_block.data(), sizeof(cur_block[0]) * cur_block.size()) ==
1925 -1) {
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301926 PLOG(ERROR) << "Error writing to file";
1927 }
xshu5899e8e2018-01-09 15:36:03 -08001928 }
xshuc905ea62021-07-11 19:57:02 -07001929 cur_buffer.clear();
xshu5899e8e2018-01-09 15:36:03 -08001930 }
xshu0a0fe512020-07-22 17:53:37 -07001931 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001932 }
1933 return true;
1934}
1935
Jimmy Chen2dddd792019-12-23 17:50:39 +02001936std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1937 std::string ifname;
1938
1939 // let the legacy hal override the interface name
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001940 legacy_hal::wifi_error err = legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
Jimmy Chen2dddd792019-12-23 17:50:39 +02001941 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1942
1943 return getWlanIfaceName(idx);
1944}
1945
lesl94d28242020-11-18 22:17:37 +08001946void WifiChip::invalidateAndClearBridgedApAll() {
1947 for (auto const& it : br_ifaces_ap_instances_) {
1948 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001949 iface_util_->removeIfaceFromBridge(it.first, iface);
lesl94d28242020-11-18 22:17:37 +08001950 legacy_hal_.lock()->deleteVirtualInterface(iface);
1951 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001952 iface_util_->deleteBridge(it.first);
lesl94d28242020-11-18 22:17:37 +08001953 }
1954 br_ifaces_ap_instances_.clear();
1955}
1956
1957void WifiChip::invalidateAndClearBridgedAp(const std::string& br_name) {
1958 if (br_name.empty()) return;
1959 // delete managed interfaces
1960 for (auto const& it : br_ifaces_ap_instances_) {
1961 if (it.first == br_name) {
1962 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001963 iface_util_->removeIfaceFromBridge(br_name, iface);
lesl94d28242020-11-18 22:17:37 +08001964 legacy_hal_.lock()->deleteVirtualInterface(iface);
1965 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001966 iface_util_->deleteBridge(br_name);
lesl94d28242020-11-18 22:17:37 +08001967 br_ifaces_ap_instances_.erase(br_name);
1968 break;
1969 }
1970 }
1971 return;
1972}
1973
1974bool WifiChip::findUsingNameFromBridgedApInstances(const std::string& name) {
1975 for (auto const& it : br_ifaces_ap_instances_) {
1976 if (it.first == name) {
1977 return true;
1978 }
1979 for (auto const& iface : it.second) {
1980 if (iface == name) {
1981 return true;
1982 }
1983 }
1984 }
1985 return false;
1986}
1987
Roshan Pius79a99752016-10-04 13:03:58 -07001988} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001989} // namespace V1_6
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001990} // namespace wifi
1991} // namespace hardware
1992} // namespace android