blob: 957b22b4a7f58ca582e3fb0f9acdf334da26583c [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>
xshu5899e8e2018-01-09 15:36:03 -080022#include <sys/stat.h>
23#include <sys/sysmacros.h>
Roshan Pius3c4e8a32016-10-03 14:53:58 -070024
Roshan Pius3c868522016-10-27 12:43:49 -070025#include "hidl_return_util.h"
Roshan Piuse2d0ab52016-12-05 15:24:20 -080026#include "hidl_struct_util.h"
Roshan Pius3c868522016-10-27 12:43:49 -070027#include "wifi_chip.h"
Roshan Pius5c055462016-10-11 08:27:27 -070028#include "wifi_status_util.h"
Roshan Pius3c4e8a32016-10-03 14:53:58 -070029
Roshan Pius35d958c2016-10-06 16:47:38 -070030namespace {
Jong Wook Kimda830c92018-07-23 15:29:38 -070031using android::sp;
xshu5899e8e2018-01-09 15:36:03 -080032using android::base::unique_fd;
Roshan Pius35d958c2016-10-06 16:47:38 -070033using android::hardware::hidl_string;
Roshan Piusabcf78f2017-10-06 16:30:38 -070034using android::hardware::hidl_vec;
Roshan Pius2c06a3f2016-12-15 17:51:40 -080035using android::hardware::wifi::V1_0::ChipModeId;
Roshan Pius52947fb2016-11-18 11:38:07 -080036using android::hardware::wifi::V1_0::IfaceType;
Roshan Pius675609b2017-10-31 14:24:58 -070037using android::hardware::wifi::V1_0::IWifiChip;
Roshan Pius52947fb2016-11-18 11:38:07 -080038
xshu5899e8e2018-01-09 15:36:03 -080039constexpr char kCpioMagic[] = "070701";
Roger Wangb294c762018-11-02 15:34:39 +080040constexpr size_t kMaxBufferSizeBytes = 1024 * 1024 * 3;
41constexpr uint32_t kMaxRingBufferFileAgeSeconds = 60 * 60 * 10;
xshu37126c92018-04-13 16:24:45 -070042constexpr uint32_t kMaxRingBufferFileNum = 20;
xshu5899e8e2018-01-09 15:36:03 -080043constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080044constexpr unsigned kMaxWlanIfaces = 100;
xshu5899e8e2018-01-09 15:36:03 -080045
Roshan Pius35d958c2016-10-06 16:47:38 -070046template <typename Iface>
Roshan Pius675609b2017-10-31 14:24:58 -070047void invalidateAndClear(std::vector<sp<Iface>>& ifaces, sp<Iface> iface) {
48 iface->invalidate();
49 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface),
50 ifaces.end());
51}
52
53template <typename Iface>
54void invalidateAndClearAll(std::vector<sp<Iface>>& ifaces) {
55 for (const auto& iface : ifaces) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070056 iface->invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -070057 }
Roshan Pius675609b2017-10-31 14:24:58 -070058 ifaces.clear();
59}
60
61template <typename Iface>
62std::vector<hidl_string> getNames(std::vector<sp<Iface>>& ifaces) {
63 std::vector<hidl_string> names;
64 for (const auto& iface : ifaces) {
65 names.emplace_back(iface->getName());
66 }
67 return names;
68}
69
70template <typename Iface>
71sp<Iface> findUsingName(std::vector<sp<Iface>>& ifaces,
72 const std::string& name) {
73 std::vector<hidl_string> names;
74 for (const auto& iface : ifaces) {
75 if (name == iface->getName()) {
76 return iface;
77 }
78 }
79 return nullptr;
Roshan Pius35d958c2016-10-06 16:47:38 -070080}
Roshan Pius9377a0d2017-10-06 13:18:54 -070081
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080082std::string getWlanIfaceName(unsigned idx) {
83 if (idx >= kMaxWlanIfaces) {
84 CHECK(false) << "Requested interface beyond wlan" << kMaxWlanIfaces;
85 return {};
86 }
Roshan Pius9377a0d2017-10-06 13:18:54 -070087
Roshan Pius8e3c7ef2017-11-03 09:43:08 -070088 std::array<char, PROPERTY_VALUE_MAX> buffer;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080089 if (idx == 0 || idx == 1) {
90 const char* altPropName =
91 (idx == 0) ? "wifi.interface" : "wifi.concurrent.interface";
Roshan Pius5b333462019-03-01 14:07:22 -080092 auto res = property_get(altPropName, buffer.data(), nullptr);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080093 if (res > 0) return buffer.data();
94 }
Roshan Pius5b333462019-03-01 14:07:22 -080095 std::string propName = "wifi.interface." + std::to_string(idx);
96 auto res = property_get(propName.c_str(), buffer.data(), nullptr);
97 if (res > 0) return buffer.data();
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080098
99 return "wlan" + std::to_string(idx);
Roshan Pius9377a0d2017-10-06 13:18:54 -0700100}
Roshan Pius9377a0d2017-10-06 13:18:54 -0700101
102std::string getP2pIfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700103 std::array<char, PROPERTY_VALUE_MAX> buffer;
104 property_get("wifi.direct.interface", buffer.data(), "p2p0");
105 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700106}
107
xshu37126c92018-04-13 16:24:45 -0700108// delete files that meet either conditions:
109// 1. older than a predefined time in the wifi tombstone dir.
110// 2. Files in excess to a predefined amount, starting from the oldest ones
xshu5899e8e2018-01-09 15:36:03 -0800111bool removeOldFilesInternal() {
112 time_t now = time(0);
113 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
Josh Gaoa568e532018-06-04 18:16:00 -0700114 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(
115 opendir(kTombstoneFolderPath), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800116 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800117 PLOG(ERROR) << "Failed to open directory";
xshu5899e8e2018-01-09 15:36:03 -0800118 return false;
119 }
xshu5899e8e2018-01-09 15:36:03 -0800120 struct dirent* dp;
121 bool success = true;
xshu37126c92018-04-13 16:24:45 -0700122 std::list<std::pair<const time_t, std::string>> valid_files;
Josh Gaoa568e532018-06-04 18:16:00 -0700123 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800124 if (dp->d_type != DT_REG) {
125 continue;
126 }
127 std::string cur_file_name(dp->d_name);
128 struct stat cur_file_stat;
129 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800130 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800131 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800132 success = false;
xshu4cb33162018-01-24 15:40:06 -0800133 continue;
134 }
xshu37126c92018-04-13 16:24:45 -0700135 const time_t cur_file_time = cur_file_stat.st_mtime;
136 valid_files.push_back(
137 std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
138 }
139 valid_files.sort(); // sort the list of files by last modified time from
140 // small to big.
141 uint32_t cur_file_count = valid_files.size();
142 for (auto cur_file : valid_files) {
143 if (cur_file_count > kMaxRingBufferFileNum ||
144 cur_file.first < delete_files_before) {
145 if (unlink(cur_file.second.c_str()) != 0) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800146 PLOG(ERROR) << "Error deleting file";
xshu37126c92018-04-13 16:24:45 -0700147 success = false;
148 }
149 cur_file_count--;
150 } else {
151 break;
xshu5899e8e2018-01-09 15:36:03 -0800152 }
153 }
154 return success;
155}
156
xshu4cb33162018-01-24 15:40:06 -0800157// Helper function for |cpioArchiveFilesInDir|
158bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name,
159 size_t file_name_len) {
160 std::array<char, 32 * 1024> read_buf;
161 ssize_t llen =
162 sprintf(read_buf.data(),
163 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
164 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
165 st.st_gid, static_cast<int>(st.st_nlink),
166 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
167 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
168 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
169 if (write(out_fd, read_buf.data(), llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800170 PLOG(ERROR) << "Error writing cpio header to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800171 return false;
172 }
173 if (write(out_fd, file_name, file_name_len) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800174 PLOG(ERROR) << "Error writing filename to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800175 return false;
176 }
177
178 // NUL Pad header up to 4 multiple bytes.
179 llen = (llen + file_name_len) % 4;
180 if (llen != 0) {
181 const uint32_t zero = 0;
182 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800183 PLOG(ERROR) << "Error padding 0s to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800184 return false;
185 }
186 }
187 return true;
188}
189
190// Helper function for |cpioArchiveFilesInDir|
191size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
192 // writing content of file
193 std::array<char, 32 * 1024> read_buf;
194 ssize_t llen = st.st_size;
195 size_t n_error = 0;
196 while (llen > 0) {
197 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
198 if (bytes_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800199 PLOG(ERROR) << "Error reading file";
xshu4cb33162018-01-24 15:40:06 -0800200 return ++n_error;
201 }
202 llen -= bytes_read;
203 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800204 PLOG(ERROR) << "Error writing data to file";
xshu4cb33162018-01-24 15:40:06 -0800205 return ++n_error;
206 }
207 if (bytes_read == 0) { // this should never happen, but just in case
208 // to unstuck from while loop
Elliott Hughes4db4add2019-03-08 12:42:57 -0800209 PLOG(ERROR) << "Unexpected read result";
xshu4cb33162018-01-24 15:40:06 -0800210 n_error++;
211 break;
212 }
213 }
214 llen = st.st_size % 4;
215 if (llen != 0) {
216 const uint32_t zero = 0;
217 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800218 PLOG(ERROR) << "Error padding 0s to file";
xshu4cb33162018-01-24 15:40:06 -0800219 return ++n_error;
220 }
221 }
222 return n_error;
223}
224
225// Helper function for |cpioArchiveFilesInDir|
226bool cpioWriteFileTrailer(int out_fd) {
227 std::array<char, 4096> read_buf;
228 read_buf.fill(0);
229 if (write(out_fd, read_buf.data(),
230 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1,
231 0x0b, 0) +
232 4) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800233 PLOG(ERROR) << "Error writing trailing bytes";
xshu4cb33162018-01-24 15:40:06 -0800234 return false;
235 }
236 return true;
237}
238
xshu5899e8e2018-01-09 15:36:03 -0800239// Archives all files in |input_dir| and writes result into |out_fd|
240// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
241// portion
xshu4cb33162018-01-24 15:40:06 -0800242size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800243 struct dirent* dp;
244 size_t n_error = 0;
Josh Gaoa568e532018-06-04 18:16:00 -0700245 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir),
246 closedir);
xshu5899e8e2018-01-09 15:36:03 -0800247 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800248 PLOG(ERROR) << "Failed to open directory";
xshu4cb33162018-01-24 15:40:06 -0800249 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800250 }
Josh Gaoa568e532018-06-04 18:16:00 -0700251 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800252 if (dp->d_type != DT_REG) {
253 continue;
254 }
255 std::string cur_file_name(dp->d_name);
xshu4cb33162018-01-24 15:40:06 -0800256 // string.size() does not include the null terminator. The cpio FreeBSD
257 // file header expects the null character to be included in the length.
258 const size_t file_name_len = cur_file_name.size() + 1;
xshu5899e8e2018-01-09 15:36:03 -0800259 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800260 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800261 if (stat(cur_file_path.c_str(), &st) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800262 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800263 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800264 continue;
265 }
266 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
267 if (fd_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800268 PLOG(ERROR) << "Failed to open file " << cur_file_path;
xshu4cb33162018-01-24 15:40:06 -0800269 n_error++;
270 continue;
271 }
272 unique_fd file_auto_closer(fd_read);
273 if (!cpioWriteHeader(out_fd, st, cur_file_name.c_str(),
274 file_name_len)) {
275 return ++n_error;
276 }
277 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
278 if (write_error) {
279 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800280 }
281 }
xshu4cb33162018-01-24 15:40:06 -0800282 if (!cpioWriteFileTrailer(out_fd)) {
283 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800284 }
285 return n_error;
286}
287
288// Helper function to create a non-const char*.
289std::vector<char> makeCharVec(const std::string& str) {
290 std::vector<char> vec(str.size() + 1);
291 vec.assign(str.begin(), str.end());
292 vec.push_back('\0');
293 return vec;
294}
295
Roshan Piusabcf78f2017-10-06 16:30:38 -0700296} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700297
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700298namespace android {
299namespace hardware {
300namespace wifi {
Jong Wook Kimda830c92018-07-23 15:29:38 -0700301namespace V1_3 {
Roshan Pius79a99752016-10-04 13:03:58 -0700302namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700303using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800304using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700305
Roshan Pius52947fb2016-11-18 11:38:07 -0800306WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700307 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700308 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800309 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Roshan Pius200a17d2017-11-01 13:03:35 -0700310 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Pius52947fb2016-11-18 11:38:07 -0800311 : chip_id_(chip_id),
312 legacy_hal_(legacy_hal),
313 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800314 iface_util_(iface_util),
Roshan Pius200a17d2017-11-01 13:03:35 -0700315 feature_flags_(feature_flags),
Roshan Pius52947fb2016-11-18 11:38:07 -0800316 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800317 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
318 modes_(feature_flags.lock()->getChipModes()),
319 debug_ring_buffer_cb_registered_(false) {}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700320
Roshan Piusaabe5752016-09-29 09:03:59 -0700321void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700322 if (!writeRingbufferFilesInternal()) {
323 LOG(ERROR) << "Error writing files to flash";
324 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700325 invalidateAndRemoveAllIfaces();
326 legacy_hal_.reset();
327 event_cb_handler_.invalidate();
328 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700329}
330
Roshan Piusabcf78f2017-10-06 16:30:38 -0700331bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700332
Jong Wook Kimda830c92018-07-23 15:29:38 -0700333std::set<sp<V1_2::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700334 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800335}
336
Roshan Pius5c055462016-10-11 08:27:27 -0700337Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700338 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
339 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700340}
341
Jong Wook Kimda830c92018-07-23 15:29:38 -0700342// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700343Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800344 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700345 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700346 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
347 &WifiChip::registerEventCallbackInternal,
348 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700349}
350
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700351Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700352 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
353 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700354}
355
Roshan Pius5c055462016-10-11 08:27:27 -0700356Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700357 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
358 &WifiChip::getAvailableModesInternal,
359 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700360}
361
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800362Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700363 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800364 return validateAndCallWithLock(
365 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
366 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700367}
368
Roshan Pius5c055462016-10-11 08:27:27 -0700369Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700370 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
371 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700372}
373
Roshan Pius5c055462016-10-11 08:27:27 -0700374Return<void> WifiChip::requestChipDebugInfo(
375 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700376 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
377 &WifiChip::requestChipDebugInfoInternal,
378 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700379}
380
381Return<void> WifiChip::requestDriverDebugDump(
382 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700383 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
384 &WifiChip::requestDriverDebugDumpInternal,
385 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700386}
387
Roshan Pius5c055462016-10-11 08:27:27 -0700388Return<void> WifiChip::requestFirmwareDebugDump(
389 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700390 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
391 &WifiChip::requestFirmwareDebugDumpInternal,
392 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700393}
394
Roshan Pius5c055462016-10-11 08:27:27 -0700395Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700396 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
397 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700398}
399
Roshan Pius5c055462016-10-11 08:27:27 -0700400Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700401 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
402 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700403}
404
Roshan Pius5c055462016-10-11 08:27:27 -0700405Return<void> WifiChip::getApIface(const hidl_string& ifname,
406 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700407 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
408 &WifiChip::getApIfaceInternal, hidl_status_cb,
409 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700410}
411
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800412Return<void> WifiChip::removeApIface(const hidl_string& ifname,
413 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700414 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
415 &WifiChip::removeApIfaceInternal, hidl_status_cb,
416 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800417}
418
Roshan Pius5c055462016-10-11 08:27:27 -0700419Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700420 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
421 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700422}
423
Roshan Pius5c055462016-10-11 08:27:27 -0700424Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700425 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
426 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700427}
428
429Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700430 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700431 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
432 &WifiChip::getNanIfaceInternal, hidl_status_cb,
433 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700434}
435
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800436Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
437 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700438 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
439 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
440 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800441}
442
Roshan Pius5c055462016-10-11 08:27:27 -0700443Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700444 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
445 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700446}
447
Roshan Pius5c055462016-10-11 08:27:27 -0700448Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700449 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
450 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700451}
452
453Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700454 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700455 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
456 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
457 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700458}
459
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800460Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
461 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700462 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
463 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
464 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800465}
466
Roshan Pius5c055462016-10-11 08:27:27 -0700467Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700468 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
469 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700470}
471
Roshan Pius5c055462016-10-11 08:27:27 -0700472Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700473 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
474 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700475}
476
477Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700478 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700479 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
480 &WifiChip::getStaIfaceInternal, hidl_status_cb,
481 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700482}
483
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800484Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
485 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700486 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
487 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
488 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800489}
490
Roshan Pius5c055462016-10-11 08:27:27 -0700491Return<void> WifiChip::createRttController(
492 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700493 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
494 &WifiChip::createRttControllerInternal,
495 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700496}
497
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700498Return<void> WifiChip::getDebugRingBuffersStatus(
499 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700500 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
501 &WifiChip::getDebugRingBuffersStatusInternal,
502 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700503}
504
505Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700506 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
507 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700508 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700509 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
510 &WifiChip::startLoggingToDebugRingBufferInternal,
511 hidl_status_cb, ring_name, verbose_level,
512 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700513}
514
515Return<void> WifiChip::forceDumpToDebugRingBuffer(
516 const hidl_string& ring_name,
517 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700518 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
519 &WifiChip::forceDumpToDebugRingBufferInternal,
520 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700521}
522
Roger Wangb294c762018-11-02 15:34:39 +0800523Return<void> WifiChip::flushRingBufferToFile(
524 flushRingBufferToFile_cb hidl_status_cb) {
525 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
526 &WifiChip::flushRingBufferToFileInternal,
527 hidl_status_cb);
528}
529
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800530Return<void> WifiChip::stopLoggingToDebugRingBuffer(
531 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700532 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
533 &WifiChip::stopLoggingToDebugRingBufferInternal,
534 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800535}
536
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700537Return<void> WifiChip::getDebugHostWakeReasonStats(
538 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700539 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
540 &WifiChip::getDebugHostWakeReasonStatsInternal,
541 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700542}
543
Roshan Pius203cb032016-12-14 17:41:20 -0800544Return<void> WifiChip::enableDebugErrorAlerts(
545 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700546 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
547 &WifiChip::enableDebugErrorAlertsInternal,
548 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800549}
550
Roshan Pius735ff432017-07-25 08:48:08 -0700551Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700552 V1_1::IWifiChip::TxPowerScenario scenario,
553 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700554 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
555 &WifiChip::selectTxPowerScenarioInternal,
556 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700557}
558
Roshan Pius735ff432017-07-25 08:48:08 -0700559Return<void> WifiChip::resetTxPowerScenario(
560 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700561 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
562 &WifiChip::resetTxPowerScenarioInternal,
563 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700564}
565
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700566Return<void> WifiChip::setLatencyMode(LatencyMode mode,
567 setLatencyMode_cb hidl_status_cb) {
568 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
569 &WifiChip::setLatencyModeInternal, hidl_status_cb,
570 mode);
571}
572
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800573Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700574 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800575 registerEventCallback_cb hidl_status_cb) {
576 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
577 &WifiChip::registerEventCallbackInternal_1_2,
578 hidl_status_cb, event_callback);
579}
580
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800581Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700582 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800583 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700584 &WifiChip::selectTxPowerScenarioInternal_1_2,
585 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800586}
587
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700588Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
589 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
590 &WifiChip::getCapabilitiesInternal_1_3,
591 hidl_status_cb);
592}
593
xshu5899e8e2018-01-09 15:36:03 -0800594Return<void> WifiChip::debug(const hidl_handle& handle,
595 const hidl_vec<hidl_string>&) {
596 if (handle != nullptr && handle->numFds >= 1) {
597 int fd = handle->data[0];
598 if (!writeRingbufferFilesInternal()) {
599 LOG(ERROR) << "Error writing files to flash";
600 }
xshu4cb33162018-01-24 15:40:06 -0800601 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800602 if (n_error != 0) {
603 LOG(ERROR) << n_error << " errors occured in cpio function";
604 }
605 fsync(fd);
606 } else {
607 LOG(ERROR) << "File handle error";
608 }
609 return Void();
610}
611
Roshan Pius35d958c2016-10-06 16:47:38 -0700612void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700613 invalidateAndClearAll(ap_ifaces_);
614 invalidateAndClearAll(nan_ifaces_);
615 invalidateAndClearAll(p2p_ifaces_);
616 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700617 // Since all the ifaces are invalid now, all RTT controller objects
618 // using those ifaces also need to be invalidated.
619 for (const auto& rtt : rtt_controllers_) {
620 rtt->invalidate();
621 }
622 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700623}
624
Roshan Pius3c868522016-10-27 12:43:49 -0700625std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700626 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700627}
628
629WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800630 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
631 // Deprecated support for this callback.
632 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700633}
634
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700635std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700636 // Deprecated support for this callback.
637 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
638}
639
640std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700641 legacy_hal::wifi_error legacy_status;
642 uint32_t legacy_feature_set;
643 uint32_t legacy_logger_feature_set;
Roshan Pius6036c022019-03-27 10:41:58 -0700644 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700645 std::tie(legacy_status, legacy_feature_set) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800646 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700647 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
648 return {createWifiStatusFromLegacyError(legacy_status), 0};
649 }
650 std::tie(legacy_status, legacy_logger_feature_set) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800651 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700652 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius876220f2017-12-28 16:19:06 -0800653 // some devices don't support querying logger feature set
654 legacy_logger_feature_set = 0;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700655 }
656 uint32_t hidl_caps;
657 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
658 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
659 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
660 }
661 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700662}
663
Roshan Pius3c868522016-10-27 12:43:49 -0700664std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
665WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700666 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700667}
668
Roshan Piusba38d9c2017-12-08 07:32:08 -0800669WifiStatus WifiChip::configureChipInternal(
670 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
671 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700672 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700673 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
674 }
675 if (mode_id == current_mode_id_) {
676 LOG(DEBUG) << "Already in the specified mode " << mode_id;
677 return createWifiStatus(WifiStatusCode::SUCCESS);
678 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800679 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700680 if (status.code != WifiStatusCode::SUCCESS) {
681 for (const auto& callback : event_cb_handler_.getCallbacks()) {
682 if (!callback->onChipReconfigureFailure(status).isOk()) {
683 LOG(ERROR)
684 << "Failed to invoke onChipReconfigureFailure callback";
685 }
686 }
687 return status;
688 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800689 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700690 if (!callback->onChipReconfigured(mode_id).isOk()) {
691 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
692 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800693 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700694 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800695 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800696 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700697}
698
699std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700700 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700701 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
702 current_mode_id_};
703 }
704 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700705}
706
707std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
708WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700709 IWifiChip::ChipDebugInfo result;
710 legacy_hal::wifi_error legacy_status;
711 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700712 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700713 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800714 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700715 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
716 LOG(ERROR) << "Failed to get driver version: "
717 << legacyErrorToString(legacy_status);
718 WifiStatus status = createWifiStatusFromLegacyError(
719 legacy_status, "failed to get driver version");
720 return {status, result};
721 }
722 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700723
Roshan Piusabcf78f2017-10-06 16:30:38 -0700724 std::string firmware_desc;
725 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800726 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700727 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
728 LOG(ERROR) << "Failed to get firmware version: "
729 << legacyErrorToString(legacy_status);
730 WifiStatus status = createWifiStatusFromLegacyError(
731 legacy_status, "failed to get firmware version");
732 return {status, result};
733 }
734 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700735
Roshan Piusabcf78f2017-10-06 16:30:38 -0700736 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700737}
738
739std::pair<WifiStatus, std::vector<uint8_t>>
740WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700741 legacy_hal::wifi_error legacy_status;
742 std::vector<uint8_t> driver_dump;
743 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700744 legacy_hal_.lock()->requestDriverMemoryDump(
745 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700746 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
747 LOG(ERROR) << "Failed to get driver debug dump: "
748 << legacyErrorToString(legacy_status);
749 return {createWifiStatusFromLegacyError(legacy_status),
750 std::vector<uint8_t>()};
751 }
752 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700753}
754
755std::pair<WifiStatus, std::vector<uint8_t>>
756WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700757 legacy_hal::wifi_error legacy_status;
758 std::vector<uint8_t> firmware_dump;
759 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700760 legacy_hal_.lock()->requestFirmwareMemoryDump(
761 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700762 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
763 LOG(ERROR) << "Failed to get firmware debug dump: "
764 << legacyErrorToString(legacy_status);
765 return {createWifiStatusFromLegacyError(legacy_status), {}};
766 }
767 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700768}
769
770std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700771 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700772 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800773 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700774 std::string ifname = allocateApIfaceName();
Roshan Pius3b6705f2019-02-14 09:43:43 -0800775 sp<WifiApIface> iface =
776 new WifiApIface(ifname, legacy_hal_, iface_util_, feature_flags_);
Roshan Pius675609b2017-10-31 14:24:58 -0700777 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700778 for (const auto& callback : event_cb_handler_.getCallbacks()) {
779 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
780 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
781 }
782 }
Roshan Pius675609b2017-10-31 14:24:58 -0700783 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700784}
785
786std::pair<WifiStatus, std::vector<hidl_string>>
787WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700788 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700789 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
790 }
Roshan Pius675609b2017-10-31 14:24:58 -0700791 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700792}
793
794std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800795 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700796 const auto iface = findUsingName(ap_ifaces_, ifname);
797 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700798 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
799 }
Roshan Pius675609b2017-10-31 14:24:58 -0700800 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700801}
802
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800803WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700804 const auto iface = findUsingName(ap_ifaces_, ifname);
805 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700806 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800807 }
Roshan Pius675609b2017-10-31 14:24:58 -0700808 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700809 for (const auto& callback : event_cb_handler_.getCallbacks()) {
810 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
811 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
812 }
813 }
814 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800815}
816
Roshan Pius3c868522016-10-27 12:43:49 -0700817std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700818 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700819 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800820 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700821 // These are still assumed to be based on wlan0.
Roshan Pius6036c022019-03-27 10:41:58 -0700822 std::string ifname = getFirstActiveWlanIfaceName();
Roshan Piuscc338202017-11-02 13:54:09 -0700823 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_);
824 nan_ifaces_.push_back(iface);
825 for (const auto& callback : event_cb_handler_.getCallbacks()) {
826 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
827 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
828 }
829 }
830 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700831}
832
833std::pair<WifiStatus, std::vector<hidl_string>>
834WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700835 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700836 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
837 }
Roshan Pius675609b2017-10-31 14:24:58 -0700838 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700839}
840
841std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800842 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700843 const auto iface = findUsingName(nan_ifaces_, ifname);
844 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700845 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
846 }
Roshan Pius675609b2017-10-31 14:24:58 -0700847 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700848}
849
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800850WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700851 const auto iface = findUsingName(nan_ifaces_, ifname);
852 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700853 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800854 }
Roshan Pius675609b2017-10-31 14:24:58 -0700855 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700856 for (const auto& callback : event_cb_handler_.getCallbacks()) {
857 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
858 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
859 }
860 }
861 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800862}
863
Roshan Pius3c868522016-10-27 12:43:49 -0700864std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700865 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700866 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800867 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700868 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700869 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
870 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700871 for (const auto& callback : event_cb_handler_.getCallbacks()) {
872 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
873 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
874 }
875 }
Roshan Pius675609b2017-10-31 14:24:58 -0700876 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700877}
878
879std::pair<WifiStatus, std::vector<hidl_string>>
880WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700881 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700882 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
883 }
Roshan Pius675609b2017-10-31 14:24:58 -0700884 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700885}
886
887std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800888 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700889 const auto iface = findUsingName(p2p_ifaces_, ifname);
890 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700891 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
892 }
Roshan Pius675609b2017-10-31 14:24:58 -0700893 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700894}
895
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800896WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700897 const auto iface = findUsingName(p2p_ifaces_, ifname);
898 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700899 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800900 }
Roshan Pius675609b2017-10-31 14:24:58 -0700901 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700902 for (const auto& callback : event_cb_handler_.getCallbacks()) {
903 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
904 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
905 }
906 }
907 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800908}
909
Roshan Pius3c868522016-10-27 12:43:49 -0700910std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700911 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700912 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800913 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700914 std::string ifname = allocateStaIfaceName();
Roshan Pius99dab382019-02-14 07:57:10 -0800915 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700916 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700917 for (const auto& callback : event_cb_handler_.getCallbacks()) {
918 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
919 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
920 }
921 }
Roshan Pius675609b2017-10-31 14:24:58 -0700922 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700923}
924
925std::pair<WifiStatus, std::vector<hidl_string>>
926WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700927 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700928 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
929 }
Roshan Pius675609b2017-10-31 14:24:58 -0700930 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700931}
932
933std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800934 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700935 const auto iface = findUsingName(sta_ifaces_, ifname);
936 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700937 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
938 }
Roshan Pius675609b2017-10-31 14:24:58 -0700939 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700940}
941
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800942WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700943 const auto iface = findUsingName(sta_ifaces_, ifname);
944 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700945 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800946 }
Roshan Pius675609b2017-10-31 14:24:58 -0700947 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700948 for (const auto& callback : event_cb_handler_.getCallbacks()) {
949 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
950 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
951 }
952 }
953 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800954}
955
Roshan Pius3c868522016-10-27 12:43:49 -0700956std::pair<WifiStatus, sp<IWifiRttController>>
957WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
Etan Cohen7240a1a2018-08-29 08:14:09 -0700958 if (sta_ifaces_.size() == 0 &&
959 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
960 LOG(ERROR) << "createRttControllerInternal: Chip cannot support STAs "
961 "(and RTT by extension)";
962 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
963 }
Roshan Pius6036c022019-03-27 10:41:58 -0700964 sp<WifiRttController> rtt = new WifiRttController(
965 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700966 rtt_controllers_.emplace_back(rtt);
967 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
Roshan Pius3c868522016-10-27 12:43:49 -0700968}
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700969
970std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
971WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700972 legacy_hal::wifi_error legacy_status;
973 std::vector<legacy_hal::wifi_ring_buffer_status>
974 legacy_ring_buffer_status_vec;
975 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -0700976 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700977 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
978 return {createWifiStatusFromLegacyError(legacy_status), {}};
979 }
980 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
981 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
982 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
983 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
984 }
985 return {createWifiStatus(WifiStatusCode::SUCCESS),
986 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700987}
988
989WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700990 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
991 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
992 WifiStatus status = registerDebugRingBufferCallback();
993 if (status.code != WifiStatusCode::SUCCESS) {
994 return status;
995 }
996 legacy_hal::wifi_error legacy_status =
997 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -0700998 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -0700999 static_cast<
1000 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1001 verbose_level),
1002 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001003 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1004 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusabcf78f2017-10-06 16:30:38 -07001005 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001006}
1007
1008WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001009 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001010 WifiStatus status = registerDebugRingBufferCallback();
1011 if (status.code != WifiStatusCode::SUCCESS) {
1012 return status;
1013 }
1014 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001015 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1016 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001017
Roshan Piusabcf78f2017-10-06 16:30:38 -07001018 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001019}
1020
Roger Wangb294c762018-11-02 15:34:39 +08001021WifiStatus WifiChip::flushRingBufferToFileInternal() {
1022 if (!writeRingbufferFilesInternal()) {
1023 LOG(ERROR) << "Error writing files to flash";
1024 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1025 }
1026 return createWifiStatus(WifiStatusCode::SUCCESS);
1027}
1028
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001029WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001030 legacy_hal::wifi_error legacy_status =
1031 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001032 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001033 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001034}
1035
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001036std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1037WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001038 legacy_hal::wifi_error legacy_status;
1039 legacy_hal::WakeReasonStats legacy_stats;
1040 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001041 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001042 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1043 return {createWifiStatusFromLegacyError(legacy_status), {}};
1044 }
1045 WifiDebugHostWakeReasonStats hidl_stats;
1046 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1047 &hidl_stats)) {
1048 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1049 }
1050 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001051}
1052
Roshan Pius203cb032016-12-14 17:41:20 -08001053WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001054 legacy_hal::wifi_error legacy_status;
1055 if (enable) {
1056 android::wp<WifiChip> weak_ptr_this(this);
1057 const auto& on_alert_callback = [weak_ptr_this](
1058 int32_t error_code,
1059 std::vector<uint8_t> debug_data) {
1060 const auto shared_ptr_this = weak_ptr_this.promote();
1061 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1062 LOG(ERROR) << "Callback invoked on an invalid object";
1063 return;
1064 }
1065 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1066 if (!callback->onDebugErrorAlert(error_code, debug_data)
1067 .isOk()) {
1068 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1069 }
1070 }
1071 };
1072 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001073 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001074 } else {
1075 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001076 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001077 }
1078 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001079}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001080
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001081WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001082 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001083 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001084 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001085 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1086 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001087}
1088
Roshan Pius735ff432017-07-25 08:48:08 -07001089WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001090 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001091 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001092 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001093}
1094
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001095WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1096 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001097 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001098 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1099 return createWifiStatusFromLegacyError(legacy_status);
1100}
1101
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001102WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001103 const sp<V1_2::IWifiChipEventCallback>& event_callback) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001104 if (!event_cb_handler_.addCallback(event_callback)) {
1105 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1106 }
1107 return createWifiStatus(WifiStatusCode::SUCCESS);
1108}
1109
Jong Wook Kimda830c92018-07-23 15:29:38 -07001110WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1111 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001112 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001113 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001114 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1115 return createWifiStatusFromLegacyError(legacy_status);
1116}
1117
Roshan Piusba38d9c2017-12-08 07:32:08 -08001118WifiStatus WifiChip::handleChipConfiguration(
1119 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1120 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001121 // If the chip is already configured in a different mode, stop
1122 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001123 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001124 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1125 << " to mode " << mode_id;
1126 invalidateAndRemoveAllIfaces();
1127 legacy_hal::wifi_error legacy_status =
1128 legacy_hal_.lock()->stop(lock, []() {});
1129 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1130 LOG(ERROR) << "Failed to stop legacy HAL: "
1131 << legacyErrorToString(legacy_status);
1132 return createWifiStatusFromLegacyError(legacy_status);
1133 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001134 }
Roshan Piuscc338202017-11-02 13:54:09 -07001135 // Firmware mode change not needed for V2 devices.
1136 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001137 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001138 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001139 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001140 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1141 }
1142 if (!success) {
1143 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1144 }
1145 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1146 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1147 LOG(ERROR) << "Failed to start legacy HAL: "
1148 << legacyErrorToString(legacy_status);
1149 return createWifiStatusFromLegacyError(legacy_status);
1150 }
Roshan Pius85c64412018-01-22 17:58:40 -08001151 // Every time the HAL is restarted, we need to register the
1152 // radio mode change callback.
1153 WifiStatus status = registerRadioModeChangeCallback();
1154 if (status.code != WifiStatusCode::SUCCESS) {
1155 // This probably is not a critical failure?
1156 LOG(ERROR) << "Failed to register radio mode change callback";
1157 }
chenpaulf5eca292019-03-14 11:08:03 +08001158 // Extract and save the version information into property.
1159 std::pair<WifiStatus, IWifiChip::ChipDebugInfo> version_info;
1160 version_info = WifiChip::requestChipDebugInfoInternal();
1161 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1162 property_set("vendor.wlan.firmware.version",
1163 version_info.second.firmwareDescription.c_str());
1164 property_set("vendor.wlan.driver.version",
1165 version_info.second.driverDescription.c_str());
1166 }
1167
Roshan Piusabcf78f2017-10-06 16:30:38 -07001168 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001169}
Roshan Pius48185b22016-12-15 19:10:30 -08001170
1171WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001172 if (debug_ring_buffer_cb_registered_) {
1173 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001174 }
Roshan Pius3797e182017-03-30 18:01:54 -07001175
Roshan Piusabcf78f2017-10-06 16:30:38 -07001176 android::wp<WifiChip> weak_ptr_this(this);
1177 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001178 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001179 const std::vector<uint8_t>& data,
1180 const legacy_hal::wifi_ring_buffer_status& status) {
1181 const auto shared_ptr_this = weak_ptr_this.promote();
1182 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1183 LOG(ERROR) << "Callback invoked on an invalid object";
1184 return;
1185 }
1186 WifiDebugRingBufferStatus hidl_status;
1187 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1188 status, &hidl_status)) {
1189 LOG(ERROR) << "Error converting ring buffer status";
1190 return;
1191 }
xshu5899e8e2018-01-09 15:36:03 -08001192 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1193 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1194 Ringbuffer& cur_buffer = target->second;
1195 cur_buffer.append(data);
1196 } else {
1197 LOG(ERROR) << "Ringname " << name << " not found";
1198 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001199 }
1200 };
1201 legacy_hal::wifi_error legacy_status =
1202 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001203 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001204
Roshan Piusabcf78f2017-10-06 16:30:38 -07001205 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1206 debug_ring_buffer_cb_registered_ = true;
1207 }
1208 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001209}
1210
Roshan Pius85c64412018-01-22 17:58:40 -08001211WifiStatus WifiChip::registerRadioModeChangeCallback() {
1212 android::wp<WifiChip> weak_ptr_this(this);
1213 const auto& on_radio_mode_change_callback =
1214 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1215 const auto shared_ptr_this = weak_ptr_this.promote();
1216 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1217 LOG(ERROR) << "Callback invoked on an invalid object";
1218 return;
1219 }
Jong Wook Kimda830c92018-07-23 15:29:38 -07001220 std::vector<V1_2::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001221 hidl_radio_mode_infos;
1222 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1223 mac_infos, &hidl_radio_mode_infos)) {
1224 LOG(ERROR) << "Error converting wifi mac info";
1225 return;
1226 }
1227 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1228 if (!callback->onRadioModeChange(hidl_radio_mode_infos)
1229 .isOk()) {
1230 LOG(ERROR) << "Failed to invoke onRadioModeChange"
1231 << " callback on: " << toString(callback);
1232 }
1233 }
1234 };
1235 legacy_hal::wifi_error legacy_status =
1236 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001237 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001238 return createWifiStatusFromLegacyError(legacy_status);
1239}
1240
Roshan Piuscc338202017-11-02 13:54:09 -07001241std::vector<IWifiChip::ChipIfaceCombination>
1242WifiChip::getCurrentModeIfaceCombinations() {
1243 if (!isValidModeId(current_mode_id_)) {
1244 LOG(ERROR) << "Chip not configured in a mode yet";
1245 return {};
1246 }
1247 for (const auto& mode : modes_) {
1248 if (mode.id == current_mode_id_) {
1249 return mode.availableCombinations;
1250 }
1251 }
1252 CHECK(0) << "Expected to find iface combinations for current mode!";
1253 return {};
1254}
1255
1256// Returns a map indexed by IfaceType with the number of ifaces currently
1257// created of the corresponding type.
1258std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1259 std::map<IfaceType, size_t> iface_counts;
1260 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1261 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1262 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1263 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1264 return iface_counts;
1265}
1266
1267// This expands the provided iface combinations to a more parseable
1268// form. Returns a vector of available combinations possible with the number
1269// of ifaces of each type in the combination.
1270// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1271std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1272 const IWifiChip::ChipIfaceCombination& combination) {
1273 uint32_t num_expanded_combos = 1;
1274 for (const auto& limit : combination.limits) {
1275 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1276 num_expanded_combos *= limit.types.size();
1277 }
1278 }
1279
1280 // Allocate the vector of expanded combos and reset all iface counts to 0
1281 // in each combo.
1282 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1283 expanded_combos.resize(num_expanded_combos);
1284 for (auto& expanded_combo : expanded_combos) {
1285 for (const auto type :
1286 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1287 expanded_combo[type] = 0;
1288 }
1289 }
1290 uint32_t span = num_expanded_combos;
1291 for (const auto& limit : combination.limits) {
1292 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1293 span /= limit.types.size();
1294 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1295 const auto iface_type =
1296 limit.types[(k / span) % limit.types.size()];
1297 expanded_combos[k][iface_type]++;
1298 }
1299 }
1300 }
1301 return expanded_combos;
1302}
1303
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001304bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1305 const std::map<IfaceType, size_t>& expanded_combo,
1306 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001307 const auto current_combo = getCurrentIfaceCombination();
1308
1309 // Check if we have space for 1 more iface of |type| in this combo
1310 for (const auto type :
1311 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1312 size_t num_ifaces_needed = current_combo.at(type);
1313 if (type == requested_type) {
1314 num_ifaces_needed++;
1315 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001316 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001317 if (num_ifaces_needed > num_ifaces_allowed) {
1318 return false;
1319 }
1320 }
1321 return true;
1322}
1323
1324// This method does the following:
1325// a) Enumerate all possible iface combos by expanding the current
1326// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001327// b) Check if the requested iface type can be added to the current mode
1328// with the iface combination that is already active.
1329bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1330 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001331 if (!isValidModeId(current_mode_id_)) {
1332 LOG(ERROR) << "Chip not configured in a mode yet";
1333 return false;
1334 }
1335 const auto combinations = getCurrentModeIfaceCombinations();
1336 for (const auto& combination : combinations) {
1337 const auto expanded_combos = expandIfaceCombinations(combination);
1338 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001339 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1340 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001341 return true;
1342 }
1343 }
1344 }
1345 return false;
1346}
1347
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001348// Note: This does not consider ifaces already active. It only checks if the
1349// provided expanded iface combination can support the requested combo.
1350bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1351 const std::map<IfaceType, size_t>& expanded_combo,
1352 const std::map<IfaceType, size_t>& req_combo) {
1353 // Check if we have space for 1 more iface of |type| in this combo
1354 for (const auto type :
1355 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1356 if (req_combo.count(type) == 0) {
1357 // Iface of "type" not in the req_combo.
1358 continue;
1359 }
1360 size_t num_ifaces_needed = req_combo.at(type);
1361 size_t num_ifaces_allowed = expanded_combo.at(type);
1362 if (num_ifaces_needed > num_ifaces_allowed) {
1363 return false;
1364 }
1365 }
1366 return true;
1367}
1368// This method does the following:
1369// a) Enumerate all possible iface combos by expanding the current
1370// ChipIfaceCombination.
1371// b) Check if the requested iface combo can be added to the current mode.
1372// Note: This does not consider ifaces already active. It only checks if the
1373// current mode can support the requested combo.
1374bool WifiChip::canCurrentModeSupportIfaceCombo(
1375 const std::map<IfaceType, size_t>& req_combo) {
1376 if (!isValidModeId(current_mode_id_)) {
1377 LOG(ERROR) << "Chip not configured in a mode yet";
1378 return false;
1379 }
1380 const auto combinations = getCurrentModeIfaceCombinations();
1381 for (const auto& combination : combinations) {
1382 const auto expanded_combos = expandIfaceCombinations(combination);
1383 for (const auto& expanded_combo : expanded_combos) {
1384 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1385 req_combo)) {
1386 return true;
1387 }
1388 }
1389 }
1390 return false;
1391}
1392
1393// This method does the following:
1394// a) Enumerate all possible iface combos by expanding the current
1395// ChipIfaceCombination.
1396// b) Check if the requested iface type can be added to the current mode.
1397bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1398 // Check if we can support atleast 1 iface of type.
1399 std::map<IfaceType, size_t> req_iface_combo;
1400 req_iface_combo[requested_type] = 1;
1401 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1402}
1403
Roshan Piuscc338202017-11-02 13:54:09 -07001404bool WifiChip::isValidModeId(ChipModeId mode_id) {
1405 for (const auto& mode : modes_) {
1406 if (mode.id == mode_id) {
1407 return true;
1408 }
1409 }
1410 return false;
1411}
1412
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001413bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1414 // Check if we can support atleast 1 STA & 1 AP concurrently.
1415 std::map<IfaceType, size_t> req_iface_combo;
1416 req_iface_combo[IfaceType::AP] = 1;
1417 req_iface_combo[IfaceType::STA] = 1;
1418 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1419}
1420
Roshan Pius6036c022019-03-27 10:41:58 -07001421std::string WifiChip::getFirstActiveWlanIfaceName() {
1422 for (unsigned idx = 0; idx < kMaxWlanIfaces; idx++) {
1423 const auto ifname = getWlanIfaceName(idx);
1424 if (findUsingName(sta_ifaces_, ifname)) return ifname;
1425 if (findUsingName(ap_ifaces_, ifname)) return ifname;
1426 }
1427 // This could happen if the chip call is made before any STA/AP
1428 // iface is created. Default to wlan0 for such cases.
1429 LOG(WARNING) << "No active wlan interfaces in use!";
1430 return getWlanIfaceName(0);
1431}
1432
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001433// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1434// not already in use.
1435// Note: This doesn't check the actual presence of these interfaces.
1436std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1437 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1438 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001439 if (findUsingName(ap_ifaces_, ifname)) continue;
1440 if (findUsingName(sta_ifaces_, ifname)) continue;
1441 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001442 }
1443 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001444 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001445 return {};
1446}
1447
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001448// AP iface names start with idx 1 for modes supporting
1449// concurrent STA, else start with idx 0.
1450std::string WifiChip::allocateApIfaceName() {
1451 return allocateApOrStaIfaceName(
1452 isStaApConcurrencyAllowedInCurrentMode() ? 1 : 0);
1453}
1454
1455// STA iface names start with idx 0.
1456// Primary STA iface will always be 0.
1457std::string WifiChip::allocateStaIfaceName() {
1458 return allocateApOrStaIfaceName(0);
1459}
1460
xshu5899e8e2018-01-09 15:36:03 -08001461bool WifiChip::writeRingbufferFilesInternal() {
1462 if (!removeOldFilesInternal()) {
1463 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1464 return false;
1465 }
1466 // write ringbuffers to file
1467 for (const auto& item : ringbuffer_map_) {
1468 const Ringbuffer& cur_buffer = item.second;
1469 if (cur_buffer.getData().empty()) {
1470 continue;
1471 }
1472 const std::string file_path_raw =
1473 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1474 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1475 if (dump_fd == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001476 PLOG(ERROR) << "create file failed";
xshu5899e8e2018-01-09 15:36:03 -08001477 return false;
1478 }
1479 unique_fd file_auto_closer(dump_fd);
1480 for (const auto& cur_block : cur_buffer.getData()) {
1481 if (write(dump_fd, cur_block.data(),
1482 sizeof(cur_block[0]) * cur_block.size()) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001483 PLOG(ERROR) << "Error writing to file";
xshu5899e8e2018-01-09 15:36:03 -08001484 }
1485 }
1486 }
1487 return true;
1488}
1489
Roshan Pius79a99752016-10-04 13:03:58 -07001490} // namespace implementation
Jong Wook Kimda830c92018-07-23 15:29:38 -07001491} // namespace V1_3
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001492} // namespace wifi
1493} // namespace hardware
1494} // namespace android