blob: 9c352bc563c8818c40414adb78f90ee21f2786db [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(
Jimmy Chen2dddd792019-12-23 17:50:39 +0200335 ChipId chip_id, bool is_primary,
336 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700337 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800338 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700339 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
340 const std::function<void(const std::string&)>& handler)
Roshan Pius52947fb2016-11-18 11:38:07 -0800341 : chip_id_(chip_id),
342 legacy_hal_(legacy_hal),
343 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800344 iface_util_(iface_util),
Roshan Pius52947fb2016-11-18 11:38:07 -0800345 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800346 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
Jimmy Chen2dddd792019-12-23 17:50:39 +0200347 modes_(feature_flags.lock()->getChipModes(is_primary)),
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700348 debug_ring_buffer_cb_registered_(false),
349 subsystemCallbackHandler_(handler) {
Roshan Pius8574e7f2019-04-01 13:30:40 -0700350 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
351}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700352
Roshan Piusaabe5752016-09-29 09:03:59 -0700353void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700354 if (!writeRingbufferFilesInternal()) {
355 LOG(ERROR) << "Error writing files to flash";
356 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700357 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700358 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700359 legacy_hal_.reset();
360 event_cb_handler_.invalidate();
361 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700362}
363
Roshan Piusabcf78f2017-10-06 16:30:38 -0700364bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700365
Jimmy Chend460df32019-11-29 17:31:22 +0200366std::set<sp<V1_4::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700367 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800368}
369
Roshan Pius5c055462016-10-11 08:27:27 -0700370Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700371 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
372 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700373}
374
Jong Wook Kimda830c92018-07-23 15:29:38 -0700375// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700376Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800377 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700378 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700379 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
380 &WifiChip::registerEventCallbackInternal,
381 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700382}
383
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700384Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700385 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
386 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700387}
388
Roshan Pius5c055462016-10-11 08:27:27 -0700389Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700390 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
391 &WifiChip::getAvailableModesInternal,
392 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700393}
394
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800395Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700396 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800397 return validateAndCallWithLock(
398 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
399 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700400}
401
Roshan Pius5c055462016-10-11 08:27:27 -0700402Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700403 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
404 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700405}
406
Roshan Pius5c055462016-10-11 08:27:27 -0700407Return<void> WifiChip::requestChipDebugInfo(
408 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700409 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
410 &WifiChip::requestChipDebugInfoInternal,
411 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700412}
413
414Return<void> WifiChip::requestDriverDebugDump(
415 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700416 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
417 &WifiChip::requestDriverDebugDumpInternal,
418 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700419}
420
Roshan Pius5c055462016-10-11 08:27:27 -0700421Return<void> WifiChip::requestFirmwareDebugDump(
422 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700423 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
424 &WifiChip::requestFirmwareDebugDumpInternal,
425 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700426}
427
Roshan Pius5c055462016-10-11 08:27:27 -0700428Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700429 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
430 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700431}
432
Roshan Pius5c055462016-10-11 08:27:27 -0700433Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700434 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
435 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700436}
437
Roshan Pius5c055462016-10-11 08:27:27 -0700438Return<void> WifiChip::getApIface(const hidl_string& ifname,
439 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700440 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
441 &WifiChip::getApIfaceInternal, hidl_status_cb,
442 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700443}
444
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800445Return<void> WifiChip::removeApIface(const hidl_string& ifname,
446 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700447 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
448 &WifiChip::removeApIfaceInternal, hidl_status_cb,
449 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800450}
451
Roshan Pius5c055462016-10-11 08:27:27 -0700452Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700453 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
454 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700455}
456
Roshan Pius5c055462016-10-11 08:27:27 -0700457Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700458 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
459 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700460}
461
462Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700463 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700464 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
465 &WifiChip::getNanIfaceInternal, hidl_status_cb,
466 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700467}
468
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800469Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
470 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700471 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
472 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
473 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800474}
475
Roshan Pius5c055462016-10-11 08:27:27 -0700476Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700477 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
478 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700479}
480
Roshan Pius5c055462016-10-11 08:27:27 -0700481Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700482 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
483 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700484}
485
486Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700487 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700488 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
489 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
490 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700491}
492
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800493Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
494 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700495 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
496 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
497 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800498}
499
Roshan Pius5c055462016-10-11 08:27:27 -0700500Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700501 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
502 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700503}
504
Roshan Pius5c055462016-10-11 08:27:27 -0700505Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700506 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
507 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700508}
509
510Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700511 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700512 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
513 &WifiChip::getStaIfaceInternal, hidl_status_cb,
514 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700515}
516
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800517Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
518 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700519 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
520 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
521 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800522}
523
Roshan Pius5c055462016-10-11 08:27:27 -0700524Return<void> WifiChip::createRttController(
525 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700526 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
527 &WifiChip::createRttControllerInternal,
528 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700529}
530
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700531Return<void> WifiChip::getDebugRingBuffersStatus(
532 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700533 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
534 &WifiChip::getDebugRingBuffersStatusInternal,
535 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700536}
537
538Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700539 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
540 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700541 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700542 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
543 &WifiChip::startLoggingToDebugRingBufferInternal,
544 hidl_status_cb, ring_name, verbose_level,
545 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700546}
547
548Return<void> WifiChip::forceDumpToDebugRingBuffer(
549 const hidl_string& ring_name,
550 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700551 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
552 &WifiChip::forceDumpToDebugRingBufferInternal,
553 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700554}
555
Roger Wangb294c762018-11-02 15:34:39 +0800556Return<void> WifiChip::flushRingBufferToFile(
557 flushRingBufferToFile_cb hidl_status_cb) {
558 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
559 &WifiChip::flushRingBufferToFileInternal,
560 hidl_status_cb);
561}
562
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800563Return<void> WifiChip::stopLoggingToDebugRingBuffer(
564 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700565 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
566 &WifiChip::stopLoggingToDebugRingBufferInternal,
567 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800568}
569
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700570Return<void> WifiChip::getDebugHostWakeReasonStats(
571 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700572 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
573 &WifiChip::getDebugHostWakeReasonStatsInternal,
574 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700575}
576
Roshan Pius203cb032016-12-14 17:41:20 -0800577Return<void> WifiChip::enableDebugErrorAlerts(
578 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700579 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
580 &WifiChip::enableDebugErrorAlertsInternal,
581 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800582}
583
Roshan Pius735ff432017-07-25 08:48:08 -0700584Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700585 V1_1::IWifiChip::TxPowerScenario scenario,
586 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700587 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
588 &WifiChip::selectTxPowerScenarioInternal,
589 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700590}
591
Roshan Pius735ff432017-07-25 08:48:08 -0700592Return<void> WifiChip::resetTxPowerScenario(
593 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700594 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
595 &WifiChip::resetTxPowerScenarioInternal,
596 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700597}
598
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700599Return<void> WifiChip::setLatencyMode(LatencyMode mode,
600 setLatencyMode_cb hidl_status_cb) {
601 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
602 &WifiChip::setLatencyModeInternal, hidl_status_cb,
603 mode);
604}
605
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800606Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700607 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800608 registerEventCallback_cb hidl_status_cb) {
609 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
610 &WifiChip::registerEventCallbackInternal_1_2,
611 hidl_status_cb, event_callback);
612}
613
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800614Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700615 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800616 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700617 &WifiChip::selectTxPowerScenarioInternal_1_2,
618 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800619}
620
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700621Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
622 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
623 &WifiChip::getCapabilitiesInternal_1_3,
624 hidl_status_cb);
625}
626
xshu5899e8e2018-01-09 15:36:03 -0800627Return<void> WifiChip::debug(const hidl_handle& handle,
628 const hidl_vec<hidl_string>&) {
629 if (handle != nullptr && handle->numFds >= 1) {
xshu0a0fe512020-07-22 17:53:37 -0700630 {
631 std::unique_lock<std::mutex> lk(lock_t);
632 for (const auto& item : ringbuffer_map_) {
633 forceDumpToDebugRingBufferInternal(item.first);
634 }
635 // unique_lock unlocked here
636 }
637 usleep(100 * 1000); // sleep for 100 milliseconds to wait for
638 // ringbuffer updates.
xshu5899e8e2018-01-09 15:36:03 -0800639 int fd = handle->data[0];
640 if (!writeRingbufferFilesInternal()) {
641 LOG(ERROR) << "Error writing files to flash";
642 }
xshu4cb33162018-01-24 15:40:06 -0800643 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800644 if (n_error != 0) {
645 LOG(ERROR) << n_error << " errors occured in cpio function";
646 }
647 fsync(fd);
648 } else {
649 LOG(ERROR) << "File handle error";
650 }
651 return Void();
652}
653
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700654Return<void> WifiChip::createRttController_1_4(
655 const sp<IWifiIface>& bound_iface,
656 createRttController_1_4_cb hidl_status_cb) {
657 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
658 &WifiChip::createRttControllerInternal_1_4,
659 hidl_status_cb, bound_iface);
660}
661
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800662Return<void> WifiChip::registerEventCallback_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +0200663 const sp<V1_4::IWifiChipEventCallback>& event_callback,
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800664 registerEventCallback_cb hidl_status_cb) {
665 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
666 &WifiChip::registerEventCallbackInternal_1_4,
667 hidl_status_cb, event_callback);
668}
669
Roshan Pius35d958c2016-10-06 16:47:38 -0700670void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700671 invalidateAndClearAll(ap_ifaces_);
672 invalidateAndClearAll(nan_ifaces_);
673 invalidateAndClearAll(p2p_ifaces_);
674 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700675 // Since all the ifaces are invalid now, all RTT controller objects
676 // using those ifaces also need to be invalidated.
677 for (const auto& rtt : rtt_controllers_) {
678 rtt->invalidate();
679 }
680 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700681}
682
Roshan Pius82368502019-05-16 12:53:02 -0700683void WifiChip::invalidateAndRemoveDependencies(
684 const std::string& removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200685 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
686 auto nan_iface = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700687 if (nan_iface->getName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200688 nan_iface->invalidate();
Roshan Pius82368502019-05-16 12:53:02 -0700689 for (const auto& callback : event_cb_handler_.getCallbacks()) {
690 if (!callback
691 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
692 .isOk()) {
693 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
694 }
695 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200696 it = nan_ifaces_.erase(it);
697 } else {
698 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700699 }
700 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200701
702 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
703 auto rtt = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700704 if (rtt->getIfaceName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200705 rtt->invalidate();
706 it = rtt_controllers_.erase(it);
707 } else {
708 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700709 }
710 }
711}
712
Roshan Pius3c868522016-10-27 12:43:49 -0700713std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700714 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700715}
716
717WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800718 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
719 // Deprecated support for this callback.
720 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700721}
722
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700723std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700724 // Deprecated support for this callback.
725 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
726}
727
Jimmy Chend460df32019-11-29 17:31:22 +0200728std::pair<WifiStatus, std::vector<V1_4::IWifiChip::ChipMode>>
Roshan Pius3c868522016-10-27 12:43:49 -0700729WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700730 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700731}
732
Roshan Piusba38d9c2017-12-08 07:32:08 -0800733WifiStatus WifiChip::configureChipInternal(
734 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
735 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700736 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700737 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
738 }
739 if (mode_id == current_mode_id_) {
740 LOG(DEBUG) << "Already in the specified mode " << mode_id;
741 return createWifiStatus(WifiStatusCode::SUCCESS);
742 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800743 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700744 if (status.code != WifiStatusCode::SUCCESS) {
745 for (const auto& callback : event_cb_handler_.getCallbacks()) {
746 if (!callback->onChipReconfigureFailure(status).isOk()) {
747 LOG(ERROR)
748 << "Failed to invoke onChipReconfigureFailure callback";
749 }
750 }
751 return status;
752 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800753 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700754 if (!callback->onChipReconfigured(mode_id).isOk()) {
755 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
756 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800757 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700758 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800759 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700760 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700761
762 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(
763 subsystemCallbackHandler_);
764
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800765 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700766}
767
768std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700769 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700770 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
771 current_mode_id_};
772 }
773 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700774}
775
Jimmy Chend460df32019-11-29 17:31:22 +0200776std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo>
Roshan Pius3c868522016-10-27 12:43:49 -0700777WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200778 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700779 legacy_hal::wifi_error legacy_status;
780 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700781 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700782 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800783 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700784 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
785 LOG(ERROR) << "Failed to get driver version: "
786 << legacyErrorToString(legacy_status);
787 WifiStatus status = createWifiStatusFromLegacyError(
788 legacy_status, "failed to get driver version");
789 return {status, result};
790 }
791 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700792
Roshan Piusabcf78f2017-10-06 16:30:38 -0700793 std::string firmware_desc;
794 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800795 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700796 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
797 LOG(ERROR) << "Failed to get firmware version: "
798 << legacyErrorToString(legacy_status);
799 WifiStatus status = createWifiStatusFromLegacyError(
800 legacy_status, "failed to get firmware version");
801 return {status, result};
802 }
803 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700804
Roshan Piusabcf78f2017-10-06 16:30:38 -0700805 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700806}
807
808std::pair<WifiStatus, std::vector<uint8_t>>
809WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700810 legacy_hal::wifi_error legacy_status;
811 std::vector<uint8_t> driver_dump;
812 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700813 legacy_hal_.lock()->requestDriverMemoryDump(
814 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700815 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
816 LOG(ERROR) << "Failed to get driver debug dump: "
817 << legacyErrorToString(legacy_status);
818 return {createWifiStatusFromLegacyError(legacy_status),
819 std::vector<uint8_t>()};
820 }
821 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700822}
823
824std::pair<WifiStatus, std::vector<uint8_t>>
825WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700826 legacy_hal::wifi_error legacy_status;
827 std::vector<uint8_t> firmware_dump;
828 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700829 legacy_hal_.lock()->requestFirmwareMemoryDump(
830 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700831 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
832 LOG(ERROR) << "Failed to get firmware debug dump: "
833 << legacyErrorToString(legacy_status);
834 return {createWifiStatusFromLegacyError(legacy_status), {}};
835 }
836 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700837}
838
839std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700840 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700841 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800842 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700843 std::string ifname = allocateApIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800844 legacy_hal::wifi_error legacy_status =
845 legacy_hal_.lock()->createVirtualInterface(
846 ifname,
847 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
848 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
849 LOG(ERROR) << "Failed to add interface: " << ifname << " "
850 << legacyErrorToString(legacy_status);
851 return {createWifiStatusFromLegacyError(legacy_status), {}};
852 }
Patrik Fimml6beae322019-10-09 17:34:01 +0200853 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700854 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700855 for (const auto& callback : event_cb_handler_.getCallbacks()) {
856 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
857 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
858 }
859 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700860 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700861 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700862}
863
864std::pair<WifiStatus, std::vector<hidl_string>>
865WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700866 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700867 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
868 }
Roshan Pius675609b2017-10-31 14:24:58 -0700869 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700870}
871
872std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800873 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700874 const auto iface = findUsingName(ap_ifaces_, ifname);
875 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700876 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
877 }
Roshan Pius675609b2017-10-31 14:24:58 -0700878 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700879}
880
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800881WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700882 const auto iface = findUsingName(ap_ifaces_, ifname);
883 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700884 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800885 }
Roshan Pius82368502019-05-16 12:53:02 -0700886 // Invalidate & remove any dependent objects first.
887 // Note: This is probably not required because we never create
888 // nan/rtt objects over AP iface. But, there is no harm to do it
889 // here and not make that assumption all over the place.
890 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800891 legacy_hal::wifi_error legacy_status =
892 legacy_hal_.lock()->deleteVirtualInterface(ifname);
893 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
894 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
895 << legacyErrorToString(legacy_status);
896 }
Roshan Pius675609b2017-10-31 14:24:58 -0700897 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700898 for (const auto& callback : event_cb_handler_.getCallbacks()) {
899 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
900 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
901 }
902 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700903 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700904 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800905}
906
Jimmy Chend460df32019-11-29 17:31:22 +0200907std::pair<WifiStatus, sp<V1_4::IWifiNanIface>>
908WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700909 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700910 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800911 }
Roshan Pius5ba0a902020-04-14 11:55:42 -0700912 bool is_dedicated_iface = true;
913 std::string ifname = getNanIfaceName();
Nate Jiang63f34ed2020-05-20 23:22:51 -0700914 if (ifname.empty() || !iface_util_.lock()->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -0700915 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
916 // not defined.
917 ifname = getFirstActiveWlanIfaceName();
918 is_dedicated_iface = false;
919 }
920 sp<WifiNanIface> iface =
921 new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -0700922 nan_ifaces_.push_back(iface);
923 for (const auto& callback : event_cb_handler_.getCallbacks()) {
924 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
925 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
926 }
927 }
928 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700929}
930
931std::pair<WifiStatus, std::vector<hidl_string>>
932WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700933 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700934 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
935 }
Roshan Pius675609b2017-10-31 14:24:58 -0700936 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700937}
938
Jimmy Chend460df32019-11-29 17:31:22 +0200939std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800940 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700941 const auto iface = findUsingName(nan_ifaces_, ifname);
942 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700943 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
944 }
Roshan Pius675609b2017-10-31 14:24:58 -0700945 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700946}
947
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800948WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700949 const auto iface = findUsingName(nan_ifaces_, ifname);
950 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700951 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800952 }
Roshan Pius675609b2017-10-31 14:24:58 -0700953 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700954 for (const auto& callback : event_cb_handler_.getCallbacks()) {
955 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
956 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
957 }
958 }
959 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800960}
961
Roshan Pius3c868522016-10-27 12:43:49 -0700962std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700963 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700964 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800965 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700966 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700967 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
968 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700969 for (const auto& callback : event_cb_handler_.getCallbacks()) {
970 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
971 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
972 }
973 }
Roshan Pius675609b2017-10-31 14:24:58 -0700974 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700975}
976
977std::pair<WifiStatus, std::vector<hidl_string>>
978WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700979 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700980 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
981 }
Roshan Pius675609b2017-10-31 14:24:58 -0700982 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700983}
984
985std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800986 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700987 const auto iface = findUsingName(p2p_ifaces_, ifname);
988 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700989 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
990 }
Roshan Pius675609b2017-10-31 14:24:58 -0700991 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700992}
993
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800994WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700995 const auto iface = findUsingName(p2p_ifaces_, ifname);
996 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700997 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800998 }
Roshan Pius675609b2017-10-31 14:24:58 -0700999 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001000 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1001 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1002 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1003 }
1004 }
1005 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001006}
1007
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001008std::pair<WifiStatus, sp<V1_3::IWifiStaIface>>
1009WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001010 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001011 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001012 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001013 std::string ifname = allocateStaIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -08001014 legacy_hal::wifi_error legacy_status =
1015 legacy_hal_.lock()->createVirtualInterface(
1016 ifname,
1017 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
1018 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1019 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1020 << legacyErrorToString(legacy_status);
1021 return {createWifiStatusFromLegacyError(legacy_status), {}};
1022 }
Roshan Pius99dab382019-02-14 07:57:10 -08001023 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001024 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001025 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1026 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1027 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1028 }
1029 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001030 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001031 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001032}
1033
1034std::pair<WifiStatus, std::vector<hidl_string>>
1035WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001036 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001037 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1038 }
Roshan Pius675609b2017-10-31 14:24:58 -07001039 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001040}
1041
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001042std::pair<WifiStatus, sp<V1_3::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001043 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001044 const auto iface = findUsingName(sta_ifaces_, ifname);
1045 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001046 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1047 }
Roshan Pius675609b2017-10-31 14:24:58 -07001048 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001049}
1050
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001051WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001052 const auto iface = findUsingName(sta_ifaces_, ifname);
1053 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001054 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001055 }
Roshan Pius82368502019-05-16 12:53:02 -07001056 // Invalidate & remove any dependent objects first.
1057 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001058 legacy_hal::wifi_error legacy_status =
1059 legacy_hal_.lock()->deleteVirtualInterface(ifname);
1060 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1061 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1062 << legacyErrorToString(legacy_status);
1063 }
Roshan Pius675609b2017-10-31 14:24:58 -07001064 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001065 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1066 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1067 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1068 }
1069 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001070 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001071 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001072}
1073
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001074std::pair<WifiStatus, sp<V1_0::IWifiRttController>>
1075WifiChip::createRttControllerInternal(const sp<IWifiIface>& /*bound_iface*/) {
1076 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001077 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001078}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001079
1080std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1081WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001082 legacy_hal::wifi_error legacy_status;
1083 std::vector<legacy_hal::wifi_ring_buffer_status>
1084 legacy_ring_buffer_status_vec;
1085 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001086 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001087 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1088 return {createWifiStatusFromLegacyError(legacy_status), {}};
1089 }
1090 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1091 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1092 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1093 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1094 }
1095 return {createWifiStatus(WifiStatusCode::SUCCESS),
1096 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001097}
1098
1099WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001100 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1101 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1102 WifiStatus status = registerDebugRingBufferCallback();
1103 if (status.code != WifiStatusCode::SUCCESS) {
1104 return status;
1105 }
1106 legacy_hal::wifi_error legacy_status =
1107 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001108 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001109 static_cast<
1110 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1111 verbose_level),
1112 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001113 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1114 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001115 // if verbose logging enabled, turn up HAL daemon logging as well.
1116 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1117 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1118 } else {
1119 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1120 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001121 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001122}
1123
1124WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001125 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001126 WifiStatus status = registerDebugRingBufferCallback();
1127 if (status.code != WifiStatusCode::SUCCESS) {
1128 return status;
1129 }
1130 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001131 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1132 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001133
Roshan Piusabcf78f2017-10-06 16:30:38 -07001134 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001135}
1136
Roger Wangb294c762018-11-02 15:34:39 +08001137WifiStatus WifiChip::flushRingBufferToFileInternal() {
1138 if (!writeRingbufferFilesInternal()) {
1139 LOG(ERROR) << "Error writing files to flash";
1140 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1141 }
1142 return createWifiStatus(WifiStatusCode::SUCCESS);
1143}
1144
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001145WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001146 legacy_hal::wifi_error legacy_status =
1147 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001148 getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001149 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1150 debug_ring_buffer_cb_registered_ = false;
1151 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001152 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001153}
1154
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001155std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1156WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001157 legacy_hal::wifi_error legacy_status;
1158 legacy_hal::WakeReasonStats legacy_stats;
1159 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001160 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001161 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1162 return {createWifiStatusFromLegacyError(legacy_status), {}};
1163 }
1164 WifiDebugHostWakeReasonStats hidl_stats;
1165 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1166 &hidl_stats)) {
1167 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1168 }
1169 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001170}
1171
Roshan Pius203cb032016-12-14 17:41:20 -08001172WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001173 legacy_hal::wifi_error legacy_status;
1174 if (enable) {
1175 android::wp<WifiChip> weak_ptr_this(this);
1176 const auto& on_alert_callback = [weak_ptr_this](
1177 int32_t error_code,
1178 std::vector<uint8_t> debug_data) {
1179 const auto shared_ptr_this = weak_ptr_this.promote();
1180 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1181 LOG(ERROR) << "Callback invoked on an invalid object";
1182 return;
1183 }
1184 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1185 if (!callback->onDebugErrorAlert(error_code, debug_data)
1186 .isOk()) {
1187 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1188 }
1189 }
1190 };
1191 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001192 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001193 } else {
1194 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001195 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001196 }
1197 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001198}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001199
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001200WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001201 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001202 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001203 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001204 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1205 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001206}
1207
Roshan Pius735ff432017-07-25 08:48:08 -07001208WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001209 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001210 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001211 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001212}
1213
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001214WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1215 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001216 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001217 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1218 return createWifiStatusFromLegacyError(legacy_status);
1219}
1220
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001221WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001222 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
1223 // Deprecated support for this callback.
1224 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001225}
1226
Jong Wook Kimda830c92018-07-23 15:29:38 -07001227WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1228 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001229 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001230 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001231 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1232 return createWifiStatusFromLegacyError(legacy_status);
1233}
1234
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001235std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
1236 legacy_hal::wifi_error legacy_status;
1237 uint32_t legacy_feature_set;
1238 uint32_t legacy_logger_feature_set;
1239 const auto ifname = getFirstActiveWlanIfaceName();
1240 std::tie(legacy_status, legacy_feature_set) =
1241 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
1242 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1243 return {createWifiStatusFromLegacyError(legacy_status), 0};
1244 }
1245 std::tie(legacy_status, legacy_logger_feature_set) =
1246 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
1247 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1248 // some devices don't support querying logger feature set
1249 legacy_logger_feature_set = 0;
1250 }
1251 uint32_t hidl_caps;
1252 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
1253 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
1254 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1255 }
1256 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1257}
1258
Jimmy Chend460df32019-11-29 17:31:22 +02001259std::pair<WifiStatus, sp<V1_4::IWifiRttController>>
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001260WifiChip::createRttControllerInternal_1_4(const sp<IWifiIface>& bound_iface) {
1261 if (sta_ifaces_.size() == 0 &&
1262 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1263 LOG(ERROR)
1264 << "createRttControllerInternal_1_4: Chip cannot support STAs "
1265 "(and RTT by extension)";
1266 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1267 }
1268 sp<WifiRttController> rtt = new WifiRttController(
1269 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1270 rtt_controllers_.emplace_back(rtt);
1271 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1272}
1273
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001274WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +02001275 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001276 if (!event_cb_handler_.addCallback(event_callback)) {
1277 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1278 }
1279 return createWifiStatus(WifiStatusCode::SUCCESS);
1280}
1281
Roshan Piusba38d9c2017-12-08 07:32:08 -08001282WifiStatus WifiChip::handleChipConfiguration(
1283 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1284 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001285 // If the chip is already configured in a different mode, stop
1286 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001287 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001288 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1289 << " to mode " << mode_id;
1290 invalidateAndRemoveAllIfaces();
1291 legacy_hal::wifi_error legacy_status =
1292 legacy_hal_.lock()->stop(lock, []() {});
1293 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1294 LOG(ERROR) << "Failed to stop legacy HAL: "
1295 << legacyErrorToString(legacy_status);
1296 return createWifiStatusFromLegacyError(legacy_status);
1297 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001298 }
Roshan Piuscc338202017-11-02 13:54:09 -07001299 // Firmware mode change not needed for V2 devices.
1300 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001301 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001302 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001303 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001304 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1305 }
1306 if (!success) {
1307 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1308 }
1309 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1310 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1311 LOG(ERROR) << "Failed to start legacy HAL: "
1312 << legacyErrorToString(legacy_status);
1313 return createWifiStatusFromLegacyError(legacy_status);
1314 }
Roshan Pius85c64412018-01-22 17:58:40 -08001315 // Every time the HAL is restarted, we need to register the
1316 // radio mode change callback.
1317 WifiStatus status = registerRadioModeChangeCallback();
1318 if (status.code != WifiStatusCode::SUCCESS) {
1319 // This probably is not a critical failure?
1320 LOG(ERROR) << "Failed to register radio mode change callback";
1321 }
chenpaulf5eca292019-03-14 11:08:03 +08001322 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001323 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001324 version_info = WifiChip::requestChipDebugInfoInternal();
1325 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1326 property_set("vendor.wlan.firmware.version",
1327 version_info.second.firmwareDescription.c_str());
1328 property_set("vendor.wlan.driver.version",
1329 version_info.second.driverDescription.c_str());
1330 }
1331
Roshan Piusabcf78f2017-10-06 16:30:38 -07001332 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001333}
Roshan Pius48185b22016-12-15 19:10:30 -08001334
1335WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001336 if (debug_ring_buffer_cb_registered_) {
1337 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001338 }
Roshan Pius3797e182017-03-30 18:01:54 -07001339
Roshan Piusabcf78f2017-10-06 16:30:38 -07001340 android::wp<WifiChip> weak_ptr_this(this);
1341 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001342 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001343 const std::vector<uint8_t>& data,
1344 const legacy_hal::wifi_ring_buffer_status& status) {
1345 const auto shared_ptr_this = weak_ptr_this.promote();
1346 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1347 LOG(ERROR) << "Callback invoked on an invalid object";
1348 return;
1349 }
1350 WifiDebugRingBufferStatus hidl_status;
1351 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1352 status, &hidl_status)) {
1353 LOG(ERROR) << "Error converting ring buffer status";
1354 return;
1355 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301356 {
1357 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1358 const auto& target =
1359 shared_ptr_this->ringbuffer_map_.find(name);
1360 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1361 Ringbuffer& cur_buffer = target->second;
1362 cur_buffer.append(data);
1363 } else {
1364 LOG(ERROR) << "Ringname " << name << " not found";
1365 return;
1366 }
xshu0a0fe512020-07-22 17:53:37 -07001367 // unique_lock unlocked here
Roshan Piusabcf78f2017-10-06 16:30:38 -07001368 }
1369 };
1370 legacy_hal::wifi_error legacy_status =
1371 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001372 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001373
Roshan Piusabcf78f2017-10-06 16:30:38 -07001374 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1375 debug_ring_buffer_cb_registered_ = true;
1376 }
1377 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001378}
1379
Roshan Pius85c64412018-01-22 17:58:40 -08001380WifiStatus WifiChip::registerRadioModeChangeCallback() {
1381 android::wp<WifiChip> weak_ptr_this(this);
1382 const auto& on_radio_mode_change_callback =
1383 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1384 const auto shared_ptr_this = weak_ptr_this.promote();
1385 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1386 LOG(ERROR) << "Callback invoked on an invalid object";
1387 return;
1388 }
Jimmy Chend460df32019-11-29 17:31:22 +02001389 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001390 hidl_radio_mode_infos;
1391 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1392 mac_infos, &hidl_radio_mode_infos)) {
1393 LOG(ERROR) << "Error converting wifi mac info";
1394 return;
1395 }
1396 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001397 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos)
Roshan Pius85c64412018-01-22 17:58:40 -08001398 .isOk()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001399 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
Roshan Pius85c64412018-01-22 17:58:40 -08001400 << " callback on: " << toString(callback);
1401 }
1402 }
1403 };
1404 legacy_hal::wifi_error legacy_status =
1405 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001406 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001407 return createWifiStatusFromLegacyError(legacy_status);
1408}
1409
Jimmy Chend460df32019-11-29 17:31:22 +02001410std::vector<V1_4::IWifiChip::ChipIfaceCombination>
Roshan Piuscc338202017-11-02 13:54:09 -07001411WifiChip::getCurrentModeIfaceCombinations() {
1412 if (!isValidModeId(current_mode_id_)) {
1413 LOG(ERROR) << "Chip not configured in a mode yet";
1414 return {};
1415 }
1416 for (const auto& mode : modes_) {
1417 if (mode.id == current_mode_id_) {
1418 return mode.availableCombinations;
1419 }
1420 }
1421 CHECK(0) << "Expected to find iface combinations for current mode!";
1422 return {};
1423}
1424
1425// Returns a map indexed by IfaceType with the number of ifaces currently
1426// created of the corresponding type.
1427std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1428 std::map<IfaceType, size_t> iface_counts;
1429 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1430 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1431 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1432 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1433 return iface_counts;
1434}
1435
1436// This expands the provided iface combinations to a more parseable
1437// form. Returns a vector of available combinations possible with the number
1438// of ifaces of each type in the combination.
1439// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1440std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
Jimmy Chend460df32019-11-29 17:31:22 +02001441 const V1_4::IWifiChip::ChipIfaceCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001442 uint32_t num_expanded_combos = 1;
1443 for (const auto& limit : combination.limits) {
1444 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1445 num_expanded_combos *= limit.types.size();
1446 }
1447 }
1448
1449 // Allocate the vector of expanded combos and reset all iface counts to 0
1450 // in each combo.
1451 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1452 expanded_combos.resize(num_expanded_combos);
1453 for (auto& expanded_combo : expanded_combos) {
1454 for (const auto type :
1455 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1456 expanded_combo[type] = 0;
1457 }
1458 }
1459 uint32_t span = num_expanded_combos;
1460 for (const auto& limit : combination.limits) {
1461 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1462 span /= limit.types.size();
1463 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1464 const auto iface_type =
1465 limit.types[(k / span) % limit.types.size()];
1466 expanded_combos[k][iface_type]++;
1467 }
1468 }
1469 }
1470 return expanded_combos;
1471}
1472
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001473bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1474 const std::map<IfaceType, size_t>& expanded_combo,
1475 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001476 const auto current_combo = getCurrentIfaceCombination();
1477
1478 // Check if we have space for 1 more iface of |type| in this combo
1479 for (const auto type :
1480 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1481 size_t num_ifaces_needed = current_combo.at(type);
1482 if (type == requested_type) {
1483 num_ifaces_needed++;
1484 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001485 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001486 if (num_ifaces_needed > num_ifaces_allowed) {
1487 return false;
1488 }
1489 }
1490 return true;
1491}
1492
1493// This method does the following:
1494// a) Enumerate all possible iface combos by expanding the current
1495// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001496// b) Check if the requested iface type can be added to the current mode
1497// with the iface combination that is already active.
1498bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1499 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001500 if (!isValidModeId(current_mode_id_)) {
1501 LOG(ERROR) << "Chip not configured in a mode yet";
1502 return false;
1503 }
1504 const auto combinations = getCurrentModeIfaceCombinations();
1505 for (const auto& combination : combinations) {
1506 const auto expanded_combos = expandIfaceCombinations(combination);
1507 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001508 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1509 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001510 return true;
1511 }
1512 }
1513 }
1514 return false;
1515}
1516
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001517// Note: This does not consider ifaces already active. It only checks if the
1518// provided expanded iface combination can support the requested combo.
1519bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1520 const std::map<IfaceType, size_t>& expanded_combo,
1521 const std::map<IfaceType, size_t>& req_combo) {
1522 // Check if we have space for 1 more iface of |type| in this combo
1523 for (const auto type :
1524 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1525 if (req_combo.count(type) == 0) {
1526 // Iface of "type" not in the req_combo.
1527 continue;
1528 }
1529 size_t num_ifaces_needed = req_combo.at(type);
1530 size_t num_ifaces_allowed = expanded_combo.at(type);
1531 if (num_ifaces_needed > num_ifaces_allowed) {
1532 return false;
1533 }
1534 }
1535 return true;
1536}
1537// This method does the following:
1538// a) Enumerate all possible iface combos by expanding the current
1539// ChipIfaceCombination.
1540// b) Check if the requested iface combo can be added to the current mode.
1541// Note: This does not consider ifaces already active. It only checks if the
1542// current mode can support the requested combo.
1543bool WifiChip::canCurrentModeSupportIfaceCombo(
1544 const std::map<IfaceType, size_t>& req_combo) {
1545 if (!isValidModeId(current_mode_id_)) {
1546 LOG(ERROR) << "Chip not configured in a mode yet";
1547 return false;
1548 }
1549 const auto combinations = getCurrentModeIfaceCombinations();
1550 for (const auto& combination : combinations) {
1551 const auto expanded_combos = expandIfaceCombinations(combination);
1552 for (const auto& expanded_combo : expanded_combos) {
1553 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1554 req_combo)) {
1555 return true;
1556 }
1557 }
1558 }
1559 return false;
1560}
1561
1562// This method does the following:
1563// a) Enumerate all possible iface combos by expanding the current
1564// ChipIfaceCombination.
1565// b) Check if the requested iface type can be added to the current mode.
1566bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1567 // Check if we can support atleast 1 iface of type.
1568 std::map<IfaceType, size_t> req_iface_combo;
1569 req_iface_combo[requested_type] = 1;
1570 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1571}
1572
Roshan Piuscc338202017-11-02 13:54:09 -07001573bool WifiChip::isValidModeId(ChipModeId mode_id) {
1574 for (const auto& mode : modes_) {
1575 if (mode.id == mode_id) {
1576 return true;
1577 }
1578 }
1579 return false;
1580}
1581
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001582bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1583 // Check if we can support atleast 1 STA & 1 AP concurrently.
1584 std::map<IfaceType, size_t> req_iface_combo;
1585 req_iface_combo[IfaceType::AP] = 1;
1586 req_iface_combo[IfaceType::STA] = 1;
1587 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1588}
1589
James Mattisd2e4c072019-05-22 16:14:48 -07001590bool WifiChip::isDualApAllowedInCurrentMode() {
1591 // Check if we can support atleast 1 STA & 1 AP concurrently.
1592 std::map<IfaceType, size_t> req_iface_combo;
1593 req_iface_combo[IfaceType::AP] = 2;
1594 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1595}
1596
Roshan Pius6036c022019-03-27 10:41:58 -07001597std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001598 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1599 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001600 // This could happen if the chip call is made before any STA/AP
1601 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001602 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Jimmy Chen2dddd792019-12-23 17:50:39 +02001603 return getWlanIfaceNameWithType(IfaceType::STA, 0);
Roshan Pius6036c022019-03-27 10:41:58 -07001604}
1605
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001606// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1607// not already in use.
1608// Note: This doesn't check the actual presence of these interfaces.
Jimmy Chen2dddd792019-12-23 17:50:39 +02001609std::string WifiChip::allocateApOrStaIfaceName(IfaceType type,
1610 uint32_t start_idx) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001611 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001612 const auto ifname = getWlanIfaceNameWithType(type, idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001613 if (findUsingName(ap_ifaces_, ifname)) continue;
1614 if (findUsingName(sta_ifaces_, ifname)) continue;
1615 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001616 }
1617 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001618 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001619 return {};
1620}
1621
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001622// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001623// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001624std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001625 // Check if we have a dedicated iface for AP.
1626 std::string ifname = getApIfaceName();
1627 if (!ifname.empty()) {
1628 return ifname;
1629 }
Jimmy Chen2dddd792019-12-23 17:50:39 +02001630 return allocateApOrStaIfaceName(IfaceType::AP,
1631 (isStaApConcurrencyAllowedInCurrentMode() &&
James Mattisd2e4c072019-05-22 16:14:48 -07001632 !isDualApAllowedInCurrentMode())
1633 ? 1
1634 : 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001635}
1636
1637// STA iface names start with idx 0.
1638// Primary STA iface will always be 0.
1639std::string WifiChip::allocateStaIfaceName() {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001640 return allocateApOrStaIfaceName(IfaceType::STA, 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001641}
1642
xshu5899e8e2018-01-09 15:36:03 -08001643bool WifiChip::writeRingbufferFilesInternal() {
1644 if (!removeOldFilesInternal()) {
1645 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1646 return false;
1647 }
1648 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301649 {
1650 std::unique_lock<std::mutex> lk(lock_t);
1651 for (const auto& item : ringbuffer_map_) {
1652 const Ringbuffer& cur_buffer = item.second;
1653 if (cur_buffer.getData().empty()) {
1654 continue;
1655 }
1656 const std::string file_path_raw =
1657 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1658 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1659 if (dump_fd == -1) {
1660 PLOG(ERROR) << "create file failed";
1661 return false;
1662 }
1663 unique_fd file_auto_closer(dump_fd);
1664 for (const auto& cur_block : cur_buffer.getData()) {
1665 if (write(dump_fd, cur_block.data(),
1666 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1667 PLOG(ERROR) << "Error writing to file";
1668 }
xshu5899e8e2018-01-09 15:36:03 -08001669 }
1670 }
xshu0a0fe512020-07-22 17:53:37 -07001671 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001672 }
1673 return true;
1674}
1675
Jimmy Chen2dddd792019-12-23 17:50:39 +02001676std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1677 std::string ifname;
1678
1679 // let the legacy hal override the interface name
1680 legacy_hal::wifi_error err =
1681 legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
1682 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1683
1684 return getWlanIfaceName(idx);
1685}
1686
Roshan Pius79a99752016-10-04 13:03:58 -07001687} // namespace implementation
Jimmy Chend460df32019-11-29 17:31:22 +02001688} // namespace V1_5
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001689} // namespace wifi
1690} // namespace hardware
1691} // namespace android