blob: b898f686eb6ec1ec16ebb2d0dd485a5e9ec3290b [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/";
Roshan Pius8574e7f2019-04-01 13:30:40 -070044constexpr char kActiveWlanIfaceNameProperty[] = "wifi.active.interface";
45constexpr char kNoActiveWlanIfaceNamePropertyValue[] = "";
46constexpr unsigned kMaxWlanIfaces = 5;
xshu5899e8e2018-01-09 15:36:03 -080047
Roshan Pius35d958c2016-10-06 16:47:38 -070048template <typename Iface>
Roshan Pius675609b2017-10-31 14:24:58 -070049void invalidateAndClear(std::vector<sp<Iface>>& ifaces, sp<Iface> iface) {
50 iface->invalidate();
51 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface),
52 ifaces.end());
53}
54
55template <typename Iface>
56void invalidateAndClearAll(std::vector<sp<Iface>>& ifaces) {
57 for (const auto& iface : ifaces) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070058 iface->invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -070059 }
Roshan Pius675609b2017-10-31 14:24:58 -070060 ifaces.clear();
61}
62
63template <typename Iface>
64std::vector<hidl_string> getNames(std::vector<sp<Iface>>& ifaces) {
65 std::vector<hidl_string> names;
66 for (const auto& iface : ifaces) {
67 names.emplace_back(iface->getName());
68 }
69 return names;
70}
71
72template <typename Iface>
73sp<Iface> findUsingName(std::vector<sp<Iface>>& ifaces,
74 const std::string& name) {
75 std::vector<hidl_string> names;
76 for (const auto& iface : ifaces) {
77 if (name == iface->getName()) {
78 return iface;
79 }
80 }
81 return nullptr;
Roshan Pius35d958c2016-10-06 16:47:38 -070082}
Roshan Pius9377a0d2017-10-06 13:18:54 -070083
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080084std::string getWlanIfaceName(unsigned idx) {
85 if (idx >= kMaxWlanIfaces) {
86 CHECK(false) << "Requested interface beyond wlan" << kMaxWlanIfaces;
87 return {};
88 }
Roshan Pius9377a0d2017-10-06 13:18:54 -070089
Roshan Pius8e3c7ef2017-11-03 09:43:08 -070090 std::array<char, PROPERTY_VALUE_MAX> buffer;
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080091 if (idx == 0 || idx == 1) {
92 const char* altPropName =
93 (idx == 0) ? "wifi.interface" : "wifi.concurrent.interface";
Roshan Pius5b333462019-03-01 14:07:22 -080094 auto res = property_get(altPropName, buffer.data(), nullptr);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -080095 if (res > 0) return buffer.data();
96 }
Roshan Pius5b333462019-03-01 14:07:22 -080097 std::string propName = "wifi.interface." + std::to_string(idx);
98 auto res = property_get(propName.c_str(), buffer.data(), nullptr);
99 if (res > 0) return buffer.data();
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800100
101 return "wlan" + std::to_string(idx);
Roshan Pius9377a0d2017-10-06 13:18:54 -0700102}
Roshan Pius9377a0d2017-10-06 13:18:54 -0700103
Roshan Pius78cb5992020-04-30 12:39:21 -0700104// Returns the dedicated iface name if one is defined.
105std::string getApIfaceName() {
106 std::array<char, PROPERTY_VALUE_MAX> buffer;
107 if (property_get("ro.vendor.wifi.sap.interface", buffer.data(), nullptr) ==
108 0) {
109 return {};
110 }
111 return buffer.data();
112}
113
Roshan Pius9377a0d2017-10-06 13:18:54 -0700114std::string getP2pIfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700115 std::array<char, PROPERTY_VALUE_MAX> buffer;
116 property_get("wifi.direct.interface", buffer.data(), "p2p0");
117 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700118}
119
Roshan Pius5ba0a902020-04-14 11:55:42 -0700120// Returns the dedicated iface name if one is defined.
121std::string getNanIfaceName() {
122 std::array<char, PROPERTY_VALUE_MAX> buffer;
123 if (property_get("wifi.aware.interface", buffer.data(), nullptr) == 0) {
124 return {};
125 }
126 return buffer.data();
127}
128
Roshan Pius8574e7f2019-04-01 13:30:40 -0700129void setActiveWlanIfaceNameProperty(const std::string& ifname) {
130 auto res = property_set(kActiveWlanIfaceNameProperty, ifname.data());
131 if (res != 0) {
132 PLOG(ERROR) << "Failed to set active wlan iface name property";
133 }
134}
135
xshu37126c92018-04-13 16:24:45 -0700136// delete files that meet either conditions:
137// 1. older than a predefined time in the wifi tombstone dir.
138// 2. Files in excess to a predefined amount, starting from the oldest ones
xshu5899e8e2018-01-09 15:36:03 -0800139bool removeOldFilesInternal() {
140 time_t now = time(0);
141 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
Josh Gaoa568e532018-06-04 18:16:00 -0700142 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(
143 opendir(kTombstoneFolderPath), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800144 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800145 PLOG(ERROR) << "Failed to open directory";
xshu5899e8e2018-01-09 15:36:03 -0800146 return false;
147 }
xshu5899e8e2018-01-09 15:36:03 -0800148 struct dirent* dp;
149 bool success = true;
xshu37126c92018-04-13 16:24:45 -0700150 std::list<std::pair<const time_t, std::string>> valid_files;
Josh Gaoa568e532018-06-04 18:16:00 -0700151 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800152 if (dp->d_type != DT_REG) {
153 continue;
154 }
155 std::string cur_file_name(dp->d_name);
156 struct stat cur_file_stat;
157 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800158 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800159 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800160 success = false;
xshu4cb33162018-01-24 15:40:06 -0800161 continue;
162 }
xshu37126c92018-04-13 16:24:45 -0700163 const time_t cur_file_time = cur_file_stat.st_mtime;
164 valid_files.push_back(
165 std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
166 }
167 valid_files.sort(); // sort the list of files by last modified time from
168 // small to big.
169 uint32_t cur_file_count = valid_files.size();
170 for (auto cur_file : valid_files) {
171 if (cur_file_count > kMaxRingBufferFileNum ||
172 cur_file.first < delete_files_before) {
173 if (unlink(cur_file.second.c_str()) != 0) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800174 PLOG(ERROR) << "Error deleting file";
xshu37126c92018-04-13 16:24:45 -0700175 success = false;
176 }
177 cur_file_count--;
178 } else {
179 break;
xshu5899e8e2018-01-09 15:36:03 -0800180 }
181 }
182 return success;
183}
184
xshu4cb33162018-01-24 15:40:06 -0800185// Helper function for |cpioArchiveFilesInDir|
186bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name,
187 size_t file_name_len) {
188 std::array<char, 32 * 1024> read_buf;
189 ssize_t llen =
190 sprintf(read_buf.data(),
191 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
192 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
193 st.st_gid, static_cast<int>(st.st_nlink),
194 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
195 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
196 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
197 if (write(out_fd, read_buf.data(), llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800198 PLOG(ERROR) << "Error writing cpio header to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800199 return false;
200 }
201 if (write(out_fd, file_name, file_name_len) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800202 PLOG(ERROR) << "Error writing filename to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800203 return false;
204 }
205
206 // NUL Pad header up to 4 multiple bytes.
207 llen = (llen + file_name_len) % 4;
208 if (llen != 0) {
209 const uint32_t zero = 0;
210 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800211 PLOG(ERROR) << "Error padding 0s to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800212 return false;
213 }
214 }
215 return true;
216}
217
218// Helper function for |cpioArchiveFilesInDir|
219size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
220 // writing content of file
221 std::array<char, 32 * 1024> read_buf;
222 ssize_t llen = st.st_size;
223 size_t n_error = 0;
224 while (llen > 0) {
225 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
226 if (bytes_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800227 PLOG(ERROR) << "Error reading file";
xshu4cb33162018-01-24 15:40:06 -0800228 return ++n_error;
229 }
230 llen -= bytes_read;
231 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800232 PLOG(ERROR) << "Error writing data to file";
xshu4cb33162018-01-24 15:40:06 -0800233 return ++n_error;
234 }
235 if (bytes_read == 0) { // this should never happen, but just in case
236 // to unstuck from while loop
Elliott Hughes4db4add2019-03-08 12:42:57 -0800237 PLOG(ERROR) << "Unexpected read result";
xshu4cb33162018-01-24 15:40:06 -0800238 n_error++;
239 break;
240 }
241 }
242 llen = st.st_size % 4;
243 if (llen != 0) {
244 const uint32_t zero = 0;
245 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800246 PLOG(ERROR) << "Error padding 0s to file";
xshu4cb33162018-01-24 15:40:06 -0800247 return ++n_error;
248 }
249 }
250 return n_error;
251}
252
253// Helper function for |cpioArchiveFilesInDir|
254bool cpioWriteFileTrailer(int out_fd) {
255 std::array<char, 4096> read_buf;
256 read_buf.fill(0);
257 if (write(out_fd, read_buf.data(),
258 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1,
259 0x0b, 0) +
260 4) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800261 PLOG(ERROR) << "Error writing trailing bytes";
xshu4cb33162018-01-24 15:40:06 -0800262 return false;
263 }
264 return true;
265}
266
xshu5899e8e2018-01-09 15:36:03 -0800267// Archives all files in |input_dir| and writes result into |out_fd|
268// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
269// portion
xshu4cb33162018-01-24 15:40:06 -0800270size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800271 struct dirent* dp;
272 size_t n_error = 0;
Josh Gaoa568e532018-06-04 18:16:00 -0700273 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir),
274 closedir);
xshu5899e8e2018-01-09 15:36:03 -0800275 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800276 PLOG(ERROR) << "Failed to open directory";
xshu4cb33162018-01-24 15:40:06 -0800277 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800278 }
Josh Gaoa568e532018-06-04 18:16:00 -0700279 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800280 if (dp->d_type != DT_REG) {
281 continue;
282 }
283 std::string cur_file_name(dp->d_name);
xshu4cb33162018-01-24 15:40:06 -0800284 // string.size() does not include the null terminator. The cpio FreeBSD
285 // file header expects the null character to be included in the length.
286 const size_t file_name_len = cur_file_name.size() + 1;
xshu5899e8e2018-01-09 15:36:03 -0800287 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800288 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800289 if (stat(cur_file_path.c_str(), &st) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800290 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800291 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800292 continue;
293 }
294 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
295 if (fd_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800296 PLOG(ERROR) << "Failed to open file " << cur_file_path;
xshu4cb33162018-01-24 15:40:06 -0800297 n_error++;
298 continue;
299 }
300 unique_fd file_auto_closer(fd_read);
301 if (!cpioWriteHeader(out_fd, st, cur_file_name.c_str(),
302 file_name_len)) {
303 return ++n_error;
304 }
305 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
306 if (write_error) {
307 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800308 }
309 }
xshu4cb33162018-01-24 15:40:06 -0800310 if (!cpioWriteFileTrailer(out_fd)) {
311 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800312 }
313 return n_error;
314}
315
316// Helper function to create a non-const char*.
317std::vector<char> makeCharVec(const std::string& str) {
318 std::vector<char> vec(str.size() + 1);
319 vec.assign(str.begin(), str.end());
320 vec.push_back('\0');
321 return vec;
322}
323
Roshan Piusabcf78f2017-10-06 16:30:38 -0700324} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700325
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700326namespace android {
327namespace hardware {
328namespace wifi {
Jimmy Chend460df32019-11-29 17:31:22 +0200329namespace V1_5 {
Roshan Pius79a99752016-10-04 13:03:58 -0700330namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700331using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800332using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700333
Roshan Pius52947fb2016-11-18 11:38:07 -0800334WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700335 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700336 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800337 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700338 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
339 const std::function<void(const std::string&)>& handler)
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()),
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700347 debug_ring_buffer_cb_registered_(false),
348 subsystemCallbackHandler_(handler) {
Roshan Pius8574e7f2019-04-01 13:30:40 -0700349 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
350}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700351
Roshan Piusaabe5752016-09-29 09:03:59 -0700352void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700353 if (!writeRingbufferFilesInternal()) {
354 LOG(ERROR) << "Error writing files to flash";
355 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700356 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700357 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700358 legacy_hal_.reset();
359 event_cb_handler_.invalidate();
360 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700361}
362
Roshan Piusabcf78f2017-10-06 16:30:38 -0700363bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700364
Jimmy Chend460df32019-11-29 17:31:22 +0200365std::set<sp<V1_4::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700366 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800367}
368
Roshan Pius5c055462016-10-11 08:27:27 -0700369Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700370 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
371 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700372}
373
Jong Wook Kimda830c92018-07-23 15:29:38 -0700374// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700375Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800376 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700377 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700378 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
379 &WifiChip::registerEventCallbackInternal,
380 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700381}
382
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700383Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700384 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
385 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700386}
387
Roshan Pius5c055462016-10-11 08:27:27 -0700388Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700389 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
390 &WifiChip::getAvailableModesInternal,
391 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700392}
393
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800394Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700395 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800396 return validateAndCallWithLock(
397 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
398 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700399}
400
Roshan Pius5c055462016-10-11 08:27:27 -0700401Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700402 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
403 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700404}
405
Roshan Pius5c055462016-10-11 08:27:27 -0700406Return<void> WifiChip::requestChipDebugInfo(
407 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700408 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
409 &WifiChip::requestChipDebugInfoInternal,
410 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700411}
412
413Return<void> WifiChip::requestDriverDebugDump(
414 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700415 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
416 &WifiChip::requestDriverDebugDumpInternal,
417 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700418}
419
Roshan Pius5c055462016-10-11 08:27:27 -0700420Return<void> WifiChip::requestFirmwareDebugDump(
421 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700422 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
423 &WifiChip::requestFirmwareDebugDumpInternal,
424 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700425}
426
Roshan Pius5c055462016-10-11 08:27:27 -0700427Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700428 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
429 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700430}
431
Roshan Pius5c055462016-10-11 08:27:27 -0700432Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700433 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
434 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700435}
436
Roshan Pius5c055462016-10-11 08:27:27 -0700437Return<void> WifiChip::getApIface(const hidl_string& ifname,
438 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700439 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
440 &WifiChip::getApIfaceInternal, hidl_status_cb,
441 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700442}
443
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800444Return<void> WifiChip::removeApIface(const hidl_string& ifname,
445 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700446 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
447 &WifiChip::removeApIfaceInternal, hidl_status_cb,
448 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800449}
450
Roshan Pius5c055462016-10-11 08:27:27 -0700451Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700452 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
453 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700454}
455
Roshan Pius5c055462016-10-11 08:27:27 -0700456Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700457 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
458 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700459}
460
461Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700462 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700463 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
464 &WifiChip::getNanIfaceInternal, hidl_status_cb,
465 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700466}
467
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800468Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
469 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700470 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
471 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
472 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800473}
474
Roshan Pius5c055462016-10-11 08:27:27 -0700475Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700476 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
477 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700478}
479
Roshan Pius5c055462016-10-11 08:27:27 -0700480Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700481 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
482 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700483}
484
485Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700486 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700487 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
488 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
489 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700490}
491
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800492Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
493 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700494 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
495 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
496 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800497}
498
Roshan Pius5c055462016-10-11 08:27:27 -0700499Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700500 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
501 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700502}
503
Roshan Pius5c055462016-10-11 08:27:27 -0700504Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700505 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
506 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700507}
508
509Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700510 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700511 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
512 &WifiChip::getStaIfaceInternal, hidl_status_cb,
513 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700514}
515
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800516Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
517 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700518 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
519 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
520 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800521}
522
Roshan Pius5c055462016-10-11 08:27:27 -0700523Return<void> WifiChip::createRttController(
524 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700525 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
526 &WifiChip::createRttControllerInternal,
527 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700528}
529
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700530Return<void> WifiChip::getDebugRingBuffersStatus(
531 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700532 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
533 &WifiChip::getDebugRingBuffersStatusInternal,
534 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700535}
536
537Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700538 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
539 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700540 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700541 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
542 &WifiChip::startLoggingToDebugRingBufferInternal,
543 hidl_status_cb, ring_name, verbose_level,
544 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700545}
546
547Return<void> WifiChip::forceDumpToDebugRingBuffer(
548 const hidl_string& ring_name,
549 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700550 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
551 &WifiChip::forceDumpToDebugRingBufferInternal,
552 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700553}
554
Roger Wangb294c762018-11-02 15:34:39 +0800555Return<void> WifiChip::flushRingBufferToFile(
556 flushRingBufferToFile_cb hidl_status_cb) {
557 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
558 &WifiChip::flushRingBufferToFileInternal,
559 hidl_status_cb);
560}
561
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800562Return<void> WifiChip::stopLoggingToDebugRingBuffer(
563 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700564 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
565 &WifiChip::stopLoggingToDebugRingBufferInternal,
566 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800567}
568
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700569Return<void> WifiChip::getDebugHostWakeReasonStats(
570 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700571 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
572 &WifiChip::getDebugHostWakeReasonStatsInternal,
573 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700574}
575
Roshan Pius203cb032016-12-14 17:41:20 -0800576Return<void> WifiChip::enableDebugErrorAlerts(
577 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700578 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
579 &WifiChip::enableDebugErrorAlertsInternal,
580 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800581}
582
Roshan Pius735ff432017-07-25 08:48:08 -0700583Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700584 V1_1::IWifiChip::TxPowerScenario scenario,
585 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700586 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
587 &WifiChip::selectTxPowerScenarioInternal,
588 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700589}
590
Roshan Pius735ff432017-07-25 08:48:08 -0700591Return<void> WifiChip::resetTxPowerScenario(
592 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700593 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
594 &WifiChip::resetTxPowerScenarioInternal,
595 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700596}
597
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700598Return<void> WifiChip::setLatencyMode(LatencyMode mode,
599 setLatencyMode_cb hidl_status_cb) {
600 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
601 &WifiChip::setLatencyModeInternal, hidl_status_cb,
602 mode);
603}
604
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800605Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700606 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800607 registerEventCallback_cb hidl_status_cb) {
608 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
609 &WifiChip::registerEventCallbackInternal_1_2,
610 hidl_status_cb, event_callback);
611}
612
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800613Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700614 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800615 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700616 &WifiChip::selectTxPowerScenarioInternal_1_2,
617 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800618}
619
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700620Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
621 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
622 &WifiChip::getCapabilitiesInternal_1_3,
623 hidl_status_cb);
624}
625
xshu5899e8e2018-01-09 15:36:03 -0800626Return<void> WifiChip::debug(const hidl_handle& handle,
627 const hidl_vec<hidl_string>&) {
628 if (handle != nullptr && handle->numFds >= 1) {
xshu0a0fe512020-07-22 17:53:37 -0700629 {
630 std::unique_lock<std::mutex> lk(lock_t);
631 for (const auto& item : ringbuffer_map_) {
632 forceDumpToDebugRingBufferInternal(item.first);
633 }
634 // unique_lock unlocked here
635 }
636 usleep(100 * 1000); // sleep for 100 milliseconds to wait for
637 // ringbuffer updates.
xshu5899e8e2018-01-09 15:36:03 -0800638 int fd = handle->data[0];
639 if (!writeRingbufferFilesInternal()) {
640 LOG(ERROR) << "Error writing files to flash";
641 }
xshu4cb33162018-01-24 15:40:06 -0800642 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800643 if (n_error != 0) {
644 LOG(ERROR) << n_error << " errors occured in cpio function";
645 }
646 fsync(fd);
647 } else {
648 LOG(ERROR) << "File handle error";
649 }
650 return Void();
651}
652
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700653Return<void> WifiChip::createRttController_1_4(
654 const sp<IWifiIface>& bound_iface,
655 createRttController_1_4_cb hidl_status_cb) {
656 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
657 &WifiChip::createRttControllerInternal_1_4,
658 hidl_status_cb, bound_iface);
659}
660
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800661Return<void> WifiChip::registerEventCallback_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +0200662 const sp<V1_4::IWifiChipEventCallback>& event_callback,
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800663 registerEventCallback_cb hidl_status_cb) {
664 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
665 &WifiChip::registerEventCallbackInternal_1_4,
666 hidl_status_cb, event_callback);
667}
668
Roshan Pius35d958c2016-10-06 16:47:38 -0700669void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700670 invalidateAndClearAll(ap_ifaces_);
671 invalidateAndClearAll(nan_ifaces_);
672 invalidateAndClearAll(p2p_ifaces_);
673 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700674 // Since all the ifaces are invalid now, all RTT controller objects
675 // using those ifaces also need to be invalidated.
676 for (const auto& rtt : rtt_controllers_) {
677 rtt->invalidate();
678 }
679 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700680}
681
Roshan Pius82368502019-05-16 12:53:02 -0700682void WifiChip::invalidateAndRemoveDependencies(
683 const std::string& removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200684 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
685 auto nan_iface = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700686 if (nan_iface->getName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200687 nan_iface->invalidate();
Roshan Pius82368502019-05-16 12:53:02 -0700688 for (const auto& callback : event_cb_handler_.getCallbacks()) {
689 if (!callback
690 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
691 .isOk()) {
692 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
693 }
694 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200695 it = nan_ifaces_.erase(it);
696 } else {
697 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700698 }
699 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200700
701 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
702 auto rtt = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700703 if (rtt->getIfaceName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200704 rtt->invalidate();
705 it = rtt_controllers_.erase(it);
706 } else {
707 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700708 }
709 }
710}
711
Roshan Pius3c868522016-10-27 12:43:49 -0700712std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700713 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700714}
715
716WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800717 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
718 // Deprecated support for this callback.
719 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700720}
721
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700722std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700723 // Deprecated support for this callback.
724 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
725}
726
Jimmy Chend460df32019-11-29 17:31:22 +0200727std::pair<WifiStatus, std::vector<V1_4::IWifiChip::ChipMode>>
Roshan Pius3c868522016-10-27 12:43:49 -0700728WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700729 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700730}
731
Roshan Piusba38d9c2017-12-08 07:32:08 -0800732WifiStatus WifiChip::configureChipInternal(
733 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
734 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700735 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700736 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
737 }
738 if (mode_id == current_mode_id_) {
739 LOG(DEBUG) << "Already in the specified mode " << mode_id;
740 return createWifiStatus(WifiStatusCode::SUCCESS);
741 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800742 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700743 if (status.code != WifiStatusCode::SUCCESS) {
744 for (const auto& callback : event_cb_handler_.getCallbacks()) {
745 if (!callback->onChipReconfigureFailure(status).isOk()) {
746 LOG(ERROR)
747 << "Failed to invoke onChipReconfigureFailure callback";
748 }
749 }
750 return status;
751 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800752 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700753 if (!callback->onChipReconfigured(mode_id).isOk()) {
754 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
755 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800756 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700757 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800758 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700759 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700760
761 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(
762 subsystemCallbackHandler_);
763
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800764 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700765}
766
767std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700768 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700769 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
770 current_mode_id_};
771 }
772 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700773}
774
Jimmy Chend460df32019-11-29 17:31:22 +0200775std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo>
Roshan Pius3c868522016-10-27 12:43:49 -0700776WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200777 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700778 legacy_hal::wifi_error legacy_status;
779 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700780 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700781 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800782 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700783 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
784 LOG(ERROR) << "Failed to get driver version: "
785 << legacyErrorToString(legacy_status);
786 WifiStatus status = createWifiStatusFromLegacyError(
787 legacy_status, "failed to get driver version");
788 return {status, result};
789 }
790 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700791
Roshan Piusabcf78f2017-10-06 16:30:38 -0700792 std::string firmware_desc;
793 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800794 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700795 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
796 LOG(ERROR) << "Failed to get firmware version: "
797 << legacyErrorToString(legacy_status);
798 WifiStatus status = createWifiStatusFromLegacyError(
799 legacy_status, "failed to get firmware version");
800 return {status, result};
801 }
802 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700803
Roshan Piusabcf78f2017-10-06 16:30:38 -0700804 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700805}
806
807std::pair<WifiStatus, std::vector<uint8_t>>
808WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700809 legacy_hal::wifi_error legacy_status;
810 std::vector<uint8_t> driver_dump;
811 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700812 legacy_hal_.lock()->requestDriverMemoryDump(
813 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700814 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
815 LOG(ERROR) << "Failed to get driver debug dump: "
816 << legacyErrorToString(legacy_status);
817 return {createWifiStatusFromLegacyError(legacy_status),
818 std::vector<uint8_t>()};
819 }
820 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700821}
822
823std::pair<WifiStatus, std::vector<uint8_t>>
824WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700825 legacy_hal::wifi_error legacy_status;
826 std::vector<uint8_t> firmware_dump;
827 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700828 legacy_hal_.lock()->requestFirmwareMemoryDump(
829 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700830 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
831 LOG(ERROR) << "Failed to get firmware debug dump: "
832 << legacyErrorToString(legacy_status);
833 return {createWifiStatusFromLegacyError(legacy_status), {}};
834 }
835 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700836}
837
838std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700839 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700840 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800841 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700842 std::string ifname = allocateApIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800843 legacy_hal::wifi_error legacy_status =
844 legacy_hal_.lock()->createVirtualInterface(
845 ifname,
846 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
847 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
848 LOG(ERROR) << "Failed to add interface: " << ifname << " "
849 << legacyErrorToString(legacy_status);
850 return {createWifiStatusFromLegacyError(legacy_status), {}};
851 }
Patrik Fimml6beae322019-10-09 17:34:01 +0200852 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700853 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700854 for (const auto& callback : event_cb_handler_.getCallbacks()) {
855 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
856 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
857 }
858 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700859 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700860 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700861}
862
863std::pair<WifiStatus, std::vector<hidl_string>>
864WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700865 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700866 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
867 }
Roshan Pius675609b2017-10-31 14:24:58 -0700868 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700869}
870
871std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800872 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700873 const auto iface = findUsingName(ap_ifaces_, ifname);
874 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700875 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
876 }
Roshan Pius675609b2017-10-31 14:24:58 -0700877 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700878}
879
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800880WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700881 const auto iface = findUsingName(ap_ifaces_, ifname);
882 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700883 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800884 }
Roshan Pius82368502019-05-16 12:53:02 -0700885 // Invalidate & remove any dependent objects first.
886 // Note: This is probably not required because we never create
887 // nan/rtt objects over AP iface. But, there is no harm to do it
888 // here and not make that assumption all over the place.
889 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800890 legacy_hal::wifi_error legacy_status =
891 legacy_hal_.lock()->deleteVirtualInterface(ifname);
892 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
893 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
894 << legacyErrorToString(legacy_status);
895 }
Roshan Pius675609b2017-10-31 14:24:58 -0700896 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700897 for (const auto& callback : event_cb_handler_.getCallbacks()) {
898 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
899 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
900 }
901 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700902 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700903 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800904}
905
Jimmy Chend460df32019-11-29 17:31:22 +0200906std::pair<WifiStatus, sp<V1_4::IWifiNanIface>>
907WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700908 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700909 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800910 }
Roshan Pius5ba0a902020-04-14 11:55:42 -0700911 bool is_dedicated_iface = true;
912 std::string ifname = getNanIfaceName();
Nate Jiang63f34ed2020-05-20 23:22:51 -0700913 if (ifname.empty() || !iface_util_.lock()->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -0700914 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
915 // not defined.
916 ifname = getFirstActiveWlanIfaceName();
917 is_dedicated_iface = false;
918 }
919 sp<WifiNanIface> iface =
920 new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -0700921 nan_ifaces_.push_back(iface);
922 for (const auto& callback : event_cb_handler_.getCallbacks()) {
923 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
924 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
925 }
926 }
927 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700928}
929
930std::pair<WifiStatus, std::vector<hidl_string>>
931WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700932 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700933 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
934 }
Roshan Pius675609b2017-10-31 14:24:58 -0700935 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700936}
937
Jimmy Chend460df32019-11-29 17:31:22 +0200938std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800939 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700940 const auto iface = findUsingName(nan_ifaces_, ifname);
941 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700942 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
943 }
Roshan Pius675609b2017-10-31 14:24:58 -0700944 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700945}
946
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800947WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700948 const auto iface = findUsingName(nan_ifaces_, ifname);
949 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700950 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800951 }
Roshan Pius675609b2017-10-31 14:24:58 -0700952 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700953 for (const auto& callback : event_cb_handler_.getCallbacks()) {
954 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
955 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
956 }
957 }
958 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800959}
960
Roshan Pius3c868522016-10-27 12:43:49 -0700961std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700962 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700963 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800964 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700965 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700966 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
967 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700968 for (const auto& callback : event_cb_handler_.getCallbacks()) {
969 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
970 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
971 }
972 }
Roshan Pius675609b2017-10-31 14:24:58 -0700973 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700974}
975
976std::pair<WifiStatus, std::vector<hidl_string>>
977WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700978 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700979 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
980 }
Roshan Pius675609b2017-10-31 14:24:58 -0700981 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700982}
983
984std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800985 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700986 const auto iface = findUsingName(p2p_ifaces_, ifname);
987 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700988 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
989 }
Roshan Pius675609b2017-10-31 14:24:58 -0700990 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700991}
992
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800993WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700994 const auto iface = findUsingName(p2p_ifaces_, ifname);
995 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700996 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800997 }
Roshan Pius675609b2017-10-31 14:24:58 -0700998 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700999 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1000 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1001 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1002 }
1003 }
1004 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001005}
1006
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001007std::pair<WifiStatus, sp<V1_3::IWifiStaIface>>
1008WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001009 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001010 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001011 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001012 std::string ifname = allocateStaIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -08001013 legacy_hal::wifi_error legacy_status =
1014 legacy_hal_.lock()->createVirtualInterface(
1015 ifname,
1016 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
1017 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1018 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1019 << legacyErrorToString(legacy_status);
1020 return {createWifiStatusFromLegacyError(legacy_status), {}};
1021 }
Roshan Pius99dab382019-02-14 07:57:10 -08001022 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001023 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001024 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1025 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1026 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1027 }
1028 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001029 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001030 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001031}
1032
1033std::pair<WifiStatus, std::vector<hidl_string>>
1034WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001035 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001036 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1037 }
Roshan Pius675609b2017-10-31 14:24:58 -07001038 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001039}
1040
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001041std::pair<WifiStatus, sp<V1_3::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001042 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001043 const auto iface = findUsingName(sta_ifaces_, ifname);
1044 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001045 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1046 }
Roshan Pius675609b2017-10-31 14:24:58 -07001047 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001048}
1049
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001050WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001051 const auto iface = findUsingName(sta_ifaces_, ifname);
1052 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001053 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001054 }
Roshan Pius82368502019-05-16 12:53:02 -07001055 // Invalidate & remove any dependent objects first.
1056 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001057 legacy_hal::wifi_error legacy_status =
1058 legacy_hal_.lock()->deleteVirtualInterface(ifname);
1059 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1060 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1061 << legacyErrorToString(legacy_status);
1062 }
Roshan Pius675609b2017-10-31 14:24:58 -07001063 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001064 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1065 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1066 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1067 }
1068 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001069 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001070 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001071}
1072
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001073std::pair<WifiStatus, sp<V1_0::IWifiRttController>>
1074WifiChip::createRttControllerInternal(const sp<IWifiIface>& /*bound_iface*/) {
1075 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001076 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001077}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001078
1079std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1080WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001081 legacy_hal::wifi_error legacy_status;
1082 std::vector<legacy_hal::wifi_ring_buffer_status>
1083 legacy_ring_buffer_status_vec;
1084 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001085 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001086 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1087 return {createWifiStatusFromLegacyError(legacy_status), {}};
1088 }
1089 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1090 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1091 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1092 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1093 }
1094 return {createWifiStatus(WifiStatusCode::SUCCESS),
1095 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001096}
1097
1098WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001099 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1100 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1101 WifiStatus status = registerDebugRingBufferCallback();
1102 if (status.code != WifiStatusCode::SUCCESS) {
1103 return status;
1104 }
1105 legacy_hal::wifi_error legacy_status =
1106 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001107 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001108 static_cast<
1109 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1110 verbose_level),
1111 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001112 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1113 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001114 // if verbose logging enabled, turn up HAL daemon logging as well.
1115 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1116 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1117 } else {
1118 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1119 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001120 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001121}
1122
1123WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001124 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001125 WifiStatus status = registerDebugRingBufferCallback();
1126 if (status.code != WifiStatusCode::SUCCESS) {
1127 return status;
1128 }
1129 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001130 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1131 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001132
Roshan Piusabcf78f2017-10-06 16:30:38 -07001133 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001134}
1135
Roger Wangb294c762018-11-02 15:34:39 +08001136WifiStatus WifiChip::flushRingBufferToFileInternal() {
1137 if (!writeRingbufferFilesInternal()) {
1138 LOG(ERROR) << "Error writing files to flash";
1139 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1140 }
1141 return createWifiStatus(WifiStatusCode::SUCCESS);
1142}
1143
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001144WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001145 legacy_hal::wifi_error legacy_status =
1146 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001147 getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001148 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1149 debug_ring_buffer_cb_registered_ = false;
1150 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001151 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001152}
1153
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001154std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1155WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001156 legacy_hal::wifi_error legacy_status;
1157 legacy_hal::WakeReasonStats legacy_stats;
1158 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001159 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001160 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1161 return {createWifiStatusFromLegacyError(legacy_status), {}};
1162 }
1163 WifiDebugHostWakeReasonStats hidl_stats;
1164 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1165 &hidl_stats)) {
1166 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1167 }
1168 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001169}
1170
Roshan Pius203cb032016-12-14 17:41:20 -08001171WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001172 legacy_hal::wifi_error legacy_status;
1173 if (enable) {
1174 android::wp<WifiChip> weak_ptr_this(this);
1175 const auto& on_alert_callback = [weak_ptr_this](
1176 int32_t error_code,
1177 std::vector<uint8_t> debug_data) {
1178 const auto shared_ptr_this = weak_ptr_this.promote();
1179 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1180 LOG(ERROR) << "Callback invoked on an invalid object";
1181 return;
1182 }
1183 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1184 if (!callback->onDebugErrorAlert(error_code, debug_data)
1185 .isOk()) {
1186 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1187 }
1188 }
1189 };
1190 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001191 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001192 } else {
1193 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001194 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001195 }
1196 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001197}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001198
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001199WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001200 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001201 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001202 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001203 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1204 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001205}
1206
Roshan Pius735ff432017-07-25 08:48:08 -07001207WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001208 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001209 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001210 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001211}
1212
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001213WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1214 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001215 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001216 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1217 return createWifiStatusFromLegacyError(legacy_status);
1218}
1219
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001220WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001221 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
1222 // Deprecated support for this callback.
1223 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001224}
1225
Jong Wook Kimda830c92018-07-23 15:29:38 -07001226WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1227 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001228 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001229 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001230 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1231 return createWifiStatusFromLegacyError(legacy_status);
1232}
1233
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001234std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
1235 legacy_hal::wifi_error legacy_status;
1236 uint32_t legacy_feature_set;
1237 uint32_t legacy_logger_feature_set;
1238 const auto ifname = getFirstActiveWlanIfaceName();
1239 std::tie(legacy_status, legacy_feature_set) =
1240 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
1241 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1242 return {createWifiStatusFromLegacyError(legacy_status), 0};
1243 }
1244 std::tie(legacy_status, legacy_logger_feature_set) =
1245 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
1246 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1247 // some devices don't support querying logger feature set
1248 legacy_logger_feature_set = 0;
1249 }
1250 uint32_t hidl_caps;
1251 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
1252 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
1253 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1254 }
1255 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1256}
1257
Jimmy Chend460df32019-11-29 17:31:22 +02001258std::pair<WifiStatus, sp<V1_4::IWifiRttController>>
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001259WifiChip::createRttControllerInternal_1_4(const sp<IWifiIface>& bound_iface) {
1260 if (sta_ifaces_.size() == 0 &&
1261 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1262 LOG(ERROR)
1263 << "createRttControllerInternal_1_4: Chip cannot support STAs "
1264 "(and RTT by extension)";
1265 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1266 }
1267 sp<WifiRttController> rtt = new WifiRttController(
1268 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1269 rtt_controllers_.emplace_back(rtt);
1270 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1271}
1272
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001273WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +02001274 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001275 if (!event_cb_handler_.addCallback(event_callback)) {
1276 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1277 }
1278 return createWifiStatus(WifiStatusCode::SUCCESS);
1279}
1280
Roshan Piusba38d9c2017-12-08 07:32:08 -08001281WifiStatus WifiChip::handleChipConfiguration(
1282 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1283 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001284 // If the chip is already configured in a different mode, stop
1285 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001286 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001287 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1288 << " to mode " << mode_id;
1289 invalidateAndRemoveAllIfaces();
1290 legacy_hal::wifi_error legacy_status =
1291 legacy_hal_.lock()->stop(lock, []() {});
1292 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1293 LOG(ERROR) << "Failed to stop legacy HAL: "
1294 << legacyErrorToString(legacy_status);
1295 return createWifiStatusFromLegacyError(legacy_status);
1296 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001297 }
Roshan Piuscc338202017-11-02 13:54:09 -07001298 // Firmware mode change not needed for V2 devices.
1299 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001300 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001301 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001302 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001303 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1304 }
1305 if (!success) {
1306 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1307 }
1308 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1309 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1310 LOG(ERROR) << "Failed to start legacy HAL: "
1311 << legacyErrorToString(legacy_status);
1312 return createWifiStatusFromLegacyError(legacy_status);
1313 }
Roshan Pius85c64412018-01-22 17:58:40 -08001314 // Every time the HAL is restarted, we need to register the
1315 // radio mode change callback.
1316 WifiStatus status = registerRadioModeChangeCallback();
1317 if (status.code != WifiStatusCode::SUCCESS) {
1318 // This probably is not a critical failure?
1319 LOG(ERROR) << "Failed to register radio mode change callback";
1320 }
chenpaulf5eca292019-03-14 11:08:03 +08001321 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001322 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001323 version_info = WifiChip::requestChipDebugInfoInternal();
1324 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1325 property_set("vendor.wlan.firmware.version",
1326 version_info.second.firmwareDescription.c_str());
1327 property_set("vendor.wlan.driver.version",
1328 version_info.second.driverDescription.c_str());
1329 }
1330
Roshan Piusabcf78f2017-10-06 16:30:38 -07001331 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001332}
Roshan Pius48185b22016-12-15 19:10:30 -08001333
1334WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001335 if (debug_ring_buffer_cb_registered_) {
1336 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001337 }
Roshan Pius3797e182017-03-30 18:01:54 -07001338
Roshan Piusabcf78f2017-10-06 16:30:38 -07001339 android::wp<WifiChip> weak_ptr_this(this);
1340 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001341 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001342 const std::vector<uint8_t>& data,
1343 const legacy_hal::wifi_ring_buffer_status& status) {
1344 const auto shared_ptr_this = weak_ptr_this.promote();
1345 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1346 LOG(ERROR) << "Callback invoked on an invalid object";
1347 return;
1348 }
1349 WifiDebugRingBufferStatus hidl_status;
1350 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1351 status, &hidl_status)) {
1352 LOG(ERROR) << "Error converting ring buffer status";
1353 return;
1354 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301355 {
1356 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1357 const auto& target =
1358 shared_ptr_this->ringbuffer_map_.find(name);
1359 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1360 Ringbuffer& cur_buffer = target->second;
1361 cur_buffer.append(data);
1362 } else {
1363 LOG(ERROR) << "Ringname " << name << " not found";
1364 return;
1365 }
xshu0a0fe512020-07-22 17:53:37 -07001366 // unique_lock unlocked here
Roshan Piusabcf78f2017-10-06 16:30:38 -07001367 }
1368 };
1369 legacy_hal::wifi_error legacy_status =
1370 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001371 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001372
Roshan Piusabcf78f2017-10-06 16:30:38 -07001373 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1374 debug_ring_buffer_cb_registered_ = true;
1375 }
1376 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001377}
1378
Roshan Pius85c64412018-01-22 17:58:40 -08001379WifiStatus WifiChip::registerRadioModeChangeCallback() {
1380 android::wp<WifiChip> weak_ptr_this(this);
1381 const auto& on_radio_mode_change_callback =
1382 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1383 const auto shared_ptr_this = weak_ptr_this.promote();
1384 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1385 LOG(ERROR) << "Callback invoked on an invalid object";
1386 return;
1387 }
Jimmy Chend460df32019-11-29 17:31:22 +02001388 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001389 hidl_radio_mode_infos;
1390 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1391 mac_infos, &hidl_radio_mode_infos)) {
1392 LOG(ERROR) << "Error converting wifi mac info";
1393 return;
1394 }
1395 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001396 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos)
Roshan Pius85c64412018-01-22 17:58:40 -08001397 .isOk()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001398 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
Roshan Pius85c64412018-01-22 17:58:40 -08001399 << " callback on: " << toString(callback);
1400 }
1401 }
1402 };
1403 legacy_hal::wifi_error legacy_status =
1404 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001405 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001406 return createWifiStatusFromLegacyError(legacy_status);
1407}
1408
Jimmy Chend460df32019-11-29 17:31:22 +02001409std::vector<V1_4::IWifiChip::ChipIfaceCombination>
Roshan Piuscc338202017-11-02 13:54:09 -07001410WifiChip::getCurrentModeIfaceCombinations() {
1411 if (!isValidModeId(current_mode_id_)) {
1412 LOG(ERROR) << "Chip not configured in a mode yet";
1413 return {};
1414 }
1415 for (const auto& mode : modes_) {
1416 if (mode.id == current_mode_id_) {
1417 return mode.availableCombinations;
1418 }
1419 }
1420 CHECK(0) << "Expected to find iface combinations for current mode!";
1421 return {};
1422}
1423
1424// Returns a map indexed by IfaceType with the number of ifaces currently
1425// created of the corresponding type.
1426std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1427 std::map<IfaceType, size_t> iface_counts;
1428 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1429 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1430 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1431 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1432 return iface_counts;
1433}
1434
1435// This expands the provided iface combinations to a more parseable
1436// form. Returns a vector of available combinations possible with the number
1437// of ifaces of each type in the combination.
1438// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1439std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
Jimmy Chend460df32019-11-29 17:31:22 +02001440 const V1_4::IWifiChip::ChipIfaceCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001441 uint32_t num_expanded_combos = 1;
1442 for (const auto& limit : combination.limits) {
1443 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1444 num_expanded_combos *= limit.types.size();
1445 }
1446 }
1447
1448 // Allocate the vector of expanded combos and reset all iface counts to 0
1449 // in each combo.
1450 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1451 expanded_combos.resize(num_expanded_combos);
1452 for (auto& expanded_combo : expanded_combos) {
1453 for (const auto type :
1454 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1455 expanded_combo[type] = 0;
1456 }
1457 }
1458 uint32_t span = num_expanded_combos;
1459 for (const auto& limit : combination.limits) {
1460 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1461 span /= limit.types.size();
1462 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1463 const auto iface_type =
1464 limit.types[(k / span) % limit.types.size()];
1465 expanded_combos[k][iface_type]++;
1466 }
1467 }
1468 }
1469 return expanded_combos;
1470}
1471
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001472bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1473 const std::map<IfaceType, size_t>& expanded_combo,
1474 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001475 const auto current_combo = getCurrentIfaceCombination();
1476
1477 // Check if we have space for 1 more iface of |type| in this combo
1478 for (const auto type :
1479 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1480 size_t num_ifaces_needed = current_combo.at(type);
1481 if (type == requested_type) {
1482 num_ifaces_needed++;
1483 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001484 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001485 if (num_ifaces_needed > num_ifaces_allowed) {
1486 return false;
1487 }
1488 }
1489 return true;
1490}
1491
1492// This method does the following:
1493// a) Enumerate all possible iface combos by expanding the current
1494// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001495// b) Check if the requested iface type can be added to the current mode
1496// with the iface combination that is already active.
1497bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1498 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001499 if (!isValidModeId(current_mode_id_)) {
1500 LOG(ERROR) << "Chip not configured in a mode yet";
1501 return false;
1502 }
1503 const auto combinations = getCurrentModeIfaceCombinations();
1504 for (const auto& combination : combinations) {
1505 const auto expanded_combos = expandIfaceCombinations(combination);
1506 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001507 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1508 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001509 return true;
1510 }
1511 }
1512 }
1513 return false;
1514}
1515
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001516// Note: This does not consider ifaces already active. It only checks if the
1517// provided expanded iface combination can support the requested combo.
1518bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1519 const std::map<IfaceType, size_t>& expanded_combo,
1520 const std::map<IfaceType, size_t>& req_combo) {
1521 // Check if we have space for 1 more iface of |type| in this combo
1522 for (const auto type :
1523 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1524 if (req_combo.count(type) == 0) {
1525 // Iface of "type" not in the req_combo.
1526 continue;
1527 }
1528 size_t num_ifaces_needed = req_combo.at(type);
1529 size_t num_ifaces_allowed = expanded_combo.at(type);
1530 if (num_ifaces_needed > num_ifaces_allowed) {
1531 return false;
1532 }
1533 }
1534 return true;
1535}
1536// This method does the following:
1537// a) Enumerate all possible iface combos by expanding the current
1538// ChipIfaceCombination.
1539// b) Check if the requested iface combo can be added to the current mode.
1540// Note: This does not consider ifaces already active. It only checks if the
1541// current mode can support the requested combo.
1542bool WifiChip::canCurrentModeSupportIfaceCombo(
1543 const std::map<IfaceType, size_t>& req_combo) {
1544 if (!isValidModeId(current_mode_id_)) {
1545 LOG(ERROR) << "Chip not configured in a mode yet";
1546 return false;
1547 }
1548 const auto combinations = getCurrentModeIfaceCombinations();
1549 for (const auto& combination : combinations) {
1550 const auto expanded_combos = expandIfaceCombinations(combination);
1551 for (const auto& expanded_combo : expanded_combos) {
1552 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1553 req_combo)) {
1554 return true;
1555 }
1556 }
1557 }
1558 return false;
1559}
1560
1561// This method does the following:
1562// a) Enumerate all possible iface combos by expanding the current
1563// ChipIfaceCombination.
1564// b) Check if the requested iface type can be added to the current mode.
1565bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1566 // Check if we can support atleast 1 iface of type.
1567 std::map<IfaceType, size_t> req_iface_combo;
1568 req_iface_combo[requested_type] = 1;
1569 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1570}
1571
Roshan Piuscc338202017-11-02 13:54:09 -07001572bool WifiChip::isValidModeId(ChipModeId mode_id) {
1573 for (const auto& mode : modes_) {
1574 if (mode.id == mode_id) {
1575 return true;
1576 }
1577 }
1578 return false;
1579}
1580
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001581bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1582 // Check if we can support atleast 1 STA & 1 AP concurrently.
1583 std::map<IfaceType, size_t> req_iface_combo;
1584 req_iface_combo[IfaceType::AP] = 1;
1585 req_iface_combo[IfaceType::STA] = 1;
1586 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1587}
1588
James Mattisd2e4c072019-05-22 16:14:48 -07001589bool WifiChip::isDualApAllowedInCurrentMode() {
1590 // Check if we can support atleast 1 STA & 1 AP concurrently.
1591 std::map<IfaceType, size_t> req_iface_combo;
1592 req_iface_combo[IfaceType::AP] = 2;
1593 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1594}
1595
Roshan Pius6036c022019-03-27 10:41:58 -07001596std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001597 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1598 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001599 // This could happen if the chip call is made before any STA/AP
1600 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001601 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Roshan Pius6036c022019-03-27 10:41:58 -07001602 return getWlanIfaceName(0);
1603}
1604
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001605// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1606// not already in use.
1607// Note: This doesn't check the actual presence of these interfaces.
1608std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1609 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1610 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001611 if (findUsingName(ap_ifaces_, ifname)) continue;
1612 if (findUsingName(sta_ifaces_, ifname)) continue;
1613 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001614 }
1615 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001616 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001617 return {};
1618}
1619
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001620// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001621// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001622std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001623 // Check if we have a dedicated iface for AP.
1624 std::string ifname = getApIfaceName();
1625 if (!ifname.empty()) {
1626 return ifname;
1627 }
James Mattisd2e4c072019-05-22 16:14:48 -07001628 return allocateApOrStaIfaceName((isStaApConcurrencyAllowedInCurrentMode() &&
1629 !isDualApAllowedInCurrentMode())
1630 ? 1
1631 : 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001632}
1633
1634// STA iface names start with idx 0.
1635// Primary STA iface will always be 0.
1636std::string WifiChip::allocateStaIfaceName() {
1637 return allocateApOrStaIfaceName(0);
1638}
1639
xshu5899e8e2018-01-09 15:36:03 -08001640bool WifiChip::writeRingbufferFilesInternal() {
1641 if (!removeOldFilesInternal()) {
1642 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1643 return false;
1644 }
1645 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301646 {
1647 std::unique_lock<std::mutex> lk(lock_t);
1648 for (const auto& item : ringbuffer_map_) {
1649 const Ringbuffer& cur_buffer = item.second;
1650 if (cur_buffer.getData().empty()) {
1651 continue;
1652 }
1653 const std::string file_path_raw =
1654 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1655 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1656 if (dump_fd == -1) {
1657 PLOG(ERROR) << "create file failed";
1658 return false;
1659 }
1660 unique_fd file_auto_closer(dump_fd);
1661 for (const auto& cur_block : cur_buffer.getData()) {
1662 if (write(dump_fd, cur_block.data(),
1663 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1664 PLOG(ERROR) << "Error writing to file";
1665 }
xshu5899e8e2018-01-09 15:36:03 -08001666 }
1667 }
xshu0a0fe512020-07-22 17:53:37 -07001668 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001669 }
1670 return true;
1671}
1672
Roshan Pius79a99752016-10-04 13:03:58 -07001673} // namespace implementation
Jimmy Chend460df32019-11-29 17:31:22 +02001674} // namespace V1_5
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001675} // namespace wifi
1676} // namespace hardware
1677} // namespace android