blob: 25eb289838b627393c76384439ab68036c9bfd26 [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 Pius3c868522016-10-27 12:43:49 -0700637std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700638 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700639}
640
641WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800642 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
643 // Deprecated support for this callback.
644 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700645}
646
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700647std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700648 // Deprecated support for this callback.
649 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
650}
651
652std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700653 legacy_hal::wifi_error legacy_status;
654 uint32_t legacy_feature_set;
655 uint32_t legacy_logger_feature_set;
Roshan Pius6036c022019-03-27 10:41:58 -0700656 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700657 std::tie(legacy_status, legacy_feature_set) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800658 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700659 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
660 return {createWifiStatusFromLegacyError(legacy_status), 0};
661 }
662 std::tie(legacy_status, legacy_logger_feature_set) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800663 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700664 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius876220f2017-12-28 16:19:06 -0800665 // some devices don't support querying logger feature set
666 legacy_logger_feature_set = 0;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700667 }
668 uint32_t hidl_caps;
669 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
670 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
671 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
672 }
673 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700674}
675
Roshan Pius3c868522016-10-27 12:43:49 -0700676std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
677WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700678 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700679}
680
Roshan Piusba38d9c2017-12-08 07:32:08 -0800681WifiStatus WifiChip::configureChipInternal(
682 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
683 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700684 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700685 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
686 }
687 if (mode_id == current_mode_id_) {
688 LOG(DEBUG) << "Already in the specified mode " << mode_id;
689 return createWifiStatus(WifiStatusCode::SUCCESS);
690 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800691 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700692 if (status.code != WifiStatusCode::SUCCESS) {
693 for (const auto& callback : event_cb_handler_.getCallbacks()) {
694 if (!callback->onChipReconfigureFailure(status).isOk()) {
695 LOG(ERROR)
696 << "Failed to invoke onChipReconfigureFailure callback";
697 }
698 }
699 return status;
700 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800701 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700702 if (!callback->onChipReconfigured(mode_id).isOk()) {
703 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
704 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800705 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700706 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800707 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius8574e7f2019-04-01 13:30:40 -0700708 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800709 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700710}
711
712std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700713 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700714 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
715 current_mode_id_};
716 }
717 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700718}
719
720std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
721WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700722 IWifiChip::ChipDebugInfo result;
723 legacy_hal::wifi_error legacy_status;
724 std::string driver_desc;
Roshan Pius6036c022019-03-27 10:41:58 -0700725 const auto ifname = getFirstActiveWlanIfaceName();
Roshan Piusabcf78f2017-10-06 16:30:38 -0700726 std::tie(legacy_status, driver_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800727 legacy_hal_.lock()->getDriverVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700728 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
729 LOG(ERROR) << "Failed to get driver version: "
730 << legacyErrorToString(legacy_status);
731 WifiStatus status = createWifiStatusFromLegacyError(
732 legacy_status, "failed to get driver version");
733 return {status, result};
734 }
735 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700736
Roshan Piusabcf78f2017-10-06 16:30:38 -0700737 std::string firmware_desc;
738 std::tie(legacy_status, firmware_desc) =
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -0800739 legacy_hal_.lock()->getFirmwareVersion(ifname);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700740 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
741 LOG(ERROR) << "Failed to get firmware version: "
742 << legacyErrorToString(legacy_status);
743 WifiStatus status = createWifiStatusFromLegacyError(
744 legacy_status, "failed to get firmware version");
745 return {status, result};
746 }
747 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700748
Roshan Piusabcf78f2017-10-06 16:30:38 -0700749 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700750}
751
752std::pair<WifiStatus, std::vector<uint8_t>>
753WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700754 legacy_hal::wifi_error legacy_status;
755 std::vector<uint8_t> driver_dump;
756 std::tie(legacy_status, driver_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700757 legacy_hal_.lock()->requestDriverMemoryDump(
758 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700759 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
760 LOG(ERROR) << "Failed to get driver debug dump: "
761 << legacyErrorToString(legacy_status);
762 return {createWifiStatusFromLegacyError(legacy_status),
763 std::vector<uint8_t>()};
764 }
765 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700766}
767
768std::pair<WifiStatus, std::vector<uint8_t>>
769WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700770 legacy_hal::wifi_error legacy_status;
771 std::vector<uint8_t> firmware_dump;
772 std::tie(legacy_status, firmware_dump) =
Roshan Pius6036c022019-03-27 10:41:58 -0700773 legacy_hal_.lock()->requestFirmwareMemoryDump(
774 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700775 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
776 LOG(ERROR) << "Failed to get firmware debug dump: "
777 << legacyErrorToString(legacy_status);
778 return {createWifiStatusFromLegacyError(legacy_status), {}};
779 }
780 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700781}
782
783std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700784 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700785 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800786 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700787 std::string ifname = allocateApIfaceName();
Roshan Pius3b6705f2019-02-14 09:43:43 -0800788 sp<WifiApIface> iface =
789 new WifiApIface(ifname, legacy_hal_, iface_util_, feature_flags_);
Roshan Pius675609b2017-10-31 14:24:58 -0700790 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700791 for (const auto& callback : event_cb_handler_.getCallbacks()) {
792 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
793 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
794 }
795 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700796 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700797 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700798}
799
800std::pair<WifiStatus, std::vector<hidl_string>>
801WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700802 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700803 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
804 }
Roshan Pius675609b2017-10-31 14:24:58 -0700805 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700806}
807
808std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800809 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700810 const auto iface = findUsingName(ap_ifaces_, ifname);
811 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700812 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
813 }
Roshan Pius675609b2017-10-31 14:24:58 -0700814 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700815}
816
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800817WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700818 const auto iface = findUsingName(ap_ifaces_, ifname);
819 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700820 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800821 }
Roshan Pius675609b2017-10-31 14:24:58 -0700822 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700823 for (const auto& callback : event_cb_handler_.getCallbacks()) {
824 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
825 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
826 }
827 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700828 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700829 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800830}
831
Roshan Pius3c868522016-10-27 12:43:49 -0700832std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700833 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700834 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800835 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700836 // These are still assumed to be based on wlan0.
Roshan Pius6036c022019-03-27 10:41:58 -0700837 std::string ifname = getFirstActiveWlanIfaceName();
Roshan Piuscc338202017-11-02 13:54:09 -0700838 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_);
839 nan_ifaces_.push_back(iface);
840 for (const auto& callback : event_cb_handler_.getCallbacks()) {
841 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
842 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
843 }
844 }
845 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700846}
847
848std::pair<WifiStatus, std::vector<hidl_string>>
849WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700850 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700851 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
852 }
Roshan Pius675609b2017-10-31 14:24:58 -0700853 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700854}
855
856std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800857 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700858 const auto iface = findUsingName(nan_ifaces_, ifname);
859 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700860 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
861 }
Roshan Pius675609b2017-10-31 14:24:58 -0700862 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700863}
864
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800865WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700866 const auto iface = findUsingName(nan_ifaces_, ifname);
867 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700868 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800869 }
Roshan Pius675609b2017-10-31 14:24:58 -0700870 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700871 for (const auto& callback : event_cb_handler_.getCallbacks()) {
872 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
873 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
874 }
875 }
876 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800877}
878
Roshan Pius3c868522016-10-27 12:43:49 -0700879std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700880 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700881 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800882 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700883 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700884 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
885 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700886 for (const auto& callback : event_cb_handler_.getCallbacks()) {
887 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
888 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
889 }
890 }
Roshan Pius675609b2017-10-31 14:24:58 -0700891 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700892}
893
894std::pair<WifiStatus, std::vector<hidl_string>>
895WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700896 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700897 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
898 }
Roshan Pius675609b2017-10-31 14:24:58 -0700899 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700900}
901
902std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800903 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700904 const auto iface = findUsingName(p2p_ifaces_, ifname);
905 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700906 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
907 }
Roshan Pius675609b2017-10-31 14:24:58 -0700908 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700909}
910
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800911WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700912 const auto iface = findUsingName(p2p_ifaces_, ifname);
913 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700914 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800915 }
Roshan Pius675609b2017-10-31 14:24:58 -0700916 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700917 for (const auto& callback : event_cb_handler_.getCallbacks()) {
918 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
919 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
920 }
921 }
922 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800923}
924
Roshan Pius3c868522016-10-27 12:43:49 -0700925std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700926 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700927 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800928 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700929 std::string ifname = allocateStaIfaceName();
Roshan Pius99dab382019-02-14 07:57:10 -0800930 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700931 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700932 for (const auto& callback : event_cb_handler_.getCallbacks()) {
933 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
934 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
935 }
936 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700937 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700938 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700939}
940
941std::pair<WifiStatus, std::vector<hidl_string>>
942WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700943 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700944 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
945 }
Roshan Pius675609b2017-10-31 14:24:58 -0700946 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700947}
948
949std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800950 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700951 const auto iface = findUsingName(sta_ifaces_, ifname);
952 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700953 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
954 }
Roshan Pius675609b2017-10-31 14:24:58 -0700955 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700956}
957
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800958WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700959 const auto iface = findUsingName(sta_ifaces_, ifname);
960 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700961 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800962 }
Roshan Pius675609b2017-10-31 14:24:58 -0700963 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700964 for (const auto& callback : event_cb_handler_.getCallbacks()) {
965 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
966 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
967 }
968 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700969 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700970 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800971}
972
Roshan Pius3c868522016-10-27 12:43:49 -0700973std::pair<WifiStatus, sp<IWifiRttController>>
974WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
Etan Cohen7240a1a2018-08-29 08:14:09 -0700975 if (sta_ifaces_.size() == 0 &&
976 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
977 LOG(ERROR) << "createRttControllerInternal: Chip cannot support STAs "
978 "(and RTT by extension)";
979 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
980 }
Roshan Pius6036c022019-03-27 10:41:58 -0700981 sp<WifiRttController> rtt = new WifiRttController(
982 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700983 rtt_controllers_.emplace_back(rtt);
984 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
Roshan Pius3c868522016-10-27 12:43:49 -0700985}
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700986
987std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
988WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700989 legacy_hal::wifi_error legacy_status;
990 std::vector<legacy_hal::wifi_ring_buffer_status>
991 legacy_ring_buffer_status_vec;
992 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -0700993 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700994 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
995 return {createWifiStatusFromLegacyError(legacy_status), {}};
996 }
997 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
998 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
999 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1000 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1001 }
1002 return {createWifiStatus(WifiStatusCode::SUCCESS),
1003 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001004}
1005
1006WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001007 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1008 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1009 WifiStatus status = registerDebugRingBufferCallback();
1010 if (status.code != WifiStatusCode::SUCCESS) {
1011 return status;
1012 }
1013 legacy_hal::wifi_error legacy_status =
1014 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001015 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001016 static_cast<
1017 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1018 verbose_level),
1019 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001020 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1021 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusabcf78f2017-10-06 16:30:38 -07001022 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001023}
1024
1025WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001026 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001027 WifiStatus status = registerDebugRingBufferCallback();
1028 if (status.code != WifiStatusCode::SUCCESS) {
1029 return status;
1030 }
1031 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001032 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1033 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001034
Roshan Piusabcf78f2017-10-06 16:30:38 -07001035 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001036}
1037
Roger Wangb294c762018-11-02 15:34:39 +08001038WifiStatus WifiChip::flushRingBufferToFileInternal() {
1039 if (!writeRingbufferFilesInternal()) {
1040 LOG(ERROR) << "Error writing files to flash";
1041 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1042 }
1043 return createWifiStatus(WifiStatusCode::SUCCESS);
1044}
1045
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001046WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001047 legacy_hal::wifi_error legacy_status =
1048 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001049 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001050 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001051}
1052
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001053std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1054WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001055 legacy_hal::wifi_error legacy_status;
1056 legacy_hal::WakeReasonStats legacy_stats;
1057 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001058 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001059 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1060 return {createWifiStatusFromLegacyError(legacy_status), {}};
1061 }
1062 WifiDebugHostWakeReasonStats hidl_stats;
1063 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1064 &hidl_stats)) {
1065 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1066 }
1067 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001068}
1069
Roshan Pius203cb032016-12-14 17:41:20 -08001070WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001071 legacy_hal::wifi_error legacy_status;
1072 if (enable) {
1073 android::wp<WifiChip> weak_ptr_this(this);
1074 const auto& on_alert_callback = [weak_ptr_this](
1075 int32_t error_code,
1076 std::vector<uint8_t> debug_data) {
1077 const auto shared_ptr_this = weak_ptr_this.promote();
1078 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1079 LOG(ERROR) << "Callback invoked on an invalid object";
1080 return;
1081 }
1082 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1083 if (!callback->onDebugErrorAlert(error_code, debug_data)
1084 .isOk()) {
1085 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1086 }
1087 }
1088 };
1089 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001090 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001091 } else {
1092 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001093 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001094 }
1095 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001096}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001097
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001098WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001099 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001100 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001101 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001102 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1103 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001104}
1105
Roshan Pius735ff432017-07-25 08:48:08 -07001106WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001107 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001108 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001109 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001110}
1111
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001112WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1113 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001114 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001115 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1116 return createWifiStatusFromLegacyError(legacy_status);
1117}
1118
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001119WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001120 const sp<V1_2::IWifiChipEventCallback>& event_callback) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001121 if (!event_cb_handler_.addCallback(event_callback)) {
1122 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1123 }
1124 return createWifiStatus(WifiStatusCode::SUCCESS);
1125}
1126
Jong Wook Kimda830c92018-07-23 15:29:38 -07001127WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1128 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001129 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001130 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001131 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1132 return createWifiStatusFromLegacyError(legacy_status);
1133}
1134
Roshan Piusba38d9c2017-12-08 07:32:08 -08001135WifiStatus WifiChip::handleChipConfiguration(
1136 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1137 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001138 // If the chip is already configured in a different mode, stop
1139 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001140 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001141 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1142 << " to mode " << mode_id;
1143 invalidateAndRemoveAllIfaces();
1144 legacy_hal::wifi_error legacy_status =
1145 legacy_hal_.lock()->stop(lock, []() {});
1146 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1147 LOG(ERROR) << "Failed to stop legacy HAL: "
1148 << legacyErrorToString(legacy_status);
1149 return createWifiStatusFromLegacyError(legacy_status);
1150 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001151 }
Roshan Piuscc338202017-11-02 13:54:09 -07001152 // Firmware mode change not needed for V2 devices.
1153 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001154 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001155 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001156 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001157 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1158 }
1159 if (!success) {
1160 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1161 }
1162 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1163 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1164 LOG(ERROR) << "Failed to start legacy HAL: "
1165 << legacyErrorToString(legacy_status);
1166 return createWifiStatusFromLegacyError(legacy_status);
1167 }
Roshan Pius85c64412018-01-22 17:58:40 -08001168 // Every time the HAL is restarted, we need to register the
1169 // radio mode change callback.
1170 WifiStatus status = registerRadioModeChangeCallback();
1171 if (status.code != WifiStatusCode::SUCCESS) {
1172 // This probably is not a critical failure?
1173 LOG(ERROR) << "Failed to register radio mode change callback";
1174 }
chenpaulf5eca292019-03-14 11:08:03 +08001175 // Extract and save the version information into property.
1176 std::pair<WifiStatus, IWifiChip::ChipDebugInfo> version_info;
1177 version_info = WifiChip::requestChipDebugInfoInternal();
1178 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1179 property_set("vendor.wlan.firmware.version",
1180 version_info.second.firmwareDescription.c_str());
1181 property_set("vendor.wlan.driver.version",
1182 version_info.second.driverDescription.c_str());
1183 }
1184
Roshan Piusabcf78f2017-10-06 16:30:38 -07001185 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001186}
Roshan Pius48185b22016-12-15 19:10:30 -08001187
1188WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001189 if (debug_ring_buffer_cb_registered_) {
1190 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001191 }
Roshan Pius3797e182017-03-30 18:01:54 -07001192
Roshan Piusabcf78f2017-10-06 16:30:38 -07001193 android::wp<WifiChip> weak_ptr_this(this);
1194 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001195 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001196 const std::vector<uint8_t>& data,
1197 const legacy_hal::wifi_ring_buffer_status& status) {
1198 const auto shared_ptr_this = weak_ptr_this.promote();
1199 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1200 LOG(ERROR) << "Callback invoked on an invalid object";
1201 return;
1202 }
1203 WifiDebugRingBufferStatus hidl_status;
1204 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1205 status, &hidl_status)) {
1206 LOG(ERROR) << "Error converting ring buffer status";
1207 return;
1208 }
xshu5899e8e2018-01-09 15:36:03 -08001209 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1210 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1211 Ringbuffer& cur_buffer = target->second;
1212 cur_buffer.append(data);
1213 } else {
1214 LOG(ERROR) << "Ringname " << name << " not found";
1215 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001216 }
1217 };
1218 legacy_hal::wifi_error legacy_status =
1219 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001220 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001221
Roshan Piusabcf78f2017-10-06 16:30:38 -07001222 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1223 debug_ring_buffer_cb_registered_ = true;
1224 }
1225 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001226}
1227
Roshan Pius85c64412018-01-22 17:58:40 -08001228WifiStatus WifiChip::registerRadioModeChangeCallback() {
1229 android::wp<WifiChip> weak_ptr_this(this);
1230 const auto& on_radio_mode_change_callback =
1231 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1232 const auto shared_ptr_this = weak_ptr_this.promote();
1233 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1234 LOG(ERROR) << "Callback invoked on an invalid object";
1235 return;
1236 }
Jong Wook Kimda830c92018-07-23 15:29:38 -07001237 std::vector<V1_2::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001238 hidl_radio_mode_infos;
1239 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1240 mac_infos, &hidl_radio_mode_infos)) {
1241 LOG(ERROR) << "Error converting wifi mac info";
1242 return;
1243 }
1244 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1245 if (!callback->onRadioModeChange(hidl_radio_mode_infos)
1246 .isOk()) {
1247 LOG(ERROR) << "Failed to invoke onRadioModeChange"
1248 << " callback on: " << toString(callback);
1249 }
1250 }
1251 };
1252 legacy_hal::wifi_error legacy_status =
1253 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001254 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001255 return createWifiStatusFromLegacyError(legacy_status);
1256}
1257
Roshan Piuscc338202017-11-02 13:54:09 -07001258std::vector<IWifiChip::ChipIfaceCombination>
1259WifiChip::getCurrentModeIfaceCombinations() {
1260 if (!isValidModeId(current_mode_id_)) {
1261 LOG(ERROR) << "Chip not configured in a mode yet";
1262 return {};
1263 }
1264 for (const auto& mode : modes_) {
1265 if (mode.id == current_mode_id_) {
1266 return mode.availableCombinations;
1267 }
1268 }
1269 CHECK(0) << "Expected to find iface combinations for current mode!";
1270 return {};
1271}
1272
1273// Returns a map indexed by IfaceType with the number of ifaces currently
1274// created of the corresponding type.
1275std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1276 std::map<IfaceType, size_t> iface_counts;
1277 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1278 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1279 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1280 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1281 return iface_counts;
1282}
1283
1284// This expands the provided iface combinations to a more parseable
1285// form. Returns a vector of available combinations possible with the number
1286// of ifaces of each type in the combination.
1287// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1288std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1289 const IWifiChip::ChipIfaceCombination& combination) {
1290 uint32_t num_expanded_combos = 1;
1291 for (const auto& limit : combination.limits) {
1292 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1293 num_expanded_combos *= limit.types.size();
1294 }
1295 }
1296
1297 // Allocate the vector of expanded combos and reset all iface counts to 0
1298 // in each combo.
1299 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1300 expanded_combos.resize(num_expanded_combos);
1301 for (auto& expanded_combo : expanded_combos) {
1302 for (const auto type :
1303 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1304 expanded_combo[type] = 0;
1305 }
1306 }
1307 uint32_t span = num_expanded_combos;
1308 for (const auto& limit : combination.limits) {
1309 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1310 span /= limit.types.size();
1311 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1312 const auto iface_type =
1313 limit.types[(k / span) % limit.types.size()];
1314 expanded_combos[k][iface_type]++;
1315 }
1316 }
1317 }
1318 return expanded_combos;
1319}
1320
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001321bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1322 const std::map<IfaceType, size_t>& expanded_combo,
1323 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001324 const auto current_combo = getCurrentIfaceCombination();
1325
1326 // Check if we have space for 1 more iface of |type| in this combo
1327 for (const auto type :
1328 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1329 size_t num_ifaces_needed = current_combo.at(type);
1330 if (type == requested_type) {
1331 num_ifaces_needed++;
1332 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001333 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001334 if (num_ifaces_needed > num_ifaces_allowed) {
1335 return false;
1336 }
1337 }
1338 return true;
1339}
1340
1341// This method does the following:
1342// a) Enumerate all possible iface combos by expanding the current
1343// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001344// b) Check if the requested iface type can be added to the current mode
1345// with the iface combination that is already active.
1346bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1347 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001348 if (!isValidModeId(current_mode_id_)) {
1349 LOG(ERROR) << "Chip not configured in a mode yet";
1350 return false;
1351 }
1352 const auto combinations = getCurrentModeIfaceCombinations();
1353 for (const auto& combination : combinations) {
1354 const auto expanded_combos = expandIfaceCombinations(combination);
1355 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001356 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1357 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001358 return true;
1359 }
1360 }
1361 }
1362 return false;
1363}
1364
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001365// Note: This does not consider ifaces already active. It only checks if the
1366// provided expanded iface combination can support the requested combo.
1367bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1368 const std::map<IfaceType, size_t>& expanded_combo,
1369 const std::map<IfaceType, size_t>& req_combo) {
1370 // Check if we have space for 1 more iface of |type| in this combo
1371 for (const auto type :
1372 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1373 if (req_combo.count(type) == 0) {
1374 // Iface of "type" not in the req_combo.
1375 continue;
1376 }
1377 size_t num_ifaces_needed = req_combo.at(type);
1378 size_t num_ifaces_allowed = expanded_combo.at(type);
1379 if (num_ifaces_needed > num_ifaces_allowed) {
1380 return false;
1381 }
1382 }
1383 return true;
1384}
1385// This method does the following:
1386// a) Enumerate all possible iface combos by expanding the current
1387// ChipIfaceCombination.
1388// b) Check if the requested iface combo can be added to the current mode.
1389// Note: This does not consider ifaces already active. It only checks if the
1390// current mode can support the requested combo.
1391bool WifiChip::canCurrentModeSupportIfaceCombo(
1392 const std::map<IfaceType, size_t>& req_combo) {
1393 if (!isValidModeId(current_mode_id_)) {
1394 LOG(ERROR) << "Chip not configured in a mode yet";
1395 return false;
1396 }
1397 const auto combinations = getCurrentModeIfaceCombinations();
1398 for (const auto& combination : combinations) {
1399 const auto expanded_combos = expandIfaceCombinations(combination);
1400 for (const auto& expanded_combo : expanded_combos) {
1401 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1402 req_combo)) {
1403 return true;
1404 }
1405 }
1406 }
1407 return false;
1408}
1409
1410// This method does the following:
1411// a) Enumerate all possible iface combos by expanding the current
1412// ChipIfaceCombination.
1413// b) Check if the requested iface type can be added to the current mode.
1414bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1415 // Check if we can support atleast 1 iface of type.
1416 std::map<IfaceType, size_t> req_iface_combo;
1417 req_iface_combo[requested_type] = 1;
1418 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1419}
1420
Roshan Piuscc338202017-11-02 13:54:09 -07001421bool WifiChip::isValidModeId(ChipModeId mode_id) {
1422 for (const auto& mode : modes_) {
1423 if (mode.id == mode_id) {
1424 return true;
1425 }
1426 }
1427 return false;
1428}
1429
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001430bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1431 // Check if we can support atleast 1 STA & 1 AP concurrently.
1432 std::map<IfaceType, size_t> req_iface_combo;
1433 req_iface_combo[IfaceType::AP] = 1;
1434 req_iface_combo[IfaceType::STA] = 1;
1435 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1436}
1437
Roshan Pius6036c022019-03-27 10:41:58 -07001438std::string WifiChip::getFirstActiveWlanIfaceName() {
1439 for (unsigned idx = 0; idx < kMaxWlanIfaces; idx++) {
1440 const auto ifname = getWlanIfaceName(idx);
1441 if (findUsingName(sta_ifaces_, ifname)) return ifname;
1442 if (findUsingName(ap_ifaces_, ifname)) return ifname;
1443 }
1444 // This could happen if the chip call is made before any STA/AP
1445 // iface is created. Default to wlan0 for such cases.
1446 LOG(WARNING) << "No active wlan interfaces in use!";
1447 return getWlanIfaceName(0);
1448}
1449
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001450// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1451// not already in use.
1452// Note: This doesn't check the actual presence of these interfaces.
1453std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1454 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1455 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001456 if (findUsingName(ap_ifaces_, ifname)) continue;
1457 if (findUsingName(sta_ifaces_, ifname)) continue;
1458 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001459 }
1460 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001461 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001462 return {};
1463}
1464
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001465// AP iface names start with idx 1 for modes supporting
1466// concurrent STA, else start with idx 0.
1467std::string WifiChip::allocateApIfaceName() {
1468 return allocateApOrStaIfaceName(
1469 isStaApConcurrencyAllowedInCurrentMode() ? 1 : 0);
1470}
1471
1472// STA iface names start with idx 0.
1473// Primary STA iface will always be 0.
1474std::string WifiChip::allocateStaIfaceName() {
1475 return allocateApOrStaIfaceName(0);
1476}
1477
xshu5899e8e2018-01-09 15:36:03 -08001478bool WifiChip::writeRingbufferFilesInternal() {
1479 if (!removeOldFilesInternal()) {
1480 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1481 return false;
1482 }
1483 // write ringbuffers to file
1484 for (const auto& item : ringbuffer_map_) {
1485 const Ringbuffer& cur_buffer = item.second;
1486 if (cur_buffer.getData().empty()) {
1487 continue;
1488 }
1489 const std::string file_path_raw =
1490 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1491 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1492 if (dump_fd == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001493 PLOG(ERROR) << "create file failed";
xshu5899e8e2018-01-09 15:36:03 -08001494 return false;
1495 }
1496 unique_fd file_auto_closer(dump_fd);
1497 for (const auto& cur_block : cur_buffer.getData()) {
1498 if (write(dump_fd, cur_block.data(),
1499 sizeof(cur_block[0]) * cur_block.size()) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -08001500 PLOG(ERROR) << "Error writing to file";
xshu5899e8e2018-01-09 15:36:03 -08001501 }
1502 }
1503 }
1504 return true;
1505}
1506
Roshan Pius79a99752016-10-04 13:03:58 -07001507} // namespace implementation
Jong Wook Kimda830c92018-07-23 15:29:38 -07001508} // namespace V1_3
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001509} // namespace wifi
1510} // namespace hardware
1511} // namespace android