blob: 4c9fad17bb94391b97985ff0a18fca3dd5515184 [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
104std::string getP2pIfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700105 std::array<char, PROPERTY_VALUE_MAX> buffer;
106 property_get("wifi.direct.interface", buffer.data(), "p2p0");
107 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700108}
109
Roshan Pius8574e7f2019-04-01 13:30:40 -0700110void setActiveWlanIfaceNameProperty(const std::string& ifname) {
111 auto res = property_set(kActiveWlanIfaceNameProperty, ifname.data());
112 if (res != 0) {
113 PLOG(ERROR) << "Failed to set active wlan iface name property";
114 }
115}
116
xshu37126c92018-04-13 16:24:45 -0700117// delete files that meet either conditions:
118// 1. older than a predefined time in the wifi tombstone dir.
119// 2. Files in excess to a predefined amount, starting from the oldest ones
xshu5899e8e2018-01-09 15:36:03 -0800120bool removeOldFilesInternal() {
121 time_t now = time(0);
122 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
Josh Gaoa568e532018-06-04 18:16:00 -0700123 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(
124 opendir(kTombstoneFolderPath), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800125 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800126 PLOG(ERROR) << "Failed to open directory";
xshu5899e8e2018-01-09 15:36:03 -0800127 return false;
128 }
xshu5899e8e2018-01-09 15:36:03 -0800129 struct dirent* dp;
130 bool success = true;
xshu37126c92018-04-13 16:24:45 -0700131 std::list<std::pair<const time_t, std::string>> valid_files;
Josh Gaoa568e532018-06-04 18:16:00 -0700132 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800133 if (dp->d_type != DT_REG) {
134 continue;
135 }
136 std::string cur_file_name(dp->d_name);
137 struct stat cur_file_stat;
138 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800139 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800140 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800141 success = false;
xshu4cb33162018-01-24 15:40:06 -0800142 continue;
143 }
xshu37126c92018-04-13 16:24:45 -0700144 const time_t cur_file_time = cur_file_stat.st_mtime;
145 valid_files.push_back(
146 std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
147 }
148 valid_files.sort(); // sort the list of files by last modified time from
149 // small to big.
150 uint32_t cur_file_count = valid_files.size();
151 for (auto cur_file : valid_files) {
152 if (cur_file_count > kMaxRingBufferFileNum ||
153 cur_file.first < delete_files_before) {
154 if (unlink(cur_file.second.c_str()) != 0) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800155 PLOG(ERROR) << "Error deleting file";
xshu37126c92018-04-13 16:24:45 -0700156 success = false;
157 }
158 cur_file_count--;
159 } else {
160 break;
xshu5899e8e2018-01-09 15:36:03 -0800161 }
162 }
163 return success;
164}
165
xshu4cb33162018-01-24 15:40:06 -0800166// Helper function for |cpioArchiveFilesInDir|
167bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name,
168 size_t file_name_len) {
169 std::array<char, 32 * 1024> read_buf;
170 ssize_t llen =
171 sprintf(read_buf.data(),
172 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
173 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
174 st.st_gid, static_cast<int>(st.st_nlink),
175 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
176 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
177 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
178 if (write(out_fd, read_buf.data(), llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800179 PLOG(ERROR) << "Error writing cpio header to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800180 return false;
181 }
182 if (write(out_fd, file_name, file_name_len) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800183 PLOG(ERROR) << "Error writing filename to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800184 return false;
185 }
186
187 // NUL Pad header up to 4 multiple bytes.
188 llen = (llen + file_name_len) % 4;
189 if (llen != 0) {
190 const uint32_t zero = 0;
191 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800192 PLOG(ERROR) << "Error padding 0s to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800193 return false;
194 }
195 }
196 return true;
197}
198
199// Helper function for |cpioArchiveFilesInDir|
200size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
201 // writing content of file
202 std::array<char, 32 * 1024> read_buf;
203 ssize_t llen = st.st_size;
204 size_t n_error = 0;
205 while (llen > 0) {
206 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
207 if (bytes_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800208 PLOG(ERROR) << "Error reading file";
xshu4cb33162018-01-24 15:40:06 -0800209 return ++n_error;
210 }
211 llen -= bytes_read;
212 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800213 PLOG(ERROR) << "Error writing data to file";
xshu4cb33162018-01-24 15:40:06 -0800214 return ++n_error;
215 }
216 if (bytes_read == 0) { // this should never happen, but just in case
217 // to unstuck from while loop
Elliott Hughes4db4add2019-03-08 12:42:57 -0800218 PLOG(ERROR) << "Unexpected read result";
xshu4cb33162018-01-24 15:40:06 -0800219 n_error++;
220 break;
221 }
222 }
223 llen = st.st_size % 4;
224 if (llen != 0) {
225 const uint32_t zero = 0;
226 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800227 PLOG(ERROR) << "Error padding 0s to file";
xshu4cb33162018-01-24 15:40:06 -0800228 return ++n_error;
229 }
230 }
231 return n_error;
232}
233
234// Helper function for |cpioArchiveFilesInDir|
235bool cpioWriteFileTrailer(int out_fd) {
236 std::array<char, 4096> read_buf;
237 read_buf.fill(0);
238 if (write(out_fd, read_buf.data(),
239 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1,
240 0x0b, 0) +
241 4) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800242 PLOG(ERROR) << "Error writing trailing bytes";
xshu4cb33162018-01-24 15:40:06 -0800243 return false;
244 }
245 return true;
246}
247
xshu5899e8e2018-01-09 15:36:03 -0800248// Archives all files in |input_dir| and writes result into |out_fd|
249// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
250// portion
xshu4cb33162018-01-24 15:40:06 -0800251size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800252 struct dirent* dp;
253 size_t n_error = 0;
Josh Gaoa568e532018-06-04 18:16:00 -0700254 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir),
255 closedir);
xshu5899e8e2018-01-09 15:36:03 -0800256 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800257 PLOG(ERROR) << "Failed to open directory";
xshu4cb33162018-01-24 15:40:06 -0800258 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800259 }
Josh Gaoa568e532018-06-04 18:16:00 -0700260 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800261 if (dp->d_type != DT_REG) {
262 continue;
263 }
264 std::string cur_file_name(dp->d_name);
xshu4cb33162018-01-24 15:40:06 -0800265 // string.size() does not include the null terminator. The cpio FreeBSD
266 // file header expects the null character to be included in the length.
267 const size_t file_name_len = cur_file_name.size() + 1;
xshu5899e8e2018-01-09 15:36:03 -0800268 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800269 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800270 if (stat(cur_file_path.c_str(), &st) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800271 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800272 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800273 continue;
274 }
275 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
276 if (fd_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800277 PLOG(ERROR) << "Failed to open file " << cur_file_path;
xshu4cb33162018-01-24 15:40:06 -0800278 n_error++;
279 continue;
280 }
281 unique_fd file_auto_closer(fd_read);
282 if (!cpioWriteHeader(out_fd, st, cur_file_name.c_str(),
283 file_name_len)) {
284 return ++n_error;
285 }
286 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
287 if (write_error) {
288 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800289 }
290 }
xshu4cb33162018-01-24 15:40:06 -0800291 if (!cpioWriteFileTrailer(out_fd)) {
292 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800293 }
294 return n_error;
295}
296
297// Helper function to create a non-const char*.
298std::vector<char> makeCharVec(const std::string& str) {
299 std::vector<char> vec(str.size() + 1);
300 vec.assign(str.begin(), str.end());
301 vec.push_back('\0');
302 return vec;
303}
304
Roshan Piusabcf78f2017-10-06 16:30:38 -0700305} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700306
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700307namespace android {
308namespace hardware {
309namespace wifi {
Ahmed ElArabawyf501a982019-07-23 15:02:22 -0700310namespace V1_4 {
Roshan Pius79a99752016-10-04 13:03:58 -0700311namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700312using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800313using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700314
Roshan Pius52947fb2016-11-18 11:38:07 -0800315WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700316 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700317 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800318 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Roshan Pius200a17d2017-11-01 13:03:35 -0700319 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Pius52947fb2016-11-18 11:38:07 -0800320 : chip_id_(chip_id),
321 legacy_hal_(legacy_hal),
322 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800323 iface_util_(iface_util),
Roshan Pius52947fb2016-11-18 11:38:07 -0800324 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800325 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
326 modes_(feature_flags.lock()->getChipModes()),
Roshan Pius8574e7f2019-04-01 13:30:40 -0700327 debug_ring_buffer_cb_registered_(false) {
328 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
329}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700330
Roshan Piusaabe5752016-09-29 09:03:59 -0700331void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700332 if (!writeRingbufferFilesInternal()) {
333 LOG(ERROR) << "Error writing files to flash";
334 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700335 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700336 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700337 legacy_hal_.reset();
338 event_cb_handler_.invalidate();
339 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700340}
341
Roshan Piusabcf78f2017-10-06 16:30:38 -0700342bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700343
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800344std::set<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700345 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800346}
347
Roshan Pius5c055462016-10-11 08:27:27 -0700348Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700349 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
350 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700351}
352
Jong Wook Kimda830c92018-07-23 15:29:38 -0700353// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700354Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800355 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700356 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700357 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
358 &WifiChip::registerEventCallbackInternal,
359 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700360}
361
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700362Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700363 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
364 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700365}
366
Roshan Pius5c055462016-10-11 08:27:27 -0700367Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700368 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
369 &WifiChip::getAvailableModesInternal,
370 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700371}
372
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800373Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700374 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800375 return validateAndCallWithLock(
376 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
377 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700378}
379
Roshan Pius5c055462016-10-11 08:27:27 -0700380Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700381 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
382 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700383}
384
Roshan Pius5c055462016-10-11 08:27:27 -0700385Return<void> WifiChip::requestChipDebugInfo(
386 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700387 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
388 &WifiChip::requestChipDebugInfoInternal,
389 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700390}
391
392Return<void> WifiChip::requestDriverDebugDump(
393 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700394 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
395 &WifiChip::requestDriverDebugDumpInternal,
396 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700397}
398
Roshan Pius5c055462016-10-11 08:27:27 -0700399Return<void> WifiChip::requestFirmwareDebugDump(
400 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700401 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
402 &WifiChip::requestFirmwareDebugDumpInternal,
403 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700404}
405
Roshan Pius5c055462016-10-11 08:27:27 -0700406Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700407 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
408 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700409}
410
Roshan Pius5c055462016-10-11 08:27:27 -0700411Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700412 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
413 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700414}
415
Roshan Pius5c055462016-10-11 08:27:27 -0700416Return<void> WifiChip::getApIface(const hidl_string& ifname,
417 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700418 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
419 &WifiChip::getApIfaceInternal, hidl_status_cb,
420 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700421}
422
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800423Return<void> WifiChip::removeApIface(const hidl_string& ifname,
424 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700425 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
426 &WifiChip::removeApIfaceInternal, hidl_status_cb,
427 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800428}
429
Roshan Pius5c055462016-10-11 08:27:27 -0700430Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700431 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
432 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700433}
434
Roshan Pius5c055462016-10-11 08:27:27 -0700435Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700436 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
437 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700438}
439
440Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700441 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700442 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
443 &WifiChip::getNanIfaceInternal, hidl_status_cb,
444 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700445}
446
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800447Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
448 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700449 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
450 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
451 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800452}
453
Roshan Pius5c055462016-10-11 08:27:27 -0700454Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700455 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
456 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700457}
458
Roshan Pius5c055462016-10-11 08:27:27 -0700459Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700460 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
461 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700462}
463
464Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700465 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700466 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
467 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
468 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700469}
470
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800471Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
472 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700473 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
474 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
475 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800476}
477
Roshan Pius5c055462016-10-11 08:27:27 -0700478Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700479 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
480 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700481}
482
Roshan Pius5c055462016-10-11 08:27:27 -0700483Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700484 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
485 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700486}
487
488Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700489 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700490 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
491 &WifiChip::getStaIfaceInternal, hidl_status_cb,
492 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700493}
494
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800495Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
496 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700497 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
498 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
499 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800500}
501
Roshan Pius5c055462016-10-11 08:27:27 -0700502Return<void> WifiChip::createRttController(
503 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700504 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
505 &WifiChip::createRttControllerInternal,
506 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700507}
508
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700509Return<void> WifiChip::getDebugRingBuffersStatus(
510 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700511 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
512 &WifiChip::getDebugRingBuffersStatusInternal,
513 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700514}
515
516Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700517 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
518 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700519 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700520 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
521 &WifiChip::startLoggingToDebugRingBufferInternal,
522 hidl_status_cb, ring_name, verbose_level,
523 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700524}
525
526Return<void> WifiChip::forceDumpToDebugRingBuffer(
527 const hidl_string& ring_name,
528 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700529 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
530 &WifiChip::forceDumpToDebugRingBufferInternal,
531 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700532}
533
Roger Wangb294c762018-11-02 15:34:39 +0800534Return<void> WifiChip::flushRingBufferToFile(
535 flushRingBufferToFile_cb hidl_status_cb) {
536 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
537 &WifiChip::flushRingBufferToFileInternal,
538 hidl_status_cb);
539}
540
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800541Return<void> WifiChip::stopLoggingToDebugRingBuffer(
542 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700543 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
544 &WifiChip::stopLoggingToDebugRingBufferInternal,
545 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800546}
547
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700548Return<void> WifiChip::getDebugHostWakeReasonStats(
549 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700550 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
551 &WifiChip::getDebugHostWakeReasonStatsInternal,
552 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700553}
554
Roshan Pius203cb032016-12-14 17:41:20 -0800555Return<void> WifiChip::enableDebugErrorAlerts(
556 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700557 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
558 &WifiChip::enableDebugErrorAlertsInternal,
559 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800560}
561
Roshan Pius735ff432017-07-25 08:48:08 -0700562Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700563 V1_1::IWifiChip::TxPowerScenario scenario,
564 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700565 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
566 &WifiChip::selectTxPowerScenarioInternal,
567 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700568}
569
Roshan Pius735ff432017-07-25 08:48:08 -0700570Return<void> WifiChip::resetTxPowerScenario(
571 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700572 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
573 &WifiChip::resetTxPowerScenarioInternal,
574 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700575}
576
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700577Return<void> WifiChip::setLatencyMode(LatencyMode mode,
578 setLatencyMode_cb hidl_status_cb) {
579 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
580 &WifiChip::setLatencyModeInternal, hidl_status_cb,
581 mode);
582}
583
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800584Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700585 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800586 registerEventCallback_cb hidl_status_cb) {
587 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
588 &WifiChip::registerEventCallbackInternal_1_2,
589 hidl_status_cb, event_callback);
590}
591
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800592Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700593 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800594 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700595 &WifiChip::selectTxPowerScenarioInternal_1_2,
596 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800597}
598
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700599Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
600 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
601 &WifiChip::getCapabilitiesInternal_1_3,
602 hidl_status_cb);
603}
604
xshu5899e8e2018-01-09 15:36:03 -0800605Return<void> WifiChip::debug(const hidl_handle& handle,
606 const hidl_vec<hidl_string>&) {
607 if (handle != nullptr && handle->numFds >= 1) {
608 int fd = handle->data[0];
609 if (!writeRingbufferFilesInternal()) {
610 LOG(ERROR) << "Error writing files to flash";
611 }
xshu4cb33162018-01-24 15:40:06 -0800612 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800613 if (n_error != 0) {
614 LOG(ERROR) << n_error << " errors occured in cpio function";
615 }
616 fsync(fd);
617 } else {
618 LOG(ERROR) << "File handle error";
619 }
620 return Void();
621}
622
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700623Return<void> WifiChip::createRttController_1_4(
624 const sp<IWifiIface>& bound_iface,
625 createRttController_1_4_cb hidl_status_cb) {
626 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
627 &WifiChip::createRttControllerInternal_1_4,
628 hidl_status_cb, bound_iface);
629}
630
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800631Return<void> WifiChip::registerEventCallback_1_4(
632 const sp<IWifiChipEventCallback>& event_callback,
633 registerEventCallback_cb hidl_status_cb) {
634 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
635 &WifiChip::registerEventCallbackInternal_1_4,
636 hidl_status_cb, event_callback);
637}
638
Roshan Pius35d958c2016-10-06 16:47:38 -0700639void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700640 invalidateAndClearAll(ap_ifaces_);
641 invalidateAndClearAll(nan_ifaces_);
642 invalidateAndClearAll(p2p_ifaces_);
643 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700644 // Since all the ifaces are invalid now, all RTT controller objects
645 // using those ifaces also need to be invalidated.
646 for (const auto& rtt : rtt_controllers_) {
647 rtt->invalidate();
648 }
649 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700650}
651
Roshan Pius82368502019-05-16 12:53:02 -0700652void WifiChip::invalidateAndRemoveDependencies(
653 const std::string& removed_iface_name) {
654 for (const auto& nan_iface : nan_ifaces_) {
655 if (nan_iface->getName() == removed_iface_name) {
656 invalidateAndClear(nan_ifaces_, nan_iface);
657 for (const auto& callback : event_cb_handler_.getCallbacks()) {
658 if (!callback
659 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
660 .isOk()) {
661 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
662 }
663 }
664 }
665 }
666 for (const auto& rtt : rtt_controllers_) {
667 if (rtt->getIfaceName() == removed_iface_name) {
668 invalidateAndClear(rtt_controllers_, rtt);
669 }
670 }
671}
672
Roshan Pius3c868522016-10-27 12:43:49 -0700673std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700674 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700675}
676
677WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800678 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
679 // Deprecated support for this callback.
680 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700681}
682
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700683std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700684 // Deprecated support for this callback.
685 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
686}
687
Roshan Pius3c868522016-10-27 12:43:49 -0700688std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
689WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700690 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700691}
692
Roshan Piusba38d9c2017-12-08 07:32:08 -0800693WifiStatus WifiChip::configureChipInternal(
694 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
695 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700696 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700697 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
698 }
699 if (mode_id == current_mode_id_) {
700 LOG(DEBUG) << "Already in the specified mode " << mode_id;
701 return createWifiStatus(WifiStatusCode::SUCCESS);
702 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800703 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700704 if (status.code != WifiStatusCode::SUCCESS) {
705 for (const auto& callback : event_cb_handler_.getCallbacks()) {
706 if (!callback->onChipReconfigureFailure(status).isOk()) {
707 LOG(ERROR)
708 << "Failed to invoke onChipReconfigureFailure callback";
709 }
710 }
711 return status;
712 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800713 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700714 if (!callback->onChipReconfigured(mode_id).isOk()) {
715 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
716 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800717 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700718 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800719 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700720 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800721 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700722}
723
724std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700725 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700726 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
727 current_mode_id_};
728 }
729 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700730}
731
732std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
733WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700734 IWifiChip::ChipDebugInfo result;
735 legacy_hal::wifi_error legacy_status;
736 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700737 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700738 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800739 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700740 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
741 LOG(ERROR) << "Failed to get driver version: "
742 << legacyErrorToString(legacy_status);
743 WifiStatus status = createWifiStatusFromLegacyError(
744 legacy_status, "failed to get driver version");
745 return {status, result};
746 }
747 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700748
Roshan Piusabcf78f2017-10-06 16:30:38 -0700749 std::string firmware_desc;
750 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800751 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700752 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
753 LOG(ERROR) << "Failed to get firmware version: "
754 << legacyErrorToString(legacy_status);
755 WifiStatus status = createWifiStatusFromLegacyError(
756 legacy_status, "failed to get firmware version");
757 return {status, result};
758 }
759 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700760
Roshan Piusabcf78f2017-10-06 16:30:38 -0700761 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700762}
763
764std::pair<WifiStatus, std::vector<uint8_t>>
765WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700766 legacy_hal::wifi_error legacy_status;
767 std::vector<uint8_t> driver_dump;
768 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700769 legacy_hal_.lock()->requestDriverMemoryDump(
770 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700771 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
772 LOG(ERROR) << "Failed to get driver debug dump: "
773 << legacyErrorToString(legacy_status);
774 return {createWifiStatusFromLegacyError(legacy_status),
775 std::vector<uint8_t>()};
776 }
777 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700778}
779
780std::pair<WifiStatus, std::vector<uint8_t>>
781WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700782 legacy_hal::wifi_error legacy_status;
783 std::vector<uint8_t> firmware_dump;
784 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700785 legacy_hal_.lock()->requestFirmwareMemoryDump(
786 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700787 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
788 LOG(ERROR) << "Failed to get firmware debug dump: "
789 << legacyErrorToString(legacy_status);
790 return {createWifiStatusFromLegacyError(legacy_status), {}};
791 }
792 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700793}
794
795std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700796 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700797 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800798 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700799 std::string ifname = allocateApIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800800 legacy_hal::wifi_error legacy_status =
801 legacy_hal_.lock()->createVirtualInterface(
802 ifname,
803 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
804 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
805 LOG(ERROR) << "Failed to add interface: " << ifname << " "
806 << legacyErrorToString(legacy_status);
807 return {createWifiStatusFromLegacyError(legacy_status), {}};
808 }
Patrik Fimml6beae322019-10-09 17:34:01 +0200809 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700810 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700811 for (const auto& callback : event_cb_handler_.getCallbacks()) {
812 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
813 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
814 }
815 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700816 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700817 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700818}
819
820std::pair<WifiStatus, std::vector<hidl_string>>
821WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700822 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700823 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
824 }
Roshan Pius675609b2017-10-31 14:24:58 -0700825 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700826}
827
828std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800829 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700830 const auto iface = findUsingName(ap_ifaces_, ifname);
831 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700832 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
833 }
Roshan Pius675609b2017-10-31 14:24:58 -0700834 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700835}
836
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800837WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700838 const auto iface = findUsingName(ap_ifaces_, ifname);
839 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700840 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800841 }
Roshan Pius82368502019-05-16 12:53:02 -0700842 // Invalidate & remove any dependent objects first.
843 // Note: This is probably not required because we never create
844 // nan/rtt objects over AP iface. But, there is no harm to do it
845 // here and not make that assumption all over the place.
846 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800847 legacy_hal::wifi_error legacy_status =
848 legacy_hal_.lock()->deleteVirtualInterface(ifname);
849 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
850 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
851 << legacyErrorToString(legacy_status);
852 }
Roshan Pius675609b2017-10-31 14:24:58 -0700853 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700854 for (const auto& callback : event_cb_handler_.getCallbacks()) {
855 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
856 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
857 }
858 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700859 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700860 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800861}
862
Roshan Pius3c868522016-10-27 12:43:49 -0700863std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700864 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700865 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800866 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700867 // These are still assumed to be based on wlan0.
Roshan Pius6036c022019-03-27 10:41:58 -0700868 std::string ifname = getFirstActiveWlanIfaceName();
Roshan Pius356d5a62019-05-17 15:07:34 -0700869 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -0700870 nan_ifaces_.push_back(iface);
871 for (const auto& callback : event_cb_handler_.getCallbacks()) {
872 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
873 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
874 }
875 }
876 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700877}
878
879std::pair<WifiStatus, std::vector<hidl_string>>
880WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700881 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700882 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
883 }
Roshan Pius675609b2017-10-31 14:24:58 -0700884 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700885}
886
887std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800888 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700889 const auto iface = findUsingName(nan_ifaces_, ifname);
890 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700891 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
892 }
Roshan Pius675609b2017-10-31 14:24:58 -0700893 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700894}
895
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800896WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700897 const auto iface = findUsingName(nan_ifaces_, ifname);
898 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700899 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800900 }
Roshan Pius675609b2017-10-31 14:24:58 -0700901 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700902 for (const auto& callback : event_cb_handler_.getCallbacks()) {
903 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
904 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
905 }
906 }
907 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800908}
909
Roshan Pius3c868522016-10-27 12:43:49 -0700910std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700911 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700912 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800913 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700914 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700915 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
916 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700917 for (const auto& callback : event_cb_handler_.getCallbacks()) {
918 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
919 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
920 }
921 }
Roshan Pius675609b2017-10-31 14:24:58 -0700922 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700923}
924
925std::pair<WifiStatus, std::vector<hidl_string>>
926WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700927 if (p2p_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(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700931}
932
933std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800934 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700935 const auto iface = findUsingName(p2p_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::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700943 const auto iface = findUsingName(p2p_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(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700948 for (const auto& callback : event_cb_handler_.getCallbacks()) {
949 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
950 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
951 }
952 }
953 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800954}
955
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -0800956std::pair<WifiStatus, sp<V1_3::IWifiStaIface>>
957WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700958 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700959 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800960 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700961 std::string ifname = allocateStaIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800962 legacy_hal::wifi_error legacy_status =
963 legacy_hal_.lock()->createVirtualInterface(
964 ifname,
965 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
966 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
967 LOG(ERROR) << "Failed to add interface: " << ifname << " "
968 << legacyErrorToString(legacy_status);
969 return {createWifiStatusFromLegacyError(legacy_status), {}};
970 }
Roshan Pius99dab382019-02-14 07:57:10 -0800971 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700972 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700973 for (const auto& callback : event_cb_handler_.getCallbacks()) {
974 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
975 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
976 }
977 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700978 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700979 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700980}
981
982std::pair<WifiStatus, std::vector<hidl_string>>
983WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700984 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700985 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
986 }
Roshan Pius675609b2017-10-31 14:24:58 -0700987 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700988}
989
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -0800990std::pair<WifiStatus, sp<V1_3::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800991 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700992 const auto iface = findUsingName(sta_ifaces_, ifname);
993 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700994 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
995 }
Roshan Pius675609b2017-10-31 14:24:58 -0700996 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700997}
998
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800999WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001000 const auto iface = findUsingName(sta_ifaces_, ifname);
1001 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001002 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001003 }
Roshan Pius82368502019-05-16 12:53:02 -07001004 // Invalidate & remove any dependent objects first.
1005 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001006 legacy_hal::wifi_error legacy_status =
1007 legacy_hal_.lock()->deleteVirtualInterface(ifname);
1008 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1009 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1010 << legacyErrorToString(legacy_status);
1011 }
Roshan Pius675609b2017-10-31 14:24:58 -07001012 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001013 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1014 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1015 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1016 }
1017 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001018 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001019 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001020}
1021
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001022std::pair<WifiStatus, sp<V1_0::IWifiRttController>>
1023WifiChip::createRttControllerInternal(const sp<IWifiIface>& /*bound_iface*/) {
1024 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001025 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001026}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001027
1028std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1029WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001030 legacy_hal::wifi_error legacy_status;
1031 std::vector<legacy_hal::wifi_ring_buffer_status>
1032 legacy_ring_buffer_status_vec;
1033 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001034 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001035 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1036 return {createWifiStatusFromLegacyError(legacy_status), {}};
1037 }
1038 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1039 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1040 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1041 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1042 }
1043 return {createWifiStatus(WifiStatusCode::SUCCESS),
1044 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001045}
1046
1047WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001048 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1049 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1050 WifiStatus status = registerDebugRingBufferCallback();
1051 if (status.code != WifiStatusCode::SUCCESS) {
1052 return status;
1053 }
1054 legacy_hal::wifi_error legacy_status =
1055 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001056 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001057 static_cast<
1058 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1059 verbose_level),
1060 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001061 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1062 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001063 // if verbose logging enabled, turn up HAL daemon logging as well.
1064 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1065 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1066 } else {
1067 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1068 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001069 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001070}
1071
1072WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001073 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001074 WifiStatus status = registerDebugRingBufferCallback();
1075 if (status.code != WifiStatusCode::SUCCESS) {
1076 return status;
1077 }
1078 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001079 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1080 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001081
Roshan Piusabcf78f2017-10-06 16:30:38 -07001082 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001083}
1084
Roger Wangb294c762018-11-02 15:34:39 +08001085WifiStatus WifiChip::flushRingBufferToFileInternal() {
1086 if (!writeRingbufferFilesInternal()) {
1087 LOG(ERROR) << "Error writing files to flash";
1088 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1089 }
1090 return createWifiStatus(WifiStatusCode::SUCCESS);
1091}
1092
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001093WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001094 legacy_hal::wifi_error legacy_status =
1095 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001096 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001097 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001098}
1099
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001100std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1101WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001102 legacy_hal::wifi_error legacy_status;
1103 legacy_hal::WakeReasonStats legacy_stats;
1104 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001105 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001106 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1107 return {createWifiStatusFromLegacyError(legacy_status), {}};
1108 }
1109 WifiDebugHostWakeReasonStats hidl_stats;
1110 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1111 &hidl_stats)) {
1112 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1113 }
1114 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001115}
1116
Roshan Pius203cb032016-12-14 17:41:20 -08001117WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001118 legacy_hal::wifi_error legacy_status;
1119 if (enable) {
1120 android::wp<WifiChip> weak_ptr_this(this);
1121 const auto& on_alert_callback = [weak_ptr_this](
1122 int32_t error_code,
1123 std::vector<uint8_t> debug_data) {
1124 const auto shared_ptr_this = weak_ptr_this.promote();
1125 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1126 LOG(ERROR) << "Callback invoked on an invalid object";
1127 return;
1128 }
1129 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1130 if (!callback->onDebugErrorAlert(error_code, debug_data)
1131 .isOk()) {
1132 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1133 }
1134 }
1135 };
1136 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001137 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001138 } else {
1139 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001140 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001141 }
1142 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001143}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001144
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001145WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001146 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001147 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001148 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001149 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1150 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001151}
1152
Roshan Pius735ff432017-07-25 08:48:08 -07001153WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001154 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001155 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001156 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001157}
1158
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001159WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1160 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001161 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001162 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1163 return createWifiStatusFromLegacyError(legacy_status);
1164}
1165
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001166WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001167 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
1168 // Deprecated support for this callback.
1169 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001170}
1171
Jong Wook Kimda830c92018-07-23 15:29:38 -07001172WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1173 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001174 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001175 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001176 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1177 return createWifiStatusFromLegacyError(legacy_status);
1178}
1179
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001180std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
1181 legacy_hal::wifi_error legacy_status;
1182 uint32_t legacy_feature_set;
1183 uint32_t legacy_logger_feature_set;
1184 const auto ifname = getFirstActiveWlanIfaceName();
1185 std::tie(legacy_status, legacy_feature_set) =
1186 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
1187 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1188 return {createWifiStatusFromLegacyError(legacy_status), 0};
1189 }
1190 std::tie(legacy_status, legacy_logger_feature_set) =
1191 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
1192 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1193 // some devices don't support querying logger feature set
1194 legacy_logger_feature_set = 0;
1195 }
1196 uint32_t hidl_caps;
1197 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
1198 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
1199 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1200 }
1201 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1202}
1203
1204std::pair<WifiStatus, sp<IWifiRttController>>
1205WifiChip::createRttControllerInternal_1_4(const sp<IWifiIface>& bound_iface) {
1206 if (sta_ifaces_.size() == 0 &&
1207 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1208 LOG(ERROR)
1209 << "createRttControllerInternal_1_4: Chip cannot support STAs "
1210 "(and RTT by extension)";
1211 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1212 }
1213 sp<WifiRttController> rtt = new WifiRttController(
1214 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1215 rtt_controllers_.emplace_back(rtt);
1216 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1217}
1218
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001219WifiStatus WifiChip::registerEventCallbackInternal_1_4(
1220 const sp<IWifiChipEventCallback>& event_callback) {
1221 if (!event_cb_handler_.addCallback(event_callback)) {
1222 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1223 }
1224 return createWifiStatus(WifiStatusCode::SUCCESS);
1225}
1226
Roshan Piusba38d9c2017-12-08 07:32:08 -08001227WifiStatus WifiChip::handleChipConfiguration(
1228 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1229 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001230 // If the chip is already configured in a different mode, stop
1231 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001232 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001233 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1234 << " to mode " << mode_id;
1235 invalidateAndRemoveAllIfaces();
1236 legacy_hal::wifi_error legacy_status =
1237 legacy_hal_.lock()->stop(lock, []() {});
1238 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1239 LOG(ERROR) << "Failed to stop legacy HAL: "
1240 << legacyErrorToString(legacy_status);
1241 return createWifiStatusFromLegacyError(legacy_status);
1242 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001243 }
Roshan Piuscc338202017-11-02 13:54:09 -07001244 // Firmware mode change not needed for V2 devices.
1245 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001246 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001247 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001248 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001249 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1250 }
1251 if (!success) {
1252 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1253 }
1254 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1255 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1256 LOG(ERROR) << "Failed to start legacy HAL: "
1257 << legacyErrorToString(legacy_status);
1258 return createWifiStatusFromLegacyError(legacy_status);
1259 }
Roshan Pius85c64412018-01-22 17:58:40 -08001260 // Every time the HAL is restarted, we need to register the
1261 // radio mode change callback.
1262 WifiStatus status = registerRadioModeChangeCallback();
1263 if (status.code != WifiStatusCode::SUCCESS) {
1264 // This probably is not a critical failure?
1265 LOG(ERROR) << "Failed to register radio mode change callback";
1266 }
chenpaulf5eca292019-03-14 11:08:03 +08001267 // Extract and save the version information into property.
1268 std::pair<WifiStatus, IWifiChip::ChipDebugInfo> version_info;
1269 version_info = WifiChip::requestChipDebugInfoInternal();
1270 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1271 property_set("vendor.wlan.firmware.version",
1272 version_info.second.firmwareDescription.c_str());
1273 property_set("vendor.wlan.driver.version",
1274 version_info.second.driverDescription.c_str());
1275 }
1276
Roshan Piusabcf78f2017-10-06 16:30:38 -07001277 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001278}
Roshan Pius48185b22016-12-15 19:10:30 -08001279
1280WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001281 if (debug_ring_buffer_cb_registered_) {
1282 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001283 }
Roshan Pius3797e182017-03-30 18:01:54 -07001284
Roshan Piusabcf78f2017-10-06 16:30:38 -07001285 android::wp<WifiChip> weak_ptr_this(this);
1286 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001287 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001288 const std::vector<uint8_t>& data,
1289 const legacy_hal::wifi_ring_buffer_status& status) {
1290 const auto shared_ptr_this = weak_ptr_this.promote();
1291 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1292 LOG(ERROR) << "Callback invoked on an invalid object";
1293 return;
1294 }
1295 WifiDebugRingBufferStatus hidl_status;
1296 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1297 status, &hidl_status)) {
1298 LOG(ERROR) << "Error converting ring buffer status";
1299 return;
1300 }
xshu5899e8e2018-01-09 15:36:03 -08001301 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1302 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1303 Ringbuffer& cur_buffer = target->second;
1304 cur_buffer.append(data);
1305 } else {
1306 LOG(ERROR) << "Ringname " << name << " not found";
1307 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001308 }
1309 };
1310 legacy_hal::wifi_error legacy_status =
1311 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001312 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001313
Roshan Piusabcf78f2017-10-06 16:30:38 -07001314 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1315 debug_ring_buffer_cb_registered_ = true;
1316 }
1317 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001318}
1319
Roshan Pius85c64412018-01-22 17:58:40 -08001320WifiStatus WifiChip::registerRadioModeChangeCallback() {
1321 android::wp<WifiChip> weak_ptr_this(this);
1322 const auto& on_radio_mode_change_callback =
1323 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1324 const auto shared_ptr_this = weak_ptr_this.promote();
1325 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1326 LOG(ERROR) << "Callback invoked on an invalid object";
1327 return;
1328 }
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001329 std::vector<IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001330 hidl_radio_mode_infos;
1331 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1332 mac_infos, &hidl_radio_mode_infos)) {
1333 LOG(ERROR) << "Error converting wifi mac info";
1334 return;
1335 }
1336 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001337 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos)
Roshan Pius85c64412018-01-22 17:58:40 -08001338 .isOk()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001339 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
Roshan Pius85c64412018-01-22 17:58:40 -08001340 << " callback on: " << toString(callback);
1341 }
1342 }
1343 };
1344 legacy_hal::wifi_error legacy_status =
1345 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001346 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001347 return createWifiStatusFromLegacyError(legacy_status);
1348}
1349
Roshan Piuscc338202017-11-02 13:54:09 -07001350std::vector<IWifiChip::ChipIfaceCombination>
1351WifiChip::getCurrentModeIfaceCombinations() {
1352 if (!isValidModeId(current_mode_id_)) {
1353 LOG(ERROR) << "Chip not configured in a mode yet";
1354 return {};
1355 }
1356 for (const auto& mode : modes_) {
1357 if (mode.id == current_mode_id_) {
1358 return mode.availableCombinations;
1359 }
1360 }
1361 CHECK(0) << "Expected to find iface combinations for current mode!";
1362 return {};
1363}
1364
1365// Returns a map indexed by IfaceType with the number of ifaces currently
1366// created of the corresponding type.
1367std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1368 std::map<IfaceType, size_t> iface_counts;
1369 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1370 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1371 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1372 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1373 return iface_counts;
1374}
1375
1376// This expands the provided iface combinations to a more parseable
1377// form. Returns a vector of available combinations possible with the number
1378// of ifaces of each type in the combination.
1379// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1380std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1381 const IWifiChip::ChipIfaceCombination& combination) {
1382 uint32_t num_expanded_combos = 1;
1383 for (const auto& limit : combination.limits) {
1384 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1385 num_expanded_combos *= limit.types.size();
1386 }
1387 }
1388
1389 // Allocate the vector of expanded combos and reset all iface counts to 0
1390 // in each combo.
1391 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1392 expanded_combos.resize(num_expanded_combos);
1393 for (auto& expanded_combo : expanded_combos) {
1394 for (const auto type :
1395 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1396 expanded_combo[type] = 0;
1397 }
1398 }
1399 uint32_t span = num_expanded_combos;
1400 for (const auto& limit : combination.limits) {
1401 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1402 span /= limit.types.size();
1403 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1404 const auto iface_type =
1405 limit.types[(k / span) % limit.types.size()];
1406 expanded_combos[k][iface_type]++;
1407 }
1408 }
1409 }
1410 return expanded_combos;
1411}
1412
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001413bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1414 const std::map<IfaceType, size_t>& expanded_combo,
1415 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001416 const auto current_combo = getCurrentIfaceCombination();
1417
1418 // Check if we have space for 1 more iface of |type| in this combo
1419 for (const auto type :
1420 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1421 size_t num_ifaces_needed = current_combo.at(type);
1422 if (type == requested_type) {
1423 num_ifaces_needed++;
1424 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001425 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001426 if (num_ifaces_needed > num_ifaces_allowed) {
1427 return false;
1428 }
1429 }
1430 return true;
1431}
1432
1433// This method does the following:
1434// a) Enumerate all possible iface combos by expanding the current
1435// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001436// b) Check if the requested iface type can be added to the current mode
1437// with the iface combination that is already active.
1438bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1439 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001440 if (!isValidModeId(current_mode_id_)) {
1441 LOG(ERROR) << "Chip not configured in a mode yet";
1442 return false;
1443 }
1444 const auto combinations = getCurrentModeIfaceCombinations();
1445 for (const auto& combination : combinations) {
1446 const auto expanded_combos = expandIfaceCombinations(combination);
1447 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001448 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1449 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001450 return true;
1451 }
1452 }
1453 }
1454 return false;
1455}
1456
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001457// Note: This does not consider ifaces already active. It only checks if the
1458// provided expanded iface combination can support the requested combo.
1459bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1460 const std::map<IfaceType, size_t>& expanded_combo,
1461 const std::map<IfaceType, size_t>& req_combo) {
1462 // Check if we have space for 1 more iface of |type| in this combo
1463 for (const auto type :
1464 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1465 if (req_combo.count(type) == 0) {
1466 // Iface of "type" not in the req_combo.
1467 continue;
1468 }
1469 size_t num_ifaces_needed = req_combo.at(type);
1470 size_t num_ifaces_allowed = expanded_combo.at(type);
1471 if (num_ifaces_needed > num_ifaces_allowed) {
1472 return false;
1473 }
1474 }
1475 return true;
1476}
1477// This method does the following:
1478// a) Enumerate all possible iface combos by expanding the current
1479// ChipIfaceCombination.
1480// b) Check if the requested iface combo can be added to the current mode.
1481// Note: This does not consider ifaces already active. It only checks if the
1482// current mode can support the requested combo.
1483bool WifiChip::canCurrentModeSupportIfaceCombo(
1484 const std::map<IfaceType, size_t>& req_combo) {
1485 if (!isValidModeId(current_mode_id_)) {
1486 LOG(ERROR) << "Chip not configured in a mode yet";
1487 return false;
1488 }
1489 const auto combinations = getCurrentModeIfaceCombinations();
1490 for (const auto& combination : combinations) {
1491 const auto expanded_combos = expandIfaceCombinations(combination);
1492 for (const auto& expanded_combo : expanded_combos) {
1493 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1494 req_combo)) {
1495 return true;
1496 }
1497 }
1498 }
1499 return false;
1500}
1501
1502// This method does the following:
1503// a) Enumerate all possible iface combos by expanding the current
1504// ChipIfaceCombination.
1505// b) Check if the requested iface type can be added to the current mode.
1506bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1507 // Check if we can support atleast 1 iface of type.
1508 std::map<IfaceType, size_t> req_iface_combo;
1509 req_iface_combo[requested_type] = 1;
1510 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1511}
1512
Roshan Piuscc338202017-11-02 13:54:09 -07001513bool WifiChip::isValidModeId(ChipModeId mode_id) {
1514 for (const auto& mode : modes_) {
1515 if (mode.id == mode_id) {
1516 return true;
1517 }
1518 }
1519 return false;
1520}
1521
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001522bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1523 // Check if we can support atleast 1 STA & 1 AP concurrently.
1524 std::map<IfaceType, size_t> req_iface_combo;
1525 req_iface_combo[IfaceType::AP] = 1;
1526 req_iface_combo[IfaceType::STA] = 1;
1527 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1528}
1529
James Mattisd2e4c072019-05-22 16:14:48 -07001530bool WifiChip::isDualApAllowedInCurrentMode() {
1531 // Check if we can support atleast 1 STA & 1 AP concurrently.
1532 std::map<IfaceType, size_t> req_iface_combo;
1533 req_iface_combo[IfaceType::AP] = 2;
1534 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1535}
1536
Roshan Pius6036c022019-03-27 10:41:58 -07001537std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001538 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1539 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001540 // This could happen if the chip call is made before any STA/AP
1541 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001542 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Roshan Pius6036c022019-03-27 10:41:58 -07001543 return getWlanIfaceName(0);
1544}
1545
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001546// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1547// not already in use.
1548// Note: This doesn't check the actual presence of these interfaces.
1549std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1550 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1551 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001552 if (findUsingName(ap_ifaces_, ifname)) continue;
1553 if (findUsingName(sta_ifaces_, ifname)) continue;
1554 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001555 }
1556 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001557 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001558 return {};
1559}
1560
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001561// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001562// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001563std::string WifiChip::allocateApIfaceName() {
James Mattisd2e4c072019-05-22 16:14:48 -07001564 return allocateApOrStaIfaceName((isStaApConcurrencyAllowedInCurrentMode() &&
1565 !isDualApAllowedInCurrentMode())
1566 ? 1
1567 : 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001568}
1569
1570// STA iface names start with idx 0.
1571// Primary STA iface will always be 0.
1572std::string WifiChip::allocateStaIfaceName() {
1573 return allocateApOrStaIfaceName(0);
1574}
1575
xshu5899e8e2018-01-09 15:36:03 -08001576bool WifiChip::writeRingbufferFilesInternal() {
1577 if (!removeOldFilesInternal()) {
1578 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1579 return false;
1580 }
1581 // write ringbuffers to file
1582 for (const auto& item : ringbuffer_map_) {
1583 const Ringbuffer& cur_buffer = item.second;
1584 if (cur_buffer.getData().empty()) {
1585 continue;
1586 }
1587 const std::string file_path_raw =
1588 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1589 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1590 if (dump_fd == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001591 PLOG(ERROR) << "create file failed";
xshu5899e8e2018-01-09 15:36:03 -08001592 return false;
1593 }
1594 unique_fd file_auto_closer(dump_fd);
1595 for (const auto& cur_block : cur_buffer.getData()) {
1596 if (write(dump_fd, cur_block.data(),
1597 sizeof(cur_block[0]) * cur_block.size()) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001598 PLOG(ERROR) << "Error writing to file";
xshu5899e8e2018-01-09 15:36:03 -08001599 }
1600 }
1601 }
1602 return true;
1603}
1604
Roshan Pius79a99752016-10-04 13:03:58 -07001605} // namespace implementation
Ahmed ElArabawyf501a982019-07-23 15:02:22 -07001606} // namespace V1_4
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001607} // namespace wifi
1608} // namespace hardware
1609} // namespace android