blob: 23dd13be66c42f026ab4d24b837c83d02fa33dd0 [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 Pius5ba0a902020-04-14 11:55:42 -0700110// Returns the dedicated iface name if one is defined.
111std::string getNanIfaceName() {
112 std::array<char, PROPERTY_VALUE_MAX> buffer;
113 if (property_get("wifi.aware.interface", buffer.data(), nullptr) == 0) {
114 return {};
115 }
116 return buffer.data();
117}
118
Roshan Pius8574e7f2019-04-01 13:30:40 -0700119void setActiveWlanIfaceNameProperty(const std::string& ifname) {
120 auto res = property_set(kActiveWlanIfaceNameProperty, ifname.data());
121 if (res != 0) {
122 PLOG(ERROR) << "Failed to set active wlan iface name property";
123 }
124}
125
xshu37126c92018-04-13 16:24:45 -0700126// delete files that meet either conditions:
127// 1. older than a predefined time in the wifi tombstone dir.
128// 2. Files in excess to a predefined amount, starting from the oldest ones
xshu5899e8e2018-01-09 15:36:03 -0800129bool removeOldFilesInternal() {
130 time_t now = time(0);
131 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
Josh Gaoa568e532018-06-04 18:16:00 -0700132 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(
133 opendir(kTombstoneFolderPath), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800134 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800135 PLOG(ERROR) << "Failed to open directory";
xshu5899e8e2018-01-09 15:36:03 -0800136 return false;
137 }
xshu5899e8e2018-01-09 15:36:03 -0800138 struct dirent* dp;
139 bool success = true;
xshu37126c92018-04-13 16:24:45 -0700140 std::list<std::pair<const time_t, std::string>> valid_files;
Josh Gaoa568e532018-06-04 18:16:00 -0700141 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800142 if (dp->d_type != DT_REG) {
143 continue;
144 }
145 std::string cur_file_name(dp->d_name);
146 struct stat cur_file_stat;
147 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800148 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800149 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800150 success = false;
xshu4cb33162018-01-24 15:40:06 -0800151 continue;
152 }
xshu37126c92018-04-13 16:24:45 -0700153 const time_t cur_file_time = cur_file_stat.st_mtime;
154 valid_files.push_back(
155 std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
156 }
157 valid_files.sort(); // sort the list of files by last modified time from
158 // small to big.
159 uint32_t cur_file_count = valid_files.size();
160 for (auto cur_file : valid_files) {
161 if (cur_file_count > kMaxRingBufferFileNum ||
162 cur_file.first < delete_files_before) {
163 if (unlink(cur_file.second.c_str()) != 0) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800164 PLOG(ERROR) << "Error deleting file";
xshu37126c92018-04-13 16:24:45 -0700165 success = false;
166 }
167 cur_file_count--;
168 } else {
169 break;
xshu5899e8e2018-01-09 15:36:03 -0800170 }
171 }
172 return success;
173}
174
xshu4cb33162018-01-24 15:40:06 -0800175// Helper function for |cpioArchiveFilesInDir|
176bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name,
177 size_t file_name_len) {
178 std::array<char, 32 * 1024> read_buf;
179 ssize_t llen =
180 sprintf(read_buf.data(),
181 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
182 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
183 st.st_gid, static_cast<int>(st.st_nlink),
184 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
185 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
186 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
187 if (write(out_fd, read_buf.data(), llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800188 PLOG(ERROR) << "Error writing cpio header to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800189 return false;
190 }
191 if (write(out_fd, file_name, file_name_len) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800192 PLOG(ERROR) << "Error writing filename to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800193 return false;
194 }
195
196 // NUL Pad header up to 4 multiple bytes.
197 llen = (llen + file_name_len) % 4;
198 if (llen != 0) {
199 const uint32_t zero = 0;
200 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800201 PLOG(ERROR) << "Error padding 0s to file " << file_name;
xshu4cb33162018-01-24 15:40:06 -0800202 return false;
203 }
204 }
205 return true;
206}
207
208// Helper function for |cpioArchiveFilesInDir|
209size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
210 // writing content of file
211 std::array<char, 32 * 1024> read_buf;
212 ssize_t llen = st.st_size;
213 size_t n_error = 0;
214 while (llen > 0) {
215 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
216 if (bytes_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800217 PLOG(ERROR) << "Error reading file";
xshu4cb33162018-01-24 15:40:06 -0800218 return ++n_error;
219 }
220 llen -= bytes_read;
221 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800222 PLOG(ERROR) << "Error writing data to file";
xshu4cb33162018-01-24 15:40:06 -0800223 return ++n_error;
224 }
225 if (bytes_read == 0) { // this should never happen, but just in case
226 // to unstuck from while loop
Elliott Hughes4db4add2019-03-08 12:42:57 -0800227 PLOG(ERROR) << "Unexpected read result";
xshu4cb33162018-01-24 15:40:06 -0800228 n_error++;
229 break;
230 }
231 }
232 llen = st.st_size % 4;
233 if (llen != 0) {
234 const uint32_t zero = 0;
235 if (write(out_fd, &zero, 4 - llen) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800236 PLOG(ERROR) << "Error padding 0s to file";
xshu4cb33162018-01-24 15:40:06 -0800237 return ++n_error;
238 }
239 }
240 return n_error;
241}
242
243// Helper function for |cpioArchiveFilesInDir|
244bool cpioWriteFileTrailer(int out_fd) {
245 std::array<char, 4096> read_buf;
246 read_buf.fill(0);
247 if (write(out_fd, read_buf.data(),
248 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1,
249 0x0b, 0) +
250 4) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800251 PLOG(ERROR) << "Error writing trailing bytes";
xshu4cb33162018-01-24 15:40:06 -0800252 return false;
253 }
254 return true;
255}
256
xshu5899e8e2018-01-09 15:36:03 -0800257// Archives all files in |input_dir| and writes result into |out_fd|
258// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
259// portion
xshu4cb33162018-01-24 15:40:06 -0800260size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800261 struct dirent* dp;
262 size_t n_error = 0;
Josh Gaoa568e532018-06-04 18:16:00 -0700263 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir),
264 closedir);
xshu5899e8e2018-01-09 15:36:03 -0800265 if (!dir_dump) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800266 PLOG(ERROR) << "Failed to open directory";
xshu4cb33162018-01-24 15:40:06 -0800267 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800268 }
Josh Gaoa568e532018-06-04 18:16:00 -0700269 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800270 if (dp->d_type != DT_REG) {
271 continue;
272 }
273 std::string cur_file_name(dp->d_name);
xshu4cb33162018-01-24 15:40:06 -0800274 // string.size() does not include the null terminator. The cpio FreeBSD
275 // file header expects the null character to be included in the length.
276 const size_t file_name_len = cur_file_name.size() + 1;
xshu5899e8e2018-01-09 15:36:03 -0800277 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800278 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800279 if (stat(cur_file_path.c_str(), &st) == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800280 PLOG(ERROR) << "Failed to get file stat for " << cur_file_path;
xshu5899e8e2018-01-09 15:36:03 -0800281 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800282 continue;
283 }
284 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
285 if (fd_read == -1) {
Elliott Hughes4db4add2019-03-08 12:42:57 -0800286 PLOG(ERROR) << "Failed to open file " << cur_file_path;
xshu4cb33162018-01-24 15:40:06 -0800287 n_error++;
288 continue;
289 }
290 unique_fd file_auto_closer(fd_read);
291 if (!cpioWriteHeader(out_fd, st, cur_file_name.c_str(),
292 file_name_len)) {
293 return ++n_error;
294 }
295 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
296 if (write_error) {
297 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800298 }
299 }
xshu4cb33162018-01-24 15:40:06 -0800300 if (!cpioWriteFileTrailer(out_fd)) {
301 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800302 }
303 return n_error;
304}
305
306// Helper function to create a non-const char*.
307std::vector<char> makeCharVec(const std::string& str) {
308 std::vector<char> vec(str.size() + 1);
309 vec.assign(str.begin(), str.end());
310 vec.push_back('\0');
311 return vec;
312}
313
Roshan Piusabcf78f2017-10-06 16:30:38 -0700314} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700315
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700316namespace android {
317namespace hardware {
318namespace wifi {
Ahmed ElArabawyf501a982019-07-23 15:02:22 -0700319namespace V1_4 {
Roshan Pius79a99752016-10-04 13:03:58 -0700320namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700321using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800322using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700323
Roshan Pius52947fb2016-11-18 11:38:07 -0800324WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700325 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700326 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
Roshan Pius99dab382019-02-14 07:57:10 -0800327 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
Roshan Pius200a17d2017-11-01 13:03:35 -0700328 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Pius52947fb2016-11-18 11:38:07 -0800329 : chip_id_(chip_id),
330 legacy_hal_(legacy_hal),
331 mode_controller_(mode_controller),
Roshan Pius99dab382019-02-14 07:57:10 -0800332 iface_util_(iface_util),
Roshan Pius52947fb2016-11-18 11:38:07 -0800333 is_valid_(true),
Tomasz Wasilczykb424da72018-11-15 11:52:57 -0800334 current_mode_id_(feature_flags::chip_mode_ids::kInvalid),
335 modes_(feature_flags.lock()->getChipModes()),
Roshan Pius8574e7f2019-04-01 13:30:40 -0700336 debug_ring_buffer_cb_registered_(false) {
337 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
338}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700339
Roshan Piusaabe5752016-09-29 09:03:59 -0700340void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700341 if (!writeRingbufferFilesInternal()) {
342 LOG(ERROR) << "Error writing files to flash";
343 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700344 invalidateAndRemoveAllIfaces();
Roshan Pius8574e7f2019-04-01 13:30:40 -0700345 setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700346 legacy_hal_.reset();
347 event_cb_handler_.invalidate();
348 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700349}
350
Roshan Piusabcf78f2017-10-06 16:30:38 -0700351bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700352
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800353std::set<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700354 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800355}
356
Roshan Pius5c055462016-10-11 08:27:27 -0700357Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700358 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
359 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700360}
361
Jong Wook Kimda830c92018-07-23 15:29:38 -0700362// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700363Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800364 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700365 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700366 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
367 &WifiChip::registerEventCallbackInternal,
368 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700369}
370
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700371Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700372 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
373 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700374}
375
Roshan Pius5c055462016-10-11 08:27:27 -0700376Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700377 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
378 &WifiChip::getAvailableModesInternal,
379 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700380}
381
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800382Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700383 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800384 return validateAndCallWithLock(
385 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
386 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700387}
388
Roshan Pius5c055462016-10-11 08:27:27 -0700389Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700390 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
391 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700392}
393
Roshan Pius5c055462016-10-11 08:27:27 -0700394Return<void> WifiChip::requestChipDebugInfo(
395 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700396 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
397 &WifiChip::requestChipDebugInfoInternal,
398 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700399}
400
401Return<void> WifiChip::requestDriverDebugDump(
402 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700403 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
404 &WifiChip::requestDriverDebugDumpInternal,
405 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700406}
407
Roshan Pius5c055462016-10-11 08:27:27 -0700408Return<void> WifiChip::requestFirmwareDebugDump(
409 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700410 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
411 &WifiChip::requestFirmwareDebugDumpInternal,
412 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700413}
414
Roshan Pius5c055462016-10-11 08:27:27 -0700415Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700416 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
417 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700418}
419
Roshan Pius5c055462016-10-11 08:27:27 -0700420Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700421 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
422 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700423}
424
Roshan Pius5c055462016-10-11 08:27:27 -0700425Return<void> WifiChip::getApIface(const hidl_string& ifname,
426 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700427 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
428 &WifiChip::getApIfaceInternal, hidl_status_cb,
429 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700430}
431
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800432Return<void> WifiChip::removeApIface(const hidl_string& ifname,
433 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700434 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
435 &WifiChip::removeApIfaceInternal, hidl_status_cb,
436 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800437}
438
Roshan Pius5c055462016-10-11 08:27:27 -0700439Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700440 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
441 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700442}
443
Roshan Pius5c055462016-10-11 08:27:27 -0700444Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700445 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
446 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700447}
448
449Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700450 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700451 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
452 &WifiChip::getNanIfaceInternal, hidl_status_cb,
453 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700454}
455
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800456Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
457 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700458 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
459 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
460 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800461}
462
Roshan Pius5c055462016-10-11 08:27:27 -0700463Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700464 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
465 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700466}
467
Roshan Pius5c055462016-10-11 08:27:27 -0700468Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700469 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
470 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700471}
472
473Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700474 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700475 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
476 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
477 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700478}
479
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800480Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
481 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700482 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
483 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
484 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800485}
486
Roshan Pius5c055462016-10-11 08:27:27 -0700487Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700488 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
489 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700490}
491
Roshan Pius5c055462016-10-11 08:27:27 -0700492Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700493 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
494 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700495}
496
497Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700498 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700499 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
500 &WifiChip::getStaIfaceInternal, hidl_status_cb,
501 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700502}
503
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800504Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
505 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700506 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
507 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
508 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800509}
510
Roshan Pius5c055462016-10-11 08:27:27 -0700511Return<void> WifiChip::createRttController(
512 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700513 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
514 &WifiChip::createRttControllerInternal,
515 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700516}
517
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700518Return<void> WifiChip::getDebugRingBuffersStatus(
519 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700520 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
521 &WifiChip::getDebugRingBuffersStatusInternal,
522 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700523}
524
525Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700526 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
527 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700528 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700529 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
530 &WifiChip::startLoggingToDebugRingBufferInternal,
531 hidl_status_cb, ring_name, verbose_level,
532 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700533}
534
535Return<void> WifiChip::forceDumpToDebugRingBuffer(
536 const hidl_string& ring_name,
537 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700538 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
539 &WifiChip::forceDumpToDebugRingBufferInternal,
540 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700541}
542
Roger Wangb294c762018-11-02 15:34:39 +0800543Return<void> WifiChip::flushRingBufferToFile(
544 flushRingBufferToFile_cb hidl_status_cb) {
545 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
546 &WifiChip::flushRingBufferToFileInternal,
547 hidl_status_cb);
548}
549
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800550Return<void> WifiChip::stopLoggingToDebugRingBuffer(
551 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700552 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
553 &WifiChip::stopLoggingToDebugRingBufferInternal,
554 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800555}
556
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700557Return<void> WifiChip::getDebugHostWakeReasonStats(
558 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700559 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
560 &WifiChip::getDebugHostWakeReasonStatsInternal,
561 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700562}
563
Roshan Pius203cb032016-12-14 17:41:20 -0800564Return<void> WifiChip::enableDebugErrorAlerts(
565 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700566 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
567 &WifiChip::enableDebugErrorAlertsInternal,
568 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800569}
570
Roshan Pius735ff432017-07-25 08:48:08 -0700571Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700572 V1_1::IWifiChip::TxPowerScenario scenario,
573 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700574 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
575 &WifiChip::selectTxPowerScenarioInternal,
576 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700577}
578
Roshan Pius735ff432017-07-25 08:48:08 -0700579Return<void> WifiChip::resetTxPowerScenario(
580 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700581 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
582 &WifiChip::resetTxPowerScenarioInternal,
583 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700584}
585
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700586Return<void> WifiChip::setLatencyMode(LatencyMode mode,
587 setLatencyMode_cb hidl_status_cb) {
588 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
589 &WifiChip::setLatencyModeInternal, hidl_status_cb,
590 mode);
591}
592
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800593Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700594 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800595 registerEventCallback_cb hidl_status_cb) {
596 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
597 &WifiChip::registerEventCallbackInternal_1_2,
598 hidl_status_cb, event_callback);
599}
600
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800601Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700602 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800603 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700604 &WifiChip::selectTxPowerScenarioInternal_1_2,
605 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800606}
607
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700608Return<void> WifiChip::getCapabilities_1_3(getCapabilities_cb hidl_status_cb) {
609 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
610 &WifiChip::getCapabilitiesInternal_1_3,
611 hidl_status_cb);
612}
613
xshu5899e8e2018-01-09 15:36:03 -0800614Return<void> WifiChip::debug(const hidl_handle& handle,
615 const hidl_vec<hidl_string>&) {
616 if (handle != nullptr && handle->numFds >= 1) {
617 int fd = handle->data[0];
618 if (!writeRingbufferFilesInternal()) {
619 LOG(ERROR) << "Error writing files to flash";
620 }
xshu4cb33162018-01-24 15:40:06 -0800621 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800622 if (n_error != 0) {
623 LOG(ERROR) << n_error << " errors occured in cpio function";
624 }
625 fsync(fd);
626 } else {
627 LOG(ERROR) << "File handle error";
628 }
629 return Void();
630}
631
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -0700632Return<void> WifiChip::createRttController_1_4(
633 const sp<IWifiIface>& bound_iface,
634 createRttController_1_4_cb hidl_status_cb) {
635 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
636 &WifiChip::createRttControllerInternal_1_4,
637 hidl_status_cb, bound_iface);
638}
639
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -0800640Return<void> WifiChip::registerEventCallback_1_4(
641 const sp<IWifiChipEventCallback>& event_callback,
642 registerEventCallback_cb hidl_status_cb) {
643 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
644 &WifiChip::registerEventCallbackInternal_1_4,
645 hidl_status_cb, event_callback);
646}
647
Roshan Pius35d958c2016-10-06 16:47:38 -0700648void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700649 invalidateAndClearAll(ap_ifaces_);
650 invalidateAndClearAll(nan_ifaces_);
651 invalidateAndClearAll(p2p_ifaces_);
652 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700653 // Since all the ifaces are invalid now, all RTT controller objects
654 // using those ifaces also need to be invalidated.
655 for (const auto& rtt : rtt_controllers_) {
656 rtt->invalidate();
657 }
658 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700659}
660
Roshan Pius82368502019-05-16 12:53:02 -0700661void WifiChip::invalidateAndRemoveDependencies(
662 const std::string& removed_iface_name) {
663 for (const auto& nan_iface : nan_ifaces_) {
664 if (nan_iface->getName() == removed_iface_name) {
665 invalidateAndClear(nan_ifaces_, nan_iface);
666 for (const auto& callback : event_cb_handler_.getCallbacks()) {
667 if (!callback
668 ->onIfaceRemoved(IfaceType::NAN, removed_iface_name)
669 .isOk()) {
670 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
671 }
672 }
673 }
674 }
675 for (const auto& rtt : rtt_controllers_) {
676 if (rtt->getIfaceName() == removed_iface_name) {
677 invalidateAndClear(rtt_controllers_, rtt);
678 }
679 }
680}
681
Roshan Pius3c868522016-10-27 12:43:49 -0700682std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700683 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700684}
685
686WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800687 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
688 // Deprecated support for this callback.
689 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700690}
691
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700692std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -0700693 // Deprecated support for this callback.
694 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), 0};
695}
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();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800809 legacy_hal::wifi_error legacy_status =
810 legacy_hal_.lock()->createVirtualInterface(
811 ifname,
812 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::AP));
813 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
814 LOG(ERROR) << "Failed to add interface: " << ifname << " "
815 << legacyErrorToString(legacy_status);
816 return {createWifiStatusFromLegacyError(legacy_status), {}};
817 }
Patrik Fimml6beae322019-10-09 17:34:01 +0200818 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700819 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700820 for (const auto& callback : event_cb_handler_.getCallbacks()) {
821 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
822 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
823 }
824 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700825 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700826 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700827}
828
829std::pair<WifiStatus, std::vector<hidl_string>>
830WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700831 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700832 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
833 }
Roshan Pius675609b2017-10-31 14:24:58 -0700834 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700835}
836
837std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800838 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), nullptr};
842 }
Roshan Pius675609b2017-10-31 14:24:58 -0700843 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700844}
845
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800846WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700847 const auto iface = findUsingName(ap_ifaces_, ifname);
848 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700849 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800850 }
Roshan Pius82368502019-05-16 12:53:02 -0700851 // Invalidate & remove any dependent objects first.
852 // Note: This is probably not required because we never create
853 // nan/rtt objects over AP iface. But, there is no harm to do it
854 // here and not make that assumption all over the place.
855 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -0800856 legacy_hal::wifi_error legacy_status =
857 legacy_hal_.lock()->deleteVirtualInterface(ifname);
858 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
859 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
860 << legacyErrorToString(legacy_status);
861 }
Roshan Pius675609b2017-10-31 14:24:58 -0700862 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700863 for (const auto& callback : event_cb_handler_.getCallbacks()) {
864 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
865 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
866 }
867 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700868 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -0700869 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800870}
871
Roshan Pius3c868522016-10-27 12:43:49 -0700872std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700873 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700874 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800875 }
Roshan Pius5ba0a902020-04-14 11:55:42 -0700876 bool is_dedicated_iface = true;
877 std::string ifname = getNanIfaceName();
878 if (ifname.empty()) {
879 // Use the first shared STA iface (wlan0) if a dedicated aware iface is
880 // not defined.
881 ifname = getFirstActiveWlanIfaceName();
882 is_dedicated_iface = false;
883 }
884 sp<WifiNanIface> iface =
885 new WifiNanIface(ifname, is_dedicated_iface, legacy_hal_, iface_util_);
Roshan Piuscc338202017-11-02 13:54:09 -0700886 nan_ifaces_.push_back(iface);
887 for (const auto& callback : event_cb_handler_.getCallbacks()) {
888 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
889 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
890 }
891 }
892 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700893}
894
895std::pair<WifiStatus, std::vector<hidl_string>>
896WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700897 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700898 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
899 }
Roshan Pius675609b2017-10-31 14:24:58 -0700900 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700901}
902
903std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800904 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700905 const auto iface = findUsingName(nan_ifaces_, ifname);
906 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700907 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
908 }
Roshan Pius675609b2017-10-31 14:24:58 -0700909 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700910}
911
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800912WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700913 const auto iface = findUsingName(nan_ifaces_, ifname);
914 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700915 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800916 }
Roshan Pius675609b2017-10-31 14:24:58 -0700917 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700918 for (const auto& callback : event_cb_handler_.getCallbacks()) {
919 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
920 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
921 }
922 }
923 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800924}
925
Roshan Pius3c868522016-10-27 12:43:49 -0700926std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700927 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700928 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800929 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700930 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700931 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
932 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700933 for (const auto& callback : event_cb_handler_.getCallbacks()) {
934 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
935 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
936 }
937 }
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::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700943 if (p2p_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(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700947}
948
949std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800950 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700951 const auto iface = findUsingName(p2p_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::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700959 const auto iface = findUsingName(p2p_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(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700964 for (const auto& callback : event_cb_handler_.getCallbacks()) {
965 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
966 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
967 }
968 }
969 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800970}
971
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -0800972std::pair<WifiStatus, sp<V1_3::IWifiStaIface>>
973WifiChip::createStaIfaceInternal() {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700974 if (!canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700975 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800976 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -0700977 std::string ifname = allocateStaIfaceName();
Sunil Raviddab4bb2020-02-03 22:45:19 -0800978 legacy_hal::wifi_error legacy_status =
979 legacy_hal_.lock()->createVirtualInterface(
980 ifname,
981 hidl_struct_util::convertHidlIfaceTypeToLegacy(IfaceType::STA));
982 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
983 LOG(ERROR) << "Failed to add interface: " << ifname << " "
984 << legacyErrorToString(legacy_status);
985 return {createWifiStatusFromLegacyError(legacy_status), {}};
986 }
Roshan Pius99dab382019-02-14 07:57:10 -0800987 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_, iface_util_);
Roshan Pius675609b2017-10-31 14:24:58 -0700988 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700989 for (const auto& callback : event_cb_handler_.getCallbacks()) {
990 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
991 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
992 }
993 }
Roshan Pius8574e7f2019-04-01 13:30:40 -0700994 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Pius675609b2017-10-31 14:24:58 -0700995 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700996}
997
998std::pair<WifiStatus, std::vector<hidl_string>>
999WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -07001000 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001001 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
1002 }
Roshan Pius675609b2017-10-31 14:24:58 -07001003 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -07001004}
1005
Ahmed ElArabawyb23485d2019-12-09 15:24:16 -08001006std::pair<WifiStatus, sp<V1_3::IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001007 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001008 const auto iface = findUsingName(sta_ifaces_, ifname);
1009 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001010 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
1011 }
Roshan Pius675609b2017-10-31 14:24:58 -07001012 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -07001013}
1014
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001015WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -07001016 const auto iface = findUsingName(sta_ifaces_, ifname);
1017 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001018 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -08001019 }
Roshan Pius82368502019-05-16 12:53:02 -07001020 // Invalidate & remove any dependent objects first.
1021 invalidateAndRemoveDependencies(ifname);
Sunil Raviddab4bb2020-02-03 22:45:19 -08001022 legacy_hal::wifi_error legacy_status =
1023 legacy_hal_.lock()->deleteVirtualInterface(ifname);
1024 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1025 LOG(ERROR) << "Failed to remove interface: " << ifname << " "
1026 << legacyErrorToString(legacy_status);
1027 }
Roshan Pius675609b2017-10-31 14:24:58 -07001028 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001029 for (const auto& callback : event_cb_handler_.getCallbacks()) {
1030 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
1031 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
1032 }
1033 }
Roshan Pius8574e7f2019-04-01 13:30:40 -07001034 setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001035 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -08001036}
1037
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001038std::pair<WifiStatus, sp<V1_0::IWifiRttController>>
1039WifiChip::createRttControllerInternal(const sp<IWifiIface>& /*bound_iface*/) {
1040 LOG(ERROR) << "createRttController is not supported on this HAL";
Ahmed ElArabawy36defb32019-12-29 21:24:27 -08001041 return {createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED), {}};
Roshan Pius3c868522016-10-27 12:43:49 -07001042}
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001043
1044std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
1045WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001046 legacy_hal::wifi_error legacy_status;
1047 std::vector<legacy_hal::wifi_ring_buffer_status>
1048 legacy_ring_buffer_status_vec;
1049 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
Roshan Pius6036c022019-03-27 10:41:58 -07001050 legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001051 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1052 return {createWifiStatusFromLegacyError(legacy_status), {}};
1053 }
1054 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
1055 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
1056 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
1057 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1058 }
1059 return {createWifiStatus(WifiStatusCode::SUCCESS),
1060 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001061}
1062
1063WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -07001064 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
1065 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
1066 WifiStatus status = registerDebugRingBufferCallback();
1067 if (status.code != WifiStatusCode::SUCCESS) {
1068 return status;
1069 }
1070 legacy_hal::wifi_error legacy_status =
1071 legacy_hal_.lock()->startRingBufferLogging(
Roshan Pius6036c022019-03-27 10:41:58 -07001072 getFirstActiveWlanIfaceName(), ring_name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001073 static_cast<
1074 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
1075 verbose_level),
1076 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -08001077 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
1078 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusa63b53f2019-11-18 11:03:13 -08001079 // if verbose logging enabled, turn up HAL daemon logging as well.
1080 if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) {
1081 android::base::SetMinimumLogSeverity(android::base::DEBUG);
1082 } else {
1083 android::base::SetMinimumLogSeverity(android::base::VERBOSE);
1084 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001085 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001086}
1087
1088WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -08001089 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001090 WifiStatus status = registerDebugRingBufferCallback();
1091 if (status.code != WifiStatusCode::SUCCESS) {
1092 return status;
1093 }
1094 legacy_hal::wifi_error legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001095 legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(),
1096 ring_name);
xshu5899e8e2018-01-09 15:36:03 -08001097
Roshan Piusabcf78f2017-10-06 16:30:38 -07001098 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001099}
1100
Roger Wangb294c762018-11-02 15:34:39 +08001101WifiStatus WifiChip::flushRingBufferToFileInternal() {
1102 if (!writeRingbufferFilesInternal()) {
1103 LOG(ERROR) << "Error writing files to flash";
1104 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1105 }
1106 return createWifiStatus(WifiStatusCode::SUCCESS);
1107}
1108
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001109WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001110 legacy_hal::wifi_error legacy_status =
1111 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001112 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001113 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001114}
1115
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001116std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1117WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001118 legacy_hal::wifi_error legacy_status;
1119 legacy_hal::WakeReasonStats legacy_stats;
1120 std::tie(legacy_status, legacy_stats) =
Roshan Pius6036c022019-03-27 10:41:58 -07001121 legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001122 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1123 return {createWifiStatusFromLegacyError(legacy_status), {}};
1124 }
1125 WifiDebugHostWakeReasonStats hidl_stats;
1126 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1127 &hidl_stats)) {
1128 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1129 }
1130 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001131}
1132
Roshan Pius203cb032016-12-14 17:41:20 -08001133WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001134 legacy_hal::wifi_error legacy_status;
1135 if (enable) {
1136 android::wp<WifiChip> weak_ptr_this(this);
1137 const auto& on_alert_callback = [weak_ptr_this](
1138 int32_t error_code,
1139 std::vector<uint8_t> debug_data) {
1140 const auto shared_ptr_this = weak_ptr_this.promote();
1141 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1142 LOG(ERROR) << "Callback invoked on an invalid object";
1143 return;
1144 }
1145 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1146 if (!callback->onDebugErrorAlert(error_code, debug_data)
1147 .isOk()) {
1148 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1149 }
1150 }
1151 };
1152 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001153 getFirstActiveWlanIfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001154 } else {
1155 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001156 getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001157 }
1158 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001159}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001160
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001161WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001162 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001163 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001164 getFirstActiveWlanIfaceName(),
Roshan Piusabcf78f2017-10-06 16:30:38 -07001165 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1166 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001167}
1168
Roshan Pius735ff432017-07-25 08:48:08 -07001169WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001170 auto legacy_status =
Roshan Pius6036c022019-03-27 10:41:58 -07001171 legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001172 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001173}
1174
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001175WifiStatus WifiChip::setLatencyModeInternal(LatencyMode mode) {
1176 auto legacy_status = legacy_hal_.lock()->setLatencyMode(
Roshan Pius6036c022019-03-27 10:41:58 -07001177 getFirstActiveWlanIfaceName(),
Ahmed ElArabawyeaf82402018-10-26 09:46:04 -07001178 hidl_struct_util::convertHidlLatencyModeToLegacy(mode));
1179 return createWifiStatusFromLegacyError(legacy_status);
1180}
1181
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001182WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001183 const sp<V1_2::IWifiChipEventCallback>& /* event_callback */) {
1184 // Deprecated support for this callback.
1185 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001186}
1187
Jong Wook Kimda830c92018-07-23 15:29:38 -07001188WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1189 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001190 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
Roshan Pius6036c022019-03-27 10:41:58 -07001191 getFirstActiveWlanIfaceName(),
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001192 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1193 return createWifiStatusFromLegacyError(legacy_status);
1194}
1195
Ahmed ElArabawyeeb53382019-10-10 20:18:31 -07001196std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal_1_3() {
1197 legacy_hal::wifi_error legacy_status;
1198 uint32_t legacy_feature_set;
1199 uint32_t legacy_logger_feature_set;
1200 const auto ifname = getFirstActiveWlanIfaceName();
1201 std::tie(legacy_status, legacy_feature_set) =
1202 legacy_hal_.lock()->getSupportedFeatureSet(ifname);
1203 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1204 return {createWifiStatusFromLegacyError(legacy_status), 0};
1205 }
1206 std::tie(legacy_status, legacy_logger_feature_set) =
1207 legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname);
1208 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1209 // some devices don't support querying logger feature set
1210 legacy_logger_feature_set = 0;
1211 }
1212 uint32_t hidl_caps;
1213 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
1214 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
1215 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
1216 }
1217 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
1218}
1219
1220std::pair<WifiStatus, sp<IWifiRttController>>
1221WifiChip::createRttControllerInternal_1_4(const sp<IWifiIface>& bound_iface) {
1222 if (sta_ifaces_.size() == 0 &&
1223 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
1224 LOG(ERROR)
1225 << "createRttControllerInternal_1_4: Chip cannot support STAs "
1226 "(and RTT by extension)";
1227 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
1228 }
1229 sp<WifiRttController> rtt = new WifiRttController(
1230 getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_);
1231 rtt_controllers_.emplace_back(rtt);
1232 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
1233}
1234
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001235WifiStatus WifiChip::registerEventCallbackInternal_1_4(
1236 const sp<IWifiChipEventCallback>& event_callback) {
1237 if (!event_cb_handler_.addCallback(event_callback)) {
1238 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1239 }
1240 return createWifiStatus(WifiStatusCode::SUCCESS);
1241}
1242
Roshan Piusba38d9c2017-12-08 07:32:08 -08001243WifiStatus WifiChip::handleChipConfiguration(
1244 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1245 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001246 // If the chip is already configured in a different mode, stop
1247 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001248 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001249 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1250 << " to mode " << mode_id;
1251 invalidateAndRemoveAllIfaces();
1252 legacy_hal::wifi_error legacy_status =
1253 legacy_hal_.lock()->stop(lock, []() {});
1254 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1255 LOG(ERROR) << "Failed to stop legacy HAL: "
1256 << legacyErrorToString(legacy_status);
1257 return createWifiStatusFromLegacyError(legacy_status);
1258 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001259 }
Roshan Piuscc338202017-11-02 13:54:09 -07001260 // Firmware mode change not needed for V2 devices.
1261 bool success = true;
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001262 if (mode_id == feature_flags::chip_mode_ids::kV1Sta) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001263 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Tomasz Wasilczykb424da72018-11-15 11:52:57 -08001264 } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001265 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1266 }
1267 if (!success) {
1268 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1269 }
1270 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1271 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1272 LOG(ERROR) << "Failed to start legacy HAL: "
1273 << legacyErrorToString(legacy_status);
1274 return createWifiStatusFromLegacyError(legacy_status);
1275 }
Roshan Pius85c64412018-01-22 17:58:40 -08001276 // Every time the HAL is restarted, we need to register the
1277 // radio mode change callback.
1278 WifiStatus status = registerRadioModeChangeCallback();
1279 if (status.code != WifiStatusCode::SUCCESS) {
1280 // This probably is not a critical failure?
1281 LOG(ERROR) << "Failed to register radio mode change callback";
1282 }
chenpaulf5eca292019-03-14 11:08:03 +08001283 // Extract and save the version information into property.
1284 std::pair<WifiStatus, IWifiChip::ChipDebugInfo> version_info;
1285 version_info = WifiChip::requestChipDebugInfoInternal();
1286 if (WifiStatusCode::SUCCESS == version_info.first.code) {
1287 property_set("vendor.wlan.firmware.version",
1288 version_info.second.firmwareDescription.c_str());
1289 property_set("vendor.wlan.driver.version",
1290 version_info.second.driverDescription.c_str());
1291 }
1292
Roshan Piusabcf78f2017-10-06 16:30:38 -07001293 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001294}
Roshan Pius48185b22016-12-15 19:10:30 -08001295
1296WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001297 if (debug_ring_buffer_cb_registered_) {
1298 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001299 }
Roshan Pius3797e182017-03-30 18:01:54 -07001300
Roshan Piusabcf78f2017-10-06 16:30:38 -07001301 android::wp<WifiChip> weak_ptr_this(this);
1302 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001303 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001304 const std::vector<uint8_t>& data,
1305 const legacy_hal::wifi_ring_buffer_status& status) {
1306 const auto shared_ptr_this = weak_ptr_this.promote();
1307 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1308 LOG(ERROR) << "Callback invoked on an invalid object";
1309 return;
1310 }
1311 WifiDebugRingBufferStatus hidl_status;
1312 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1313 status, &hidl_status)) {
1314 LOG(ERROR) << "Error converting ring buffer status";
1315 return;
1316 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301317 {
1318 std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t);
1319 const auto& target =
1320 shared_ptr_this->ringbuffer_map_.find(name);
1321 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1322 Ringbuffer& cur_buffer = target->second;
1323 cur_buffer.append(data);
1324 } else {
1325 LOG(ERROR) << "Ringname " << name << " not found";
1326 return;
1327 }
1328 // unlock
Roshan Piusabcf78f2017-10-06 16:30:38 -07001329 }
1330 };
1331 legacy_hal::wifi_error legacy_status =
1332 legacy_hal_.lock()->registerRingBufferCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001333 getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001334
Roshan Piusabcf78f2017-10-06 16:30:38 -07001335 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1336 debug_ring_buffer_cb_registered_ = true;
1337 }
1338 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001339}
1340
Roshan Pius85c64412018-01-22 17:58:40 -08001341WifiStatus WifiChip::registerRadioModeChangeCallback() {
1342 android::wp<WifiChip> weak_ptr_this(this);
1343 const auto& on_radio_mode_change_callback =
1344 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1345 const auto shared_ptr_this = weak_ptr_this.promote();
1346 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1347 LOG(ERROR) << "Callback invoked on an invalid object";
1348 return;
1349 }
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001350 std::vector<IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001351 hidl_radio_mode_infos;
1352 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1353 mac_infos, &hidl_radio_mode_infos)) {
1354 LOG(ERROR) << "Error converting wifi mac info";
1355 return;
1356 }
1357 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001358 if (!callback->onRadioModeChange_1_4(hidl_radio_mode_infos)
Roshan Pius85c64412018-01-22 17:58:40 -08001359 .isOk()) {
Ahmed ElArabawyfd809fc2019-11-15 18:19:15 -08001360 LOG(ERROR) << "Failed to invoke onRadioModeChange_1_4"
Roshan Pius85c64412018-01-22 17:58:40 -08001361 << " callback on: " << toString(callback);
1362 }
1363 }
1364 };
1365 legacy_hal::wifi_error legacy_status =
1366 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
Roshan Pius6036c022019-03-27 10:41:58 -07001367 getFirstActiveWlanIfaceName(), on_radio_mode_change_callback);
Roshan Pius85c64412018-01-22 17:58:40 -08001368 return createWifiStatusFromLegacyError(legacy_status);
1369}
1370
Roshan Piuscc338202017-11-02 13:54:09 -07001371std::vector<IWifiChip::ChipIfaceCombination>
1372WifiChip::getCurrentModeIfaceCombinations() {
1373 if (!isValidModeId(current_mode_id_)) {
1374 LOG(ERROR) << "Chip not configured in a mode yet";
1375 return {};
1376 }
1377 for (const auto& mode : modes_) {
1378 if (mode.id == current_mode_id_) {
1379 return mode.availableCombinations;
1380 }
1381 }
1382 CHECK(0) << "Expected to find iface combinations for current mode!";
1383 return {};
1384}
1385
1386// Returns a map indexed by IfaceType with the number of ifaces currently
1387// created of the corresponding type.
1388std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1389 std::map<IfaceType, size_t> iface_counts;
1390 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1391 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1392 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1393 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1394 return iface_counts;
1395}
1396
1397// This expands the provided iface combinations to a more parseable
1398// form. Returns a vector of available combinations possible with the number
1399// of ifaces of each type in the combination.
1400// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1401std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1402 const IWifiChip::ChipIfaceCombination& combination) {
1403 uint32_t num_expanded_combos = 1;
1404 for (const auto& limit : combination.limits) {
1405 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1406 num_expanded_combos *= limit.types.size();
1407 }
1408 }
1409
1410 // Allocate the vector of expanded combos and reset all iface counts to 0
1411 // in each combo.
1412 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1413 expanded_combos.resize(num_expanded_combos);
1414 for (auto& expanded_combo : expanded_combos) {
1415 for (const auto type :
1416 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1417 expanded_combo[type] = 0;
1418 }
1419 }
1420 uint32_t span = num_expanded_combos;
1421 for (const auto& limit : combination.limits) {
1422 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1423 span /= limit.types.size();
1424 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1425 const auto iface_type =
1426 limit.types[(k / span) % limit.types.size()];
1427 expanded_combos[k][iface_type]++;
1428 }
1429 }
1430 }
1431 return expanded_combos;
1432}
1433
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001434bool WifiChip::canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1435 const std::map<IfaceType, size_t>& expanded_combo,
1436 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001437 const auto current_combo = getCurrentIfaceCombination();
1438
1439 // Check if we have space for 1 more iface of |type| in this combo
1440 for (const auto type :
1441 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1442 size_t num_ifaces_needed = current_combo.at(type);
1443 if (type == requested_type) {
1444 num_ifaces_needed++;
1445 }
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001446 size_t num_ifaces_allowed = expanded_combo.at(type);
Roshan Piuscc338202017-11-02 13:54:09 -07001447 if (num_ifaces_needed > num_ifaces_allowed) {
1448 return false;
1449 }
1450 }
1451 return true;
1452}
1453
1454// This method does the following:
1455// a) Enumerate all possible iface combos by expanding the current
1456// ChipIfaceCombination.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001457// b) Check if the requested iface type can be added to the current mode
1458// with the iface combination that is already active.
1459bool WifiChip::canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
1460 IfaceType requested_type) {
Roshan Piuscc338202017-11-02 13:54:09 -07001461 if (!isValidModeId(current_mode_id_)) {
1462 LOG(ERROR) << "Chip not configured in a mode yet";
1463 return false;
1464 }
1465 const auto combinations = getCurrentModeIfaceCombinations();
1466 for (const auto& combination : combinations) {
1467 const auto expanded_combos = expandIfaceCombinations(combination);
1468 for (const auto& expanded_combo : expanded_combos) {
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001469 if (canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
1470 expanded_combo, requested_type)) {
Roshan Piuscc338202017-11-02 13:54:09 -07001471 return true;
1472 }
1473 }
1474 }
1475 return false;
1476}
1477
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001478// Note: This does not consider ifaces already active. It only checks if the
1479// provided expanded iface combination can support the requested combo.
1480bool WifiChip::canExpandedIfaceComboSupportIfaceCombo(
1481 const std::map<IfaceType, size_t>& expanded_combo,
1482 const std::map<IfaceType, size_t>& req_combo) {
1483 // Check if we have space for 1 more iface of |type| in this combo
1484 for (const auto type :
1485 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1486 if (req_combo.count(type) == 0) {
1487 // Iface of "type" not in the req_combo.
1488 continue;
1489 }
1490 size_t num_ifaces_needed = req_combo.at(type);
1491 size_t num_ifaces_allowed = expanded_combo.at(type);
1492 if (num_ifaces_needed > num_ifaces_allowed) {
1493 return false;
1494 }
1495 }
1496 return true;
1497}
1498// This method does the following:
1499// a) Enumerate all possible iface combos by expanding the current
1500// ChipIfaceCombination.
1501// b) Check if the requested iface combo can be added to the current mode.
1502// Note: This does not consider ifaces already active. It only checks if the
1503// current mode can support the requested combo.
1504bool WifiChip::canCurrentModeSupportIfaceCombo(
1505 const std::map<IfaceType, size_t>& req_combo) {
1506 if (!isValidModeId(current_mode_id_)) {
1507 LOG(ERROR) << "Chip not configured in a mode yet";
1508 return false;
1509 }
1510 const auto combinations = getCurrentModeIfaceCombinations();
1511 for (const auto& combination : combinations) {
1512 const auto expanded_combos = expandIfaceCombinations(combination);
1513 for (const auto& expanded_combo : expanded_combos) {
1514 if (canExpandedIfaceComboSupportIfaceCombo(expanded_combo,
1515 req_combo)) {
1516 return true;
1517 }
1518 }
1519 }
1520 return false;
1521}
1522
1523// This method does the following:
1524// a) Enumerate all possible iface combos by expanding the current
1525// ChipIfaceCombination.
1526// b) Check if the requested iface type can be added to the current mode.
1527bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType requested_type) {
1528 // Check if we can support atleast 1 iface of type.
1529 std::map<IfaceType, size_t> req_iface_combo;
1530 req_iface_combo[requested_type] = 1;
1531 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1532}
1533
Roshan Piuscc338202017-11-02 13:54:09 -07001534bool WifiChip::isValidModeId(ChipModeId mode_id) {
1535 for (const auto& mode : modes_) {
1536 if (mode.id == mode_id) {
1537 return true;
1538 }
1539 }
1540 return false;
1541}
1542
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001543bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() {
1544 // Check if we can support atleast 1 STA & 1 AP concurrently.
1545 std::map<IfaceType, size_t> req_iface_combo;
1546 req_iface_combo[IfaceType::AP] = 1;
1547 req_iface_combo[IfaceType::STA] = 1;
1548 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1549}
1550
James Mattisd2e4c072019-05-22 16:14:48 -07001551bool WifiChip::isDualApAllowedInCurrentMode() {
1552 // Check if we can support atleast 1 STA & 1 AP concurrently.
1553 std::map<IfaceType, size_t> req_iface_combo;
1554 req_iface_combo[IfaceType::AP] = 2;
1555 return canCurrentModeSupportIfaceCombo(req_iface_combo);
1556}
1557
Roshan Pius6036c022019-03-27 10:41:58 -07001558std::string WifiChip::getFirstActiveWlanIfaceName() {
Roshan Pius444473f2019-04-19 08:41:20 -07001559 if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName();
1560 if (ap_ifaces_.size() > 0) return ap_ifaces_[0]->getName();
Roshan Pius6036c022019-03-27 10:41:58 -07001561 // This could happen if the chip call is made before any STA/AP
1562 // iface is created. Default to wlan0 for such cases.
Roshan Pius444473f2019-04-19 08:41:20 -07001563 LOG(WARNING) << "No active wlan interfaces in use! Using default";
Roshan Pius6036c022019-03-27 10:41:58 -07001564 return getWlanIfaceName(0);
1565}
1566
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001567// Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx|
1568// not already in use.
1569// Note: This doesn't check the actual presence of these interfaces.
1570std::string WifiChip::allocateApOrStaIfaceName(uint32_t start_idx) {
1571 for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) {
1572 const auto ifname = getWlanIfaceName(idx);
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001573 if (findUsingName(ap_ifaces_, ifname)) continue;
1574 if (findUsingName(sta_ifaces_, ifname)) continue;
1575 return ifname;
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001576 }
1577 // This should never happen. We screwed up somewhere if it did.
Tomasz Wasilczyk77401d32018-12-20 12:42:54 -08001578 CHECK(false) << "All wlan interfaces in use already!";
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001579 return {};
1580}
1581
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001582// AP iface names start with idx 1 for modes supporting
James Mattisd2e4c072019-05-22 16:14:48 -07001583// concurrent STA and not dual AP, else start with idx 0.
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001584std::string WifiChip::allocateApIfaceName() {
James Mattisd2e4c072019-05-22 16:14:48 -07001585 return allocateApOrStaIfaceName((isStaApConcurrencyAllowedInCurrentMode() &&
1586 !isDualApAllowedInCurrentMode())
1587 ? 1
1588 : 0);
Roshan Piusa3e5b7f2019-03-25 13:52:45 -07001589}
1590
1591// STA iface names start with idx 0.
1592// Primary STA iface will always be 0.
1593std::string WifiChip::allocateStaIfaceName() {
1594 return allocateApOrStaIfaceName(0);
1595}
1596
xshu5899e8e2018-01-09 15:36:03 -08001597bool WifiChip::writeRingbufferFilesInternal() {
1598 if (!removeOldFilesInternal()) {
1599 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1600 return false;
1601 }
1602 // write ringbuffers to file
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301603 {
1604 std::unique_lock<std::mutex> lk(lock_t);
1605 for (const auto& item : ringbuffer_map_) {
1606 const Ringbuffer& cur_buffer = item.second;
1607 if (cur_buffer.getData().empty()) {
1608 continue;
1609 }
1610 const std::string file_path_raw =
1611 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1612 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1613 if (dump_fd == -1) {
1614 PLOG(ERROR) << "create file failed";
1615 return false;
1616 }
1617 unique_fd file_auto_closer(dump_fd);
1618 for (const auto& cur_block : cur_buffer.getData()) {
1619 if (write(dump_fd, cur_block.data(),
1620 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1621 PLOG(ERROR) << "Error writing to file";
1622 }
xshu5899e8e2018-01-09 15:36:03 -08001623 }
1624 }
Veerendranath Jakkam25b3a6f2020-04-14 22:04:39 +05301625 // unlock
xshu5899e8e2018-01-09 15:36:03 -08001626 }
1627 return true;
1628}
1629
Roshan Pius79a99752016-10-04 13:03:58 -07001630} // namespace implementation
Ahmed ElArabawyf501a982019-07-23 15:02:22 -07001631} // namespace V1_4
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001632} // namespace wifi
1633} // namespace hardware
1634} // namespace android