blob: a1857246a7023ec0c42d86663cf33f2b5f9cced4 [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
Roshan Pius35d958c2016-10-06 16:47:38 -0700725void WifiChip::invalidateAndRemoveAllIfaces() {
lesl94d28242020-11-18 22:17:37 +0800726 invalidateAndClearBridgedApAll();
Roshan Pius675609b2017-10-31 14:24:58 -0700727 invalidateAndClearAll(ap_ifaces_);
728 invalidateAndClearAll(nan_ifaces_);
729 invalidateAndClearAll(p2p_ifaces_);
730 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700731 // Since all the ifaces are invalid now, all RTT controller objects
732 // using those ifaces also need to be invalidated.
733 for (const auto& rtt : rtt_controllers_) {
734 rtt->invalidate();
735 }
736 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700737}
738
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800739void WifiChip::invalidateAndRemoveDependencies(const std::string& removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200740 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
741 auto nan_iface = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700742 if (nan_iface->getName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200743 nan_iface->invalidate();
Roshan Pius82368502019-05-16 12:53:02 -0700744 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800745 if (!callback->onIfaceRemoved(IfaceType::NAN, removed_iface_name).isOk()) {
Roshan Pius82368502019-05-16 12:53:02 -0700746 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
747 }
748 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200749 it = nan_ifaces_.erase(it);
750 } else {
751 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700752 }
753 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200754
755 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
756 auto rtt = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700757 if (rtt->getIfaceName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200758 rtt->invalidate();
759 it = rtt_controllers_.erase(it);
760 } else {
761 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700762 }
763 }
764}
765
Roshan Pius3c868522016-10-27 12:43:49 -0700766std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700767 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700768}
769
770WifiStatus WifiChip::registerEventCallbackInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800771 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800772 // Deprecated support for this callback.
773 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700774}
775
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700776std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700777 // Deprecated support for this callback.
778 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
779}
780
Jimmy Chend460df32019-11-29 17:31:22 +0200781std::pair<WifiStatus, std::vector<V1_4::IWifiChip::ChipMode>>
Roshan Pius3c868522016-10-27 12:43:49 -0700782WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700783 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700784}
785
Roshan Piusba38d9c2017-12-08 07:32:08 -0800786WifiStatus WifiChip::configureChipInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800787 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700788 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700789 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
790 }
791 if (mode_id == current_mode_id_) {
792 LOG(DEBUG) << "Already in the specified mode " << mode_id;
793 return createWifiStatus(WifiStatusCode::SUCCESS);
794 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800795 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700796 if (status.code != WifiStatusCode::SUCCESS) {
797 for (const auto& callback : event_cb_handler_.getCallbacks()) {
798 if (!callback->onChipReconfigureFailure(status).isOk()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800799 LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback";
Roshan Piusabcf78f2017-10-06 16:30:38 -0700800 }
801 }
802 return status;
803 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800804 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700805 if (!callback->onChipReconfigured(mode_id).isOk()) {
806 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
807 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800808 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700809 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800810 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700811 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700812
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800813 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(subsystemCallbackHandler_);
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700814
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800815 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700816}
817
818std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700819 if (!isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800820 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), current_mode_id_};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700821 }
822 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700823}
824
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800825std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200826 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700827 legacy_hal::wifi_error legacy_status;
828 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700829 const auto ifname = getFirstActiveWlanIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800830 std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700831 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800832 LOG(ERROR) << "Failed to get driver version: " << legacyErrorToString(legacy_status);
833 WifiStatus status =
834 createWifiStatusFromLegacyError(legacy_status, "failed to get driver version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700835 return {status, result};
836 }
837 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700838
Roshan Piusabcf78f2017-10-06 16:30:38 -0700839 std::string firmware_desc;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800840 std::tie(legacy_status, firmware_desc) = legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700841 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800842 LOG(ERROR) << "Failed to get firmware version: " << legacyErrorToString(legacy_status);
843 WifiStatus status =
844 createWifiStatusFromLegacyError(legacy_status, "failed to get firmware version");
Roshan Piusabcf78f2017-10-06 16:30:38 -0700845 return {status, result};
846 }
847 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700848
Roshan Piusabcf78f2017-10-06 16:30:38 -0700849 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700850}
851
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800852std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700853 legacy_hal::wifi_error legacy_status;
854 std::vector<uint8_t> driver_dump;
855 std::tie(legacy_status, driver_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800856 legacy_hal_.lock()->requestDriverMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700857 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800858 LOG(ERROR) << "Failed to get driver debug dump: " << legacyErrorToString(legacy_status);
859 return {createWifiStatusFromLegacyError(legacy_status), std::vector<uint8_t>()};
Roshan Piusabcf78f2017-10-06 16:30:38 -0700860 }
861 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700862}
863
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800864std::pair<WifiStatus, std::vector<uint8_t>> WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700865 legacy_hal::wifi_error legacy_status;
866 std::vector<uint8_t> firmware_dump;
867 std::tie(legacy_status, firmware_dump) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800868 legacy_hal_.lock()->requestFirmwareMemoryDump(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700869 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800870 LOG(ERROR) << "Failed to get firmware debug dump: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700871 return {createWifiStatusFromLegacyError(legacy_status), {}};
872 }
873 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700874}
875
lesl94d28242020-11-18 22:17:37 +0800876WifiStatus WifiChip::createVirtualApInterface(const std::string& apVirtIf) {
877 legacy_hal::wifi_error legacy_status;
878 legacy_status = legacy_hal_.lock()->createVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800879 apVirtIf, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
Sunil Raviddab4bb2020-02-03 22:45:19 -0800880 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
lesl94d28242020-11-18 22:17:37 +0800881 LOG(ERROR) << "Failed to add interface: " << apVirtIf << " "
Sunil Raviddab4bb2020-02-03 22:45:19 -0800882 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +0800883 return createWifiStatusFromLegacyError(legacy_status);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800884 }
lesl94d28242020-11-18 22:17:37 +0800885 return createWifiStatus(WifiStatusCode::SUCCESS);
886}
887
888sp<WifiApIface> WifiChip::newWifiApIface(std::string& ifname) {
lesl420c4fc2020-11-23 19:33:04 +0800889 std::vector<std::string> ap_instances;
890 for (auto const& it : br_ifaces_ap_instances_) {
891 if (it.first == ifname) {
892 ap_instances = it.second;
893 }
894 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800895 sp<WifiApIface> iface = new WifiApIface(ifname, ap_instances, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700896 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700897 for (const auto& callback : event_cb_handler_.getCallbacks()) {
898 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
899 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
900 }
901 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700902 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
lesl94d28242020-11-18 22:17:37 +0800903 return iface;
904}
905
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800906std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createApIfaceInternal() {
lesl94d28242020-11-18 22:17:37 +0800907 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
908 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
909 }
910 std::string ifname = allocateApIfaceName();
911 WifiStatus status = createVirtualApInterface(ifname);
912 if (status.code != WifiStatusCode::SUCCESS) {
913 return {status, {}};
914 }
915 sp<WifiApIface> iface = newWifiApIface(ifname);
916 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
917}
918
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800919std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::createBridgedApIfaceInternal() {
lesl94d28242020-11-18 22:17:37 +0800920 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
921 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
922 }
lesl261818b2020-11-27 12:37:35 +0800923 std::vector<std::string> ap_instances = allocateBridgedApInstanceNames();
924 if (ap_instances.size() < 2) {
925 LOG(ERROR) << "Fail to allocate two instances";
926 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
927 }
928 std::string br_ifname = kApBridgeIfacePrefix + ap_instances[0];
lesl94d28242020-11-18 22:17:37 +0800929 for (int i = 0; i < 2; i++) {
lesl261818b2020-11-27 12:37:35 +0800930 WifiStatus status = createVirtualApInterface(ap_instances[i]);
lesl94d28242020-11-18 22:17:37 +0800931 if (status.code != WifiStatusCode::SUCCESS) {
lesl261818b2020-11-27 12:37:35 +0800932 if (i != 0) { // The failure happened when creating second virtual
933 // iface.
lesl94d28242020-11-18 22:17:37 +0800934 legacy_hal_.lock()->deleteVirtualInterface(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800935 ap_instances.front()); // Remove the first virtual iface.
lesl94d28242020-11-18 22:17:37 +0800936 }
937 return {status, {}};
938 }
lesl94d28242020-11-18 22:17:37 +0800939 }
940 br_ifaces_ap_instances_[br_ifname] = ap_instances;
Roshan Pius8c1a67b2021-03-02 10:00:23 -0800941 if (!iface_util_->createBridge(br_ifname)) {
lesl94d28242020-11-18 22:17:37 +0800942 LOG(ERROR) << "Failed createBridge - br_name=" << br_ifname.c_str();
943 invalidateAndClearBridgedAp(br_ifname);
944 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
945 }
946 for (auto const& instance : ap_instances) {
947 // Bind ap instance interface to AP bridge
Roshan Pius8c1a67b2021-03-02 10:00:23 -0800948 if (!iface_util_->addIfaceToBridge(br_ifname, instance)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800949 LOG(ERROR) << "Failed add if to Bridge - if_name=" << instance.c_str();
lesl94d28242020-11-18 22:17:37 +0800950 invalidateAndClearBridgedAp(br_ifname);
951 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
952 }
953 }
954 sp<WifiApIface> iface = newWifiApIface(br_ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700955 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700956}
957
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800958std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700959 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700960 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
961 }
Roshan Pius675609b2017-10-31 14:24:58 -0700962 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700963}
964
lesl420c4fc2020-11-23 19:33:04 +0800965std::pair<WifiStatus, sp<V1_5::IWifiApIface>> WifiChip::getApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800966 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700967 const auto iface = findUsingName(ap_ifaces_, ifname);
968 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700969 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
970 }
Roshan Pius675609b2017-10-31 14:24:58 -0700971 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700972}
973
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800974WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700975 const auto iface = findUsingName(ap_ifaces_, ifname);
976 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700977 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800978 }
Roshan Pius82368502019-05-16 12:53:02 -0700979 // Invalidate & remove any dependent objects first.
980 // Note: This is probably not required because we never create
981 // nan/rtt objects over AP iface. But, there is no harm to do it
982 // here and not make that assumption all over the place.
983 invalidateAndRemoveDependencies(ifname);
lesl94d28242020-11-18 22:17:37 +0800984 // Clear the bridge interface and the iface instance.
985 invalidateAndClearBridgedAp(ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700986 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700987 for (const auto& callback : event_cb_handler_.getCallbacks()) {
988 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
989 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
990 }
991 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700992 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700993 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800994}
995
lesl94d28242020-11-18 22:17:37 +0800996WifiStatus WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -0800997 const std::string& ifname, const std::string& ifInstanceName) {
lesl94d28242020-11-18 22:17:37 +0800998 const auto iface = findUsingName(ap_ifaces_, ifname);
lesl819e3722021-01-07 09:49:04 +0800999 if (!iface.get() || ifInstanceName.empty()) {
lesl94d28242020-11-18 22:17:37 +08001000 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1001 }
1002 // Requires to remove one of the instance in bridge mode
1003 for (auto const& it : br_ifaces_ap_instances_) {
1004 if (it.first == ifname) {
Les Lee03d642f2021-06-21 21:25:20 +08001005 std::vector<std::string> ap_instances = it.second;
1006 for (auto const& iface : ap_instances) {
lesl94d28242020-11-18 22:17:37 +08001007 if (iface == ifInstanceName) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001008 if (!iface_util_->removeIfaceFromBridge(it.first, iface)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001009 LOG(ERROR) << "Failed to remove interface: " << ifInstanceName << " from "
1010 << ifname;
1011 return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE);
lesl94d28242020-11-18 22:17:37 +08001012 }
George Burgess IV2c0a47d2021-01-20 21:14:13 -08001013 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001014 legacy_hal_.lock()->deleteVirtualInterface(iface);
lesl94d28242020-11-18 22:17:37 +08001015 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001016 LOG(ERROR) << "Failed to del interface: " << iface << " "
1017 << legacyErrorToString(legacy_status);
lesl94d28242020-11-18 22:17:37 +08001018 return createWifiStatusFromLegacyError(legacy_status);
1019 }
Les Lee03d642f2021-06-21 21:25:20 +08001020 ap_instances.erase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001021 std::remove(ap_instances.begin(), ap_instances.end(), ifInstanceName),
1022 ap_instances.end());
Les Lee03d642f2021-06-21 21:25:20 +08001023 br_ifaces_ap_instances_[ifname] = ap_instances;
1024 break;
lesl94d28242020-11-18 22:17:37 +08001025 }
1026 }
1027 break;
1028 }
1029 }
lesl669c9062021-01-22 19:37:47 +08001030 iface->removeInstance(ifInstanceName);
Les Lee03d642f2021-06-21 21:25:20 +08001031 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
1032
lesl94d28242020-11-18 22:17:37 +08001033 return createWifiStatus(WifiStatusCode::SUCCESS);
1034}
1035
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001036std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001037 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001038 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -08001039 }
Roshan Pius5ba0a902020-04-14 11:55:42 -07001040 bool is_dedicated_iface = true;
lesl261818b2020-11-27 12:37:35 +08001041 std::string ifname = getPredefinedNanIfaceName();
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001042 if (ifname.empty() || !iface_util_->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -07001043 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
1044 // not defined.
1045 ifname = getFirstActiveWlanIfaceName();
1046 is_dedicated_iface = false;
1047 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001048 sp<WifiNanIface> iface = new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -07001049 nan_ifaces_.push_back(iface);
1050 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1051 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
1052 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1053 }
1054 }
1055 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001056}
1057
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001058std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001059 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001060 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1061 }
Roshan Pius675609b2017-10-31 14:24:58 -07001062 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001063}
1064
Jimmy Chend460df32019-11-29 17:31:22 +02001065std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001066 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001067 const auto iface = findUsingName(nan_ifaces_, ifname);
1068 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001069 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1070 }
Roshan Pius675609b2017-10-31 14:24:58 -07001071 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001072}
1073
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001074WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001075 const auto iface = findUsingName(nan_ifaces_, ifname);
1076 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001077 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001078 }
Roshan Pius675609b2017-10-31 14:24:58 -07001079 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001080 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1081 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
1082 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1083 }
1084 }
1085 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001086}
1087
Roshan Pius3c868522016-10-27 12:43:49 -07001088std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001089 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001090 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001091 }
lesl261818b2020-11-27 12:37:35 +08001092 std::string ifname = getPredefinedP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -07001093 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
1094 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001095 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1096 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
1097 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1098 }
1099 }
Roshan Pius675609b2017-10-31 14:24:58 -07001100 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001101}
1102
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001103std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001104 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001105 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1106 }
Roshan Pius675609b2017-10-31 14:24:58 -07001107 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001108}
1109
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001110std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001111 const auto iface = findUsingName(p2p_ifaces_, ifname);
1112 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001113 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1114 }
Roshan Pius675609b2017-10-31 14:24:58 -07001115 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001116}
1117
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001118WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001119 const auto iface = findUsingName(p2p_ifaces_, ifname);
1120 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001121 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001122 }
Roshan Pius675609b2017-10-31 14:24:58 -07001123 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001124 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1125 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1126 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1127 }
1128 }
1129 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001130}
1131
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001132std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001133 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001134 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001135 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001136 std::string ifname = allocateStaIfaceName();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001137 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->createVirtualInterface(
1138 ifname, hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
Sunil Raviddab4bb2020-02-03 22:45:19 -08001139 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1140 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1141 << legacyErrorToString(legacy_status);
1142 return {createWifiStatusFromLegacyError(legacy_status), {}};
1143 }
Roshan Pius99dab382019-02-14 07:57:10 -08001144 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001145 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001146 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1147 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1148 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1149 }
1150 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001151 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001152 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001153}
1154
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001155std::pair<WifiStatus, std::vector<hidl_string>> WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001156 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001157 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1158 }
Roshan Pius675609b2017-10-31 14:24:58 -07001159 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001160}
1161
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001162std::pair<WifiStatus, sp<V1_6::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001163 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001164 const auto iface = findUsingName(sta_ifaces_, ifname);
1165 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001166 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1167 }
Roshan Pius675609b2017-10-31 14:24:58 -07001168 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001169}
1170
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001171WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001172 const auto iface = findUsingName(sta_ifaces_, ifname);
1173 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001174 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001175 }
Roshan Pius82368502019-05-16 12:53:02 -07001176 // Invalidate & remove any dependent objects first.
1177 invalidateAndRemoveDependencies(ifname);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001178 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->deleteVirtualInterface(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001179 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1180 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1181 << legacyErrorToString(legacy_status);
1182 }
Roshan Pius675609b2017-10-31 14:24:58 -07001183 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001184 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1185 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1186 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1187 }
1188 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001189 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001190 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001191}
1192
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001193std::pair<WifiStatus, sp<V1_0::IWifiRttController>> WifiChip::createRttControllerInternal(
1194 const sp<IWifiIface>& /*bound_iface*/) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001195 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001196 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001197}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001198
1199std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1200WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001201 legacy_hal::wifi_error legacy_status;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001202 std::vector<legacy_hal::wifi_ring_buffer_status> legacy_ring_buffer_status_vec;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001203 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001204 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001205 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1206 return {createWifiStatusFromLegacyError(legacy_status), {}};
1207 }
1208 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1209 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001210 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001211 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1212 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001213 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001214}
1215
1216WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001217 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1218 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001219 WifiStatus status = registerDebugRingBufferCallback();
1220 if (status.code != WifiStatusCode::SUCCESS) {
1221 return status;
1222 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001223 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001224 getFirstActiveWlanIfaceName(), ring_name,
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001225 static_cast<std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(verbose_level),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001226 max_interval_in_sec, min_data_size_in_bytes);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001227 ringbuffer_map_.insert(
1228 std::pair<std::string, Ringbuffer>(ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001229 // if verbose logging enabled, turn up HAL daemon logging as well.
1230 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1231 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1232 } else {
1233 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1234 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001235 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001236}
1237
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001238WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001239 WifiStatus status = registerDebugRingBufferCallback();
1240 if (status.code != WifiStatusCode::SUCCESS) {
1241 return status;
1242 }
1243 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001244 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001245
Roshan Piusabcf78f2017-10-06 16:30:38 -07001246 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001247}
1248
Roger Wangb294c762018-11-02 15:34:39 +08001249WifiStatus WifiChip::flushRingBufferToFileInternal() {
1250 if (!writeRingbufferFilesInternal()) {
1251 LOG(ERROR) << "Error writing files to flash";
1252 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1253 }
1254 return createWifiStatus(WifiStatusCode::SUCCESS);
1255}
1256
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001257WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001258 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001259 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001260 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1261 debug_ring_buffer_cb_registered_ = false;
1262 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001263 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001264}
1265
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001266std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1267WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001268 legacy_hal::wifi_error legacy_status;
1269 legacy_hal::WakeReasonStats legacy_stats;
1270 std::tie(legacy_status, legacy_stats) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001271 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001272 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1273 return {createWifiStatusFromLegacyError(legacy_status), {}};
1274 }
1275 WifiDebugHostWakeReasonStats hidl_stats;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001276 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats, &hidl_stats)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001277 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1278 }
1279 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001280}
1281
Roshan Pius203cb032016-12-14 17:41:20 -08001282WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001283 legacy_hal::wifi_error legacy_status;
1284 if (enable) {
1285 android::wp<WifiChip> weak_ptr_this(this);
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001286 const auto& on_alert_callback = [weak_ptr_this](int32_t error_code,
1287 std::vector<uint8_t> debug_data) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001288 const auto shared_ptr_this = weak_ptr_this.promote();
1289 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1290 LOG(ERROR) << "Callback invoked on an invalid object";
1291 return;
1292 }
1293 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001294 if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001295 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1296 }
1297 }
1298 };
1299 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001300 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001301 } else {
1302 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001303 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001304 }
1305 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001306}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001307
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001308WifiStatus WifiChip::selectTxPowerScenarioInternal(V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001309 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001310 getFirstActiveWlanIfaceName(),
1311 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
Roshan Piusabcf78f2017-10-06 16:30:38 -07001312 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001313}
1314
Roshan Pius735ff432017-07-25 08:48:08 -07001315WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001316 auto legacy_status = legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001317 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001318}
1319
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001320WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1321 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001322 getFirstActiveWlanIfaceName(), hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001323 return createWifiStatusFromLegacyError(legacy_status);
1324}
1325
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001326WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001327 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001328 // Deprecated support for this callback.
1329 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001330}
1331
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001332WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001333 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001334 getFirstActiveWlanIfaceName(),
1335 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001336 return createWifiStatusFromLegacyError(legacy_status);
1337}
1338
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001339std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001340 // Deprecated support for this callback.
1341 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
1342}
1343
1344std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_5() {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001345 legacy_hal::wifi_error legacy_status;
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001346 uint64_t legacy_feature_set;
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001347 uint32_t legacy_logger_feature_set;
1348 const auto ifname = getFirstActiveWlanIfaceName();
1349 std::tie(legacy_status, legacy_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001350 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001351 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1352 return {createWifiStatusFromLegacyError(legacy_status), 0};
1353 }
1354 std::tie(legacy_status, legacy_logger_feature_set) =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001355 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001356 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1357 // some devices don't support querying logger feature set
1358 legacy_logger_feature_set = 0;
1359 }
1360 uint32_t hidl_caps;
1361 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001362 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001363 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1364 }
1365 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1366}
1367
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001368std::pair<WifiStatus, sp<V1_4::IWifiRttController>> WifiChip::createRttControllerInternal_1_4(
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001369 const sp<IWifiIface>& /*bound_iface*/) {
1370 LOG(ERROR) << "createRttController_1_4 is not supported on this HAL";
1371 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001372}
1373
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001374WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001375 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001376 if (!event_cb_handler_.addCallback(event_callback)) {
1377 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1378 }
1379 return createWifiStatus(WifiStatusCode::SUCCESS);
1380}
1381
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001382WifiStatus WifiChip::setMultiStaPrimaryConnectionInternal(const std::string& ifname) {
1383 auto legacy_status = legacy_hal_.lock()->multiStaSetPrimaryConnection(ifname);
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001384 return createWifiStatusFromLegacyError(legacy_status);
1385}
1386
1387WifiStatus WifiChip::setMultiStaUseCaseInternal(MultiStaUseCase use_case) {
1388 auto legacy_status = legacy_hal_.lock()->multiStaSetUseCase(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001389 hidl_struct_util::convertHidlMultiStaUseCaseToLegacy(use_case));
Roshan Piuse9d1e7d2020-11-04 11:44:16 -08001390 return createWifiStatusFromLegacyError(legacy_status);
1391}
1392
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001393WifiStatus WifiChip::setCoexUnsafeChannelsInternal(std::vector<CoexUnsafeChannel> unsafe_channels,
1394 uint32_t restrictions) {
Quang Luong94bcce52020-11-25 17:52:19 -08001395 std::vector<legacy_hal::wifi_coex_unsafe_channel> legacy_unsafe_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001396 if (!hidl_struct_util::convertHidlVectorOfCoexUnsafeChannelToLegacy(unsafe_channels,
1397 &legacy_unsafe_channels)) {
Quang Luong94bcce52020-11-25 17:52:19 -08001398 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
1399 }
Quang Luongab70a832020-12-14 13:01:32 -08001400 uint32_t legacy_restrictions = 0;
1401 if (restrictions & CoexRestriction::WIFI_DIRECT) {
1402 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_DIRECT;
1403 }
1404 if (restrictions & CoexRestriction::SOFTAP) {
1405 legacy_restrictions |= legacy_hal::wifi_coex_restriction::SOFTAP;
1406 }
1407 if (restrictions & CoexRestriction::WIFI_AWARE) {
1408 legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_AWARE;
1409 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001410 auto legacy_status =
1411 legacy_hal_.lock()->setCoexUnsafeChannels(legacy_unsafe_channels, legacy_restrictions);
Quang Luong94bcce52020-11-25 17:52:19 -08001412 return createWifiStatusFromLegacyError(legacy_status);
1413}
1414
Kumar Anandda62c382020-11-18 17:17:47 -08001415WifiStatus WifiChip::setCountryCodeInternal(const std::array<int8_t, 2>& code) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001416 auto legacy_status = legacy_hal_.lock()->setCountryCode(getFirstActiveWlanIfaceName(), code);
Kumar Anandda62c382020-11-18 17:17:47 -08001417 return createWifiStatusFromLegacyError(legacy_status);
1418}
1419
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001420std::pair<WifiStatus, std::vector<V1_5::WifiUsableChannel>> WifiChip::getUsableChannelsInternal(
1421 WifiBand /*band*/, uint32_t /*ifaceModeMask*/, uint32_t /*filterMask*/) {
1422 LOG(ERROR) << "getUsableChannels is not supported on this HAL";
1423 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
1424}
1425
1426WifiStatus WifiChip::triggerSubsystemRestartInternal() {
1427 auto legacy_status = legacy_hal_.lock()->triggerSubsystemRestart();
1428 return createWifiStatusFromLegacyError(legacy_status);
1429}
1430
1431std::pair<WifiStatus, sp<V1_6::IWifiRttController>> WifiChip::createRttControllerInternal_1_6(
1432 const sp<IWifiIface>& bound_iface) {
1433 if (sta_ifaces_.size() == 0 && !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1434 LOG(ERROR) << "createRttControllerInternal_1_6: Chip cannot support STAs "
1435 "(and RTT by extension)";
1436 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1437 }
1438 sp<WifiRttController> rtt =
1439 new WifiRttController(getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1440 rtt_controllers_.emplace_back(rtt);
1441 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1442}
1443
1444std::pair<WifiStatus, std::vector<V1_6::WifiUsableChannel>> WifiChip::getUsableChannelsInternal_1_6(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001445 WifiBand band, uint32_t ifaceModeMask, uint32_t filterMask) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001446 legacy_hal::wifi_error legacy_status;
1447 std::vector<legacy_hal::wifi_usable_channel> legacy_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001448 std::tie(legacy_status, legacy_usable_channels) = legacy_hal_.lock()->getUsableChannels(
Kumar Anand2a630a32021-01-21 14:09:14 -08001449 hidl_struct_util::convertHidlWifiBandToLegacyMacBand(band),
Kumar Anandaea86e02021-02-10 16:22:31 -08001450 hidl_struct_util::convertHidlWifiIfaceModeToLegacy(ifaceModeMask),
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001451 hidl_struct_util::convertHidlUsableChannelFilterToLegacy(filterMask));
Kumar Anandaea86e02021-02-10 16:22:31 -08001452
Kumar Anand2a630a32021-01-21 14:09:14 -08001453 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1454 return {createWifiStatusFromLegacyError(legacy_status), {}};
1455 }
Ahmed ElArabawy05571e42022-01-19 11:54:11 -08001456 std::vector<V1_6::WifiUsableChannel> hidl_usable_channels;
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001457 if (!hidl_struct_util::convertLegacyWifiUsableChannelsToHidl(legacy_usable_channels,
1458 &hidl_usable_channels)) {
Kumar Anand2a630a32021-01-21 14:09:14 -08001459 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1460 }
1461 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_usable_channels};
1462}
1463
Roshan Piusba38d9c2017-12-08 07:32:08 -08001464WifiStatus WifiChip::handleChipConfiguration(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001465 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001466 // If the chip is already configured in a different mode, stop
1467 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001468 if (isValidModeId(current_mode_id_)) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001469 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_ << " to mode " << mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -08001470 invalidateAndRemoveAllIfaces();
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001471 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stop(lock, []() {});
Roshan Piusba38d9c2017-12-08 07:32:08 -08001472 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001473 LOG(ERROR) << "Failed to stop legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusba38d9c2017-12-08 07:32:08 -08001474 return createWifiStatusFromLegacyError(legacy_status);
1475 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001476 }
Roshan Piuscc338202017-11-02 13:54:09 -07001477 // Firmware mode change not needed for V2 devices.
1478 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001479 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001480 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001481 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001482 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1483 }
1484 if (!success) {
1485 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1486 }
1487 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1488 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001489 LOG(ERROR) << "Failed to start legacy HAL: " << legacyErrorToString(legacy_status);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001490 return createWifiStatusFromLegacyError(legacy_status);
1491 }
Roshan Pius85c64412018-01-22 17:58:40 -08001492 // Every time the HAL is restarted, we need to register the
1493 // radio mode change callback.
1494 WifiStatus status = registerRadioModeChangeCallback();
1495 if (status.code != WifiStatusCode::SUCCESS) {
1496 // This probably is not a critical failure?
1497 LOG(ERROR) << "Failed to register radio mode change callback";
1498 }
chenpaulf5eca292019-03-14 11:08:03 +08001499 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001500 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001501 version_info = WifiChip::requestChipDebugInfoInternal();
1502 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1503 property_set("vendor.wlan.firmware.version",
1504 version_info.second.firmwareDescription.c_str());
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001505 property_set("vendor.wlan.driver.version", version_info.second.driverDescription.c_str());
chenpaulf5eca292019-03-14 11:08:03 +08001506 }
1507
Roshan Piusabcf78f2017-10-06 16:30:38 -07001508 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001509}
Roshan Pius48185b22016-12-15 19:10:30 -08001510
1511WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001512 if (debug_ring_buffer_cb_registered_) {
1513 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001514 }
Roshan Pius3797e182017-03-30 18:01:54 -07001515
Roshan Piusabcf78f2017-10-06 16:30:38 -07001516 android::wp<WifiChip> weak_ptr_this(this);
1517 const auto& on_ring_buffer_data_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001518 [weak_ptr_this](const std::string& name, const std::vector<uint8_t>& data,
1519 const legacy_hal::wifi_ring_buffer_status& status) {
1520 const auto shared_ptr_this = weak_ptr_this.promote();
1521 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1522 LOG(ERROR) << "Callback invoked on an invalid object";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301523 return;
1524 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001525 WifiDebugRingBufferStatus hidl_status;
1526 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(status,
1527 &hidl_status)) {
1528 LOG(ERROR) << "Error converting ring buffer status";
1529 return;
1530 }
1531 {
1532 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1533 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1534 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1535 Ringbuffer& cur_buffer = target->second;
1536 cur_buffer.append(data);
1537 } else {
1538 LOG(ERROR) << "Ringname " << name << " not found";
1539 return;
1540 }
1541 // unique_lock unlocked here
1542 }
1543 };
1544 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001545 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001546
Roshan Piusabcf78f2017-10-06 16:30:38 -07001547 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1548 debug_ring_buffer_cb_registered_ = true;
1549 }
1550 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001551}
1552
Roshan Pius85c64412018-01-22 17:58:40 -08001553WifiStatus WifiChip::registerRadioModeChangeCallback() {
1554 android::wp<WifiChip> weak_ptr_this(this);
1555 const auto& on_radio_mode_change_callback =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001556 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1557 const auto shared_ptr_this = weak_ptr_this.promote();
1558 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1559 LOG(ERROR) << "Callback invoked on an invalid object";
1560 return;
Roshan Pius85c64412018-01-22 17:58:40 -08001561 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001562 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo> hidl_radio_mode_infos;
1563 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(mac_infos,
1564 &hidl_radio_mode_infos)) {
1565 LOG(ERROR) << "Error converting wifi mac info";
1566 return;
1567 }
1568 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1569 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos).isOk()) {
1570 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
1571 << " callback on: " << toString(callback);
1572 }
1573 }
1574 };
Roshan Pius85c64412018-01-22 17:58:40 -08001575 legacy_hal::wifi_error legacy_status =
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001576 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1577 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001578 return createWifiStatusFromLegacyError(legacy_status);
1579}
1580
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001581std::vector<V1_4::IWifiChip::ChipIfaceCombination> WifiChip::getCurrentModeIfaceCombinations() {
Roshan Piuscc338202017-11-02 13:54:09 -07001582 if (!isValidModeId(current_mode_id_)) {
1583 LOG(ERROR) << "Chip not configured in a mode yet";
1584 return {};
1585 }
1586 for (const auto& mode : modes_) {
1587 if (mode.id == current_mode_id_) {
1588 return mode.availableCombinations;
1589 }
1590 }
1591 CHECK(0) << "Expected to find iface combinations for current mode!";
1592 return {};
1593}
1594
1595// Returns a map indexed by IfaceType with the number of ifaces currently
1596// created of the corresponding type.
1597std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1598 std::map<IfaceType, size_t> iface_counts;
1599 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1600 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1601 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1602 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1603 return iface_counts;
1604}
1605
1606// This expands the provided iface combinations to a more parseable
1607// form. Returns a vector of available combinations possible with the number
1608// of ifaces of each type in the combination.
1609// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1610std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001611 const V1_4::IWifiChip::ChipIfaceCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001612 uint32_t num_expanded_combos = 1;
1613 for (const auto& limit : combination.limits) {
1614 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1615 num_expanded_combos *= limit.types.size();
1616 }
1617 }
1618
1619 // Allocate the vector of expanded combos and reset all iface counts to 0
1620 // in each combo.
1621 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1622 expanded_combos.resize(num_expanded_combos);
1623 for (auto& expanded_combo : expanded_combos) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001624 for (const auto type : {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001625 expanded_combo[type] = 0;
1626 }
1627 }
1628 uint32_t span = num_expanded_combos;
1629 for (const auto& limit : combination.limits) {
1630 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1631 span /= limit.types.size();
1632 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001633 const auto iface_type = limit.types[(k / span) % limit.types.size()];
Roshan Piuscc338202017-11-02 13:54:09 -07001634 expanded_combos[k][iface_type]++;
1635 }
1636 }
1637 }
1638 return expanded_combos;
1639}
1640
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001641bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001642 const std::map<IfaceType, size_t>& expanded_combo, IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001643 const auto current_combo = getCurrentIfaceCombination();
1644
1645 // Check if we have space for 1 more iface of |type| in this combo
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001646 for (const auto type : {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
Roshan Piuscc338202017-11-02 13:54:09 -07001647 size_t num_ifaces_needed = current_combo.at(type);
1648 if (type == requested_type) {
1649 num_ifaces_needed++;
1650 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001651 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001652 if (num_ifaces_needed > num_ifaces_allowed) {
1653 return false;
1654 }
1655 }
1656 return true;
1657}
1658
1659// This method does the following:
1660// a) Enumerate all possible iface combos by expanding the current
1661// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001662// b) Check if the requested iface type can be added to the current mode
1663// with the iface combination that is already active.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001664bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001665 if (!isValidModeId(current_mode_id_)) {
1666 LOG(ERROR) << "Chip not configured in a mode yet";
1667 return false;
1668 }
1669 const auto combinations = getCurrentModeIfaceCombinations();
1670 for (const auto& combination : combinations) {
1671 const auto expanded_combos = expandIfaceCombinations(combination);
1672 for (const auto& expanded_combo : expanded_combos) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001673 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(expanded_combo,
1674 requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001675 return true;
1676 }
1677 }
1678 }
1679 return false;
1680}
1681
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001682// Note: This does not consider ifaces already active. It only checks if the
1683// provided expanded iface combination can support the requested combo.
1684bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001685 const std::map<IfaceType, size_t>& expanded_combo,
1686 const std::map<IfaceType, size_t>& req_combo) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001687 // Check if we have space for 1 more iface of |type| in this combo
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001688 for (const auto type : {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001689 if (req_combo.count(type) == 0) {
1690 // Iface of "type" not in the req_combo.
1691 continue;
1692 }
1693 size_t num_ifaces_needed = req_combo.at(type);
1694 size_t num_ifaces_allowed = expanded_combo.at(type);
1695 if (num_ifaces_needed > num_ifaces_allowed) {
1696 return false;
1697 }
1698 }
1699 return true;
1700}
1701// This method does the following:
1702// a) Enumerate all possible iface combos by expanding the current
1703// ChipIfaceCombination.
1704// b) Check if the requested iface combo can be added to the current mode.
1705// Note: This does not consider ifaces already active. It only checks if the
1706// current mode can support the requested combo.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001707bool WifiChip::canCurrentModeSupportIfaceCombo(const std::map<IfaceType, size_t>& req_combo) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001708 if (!isValidModeId(current_mode_id_)) {
1709 LOG(ERROR) << "Chip not configured in a mode yet";
1710 return false;
1711 }
1712 const auto combinations = getCurrentModeIfaceCombinations();
1713 for (const auto& combination : combinations) {
1714 const auto expanded_combos = expandIfaceCombinations(combination);
1715 for (const auto& expanded_combo : expanded_combos) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001716 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo, req_combo)) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001717 return true;
1718 }
1719 }
1720 }
1721 return false;
1722}
1723
1724// This method does the following:
1725// a) Enumerate all possible iface combos by expanding the current
1726// ChipIfaceCombination.
1727// b) Check if the requested iface type can be added to the current mode.
1728bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
lesl261818b2020-11-27 12:37:35 +08001729 // Check if we can support at least 1 iface of type.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001730 std::map<IfaceType, size_t> req_iface_combo;
1731 req_iface_combo[requested_type] = 1;
1732 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1733}
1734
Roshan Piuscc338202017-11-02 13:54:09 -07001735bool WifiChip::isValidModeId(ChipModeId mode_id) {
1736 for (const auto& mode : modes_) {
1737 if (mode.id == mode_id) {
1738 return true;
1739 }
1740 }
1741 return false;
1742}
1743
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001744bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
lesl261818b2020-11-27 12:37:35 +08001745 // Check if we can support at least 1 STA & 1 AP concurrently.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001746 std::map<IfaceType, size_t> req_iface_combo;
1747 req_iface_combo[IfaceType::AP] = 1;
1748 req_iface_combo[IfaceType::STA] = 1;
1749 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1750}
1751
lesl261818b2020-11-27 12:37:35 +08001752bool WifiChip::isDualStaConcurrencyAllowedInCurrentMode() {
1753 // Check if we can support at least 2 STA concurrently.
James Mattisd2e4c072019-05-22 16:14:48 -07001754 std::map<IfaceType, size_t> req_iface_combo;
lesl261818b2020-11-27 12:37:35 +08001755 req_iface_combo[IfaceType::STA] = 2;
James Mattisd2e4c072019-05-22 16:14:48 -07001756 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1757}
1758
Roshan Pius6036c022019-03-27 10:41:58 -07001759std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001760 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
leslf012b652021-01-08 15:22:49 +08001761 if (ap_ifaces_.size() > 0) {
1762 // If the first active wlan iface is bridged iface.
1763 // Return first instance name.
1764 for (auto const& it : br_ifaces_ap_instances_) {
1765 if (it.first == ap_ifaces_[0]->getName()) {
1766 return it.second[0];
1767 }
1768 }
1769 return ap_ifaces_[0]->getName();
1770 }
Roshan Pius6036c022019-03-27 10:41:58 -07001771 // This could happen if the chip call is made before any STA/AP
1772 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001773 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Jimmy Chen2dddd792019-12-23 17:50:39 +02001774 return getWlanIfaceNameWithType(IfaceType::STA, 0);
Roshan Pius6036c022019-03-27 10:41:58 -07001775}
1776
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001777// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1778// not already in use.
1779// Note: This doesn't check the actual presence of these interfaces.
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001780std::string WifiChip::allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001781 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001782 const auto ifname = getWlanIfaceNameWithType(type, idx);
lesl94d28242020-11-18 22:17:37 +08001783 if (findUsingNameFromBridgedApInstances(ifname)) continue;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001784 if (findUsingName(ap_ifaces_, ifname)) continue;
1785 if (findUsingName(sta_ifaces_, ifname)) continue;
1786 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001787 }
1788 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001789 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001790 return {};
1791}
1792
lesl261818b2020-11-27 12:37:35 +08001793uint32_t WifiChip::startIdxOfApIface() {
1794 if (isDualStaConcurrencyAllowedInCurrentMode()) {
1795 // When the HAL support dual STAs, AP should start with idx 2.
1796 return 2;
1797 } else if (isStaApConcurrencyAllowedInCurrentMode()) {
1798 // When the HAL support STA + AP but it doesn't support dual STAs.
1799 // AP should start with idx 1.
1800 return 1;
1801 }
1802 // No concurrency support.
1803 return 0;
1804}
1805
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001806// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001807// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001808std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001809 // Check if we have a dedicated iface for AP.
lesl261818b2020-11-27 12:37:35 +08001810 std::vector<std::string> ifnames = getPredefinedApIfaceNames(false);
1811 if (!ifnames.empty()) {
1812 return ifnames[0];
Roshan Pius78cb5992020-04-30 12:39:21 -07001813 }
lesl261818b2020-11-27 12:37:35 +08001814 return allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface());
1815}
1816
1817std::vector<std::string> WifiChip::allocateBridgedApInstanceNames() {
1818 // Check if we have a dedicated iface for AP.
1819 std::vector<std::string> instances = getPredefinedApIfaceNames(true);
1820 if (instances.size() == 2) {
1821 return instances;
1822 } else {
1823 int num_ifaces_need_to_allocate = 2 - instances.size();
1824 for (int i = 0; i < num_ifaces_need_to_allocate; i++) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001825 std::string instance_name =
1826 allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface() + i);
lesl261818b2020-11-27 12:37:35 +08001827 if (!instance_name.empty()) {
1828 instances.push_back(instance_name);
1829 }
1830 }
1831 }
1832 return instances;
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001833}
1834
1835// STA iface names start with idx 0.
1836// Primary STA iface will always be 0.
1837std::string WifiChip::allocateStaIfaceName() {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001838 return allocateApOrStaIfaceName(IfaceType::STA, 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001839}
1840
xshu5899e8e2018-01-09 15:36:03 -08001841bool WifiChip::writeRingbufferFilesInternal() {
1842 if (!removeOldFilesInternal()) {
1843 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1844 return false;
1845 }
1846 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301847 {
1848 std::unique_lock<std::mutex> lk(lock_t);
xshuc905ea62021-07-11 19:57:02 -07001849 for (auto& item : ringbuffer_map_) {
1850 Ringbuffer& cur_buffer = item.second;
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301851 if (cur_buffer.getData().empty()) {
1852 continue;
1853 }
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001854 const std::string file_path_raw = kTombstoneFolderPath + item.first + "XXXXXXXXXX";
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301855 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1856 if (dump_fd == -1) {
1857 PLOG(ERROR) << "create file failed";
1858 return false;
1859 }
1860 unique_fd file_auto_closer(dump_fd);
1861 for (const auto& cur_block : cur_buffer.getData()) {
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001862 if (write(dump_fd, cur_block.data(), sizeof(cur_block[0]) * cur_block.size()) ==
1863 -1) {
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301864 PLOG(ERROR) << "Error writing to file";
1865 }
xshu5899e8e2018-01-09 15:36:03 -08001866 }
xshuc905ea62021-07-11 19:57:02 -07001867 cur_buffer.clear();
xshu5899e8e2018-01-09 15:36:03 -08001868 }
xshu0a0fe512020-07-22 17:53:37 -07001869 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001870 }
1871 return true;
1872}
1873
Jimmy Chen2dddd792019-12-23 17:50:39 +02001874std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1875 std::string ifname;
1876
1877 // let the legacy hal override the interface name
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001878 legacy_hal::wifi_error err = legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
Jimmy Chen2dddd792019-12-23 17:50:39 +02001879 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1880
1881 return getWlanIfaceName(idx);
1882}
1883
lesl94d28242020-11-18 22:17:37 +08001884void WifiChip::invalidateAndClearBridgedApAll() {
1885 for (auto const& it : br_ifaces_ap_instances_) {
1886 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001887 iface_util_->removeIfaceFromBridge(it.first, iface);
lesl94d28242020-11-18 22:17:37 +08001888 legacy_hal_.lock()->deleteVirtualInterface(iface);
1889 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001890 iface_util_->deleteBridge(it.first);
lesl94d28242020-11-18 22:17:37 +08001891 }
1892 br_ifaces_ap_instances_.clear();
1893}
1894
1895void WifiChip::invalidateAndClearBridgedAp(const std::string& br_name) {
1896 if (br_name.empty()) return;
1897 // delete managed interfaces
1898 for (auto const& it : br_ifaces_ap_instances_) {
1899 if (it.first == br_name) {
1900 for (auto const& iface : it.second) {
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001901 iface_util_->removeIfaceFromBridge(br_name, iface);
lesl94d28242020-11-18 22:17:37 +08001902 legacy_hal_.lock()->deleteVirtualInterface(iface);
1903 }
Roshan Pius8c1a67b2021-03-02 10:00:23 -08001904 iface_util_->deleteBridge(br_name);
lesl94d28242020-11-18 22:17:37 +08001905 br_ifaces_ap_instances_.erase(br_name);
1906 break;
1907 }
1908 }
1909 return;
1910}
1911
1912bool WifiChip::findUsingNameFromBridgedApInstances(const std::string& name) {
1913 for (auto const& it : br_ifaces_ap_instances_) {
1914 if (it.first == name) {
1915 return true;
1916 }
1917 for (auto const& iface : it.second) {
1918 if (iface == name) {
1919 return true;
1920 }
1921 }
1922 }
1923 return false;
1924}
1925
Roshan Pius79a99752016-10-04 13:03:58 -07001926} // namespace implementation
Ahmed ElArabawy687ce132022-01-11 16:42:48 -08001927} // namespace V1_6
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001928} // namespace wifi
1929} // namespace hardware
1930} // namespace android