blob: 6cc12358e54bdbbcc5ff74bd7147fc14d4b500eb [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 Luong6d661672022-02-16 13:08:39 -0800794 // Deprecated support -- use getAvailableModes_1_6 for more granular concurrency combinations.
795 std::vector<V1_0::IWifiChip::ChipMode> modes_1_0 = {};
796 for (const auto& mode_1_6 : modes_) {
797 std::vector<V1_0::IWifiChip::ChipIfaceCombination> combos_1_0;
798 for (const auto& combo_1_6 : mode_1_6.availableCombinations) {
799 std::vector<V1_0::IWifiChip::ChipIfaceCombinationLimit> limits_1_0;
800 for (const auto& limit_1_6 : combo_1_6.limits) {
801 std::vector<IfaceType> types_1_0;
802 for (IfaceConcurrencyType type_1_6 : limit_1_6.types) {
803 switch (type_1_6) {
804 case IfaceConcurrencyType::STA:
805 types_1_0.push_back(IfaceType::STA);
806 break;
807 case IfaceConcurrencyType::AP:
808 types_1_0.push_back(IfaceType::AP);
809 break;
810 case IfaceConcurrencyType::AP_BRIDGED:
811 // Ignore AP_BRIDGED
812 break;
813 case IfaceConcurrencyType::P2P:
814 types_1_0.push_back(IfaceType::P2P);
815 break;
816 case IfaceConcurrencyType::NAN:
817 types_1_0.push_back(IfaceType::NAN);
818 break;
819 }
820 }
821 if (types_1_0.empty()) {
822 continue;
823 }
824 V1_0::IWifiChip::ChipIfaceCombinationLimit limit_1_0;
825 limit_1_0.types = hidl_vec(types_1_0);
826 limit_1_0.maxIfaces = limit_1_6.maxIfaces;
827 limits_1_0.push_back(limit_1_0);
828 }
829 if (limits_1_0.empty()) {
830 continue;
831 }
832 V1_0::IWifiChip::ChipIfaceCombination combo_1_0;
833 combo_1_0.limits = hidl_vec(limits_1_0);
834 combos_1_0.push_back(combo_1_0);
835 }
836 if (combos_1_0.empty()) {
837 continue;
838 }
839 V1_0::IWifiChip::ChipMode mode_1_0;
840 mode_1_0.id = mode_1_6.id;
841 mode_1_0.availableCombinations = hidl_vec(combos_1_0);
842 modes_1_0.push_back(mode_1_0);
843 }
844 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_1_0};
Roshan Pius3c868522016-10-27 12:43:49 -0700845}
846
Roshan Piusba38d9c2017-12-08 07:32:08 -0800847WifiStatus WifiChip::configureChipInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800848 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700849 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700850 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
851 }
852 if (mode_id == current_mode_id_) {
853 LOG(DEBUG) << "Already in the specified mode " << mode_id;
854 return createWifiStatus(WifiStatusCode::SUCCESS);
855 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800856 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700857 if (status.code != WifiStatusCode::SUCCESS) {
858 for (const auto& callback : event_cb_handler_.getCallbacks()) {
859 if (!callback->onChipReconfigureFailure(status).isOk()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800860 LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
Roshan Piusabcf78f2017-10-06 16:30:38 -0700861 }
862 }
863 return status;
864 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800865 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700866 if (!callback->onChipReconfigured(mode_id).isOk()) {
867 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
868 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800869 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700870 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800871 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700872 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700873
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800874 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(subsystemCallbackHandler_);
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700875
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800876 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700877}
878
879std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700880 if (!isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800881 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), current_mode_id_};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700882 }
883 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700884}
885
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800886std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200887 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700888 legacy_hal::wifi_error legacy_status;
889 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700890 const auto ifname = getFirstActiveWlanIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800891 std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700892 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800893 LOG(ERROR) << "Failed to get driver version: " << legacyErrorToString(legacy_status);
894 WifiStatus status =
895 createWifiStatusFromLegacyError(legacy_status, "failed to get driver version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700896 return {status, result};
897 }
898 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700899
Roshan Piusabcf78f2017-10-06 16:30:38 -0700900 std::string firmware_desc;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800901 std::tie(legacy_status, firmware_desc) = legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700902 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800903 LOG(ERROR) << "Failed to get firmware version: " << legacyErrorToString(legacy_status);
904 WifiStatus status =
905 createWifiStatusFromLegacyError(legacy_status, "failed to get firmware version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700906 return {status, result};
907 }
908 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700909
Roshan Piusabcf78f2017-10-06 16:30:38 -0700910 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700911}
912
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800913std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700914 legacy_hal::wifi_error legacy_status;
915 std::vector<uint8_t> driver_dump;
916 std::tie(legacy_status, driver_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800917 legacy_hal_.lock()->requestDriverMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700918 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800919 LOG(ERROR) << "Failed to get driver debug dump: " << legacyErrorToString(legacy_status);
920 return {createWifiStatusFromLegacyError(legacy_status), std::vector<uint8_t>()};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700921 }
922 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700923}
924
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800925std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700926 legacy_hal::wifi_error legacy_status;
927 std::vector<uint8_t> firmware_dump;
928 std::tie(legacy_status, firmware_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800929 legacy_hal_.lock()->requestFirmwareMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700930 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800931 LOG(ERROR) << "Failed to get firmware debug dump: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700932 return {createWifiStatusFromLegacyError(legacy_status), {}};
933 }
934 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700935}
936
lesl94d28242020-11-18 22:17:37 +0800937WifiStatus WifiChip::createVirtualApInterface(const std::string& apVirtIf) {
938 legacy_hal::wifi_error legacy_status;
939 legacy_status = legacy_hal_.lock()->createVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800940 apVirtIf, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
Sunil Raviddab4bb2020-02-03 22:45:19 -0800941 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
lesl94d28242020-11-18 22:17:37 +0800942 LOG(ERROR) << "Failed to add interface: " << apVirtIf << " "
Sunil Raviddab4bb2020-02-03 22:45:19 -0800943 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +0800944 return createWifiStatusFromLegacyError(legacy_status);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800945 }
lesl94d28242020-11-18 22:17:37 +0800946 return createWifiStatus(WifiStatusCode::SUCCESS);
947}
948
949sp<WifiApIface> WifiChip::newWifiApIface(std::string& ifname) {
lesl420c4fc2020-11-23 19:33:04 +0800950 std::vector<std::string> ap_instances;
951 for (auto const& it : br_ifaces_ap_instances_) {
952 if (it.first == ifname) {
953 ap_instances = it.second;
954 }
955 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800956 sp<WifiApIface> iface = new WifiApIface(ifname, ap_instances, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700957 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700958 for (const auto& callback : event_cb_handler_.getCallbacks()) {
959 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
960 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
961 }
962 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700963 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
lesl94d28242020-11-18 22:17:37 +0800964 return iface;
965}
966
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800967std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createApIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -0800968 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP)) {
lesl94d28242020-11-18 22:17:37 +0800969 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
970 }
971 std::string ifname = allocateApIfaceName();
972 WifiStatus status = createVirtualApInterface(ifname);
973 if (status.code != WifiStatusCode::SUCCESS) {
974 return {status, {}};
975 }
976 sp<WifiApIface> iface = newWifiApIface(ifname);
977 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
978}
979
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800980std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createBridgedApIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -0800981 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP_BRIDGED)) {
lesl94d28242020-11-18 22:17:37 +0800982 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
983 }
lesl261818b2020-11-27 12:37:35 +0800984 std::vector<std::string> ap_instances = allocateBridgedApInstanceNames();
985 if (ap_instances.size() < 2) {
986 LOG(ERROR) << "Fail to allocate two instances";
987 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
988 }
989 std::string br_ifname = kApBridgeIfacePrefix + ap_instances[0];
lesl94d28242020-11-18 22:17:37 +0800990 for (int i = 0; i < 2; i++) {
lesl261818b2020-11-27 12:37:35 +0800991 WifiStatus status = createVirtualApInterface(ap_instances[i]);
lesl94d28242020-11-18 22:17:37 +0800992 if (status.code != WifiStatusCode::SUCCESS) {
lesl261818b2020-11-27 12:37:35 +0800993 if (i != 0) { // The failure happened when creating second virtual
994 // iface.
lesl94d28242020-11-18 22:17:37 +0800995 legacy_hal_.lock()->deleteVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800996 ap_instances.front()); // Remove the first virtual iface.
lesl94d28242020-11-18 22:17:37 +0800997 }
998 return {status, {}};
999 }
lesl94d28242020-11-18 22:17:37 +08001000 }
1001 br_ifaces_ap_instances_[br_ifname] = ap_instances;
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001002 if (!iface_util_->createBridge(br_ifname)) {
lesl94d28242020-11-18 22:17:37 +08001003 LOG(ERROR) << "Failed createBridge - br_name=" << br_ifname.c_str();
1004 invalidateAndClearBridgedAp(br_ifname);
1005 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1006 }
1007 for (auto const& instance : ap_instances) {
1008 // Bind ap instance interface to AP bridge
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001009 if (!iface_util_->addIfaceToBridge(br_ifname, instance)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001010 LOG(ERROR) << "Failed add if to Bridge - if_name=" << instance.c_str();
lesl94d28242020-11-18 22:17:37 +08001011 invalidateAndClearBridgedAp(br_ifname);
1012 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1013 }
1014 }
1015 sp<WifiApIface> iface = newWifiApIface(br_ifname);
Roshan Pius675609b2017-10-31 14:24:58 -07001016 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001017}
1018
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001019std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001020 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001021 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1022 }
Roshan Pius675609b2017-10-31 14:24:58 -07001023 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001024}
1025
lesl420c4fc2020-11-23 19:33:04 +08001026std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::getApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001027 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001028 const auto iface = findUsingName(ap_ifaces_, ifname);
1029 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001030 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1031 }
Roshan Pius675609b2017-10-31 14:24:58 -07001032 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001033}
1034
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001035WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001036 const auto iface = findUsingName(ap_ifaces_, ifname);
1037 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001038 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001039 }
Roshan Pius82368502019-05-16 12:53:02 -07001040 // Invalidate & remove any dependent objects first.
1041 // Note: This is probably not required because we never create
1042 // nan/rtt objects over AP iface. But, there is no harm to do it
1043 // here and not make that assumption all over the place.
1044 invalidateAndRemoveDependencies(ifname);
lesl94d28242020-11-18 22:17:37 +08001045 // Clear the bridge interface and the iface instance.
1046 invalidateAndClearBridgedAp(ifname);
Roshan Pius675609b2017-10-31 14:24:58 -07001047 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001048 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1049 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
1050 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1051 }
1052 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001053 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001054 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001055}
1056
lesl94d28242020-11-18 22:17:37 +08001057WifiStatus WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001058 const std::string& ifname, const std::string& ifInstanceName) {
lesl94d28242020-11-18 22:17:37 +08001059 const auto iface = findUsingName(ap_ifaces_, ifname);
lesl819e3722021-01-07 09:49:04 +08001060 if (!iface.get() || ifInstanceName.empty()) {
lesl94d28242020-11-18 22:17:37 +08001061 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1062 }
1063 // Requires to remove one of the instance in bridge mode
1064 for (auto const& it : br_ifaces_ap_instances_) {
1065 if (it.first == ifname) {
Les Lee03d642f2021-06-21 21:25:20 +08001066 std::vector<std::string> ap_instances = it.second;
1067 for (auto const& iface : ap_instances) {
lesl94d28242020-11-18 22:17:37 +08001068 if (iface == ifInstanceName) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001069 if (!iface_util_->removeIfaceFromBridge(it.first, iface)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001070 LOG(ERROR) << "Failed to remove interface: " << ifInstanceName << " from "
1071 << ifname;
1072 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE);
lesl94d28242020-11-18 22:17:37 +08001073 }
George Burgess IV2c0a47d2021-01-20 21:14:13 -08001074 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001075 legacy_hal_.lock()->deleteVirtualInterface(iface);
lesl94d28242020-11-18 22:17:37 +08001076 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001077 LOG(ERROR) << "Failed to del interface: " << iface << " "
1078 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +08001079 return createWifiStatusFromLegacyError(legacy_status);
1080 }
Les Lee03d642f2021-06-21 21:25:20 +08001081 ap_instances.erase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001082 std::remove(ap_instances.begin(), ap_instances.end(), ifInstanceName),
1083 ap_instances.end());
Les Lee03d642f2021-06-21 21:25:20 +08001084 br_ifaces_ap_instances_[ifname] = ap_instances;
1085 break;
lesl94d28242020-11-18 22:17:37 +08001086 }
1087 }
1088 break;
1089 }
1090 }
lesl669c9062021-01-22 19:37:47 +08001091 iface->removeInstance(ifInstanceName);
Les Lee03d642f2021-06-21 21:25:20 +08001092 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1093
lesl94d28242020-11-18 22:17:37 +08001094 return createWifiStatus(WifiStatusCode::SUCCESS);
1095}
1096
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001097std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -08001098 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001099 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -08001100 }
Roshan Pius5ba0a902020-04-14 11:55:42 -07001101 bool is_dedicated_iface = true;
lesl261818b2020-11-27 12:37:35 +08001102 std::string ifname = getPredefinedNanIfaceName();
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001103 if (ifname.empty() || !iface_util_->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -07001104 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
1105 // not defined.
1106 ifname = getFirstActiveWlanIfaceName();
1107 is_dedicated_iface = false;
1108 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001109 sp<WifiNanIface> iface = new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -07001110 nan_ifaces_.push_back(iface);
1111 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1112 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
1113 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1114 }
1115 }
1116 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001117}
1118
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001119std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001120 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001121 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1122 }
Roshan Pius675609b2017-10-31 14:24:58 -07001123 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001124}
1125
Jimmy Chend460df32019-11-29 17:31:22 +02001126std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001127 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001128 const auto iface = findUsingName(nan_ifaces_, ifname);
1129 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001130 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1131 }
Roshan Pius675609b2017-10-31 14:24:58 -07001132 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001133}
1134
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001135WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001136 const auto iface = findUsingName(nan_ifaces_, ifname);
1137 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001138 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001139 }
Roshan Pius675609b2017-10-31 14:24:58 -07001140 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001141 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1142 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
1143 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1144 }
1145 }
1146 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001147}
1148
Roshan Pius3c868522016-10-27 12:43:49 -07001149std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -08001150 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001151 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001152 }
lesl261818b2020-11-27 12:37:35 +08001153 std::string ifname = getPredefinedP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -07001154 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
1155 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001156 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1157 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
1158 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1159 }
1160 }
Roshan Pius675609b2017-10-31 14:24:58 -07001161 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001162}
1163
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001164std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001165 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001166 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1167 }
Roshan Pius675609b2017-10-31 14:24:58 -07001168 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001169}
1170
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001171std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001172 const auto iface = findUsingName(p2p_ifaces_, ifname);
1173 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001174 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1175 }
Roshan Pius675609b2017-10-31 14:24:58 -07001176 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001177}
1178
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001179WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001180 const auto iface = findUsingName(p2p_ifaces_, ifname);
1181 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001182 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001183 }
Roshan Pius675609b2017-10-31 14:24:58 -07001184 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001185 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1186 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1187 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1188 }
1189 }
1190 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001191}
1192
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001193std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Quang Luong5d8805e2022-01-28 15:46:40 -08001194 if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001195 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001196 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001197 std::string ifname = allocateStaIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001198 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->createVirtualInterface(
1199 ifname, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
Sunil Raviddab4bb2020-02-03 22:45:19 -08001200 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1201 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1202 << legacyErrorToString(legacy_status);
1203 return {createWifiStatusFromLegacyError(legacy_status), {}};
1204 }
Roshan Pius99dab382019-02-14 07:57:10 -08001205 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001206 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001207 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1208 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1209 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1210 }
1211 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001212 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001213 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001214}
1215
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001216std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001217 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001218 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1219 }
Roshan Pius675609b2017-10-31 14:24:58 -07001220 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001221}
1222
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001223std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001224 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001225 const auto iface = findUsingName(sta_ifaces_, ifname);
1226 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001227 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1228 }
Roshan Pius675609b2017-10-31 14:24:58 -07001229 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001230}
1231
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001232WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001233 const auto iface = findUsingName(sta_ifaces_, ifname);
1234 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001235 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001236 }
Roshan Pius82368502019-05-16 12:53:02 -07001237 // Invalidate & remove any dependent objects first.
1238 invalidateAndRemoveDependencies(ifname);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001239 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->deleteVirtualInterface(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001240 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1241 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1242 << legacyErrorToString(legacy_status);
1243 }
Roshan Pius675609b2017-10-31 14:24:58 -07001244 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001245 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1246 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1247 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1248 }
1249 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001250 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001251 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001252}
1253
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001254std::pair<WifiStatus, sp<V1_0::IWifiRttController>> WifiChip::createRttControllerInternal(
1255 const sp<IWifiIface>& /*bound_iface*/) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001256 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001257 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001258}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001259
1260std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1261WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001262 legacy_hal::wifi_error legacy_status;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001263 std::vector<legacy_hal::wifi_ring_buffer_status> legacy_ring_buffer_status_vec;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001264 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001265 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001266 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1267 return {createWifiStatusFromLegacyError(legacy_status), {}};
1268 }
1269 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1270 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001271 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001272 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1273 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001274 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001275}
1276
1277WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001278 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1279 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001280 WifiStatus status = registerDebugRingBufferCallback();
1281 if (status.code != WifiStatusCode::SUCCESS) {
1282 return status;
1283 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001284 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001285 getFirstActiveWlanIfaceName(), ring_name,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001286 static_cast<std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(verbose_level),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001287 max_interval_in_sec, min_data_size_in_bytes);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001288 ringbuffer_map_.insert(
1289 std::pair<std::string, Ringbuffer>(ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001290 // if verbose logging enabled, turn up HAL daemon logging as well.
1291 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1292 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1293 } else {
1294 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1295 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001296 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001297}
1298
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001299WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001300 WifiStatus status = registerDebugRingBufferCallback();
1301 if (status.code != WifiStatusCode::SUCCESS) {
1302 return status;
1303 }
1304 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001305 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001306
Roshan Piusabcf78f2017-10-06 16:30:38 -07001307 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001308}
1309
Roger Wangb294c762018-11-02 15:34:39 +08001310WifiStatus WifiChip::flushRingBufferToFileInternal() {
1311 if (!writeRingbufferFilesInternal()) {
1312 LOG(ERROR) << "Error writing files to flash";
1313 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1314 }
1315 return createWifiStatus(WifiStatusCode::SUCCESS);
1316}
1317
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001318WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001319 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001320 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001321 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1322 debug_ring_buffer_cb_registered_ = false;
1323 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001324 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001325}
1326
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001327std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1328WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001329 legacy_hal::wifi_error legacy_status;
1330 legacy_hal::WakeReasonStats legacy_stats;
1331 std::tie(legacy_status, legacy_stats) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001332 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001333 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1334 return {createWifiStatusFromLegacyError(legacy_status), {}};
1335 }
1336 WifiDebugHostWakeReasonStats hidl_stats;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001337 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats, &hidl_stats)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001338 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1339 }
1340 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001341}
1342
Roshan Pius203cb032016-12-14 17:41:20 -08001343WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001344 legacy_hal::wifi_error legacy_status;
1345 if (enable) {
1346 android::wp<WifiChip> weak_ptr_this(this);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001347 const auto& on_alert_callback = [weak_ptr_this](int32_t error_code,
1348 std::vector<uint8_t> debug_data) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001349 const auto shared_ptr_this = weak_ptr_this.promote();
1350 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1351 LOG(ERROR) << "Callback invoked on an invalid object";
1352 return;
1353 }
1354 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001355 if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001356 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1357 }
1358 }
1359 };
1360 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001361 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001362 } else {
1363 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001364 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001365 }
1366 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001367}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001368
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001369WifiStatus WifiChip::selectTxPowerScenarioInternal(V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001370 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001371 getFirstActiveWlanIfaceName(),
1372 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
Roshan Piusabcf78f2017-10-06 16:30:38 -07001373 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001374}
1375
Roshan Pius735ff432017-07-25 08:48:08 -07001376WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001377 auto legacy_status = legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001378 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001379}
1380
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001381WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1382 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001383 getFirstActiveWlanIfaceName(), hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001384 return createWifiStatusFromLegacyError(legacy_status);
1385}
1386
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001387WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001388 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001389 // Deprecated support for this callback.
1390 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001391}
1392
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001393WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001394 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001395 getFirstActiveWlanIfaceName(),
1396 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001397 return createWifiStatusFromLegacyError(legacy_status);
1398}
1399
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001400std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001401 // Deprecated support for this callback.
1402 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
1403}
1404
1405std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_5() {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001406 legacy_hal::wifi_error legacy_status;
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001407 uint64_t legacy_feature_set;
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001408 uint32_t legacy_logger_feature_set;
1409 const auto ifname = getFirstActiveWlanIfaceName();
1410 std::tie(legacy_status, legacy_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001411 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001412 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1413 return {createWifiStatusFromLegacyError(legacy_status), 0};
1414 }
1415 std::tie(legacy_status, legacy_logger_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001416 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001417 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1418 // some devices don't support querying logger feature set
1419 legacy_logger_feature_set = 0;
1420 }
1421 uint32_t hidl_caps;
1422 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001423 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001424 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1425 }
1426 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1427}
1428
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001429std::pair<WifiStatus, sp<V1_4::IWifiRttController>> WifiChip::createRttControllerInternal_1_4(
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001430 const sp<IWifiIface>& /*bound_iface*/) {
1431 LOG(ERROR) << "createRttController_1_4 is not supported on this HAL";
1432 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001433}
1434
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001435WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001436 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001437 if (!event_cb_handler_.addCallback(event_callback)) {
1438 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1439 }
1440 return createWifiStatus(WifiStatusCode::SUCCESS);
1441}
1442
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001443WifiStatus WifiChip::setMultiStaPrimaryConnectionInternal(const std::string& ifname) {
1444 auto legacy_status = legacy_hal_.lock()->multiStaSetPrimaryConnection(ifname);
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001445 return createWifiStatusFromLegacyError(legacy_status);
1446}
1447
1448WifiStatus WifiChip::setMultiStaUseCaseInternal(MultiStaUseCase use_case) {
1449 auto legacy_status = legacy_hal_.lock()->multiStaSetUseCase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001450 hidl_struct_util::convertHidlMultiStaUseCaseToLegacy(use_case));
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001451 return createWifiStatusFromLegacyError(legacy_status);
1452}
1453
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001454WifiStatus WifiChip::setCoexUnsafeChannelsInternal(std::vector<CoexUnsafeChannel> unsafe_channels,
1455 uint32_t restrictions) {
Quang Luong94bcce52020-11-25 17:52:19 -08001456 std::vector<legacy_hal::wifi_coex_unsafe_channel> legacy_unsafe_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001457 if (!hidl_struct_util::convertHidlVectorOfCoexUnsafeChannelToLegacy(unsafe_channels,
1458 &legacy_unsafe_channels)) {
Quang Luong94bcce52020-11-25 17:52:19 -08001459 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1460 }
Quang Luongab70a832020-12-14 13:01:32 -08001461 uint32_t legacy_restrictions = 0;
1462 if (restrictions & CoexRestriction::WIFI_DIRECT) {
1463 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_DIRECT;
1464 }
1465 if (restrictions & CoexRestriction::SOFTAP) {
1466 legacy_restrictions |= legacy_hal::wifi_coex_restriction::SOFTAP;
1467 }
1468 if (restrictions & CoexRestriction::WIFI_AWARE) {
1469 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_AWARE;
1470 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001471 auto legacy_status =
1472 legacy_hal_.lock()->setCoexUnsafeChannels(legacy_unsafe_channels, legacy_restrictions);
Quang Luong94bcce52020-11-25 17:52:19 -08001473 return createWifiStatusFromLegacyError(legacy_status);
1474}
1475
Kumar Anandda62c382020-11-18 17:17:47 -08001476WifiStatus WifiChip::setCountryCodeInternal(const std::array<int8_t, 2>& code) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001477 auto legacy_status = legacy_hal_.lock()->setCountryCode(getFirstActiveWlanIfaceName(), code);
Kumar Anandda62c382020-11-18 17:17:47 -08001478 return createWifiStatusFromLegacyError(legacy_status);
1479}
1480
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001481std::pair<WifiStatus, std::vector<V1_5::WifiUsableChannel>> WifiChip::getUsableChannelsInternal(
1482 WifiBand /*band*/, uint32_t /*ifaceModeMask*/, uint32_t /*filterMask*/) {
1483 LOG(ERROR) << "getUsableChannels is not supported on this HAL";
1484 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
1485}
1486
1487WifiStatus WifiChip::triggerSubsystemRestartInternal() {
1488 auto legacy_status = legacy_hal_.lock()->triggerSubsystemRestart();
1489 return createWifiStatusFromLegacyError(legacy_status);
1490}
1491
1492std::pair<WifiStatus, sp<V1_6::IWifiRttController>> WifiChip::createRttControllerInternal_1_6(
1493 const sp<IWifiIface>& bound_iface) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001494 if (sta_ifaces_.size() == 0 &&
1495 !canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) {
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001496 LOG(ERROR) << "createRttControllerInternal_1_6: Chip cannot support STAs "
1497 "(and RTT by extension)";
1498 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1499 }
1500 sp<WifiRttController> rtt =
1501 new WifiRttController(getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1502 rtt_controllers_.emplace_back(rtt);
1503 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1504}
1505
1506std::pair<WifiStatus, std::vector<V1_6::WifiUsableChannel>> WifiChip::getUsableChannelsInternal_1_6(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001507 WifiBand band, uint32_t ifaceModeMask, uint32_t filterMask) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001508 legacy_hal::wifi_error legacy_status;
1509 std::vector<legacy_hal::wifi_usable_channel> legacy_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001510 std::tie(legacy_status, legacy_usable_channels) = legacy_hal_.lock()->getUsableChannels(
Kumar Anand2a630a32021-01-21 14:09:14 -08001511 hidl_struct_util::convertHidlWifiBandToLegacyMacBand(band),
Kumar Anandaea86e02021-02-10 16:22:31 -08001512 hidl_struct_util::convertHidlWifiIfaceModeToLegacy(ifaceModeMask),
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001513 hidl_struct_util::convertHidlUsableChannelFilterToLegacy(filterMask));
Kumar Anandaea86e02021-02-10 16:22:31 -08001514
Kumar Anand2a630a32021-01-21 14:09:14 -08001515 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1516 return {createWifiStatusFromLegacyError(legacy_status), {}};
1517 }
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001518 std::vector<V1_6::WifiUsableChannel> hidl_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001519 if (!hidl_struct_util::convertLegacyWifiUsableChannelsToHidl(legacy_usable_channels,
1520 &hidl_usable_channels)) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001521 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1522 }
1523 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_usable_channels};
1524}
1525
Sunil Ravief97d232022-01-24 10:39:56 -08001526std::pair<WifiStatus, V1_6::WifiRadioCombinationMatrix>
1527WifiChip::getSupportedRadioCombinationsMatrixInternal() {
1528 legacy_hal::wifi_error legacy_status;
1529 legacy_hal::wifi_radio_combination_matrix* legacy_matrix;
1530
1531 std::tie(legacy_status, legacy_matrix) =
1532 legacy_hal_.lock()->getSupportedRadioCombinationsMatrix();
1533 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1534 LOG(ERROR) << "Failed to get SupportedRadioCombinations matrix from legacy HAL: "
1535 << legacyErrorToString(legacy_status);
1536 return {createWifiStatusFromLegacyError(legacy_status), {}};
1537 }
1538
1539 V1_6::WifiRadioCombinationMatrix hidl_matrix;
1540 if (!hidl_struct_util::convertLegacyRadioCombinationsMatrixToHidl(legacy_matrix,
1541 &hidl_matrix)) {
1542 LOG(ERROR) << "Failed convertLegacyRadioCombinationsMatrixToHidl() ";
1543 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), {}};
1544 }
1545 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_matrix};
1546}
1547
Quang Luong5d8805e2022-01-28 15:46:40 -08001548std::pair<WifiStatus, std::vector<V1_6::IWifiChip::ChipMode>>
1549WifiChip::getAvailableModesInternal_1_6() {
1550 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
1551}
1552
Roshan Piusba38d9c2017-12-08 07:32:08 -08001553WifiStatus WifiChip::handleChipConfiguration(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001554 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001555 // If the chip is already configured in a different mode, stop
1556 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001557 if (isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001558 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_ << " to mode " << mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -08001559 invalidateAndRemoveAllIfaces();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001560 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stop(lock, []() {});
Roshan Piusba38d9c2017-12-08 07:32:08 -08001561 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001562 LOG(ERROR) << "Failed to stop legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusba38d9c2017-12-08 07:32:08 -08001563 return createWifiStatusFromLegacyError(legacy_status);
1564 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001565 }
Roshan Piuscc338202017-11-02 13:54:09 -07001566 // Firmware mode change not needed for V2 devices.
1567 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001568 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001569 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001570 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001571 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1572 }
1573 if (!success) {
1574 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1575 }
1576 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1577 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001578 LOG(ERROR) << "Failed to start legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001579 return createWifiStatusFromLegacyError(legacy_status);
1580 }
Roshan Pius85c64412018-01-22 17:58:40 -08001581 // Every time the HAL is restarted, we need to register the
1582 // radio mode change callback.
1583 WifiStatus status = registerRadioModeChangeCallback();
1584 if (status.code != WifiStatusCode::SUCCESS) {
1585 // This probably is not a critical failure?
1586 LOG(ERROR) << "Failed to register radio mode change callback";
1587 }
chenpaulf5eca292019-03-14 11:08:03 +08001588 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001589 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001590 version_info = WifiChip::requestChipDebugInfoInternal();
1591 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1592 property_set("vendor.wlan.firmware.version",
1593 version_info.second.firmwareDescription.c_str());
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001594 property_set("vendor.wlan.driver.version", version_info.second.driverDescription.c_str());
chenpaulf5eca292019-03-14 11:08:03 +08001595 }
1596
Roshan Piusabcf78f2017-10-06 16:30:38 -07001597 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001598}
Roshan Pius48185b22016-12-15 19:10:30 -08001599
1600WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001601 if (debug_ring_buffer_cb_registered_) {
1602 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001603 }
Roshan Pius3797e182017-03-30 18:01:54 -07001604
Roshan Piusabcf78f2017-10-06 16:30:38 -07001605 android::wp<WifiChip> weak_ptr_this(this);
1606 const auto& on_ring_buffer_data_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001607 [weak_ptr_this](const std::string& name, const std::vector<uint8_t>& data,
1608 const legacy_hal::wifi_ring_buffer_status& status) {
1609 const auto shared_ptr_this = weak_ptr_this.promote();
1610 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1611 LOG(ERROR) << "Callback invoked on an invalid object";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301612 return;
1613 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001614 WifiDebugRingBufferStatus hidl_status;
1615 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(status,
1616 &hidl_status)) {
1617 LOG(ERROR) << "Error converting ring buffer status";
1618 return;
1619 }
1620 {
1621 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1622 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1623 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1624 Ringbuffer& cur_buffer = target->second;
1625 cur_buffer.append(data);
1626 } else {
1627 LOG(ERROR) << "Ringname " << name << " not found";
1628 return;
1629 }
1630 // unique_lock unlocked here
1631 }
1632 };
1633 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001634 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001635
Roshan Piusabcf78f2017-10-06 16:30:38 -07001636 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1637 debug_ring_buffer_cb_registered_ = true;
1638 }
1639 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001640}
1641
Roshan Pius85c64412018-01-22 17:58:40 -08001642WifiStatus WifiChip::registerRadioModeChangeCallback() {
1643 android::wp<WifiChip> weak_ptr_this(this);
1644 const auto& on_radio_mode_change_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001645 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1646 const auto shared_ptr_this = weak_ptr_this.promote();
1647 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1648 LOG(ERROR) << "Callback invoked on an invalid object";
1649 return;
Roshan Pius85c64412018-01-22 17:58:40 -08001650 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001651 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo> hidl_radio_mode_infos;
1652 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(mac_infos,
1653 &hidl_radio_mode_infos)) {
1654 LOG(ERROR) << "Error converting wifi mac info";
1655 return;
1656 }
1657 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1658 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos).isOk()) {
1659 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
1660 << " callback on: " << toString(callback);
1661 }
1662 }
1663 };
Roshan Pius85c64412018-01-22 17:58:40 -08001664 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001665 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1666 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001667 return createWifiStatusFromLegacyError(legacy_status);
1668}
1669
Quang Luong5d8805e2022-01-28 15:46:40 -08001670std::vector<V1_6::IWifiChip::ChipConcurrencyCombination>
1671WifiChip::getCurrentModeConcurrencyCombinations() {
Roshan Piuscc338202017-11-02 13:54:09 -07001672 if (!isValidModeId(current_mode_id_)) {
1673 LOG(ERROR) << "Chip not configured in a mode yet";
1674 return {};
1675 }
1676 for (const auto& mode : modes_) {
1677 if (mode.id == current_mode_id_) {
1678 return mode.availableCombinations;
1679 }
1680 }
Quang Luong5d8805e2022-01-28 15:46:40 -08001681 CHECK(0) << "Expected to find concurrency combinations for current mode!";
Roshan Piuscc338202017-11-02 13:54:09 -07001682 return {};
1683}
1684
Quang Luong5d8805e2022-01-28 15:46:40 -08001685// Returns a map indexed by IfaceConcurrencyType with the number of ifaces currently
1686// created of the corresponding concurrency type.
1687std::map<IfaceConcurrencyType, size_t> WifiChip::getCurrentConcurrencyCombination() {
1688 std::map<IfaceConcurrencyType, size_t> iface_counts;
1689 uint32_t num_ap = 0;
1690 uint32_t num_ap_bridged = 0;
1691 for (const auto& ap_iface : ap_ifaces_) {
1692 std::string ap_iface_name = ap_iface->getName();
1693 if (br_ifaces_ap_instances_.count(ap_iface_name) > 0 &&
1694 br_ifaces_ap_instances_[ap_iface_name].size() > 1) {
1695 num_ap_bridged++;
1696 } else {
1697 num_ap++;
1698 }
1699 }
1700 iface_counts[IfaceConcurrencyType::AP] = num_ap;
1701 iface_counts[IfaceConcurrencyType::AP_BRIDGED] = num_ap_bridged;
1702 iface_counts[IfaceConcurrencyType::NAN] = nan_ifaces_.size();
1703 iface_counts[IfaceConcurrencyType::P2P] = p2p_ifaces_.size();
1704 iface_counts[IfaceConcurrencyType::STA] = sta_ifaces_.size();
Roshan Piuscc338202017-11-02 13:54:09 -07001705 return iface_counts;
1706}
1707
Quang Luong5d8805e2022-01-28 15:46:40 -08001708// This expands the provided concurrency combinations to a more parseable
Roshan Piuscc338202017-11-02 13:54:09 -07001709// form. Returns a vector of available combinations possible with the number
Quang Luong5d8805e2022-01-28 15:46:40 -08001710// of each concurrency type in the combination.
1711// This method is a port of HalDeviceManager.expandConcurrencyCombos() from framework.
1712std::vector<std::map<IfaceConcurrencyType, size_t>> WifiChip::expandConcurrencyCombinations(
1713 const V1_6::IWifiChip::ChipConcurrencyCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001714 uint32_t num_expanded_combos = 1;
1715 for (const auto& limit : combination.limits) {
1716 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1717 num_expanded_combos *= limit.types.size();
1718 }
1719 }
1720
Quang Luong5d8805e2022-01-28 15:46:40 -08001721 // Allocate the vector of expanded combos and reset all concurrency type counts to 0
Roshan Piuscc338202017-11-02 13:54:09 -07001722 // in each combo.
Quang Luong5d8805e2022-01-28 15:46:40 -08001723 std::vector<std::map<IfaceConcurrencyType, size_t>> expanded_combos;
Roshan Piuscc338202017-11-02 13:54:09 -07001724 expanded_combos.resize(num_expanded_combos);
1725 for (auto& expanded_combo : expanded_combos) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001726 for (const auto type :
1727 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, IfaceConcurrencyType::NAN,
1728 IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001729 expanded_combo[type] = 0;
1730 }
1731 }
1732 uint32_t span = num_expanded_combos;
1733 for (const auto& limit : combination.limits) {
1734 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1735 span /= limit.types.size();
1736 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001737 const auto iface_type = limit.types[(k / span) % limit.types.size()];
Roshan Piuscc338202017-11-02 13:54:09 -07001738 expanded_combos[k][iface_type]++;
1739 }
1740 }
1741 }
1742 return expanded_combos;
1743}
1744
Quang Luong5d8805e2022-01-28 15:46:40 -08001745bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(
1746 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1747 IfaceConcurrencyType requested_type) {
1748 const auto current_combo = getCurrentConcurrencyCombination();
Roshan Piuscc338202017-11-02 13:54:09 -07001749
1750 // Check if we have space for 1 more iface of |type| in this combo
Quang Luong5d8805e2022-01-28 15:46:40 -08001751 for (const auto type :
1752 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, IfaceConcurrencyType::NAN,
1753 IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001754 size_t num_ifaces_needed = current_combo.at(type);
1755 if (type == requested_type) {
1756 num_ifaces_needed++;
1757 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001758 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001759 if (num_ifaces_needed > num_ifaces_allowed) {
1760 return false;
1761 }
1762 }
1763 return true;
1764}
1765
1766// This method does the following:
Quang Luong5d8805e2022-01-28 15:46:40 -08001767// a) Enumerate all possible concurrency combos by expanding the current
1768// ChipConcurrencyCombination.
1769// b) Check if the requested concurrency type can be added to the current mode
1770// with the concurrency combination that is already active.
1771bool WifiChip::canCurrentModeSupportConcurrencyTypeWithCurrentTypes(
1772 IfaceConcurrencyType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001773 if (!isValidModeId(current_mode_id_)) {
1774 LOG(ERROR) << "Chip not configured in a mode yet";
1775 return false;
1776 }
Quang Luong5d8805e2022-01-28 15:46:40 -08001777 const auto combinations = getCurrentModeConcurrencyCombinations();
Roshan Piuscc338202017-11-02 13:54:09 -07001778 for (const auto& combination : combinations) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001779 const auto expanded_combos = expandConcurrencyCombinations(combination);
Roshan Piuscc338202017-11-02 13:54:09 -07001780 for (const auto& expanded_combo : expanded_combos) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001781 if (canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(expanded_combo,
1782 requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001783 return true;
1784 }
1785 }
1786 }
1787 return false;
1788}
1789
Quang Luong5d8805e2022-01-28 15:46:40 -08001790// Note: This does not consider concurrency types already active. It only checks if the
1791// provided expanded concurrency combination can support the requested combo.
1792bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyCombo(
1793 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
1794 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
1795 // Check if we have space for 1 more |type| in this combo
1796 for (const auto type :
1797 {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, IfaceConcurrencyType::NAN,
1798 IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001799 if (req_combo.count(type) == 0) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001800 // Concurrency type not in the req_combo.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001801 continue;
1802 }
1803 size_t num_ifaces_needed = req_combo.at(type);
1804 size_t num_ifaces_allowed = expanded_combo.at(type);
1805 if (num_ifaces_needed > num_ifaces_allowed) {
1806 return false;
1807 }
1808 }
1809 return true;
1810}
1811// This method does the following:
Quang Luong5d8805e2022-01-28 15:46:40 -08001812// a) Enumerate all possible concurrency combos by expanding the current
1813// ChipConcurrencyCombination.
1814// b) Check if the requested concurrency combo can be added to the current mode.
1815// Note: This does not consider concurrency types already active. It only checks if the
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001816// current mode can support the requested combo.
Quang Luong5d8805e2022-01-28 15:46:40 -08001817bool WifiChip::canCurrentModeSupportConcurrencyCombo(
1818 const std::map<IfaceConcurrencyType, size_t>& req_combo) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001819 if (!isValidModeId(current_mode_id_)) {
1820 LOG(ERROR) << "Chip not configured in a mode yet";
1821 return false;
1822 }
Quang Luong5d8805e2022-01-28 15:46:40 -08001823 const auto combinations = getCurrentModeConcurrencyCombinations();
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001824 for (const auto& combination : combinations) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001825 const auto expanded_combos = expandConcurrencyCombinations(combination);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001826 for (const auto& expanded_combo : expanded_combos) {
Quang Luong5d8805e2022-01-28 15:46:40 -08001827 if (canExpandedConcurrencyComboSupportConcurrencyCombo(expanded_combo, req_combo)) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001828 return true;
1829 }
1830 }
1831 }
1832 return false;
1833}
1834
1835// This method does the following:
Quang Luong5d8805e2022-01-28 15:46:40 -08001836// a) Enumerate all possible concurrency combos by expanding the current
1837// ChipConcurrencyCombination.
1838// b) Check if the requested concurrency type can be added to the current mode.
1839bool WifiChip::canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type) {
1840 // Check if we can support at least 1 of the requested concurrency type.
1841 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001842 req_iface_combo[requested_type] = 1;
Quang Luong5d8805e2022-01-28 15:46:40 -08001843 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001844}
1845
Roshan Piuscc338202017-11-02 13:54:09 -07001846bool WifiChip::isValidModeId(ChipModeId mode_id) {
1847 for (const auto& mode : modes_) {
1848 if (mode.id == mode_id) {
1849 return true;
1850 }
1851 }
1852 return false;
1853}
1854
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001855bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
lesl261818b2020-11-27 12:37:35 +08001856 // Check if we can support at least 1 STA & 1 AP concurrently.
Quang Luong5d8805e2022-01-28 15:46:40 -08001857 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1858 req_iface_combo[IfaceConcurrencyType::STA] = 1;
1859 req_iface_combo[IfaceConcurrencyType::AP] = 1;
1860 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001861}
1862
lesl261818b2020-11-27 12:37:35 +08001863bool WifiChip::isDualStaConcurrencyAllowedInCurrentMode() {
1864 // Check if we can support at least 2 STA concurrently.
Quang Luong5d8805e2022-01-28 15:46:40 -08001865 std::map<IfaceConcurrencyType, size_t> req_iface_combo;
1866 req_iface_combo[IfaceConcurrencyType::STA] = 2;
1867 return canCurrentModeSupportConcurrencyCombo(req_iface_combo);
James Mattisd2e4c072019-05-22 16:14:48 -07001868}
1869
Roshan Pius6036c022019-03-27 10:41:58 -07001870std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001871 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
leslf012b652021-01-08 15:22:49 +08001872 if (ap_ifaces_.size() > 0) {
1873 // If the first active wlan iface is bridged iface.
1874 // Return first instance name.
1875 for (auto const& it : br_ifaces_ap_instances_) {
1876 if (it.first == ap_ifaces_[0]->getName()) {
1877 return it.second[0];
1878 }
1879 }
1880 return ap_ifaces_[0]->getName();
1881 }
Roshan Pius6036c022019-03-27 10:41:58 -07001882 // This could happen if the chip call is made before any STA/AP
1883 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001884 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Jimmy Chen2dddd792019-12-23 17:50:39 +02001885 return getWlanIfaceNameWithType(IfaceType::STA, 0);
Roshan Pius6036c022019-03-27 10:41:58 -07001886}
1887
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001888// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1889// not already in use.
1890// Note: This doesn't check the actual presence of these interfaces.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001891std::string WifiChip::allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001892 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001893 const auto ifname = getWlanIfaceNameWithType(type, idx);
lesl94d28242020-11-18 22:17:37 +08001894 if (findUsingNameFromBridgedApInstances(ifname)) continue;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001895 if (findUsingName(ap_ifaces_, ifname)) continue;
1896 if (findUsingName(sta_ifaces_, ifname)) continue;
1897 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001898 }
1899 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001900 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001901 return {};
1902}
1903
lesl261818b2020-11-27 12:37:35 +08001904uint32_t WifiChip::startIdxOfApIface() {
1905 if (isDualStaConcurrencyAllowedInCurrentMode()) {
1906 // When the HAL support dual STAs, AP should start with idx 2.
1907 return 2;
1908 } else if (isStaApConcurrencyAllowedInCurrentMode()) {
1909 // When the HAL support STA + AP but it doesn't support dual STAs.
1910 // AP should start with idx 1.
1911 return 1;
1912 }
1913 // No concurrency support.
1914 return 0;
1915}
1916
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001917// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001918// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001919std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001920 // Check if we have a dedicated iface for AP.
lesl261818b2020-11-27 12:37:35 +08001921 std::vector<std::string> ifnames = getPredefinedApIfaceNames(false);
1922 if (!ifnames.empty()) {
1923 return ifnames[0];
Roshan Pius78cb5992020-04-30 12:39:21 -07001924 }
lesl261818b2020-11-27 12:37:35 +08001925 return allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface());
1926}
1927
1928std::vector<std::string> WifiChip::allocateBridgedApInstanceNames() {
1929 // Check if we have a dedicated iface for AP.
1930 std::vector<std::string> instances = getPredefinedApIfaceNames(true);
1931 if (instances.size() == 2) {
1932 return instances;
1933 } else {
1934 int num_ifaces_need_to_allocate = 2 - instances.size();
1935 for (int i = 0; i < num_ifaces_need_to_allocate; i++) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001936 std::string instance_name =
1937 allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface() + i);
lesl261818b2020-11-27 12:37:35 +08001938 if (!instance_name.empty()) {
1939 instances.push_back(instance_name);
1940 }
1941 }
1942 }
1943 return instances;
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001944}
1945
1946// STA iface names start with idx 0.
1947// Primary STA iface will always be 0.
1948std::string WifiChip::allocateStaIfaceName() {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001949 return allocateApOrStaIfaceName(IfaceType::STA, 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001950}
1951
xshu5899e8e2018-01-09 15:36:03 -08001952bool WifiChip::writeRingbufferFilesInternal() {
1953 if (!removeOldFilesInternal()) {
1954 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1955 return false;
1956 }
1957 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301958 {
1959 std::unique_lock<std::mutex> lk(lock_t);
xshuc905ea62021-07-11 19:57:02 -07001960 for (auto& item : ringbuffer_map_) {
1961 Ringbuffer& cur_buffer = item.second;
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301962 if (cur_buffer.getData().empty()) {
1963 continue;
1964 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001965 const std::string file_path_raw = kTombstoneFolderPath + item.first + "XXXXXXXXXX";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301966 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1967 if (dump_fd == -1) {
1968 PLOG(ERROR) << "create file failed";
1969 return false;
1970 }
1971 unique_fd file_auto_closer(dump_fd);
1972 for (const auto& cur_block : cur_buffer.getData()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001973 if (write(dump_fd, cur_block.data(), sizeof(cur_block[0]) * cur_block.size()) ==
1974 -1) {
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301975 PLOG(ERROR) << "Error writing to file";
1976 }
xshu5899e8e2018-01-09 15:36:03 -08001977 }
xshuc905ea62021-07-11 19:57:02 -07001978 cur_buffer.clear();
xshu5899e8e2018-01-09 15:36:03 -08001979 }
xshu0a0fe512020-07-22 17:53:37 -07001980 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001981 }
1982 return true;
1983}
1984
Jimmy Chen2dddd792019-12-23 17:50:39 +02001985std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1986 std::string ifname;
1987
1988 // let the legacy hal override the interface name
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001989 legacy_hal::wifi_error err = legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
Jimmy Chen2dddd792019-12-23 17:50:39 +02001990 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1991
1992 return getWlanIfaceName(idx);
1993}
1994
lesl94d28242020-11-18 22:17:37 +08001995void WifiChip::invalidateAndClearBridgedApAll() {
1996 for (auto const& it : br_ifaces_ap_instances_) {
1997 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001998 iface_util_->removeIfaceFromBridge(it.first, iface);
lesl94d28242020-11-18 22:17:37 +08001999 legacy_hal_.lock()->deleteVirtualInterface(iface);
2000 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08002001 iface_util_->deleteBridge(it.first);
lesl94d28242020-11-18 22:17:37 +08002002 }
2003 br_ifaces_ap_instances_.clear();
2004}
2005
2006void WifiChip::invalidateAndClearBridgedAp(const std::string& br_name) {
2007 if (br_name.empty()) return;
2008 // delete managed interfaces
2009 for (auto const& it : br_ifaces_ap_instances_) {
2010 if (it.first == br_name) {
2011 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08002012 iface_util_->removeIfaceFromBridge(br_name, iface);
lesl94d28242020-11-18 22:17:37 +08002013 legacy_hal_.lock()->deleteVirtualInterface(iface);
2014 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08002015 iface_util_->deleteBridge(br_name);
lesl94d28242020-11-18 22:17:37 +08002016 br_ifaces_ap_instances_.erase(br_name);
2017 break;
2018 }
2019 }
2020 return;
2021}
2022
2023bool WifiChip::findUsingNameFromBridgedApInstances(const std::string& name) {
2024 for (auto const& it : br_ifaces_ap_instances_) {
2025 if (it.first == name) {
2026 return true;
2027 }
2028 for (auto const& iface : it.second) {
2029 if (iface == name) {
2030 return true;
2031 }
2032 }
2033 }
2034 return false;
2035}
2036
Roshan Pius79a99752016-10-04 13:03:58 -07002037} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08002038} // namespace V1_6
Roshan Pius3c4e8a32016-10-03 14:53:58 -07002039} // namespace wifi
2040} // namespace hardware
2041} // namespace android