blob: 27320c407b1c568898edd66c3c7e6749f64719b1 [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 {
Jong Wook Kimda830c92018-07-23 15:29:38 -0700310namespace V1_3 {
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 Pius200a17d2017-11-01 13:03:35 -0700324 feature_flags_(feature_flags),
Roshan Pius52947fb2016-11-18 11:38:07 -0800325 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800326 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
327 modes_(feature_flags.lock()->getChipModes()),
Roshan Pius8574e7f2019-04-01 13:30:40 -0700328 debug_ring_buffer_cb_registered_(false) {
329 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
330}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700331
Roshan Piusaabe5752016-09-29 09:03:59 -0700332void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700333 if (!writeRingbufferFilesInternal()) {
334 LOG(ERROR) << "Error writing files to flash";
335 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700336 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700337 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700338 legacy_hal_.reset();
339 event_cb_handler_.invalidate();
340 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700341}
342
Roshan Piusabcf78f2017-10-06 16:30:38 -0700343bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700344
Jong Wook Kimda830c92018-07-23 15:29:38 -0700345std::set<sp<V1_2::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700346 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800347}
348
Roshan Pius5c055462016-10-11 08:27:27 -0700349Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700350 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
351 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700352}
353
Jong Wook Kimda830c92018-07-23 15:29:38 -0700354// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700355Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800356 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700357 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700358 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
359 &WifiChip::registerEventCallbackInternal,
360 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700361}
362
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700363Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700364 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
365 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700366}
367
Roshan Pius5c055462016-10-11 08:27:27 -0700368Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700369 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
370 &WifiChip::getAvailableModesInternal,
371 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700372}
373
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800374Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700375 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800376 return validateAndCallWithLock(
377 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
378 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700379}
380
Roshan Pius5c055462016-10-11 08:27:27 -0700381Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700382 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
383 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700384}
385
Roshan Pius5c055462016-10-11 08:27:27 -0700386Return<void> WifiChip::requestChipDebugInfo(
387 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700388 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
389 &WifiChip::requestChipDebugInfoInternal,
390 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700391}
392
393Return<void> WifiChip::requestDriverDebugDump(
394 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700395 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
396 &WifiChip::requestDriverDebugDumpInternal,
397 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700398}
399
Roshan Pius5c055462016-10-11 08:27:27 -0700400Return<void> WifiChip::requestFirmwareDebugDump(
401 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700402 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
403 &WifiChip::requestFirmwareDebugDumpInternal,
404 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700405}
406
Roshan Pius5c055462016-10-11 08:27:27 -0700407Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700408 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
409 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700410}
411
Roshan Pius5c055462016-10-11 08:27:27 -0700412Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700413 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
414 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700415}
416
Roshan Pius5c055462016-10-11 08:27:27 -0700417Return<void> WifiChip::getApIface(const hidl_string& ifname,
418 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700419 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
420 &WifiChip::getApIfaceInternal, hidl_status_cb,
421 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700422}
423
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800424Return<void> WifiChip::removeApIface(const hidl_string& ifname,
425 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700426 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
427 &WifiChip::removeApIfaceInternal, hidl_status_cb,
428 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800429}
430
Roshan Pius5c055462016-10-11 08:27:27 -0700431Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700432 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
433 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700434}
435
Roshan Pius5c055462016-10-11 08:27:27 -0700436Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700437 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
438 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700439}
440
441Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700442 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700443 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
444 &WifiChip::getNanIfaceInternal, hidl_status_cb,
445 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700446}
447
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800448Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
449 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700450 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
451 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
452 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800453}
454
Roshan Pius5c055462016-10-11 08:27:27 -0700455Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700456 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
457 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700458}
459
Roshan Pius5c055462016-10-11 08:27:27 -0700460Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700461 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
462 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700463}
464
465Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700466 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700467 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
468 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
469 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700470}
471
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800472Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
473 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700474 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
475 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
476 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800477}
478
Roshan Pius5c055462016-10-11 08:27:27 -0700479Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700480 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
481 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700482}
483
Roshan Pius5c055462016-10-11 08:27:27 -0700484Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700485 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
486 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700487}
488
489Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700490 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700491 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
492 &WifiChip::getStaIfaceInternal, hidl_status_cb,
493 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700494}
495
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800496Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
497 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700498 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
499 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
500 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800501}
502
Roshan Pius5c055462016-10-11 08:27:27 -0700503Return<void> WifiChip::createRttController(
504 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700505 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
506 &WifiChip::createRttControllerInternal,
507 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700508}
509
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700510Return<void> WifiChip::getDebugRingBuffersStatus(
511 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700512 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
513 &WifiChip::getDebugRingBuffersStatusInternal,
514 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700515}
516
517Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700518 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
519 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700520 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700521 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
522 &WifiChip::startLoggingToDebugRingBufferInternal,
523 hidl_status_cb, ring_name, verbose_level,
524 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700525}
526
527Return<void> WifiChip::forceDumpToDebugRingBuffer(
528 const hidl_string& ring_name,
529 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700530 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
531 &WifiChip::forceDumpToDebugRingBufferInternal,
532 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700533}
534
Roger Wangb294c762018-11-02 15:34:39 +0800535Return<void> WifiChip::flushRingBufferToFile(
536 flushRingBufferToFile_cb hidl_status_cb) {
537 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
538 &WifiChip::flushRingBufferToFileInternal,
539 hidl_status_cb);
540}
541
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800542Return<void> WifiChip::stopLoggingToDebugRingBuffer(
543 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700544 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
545 &WifiChip::stopLoggingToDebugRingBufferInternal,
546 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800547}
548
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700549Return<void> WifiChip::getDebugHostWakeReasonStats(
550 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700551 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
552 &WifiChip::getDebugHostWakeReasonStatsInternal,
553 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700554}
555
Roshan Pius203cb032016-12-14 17:41:20 -0800556Return<void> WifiChip::enableDebugErrorAlerts(
557 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700558 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
559 &WifiChip::enableDebugErrorAlertsInternal,
560 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800561}
562
Roshan Pius735ff432017-07-25 08:48:08 -0700563Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700564 V1_1::IWifiChip::TxPowerScenario scenario,
565 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700566 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
567 &WifiChip::selectTxPowerScenarioInternal,
568 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700569}
570
Roshan Pius735ff432017-07-25 08:48:08 -0700571Return<void> WifiChip::resetTxPowerScenario(
572 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700573 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
574 &WifiChip::resetTxPowerScenarioInternal,
575 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700576}
577
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700578Return<void> WifiChip::setLatencyMode(LatencyMode mode,
579 setLatencyMode_cb hidl_status_cb) {
580 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
581 &WifiChip::setLatencyModeInternal, hidl_status_cb,
582 mode);
583}
584
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800585Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700586 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800587 registerEventCallback_cb hidl_status_cb) {
588 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
589 &WifiChip::registerEventCallbackInternal_1_2,
590 hidl_status_cb, event_callback);
591}
592
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800593Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700594 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800595 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700596 &WifiChip::selectTxPowerScenarioInternal_1_2,
597 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800598}
599
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700600Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
601 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
602 &WifiChip::getCapabilitiesInternal_1_3,
603 hidl_status_cb);
604}
605
xshu5899e8e2018-01-09 15:36:03 -0800606Return<void> WifiChip::debug(const hidl_handle& handle,
607 const hidl_vec<hidl_string>&) {
608 if (handle != nullptr && handle->numFds >= 1) {
609 int fd = handle->data[0];
610 if (!writeRingbufferFilesInternal()) {
611 LOG(ERROR) << "Error writing files to flash";
612 }
xshu4cb33162018-01-24 15:40:06 -0800613 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800614 if (n_error != 0) {
615 LOG(ERROR) << n_error << " errors occured in cpio function";
616 }
617 fsync(fd);
618 } else {
619 LOG(ERROR) << "File handle error";
620 }
621 return Void();
622}
623
Roshan Pius35d958c2016-10-06 16:47:38 -0700624void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700625 invalidateAndClearAll(ap_ifaces_);
626 invalidateAndClearAll(nan_ifaces_);
627 invalidateAndClearAll(p2p_ifaces_);
628 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700629 // Since all the ifaces are invalid now, all RTT controller objects
630 // using those ifaces also need to be invalidated.
631 for (const auto& rtt : rtt_controllers_) {
632 rtt->invalidate();
633 }
634 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700635}
636
Roshan Pius82368502019-05-16 12:53:02 -0700637void WifiChip::invalidateAndRemoveDependencies(
638 const std::string& removed_iface_name) {
639 for (const auto& nan_iface : nan_ifaces_) {
640 if (nan_iface->getName() == removed_iface_name) {
641 invalidateAndClear(nan_ifaces_, nan_iface);
642 for (const auto& callback : event_cb_handler_.getCallbacks()) {
643 if (!callback
644 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
645 .isOk()) {
646 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
647 }
648 }
649 }
650 }
651 for (const auto& rtt : rtt_controllers_) {
652 if (rtt->getIfaceName() == removed_iface_name) {
653 invalidateAndClear(rtt_controllers_, rtt);
654 }
655 }
656}
657
Roshan Pius3c868522016-10-27 12:43:49 -0700658std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700659 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700660}
661
662WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800663 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
664 // Deprecated support for this callback.
665 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700666}
667
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700668std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700669 // Deprecated support for this callback.
670 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
671}
672
673std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700674 legacy_hal::wifi_error legacy_status;
675 uint32_t legacy_feature_set;
676 uint32_t legacy_logger_feature_set;
Roshan Pius6036c022019-03-27 10:41:58 -0700677 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700678 std::tie(legacy_status, legacy_feature_set) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800679 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700680 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
681 return {createWifiStatusFromLegacyError(legacy_status), 0};
682 }
683 std::tie(legacy_status, legacy_logger_feature_set) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800684 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700685 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius876220f2017-12-28 16:19:06 -0800686 // some devices don't support querying logger feature set
687 legacy_logger_feature_set = 0;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700688 }
689 uint32_t hidl_caps;
690 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
691 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
692 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
693 }
694 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700695}
696
Roshan Pius3c868522016-10-27 12:43:49 -0700697std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
698WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700699 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700700}
701
Roshan Piusba38d9c2017-12-08 07:32:08 -0800702WifiStatus WifiChip::configureChipInternal(
703 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
704 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700705 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700706 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
707 }
708 if (mode_id == current_mode_id_) {
709 LOG(DEBUG) << "Already in the specified mode " << mode_id;
710 return createWifiStatus(WifiStatusCode::SUCCESS);
711 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800712 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700713 if (status.code != WifiStatusCode::SUCCESS) {
714 for (const auto& callback : event_cb_handler_.getCallbacks()) {
715 if (!callback->onChipReconfigureFailure(status).isOk()) {
716 LOG(ERROR)
717 << "Failed to invoke onChipReconfigureFailure callback";
718 }
719 }
720 return status;
721 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800722 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700723 if (!callback->onChipReconfigured(mode_id).isOk()) {
724 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
725 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800726 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700727 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800728 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700729 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800730 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700731}
732
733std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700734 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700735 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
736 current_mode_id_};
737 }
738 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700739}
740
741std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
742WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700743 IWifiChip::ChipDebugInfo result;
744 legacy_hal::wifi_error legacy_status;
745 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700746 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700747 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800748 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700749 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
750 LOG(ERROR) << "Failed to get driver version: "
751 << legacyErrorToString(legacy_status);
752 WifiStatus status = createWifiStatusFromLegacyError(
753 legacy_status, "failed to get driver version");
754 return {status, result};
755 }
756 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700757
Roshan Piusabcf78f2017-10-06 16:30:38 -0700758 std::string firmware_desc;
759 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800760 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700761 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
762 LOG(ERROR) << "Failed to get firmware version: "
763 << legacyErrorToString(legacy_status);
764 WifiStatus status = createWifiStatusFromLegacyError(
765 legacy_status, "failed to get firmware version");
766 return {status, result};
767 }
768 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700769
Roshan Piusabcf78f2017-10-06 16:30:38 -0700770 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700771}
772
773std::pair<WifiStatus, std::vector<uint8_t>>
774WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700775 legacy_hal::wifi_error legacy_status;
776 std::vector<uint8_t> driver_dump;
777 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700778 legacy_hal_.lock()->requestDriverMemoryDump(
779 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700780 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
781 LOG(ERROR) << "Failed to get driver debug dump: "
782 << legacyErrorToString(legacy_status);
783 return {createWifiStatusFromLegacyError(legacy_status),
784 std::vector<uint8_t>()};
785 }
786 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700787}
788
789std::pair<WifiStatus, std::vector<uint8_t>>
790WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700791 legacy_hal::wifi_error legacy_status;
792 std::vector<uint8_t> firmware_dump;
793 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700794 legacy_hal_.lock()->requestFirmwareMemoryDump(
795 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700796 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
797 LOG(ERROR) << "Failed to get firmware debug dump: "
798 << legacyErrorToString(legacy_status);
799 return {createWifiStatusFromLegacyError(legacy_status), {}};
800 }
801 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700802}
803
804std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700805 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700806 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800807 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700808 std::string ifname = allocateApIfaceName();
Roshan Pius3b6705f2019-02-14 09:43:43 -0800809 sp<WifiApIface> iface =
810 new WifiApIface(ifname, legacy_hal_, iface_util_, feature_flags_);
Roshan Pius675609b2017-10-31 14:24:58 -0700811 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700812 for (const auto& callback : event_cb_handler_.getCallbacks()) {
813 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
814 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
815 }
816 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700817 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700818 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700819}
820
821std::pair<WifiStatus, std::vector<hidl_string>>
822WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700823 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700824 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
825 }
Roshan Pius675609b2017-10-31 14:24:58 -0700826 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700827}
828
829std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800830 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700831 const auto iface = findUsingName(ap_ifaces_, ifname);
832 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700833 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
834 }
Roshan Pius675609b2017-10-31 14:24:58 -0700835 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700836}
837
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800838WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700839 const auto iface = findUsingName(ap_ifaces_, ifname);
840 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700841 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800842 }
Roshan Pius82368502019-05-16 12:53:02 -0700843 // Invalidate & remove any dependent objects first.
844 // Note: This is probably not required because we never create
845 // nan/rtt objects over AP iface. But, there is no harm to do it
846 // here and not make that assumption all over the place.
847 invalidateAndRemoveDependencies(ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700848 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700849 for (const auto& callback : event_cb_handler_.getCallbacks()) {
850 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
851 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
852 }
853 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700854 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700855 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800856}
857
Roshan Pius3c868522016-10-27 12:43:49 -0700858std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700859 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700860 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800861 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700862 // These are still assumed to be based on wlan0.
Roshan Pius6036c022019-03-27 10:41:58 -0700863 std::string ifname = getFirstActiveWlanIfaceName();
Roshan Piuscc338202017-11-02 13:54:09 -0700864 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_);
865 nan_ifaces_.push_back(iface);
866 for (const auto& callback : event_cb_handler_.getCallbacks()) {
867 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
868 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
869 }
870 }
871 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700872}
873
874std::pair<WifiStatus, std::vector<hidl_string>>
875WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700876 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700877 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
878 }
Roshan Pius675609b2017-10-31 14:24:58 -0700879 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700880}
881
882std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800883 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700884 const auto iface = findUsingName(nan_ifaces_, ifname);
885 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700886 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
887 }
Roshan Pius675609b2017-10-31 14:24:58 -0700888 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700889}
890
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800891WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700892 const auto iface = findUsingName(nan_ifaces_, ifname);
893 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700894 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800895 }
Roshan Pius675609b2017-10-31 14:24:58 -0700896 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700897 for (const auto& callback : event_cb_handler_.getCallbacks()) {
898 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
899 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
900 }
901 }
902 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800903}
904
Roshan Pius3c868522016-10-27 12:43:49 -0700905std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700906 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700907 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800908 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700909 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700910 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
911 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700912 for (const auto& callback : event_cb_handler_.getCallbacks()) {
913 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
914 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
915 }
916 }
Roshan Pius675609b2017-10-31 14:24:58 -0700917 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700918}
919
920std::pair<WifiStatus, std::vector<hidl_string>>
921WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700922 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700923 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
924 }
Roshan Pius675609b2017-10-31 14:24:58 -0700925 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700926}
927
928std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800929 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700930 const auto iface = findUsingName(p2p_ifaces_, ifname);
931 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700932 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
933 }
Roshan Pius675609b2017-10-31 14:24:58 -0700934 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700935}
936
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800937WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700938 const auto iface = findUsingName(p2p_ifaces_, ifname);
939 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700940 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800941 }
Roshan Pius675609b2017-10-31 14:24:58 -0700942 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700943 for (const auto& callback : event_cb_handler_.getCallbacks()) {
944 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
945 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
946 }
947 }
948 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800949}
950
Roshan Pius3c868522016-10-27 12:43:49 -0700951std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700952 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700953 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800954 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700955 std::string ifname = allocateStaIfaceName();
Roshan Pius99dab382019-02-14 07:57:10 -0800956 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700957 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700958 for (const auto& callback : event_cb_handler_.getCallbacks()) {
959 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
960 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
961 }
962 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700963 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700964 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700965}
966
967std::pair<WifiStatus, std::vector<hidl_string>>
968WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700969 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700970 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
971 }
Roshan Pius675609b2017-10-31 14:24:58 -0700972 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700973}
974
975std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800976 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700977 const auto iface = findUsingName(sta_ifaces_, ifname);
978 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700979 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
980 }
Roshan Pius675609b2017-10-31 14:24:58 -0700981 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700982}
983
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800984WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700985 const auto iface = findUsingName(sta_ifaces_, ifname);
986 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700987 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800988 }
Roshan Pius82368502019-05-16 12:53:02 -0700989 // Invalidate & remove any dependent objects first.
990 invalidateAndRemoveDependencies(ifname);
Roshan Pius675609b2017-10-31 14:24:58 -0700991 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700992 for (const auto& callback : event_cb_handler_.getCallbacks()) {
993 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
994 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
995 }
996 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700997 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700998 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800999}
1000
Roshan Pius3c868522016-10-27 12:43:49 -07001001std::pair<WifiStatus, sp<IWifiRttController>>
1002WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
Etan Cohen7240a1a2018-08-29 08:14:09 -07001003 if (sta_ifaces_.size() == 0 &&
1004 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1005 LOG(ERROR) << "createRttControllerInternal: Chip cannot support STAs "
1006 "(and RTT by extension)";
1007 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1008 }
Roshan Pius6036c022019-03-27 10:41:58 -07001009 sp<WifiRttController> rtt = new WifiRttController(
1010 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001011 rtt_controllers_.emplace_back(rtt);
1012 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
Roshan Pius3c868522016-10-27 12:43:49 -07001013}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001014
1015std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1016WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001017 legacy_hal::wifi_error legacy_status;
1018 std::vector<legacy_hal::wifi_ring_buffer_status>
1019 legacy_ring_buffer_status_vec;
1020 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001021 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001022 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1023 return {createWifiStatusFromLegacyError(legacy_status), {}};
1024 }
1025 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1026 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1027 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1028 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1029 }
1030 return {createWifiStatus(WifiStatusCode::SUCCESS),
1031 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001032}
1033
1034WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001035 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1036 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1037 WifiStatus status = registerDebugRingBufferCallback();
1038 if (status.code != WifiStatusCode::SUCCESS) {
1039 return status;
1040 }
1041 legacy_hal::wifi_error legacy_status =
1042 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001043 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001044 static_cast<
1045 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1046 verbose_level),
1047 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001048 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1049 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusabcf78f2017-10-06 16:30:38 -07001050 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001051}
1052
1053WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001054 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001055 WifiStatus status = registerDebugRingBufferCallback();
1056 if (status.code != WifiStatusCode::SUCCESS) {
1057 return status;
1058 }
1059 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001060 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1061 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001062
Roshan Piusabcf78f2017-10-06 16:30:38 -07001063 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001064}
1065
Roger Wangb294c762018-11-02 15:34:39 +08001066WifiStatus WifiChip::flushRingBufferToFileInternal() {
1067 if (!writeRingbufferFilesInternal()) {
1068 LOG(ERROR) << "Error writing files to flash";
1069 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1070 }
1071 return createWifiStatus(WifiStatusCode::SUCCESS);
1072}
1073
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001074WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001075 legacy_hal::wifi_error legacy_status =
1076 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001077 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001078 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001079}
1080
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001081std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1082WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001083 legacy_hal::wifi_error legacy_status;
1084 legacy_hal::WakeReasonStats legacy_stats;
1085 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001086 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001087 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1088 return {createWifiStatusFromLegacyError(legacy_status), {}};
1089 }
1090 WifiDebugHostWakeReasonStats hidl_stats;
1091 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1092 &hidl_stats)) {
1093 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1094 }
1095 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001096}
1097
Roshan Pius203cb032016-12-14 17:41:20 -08001098WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001099 legacy_hal::wifi_error legacy_status;
1100 if (enable) {
1101 android::wp<WifiChip> weak_ptr_this(this);
1102 const auto& on_alert_callback = [weak_ptr_this](
1103 int32_t error_code,
1104 std::vector<uint8_t> debug_data) {
1105 const auto shared_ptr_this = weak_ptr_this.promote();
1106 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1107 LOG(ERROR) << "Callback invoked on an invalid object";
1108 return;
1109 }
1110 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1111 if (!callback->onDebugErrorAlert(error_code, debug_data)
1112 .isOk()) {
1113 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1114 }
1115 }
1116 };
1117 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001118 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001119 } else {
1120 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001121 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001122 }
1123 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001124}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001125
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001126WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001127 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001128 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001129 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001130 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1131 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001132}
1133
Roshan Pius735ff432017-07-25 08:48:08 -07001134WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001135 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001136 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001137 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001138}
1139
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001140WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1141 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001142 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001143 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1144 return createWifiStatusFromLegacyError(legacy_status);
1145}
1146
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001147WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001148 const sp<V1_2::IWifiChipEventCallback>& event_callback) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001149 if (!event_cb_handler_.addCallback(event_callback)) {
1150 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1151 }
1152 return createWifiStatus(WifiStatusCode::SUCCESS);
1153}
1154
Jong Wook Kimda830c92018-07-23 15:29:38 -07001155WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1156 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001157 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001158 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001159 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1160 return createWifiStatusFromLegacyError(legacy_status);
1161}
1162
Roshan Piusba38d9c2017-12-08 07:32:08 -08001163WifiStatus WifiChip::handleChipConfiguration(
1164 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1165 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001166 // If the chip is already configured in a different mode, stop
1167 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001168 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001169 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1170 << " to mode " << mode_id;
1171 invalidateAndRemoveAllIfaces();
1172 legacy_hal::wifi_error legacy_status =
1173 legacy_hal_.lock()->stop(lock, []() {});
1174 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1175 LOG(ERROR) << "Failed to stop legacy HAL: "
1176 << legacyErrorToString(legacy_status);
1177 return createWifiStatusFromLegacyError(legacy_status);
1178 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001179 }
Roshan Piuscc338202017-11-02 13:54:09 -07001180 // Firmware mode change not needed for V2 devices.
1181 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001182 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001183 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001184 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001185 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1186 }
1187 if (!success) {
1188 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1189 }
1190 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1191 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1192 LOG(ERROR) << "Failed to start legacy HAL: "
1193 << legacyErrorToString(legacy_status);
1194 return createWifiStatusFromLegacyError(legacy_status);
1195 }
Roshan Pius85c64412018-01-22 17:58:40 -08001196 // Every time the HAL is restarted, we need to register the
1197 // radio mode change callback.
1198 WifiStatus status = registerRadioModeChangeCallback();
1199 if (status.code != WifiStatusCode::SUCCESS) {
1200 // This probably is not a critical failure?
1201 LOG(ERROR) << "Failed to register radio mode change callback";
1202 }
chenpaulf5eca292019-03-14 11:08:03 +08001203 // Extract and save the version information into property.
1204 std::pair<WifiStatus, IWifiChip::ChipDebugInfo> version_info;
1205 version_info = WifiChip::requestChipDebugInfoInternal();
1206 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1207 property_set("vendor.wlan.firmware.version",
1208 version_info.second.firmwareDescription.c_str());
1209 property_set("vendor.wlan.driver.version",
1210 version_info.second.driverDescription.c_str());
1211 }
1212
Roshan Piusabcf78f2017-10-06 16:30:38 -07001213 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001214}
Roshan Pius48185b22016-12-15 19:10:30 -08001215
1216WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001217 if (debug_ring_buffer_cb_registered_) {
1218 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001219 }
Roshan Pius3797e182017-03-30 18:01:54 -07001220
Roshan Piusabcf78f2017-10-06 16:30:38 -07001221 android::wp<WifiChip> weak_ptr_this(this);
1222 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001223 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001224 const std::vector<uint8_t>& data,
1225 const legacy_hal::wifi_ring_buffer_status& status) {
1226 const auto shared_ptr_this = weak_ptr_this.promote();
1227 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1228 LOG(ERROR) << "Callback invoked on an invalid object";
1229 return;
1230 }
1231 WifiDebugRingBufferStatus hidl_status;
1232 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1233 status, &hidl_status)) {
1234 LOG(ERROR) << "Error converting ring buffer status";
1235 return;
1236 }
xshu5899e8e2018-01-09 15:36:03 -08001237 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1238 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1239 Ringbuffer& cur_buffer = target->second;
1240 cur_buffer.append(data);
1241 } else {
1242 LOG(ERROR) << "Ringname " << name << " not found";
1243 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001244 }
1245 };
1246 legacy_hal::wifi_error legacy_status =
1247 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001248 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001249
Roshan Piusabcf78f2017-10-06 16:30:38 -07001250 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1251 debug_ring_buffer_cb_registered_ = true;
1252 }
1253 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001254}
1255
Roshan Pius85c64412018-01-22 17:58:40 -08001256WifiStatus WifiChip::registerRadioModeChangeCallback() {
1257 android::wp<WifiChip> weak_ptr_this(this);
1258 const auto& on_radio_mode_change_callback =
1259 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1260 const auto shared_ptr_this = weak_ptr_this.promote();
1261 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1262 LOG(ERROR) << "Callback invoked on an invalid object";
1263 return;
1264 }
Jong Wook Kimda830c92018-07-23 15:29:38 -07001265 std::vector<V1_2::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001266 hidl_radio_mode_infos;
1267 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1268 mac_infos, &hidl_radio_mode_infos)) {
1269 LOG(ERROR) << "Error converting wifi mac info";
1270 return;
1271 }
1272 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1273 if (!callback->onRadioModeChange(hidl_radio_mode_infos)
1274 .isOk()) {
1275 LOG(ERROR) << "Failed to invoke onRadioModeChange"
1276 << " callback on: " << toString(callback);
1277 }
1278 }
1279 };
1280 legacy_hal::wifi_error legacy_status =
1281 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001282 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001283 return createWifiStatusFromLegacyError(legacy_status);
1284}
1285
Roshan Piuscc338202017-11-02 13:54:09 -07001286std::vector<IWifiChip::ChipIfaceCombination>
1287WifiChip::getCurrentModeIfaceCombinations() {
1288 if (!isValidModeId(current_mode_id_)) {
1289 LOG(ERROR) << "Chip not configured in a mode yet";
1290 return {};
1291 }
1292 for (const auto& mode : modes_) {
1293 if (mode.id == current_mode_id_) {
1294 return mode.availableCombinations;
1295 }
1296 }
1297 CHECK(0) << "Expected to find iface combinations for current mode!";
1298 return {};
1299}
1300
1301// Returns a map indexed by IfaceType with the number of ifaces currently
1302// created of the corresponding type.
1303std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1304 std::map<IfaceType, size_t> iface_counts;
1305 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1306 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1307 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1308 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1309 return iface_counts;
1310}
1311
1312// This expands the provided iface combinations to a more parseable
1313// form. Returns a vector of available combinations possible with the number
1314// of ifaces of each type in the combination.
1315// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1316std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1317 const IWifiChip::ChipIfaceCombination& combination) {
1318 uint32_t num_expanded_combos = 1;
1319 for (const auto& limit : combination.limits) {
1320 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1321 num_expanded_combos *= limit.types.size();
1322 }
1323 }
1324
1325 // Allocate the vector of expanded combos and reset all iface counts to 0
1326 // in each combo.
1327 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1328 expanded_combos.resize(num_expanded_combos);
1329 for (auto& expanded_combo : expanded_combos) {
1330 for (const auto type :
1331 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1332 expanded_combo[type] = 0;
1333 }
1334 }
1335 uint32_t span = num_expanded_combos;
1336 for (const auto& limit : combination.limits) {
1337 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1338 span /= limit.types.size();
1339 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1340 const auto iface_type =
1341 limit.types[(k / span) % limit.types.size()];
1342 expanded_combos[k][iface_type]++;
1343 }
1344 }
1345 }
1346 return expanded_combos;
1347}
1348
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001349bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1350 const std::map<IfaceType, size_t>& expanded_combo,
1351 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001352 const auto current_combo = getCurrentIfaceCombination();
1353
1354 // Check if we have space for 1 more iface of |type| in this combo
1355 for (const auto type :
1356 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1357 size_t num_ifaces_needed = current_combo.at(type);
1358 if (type == requested_type) {
1359 num_ifaces_needed++;
1360 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001361 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001362 if (num_ifaces_needed > num_ifaces_allowed) {
1363 return false;
1364 }
1365 }
1366 return true;
1367}
1368
1369// This method does the following:
1370// a) Enumerate all possible iface combos by expanding the current
1371// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001372// b) Check if the requested iface type can be added to the current mode
1373// with the iface combination that is already active.
1374bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1375 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001376 if (!isValidModeId(current_mode_id_)) {
1377 LOG(ERROR) << "Chip not configured in a mode yet";
1378 return false;
1379 }
1380 const auto combinations = getCurrentModeIfaceCombinations();
1381 for (const auto& combination : combinations) {
1382 const auto expanded_combos = expandIfaceCombinations(combination);
1383 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001384 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1385 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001386 return true;
1387 }
1388 }
1389 }
1390 return false;
1391}
1392
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001393// Note: This does not consider ifaces already active. It only checks if the
1394// provided expanded iface combination can support the requested combo.
1395bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1396 const std::map<IfaceType, size_t>& expanded_combo,
1397 const std::map<IfaceType, size_t>& req_combo) {
1398 // Check if we have space for 1 more iface of |type| in this combo
1399 for (const auto type :
1400 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1401 if (req_combo.count(type) == 0) {
1402 // Iface of "type" not in the req_combo.
1403 continue;
1404 }
1405 size_t num_ifaces_needed = req_combo.at(type);
1406 size_t num_ifaces_allowed = expanded_combo.at(type);
1407 if (num_ifaces_needed > num_ifaces_allowed) {
1408 return false;
1409 }
1410 }
1411 return true;
1412}
1413// This method does the following:
1414// a) Enumerate all possible iface combos by expanding the current
1415// ChipIfaceCombination.
1416// b) Check if the requested iface combo can be added to the current mode.
1417// Note: This does not consider ifaces already active. It only checks if the
1418// current mode can support the requested combo.
1419bool WifiChip::canCurrentModeSupportIfaceCombo(
1420 const std::map<IfaceType, size_t>& req_combo) {
1421 if (!isValidModeId(current_mode_id_)) {
1422 LOG(ERROR) << "Chip not configured in a mode yet";
1423 return false;
1424 }
1425 const auto combinations = getCurrentModeIfaceCombinations();
1426 for (const auto& combination : combinations) {
1427 const auto expanded_combos = expandIfaceCombinations(combination);
1428 for (const auto& expanded_combo : expanded_combos) {
1429 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1430 req_combo)) {
1431 return true;
1432 }
1433 }
1434 }
1435 return false;
1436}
1437
1438// This method does the following:
1439// a) Enumerate all possible iface combos by expanding the current
1440// ChipIfaceCombination.
1441// b) Check if the requested iface type can be added to the current mode.
1442bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1443 // Check if we can support atleast 1 iface of type.
1444 std::map<IfaceType, size_t> req_iface_combo;
1445 req_iface_combo[requested_type] = 1;
1446 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1447}
1448
Roshan Piuscc338202017-11-02 13:54:09 -07001449bool WifiChip::isValidModeId(ChipModeId mode_id) {
1450 for (const auto& mode : modes_) {
1451 if (mode.id == mode_id) {
1452 return true;
1453 }
1454 }
1455 return false;
1456}
1457
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001458bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1459 // Check if we can support atleast 1 STA & 1 AP concurrently.
1460 std::map<IfaceType, size_t> req_iface_combo;
1461 req_iface_combo[IfaceType::AP] = 1;
1462 req_iface_combo[IfaceType::STA] = 1;
1463 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1464}
1465
Roshan Pius6036c022019-03-27 10:41:58 -07001466std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001467 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1468 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001469 // This could happen if the chip call is made before any STA/AP
1470 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001471 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Roshan Pius6036c022019-03-27 10:41:58 -07001472 return getWlanIfaceName(0);
1473}
1474
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001475// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1476// not already in use.
1477// Note: This doesn't check the actual presence of these interfaces.
1478std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1479 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1480 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001481 if (findUsingName(ap_ifaces_, ifname)) continue;
1482 if (findUsingName(sta_ifaces_, ifname)) continue;
1483 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001484 }
1485 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001486 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001487 return {};
1488}
1489
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001490// AP iface names start with idx 1 for modes supporting
1491// concurrent STA, else start with idx 0.
1492std::string WifiChip::allocateApIfaceName() {
1493 return allocateApOrStaIfaceName(
1494 isStaApConcurrencyAllowedInCurrentMode() ? 1 : 0);
1495}
1496
1497// STA iface names start with idx 0.
1498// Primary STA iface will always be 0.
1499std::string WifiChip::allocateStaIfaceName() {
1500 return allocateApOrStaIfaceName(0);
1501}
1502
xshu5899e8e2018-01-09 15:36:03 -08001503bool WifiChip::writeRingbufferFilesInternal() {
1504 if (!removeOldFilesInternal()) {
1505 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1506 return false;
1507 }
1508 // write ringbuffers to file
1509 for (const auto& item : ringbuffer_map_) {
1510 const Ringbuffer& cur_buffer = item.second;
1511 if (cur_buffer.getData().empty()) {
1512 continue;
1513 }
1514 const std::string file_path_raw =
1515 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1516 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1517 if (dump_fd == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001518 PLOG(ERROR) << "create file failed";
xshu5899e8e2018-01-09 15:36:03 -08001519 return false;
1520 }
1521 unique_fd file_auto_closer(dump_fd);
1522 for (const auto& cur_block : cur_buffer.getData()) {
1523 if (write(dump_fd, cur_block.data(),
1524 sizeof(cur_block[0]) * cur_block.size()) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001525 PLOG(ERROR) << "Error writing to file";
xshu5899e8e2018-01-09 15:36:03 -08001526 }
1527 }
1528 }
1529 return true;
1530}
1531
Roshan Pius79a99752016-10-04 13:03:58 -07001532} // namespace implementation
Jong Wook Kimda830c92018-07-23 15:29:38 -07001533} // namespace V1_3
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001534} // namespace wifi
1535} // namespace hardware
1536} // namespace android