blob: fa4f354e62ca3e21db23d47869fd308a0fabc80d [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>
Nate Jiang4ad7daa2020-05-18 15:17:47 -070022#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
Roshan Pius35d958c2016-10-06 16:47:38 -070031namespace {
Jong Wook Kimda830c92018-07-23 15:29:38 -070032using android::sp;
xshu5899e8e2018-01-09 15:36:03 -080033using android::base::unique_fd;
Roshan Pius35d958c2016-10-06 16:47:38 -070034using android::hardware::hidl_string;
Roshan Piusabcf78f2017-10-06 16:30:38 -070035using android::hardware::hidl_vec;
Roshan Pius2c06a3f2016-12-15 17:51:40 -080036using android::hardware::wifi::V1_0::ChipModeId;
Roshan Pius52947fb2016-11-18 11:38:07 -080037using android::hardware::wifi::V1_0::IfaceType;
Roshan Pius675609b2017-10-31 14:24:58 -070038using android::hardware::wifi::V1_0::IWifiChip;
Roshan Pius52947fb2016-11-18 11:38:07 -080039
xshu5899e8e2018-01-09 15:36:03 -080040constexpr char kCpioMagic[] = "070701";
Roger Wangb294c762018-11-02 15:34:39 +080041constexpr size_t kMaxBufferSizeBytes = 1024 * 1024 * 3;
42constexpr uint32_t kMaxRingBufferFileAgeSeconds = 60 * 60 * 10;
xshu37126c92018-04-13 16:24:45 -070043constexpr uint32_t kMaxRingBufferFileNum = 20;
xshu5899e8e2018-01-09 15:36:03 -080044constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
Roshan Pius8574e7f2019-04-01 13:30:40 -070045constexpr char kActiveWlanIfaceNameProperty[] = "wifi.active.interface";
46constexpr char kNoActiveWlanIfaceNamePropertyValue[] = "";
47constexpr unsigned kMaxWlanIfaces = 5;
xshu5899e8e2018-01-09 15:36:03 -080048
Roshan Pius35d958c2016-10-06 16:47:38 -070049template <typename Iface>
Roshan Pius675609b2017-10-31 14:24:58 -070050void invalidateAndClear(std::vector<sp<Iface>>& ifaces, sp<Iface> iface) {
51 iface->invalidate();
52 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface),
53 ifaces.end());
54}
55
56template <typename Iface>
57void invalidateAndClearAll(std::vector<sp<Iface>>& ifaces) {
58 for (const auto& iface : ifaces) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070059 iface->invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -070060 }
Roshan Pius675609b2017-10-31 14:24:58 -070061 ifaces.clear();
62}
63
64template <typename Iface>
65std::vector<hidl_string> getNames(std::vector<sp<Iface>>& ifaces) {
66 std::vector<hidl_string> names;
67 for (const auto& iface : ifaces) {
68 names.emplace_back(iface->getName());
69 }
70 return names;
71}
72
73template <typename Iface>
74sp<Iface> findUsingName(std::vector<sp<Iface>>& ifaces,
75 const std::string& name) {
76 std::vector<hidl_string> names;
77 for (const auto& iface : ifaces) {
78 if (name == iface->getName()) {
79 return iface;
80 }
81 }
82 return nullptr;
Roshan Pius35d958c2016-10-06 16:47:38 -070083}
Roshan Pius9377a0d2017-10-06 13:18:54 -070084
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080085std::string getWlanIfaceName(unsigned idx) {
86 if (idx >= kMaxWlanIfaces) {
87 CHECK(false) << "Requested interface beyond wlan" << kMaxWlanIfaces;
88 return {};
89 }
Roshan Pius9377a0d2017-10-06 13:18:54 -070090
Roshan Pius8e3c7ef2017-11-03 09:43:08 -070091 std::array<char, PROPERTY_VALUE_MAX> buffer;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080092 if (idx == 0 || idx == 1) {
93 const char* altPropName =
94 (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
Roshan Pius78cb5992020-04-30 12:39:21 -0700105// Returns the dedicated iface name if one is defined.
106std::string getApIfaceName() {
107 std::array<char, PROPERTY_VALUE_MAX> buffer;
108 if (property_get("ro.vendor.wifi.sap.interface", buffer.data(), nullptr) ==
109 0) {
110 return {};
111 }
112 return buffer.data();
113}
114
Roshan Pius9377a0d2017-10-06 13:18:54 -0700115std::string getP2pIfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700116 std::array<char, PROPERTY_VALUE_MAX> buffer;
117 property_get("wifi.direct.interface", buffer.data(), "p2p0");
118 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700119}
120
Roshan Pius5ba0a902020-04-14 11:55:42 -0700121// Returns the dedicated iface name if one is defined.
122std::string getNanIfaceName() {
123 std::array<char, PROPERTY_VALUE_MAX> buffer;
124 if (property_get("wifi.aware.interface", buffer.data(), nullptr) == 0) {
125 return {};
126 }
127 return buffer.data();
128}
129
Roshan Pius8574e7f2019-04-01 13:30:40 -0700130void setActiveWlanIfaceNameProperty(const std::string& ifname) {
131 auto res = property_set(kActiveWlanIfaceNameProperty, ifname.data());
132 if (res != 0) {
133 PLOG(ERROR) << "Failed to set active wlan iface name property";
134 }
135}
136
xshu37126c92018-04-13 16:24:45 -0700137// delete files that meet either conditions:
138// 1. older than a predefined time in the wifi tombstone dir.
139// 2. Files in excess to a predefined amount, starting from the oldest ones
xshu5899e8e2018-01-09 15:36:03 -0800140bool removeOldFilesInternal() {
141 time_t now = time(0);
142 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
Josh Gaoa568e532018-06-04 18:16:00 -0700143 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(
144 opendir(kTombstoneFolderPath), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800145 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800146 PLOG(ERROR) << "Failed to open directory";
xshu5899e8e2018-01-09 15:36:03 -0800147 return false;
148 }
xshu5899e8e2018-01-09 15:36:03 -0800149 struct dirent* dp;
150 bool success = true;
xshu37126c92018-04-13 16:24:45 -0700151 std::list<std::pair<const time_t, std::string>> valid_files;
Josh Gaoa568e532018-06-04 18:16:00 -0700152 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800153 if (dp->d_type != DT_REG) {
154 continue;
155 }
156 std::string cur_file_name(dp->d_name);
157 struct stat cur_file_stat;
158 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800159 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800160 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800161 success = false;
xshu4cb33162018-01-24 15:40:06 -0800162 continue;
163 }
xshu37126c92018-04-13 16:24:45 -0700164 const time_t cur_file_time = cur_file_stat.st_mtime;
165 valid_files.push_back(
166 std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
167 }
168 valid_files.sort(); // sort the list of files by last modified time from
169 // small to big.
170 uint32_t cur_file_count = valid_files.size();
171 for (auto cur_file : valid_files) {
172 if (cur_file_count > kMaxRingBufferFileNum ||
173 cur_file.first < delete_files_before) {
174 if (unlink(cur_file.second.c_str()) != 0) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800175 PLOG(ERROR) << "Error deleting file";
xshu37126c92018-04-13 16:24:45 -0700176 success = false;
177 }
178 cur_file_count--;
179 } else {
180 break;
xshu5899e8e2018-01-09 15:36:03 -0800181 }
182 }
183 return success;
184}
185
xshu4cb33162018-01-24 15:40:06 -0800186// Helper function for |cpioArchiveFilesInDir|
187bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name,
188 size_t file_name_len) {
189 std::array<char, 32 * 1024> read_buf;
190 ssize_t llen =
191 sprintf(read_buf.data(),
192 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
193 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
194 st.st_gid, static_cast<int>(st.st_nlink),
195 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
196 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
197 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
198 if (write(out_fd, read_buf.data(), llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800199 PLOG(ERROR) << "Error writing cpio header to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800200 return false;
201 }
202 if (write(out_fd, file_name, file_name_len) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800203 PLOG(ERROR) << "Error writing filename to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800204 return false;
205 }
206
207 // NUL Pad header up to 4 multiple bytes.
208 llen = (llen + file_name_len) % 4;
209 if (llen != 0) {
210 const uint32_t zero = 0;
211 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800212 PLOG(ERROR) << "Error padding 0s to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800213 return false;
214 }
215 }
216 return true;
217}
218
219// Helper function for |cpioArchiveFilesInDir|
220size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
221 // writing content of file
222 std::array<char, 32 * 1024> read_buf;
223 ssize_t llen = st.st_size;
224 size_t n_error = 0;
225 while (llen > 0) {
226 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
227 if (bytes_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800228 PLOG(ERROR) << "Error reading file";
xshu4cb33162018-01-24 15:40:06 -0800229 return ++n_error;
230 }
231 llen -= bytes_read;
232 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800233 PLOG(ERROR) << "Error writing data to file";
xshu4cb33162018-01-24 15:40:06 -0800234 return ++n_error;
235 }
236 if (bytes_read == 0) { // this should never happen, but just in case
237 // to unstuck from while loop
Elliott Hughes4db4add2019-03-08 12:42:57 -0800238 PLOG(ERROR) << "Unexpected read result";
xshu4cb33162018-01-24 15:40:06 -0800239 n_error++;
240 break;
241 }
242 }
243 llen = st.st_size % 4;
244 if (llen != 0) {
245 const uint32_t zero = 0;
246 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800247 PLOG(ERROR) << "Error padding 0s to file";
xshu4cb33162018-01-24 15:40:06 -0800248 return ++n_error;
249 }
250 }
251 return n_error;
252}
253
254// Helper function for |cpioArchiveFilesInDir|
255bool cpioWriteFileTrailer(int out_fd) {
256 std::array<char, 4096> read_buf;
257 read_buf.fill(0);
258 if (write(out_fd, read_buf.data(),
259 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1,
260 0x0b, 0) +
261 4) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800262 PLOG(ERROR) << "Error writing trailing bytes";
xshu4cb33162018-01-24 15:40:06 -0800263 return false;
264 }
265 return true;
266}
267
xshu5899e8e2018-01-09 15:36:03 -0800268// Archives all files in |input_dir| and writes result into |out_fd|
269// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
270// portion
xshu4cb33162018-01-24 15:40:06 -0800271size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800272 struct dirent* dp;
273 size_t n_error = 0;
Josh Gaoa568e532018-06-04 18:16:00 -0700274 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir),
275 closedir);
xshu5899e8e2018-01-09 15:36:03 -0800276 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800277 PLOG(ERROR) << "Failed to open directory";
xshu4cb33162018-01-24 15:40:06 -0800278 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800279 }
Josh Gaoa568e532018-06-04 18:16:00 -0700280 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800281 if (dp->d_type != DT_REG) {
282 continue;
283 }
284 std::string cur_file_name(dp->d_name);
xshu4cb33162018-01-24 15:40:06 -0800285 // string.size() does not include the null terminator. The cpio FreeBSD
286 // file header expects the null character to be included in the length.
287 const size_t file_name_len = cur_file_name.size() + 1;
xshu5899e8e2018-01-09 15:36:03 -0800288 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800289 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800290 if (stat(cur_file_path.c_str(), &st) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800291 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800292 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800293 continue;
294 }
295 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
296 if (fd_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800297 PLOG(ERROR) << "Failed to open file " << cur_file_path;
xshu4cb33162018-01-24 15:40:06 -0800298 n_error++;
299 continue;
300 }
301 unique_fd file_auto_closer(fd_read);
302 if (!cpioWriteHeader(out_fd, st, cur_file_name.c_str(),
303 file_name_len)) {
304 return ++n_error;
305 }
306 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
307 if (write_error) {
308 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800309 }
310 }
xshu4cb33162018-01-24 15:40:06 -0800311 if (!cpioWriteFileTrailer(out_fd)) {
312 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800313 }
314 return n_error;
315}
316
317// Helper function to create a non-const char*.
318std::vector<char> makeCharVec(const std::string& str) {
319 std::vector<char> vec(str.size() + 1);
320 vec.assign(str.begin(), str.end());
321 vec.push_back('\0');
322 return vec;
323}
324
Roshan Piusabcf78f2017-10-06 16:30:38 -0700325} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700326
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700327namespace android {
328namespace hardware {
329namespace wifi {
Ahmed ElArabawyf501a982019-07-23 15:02:22 -0700330namespace V1_4 {
Roshan Pius79a99752016-10-04 13:03:58 -0700331namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700332using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800333using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700334
Roshan Pius52947fb2016-11-18 11:38:07 -0800335WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700336 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700337 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800338 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Roshan Pius200a17d2017-11-01 13:03:35 -0700339 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Pius52947fb2016-11-18 11:38:07 -0800340 : chip_id_(chip_id),
341 legacy_hal_(legacy_hal),
342 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800343 iface_util_(iface_util),
Roshan Pius52947fb2016-11-18 11:38:07 -0800344 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800345 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
346 modes_(feature_flags.lock()->getChipModes()),
Roshan Pius8574e7f2019-04-01 13:30:40 -0700347 debug_ring_buffer_cb_registered_(false) {
348 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
349}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700350
Roshan Piusaabe5752016-09-29 09:03:59 -0700351void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700352 if (!writeRingbufferFilesInternal()) {
353 LOG(ERROR) << "Error writing files to flash";
354 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700355 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700356 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700357 legacy_hal_.reset();
358 event_cb_handler_.invalidate();
359 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700360}
361
Roshan Piusabcf78f2017-10-06 16:30:38 -0700362bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700363
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800364std::set<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700365 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800366}
367
Roshan Pius5c055462016-10-11 08:27:27 -0700368Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700369 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
370 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700371}
372
Jong Wook Kimda830c92018-07-23 15:29:38 -0700373// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700374Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800375 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700376 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700377 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
378 &WifiChip::registerEventCallbackInternal,
379 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700380}
381
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700382Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700383 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
384 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700385}
386
Roshan Pius5c055462016-10-11 08:27:27 -0700387Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700388 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
389 &WifiChip::getAvailableModesInternal,
390 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700391}
392
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800393Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700394 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800395 return validateAndCallWithLock(
396 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
397 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700398}
399
Roshan Pius5c055462016-10-11 08:27:27 -0700400Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700401 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
402 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700403}
404
Roshan Pius5c055462016-10-11 08:27:27 -0700405Return<void> WifiChip::requestChipDebugInfo(
406 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700407 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
408 &WifiChip::requestChipDebugInfoInternal,
409 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700410}
411
412Return<void> WifiChip::requestDriverDebugDump(
413 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700414 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
415 &WifiChip::requestDriverDebugDumpInternal,
416 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700417}
418
Roshan Pius5c055462016-10-11 08:27:27 -0700419Return<void> WifiChip::requestFirmwareDebugDump(
420 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700421 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
422 &WifiChip::requestFirmwareDebugDumpInternal,
423 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700424}
425
Roshan Pius5c055462016-10-11 08:27:27 -0700426Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700427 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
428 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700429}
430
Roshan Pius5c055462016-10-11 08:27:27 -0700431Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700432 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
433 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700434}
435
Roshan Pius5c055462016-10-11 08:27:27 -0700436Return<void> WifiChip::getApIface(const hidl_string& ifname,
437 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700438 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
439 &WifiChip::getApIfaceInternal, hidl_status_cb,
440 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700441}
442
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800443Return<void> WifiChip::removeApIface(const hidl_string& ifname,
444 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700445 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
446 &WifiChip::removeApIfaceInternal, hidl_status_cb,
447 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800448}
449
Roshan Pius5c055462016-10-11 08:27:27 -0700450Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700451 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
452 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700453}
454
Roshan Pius5c055462016-10-11 08:27:27 -0700455Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700456 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
457 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700458}
459
460Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700461 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700462 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
463 &WifiChip::getNanIfaceInternal, hidl_status_cb,
464 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700465}
466
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800467Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
468 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700469 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
470 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
471 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800472}
473
Roshan Pius5c055462016-10-11 08:27:27 -0700474Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700475 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
476 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700477}
478
Roshan Pius5c055462016-10-11 08:27:27 -0700479Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700480 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
481 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700482}
483
484Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700485 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700486 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
487 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
488 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700489}
490
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800491Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
492 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700493 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
494 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
495 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800496}
497
Roshan Pius5c055462016-10-11 08:27:27 -0700498Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700499 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
500 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700501}
502
Roshan Pius5c055462016-10-11 08:27:27 -0700503Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700504 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
505 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700506}
507
508Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700509 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700510 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
511 &WifiChip::getStaIfaceInternal, hidl_status_cb,
512 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700513}
514
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800515Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
516 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700517 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
518 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
519 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800520}
521
Roshan Pius5c055462016-10-11 08:27:27 -0700522Return<void> WifiChip::createRttController(
523 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700524 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
525 &WifiChip::createRttControllerInternal,
526 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700527}
528
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700529Return<void> WifiChip::getDebugRingBuffersStatus(
530 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700531 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
532 &WifiChip::getDebugRingBuffersStatusInternal,
533 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700534}
535
536Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700537 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
538 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700539 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700540 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
541 &WifiChip::startLoggingToDebugRingBufferInternal,
542 hidl_status_cb, ring_name, verbose_level,
543 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700544}
545
546Return<void> WifiChip::forceDumpToDebugRingBuffer(
547 const hidl_string& ring_name,
548 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700549 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
550 &WifiChip::forceDumpToDebugRingBufferInternal,
551 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700552}
553
Roger Wangb294c762018-11-02 15:34:39 +0800554Return<void> WifiChip::flushRingBufferToFile(
555 flushRingBufferToFile_cb hidl_status_cb) {
556 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
557 &WifiChip::flushRingBufferToFileInternal,
558 hidl_status_cb);
559}
560
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800561Return<void> WifiChip::stopLoggingToDebugRingBuffer(
562 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700563 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
564 &WifiChip::stopLoggingToDebugRingBufferInternal,
565 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800566}
567
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700568Return<void> WifiChip::getDebugHostWakeReasonStats(
569 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700570 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
571 &WifiChip::getDebugHostWakeReasonStatsInternal,
572 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700573}
574
Roshan Pius203cb032016-12-14 17:41:20 -0800575Return<void> WifiChip::enableDebugErrorAlerts(
576 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700577 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
578 &WifiChip::enableDebugErrorAlertsInternal,
579 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800580}
581
Roshan Pius735ff432017-07-25 08:48:08 -0700582Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700583 V1_1::IWifiChip::TxPowerScenario scenario,
584 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700585 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
586 &WifiChip::selectTxPowerScenarioInternal,
587 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700588}
589
Roshan Pius735ff432017-07-25 08:48:08 -0700590Return<void> WifiChip::resetTxPowerScenario(
591 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700592 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
593 &WifiChip::resetTxPowerScenarioInternal,
594 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700595}
596
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700597Return<void> WifiChip::setLatencyMode(LatencyMode mode,
598 setLatencyMode_cb hidl_status_cb) {
599 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
600 &WifiChip::setLatencyModeInternal, hidl_status_cb,
601 mode);
602}
603
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800604Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700605 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800606 registerEventCallback_cb hidl_status_cb) {
607 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
608 &WifiChip::registerEventCallbackInternal_1_2,
609 hidl_status_cb, event_callback);
610}
611
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800612Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700613 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800614 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700615 &WifiChip::selectTxPowerScenarioInternal_1_2,
616 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800617}
618
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700619Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
620 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
621 &WifiChip::getCapabilitiesInternal_1_3,
622 hidl_status_cb);
623}
624
xshu5899e8e2018-01-09 15:36:03 -0800625Return<void> WifiChip::debug(const hidl_handle& handle,
626 const hidl_vec<hidl_string>&) {
627 if (handle != nullptr && handle->numFds >= 1) {
628 int fd = handle->data[0];
629 if (!writeRingbufferFilesInternal()) {
630 LOG(ERROR) << "Error writing files to flash";
631 }
xshu4cb33162018-01-24 15:40:06 -0800632 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800633 if (n_error != 0) {
634 LOG(ERROR) << n_error << " errors occured in cpio function";
635 }
636 fsync(fd);
637 } else {
638 LOG(ERROR) << "File handle error";
639 }
640 return Void();
641}
642
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700643Return<void> WifiChip::createRttController_1_4(
644 const sp<IWifiIface>& bound_iface,
645 createRttController_1_4_cb hidl_status_cb) {
646 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
647 &WifiChip::createRttControllerInternal_1_4,
648 hidl_status_cb, bound_iface);
649}
650
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800651Return<void> WifiChip::registerEventCallback_1_4(
652 const sp<IWifiChipEventCallback>& event_callback,
653 registerEventCallback_cb hidl_status_cb) {
654 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
655 &WifiChip::registerEventCallbackInternal_1_4,
656 hidl_status_cb, event_callback);
657}
658
Roshan Pius35d958c2016-10-06 16:47:38 -0700659void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700660 invalidateAndClearAll(ap_ifaces_);
661 invalidateAndClearAll(nan_ifaces_);
662 invalidateAndClearAll(p2p_ifaces_);
663 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700664 // Since all the ifaces are invalid now, all RTT controller objects
665 // using those ifaces also need to be invalidated.
666 for (const auto& rtt : rtt_controllers_) {
667 rtt->invalidate();
668 }
669 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700670}
671
Roshan Pius82368502019-05-16 12:53:02 -0700672void WifiChip::invalidateAndRemoveDependencies(
673 const std::string& removed_iface_name) {
674 for (const auto& nan_iface : nan_ifaces_) {
675 if (nan_iface->getName() == removed_iface_name) {
676 invalidateAndClear(nan_ifaces_, nan_iface);
677 for (const auto& callback : event_cb_handler_.getCallbacks()) {
678 if (!callback
679 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
680 .isOk()) {
681 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
682 }
683 }
684 }
685 }
686 for (const auto& rtt : rtt_controllers_) {
687 if (rtt->getIfaceName() == removed_iface_name) {
688 invalidateAndClear(rtt_controllers_, rtt);
689 }
690 }
691}
692
Roshan Pius3c868522016-10-27 12:43:49 -0700693std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700694 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700695}
696
697WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800698 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
699 // Deprecated support for this callback.
700 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700701}
702
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700703std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700704 // Deprecated support for this callback.
705 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
706}
707
Roshan Pius3c868522016-10-27 12:43:49 -0700708std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
709WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700710 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700711}
712
Roshan Piusba38d9c2017-12-08 07:32:08 -0800713WifiStatus WifiChip::configureChipInternal(
714 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
715 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700716 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700717 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
718 }
719 if (mode_id == current_mode_id_) {
720 LOG(DEBUG) << "Already in the specified mode " << mode_id;
721 return createWifiStatus(WifiStatusCode::SUCCESS);
722 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800723 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700724 if (status.code != WifiStatusCode::SUCCESS) {
725 for (const auto& callback : event_cb_handler_.getCallbacks()) {
726 if (!callback->onChipReconfigureFailure(status).isOk()) {
727 LOG(ERROR)
728 << "Failed to invoke onChipReconfigureFailure callback";
729 }
730 }
731 return status;
732 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800733 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700734 if (!callback->onChipReconfigured(mode_id).isOk()) {
735 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
736 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800737 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700738 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800739 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700740 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800741 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700742}
743
744std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700745 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700746 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
747 current_mode_id_};
748 }
749 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700750}
751
752std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
753WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700754 IWifiChip::ChipDebugInfo result;
755 legacy_hal::wifi_error legacy_status;
756 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700757 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700758 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800759 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700760 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
761 LOG(ERROR) << "Failed to get driver version: "
762 << legacyErrorToString(legacy_status);
763 WifiStatus status = createWifiStatusFromLegacyError(
764 legacy_status, "failed to get driver version");
765 return {status, result};
766 }
767 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700768
Roshan Piusabcf78f2017-10-06 16:30:38 -0700769 std::string firmware_desc;
770 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800771 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700772 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
773 LOG(ERROR) << "Failed to get firmware version: "
774 << legacyErrorToString(legacy_status);
775 WifiStatus status = createWifiStatusFromLegacyError(
776 legacy_status, "failed to get firmware version");
777 return {status, result};
778 }
779 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700780
Roshan Piusabcf78f2017-10-06 16:30:38 -0700781 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700782}
783
784std::pair<WifiStatus, std::vector<uint8_t>>
785WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700786 legacy_hal::wifi_error legacy_status;
787 std::vector<uint8_t> driver_dump;
788 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700789 legacy_hal_.lock()->requestDriverMemoryDump(
790 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700791 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
792 LOG(ERROR) << "Failed to get driver debug dump: "
793 << legacyErrorToString(legacy_status);
794 return {createWifiStatusFromLegacyError(legacy_status),
795 std::vector<uint8_t>()};
796 }
797 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700798}
799
800std::pair<WifiStatus, std::vector<uint8_t>>
801WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700802 legacy_hal::wifi_error legacy_status;
803 std::vector<uint8_t> firmware_dump;
804 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700805 legacy_hal_.lock()->requestFirmwareMemoryDump(
806 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700807 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
808 LOG(ERROR) << "Failed to get firmware debug dump: "
809 << legacyErrorToString(legacy_status);
810 return {createWifiStatusFromLegacyError(legacy_status), {}};
811 }
812 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700813}
814
815std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700816 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700817 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800818 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700819 std::string ifname = allocateApIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800820 legacy_hal::wifi_error legacy_status =
821 legacy_hal_.lock()->createVirtualInterface(
822 ifname,
823 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
824 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
825 LOG(ERROR) << "Failed to add interface: " << ifname << " "
826 << legacyErrorToString(legacy_status);
827 return {createWifiStatusFromLegacyError(legacy_status), {}};
828 }
Patrik Fimml6beae322019-10-09 17:34:01 +0200829 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700830 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700831 for (const auto& callback : event_cb_handler_.getCallbacks()) {
832 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
833 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
834 }
835 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700836 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700837 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700838}
839
840std::pair<WifiStatus, std::vector<hidl_string>>
841WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700842 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700843 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
844 }
Roshan Pius675609b2017-10-31 14:24:58 -0700845 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700846}
847
848std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800849 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700850 const auto iface = findUsingName(ap_ifaces_, ifname);
851 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700852 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
853 }
Roshan Pius675609b2017-10-31 14:24:58 -0700854 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700855}
856
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800857WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700858 const auto iface = findUsingName(ap_ifaces_, ifname);
859 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700860 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800861 }
Roshan Pius82368502019-05-16 12:53:02 -0700862 // Invalidate & remove any dependent objects first.
863 // Note: This is probably not required because we never create
864 // nan/rtt objects over AP iface. But, there is no harm to do it
865 // here and not make that assumption all over the place.
866 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800867 legacy_hal::wifi_error legacy_status =
868 legacy_hal_.lock()->deleteVirtualInterface(ifname);
869 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
870 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
871 << legacyErrorToString(legacy_status);
872 }
Roshan Pius675609b2017-10-31 14:24:58 -0700873 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700874 for (const auto& callback : event_cb_handler_.getCallbacks()) {
875 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
876 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
877 }
878 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700879 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700880 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800881}
882
Roshan Pius3c868522016-10-27 12:43:49 -0700883std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700884 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700885 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800886 }
Roshan Pius5ba0a902020-04-14 11:55:42 -0700887 bool is_dedicated_iface = true;
888 std::string ifname = getNanIfaceName();
Nate Jiang4ad7daa2020-05-18 15:17:47 -0700889 if (ifname.empty() || if_nametoindex(ifname.c_str())) {
Roshan Pius5ba0a902020-04-14 11:55:42 -0700890 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
891 // not defined.
892 ifname = getFirstActiveWlanIfaceName();
893 is_dedicated_iface = false;
894 }
895 sp<WifiNanIface> iface =
896 new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -0700897 nan_ifaces_.push_back(iface);
898 for (const auto& callback : event_cb_handler_.getCallbacks()) {
899 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
900 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
901 }
902 }
903 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700904}
905
906std::pair<WifiStatus, std::vector<hidl_string>>
907WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700908 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700909 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
910 }
Roshan Pius675609b2017-10-31 14:24:58 -0700911 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700912}
913
914std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800915 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700916 const auto iface = findUsingName(nan_ifaces_, ifname);
917 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700918 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
919 }
Roshan Pius675609b2017-10-31 14:24:58 -0700920 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700921}
922
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800923WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700924 const auto iface = findUsingName(nan_ifaces_, ifname);
925 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700926 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800927 }
Roshan Pius675609b2017-10-31 14:24:58 -0700928 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700929 for (const auto& callback : event_cb_handler_.getCallbacks()) {
930 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
931 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
932 }
933 }
934 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800935}
936
Roshan Pius3c868522016-10-27 12:43:49 -0700937std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700938 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700939 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800940 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700941 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700942 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
943 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700944 for (const auto& callback : event_cb_handler_.getCallbacks()) {
945 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
946 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
947 }
948 }
Roshan Pius675609b2017-10-31 14:24:58 -0700949 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700950}
951
952std::pair<WifiStatus, std::vector<hidl_string>>
953WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700954 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700955 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
956 }
Roshan Pius675609b2017-10-31 14:24:58 -0700957 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700958}
959
960std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800961 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700962 const auto iface = findUsingName(p2p_ifaces_, ifname);
963 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700964 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
965 }
Roshan Pius675609b2017-10-31 14:24:58 -0700966 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700967}
968
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800969WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700970 const auto iface = findUsingName(p2p_ifaces_, ifname);
971 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700972 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800973 }
Roshan Pius675609b2017-10-31 14:24:58 -0700974 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700975 for (const auto& callback : event_cb_handler_.getCallbacks()) {
976 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
977 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
978 }
979 }
980 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800981}
982
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -0800983std::pair<WifiStatus, sp<V1_3::IWifiStaIface>>
984WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700985 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700986 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800987 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700988 std::string ifname = allocateStaIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800989 legacy_hal::wifi_error legacy_status =
990 legacy_hal_.lock()->createVirtualInterface(
991 ifname,
992 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
993 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
994 LOG(ERROR) << "Failed to add interface: " << ifname << " "
995 << legacyErrorToString(legacy_status);
996 return {createWifiStatusFromLegacyError(legacy_status), {}};
997 }
Roshan Pius99dab382019-02-14 07:57:10 -0800998 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700999 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001000 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1001 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1002 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1003 }
1004 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001005 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001006 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001007}
1008
1009std::pair<WifiStatus, std::vector<hidl_string>>
1010WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001011 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001012 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1013 }
Roshan Pius675609b2017-10-31 14:24:58 -07001014 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001015}
1016
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001017std::pair<WifiStatus, sp<V1_3::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001018 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001019 const auto iface = findUsingName(sta_ifaces_, ifname);
1020 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001021 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1022 }
Roshan Pius675609b2017-10-31 14:24:58 -07001023 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001024}
1025
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001026WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001027 const auto iface = findUsingName(sta_ifaces_, ifname);
1028 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001029 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001030 }
Roshan Pius82368502019-05-16 12:53:02 -07001031 // Invalidate & remove any dependent objects first.
1032 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001033 legacy_hal::wifi_error legacy_status =
1034 legacy_hal_.lock()->deleteVirtualInterface(ifname);
1035 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1036 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1037 << legacyErrorToString(legacy_status);
1038 }
Roshan Pius675609b2017-10-31 14:24:58 -07001039 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001040 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1041 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1042 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1043 }
1044 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001045 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001046 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001047}
1048
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001049std::pair<WifiStatus, sp<V1_0::IWifiRttController>>
1050WifiChip::createRttControllerInternal(const sp<IWifiIface>& /*bound_iface*/) {
1051 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001052 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001053}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001054
1055std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1056WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001057 legacy_hal::wifi_error legacy_status;
1058 std::vector<legacy_hal::wifi_ring_buffer_status>
1059 legacy_ring_buffer_status_vec;
1060 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001061 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001062 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1063 return {createWifiStatusFromLegacyError(legacy_status), {}};
1064 }
1065 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1066 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1067 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1068 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1069 }
1070 return {createWifiStatus(WifiStatusCode::SUCCESS),
1071 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001072}
1073
1074WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001075 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1076 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1077 WifiStatus status = registerDebugRingBufferCallback();
1078 if (status.code != WifiStatusCode::SUCCESS) {
1079 return status;
1080 }
1081 legacy_hal::wifi_error legacy_status =
1082 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001083 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001084 static_cast<
1085 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1086 verbose_level),
1087 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001088 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1089 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001090 // if verbose logging enabled, turn up HAL daemon logging as well.
1091 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1092 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1093 } else {
1094 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1095 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001096 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001097}
1098
1099WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001100 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001101 WifiStatus status = registerDebugRingBufferCallback();
1102 if (status.code != WifiStatusCode::SUCCESS) {
1103 return status;
1104 }
1105 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001106 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1107 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001108
Roshan Piusabcf78f2017-10-06 16:30:38 -07001109 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001110}
1111
Roger Wangb294c762018-11-02 15:34:39 +08001112WifiStatus WifiChip::flushRingBufferToFileInternal() {
1113 if (!writeRingbufferFilesInternal()) {
1114 LOG(ERROR) << "Error writing files to flash";
1115 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1116 }
1117 return createWifiStatus(WifiStatusCode::SUCCESS);
1118}
1119
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001120WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001121 legacy_hal::wifi_error legacy_status =
1122 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001123 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001124 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001125}
1126
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001127std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1128WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001129 legacy_hal::wifi_error legacy_status;
1130 legacy_hal::WakeReasonStats legacy_stats;
1131 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001132 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001133 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1134 return {createWifiStatusFromLegacyError(legacy_status), {}};
1135 }
1136 WifiDebugHostWakeReasonStats hidl_stats;
1137 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1138 &hidl_stats)) {
1139 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1140 }
1141 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001142}
1143
Roshan Pius203cb032016-12-14 17:41:20 -08001144WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001145 legacy_hal::wifi_error legacy_status;
1146 if (enable) {
1147 android::wp<WifiChip> weak_ptr_this(this);
1148 const auto& on_alert_callback = [weak_ptr_this](
1149 int32_t error_code,
1150 std::vector<uint8_t> debug_data) {
1151 const auto shared_ptr_this = weak_ptr_this.promote();
1152 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1153 LOG(ERROR) << "Callback invoked on an invalid object";
1154 return;
1155 }
1156 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1157 if (!callback->onDebugErrorAlert(error_code, debug_data)
1158 .isOk()) {
1159 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1160 }
1161 }
1162 };
1163 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001164 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001165 } else {
1166 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001167 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001168 }
1169 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001170}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001171
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001172WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001173 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001174 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001175 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001176 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1177 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001178}
1179
Roshan Pius735ff432017-07-25 08:48:08 -07001180WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001181 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001182 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001183 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001184}
1185
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001186WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1187 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001188 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001189 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1190 return createWifiStatusFromLegacyError(legacy_status);
1191}
1192
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001193WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001194 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
1195 // Deprecated support for this callback.
1196 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001197}
1198
Jong Wook Kimda830c92018-07-23 15:29:38 -07001199WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1200 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001201 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001202 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001203 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1204 return createWifiStatusFromLegacyError(legacy_status);
1205}
1206
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001207std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
1208 legacy_hal::wifi_error legacy_status;
1209 uint32_t legacy_feature_set;
1210 uint32_t legacy_logger_feature_set;
1211 const auto ifname = getFirstActiveWlanIfaceName();
1212 std::tie(legacy_status, legacy_feature_set) =
1213 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
1214 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1215 return {createWifiStatusFromLegacyError(legacy_status), 0};
1216 }
1217 std::tie(legacy_status, legacy_logger_feature_set) =
1218 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
1219 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1220 // some devices don't support querying logger feature set
1221 legacy_logger_feature_set = 0;
1222 }
1223 uint32_t hidl_caps;
1224 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
1225 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
1226 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1227 }
1228 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1229}
1230
1231std::pair<WifiStatus, sp<IWifiRttController>>
1232WifiChip::createRttControllerInternal_1_4(const sp<IWifiIface>& bound_iface) {
1233 if (sta_ifaces_.size() == 0 &&
1234 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1235 LOG(ERROR)
1236 << "createRttControllerInternal_1_4: Chip cannot support STAs "
1237 "(and RTT by extension)";
1238 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1239 }
1240 sp<WifiRttController> rtt = new WifiRttController(
1241 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1242 rtt_controllers_.emplace_back(rtt);
1243 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1244}
1245
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001246WifiStatus WifiChip::registerEventCallbackInternal_1_4(
1247 const sp<IWifiChipEventCallback>& event_callback) {
1248 if (!event_cb_handler_.addCallback(event_callback)) {
1249 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1250 }
1251 return createWifiStatus(WifiStatusCode::SUCCESS);
1252}
1253
Roshan Piusba38d9c2017-12-08 07:32:08 -08001254WifiStatus WifiChip::handleChipConfiguration(
1255 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1256 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001257 // If the chip is already configured in a different mode, stop
1258 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001259 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001260 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1261 << " to mode " << mode_id;
1262 invalidateAndRemoveAllIfaces();
1263 legacy_hal::wifi_error legacy_status =
1264 legacy_hal_.lock()->stop(lock, []() {});
1265 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1266 LOG(ERROR) << "Failed to stop legacy HAL: "
1267 << legacyErrorToString(legacy_status);
1268 return createWifiStatusFromLegacyError(legacy_status);
1269 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001270 }
Roshan Piuscc338202017-11-02 13:54:09 -07001271 // Firmware mode change not needed for V2 devices.
1272 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001273 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001274 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001275 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001276 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1277 }
1278 if (!success) {
1279 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1280 }
1281 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1282 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1283 LOG(ERROR) << "Failed to start legacy HAL: "
1284 << legacyErrorToString(legacy_status);
1285 return createWifiStatusFromLegacyError(legacy_status);
1286 }
Roshan Pius85c64412018-01-22 17:58:40 -08001287 // Every time the HAL is restarted, we need to register the
1288 // radio mode change callback.
1289 WifiStatus status = registerRadioModeChangeCallback();
1290 if (status.code != WifiStatusCode::SUCCESS) {
1291 // This probably is not a critical failure?
1292 LOG(ERROR) << "Failed to register radio mode change callback";
1293 }
chenpaulf5eca292019-03-14 11:08:03 +08001294 // Extract and save the version information into property.
1295 std::pair<WifiStatus, IWifiChip::ChipDebugInfo> version_info;
1296 version_info = WifiChip::requestChipDebugInfoInternal();
1297 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1298 property_set("vendor.wlan.firmware.version",
1299 version_info.second.firmwareDescription.c_str());
1300 property_set("vendor.wlan.driver.version",
1301 version_info.second.driverDescription.c_str());
1302 }
1303
Roshan Piusabcf78f2017-10-06 16:30:38 -07001304 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001305}
Roshan Pius48185b22016-12-15 19:10:30 -08001306
1307WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001308 if (debug_ring_buffer_cb_registered_) {
1309 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001310 }
Roshan Pius3797e182017-03-30 18:01:54 -07001311
Roshan Piusabcf78f2017-10-06 16:30:38 -07001312 android::wp<WifiChip> weak_ptr_this(this);
1313 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001314 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001315 const std::vector<uint8_t>& data,
1316 const legacy_hal::wifi_ring_buffer_status& status) {
1317 const auto shared_ptr_this = weak_ptr_this.promote();
1318 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1319 LOG(ERROR) << "Callback invoked on an invalid object";
1320 return;
1321 }
1322 WifiDebugRingBufferStatus hidl_status;
1323 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1324 status, &hidl_status)) {
1325 LOG(ERROR) << "Error converting ring buffer status";
1326 return;
1327 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301328 {
1329 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1330 const auto& target =
1331 shared_ptr_this->ringbuffer_map_.find(name);
1332 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1333 Ringbuffer& cur_buffer = target->second;
1334 cur_buffer.append(data);
1335 } else {
1336 LOG(ERROR) << "Ringname " << name << " not found";
1337 return;
1338 }
1339 // unlock
Roshan Piusabcf78f2017-10-06 16:30:38 -07001340 }
1341 };
1342 legacy_hal::wifi_error legacy_status =
1343 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001344 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001345
Roshan Piusabcf78f2017-10-06 16:30:38 -07001346 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1347 debug_ring_buffer_cb_registered_ = true;
1348 }
1349 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001350}
1351
Roshan Pius85c64412018-01-22 17:58:40 -08001352WifiStatus WifiChip::registerRadioModeChangeCallback() {
1353 android::wp<WifiChip> weak_ptr_this(this);
1354 const auto& on_radio_mode_change_callback =
1355 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1356 const auto shared_ptr_this = weak_ptr_this.promote();
1357 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1358 LOG(ERROR) << "Callback invoked on an invalid object";
1359 return;
1360 }
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001361 std::vector<IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001362 hidl_radio_mode_infos;
1363 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1364 mac_infos, &hidl_radio_mode_infos)) {
1365 LOG(ERROR) << "Error converting wifi mac info";
1366 return;
1367 }
1368 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001369 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos)
Roshan Pius85c64412018-01-22 17:58:40 -08001370 .isOk()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001371 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
Roshan Pius85c64412018-01-22 17:58:40 -08001372 << " callback on: " << toString(callback);
1373 }
1374 }
1375 };
1376 legacy_hal::wifi_error legacy_status =
1377 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001378 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001379 return createWifiStatusFromLegacyError(legacy_status);
1380}
1381
Roshan Piuscc338202017-11-02 13:54:09 -07001382std::vector<IWifiChip::ChipIfaceCombination>
1383WifiChip::getCurrentModeIfaceCombinations() {
1384 if (!isValidModeId(current_mode_id_)) {
1385 LOG(ERROR) << "Chip not configured in a mode yet";
1386 return {};
1387 }
1388 for (const auto& mode : modes_) {
1389 if (mode.id == current_mode_id_) {
1390 return mode.availableCombinations;
1391 }
1392 }
1393 CHECK(0) << "Expected to find iface combinations for current mode!";
1394 return {};
1395}
1396
1397// Returns a map indexed by IfaceType with the number of ifaces currently
1398// created of the corresponding type.
1399std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1400 std::map<IfaceType, size_t> iface_counts;
1401 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1402 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1403 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1404 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1405 return iface_counts;
1406}
1407
1408// This expands the provided iface combinations to a more parseable
1409// form. Returns a vector of available combinations possible with the number
1410// of ifaces of each type in the combination.
1411// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1412std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1413 const IWifiChip::ChipIfaceCombination& combination) {
1414 uint32_t num_expanded_combos = 1;
1415 for (const auto& limit : combination.limits) {
1416 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1417 num_expanded_combos *= limit.types.size();
1418 }
1419 }
1420
1421 // Allocate the vector of expanded combos and reset all iface counts to 0
1422 // in each combo.
1423 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1424 expanded_combos.resize(num_expanded_combos);
1425 for (auto& expanded_combo : expanded_combos) {
1426 for (const auto type :
1427 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1428 expanded_combo[type] = 0;
1429 }
1430 }
1431 uint32_t span = num_expanded_combos;
1432 for (const auto& limit : combination.limits) {
1433 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1434 span /= limit.types.size();
1435 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1436 const auto iface_type =
1437 limit.types[(k / span) % limit.types.size()];
1438 expanded_combos[k][iface_type]++;
1439 }
1440 }
1441 }
1442 return expanded_combos;
1443}
1444
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001445bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1446 const std::map<IfaceType, size_t>& expanded_combo,
1447 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001448 const auto current_combo = getCurrentIfaceCombination();
1449
1450 // Check if we have space for 1 more iface of |type| in this combo
1451 for (const auto type :
1452 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1453 size_t num_ifaces_needed = current_combo.at(type);
1454 if (type == requested_type) {
1455 num_ifaces_needed++;
1456 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001457 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001458 if (num_ifaces_needed > num_ifaces_allowed) {
1459 return false;
1460 }
1461 }
1462 return true;
1463}
1464
1465// This method does the following:
1466// a) Enumerate all possible iface combos by expanding the current
1467// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001468// b) Check if the requested iface type can be added to the current mode
1469// with the iface combination that is already active.
1470bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1471 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001472 if (!isValidModeId(current_mode_id_)) {
1473 LOG(ERROR) << "Chip not configured in a mode yet";
1474 return false;
1475 }
1476 const auto combinations = getCurrentModeIfaceCombinations();
1477 for (const auto& combination : combinations) {
1478 const auto expanded_combos = expandIfaceCombinations(combination);
1479 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001480 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1481 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001482 return true;
1483 }
1484 }
1485 }
1486 return false;
1487}
1488
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001489// Note: This does not consider ifaces already active. It only checks if the
1490// provided expanded iface combination can support the requested combo.
1491bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1492 const std::map<IfaceType, size_t>& expanded_combo,
1493 const std::map<IfaceType, size_t>& req_combo) {
1494 // Check if we have space for 1 more iface of |type| in this combo
1495 for (const auto type :
1496 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1497 if (req_combo.count(type) == 0) {
1498 // Iface of "type" not in the req_combo.
1499 continue;
1500 }
1501 size_t num_ifaces_needed = req_combo.at(type);
1502 size_t num_ifaces_allowed = expanded_combo.at(type);
1503 if (num_ifaces_needed > num_ifaces_allowed) {
1504 return false;
1505 }
1506 }
1507 return true;
1508}
1509// This method does the following:
1510// a) Enumerate all possible iface combos by expanding the current
1511// ChipIfaceCombination.
1512// b) Check if the requested iface combo can be added to the current mode.
1513// Note: This does not consider ifaces already active. It only checks if the
1514// current mode can support the requested combo.
1515bool WifiChip::canCurrentModeSupportIfaceCombo(
1516 const std::map<IfaceType, size_t>& req_combo) {
1517 if (!isValidModeId(current_mode_id_)) {
1518 LOG(ERROR) << "Chip not configured in a mode yet";
1519 return false;
1520 }
1521 const auto combinations = getCurrentModeIfaceCombinations();
1522 for (const auto& combination : combinations) {
1523 const auto expanded_combos = expandIfaceCombinations(combination);
1524 for (const auto& expanded_combo : expanded_combos) {
1525 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1526 req_combo)) {
1527 return true;
1528 }
1529 }
1530 }
1531 return false;
1532}
1533
1534// This method does the following:
1535// a) Enumerate all possible iface combos by expanding the current
1536// ChipIfaceCombination.
1537// b) Check if the requested iface type can be added to the current mode.
1538bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1539 // Check if we can support atleast 1 iface of type.
1540 std::map<IfaceType, size_t> req_iface_combo;
1541 req_iface_combo[requested_type] = 1;
1542 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1543}
1544
Roshan Piuscc338202017-11-02 13:54:09 -07001545bool WifiChip::isValidModeId(ChipModeId mode_id) {
1546 for (const auto& mode : modes_) {
1547 if (mode.id == mode_id) {
1548 return true;
1549 }
1550 }
1551 return false;
1552}
1553
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001554bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1555 // Check if we can support atleast 1 STA & 1 AP concurrently.
1556 std::map<IfaceType, size_t> req_iface_combo;
1557 req_iface_combo[IfaceType::AP] = 1;
1558 req_iface_combo[IfaceType::STA] = 1;
1559 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1560}
1561
James Mattisd2e4c072019-05-22 16:14:48 -07001562bool WifiChip::isDualApAllowedInCurrentMode() {
1563 // Check if we can support atleast 1 STA & 1 AP concurrently.
1564 std::map<IfaceType, size_t> req_iface_combo;
1565 req_iface_combo[IfaceType::AP] = 2;
1566 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1567}
1568
Roshan Pius6036c022019-03-27 10:41:58 -07001569std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001570 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1571 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001572 // This could happen if the chip call is made before any STA/AP
1573 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001574 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Roshan Pius6036c022019-03-27 10:41:58 -07001575 return getWlanIfaceName(0);
1576}
1577
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001578// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1579// not already in use.
1580// Note: This doesn't check the actual presence of these interfaces.
1581std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1582 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1583 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001584 if (findUsingName(ap_ifaces_, ifname)) continue;
1585 if (findUsingName(sta_ifaces_, ifname)) continue;
1586 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001587 }
1588 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001589 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001590 return {};
1591}
1592
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001593// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001594// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001595std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001596 // Check if we have a dedicated iface for AP.
1597 std::string ifname = getApIfaceName();
1598 if (!ifname.empty()) {
1599 return ifname;
1600 }
James Mattisd2e4c072019-05-22 16:14:48 -07001601 return allocateApOrStaIfaceName((isStaApConcurrencyAllowedInCurrentMode() &&
1602 !isDualApAllowedInCurrentMode())
1603 ? 1
1604 : 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001605}
1606
1607// STA iface names start with idx 0.
1608// Primary STA iface will always be 0.
1609std::string WifiChip::allocateStaIfaceName() {
1610 return allocateApOrStaIfaceName(0);
1611}
1612
xshu5899e8e2018-01-09 15:36:03 -08001613bool WifiChip::writeRingbufferFilesInternal() {
1614 if (!removeOldFilesInternal()) {
1615 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1616 return false;
1617 }
1618 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301619 {
1620 std::unique_lock<std::mutex> lk(lock_t);
1621 for (const auto& item : ringbuffer_map_) {
1622 const Ringbuffer& cur_buffer = item.second;
1623 if (cur_buffer.getData().empty()) {
1624 continue;
1625 }
1626 const std::string file_path_raw =
1627 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1628 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1629 if (dump_fd == -1) {
1630 PLOG(ERROR) << "create file failed";
1631 return false;
1632 }
1633 unique_fd file_auto_closer(dump_fd);
1634 for (const auto& cur_block : cur_buffer.getData()) {
1635 if (write(dump_fd, cur_block.data(),
1636 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1637 PLOG(ERROR) << "Error writing to file";
1638 }
xshu5899e8e2018-01-09 15:36:03 -08001639 }
1640 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301641 // unlock
xshu5899e8e2018-01-09 15:36:03 -08001642 }
1643 return true;
1644}
1645
Roshan Pius79a99752016-10-04 13:03:58 -07001646} // namespace implementation
Ahmed ElArabawyf501a982019-07-23 15:02:22 -07001647} // namespace V1_4
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001648} // namespace wifi
1649} // namespace hardware
1650} // namespace android