blob: 8f0e0005c387f8b28cc75a779abece24b392ead7 [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(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700339 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700340 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800341 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700342 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
343 const std::function<void(const std::string&)>& handler)
Roshan Pius52947fb2016-11-18 11:38:07 -0800344 : chip_id_(chip_id),
345 legacy_hal_(legacy_hal),
346 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800347 iface_util_(iface_util),
Roshan Pius52947fb2016-11-18 11:38:07 -0800348 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800349 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
350 modes_(feature_flags.lock()->getChipModes()),
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700351 debug_ring_buffer_cb_registered_(false),
352 subsystemCallbackHandler_(handler) {
Roshan Pius8574e7f2019-04-01 13:30:40 -0700353 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
354}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700355
Roshan Piusaabe5752016-09-29 09:03:59 -0700356void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700357 if (!writeRingbufferFilesInternal()) {
358 LOG(ERROR) << "Error writing files to flash";
359 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700360 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700361 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700362 legacy_hal_.reset();
363 event_cb_handler_.invalidate();
364 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700365}
366
Roshan Piusabcf78f2017-10-06 16:30:38 -0700367bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700368
Jimmy Chend460df32019-11-29 17:31:22 +0200369std::set<sp<V1_4::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700370 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800371}
372
Roshan Pius5c055462016-10-11 08:27:27 -0700373Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700374 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
375 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700376}
377
Jong Wook Kimda830c92018-07-23 15:29:38 -0700378// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700379Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800380 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700381 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700382 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
383 &WifiChip::registerEventCallbackInternal,
384 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700385}
386
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700387Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700388 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
389 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700390}
391
Roshan Pius5c055462016-10-11 08:27:27 -0700392Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700393 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
394 &WifiChip::getAvailableModesInternal,
395 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700396}
397
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800398Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700399 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800400 return validateAndCallWithLock(
401 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
402 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700403}
404
Roshan Pius5c055462016-10-11 08:27:27 -0700405Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700406 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
407 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700408}
409
Roshan Pius5c055462016-10-11 08:27:27 -0700410Return<void> WifiChip::requestChipDebugInfo(
411 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700412 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
413 &WifiChip::requestChipDebugInfoInternal,
414 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700415}
416
417Return<void> WifiChip::requestDriverDebugDump(
418 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700419 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
420 &WifiChip::requestDriverDebugDumpInternal,
421 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700422}
423
Roshan Pius5c055462016-10-11 08:27:27 -0700424Return<void> WifiChip::requestFirmwareDebugDump(
425 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700426 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
427 &WifiChip::requestFirmwareDebugDumpInternal,
428 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700429}
430
Roshan Pius5c055462016-10-11 08:27:27 -0700431Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700432 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
433 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700434}
435
Roshan Pius5c055462016-10-11 08:27:27 -0700436Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700437 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
438 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700439}
440
Roshan Pius5c055462016-10-11 08:27:27 -0700441Return<void> WifiChip::getApIface(const hidl_string& ifname,
442 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700443 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
444 &WifiChip::getApIfaceInternal, hidl_status_cb,
445 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700446}
447
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800448Return<void> WifiChip::removeApIface(const hidl_string& ifname,
449 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700450 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
451 &WifiChip::removeApIfaceInternal, hidl_status_cb,
452 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800453}
454
Roshan Pius5c055462016-10-11 08:27:27 -0700455Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700456 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
457 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700458}
459
Roshan Pius5c055462016-10-11 08:27:27 -0700460Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700461 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
462 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700463}
464
465Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700466 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700467 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
468 &WifiChip::getNanIfaceInternal, hidl_status_cb,
469 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700470}
471
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800472Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
473 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700474 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
475 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
476 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800477}
478
Roshan Pius5c055462016-10-11 08:27:27 -0700479Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700480 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
481 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700482}
483
Roshan Pius5c055462016-10-11 08:27:27 -0700484Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700485 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
486 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700487}
488
489Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700490 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700491 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
492 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
493 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700494}
495
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800496Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
497 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700498 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
499 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
500 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800501}
502
Roshan Pius5c055462016-10-11 08:27:27 -0700503Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700504 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
505 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700506}
507
Roshan Pius5c055462016-10-11 08:27:27 -0700508Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700509 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
510 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700511}
512
513Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700514 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700515 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
516 &WifiChip::getStaIfaceInternal, hidl_status_cb,
517 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700518}
519
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800520Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
521 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700522 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
523 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
524 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800525}
526
Roshan Pius5c055462016-10-11 08:27:27 -0700527Return<void> WifiChip::createRttController(
528 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700529 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
530 &WifiChip::createRttControllerInternal,
531 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700532}
533
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700534Return<void> WifiChip::getDebugRingBuffersStatus(
535 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700536 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
537 &WifiChip::getDebugRingBuffersStatusInternal,
538 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700539}
540
541Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700542 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
543 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700544 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700545 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
546 &WifiChip::startLoggingToDebugRingBufferInternal,
547 hidl_status_cb, ring_name, verbose_level,
548 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700549}
550
551Return<void> WifiChip::forceDumpToDebugRingBuffer(
552 const hidl_string& ring_name,
553 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700554 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
555 &WifiChip::forceDumpToDebugRingBufferInternal,
556 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700557}
558
Roger Wangb294c762018-11-02 15:34:39 +0800559Return<void> WifiChip::flushRingBufferToFile(
560 flushRingBufferToFile_cb hidl_status_cb) {
561 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
562 &WifiChip::flushRingBufferToFileInternal,
563 hidl_status_cb);
564}
565
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800566Return<void> WifiChip::stopLoggingToDebugRingBuffer(
567 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700568 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
569 &WifiChip::stopLoggingToDebugRingBufferInternal,
570 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800571}
572
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700573Return<void> WifiChip::getDebugHostWakeReasonStats(
574 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700575 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
576 &WifiChip::getDebugHostWakeReasonStatsInternal,
577 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700578}
579
Roshan Pius203cb032016-12-14 17:41:20 -0800580Return<void> WifiChip::enableDebugErrorAlerts(
581 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700582 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
583 &WifiChip::enableDebugErrorAlertsInternal,
584 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800585}
586
Roshan Pius735ff432017-07-25 08:48:08 -0700587Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700588 V1_1::IWifiChip::TxPowerScenario scenario,
589 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700590 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
591 &WifiChip::selectTxPowerScenarioInternal,
592 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700593}
594
Roshan Pius735ff432017-07-25 08:48:08 -0700595Return<void> WifiChip::resetTxPowerScenario(
596 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700597 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
598 &WifiChip::resetTxPowerScenarioInternal,
599 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700600}
601
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700602Return<void> WifiChip::setLatencyMode(LatencyMode mode,
603 setLatencyMode_cb hidl_status_cb) {
604 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
605 &WifiChip::setLatencyModeInternal, hidl_status_cb,
606 mode);
607}
608
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800609Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700610 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800611 registerEventCallback_cb hidl_status_cb) {
612 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
613 &WifiChip::registerEventCallbackInternal_1_2,
614 hidl_status_cb, event_callback);
615}
616
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800617Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700618 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800619 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700620 &WifiChip::selectTxPowerScenarioInternal_1_2,
621 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800622}
623
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700624Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
625 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
626 &WifiChip::getCapabilitiesInternal_1_3,
627 hidl_status_cb);
628}
629
xshu5899e8e2018-01-09 15:36:03 -0800630Return<void> WifiChip::debug(const hidl_handle& handle,
631 const hidl_vec<hidl_string>&) {
632 if (handle != nullptr && handle->numFds >= 1) {
xshu0a0fe512020-07-22 17:53:37 -0700633 {
634 std::unique_lock<std::mutex> lk(lock_t);
635 for (const auto& item : ringbuffer_map_) {
636 forceDumpToDebugRingBufferInternal(item.first);
637 }
638 // unique_lock unlocked here
639 }
640 usleep(100 * 1000); // sleep for 100 milliseconds to wait for
641 // ringbuffer updates.
xshu5899e8e2018-01-09 15:36:03 -0800642 int fd = handle->data[0];
643 if (!writeRingbufferFilesInternal()) {
644 LOG(ERROR) << "Error writing files to flash";
645 }
xshu4cb33162018-01-24 15:40:06 -0800646 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800647 if (n_error != 0) {
648 LOG(ERROR) << n_error << " errors occured in cpio function";
649 }
650 fsync(fd);
651 } else {
652 LOG(ERROR) << "File handle error";
653 }
654 return Void();
655}
656
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700657Return<void> WifiChip::createRttController_1_4(
658 const sp<IWifiIface>& bound_iface,
659 createRttController_1_4_cb hidl_status_cb) {
660 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
661 &WifiChip::createRttControllerInternal_1_4,
662 hidl_status_cb, bound_iface);
663}
664
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800665Return<void> WifiChip::registerEventCallback_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +0200666 const sp<V1_4::IWifiChipEventCallback>& event_callback,
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800667 registerEventCallback_cb hidl_status_cb) {
668 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
669 &WifiChip::registerEventCallbackInternal_1_4,
670 hidl_status_cb, event_callback);
671}
672
Roshan Pius35d958c2016-10-06 16:47:38 -0700673void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700674 invalidateAndClearAll(ap_ifaces_);
675 invalidateAndClearAll(nan_ifaces_);
676 invalidateAndClearAll(p2p_ifaces_);
677 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700678 // Since all the ifaces are invalid now, all RTT controller objects
679 // using those ifaces also need to be invalidated.
680 for (const auto& rtt : rtt_controllers_) {
681 rtt->invalidate();
682 }
683 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700684}
685
Roshan Pius82368502019-05-16 12:53:02 -0700686void WifiChip::invalidateAndRemoveDependencies(
687 const std::string& removed_iface_name) {
688 for (const auto& nan_iface : nan_ifaces_) {
689 if (nan_iface->getName() == removed_iface_name) {
690 invalidateAndClear(nan_ifaces_, nan_iface);
691 for (const auto& callback : event_cb_handler_.getCallbacks()) {
692 if (!callback
693 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
694 .isOk()) {
695 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
696 }
697 }
698 }
699 }
700 for (const auto& rtt : rtt_controllers_) {
701 if (rtt->getIfaceName() == removed_iface_name) {
702 invalidateAndClear(rtt_controllers_, rtt);
703 }
704 }
705}
706
Roshan Pius3c868522016-10-27 12:43:49 -0700707std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700708 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700709}
710
711WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800712 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
713 // Deprecated support for this callback.
714 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700715}
716
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700717std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700718 // Deprecated support for this callback.
719 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
720}
721
Jimmy Chend460df32019-11-29 17:31:22 +0200722std::pair<WifiStatus, std::vector<V1_4::IWifiChip::ChipMode>>
Roshan Pius3c868522016-10-27 12:43:49 -0700723WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700724 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700725}
726
Roshan Piusba38d9c2017-12-08 07:32:08 -0800727WifiStatus WifiChip::configureChipInternal(
728 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
729 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700730 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700731 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
732 }
733 if (mode_id == current_mode_id_) {
734 LOG(DEBUG) << "Already in the specified mode " << mode_id;
735 return createWifiStatus(WifiStatusCode::SUCCESS);
736 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800737 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700738 if (status.code != WifiStatusCode::SUCCESS) {
739 for (const auto& callback : event_cb_handler_.getCallbacks()) {
740 if (!callback->onChipReconfigureFailure(status).isOk()) {
741 LOG(ERROR)
742 << "Failed to invoke onChipReconfigureFailure callback";
743 }
744 }
745 return status;
746 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800747 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700748 if (!callback->onChipReconfigured(mode_id).isOk()) {
749 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
750 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800751 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700752 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800753 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700754 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Ahmed ElArabawy2134bf72020-06-18 15:07:12 -0700755
756 legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(
757 subsystemCallbackHandler_);
758
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800759 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700760}
761
762std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700763 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700764 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
765 current_mode_id_};
766 }
767 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700768}
769
Jimmy Chend460df32019-11-29 17:31:22 +0200770std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo>
Roshan Pius3c868522016-10-27 12:43:49 -0700771WifiChip::requestChipDebugInfoInternal() {
Jimmy Chend460df32019-11-29 17:31:22 +0200772 V1_4::IWifiChip::ChipDebugInfo result;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700773 legacy_hal::wifi_error legacy_status;
774 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700775 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700776 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800777 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700778 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
779 LOG(ERROR) << "Failed to get driver version: "
780 << legacyErrorToString(legacy_status);
781 WifiStatus status = createWifiStatusFromLegacyError(
782 legacy_status, "failed to get driver version");
783 return {status, result};
784 }
785 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700786
Roshan Piusabcf78f2017-10-06 16:30:38 -0700787 std::string firmware_desc;
788 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800789 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700790 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
791 LOG(ERROR) << "Failed to get firmware version: "
792 << legacyErrorToString(legacy_status);
793 WifiStatus status = createWifiStatusFromLegacyError(
794 legacy_status, "failed to get firmware version");
795 return {status, result};
796 }
797 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700798
Roshan Piusabcf78f2017-10-06 16:30:38 -0700799 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700800}
801
802std::pair<WifiStatus, std::vector<uint8_t>>
803WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700804 legacy_hal::wifi_error legacy_status;
805 std::vector<uint8_t> driver_dump;
806 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700807 legacy_hal_.lock()->requestDriverMemoryDump(
808 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700809 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
810 LOG(ERROR) << "Failed to get driver debug dump: "
811 << legacyErrorToString(legacy_status);
812 return {createWifiStatusFromLegacyError(legacy_status),
813 std::vector<uint8_t>()};
814 }
815 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700816}
817
818std::pair<WifiStatus, std::vector<uint8_t>>
819WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700820 legacy_hal::wifi_error legacy_status;
821 std::vector<uint8_t> firmware_dump;
822 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700823 legacy_hal_.lock()->requestFirmwareMemoryDump(
824 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700825 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
826 LOG(ERROR) << "Failed to get firmware debug dump: "
827 << legacyErrorToString(legacy_status);
828 return {createWifiStatusFromLegacyError(legacy_status), {}};
829 }
830 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700831}
832
833std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700834 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700835 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800836 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700837 std::string ifname = allocateApIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800838 legacy_hal::wifi_error legacy_status =
839 legacy_hal_.lock()->createVirtualInterface(
840 ifname,
841 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
842 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
843 LOG(ERROR) << "Failed to add interface: " << ifname << " "
844 << legacyErrorToString(legacy_status);
845 return {createWifiStatusFromLegacyError(legacy_status), {}};
846 }
Patrik Fimml6beae322019-10-09 17:34:01 +0200847 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700848 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700849 for (const auto& callback : event_cb_handler_.getCallbacks()) {
850 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
851 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
852 }
853 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700854 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700855 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700856}
857
858std::pair<WifiStatus, std::vector<hidl_string>>
859WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700860 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700861 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
862 }
Roshan Pius675609b2017-10-31 14:24:58 -0700863 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700864}
865
866std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800867 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700868 const auto iface = findUsingName(ap_ifaces_, ifname);
869 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700870 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
871 }
Roshan Pius675609b2017-10-31 14:24:58 -0700872 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700873}
874
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800875WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700876 const auto iface = findUsingName(ap_ifaces_, ifname);
877 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700878 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800879 }
Roshan Pius82368502019-05-16 12:53:02 -0700880 // Invalidate & remove any dependent objects first.
881 // Note: This is probably not required because we never create
882 // nan/rtt objects over AP iface. But, there is no harm to do it
883 // here and not make that assumption all over the place.
884 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800885 legacy_hal::wifi_error legacy_status =
886 legacy_hal_.lock()->deleteVirtualInterface(ifname);
887 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
888 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
889 << legacyErrorToString(legacy_status);
890 }
Roshan Pius675609b2017-10-31 14:24:58 -0700891 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700892 for (const auto& callback : event_cb_handler_.getCallbacks()) {
893 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
894 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
895 }
896 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700897 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700898 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800899}
900
Jimmy Chend460df32019-11-29 17:31:22 +0200901std::pair<WifiStatus, sp<V1_4::IWifiNanIface>>
902WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700903 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700904 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800905 }
Roshan Pius5ba0a902020-04-14 11:55:42 -0700906 bool is_dedicated_iface = true;
907 std::string ifname = getNanIfaceName();
Nate Jiang63f34ed2020-05-20 23:22:51 -0700908 if (ifname.empty() || !iface_util_.lock()->ifNameToIndex(ifname)) {
Roshan Pius5ba0a902020-04-14 11:55:42 -0700909 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
910 // not defined.
911 ifname = getFirstActiveWlanIfaceName();
912 is_dedicated_iface = false;
913 }
914 sp<WifiNanIface> iface =
915 new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -0700916 nan_ifaces_.push_back(iface);
917 for (const auto& callback : event_cb_handler_.getCallbacks()) {
918 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
919 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
920 }
921 }
922 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700923}
924
925std::pair<WifiStatus, std::vector<hidl_string>>
926WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700927 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700928 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
929 }
Roshan Pius675609b2017-10-31 14:24:58 -0700930 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700931}
932
Jimmy Chend460df32019-11-29 17:31:22 +0200933std::pair<WifiStatus, sp<V1_4::IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800934 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700935 const auto iface = findUsingName(nan_ifaces_, ifname);
936 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700937 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
938 }
Roshan Pius675609b2017-10-31 14:24:58 -0700939 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700940}
941
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800942WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700943 const auto iface = findUsingName(nan_ifaces_, ifname);
944 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700945 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800946 }
Roshan Pius675609b2017-10-31 14:24:58 -0700947 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700948 for (const auto& callback : event_cb_handler_.getCallbacks()) {
949 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
950 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
951 }
952 }
953 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800954}
955
Roshan Pius3c868522016-10-27 12:43:49 -0700956std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700957 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700958 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800959 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700960 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700961 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
962 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700963 for (const auto& callback : event_cb_handler_.getCallbacks()) {
964 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
965 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
966 }
967 }
Roshan Pius675609b2017-10-31 14:24:58 -0700968 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700969}
970
971std::pair<WifiStatus, std::vector<hidl_string>>
972WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700973 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700974 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
975 }
Roshan Pius675609b2017-10-31 14:24:58 -0700976 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700977}
978
979std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800980 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700981 const auto iface = findUsingName(p2p_ifaces_, ifname);
982 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700983 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
984 }
Roshan Pius675609b2017-10-31 14:24:58 -0700985 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700986}
987
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800988WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700989 const auto iface = findUsingName(p2p_ifaces_, ifname);
990 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700991 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800992 }
Roshan Pius675609b2017-10-31 14:24:58 -0700993 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700994 for (const auto& callback : event_cb_handler_.getCallbacks()) {
995 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
996 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
997 }
998 }
999 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001000}
1001
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001002std::pair<WifiStatus, sp<V1_3::IWifiStaIface>>
1003WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001004 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001005 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -08001006 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001007 std::string ifname = allocateStaIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -08001008 legacy_hal::wifi_error legacy_status =
1009 legacy_hal_.lock()->createVirtualInterface(
1010 ifname,
1011 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
1012 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1013 LOG(ERROR) << "Failed to add interface: " << ifname << " "
1014 << legacyErrorToString(legacy_status);
1015 return {createWifiStatusFromLegacyError(legacy_status), {}};
1016 }
Roshan Pius99dab382019-02-14 07:57:10 -08001017 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -07001018 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001019 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1020 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
1021 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
1022 }
1023 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001024 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -07001025 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001026}
1027
1028std::pair<WifiStatus, std::vector<hidl_string>>
1029WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001030 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001031 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1032 }
Roshan Pius675609b2017-10-31 14:24:58 -07001033 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001034}
1035
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001036std::pair<WifiStatus, sp<V1_3::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001037 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001038 const auto iface = findUsingName(sta_ifaces_, ifname);
1039 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001040 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1041 }
Roshan Pius675609b2017-10-31 14:24:58 -07001042 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001043}
1044
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001045WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001046 const auto iface = findUsingName(sta_ifaces_, ifname);
1047 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001048 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001049 }
Roshan Pius82368502019-05-16 12:53:02 -07001050 // Invalidate & remove any dependent objects first.
1051 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001052 legacy_hal::wifi_error legacy_status =
1053 legacy_hal_.lock()->deleteVirtualInterface(ifname);
1054 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1055 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1056 << legacyErrorToString(legacy_status);
1057 }
Roshan Pius675609b2017-10-31 14:24:58 -07001058 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001059 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1060 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1061 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1062 }
1063 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001064 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001065 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001066}
1067
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001068std::pair<WifiStatus, sp<V1_0::IWifiRttController>>
1069WifiChip::createRttControllerInternal(const sp<IWifiIface>& /*bound_iface*/) {
1070 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001071 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001072}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001073
1074std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1075WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001076 legacy_hal::wifi_error legacy_status;
1077 std::vector<legacy_hal::wifi_ring_buffer_status>
1078 legacy_ring_buffer_status_vec;
1079 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001080 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001081 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1082 return {createWifiStatusFromLegacyError(legacy_status), {}};
1083 }
1084 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1085 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1086 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1087 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1088 }
1089 return {createWifiStatus(WifiStatusCode::SUCCESS),
1090 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001091}
1092
1093WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001094 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1095 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1096 WifiStatus status = registerDebugRingBufferCallback();
1097 if (status.code != WifiStatusCode::SUCCESS) {
1098 return status;
1099 }
1100 legacy_hal::wifi_error legacy_status =
1101 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001102 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001103 static_cast<
1104 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1105 verbose_level),
1106 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001107 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1108 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001109 // if verbose logging enabled, turn up HAL daemon logging as well.
1110 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1111 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1112 } else {
1113 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1114 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001115 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001116}
1117
1118WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001119 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001120 WifiStatus status = registerDebugRingBufferCallback();
1121 if (status.code != WifiStatusCode::SUCCESS) {
1122 return status;
1123 }
1124 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001125 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1126 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001127
Roshan Piusabcf78f2017-10-06 16:30:38 -07001128 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001129}
1130
Roger Wangb294c762018-11-02 15:34:39 +08001131WifiStatus WifiChip::flushRingBufferToFileInternal() {
1132 if (!writeRingbufferFilesInternal()) {
1133 LOG(ERROR) << "Error writing files to flash";
1134 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1135 }
1136 return createWifiStatus(WifiStatusCode::SUCCESS);
1137}
1138
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001139WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001140 legacy_hal::wifi_error legacy_status =
1141 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001142 getFirstActiveWlanIfaceName());
xshu0a0fe512020-07-22 17:53:37 -07001143 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1144 debug_ring_buffer_cb_registered_ = false;
1145 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001146 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001147}
1148
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001149std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1150WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001151 legacy_hal::wifi_error legacy_status;
1152 legacy_hal::WakeReasonStats legacy_stats;
1153 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001154 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001155 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1156 return {createWifiStatusFromLegacyError(legacy_status), {}};
1157 }
1158 WifiDebugHostWakeReasonStats hidl_stats;
1159 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1160 &hidl_stats)) {
1161 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1162 }
1163 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001164}
1165
Roshan Pius203cb032016-12-14 17:41:20 -08001166WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001167 legacy_hal::wifi_error legacy_status;
1168 if (enable) {
1169 android::wp<WifiChip> weak_ptr_this(this);
1170 const auto& on_alert_callback = [weak_ptr_this](
1171 int32_t error_code,
1172 std::vector<uint8_t> debug_data) {
1173 const auto shared_ptr_this = weak_ptr_this.promote();
1174 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1175 LOG(ERROR) << "Callback invoked on an invalid object";
1176 return;
1177 }
1178 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1179 if (!callback->onDebugErrorAlert(error_code, debug_data)
1180 .isOk()) {
1181 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1182 }
1183 }
1184 };
1185 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001186 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001187 } else {
1188 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001189 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001190 }
1191 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001192}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001193
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001194WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001195 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001196 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001197 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001198 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1199 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001200}
1201
Roshan Pius735ff432017-07-25 08:48:08 -07001202WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001203 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001204 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001205 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001206}
1207
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001208WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1209 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001210 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001211 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1212 return createWifiStatusFromLegacyError(legacy_status);
1213}
1214
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001215WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001216 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
1217 // Deprecated support for this callback.
1218 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001219}
1220
Jong Wook Kimda830c92018-07-23 15:29:38 -07001221WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1222 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001223 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001224 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001225 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1226 return createWifiStatusFromLegacyError(legacy_status);
1227}
1228
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001229std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
1230 legacy_hal::wifi_error legacy_status;
1231 uint32_t legacy_feature_set;
1232 uint32_t legacy_logger_feature_set;
1233 const auto ifname = getFirstActiveWlanIfaceName();
1234 std::tie(legacy_status, legacy_feature_set) =
1235 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
1236 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1237 return {createWifiStatusFromLegacyError(legacy_status), 0};
1238 }
1239 std::tie(legacy_status, legacy_logger_feature_set) =
1240 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
1241 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1242 // some devices don't support querying logger feature set
1243 legacy_logger_feature_set = 0;
1244 }
1245 uint32_t hidl_caps;
1246 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
1247 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
1248 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1249 }
1250 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1251}
1252
Jimmy Chend460df32019-11-29 17:31:22 +02001253std::pair<WifiStatus, sp<V1_4::IWifiRttController>>
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001254WifiChip::createRttControllerInternal_1_4(const sp<IWifiIface>& bound_iface) {
1255 if (sta_ifaces_.size() == 0 &&
1256 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1257 LOG(ERROR)
1258 << "createRttControllerInternal_1_4: Chip cannot support STAs "
1259 "(and RTT by extension)";
1260 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1261 }
1262 sp<WifiRttController> rtt = new WifiRttController(
1263 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1264 rtt_controllers_.emplace_back(rtt);
1265 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1266}
1267
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001268WifiStatus WifiChip::registerEventCallbackInternal_1_4(
Jimmy Chend460df32019-11-29 17:31:22 +02001269 const sp<V1_4::IWifiChipEventCallback>& event_callback) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001270 if (!event_cb_handler_.addCallback(event_callback)) {
1271 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1272 }
1273 return createWifiStatus(WifiStatusCode::SUCCESS);
1274}
1275
Roshan Piusba38d9c2017-12-08 07:32:08 -08001276WifiStatus WifiChip::handleChipConfiguration(
1277 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1278 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001279 // If the chip is already configured in a different mode, stop
1280 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001281 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001282 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1283 << " to mode " << mode_id;
1284 invalidateAndRemoveAllIfaces();
1285 legacy_hal::wifi_error legacy_status =
1286 legacy_hal_.lock()->stop(lock, []() {});
1287 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1288 LOG(ERROR) << "Failed to stop legacy HAL: "
1289 << legacyErrorToString(legacy_status);
1290 return createWifiStatusFromLegacyError(legacy_status);
1291 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001292 }
Roshan Piuscc338202017-11-02 13:54:09 -07001293 // Firmware mode change not needed for V2 devices.
1294 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001295 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001296 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001297 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001298 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1299 }
1300 if (!success) {
1301 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1302 }
1303 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1304 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1305 LOG(ERROR) << "Failed to start legacy HAL: "
1306 << legacyErrorToString(legacy_status);
1307 return createWifiStatusFromLegacyError(legacy_status);
1308 }
Roshan Pius85c64412018-01-22 17:58:40 -08001309 // Every time the HAL is restarted, we need to register the
1310 // radio mode change callback.
1311 WifiStatus status = registerRadioModeChangeCallback();
1312 if (status.code != WifiStatusCode::SUCCESS) {
1313 // This probably is not a critical failure?
1314 LOG(ERROR) << "Failed to register radio mode change callback";
1315 }
chenpaulf5eca292019-03-14 11:08:03 +08001316 // Extract and save the version information into property.
Jimmy Chend460df32019-11-29 17:31:22 +02001317 std::pair<WifiStatus, V1_4::IWifiChip::ChipDebugInfo> version_info;
chenpaulf5eca292019-03-14 11:08:03 +08001318 version_info = WifiChip::requestChipDebugInfoInternal();
1319 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1320 property_set("vendor.wlan.firmware.version",
1321 version_info.second.firmwareDescription.c_str());
1322 property_set("vendor.wlan.driver.version",
1323 version_info.second.driverDescription.c_str());
1324 }
1325
Roshan Piusabcf78f2017-10-06 16:30:38 -07001326 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001327}
Roshan Pius48185b22016-12-15 19:10:30 -08001328
1329WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001330 if (debug_ring_buffer_cb_registered_) {
1331 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001332 }
Roshan Pius3797e182017-03-30 18:01:54 -07001333
Roshan Piusabcf78f2017-10-06 16:30:38 -07001334 android::wp<WifiChip> weak_ptr_this(this);
1335 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001336 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001337 const std::vector<uint8_t>& data,
1338 const legacy_hal::wifi_ring_buffer_status& status) {
1339 const auto shared_ptr_this = weak_ptr_this.promote();
1340 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1341 LOG(ERROR) << "Callback invoked on an invalid object";
1342 return;
1343 }
1344 WifiDebugRingBufferStatus hidl_status;
1345 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1346 status, &hidl_status)) {
1347 LOG(ERROR) << "Error converting ring buffer status";
1348 return;
1349 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301350 {
1351 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1352 const auto& target =
1353 shared_ptr_this->ringbuffer_map_.find(name);
1354 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1355 Ringbuffer& cur_buffer = target->second;
1356 cur_buffer.append(data);
1357 } else {
1358 LOG(ERROR) << "Ringname " << name << " not found";
1359 return;
1360 }
xshu0a0fe512020-07-22 17:53:37 -07001361 // unique_lock unlocked here
Roshan Piusabcf78f2017-10-06 16:30:38 -07001362 }
1363 };
1364 legacy_hal::wifi_error legacy_status =
1365 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001366 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001367
Roshan Piusabcf78f2017-10-06 16:30:38 -07001368 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1369 debug_ring_buffer_cb_registered_ = true;
1370 }
1371 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001372}
1373
Roshan Pius85c64412018-01-22 17:58:40 -08001374WifiStatus WifiChip::registerRadioModeChangeCallback() {
1375 android::wp<WifiChip> weak_ptr_this(this);
1376 const auto& on_radio_mode_change_callback =
1377 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1378 const auto shared_ptr_this = weak_ptr_this.promote();
1379 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1380 LOG(ERROR) << "Callback invoked on an invalid object";
1381 return;
1382 }
Jimmy Chend460df32019-11-29 17:31:22 +02001383 std::vector<V1_4::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001384 hidl_radio_mode_infos;
1385 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1386 mac_infos, &hidl_radio_mode_infos)) {
1387 LOG(ERROR) << "Error converting wifi mac info";
1388 return;
1389 }
1390 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001391 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos)
Roshan Pius85c64412018-01-22 17:58:40 -08001392 .isOk()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001393 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
Roshan Pius85c64412018-01-22 17:58:40 -08001394 << " callback on: " << toString(callback);
1395 }
1396 }
1397 };
1398 legacy_hal::wifi_error legacy_status =
1399 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001400 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001401 return createWifiStatusFromLegacyError(legacy_status);
1402}
1403
Jimmy Chend460df32019-11-29 17:31:22 +02001404std::vector<V1_4::IWifiChip::ChipIfaceCombination>
Roshan Piuscc338202017-11-02 13:54:09 -07001405WifiChip::getCurrentModeIfaceCombinations() {
1406 if (!isValidModeId(current_mode_id_)) {
1407 LOG(ERROR) << "Chip not configured in a mode yet";
1408 return {};
1409 }
1410 for (const auto& mode : modes_) {
1411 if (mode.id == current_mode_id_) {
1412 return mode.availableCombinations;
1413 }
1414 }
1415 CHECK(0) << "Expected to find iface combinations for current mode!";
1416 return {};
1417}
1418
1419// Returns a map indexed by IfaceType with the number of ifaces currently
1420// created of the corresponding type.
1421std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1422 std::map<IfaceType, size_t> iface_counts;
1423 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1424 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1425 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1426 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1427 return iface_counts;
1428}
1429
1430// This expands the provided iface combinations to a more parseable
1431// form. Returns a vector of available combinations possible with the number
1432// of ifaces of each type in the combination.
1433// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1434std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
Jimmy Chend460df32019-11-29 17:31:22 +02001435 const V1_4::IWifiChip::ChipIfaceCombination& combination) {
Roshan Piuscc338202017-11-02 13:54:09 -07001436 uint32_t num_expanded_combos = 1;
1437 for (const auto& limit : combination.limits) {
1438 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1439 num_expanded_combos *= limit.types.size();
1440 }
1441 }
1442
1443 // Allocate the vector of expanded combos and reset all iface counts to 0
1444 // in each combo.
1445 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1446 expanded_combos.resize(num_expanded_combos);
1447 for (auto& expanded_combo : expanded_combos) {
1448 for (const auto type :
1449 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1450 expanded_combo[type] = 0;
1451 }
1452 }
1453 uint32_t span = num_expanded_combos;
1454 for (const auto& limit : combination.limits) {
1455 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1456 span /= limit.types.size();
1457 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1458 const auto iface_type =
1459 limit.types[(k / span) % limit.types.size()];
1460 expanded_combos[k][iface_type]++;
1461 }
1462 }
1463 }
1464 return expanded_combos;
1465}
1466
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001467bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1468 const std::map<IfaceType, size_t>& expanded_combo,
1469 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001470 const auto current_combo = getCurrentIfaceCombination();
1471
1472 // Check if we have space for 1 more iface of |type| in this combo
1473 for (const auto type :
1474 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1475 size_t num_ifaces_needed = current_combo.at(type);
1476 if (type == requested_type) {
1477 num_ifaces_needed++;
1478 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001479 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001480 if (num_ifaces_needed > num_ifaces_allowed) {
1481 return false;
1482 }
1483 }
1484 return true;
1485}
1486
1487// This method does the following:
1488// a) Enumerate all possible iface combos by expanding the current
1489// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001490// b) Check if the requested iface type can be added to the current mode
1491// with the iface combination that is already active.
1492bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1493 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001494 if (!isValidModeId(current_mode_id_)) {
1495 LOG(ERROR) << "Chip not configured in a mode yet";
1496 return false;
1497 }
1498 const auto combinations = getCurrentModeIfaceCombinations();
1499 for (const auto& combination : combinations) {
1500 const auto expanded_combos = expandIfaceCombinations(combination);
1501 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001502 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1503 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001504 return true;
1505 }
1506 }
1507 }
1508 return false;
1509}
1510
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001511// Note: This does not consider ifaces already active. It only checks if the
1512// provided expanded iface combination can support the requested combo.
1513bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1514 const std::map<IfaceType, size_t>& expanded_combo,
1515 const std::map<IfaceType, size_t>& req_combo) {
1516 // Check if we have space for 1 more iface of |type| in this combo
1517 for (const auto type :
1518 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1519 if (req_combo.count(type) == 0) {
1520 // Iface of "type" not in the req_combo.
1521 continue;
1522 }
1523 size_t num_ifaces_needed = req_combo.at(type);
1524 size_t num_ifaces_allowed = expanded_combo.at(type);
1525 if (num_ifaces_needed > num_ifaces_allowed) {
1526 return false;
1527 }
1528 }
1529 return true;
1530}
1531// This method does the following:
1532// a) Enumerate all possible iface combos by expanding the current
1533// ChipIfaceCombination.
1534// b) Check if the requested iface combo can be added to the current mode.
1535// Note: This does not consider ifaces already active. It only checks if the
1536// current mode can support the requested combo.
1537bool WifiChip::canCurrentModeSupportIfaceCombo(
1538 const std::map<IfaceType, size_t>& req_combo) {
1539 if (!isValidModeId(current_mode_id_)) {
1540 LOG(ERROR) << "Chip not configured in a mode yet";
1541 return false;
1542 }
1543 const auto combinations = getCurrentModeIfaceCombinations();
1544 for (const auto& combination : combinations) {
1545 const auto expanded_combos = expandIfaceCombinations(combination);
1546 for (const auto& expanded_combo : expanded_combos) {
1547 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1548 req_combo)) {
1549 return true;
1550 }
1551 }
1552 }
1553 return false;
1554}
1555
1556// This method does the following:
1557// a) Enumerate all possible iface combos by expanding the current
1558// ChipIfaceCombination.
1559// b) Check if the requested iface type can be added to the current mode.
1560bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1561 // Check if we can support atleast 1 iface of type.
1562 std::map<IfaceType, size_t> req_iface_combo;
1563 req_iface_combo[requested_type] = 1;
1564 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1565}
1566
Roshan Piuscc338202017-11-02 13:54:09 -07001567bool WifiChip::isValidModeId(ChipModeId mode_id) {
1568 for (const auto& mode : modes_) {
1569 if (mode.id == mode_id) {
1570 return true;
1571 }
1572 }
1573 return false;
1574}
1575
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001576bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1577 // Check if we can support atleast 1 STA & 1 AP concurrently.
1578 std::map<IfaceType, size_t> req_iface_combo;
1579 req_iface_combo[IfaceType::AP] = 1;
1580 req_iface_combo[IfaceType::STA] = 1;
1581 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1582}
1583
James Mattisd2e4c072019-05-22 16:14:48 -07001584bool WifiChip::isDualApAllowedInCurrentMode() {
1585 // Check if we can support atleast 1 STA & 1 AP concurrently.
1586 std::map<IfaceType, size_t> req_iface_combo;
1587 req_iface_combo[IfaceType::AP] = 2;
1588 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1589}
1590
Roshan Pius6036c022019-03-27 10:41:58 -07001591std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001592 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1593 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001594 // This could happen if the chip call is made before any STA/AP
1595 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001596 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Roshan Pius6036c022019-03-27 10:41:58 -07001597 return getWlanIfaceName(0);
1598}
1599
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001600// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1601// not already in use.
1602// Note: This doesn't check the actual presence of these interfaces.
1603std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1604 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1605 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001606 if (findUsingName(ap_ifaces_, ifname)) continue;
1607 if (findUsingName(sta_ifaces_, ifname)) continue;
1608 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001609 }
1610 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001611 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001612 return {};
1613}
1614
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001615// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001616// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001617std::string WifiChip::allocateApIfaceName() {
Roshan Pius78cb5992020-04-30 12:39:21 -07001618 // Check if we have a dedicated iface for AP.
1619 std::string ifname = getApIfaceName();
1620 if (!ifname.empty()) {
1621 return ifname;
1622 }
James Mattisd2e4c072019-05-22 16:14:48 -07001623 return allocateApOrStaIfaceName((isStaApConcurrencyAllowedInCurrentMode() &&
1624 !isDualApAllowedInCurrentMode())
1625 ? 1
1626 : 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001627}
1628
1629// STA iface names start with idx 0.
1630// Primary STA iface will always be 0.
1631std::string WifiChip::allocateStaIfaceName() {
1632 return allocateApOrStaIfaceName(0);
1633}
1634
xshu5899e8e2018-01-09 15:36:03 -08001635bool WifiChip::writeRingbufferFilesInternal() {
1636 if (!removeOldFilesInternal()) {
1637 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1638 return false;
1639 }
1640 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301641 {
1642 std::unique_lock<std::mutex> lk(lock_t);
1643 for (const auto& item : ringbuffer_map_) {
1644 const Ringbuffer& cur_buffer = item.second;
1645 if (cur_buffer.getData().empty()) {
1646 continue;
1647 }
1648 const std::string file_path_raw =
1649 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1650 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1651 if (dump_fd == -1) {
1652 PLOG(ERROR) << "create file failed";
1653 return false;
1654 }
1655 unique_fd file_auto_closer(dump_fd);
1656 for (const auto& cur_block : cur_buffer.getData()) {
1657 if (write(dump_fd, cur_block.data(),
1658 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1659 PLOG(ERROR) << "Error writing to file";
1660 }
xshu5899e8e2018-01-09 15:36:03 -08001661 }
1662 }
xshu0a0fe512020-07-22 17:53:37 -07001663 // unique_lock unlocked here
xshu5899e8e2018-01-09 15:36:03 -08001664 }
1665 return true;
1666}
1667
Roshan Pius79a99752016-10-04 13:03:58 -07001668} // namespace implementation
Jimmy Chend460df32019-11-29 17:31:22 +02001669} // namespace V1_5
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001670} // namespace wifi
1671} // namespace hardware
1672} // namespace android