blob: ebff722859f1a78e7667d2530576d30010af928d [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);
xshu5899e8e2018-01-09 15:36:03 -0800284 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800285 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800286 if (stat(cur_file_path.c_str(), &st) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800287 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800288 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800289 continue;
290 }
291 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
292 if (fd_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800293 PLOG(ERROR) << "Failed to open file " << cur_file_path;
xshu4cb33162018-01-24 15:40:06 -0800294 n_error++;
295 continue;
296 }
xshuf392fb42020-08-13 16:57:00 -0700297 std::string file_name_with_last_modified_time =
298 cur_file_name + "-" + std::to_string(st.st_mtime);
299 // string.size() does not include the null terminator. The cpio FreeBSD
300 // file header expects the null character to be included in the length.
301 const size_t file_name_len =
302 file_name_with_last_modified_time.size() + 1;
xshu4cb33162018-01-24 15:40:06 -0800303 unique_fd file_auto_closer(fd_read);
xshuf392fb42020-08-13 16:57:00 -0700304 if (!cpioWriteHeader(out_fd, st,
305 file_name_with_last_modified_time.c_str(),
xshu4cb33162018-01-24 15:40:06 -0800306 file_name_len)) {
307 return ++n_error;
308 }
309 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
310 if (write_error) {
311 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800312 }
313 }
xshu4cb33162018-01-24 15:40:06 -0800314 if (!cpioWriteFileTrailer(out_fd)) {
315 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800316 }
317 return n_error;
318}
319
320// Helper function to create a non-const char*.
321std::vector<char> makeCharVec(const std::string& str) {
322 std::vector<char> vec(str.size() + 1);
323 vec.assign(str.begin(), str.end());
324 vec.push_back('\0');
325 return vec;
326}
327
Roshan Piusabcf78f2017-10-06 16:30:38 -0700328} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700329
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700330namespace android {
331namespace hardware {
332namespace wifi {
Jimmy Chend460df32019-11-29 17:31:22 +0200333namespace V1_5 {
Roshan Pius79a99752016-10-04 13:03:58 -0700334namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700335using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800336using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700337
Roshan Pius52947fb2016-11-18 11:38:07 -0800338WifiChip::WifiChip(
Jimmy Chen2dddd792019-12-23 17:50:39 +0200339 ChipId chip_id, bool is_primary,
340 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700341 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800342 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700343 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
344 const std::function<void(const std::string&)>& handler)
Roshan Pius52947fb2016-11-18 11:38:07 -0800345 : chip_id_(chip_id),
346 legacy_hal_(legacy_hal),
347 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800348 iface_util_(iface_util),
Roshan Pius52947fb2016-11-18 11:38:07 -0800349 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800350 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
Jimmy Chen2dddd792019-12-23 17:50:39 +0200351 modes_(feature_flags.lock()->getChipModes(is_primary)),
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700352 debug_ring_buffer_cb_registered_(false),
353 subsystemCallbackHandler_(handler) {
Roshan Pius8574e7f2019-04-01 13:30:40 -0700354 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
355}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700356
Roshan Piusaabe5752016-09-29 09:03:59 -0700357void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700358 if (!writeRingbufferFilesInternal()) {
359 LOG(ERROR) << "Error writing files to flash";
360 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700361 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700362 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700363 legacy_hal_.reset();
364 event_cb_handler_.invalidate();
365 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700366}
367
Roshan Piusabcf78f2017-10-06 16:30:38 -0700368bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700369
Jimmy Chend460df32019-11-29 17:31:22 +0200370std::set<sp<V1_4::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700371 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800372}
373
Roshan Pius5c055462016-10-11 08:27:27 -0700374Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700375 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
376 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700377}
378
Jong Wook Kimda830c92018-07-23 15:29:38 -0700379// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700380Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800381 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700382 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700383 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
384 &WifiChip::registerEventCallbackInternal,
385 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700386}
387
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700388Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700389 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
390 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700391}
392
Roshan Pius5c055462016-10-11 08:27:27 -0700393Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700394 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
395 &WifiChip::getAvailableModesInternal,
396 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700397}
398
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800399Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700400 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800401 return validateAndCallWithLock(
402 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
403 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700404}
405
Roshan Pius5c055462016-10-11 08:27:27 -0700406Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700407 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
408 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700409}
410
Roshan Pius5c055462016-10-11 08:27:27 -0700411Return<void> WifiChip::requestChipDebugInfo(
412 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700413 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
414 &WifiChip::requestChipDebugInfoInternal,
415 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700416}
417
418Return<void> WifiChip::requestDriverDebugDump(
419 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700420 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
421 &WifiChip::requestDriverDebugDumpInternal,
422 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700423}
424
Roshan Pius5c055462016-10-11 08:27:27 -0700425Return<void> WifiChip::requestFirmwareDebugDump(
426 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700427 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
428 &WifiChip::requestFirmwareDebugDumpInternal,
429 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700430}
431
Roshan Pius5c055462016-10-11 08:27:27 -0700432Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700433 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
434 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700435}
436
Roshan Pius5c055462016-10-11 08:27:27 -0700437Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700438 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
439 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700440}
441
Roshan Pius5c055462016-10-11 08:27:27 -0700442Return<void> WifiChip::getApIface(const hidl_string& ifname,
443 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700444 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
445 &WifiChip::getApIfaceInternal, hidl_status_cb,
446 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700447}
448
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800449Return<void> WifiChip::removeApIface(const hidl_string& ifname,
450 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700451 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
452 &WifiChip::removeApIfaceInternal, hidl_status_cb,
453 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800454}
455
Roshan Pius5c055462016-10-11 08:27:27 -0700456Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700457 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
458 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700459}
460
Roshan Pius5c055462016-10-11 08:27:27 -0700461Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700462 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
463 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700464}
465
466Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700467 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700468 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
469 &WifiChip::getNanIfaceInternal, hidl_status_cb,
470 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700471}
472
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800473Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
474 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700475 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
476 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
477 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800478}
479
Roshan Pius5c055462016-10-11 08:27:27 -0700480Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700481 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
482 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700483}
484
Roshan Pius5c055462016-10-11 08:27:27 -0700485Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700486 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
487 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700488}
489
490Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700491 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700492 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
493 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
494 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700495}
496
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800497Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
498 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700499 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
500 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
501 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800502}
503
Roshan Pius5c055462016-10-11 08:27:27 -0700504Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700505 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
506 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700507}
508
Roshan Pius5c055462016-10-11 08:27:27 -0700509Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700510 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
511 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700512}
513
514Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700515 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700516 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
517 &WifiChip::getStaIfaceInternal, hidl_status_cb,
518 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700519}
520
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800521Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
522 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700523 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
524 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
525 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800526}
527
Roshan Pius5c055462016-10-11 08:27:27 -0700528Return<void> WifiChip::createRttController(
529 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700530 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
531 &WifiChip::createRttControllerInternal,
532 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700533}
534
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700535Return<void> WifiChip::getDebugRingBuffersStatus(
536 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700537 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
538 &WifiChip::getDebugRingBuffersStatusInternal,
539 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700540}
541
542Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700543 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
544 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700545 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700546 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
547 &WifiChip::startLoggingToDebugRingBufferInternal,
548 hidl_status_cb, ring_name, verbose_level,
549 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700550}
551
552Return<void> WifiChip::forceDumpToDebugRingBuffer(
553 const hidl_string& ring_name,
554 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700555 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
556 &WifiChip::forceDumpToDebugRingBufferInternal,
557 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700558}
559
Roger Wangb294c762018-11-02 15:34:39 +0800560Return<void> WifiChip::flushRingBufferToFile(
561 flushRingBufferToFile_cb hidl_status_cb) {
562 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
563 &WifiChip::flushRingBufferToFileInternal,
564 hidl_status_cb);
565}
566
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800567Return<void> WifiChip::stopLoggingToDebugRingBuffer(
568 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700569 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
570 &WifiChip::stopLoggingToDebugRingBufferInternal,
571 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800572}
573
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700574Return<void> WifiChip::getDebugHostWakeReasonStats(
575 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700576 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
577 &WifiChip::getDebugHostWakeReasonStatsInternal,
578 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700579}
580
Roshan Pius203cb032016-12-14 17:41:20 -0800581Return<void> WifiChip::enableDebugErrorAlerts(
582 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700583 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
584 &WifiChip::enableDebugErrorAlertsInternal,
585 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800586}
587
Roshan Pius735ff432017-07-25 08:48:08 -0700588Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700589 V1_1::IWifiChip::TxPowerScenario scenario,
590 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700591 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
592 &WifiChip::selectTxPowerScenarioInternal,
593 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700594}
595
Roshan Pius735ff432017-07-25 08:48:08 -0700596Return<void> WifiChip::resetTxPowerScenario(
597 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700598 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
599 &WifiChip::resetTxPowerScenarioInternal,
600 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700601}
602
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700603Return<void> WifiChip::setLatencyMode(LatencyMode mode,
604 setLatencyMode_cb hidl_status_cb) {
605 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
606 &WifiChip::setLatencyModeInternal, hidl_status_cb,
607 mode);
608}
609
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800610Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700611 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800612 registerEventCallback_cb hidl_status_cb) {
613 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
614 &WifiChip::registerEventCallbackInternal_1_2,
615 hidl_status_cb, event_callback);
616}
617
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800618Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700619 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800620 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700621 &WifiChip::selectTxPowerScenarioInternal_1_2,
622 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800623}
624
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700625Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
626 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
627 &WifiChip::getCapabilitiesInternal_1_3,
628 hidl_status_cb);
629}
630
Jimmy Chen1bdf1a72019-12-23 17:53:40 +0200631Return<void> WifiChip::getCapabilities_1_5(
632 getCapabilities_1_5_cb hidl_status_cb) {
633 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
634 &WifiChip::getCapabilitiesInternal_1_5,
635 hidl_status_cb);
636}
637
xshu5899e8e2018-01-09 15:36:03 -0800638Return<void> WifiChip::debug(const hidl_handle& handle,
639 const hidl_vec<hidl_string>&) {
640 if (handle != nullptr && handle->numFds >= 1) {
xshu0a0fe512020-07-22 17:53:37 -0700641 {
642 std::unique_lock<std::mutex> lk(lock_t);
643 for (const auto& item : ringbuffer_map_) {
644 forceDumpToDebugRingBufferInternal(item.first);
645 }
646 // unique_lock unlocked here
647 }
648 usleep(100 * 1000); // sleep for 100 milliseconds to wait for
649 // ringbuffer updates.
xshu5899e8e2018-01-09 15:36:03 -0800650 int fd = handle->data[0];
651 if (!writeRingbufferFilesInternal()) {
652 LOG(ERROR) << "Error writing files to flash";
653 }
xshu4cb33162018-01-24 15:40:06 -0800654 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800655 if (n_error != 0) {
656 LOG(ERROR) << n_error << " errors occured in cpio function";
657 }
658 fsync(fd);
659 } else {
660 LOG(ERROR) << "File handle error";
661 }
662 return Void();
663}
664
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700665Return<void> WifiChip::createRttController_1_4(
666 const sp<IWifiIface>& bound_iface,
667 createRttController_1_4_cb hidl_status_cb) {
668 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
669 &WifiChip::createRttControllerInternal_1_4,
670 hidl_status_cb, bound_iface);
671}
672
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800673Return<void> WifiChip::registerEventCallback_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +0200674 const sp<V1_4::IWifiChipEventCallback>& event_callback,
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800675 registerEventCallback_cb hidl_status_cb) {
676 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
677 &WifiChip::registerEventCallbackInternal_1_4,
678 hidl_status_cb, event_callback);
679}
680
Roshan Pius35d958c2016-10-06 16:47:38 -0700681void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700682 invalidateAndClearAll(ap_ifaces_);
683 invalidateAndClearAll(nan_ifaces_);
684 invalidateAndClearAll(p2p_ifaces_);
685 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700686 // Since all the ifaces are invalid now, all RTT controller objects
687 // using those ifaces also need to be invalidated.
688 for (const auto& rtt : rtt_controllers_) {
689 rtt->invalidate();
690 }
691 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700692}
693
Roshan Pius82368502019-05-16 12:53:02 -0700694void WifiChip::invalidateAndRemoveDependencies(
695 const std::string& removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200696 for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) {
697 auto nan_iface = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700698 if (nan_iface->getName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200699 nan_iface->invalidate();
Roshan Pius82368502019-05-16 12:53:02 -0700700 for (const auto& callback : event_cb_handler_.getCallbacks()) {
701 if (!callback
702 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
703 .isOk()) {
704 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
705 }
706 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200707 it = nan_ifaces_.erase(it);
708 } else {
709 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700710 }
711 }
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200712
713 for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) {
714 auto rtt = *it;
Roshan Pius82368502019-05-16 12:53:02 -0700715 if (rtt->getIfaceName() == removed_iface_name) {
Jimmy Chen7a82ad82019-11-29 17:31:22 +0200716 rtt->invalidate();
717 it = rtt_controllers_.erase(it);
718 } else {
719 ++it;
Roshan Pius82368502019-05-16 12:53:02 -0700720 }
721 }
722}
723
Roshan Pius3c868522016-10-27 12:43:49 -0700724std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700725 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700726}
727
728WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800729 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
730 // Deprecated support for this callback.
731 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700732}
733
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700734std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700735 // Deprecated support for this callback.
736 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
737}
738
Jimmy Chend460df32019-11-29 17:31:22 +0200739std::pair<WifiStatus, std::vector<V1_4::IWifiChip::ChipMode>>
Roshan Pius3c868522016-10-27 12:43:49 -0700740WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700741 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700742}
743
Roshan Piusba38d9c2017-12-08 07:32:08 -0800744WifiStatus WifiChip::configureChipInternal(
745 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
746 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700747 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700748 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
749 }
750 if (mode_id == current_mode_id_) {
751 LOG(DEBUG) << "Already in the specified mode " << mode_id;
752 return createWifiStatus(WifiStatusCode::SUCCESS);
753 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800754 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700755 if (status.code != WifiStatusCode::SUCCESS) {
756 for (const auto& callback : event_cb_handler_.getCallbacks()) {
757 if (!callback->onChipReconfigureFailure(status).isOk()) {
758 LOG(ERROR)
759 << "Failed to invoke onChipReconfigureFailure callback";
760 }
761 }
762 return status;
763 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800764 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700765 if (!callback->onChipReconfigured(mode_id).isOk()) {
766 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
767 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800768 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700769 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800770 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700771 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700772
773 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(
774 subsystemCallbackHandler_);
775
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800776 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700777}
778
779std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700780 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700781 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
782 current_mode_id_};
783 }
784 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700785}
786
Jimmy Chend460df32019-11-29 17:31:22 +0200787std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo>
Roshan Pius3c868522016-10-27 12:43:49 -0700788WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200789 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700790 legacy_hal::wifi_error legacy_status;
791 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700792 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700793 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800794 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700795 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
796 LOG(ERROR) << "Failed to get driver version: "
797 << legacyErrorToString(legacy_status);
798 WifiStatus status = createWifiStatusFromLegacyError(
799 legacy_status, "failed to get driver version");
800 return {status, result};
801 }
802 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700803
Roshan Piusabcf78f2017-10-06 16:30:38 -0700804 std::string firmware_desc;
805 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800806 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700807 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
808 LOG(ERROR) << "Failed to get firmware version: "
809 << legacyErrorToString(legacy_status);
810 WifiStatus status = createWifiStatusFromLegacyError(
811 legacy_status, "failed to get firmware version");
812 return {status, result};
813 }
814 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700815
Roshan Piusabcf78f2017-10-06 16:30:38 -0700816 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700817}
818
819std::pair<WifiStatus, std::vector<uint8_t>>
820WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700821 legacy_hal::wifi_error legacy_status;
822 std::vector<uint8_t> driver_dump;
823 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700824 legacy_hal_.lock()->requestDriverMemoryDump(
825 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700826 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
827 LOG(ERROR) << "Failed to get driver debug dump: "
828 << legacyErrorToString(legacy_status);
829 return {createWifiStatusFromLegacyError(legacy_status),
830 std::vector<uint8_t>()};
831 }
832 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700833}
834
835std::pair<WifiStatus, std::vector<uint8_t>>
836WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700837 legacy_hal::wifi_error legacy_status;
838 std::vector<uint8_t> firmware_dump;
839 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700840 legacy_hal_.lock()->requestFirmwareMemoryDump(
841 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700842 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
843 LOG(ERROR) << "Failed to get firmware debug dump: "
844 << legacyErrorToString(legacy_status);
845 return {createWifiStatusFromLegacyError(legacy_status), {}};
846 }
847 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700848}
849
850std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700851 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700852 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800853 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700854 std::string ifname = allocateApIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800855 legacy_hal::wifi_error legacy_status =
856 legacy_hal_.lock()->createVirtualInterface(
857 ifname,
858 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
859 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
860 LOG(ERROR) << "Failed to add interface: " << ifname << " "
861 << legacyErrorToString(legacy_status);
862 return {createWifiStatusFromLegacyError(legacy_status), {}};
863 }
Patrik Fimml6beae322019-10-09 17:34:01 +0200864 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700865 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700866 for (const auto& callback : event_cb_handler_.getCallbacks()) {
867 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
868 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
869 }
870 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700871 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700872 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700873}
874
875std::pair<WifiStatus, std::vector<hidl_string>>
876WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700877 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700878 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
879 }
Roshan Pius675609b2017-10-31 14:24:58 -0700880 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700881}
882
883std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800884 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700885 const auto iface = findUsingName(ap_ifaces_, ifname);
886 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700887 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
888 }
Roshan Pius675609b2017-10-31 14:24:58 -0700889 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700890}
891
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800892WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700893 const auto iface = findUsingName(ap_ifaces_, ifname);
894 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700895 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800896 }
Roshan Pius82368502019-05-16 12:53:02 -0700897 // Invalidate & remove any dependent objects first.
898 // Note: This is probably not required because we never create
899 // nan/rtt objects over AP iface. But, there is no harm to do it
900 // here and not make that assumption all over the place.
901 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800902 legacy_hal::wifi_error legacy_status =
903 legacy_hal_.lock()->deleteVirtualInterface(ifname);
904 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
905 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
906 << legacyErrorToString(legacy_status);
907 }
Roshan Pius675609b2017-10-31 14:24:58 -0700908 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700909 for (const auto& callback : event_cb_handler_.getCallbacks()) {
910 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
911 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
912 }
913 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700914 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700915 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800916}
917
Jimmy Chend460df32019-11-29 17:31:22 +0200918std::pair<WifiStatus, sp<V1_4::IWifiNanIface>>
919WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700920 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700921 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800922 }
Roshan Pius5ba0a902020-04-14 11:55:42 -0700923 bool is_dedicated_iface = true;
924 std::string ifname = getNanIfaceName();
Nate Jiang63f34ed2020-05-20 23:22:51 -0700925 if (ifname.empty() || !iface_util_.lock()->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -0700926 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
927 // not defined.
928 ifname = getFirstActiveWlanIfaceName();
929 is_dedicated_iface = false;
930 }
931 sp<WifiNanIface> iface =
932 new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -0700933 nan_ifaces_.push_back(iface);
934 for (const auto& callback : event_cb_handler_.getCallbacks()) {
935 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
936 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
937 }
938 }
939 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700940}
941
942std::pair<WifiStatus, std::vector<hidl_string>>
943WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700944 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700945 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
946 }
Roshan Pius675609b2017-10-31 14:24:58 -0700947 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700948}
949
Jimmy Chend460df32019-11-29 17:31:22 +0200950std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800951 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700952 const auto iface = findUsingName(nan_ifaces_, ifname);
953 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700954 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
955 }
Roshan Pius675609b2017-10-31 14:24:58 -0700956 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700957}
958
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800959WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700960 const auto iface = findUsingName(nan_ifaces_, ifname);
961 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700962 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800963 }
Roshan Pius675609b2017-10-31 14:24:58 -0700964 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700965 for (const auto& callback : event_cb_handler_.getCallbacks()) {
966 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
967 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
968 }
969 }
970 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800971}
972
Roshan Pius3c868522016-10-27 12:43:49 -0700973std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700974 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700975 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800976 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700977 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700978 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
979 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700980 for (const auto& callback : event_cb_handler_.getCallbacks()) {
981 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
982 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
983 }
984 }
Roshan Pius675609b2017-10-31 14:24:58 -0700985 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700986}
987
988std::pair<WifiStatus, std::vector<hidl_string>>
989WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700990 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700991 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
992 }
Roshan Pius675609b2017-10-31 14:24:58 -0700993 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700994}
995
996std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800997 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700998 const auto iface = findUsingName(p2p_ifaces_, ifname);
999 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001000 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1001 }
Roshan Pius675609b2017-10-31 14:24:58 -07001002 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001003}
1004
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001005WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001006 const auto iface = findUsingName(p2p_ifaces_, ifname);
1007 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001008 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001009 }
Roshan Pius675609b2017-10-31 14:24:58 -07001010 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001011 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1012 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
1013 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1014 }
1015 }
1016 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001017}
1018
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001019std::pair<WifiStatus, sp<V1_3::IWifiStaIface>>
1020WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001021 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001022 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001023 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001024 std::string ifname = allocateStaIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -08001025 legacy_hal::wifi_error legacy_status =
1026 legacy_hal_.lock()->createVirtualInterface(
1027 ifname,
1028 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
1029 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1030 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1031 << legacyErrorToString(legacy_status);
1032 return {createWifiStatusFromLegacyError(legacy_status), {}};
1033 }
Roshan Pius99dab382019-02-14 07:57:10 -08001034 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001035 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001036 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1037 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1038 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1039 }
1040 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001041 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001042 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001043}
1044
1045std::pair<WifiStatus, std::vector<hidl_string>>
1046WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001047 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001048 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1049 }
Roshan Pius675609b2017-10-31 14:24:58 -07001050 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001051}
1052
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001053std::pair<WifiStatus, sp<V1_3::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001054 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001055 const auto iface = findUsingName(sta_ifaces_, ifname);
1056 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001057 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1058 }
Roshan Pius675609b2017-10-31 14:24:58 -07001059 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001060}
1061
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001062WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001063 const auto iface = findUsingName(sta_ifaces_, ifname);
1064 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001065 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001066 }
Roshan Pius82368502019-05-16 12:53:02 -07001067 // Invalidate & remove any dependent objects first.
1068 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001069 legacy_hal::wifi_error legacy_status =
1070 legacy_hal_.lock()->deleteVirtualInterface(ifname);
1071 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1072 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1073 << legacyErrorToString(legacy_status);
1074 }
Roshan Pius675609b2017-10-31 14:24:58 -07001075 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001076 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1077 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1078 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1079 }
1080 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001081 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001082 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001083}
1084
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001085std::pair<WifiStatus, sp<V1_0::IWifiRttController>>
1086WifiChip::createRttControllerInternal(const sp<IWifiIface>& /*bound_iface*/) {
1087 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001088 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001089}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001090
1091std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1092WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001093 legacy_hal::wifi_error legacy_status;
1094 std::vector<legacy_hal::wifi_ring_buffer_status>
1095 legacy_ring_buffer_status_vec;
1096 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001097 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001098 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1099 return {createWifiStatusFromLegacyError(legacy_status), {}};
1100 }
1101 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1102 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1103 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1104 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1105 }
1106 return {createWifiStatus(WifiStatusCode::SUCCESS),
1107 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001108}
1109
1110WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001111 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1112 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1113 WifiStatus status = registerDebugRingBufferCallback();
1114 if (status.code != WifiStatusCode::SUCCESS) {
1115 return status;
1116 }
1117 legacy_hal::wifi_error legacy_status =
1118 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001119 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001120 static_cast<
1121 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1122 verbose_level),
1123 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001124 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1125 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001126 // if verbose logging enabled, turn up HAL daemon logging as well.
1127 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1128 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1129 } else {
1130 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1131 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001132 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001133}
1134
1135WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001136 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001137 WifiStatus status = registerDebugRingBufferCallback();
1138 if (status.code != WifiStatusCode::SUCCESS) {
1139 return status;
1140 }
1141 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001142 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1143 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001144
Roshan Piusabcf78f2017-10-06 16:30:38 -07001145 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001146}
1147
Roger Wangb294c762018-11-02 15:34:39 +08001148WifiStatus WifiChip::flushRingBufferToFileInternal() {
1149 if (!writeRingbufferFilesInternal()) {
1150 LOG(ERROR) << "Error writing files to flash";
1151 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1152 }
1153 return createWifiStatus(WifiStatusCode::SUCCESS);
1154}
1155
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001156WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001157 legacy_hal::wifi_error legacy_status =
1158 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001159 getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001160 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1161 debug_ring_buffer_cb_registered_ = false;
1162 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001163 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001164}
1165
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001166std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1167WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001168 legacy_hal::wifi_error legacy_status;
1169 legacy_hal::WakeReasonStats legacy_stats;
1170 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001171 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001172 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1173 return {createWifiStatusFromLegacyError(legacy_status), {}};
1174 }
1175 WifiDebugHostWakeReasonStats hidl_stats;
1176 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1177 &hidl_stats)) {
1178 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1179 }
1180 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001181}
1182
Roshan Pius203cb032016-12-14 17:41:20 -08001183WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001184 legacy_hal::wifi_error legacy_status;
1185 if (enable) {
1186 android::wp<WifiChip> weak_ptr_this(this);
1187 const auto& on_alert_callback = [weak_ptr_this](
1188 int32_t error_code,
1189 std::vector<uint8_t> debug_data) {
1190 const auto shared_ptr_this = weak_ptr_this.promote();
1191 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1192 LOG(ERROR) << "Callback invoked on an invalid object";
1193 return;
1194 }
1195 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1196 if (!callback->onDebugErrorAlert(error_code, debug_data)
1197 .isOk()) {
1198 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1199 }
1200 }
1201 };
1202 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001203 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001204 } else {
1205 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001206 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001207 }
1208 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001209}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001210
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001211WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001212 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001213 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001214 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001215 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1216 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001217}
1218
Roshan Pius735ff432017-07-25 08:48:08 -07001219WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001220 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001221 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001222 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001223}
1224
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001225WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1226 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001227 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001228 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1229 return createWifiStatusFromLegacyError(legacy_status);
1230}
1231
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001232WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001233 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
1234 // Deprecated support for this callback.
1235 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001236}
1237
Jong Wook Kimda830c92018-07-23 15:29:38 -07001238WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1239 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001240 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001241 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001242 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1243 return createWifiStatusFromLegacyError(legacy_status);
1244}
1245
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001246std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001247 // Deprecated support for this callback.
1248 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
1249}
1250
1251std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_5() {
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001252 legacy_hal::wifi_error legacy_status;
Jimmy Chen1bdf1a72019-12-23 17:53:40 +02001253 uint64_t legacy_feature_set;
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001254 uint32_t legacy_logger_feature_set;
1255 const auto ifname = getFirstActiveWlanIfaceName();
1256 std::tie(legacy_status, legacy_feature_set) =
1257 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
1258 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1259 return {createWifiStatusFromLegacyError(legacy_status), 0};
1260 }
1261 std::tie(legacy_status, legacy_logger_feature_set) =
1262 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
1263 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1264 // some devices don't support querying logger feature set
1265 legacy_logger_feature_set = 0;
1266 }
1267 uint32_t hidl_caps;
1268 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
1269 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
1270 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1271 }
1272 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1273}
1274
Jimmy Chend460df32019-11-29 17:31:22 +02001275std::pair<WifiStatus, sp<V1_4::IWifiRttController>>
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001276WifiChip::createRttControllerInternal_1_4(const sp<IWifiIface>& bound_iface) {
1277 if (sta_ifaces_.size() == 0 &&
1278 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1279 LOG(ERROR)
1280 << "createRttControllerInternal_1_4: Chip cannot support STAs "
1281 "(and RTT by extension)";
1282 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1283 }
1284 sp<WifiRttController> rtt = new WifiRttController(
1285 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1286 rtt_controllers_.emplace_back(rtt);
1287 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1288}
1289
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001290WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +02001291 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001292 if (!event_cb_handler_.addCallback(event_callback)) {
1293 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1294 }
1295 return createWifiStatus(WifiStatusCode::SUCCESS);
1296}
1297
Roshan Piusba38d9c2017-12-08 07:32:08 -08001298WifiStatus WifiChip::handleChipConfiguration(
1299 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1300 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001301 // If the chip is already configured in a different mode, stop
1302 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001303 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001304 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1305 << " to mode " << mode_id;
1306 invalidateAndRemoveAllIfaces();
1307 legacy_hal::wifi_error legacy_status =
1308 legacy_hal_.lock()->stop(lock, []() {});
1309 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1310 LOG(ERROR) << "Failed to stop legacy HAL: "
1311 << legacyErrorToString(legacy_status);
1312 return createWifiStatusFromLegacyError(legacy_status);
1313 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001314 }
Roshan Piuscc338202017-11-02 13:54:09 -07001315 // Firmware mode change not needed for V2 devices.
1316 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001317 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001318 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001319 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001320 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1321 }
1322 if (!success) {
1323 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1324 }
1325 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1326 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1327 LOG(ERROR) << "Failed to start legacy HAL: "
1328 << legacyErrorToString(legacy_status);
1329 return createWifiStatusFromLegacyError(legacy_status);
1330 }
Roshan Pius85c64412018-01-22 17:58:40 -08001331 // Every time the HAL is restarted, we need to register the
1332 // radio mode change callback.
1333 WifiStatus status = registerRadioModeChangeCallback();
1334 if (status.code != WifiStatusCode::SUCCESS) {
1335 // This probably is not a critical failure?
1336 LOG(ERROR) << "Failed to register radio mode change callback";
1337 }
chenpaulf5eca292019-03-14 11:08:03 +08001338 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001339 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001340 version_info = WifiChip::requestChipDebugInfoInternal();
1341 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1342 property_set("vendor.wlan.firmware.version",
1343 version_info.second.firmwareDescription.c_str());
1344 property_set("vendor.wlan.driver.version",
1345 version_info.second.driverDescription.c_str());
1346 }
1347
Roshan Piusabcf78f2017-10-06 16:30:38 -07001348 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001349}
Roshan Pius48185b22016-12-15 19:10:30 -08001350
1351WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001352 if (debug_ring_buffer_cb_registered_) {
1353 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001354 }
Roshan Pius3797e182017-03-30 18:01:54 -07001355
Roshan Piusabcf78f2017-10-06 16:30:38 -07001356 android::wp<WifiChip> weak_ptr_this(this);
1357 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001358 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001359 const std::vector<uint8_t>& data,
1360 const legacy_hal::wifi_ring_buffer_status& status) {
1361 const auto shared_ptr_this = weak_ptr_this.promote();
1362 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1363 LOG(ERROR) << "Callback invoked on an invalid object";
1364 return;
1365 }
1366 WifiDebugRingBufferStatus hidl_status;
1367 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1368 status, &hidl_status)) {
1369 LOG(ERROR) << "Error converting ring buffer status";
1370 return;
1371 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301372 {
1373 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1374 const auto& target =
1375 shared_ptr_this->ringbuffer_map_.find(name);
1376 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1377 Ringbuffer& cur_buffer = target->second;
1378 cur_buffer.append(data);
1379 } else {
1380 LOG(ERROR) << "Ringname " << name << " not found";
1381 return;
1382 }
xshu0a0fe512020-07-22 17:53:37 -07001383 // unique_lock unlocked here
Roshan Piusabcf78f2017-10-06 16:30:38 -07001384 }
1385 };
1386 legacy_hal::wifi_error legacy_status =
1387 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001388 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001389
Roshan Piusabcf78f2017-10-06 16:30:38 -07001390 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1391 debug_ring_buffer_cb_registered_ = true;
1392 }
1393 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001394}
1395
Roshan Pius85c64412018-01-22 17:58:40 -08001396WifiStatus WifiChip::registerRadioModeChangeCallback() {
1397 android::wp<WifiChip> weak_ptr_this(this);
1398 const auto& on_radio_mode_change_callback =
1399 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1400 const auto shared_ptr_this = weak_ptr_this.promote();
1401 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1402 LOG(ERROR) << "Callback invoked on an invalid object";
1403 return;
1404 }
Jimmy Chend460df32019-11-29 17:31:22 +02001405 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001406 hidl_radio_mode_infos;
1407 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1408 mac_infos, &hidl_radio_mode_infos)) {
1409 LOG(ERROR) << "Error converting wifi mac info";
1410 return;
1411 }
1412 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001413 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos)
Roshan Pius85c64412018-01-22 17:58:40 -08001414 .isOk()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001415 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
Roshan Pius85c64412018-01-22 17:58:40 -08001416 << " callback on: " << toString(callback);
1417 }
1418 }
1419 };
1420 legacy_hal::wifi_error legacy_status =
1421 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001422 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001423 return createWifiStatusFromLegacyError(legacy_status);
1424}
1425
Jimmy Chend460df32019-11-29 17:31:22 +02001426std::vector<V1_4::IWifiChip::ChipIfaceCombination>
Roshan Piuscc338202017-11-02 13:54:09 -07001427WifiChip::getCurrentModeIfaceCombinations() {
1428 if (!isValidModeId(current_mode_id_)) {
1429 LOG(ERROR) << "Chip not configured in a mode yet";
1430 return {};
1431 }
1432 for (const auto& mode : modes_) {
1433 if (mode.id == current_mode_id_) {
1434 return mode.availableCombinations;
1435 }
1436 }
1437 CHECK(0) << "Expected to find iface combinations for current mode!";
1438 return {};
1439}
1440
1441// Returns a map indexed by IfaceType with the number of ifaces currently
1442// created of the corresponding type.
1443std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1444 std::map<IfaceType, size_t> iface_counts;
1445 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1446 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1447 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1448 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1449 return iface_counts;
1450}
1451
1452// This expands the provided iface combinations to a more parseable
1453// form. Returns a vector of available combinations possible with the number
1454// of ifaces of each type in the combination.
1455// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1456std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
Jimmy Chend460df32019-11-29 17:31:22 +02001457 const V1_4::IWifiChip::ChipIfaceCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001458 uint32_t num_expanded_combos = 1;
1459 for (const auto& limit : combination.limits) {
1460 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1461 num_expanded_combos *= limit.types.size();
1462 }
1463 }
1464
1465 // Allocate the vector of expanded combos and reset all iface counts to 0
1466 // in each combo.
1467 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1468 expanded_combos.resize(num_expanded_combos);
1469 for (auto& expanded_combo : expanded_combos) {
1470 for (const auto type :
1471 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1472 expanded_combo[type] = 0;
1473 }
1474 }
1475 uint32_t span = num_expanded_combos;
1476 for (const auto& limit : combination.limits) {
1477 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1478 span /= limit.types.size();
1479 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1480 const auto iface_type =
1481 limit.types[(k / span) % limit.types.size()];
1482 expanded_combos[k][iface_type]++;
1483 }
1484 }
1485 }
1486 return expanded_combos;
1487}
1488
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001489bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1490 const std::map<IfaceType, size_t>& expanded_combo,
1491 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001492 const auto current_combo = getCurrentIfaceCombination();
1493
1494 // Check if we have space for 1 more iface of |type| in this combo
1495 for (const auto type :
1496 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1497 size_t num_ifaces_needed = current_combo.at(type);
1498 if (type == requested_type) {
1499 num_ifaces_needed++;
1500 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001501 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001502 if (num_ifaces_needed > num_ifaces_allowed) {
1503 return false;
1504 }
1505 }
1506 return true;
1507}
1508
1509// This method does the following:
1510// a) Enumerate all possible iface combos by expanding the current
1511// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001512// b) Check if the requested iface type can be added to the current mode
1513// with the iface combination that is already active.
1514bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1515 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001516 if (!isValidModeId(current_mode_id_)) {
1517 LOG(ERROR) << "Chip not configured in a mode yet";
1518 return false;
1519 }
1520 const auto combinations = getCurrentModeIfaceCombinations();
1521 for (const auto& combination : combinations) {
1522 const auto expanded_combos = expandIfaceCombinations(combination);
1523 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001524 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1525 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001526 return true;
1527 }
1528 }
1529 }
1530 return false;
1531}
1532
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001533// Note: This does not consider ifaces already active. It only checks if the
1534// provided expanded iface combination can support the requested combo.
1535bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1536 const std::map<IfaceType, size_t>& expanded_combo,
1537 const std::map<IfaceType, size_t>& req_combo) {
1538 // Check if we have space for 1 more iface of |type| in this combo
1539 for (const auto type :
1540 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1541 if (req_combo.count(type) == 0) {
1542 // Iface of "type" not in the req_combo.
1543 continue;
1544 }
1545 size_t num_ifaces_needed = req_combo.at(type);
1546 size_t num_ifaces_allowed = expanded_combo.at(type);
1547 if (num_ifaces_needed > num_ifaces_allowed) {
1548 return false;
1549 }
1550 }
1551 return true;
1552}
1553// This method does the following:
1554// a) Enumerate all possible iface combos by expanding the current
1555// ChipIfaceCombination.
1556// b) Check if the requested iface combo can be added to the current mode.
1557// Note: This does not consider ifaces already active. It only checks if the
1558// current mode can support the requested combo.
1559bool WifiChip::canCurrentModeSupportIfaceCombo(
1560 const std::map<IfaceType, size_t>& req_combo) {
1561 if (!isValidModeId(current_mode_id_)) {
1562 LOG(ERROR) << "Chip not configured in a mode yet";
1563 return false;
1564 }
1565 const auto combinations = getCurrentModeIfaceCombinations();
1566 for (const auto& combination : combinations) {
1567 const auto expanded_combos = expandIfaceCombinations(combination);
1568 for (const auto& expanded_combo : expanded_combos) {
1569 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1570 req_combo)) {
1571 return true;
1572 }
1573 }
1574 }
1575 return false;
1576}
1577
1578// This method does the following:
1579// a) Enumerate all possible iface combos by expanding the current
1580// ChipIfaceCombination.
1581// b) Check if the requested iface type can be added to the current mode.
1582bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1583 // Check if we can support atleast 1 iface of type.
1584 std::map<IfaceType, size_t> req_iface_combo;
1585 req_iface_combo[requested_type] = 1;
1586 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1587}
1588
Roshan Piuscc338202017-11-02 13:54:09 -07001589bool WifiChip::isValidModeId(ChipModeId mode_id) {
1590 for (const auto& mode : modes_) {
1591 if (mode.id == mode_id) {
1592 return true;
1593 }
1594 }
1595 return false;
1596}
1597
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001598bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1599 // Check if we can support atleast 1 STA & 1 AP concurrently.
1600 std::map<IfaceType, size_t> req_iface_combo;
1601 req_iface_combo[IfaceType::AP] = 1;
1602 req_iface_combo[IfaceType::STA] = 1;
1603 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1604}
1605
James Mattisd2e4c072019-05-22 16:14:48 -07001606bool WifiChip::isDualApAllowedInCurrentMode() {
1607 // Check if we can support atleast 1 STA & 1 AP concurrently.
1608 std::map<IfaceType, size_t> req_iface_combo;
1609 req_iface_combo[IfaceType::AP] = 2;
1610 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1611}
1612
Roshan Pius6036c022019-03-27 10:41:58 -07001613std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001614 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1615 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001616 // This could happen if the chip call is made before any STA/AP
1617 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001618 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Jimmy Chen2dddd792019-12-23 17:50:39 +02001619 return getWlanIfaceNameWithType(IfaceType::STA, 0);
Roshan Pius6036c022019-03-27 10:41:58 -07001620}
1621
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001622// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1623// not already in use.
1624// Note: This doesn't check the actual presence of these interfaces.
Jimmy Chen2dddd792019-12-23 17:50:39 +02001625std::string WifiChip::allocateApOrStaIfaceName(IfaceType type,
1626 uint32_t start_idx) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001627 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001628 const auto ifname = getWlanIfaceNameWithType(type, idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001629 if (findUsingName(ap_ifaces_, ifname)) continue;
1630 if (findUsingName(sta_ifaces_, ifname)) continue;
1631 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001632 }
1633 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001634 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001635 return {};
1636}
1637
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001638// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001639// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001640std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001641 // Check if we have a dedicated iface for AP.
1642 std::string ifname = getApIfaceName();
1643 if (!ifname.empty()) {
1644 return ifname;
1645 }
Jimmy Chen2dddd792019-12-23 17:50:39 +02001646 return allocateApOrStaIfaceName(IfaceType::AP,
1647 (isStaApConcurrencyAllowedInCurrentMode() &&
James Mattisd2e4c072019-05-22 16:14:48 -07001648 !isDualApAllowedInCurrentMode())
1649 ? 1
1650 : 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001651}
1652
1653// STA iface names start with idx 0.
1654// Primary STA iface will always be 0.
1655std::string WifiChip::allocateStaIfaceName() {
Jimmy Chen2dddd792019-12-23 17:50:39 +02001656 return allocateApOrStaIfaceName(IfaceType::STA, 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001657}
1658
xshu5899e8e2018-01-09 15:36:03 -08001659bool WifiChip::writeRingbufferFilesInternal() {
1660 if (!removeOldFilesInternal()) {
1661 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1662 return false;
1663 }
1664 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301665 {
1666 std::unique_lock<std::mutex> lk(lock_t);
1667 for (const auto& item : ringbuffer_map_) {
1668 const Ringbuffer& cur_buffer = item.second;
1669 if (cur_buffer.getData().empty()) {
1670 continue;
1671 }
1672 const std::string file_path_raw =
1673 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1674 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1675 if (dump_fd == -1) {
1676 PLOG(ERROR) << "create file failed";
1677 return false;
1678 }
1679 unique_fd file_auto_closer(dump_fd);
1680 for (const auto& cur_block : cur_buffer.getData()) {
1681 if (write(dump_fd, cur_block.data(),
1682 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1683 PLOG(ERROR) << "Error writing to file";
1684 }
xshu5899e8e2018-01-09 15:36:03 -08001685 }
1686 }
xshu0a0fe512020-07-22 17:53:37 -07001687 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001688 }
1689 return true;
1690}
1691
Jimmy Chen2dddd792019-12-23 17:50:39 +02001692std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) {
1693 std::string ifname;
1694
1695 // let the legacy hal override the interface name
1696 legacy_hal::wifi_error err =
1697 legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname);
1698 if (err == legacy_hal::WIFI_SUCCESS) return ifname;
1699
1700 return getWlanIfaceName(idx);
1701}
1702
Roshan Pius79a99752016-10-04 13:03:58 -07001703} // namespace implementation
Jimmy Chend460df32019-11-29 17:31:22 +02001704} // namespace V1_5
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001705} // namespace wifi
1706} // namespace hardware
1707} // namespace android