blob: faf1862530f023bf3e863a56a40061c292d6c9be [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
Roshan Pius2c06a3f2016-12-15 17:51:40 -080039constexpr ChipModeId kInvalidModeId = UINT32_MAX;
Roshan Piuscc338202017-11-02 13:54:09 -070040// These mode ID's should be unique (even across combo versions). Refer to
41// handleChipConfiguration() for it's usage.
42// Mode ID's for V1
43constexpr ChipModeId kV1StaChipModeId = 0;
44constexpr ChipModeId kV1ApChipModeId = 1;
45// Mode ID for V2
46constexpr ChipModeId kV2ChipModeId = 2;
Roshan Pius35d958c2016-10-06 16:47:38 -070047
xshu5899e8e2018-01-09 15:36:03 -080048constexpr char kCpioMagic[] = "070701";
49constexpr size_t kMaxBufferSizeBytes = 1024 * 1024;
50constexpr uint32_t kMaxRingBufferFileAgeSeconds = 60 * 60;
xshu37126c92018-04-13 16:24:45 -070051constexpr uint32_t kMaxRingBufferFileNum = 20;
xshu5899e8e2018-01-09 15:36:03 -080052constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
53
Roshan Pius35d958c2016-10-06 16:47:38 -070054template <typename Iface>
Roshan Pius675609b2017-10-31 14:24:58 -070055void invalidateAndClear(std::vector<sp<Iface>>& ifaces, sp<Iface> iface) {
56 iface->invalidate();
57 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface),
58 ifaces.end());
59}
60
61template <typename Iface>
62void invalidateAndClearAll(std::vector<sp<Iface>>& ifaces) {
63 for (const auto& iface : ifaces) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070064 iface->invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -070065 }
Roshan Pius675609b2017-10-31 14:24:58 -070066 ifaces.clear();
67}
68
69template <typename Iface>
70std::vector<hidl_string> getNames(std::vector<sp<Iface>>& ifaces) {
71 std::vector<hidl_string> names;
72 for (const auto& iface : ifaces) {
73 names.emplace_back(iface->getName());
74 }
75 return names;
76}
77
78template <typename Iface>
79sp<Iface> findUsingName(std::vector<sp<Iface>>& ifaces,
80 const std::string& name) {
81 std::vector<hidl_string> names;
82 for (const auto& iface : ifaces) {
83 if (name == iface->getName()) {
84 return iface;
85 }
86 }
87 return nullptr;
Roshan Pius35d958c2016-10-06 16:47:38 -070088}
Roshan Pius9377a0d2017-10-06 13:18:54 -070089
90std::string getWlan0IfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -070091 std::array<char, PROPERTY_VALUE_MAX> buffer;
92 property_get("wifi.interface", buffer.data(), "wlan0");
93 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -070094}
95
Roshan Pius9377a0d2017-10-06 13:18:54 -070096std::string getWlan1IfaceName() {
Roshan Pius8e3c7ef2017-11-03 09:43:08 -070097 std::array<char, PROPERTY_VALUE_MAX> buffer;
98 property_get("wifi.concurrent.interface", buffer.data(), "wlan1");
99 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700100}
Roshan Pius9377a0d2017-10-06 13:18:54 -0700101
102std::string getP2pIfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700103 std::array<char, PROPERTY_VALUE_MAX> buffer;
104 property_get("wifi.direct.interface", buffer.data(), "p2p0");
105 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700106}
107
xshu37126c92018-04-13 16:24:45 -0700108// delete files that meet either conditions:
109// 1. older than a predefined time in the wifi tombstone dir.
110// 2. Files in excess to a predefined amount, starting from the oldest ones
xshu5899e8e2018-01-09 15:36:03 -0800111bool removeOldFilesInternal() {
112 time_t now = time(0);
113 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
Josh Gaoa568e532018-06-04 18:16:00 -0700114 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(
115 opendir(kTombstoneFolderPath), closedir);
xshu5899e8e2018-01-09 15:36:03 -0800116 if (!dir_dump) {
117 LOG(ERROR) << "Failed to open directory: " << strerror(errno);
118 return false;
119 }
xshu5899e8e2018-01-09 15:36:03 -0800120 struct dirent* dp;
121 bool success = true;
xshu37126c92018-04-13 16:24:45 -0700122 std::list<std::pair<const time_t, std::string>> valid_files;
Josh Gaoa568e532018-06-04 18:16:00 -0700123 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800124 if (dp->d_type != DT_REG) {
125 continue;
126 }
127 std::string cur_file_name(dp->d_name);
128 struct stat cur_file_stat;
129 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800130 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
xshu5899e8e2018-01-09 15:36:03 -0800131 LOG(ERROR) << "Failed to get file stat for " << cur_file_path
132 << ": " << strerror(errno);
133 success = false;
xshu4cb33162018-01-24 15:40:06 -0800134 continue;
135 }
xshu37126c92018-04-13 16:24:45 -0700136 const time_t cur_file_time = cur_file_stat.st_mtime;
137 valid_files.push_back(
138 std::pair<const time_t, std::string>(cur_file_time, cur_file_path));
139 }
140 valid_files.sort(); // sort the list of files by last modified time from
141 // small to big.
142 uint32_t cur_file_count = valid_files.size();
143 for (auto cur_file : valid_files) {
144 if (cur_file_count > kMaxRingBufferFileNum ||
145 cur_file.first < delete_files_before) {
146 if (unlink(cur_file.second.c_str()) != 0) {
147 LOG(ERROR) << "Error deleting file " << strerror(errno);
148 success = false;
149 }
150 cur_file_count--;
151 } else {
152 break;
xshu5899e8e2018-01-09 15:36:03 -0800153 }
154 }
155 return success;
156}
157
xshu4cb33162018-01-24 15:40:06 -0800158// Helper function for |cpioArchiveFilesInDir|
159bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name,
160 size_t file_name_len) {
161 std::array<char, 32 * 1024> read_buf;
162 ssize_t llen =
163 sprintf(read_buf.data(),
164 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
165 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
166 st.st_gid, static_cast<int>(st.st_nlink),
167 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
168 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
169 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
170 if (write(out_fd, read_buf.data(), llen) == -1) {
171 LOG(ERROR) << "Error writing cpio header to file " << file_name << " "
172 << strerror(errno);
173 return false;
174 }
175 if (write(out_fd, file_name, file_name_len) == -1) {
176 LOG(ERROR) << "Error writing filename to file " << file_name << " "
177 << strerror(errno);
178 return false;
179 }
180
181 // NUL Pad header up to 4 multiple bytes.
182 llen = (llen + file_name_len) % 4;
183 if (llen != 0) {
184 const uint32_t zero = 0;
185 if (write(out_fd, &zero, 4 - llen) == -1) {
186 LOG(ERROR) << "Error padding 0s to file " << file_name << " "
187 << strerror(errno);
188 return false;
189 }
190 }
191 return true;
192}
193
194// Helper function for |cpioArchiveFilesInDir|
195size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
196 // writing content of file
197 std::array<char, 32 * 1024> read_buf;
198 ssize_t llen = st.st_size;
199 size_t n_error = 0;
200 while (llen > 0) {
201 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
202 if (bytes_read == -1) {
203 LOG(ERROR) << "Error reading file " << strerror(errno);
204 return ++n_error;
205 }
206 llen -= bytes_read;
207 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
208 LOG(ERROR) << "Error writing data to file " << strerror(errno);
209 return ++n_error;
210 }
211 if (bytes_read == 0) { // this should never happen, but just in case
212 // to unstuck from while loop
213 LOG(ERROR) << "Unexpected read result for " << strerror(errno);
214 n_error++;
215 break;
216 }
217 }
218 llen = st.st_size % 4;
219 if (llen != 0) {
220 const uint32_t zero = 0;
221 if (write(out_fd, &zero, 4 - llen) == -1) {
222 LOG(ERROR) << "Error padding 0s to file " << strerror(errno);
223 return ++n_error;
224 }
225 }
226 return n_error;
227}
228
229// Helper function for |cpioArchiveFilesInDir|
230bool cpioWriteFileTrailer(int out_fd) {
231 std::array<char, 4096> read_buf;
232 read_buf.fill(0);
233 if (write(out_fd, read_buf.data(),
234 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1,
235 0x0b, 0) +
236 4) == -1) {
237 LOG(ERROR) << "Error writing trailing bytes " << strerror(errno);
238 return false;
239 }
240 return true;
241}
242
xshu5899e8e2018-01-09 15:36:03 -0800243// Archives all files in |input_dir| and writes result into |out_fd|
244// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
245// portion
xshu4cb33162018-01-24 15:40:06 -0800246size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800247 struct dirent* dp;
248 size_t n_error = 0;
Josh Gaoa568e532018-06-04 18:16:00 -0700249 std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir),
250 closedir);
xshu5899e8e2018-01-09 15:36:03 -0800251 if (!dir_dump) {
252 LOG(ERROR) << "Failed to open directory: " << strerror(errno);
xshu4cb33162018-01-24 15:40:06 -0800253 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800254 }
Josh Gaoa568e532018-06-04 18:16:00 -0700255 while ((dp = readdir(dir_dump.get()))) {
xshu5899e8e2018-01-09 15:36:03 -0800256 if (dp->d_type != DT_REG) {
257 continue;
258 }
259 std::string cur_file_name(dp->d_name);
xshu4cb33162018-01-24 15:40:06 -0800260 // string.size() does not include the null terminator. The cpio FreeBSD
261 // file header expects the null character to be included in the length.
262 const size_t file_name_len = cur_file_name.size() + 1;
xshu5899e8e2018-01-09 15:36:03 -0800263 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800264 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800265 if (stat(cur_file_path.c_str(), &st) == -1) {
xshu5899e8e2018-01-09 15:36:03 -0800266 LOG(ERROR) << "Failed to get file stat for " << cur_file_path
267 << ": " << strerror(errno);
268 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800269 continue;
270 }
271 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
272 if (fd_read == -1) {
273 LOG(ERROR) << "Failed to open file " << cur_file_path << " "
274 << strerror(errno);
275 n_error++;
276 continue;
277 }
278 unique_fd file_auto_closer(fd_read);
279 if (!cpioWriteHeader(out_fd, st, cur_file_name.c_str(),
280 file_name_len)) {
281 return ++n_error;
282 }
283 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
284 if (write_error) {
285 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800286 }
287 }
xshu4cb33162018-01-24 15:40:06 -0800288 if (!cpioWriteFileTrailer(out_fd)) {
289 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800290 }
291 return n_error;
292}
293
294// Helper function to create a non-const char*.
295std::vector<char> makeCharVec(const std::string& str) {
296 std::vector<char> vec(str.size() + 1);
297 vec.assign(str.begin(), str.end());
298 vec.push_back('\0');
299 return vec;
300}
301
Roshan Piusabcf78f2017-10-06 16:30:38 -0700302} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700303
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700304namespace android {
305namespace hardware {
306namespace wifi {
Jong Wook Kimda830c92018-07-23 15:29:38 -0700307namespace V1_3 {
Roshan Pius79a99752016-10-04 13:03:58 -0700308namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700309using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800310using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700311
Roshan Pius52947fb2016-11-18 11:38:07 -0800312WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700313 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700314 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
315 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Pius52947fb2016-11-18 11:38:07 -0800316 : chip_id_(chip_id),
317 legacy_hal_(legacy_hal),
318 mode_controller_(mode_controller),
Roshan Pius200a17d2017-11-01 13:03:35 -0700319 feature_flags_(feature_flags),
Roshan Pius52947fb2016-11-18 11:38:07 -0800320 is_valid_(true),
Roshan Pius48185b22016-12-15 19:10:30 -0800321 current_mode_id_(kInvalidModeId),
Roshan Piuscc338202017-11-02 13:54:09 -0700322 debug_ring_buffer_cb_registered_(false) {
323 populateModes();
324}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700325
Roshan Piusaabe5752016-09-29 09:03:59 -0700326void WifiChip::invalidate() {
xshu37126c92018-04-13 16:24:45 -0700327 if (!writeRingbufferFilesInternal()) {
328 LOG(ERROR) << "Error writing files to flash";
329 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700330 invalidateAndRemoveAllIfaces();
331 legacy_hal_.reset();
332 event_cb_handler_.invalidate();
333 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700334}
335
Roshan Piusabcf78f2017-10-06 16:30:38 -0700336bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700337
Jong Wook Kimda830c92018-07-23 15:29:38 -0700338std::set<sp<V1_2::IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700339 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800340}
341
Roshan Pius5c055462016-10-11 08:27:27 -0700342Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700343 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
344 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700345}
346
Jong Wook Kimda830c92018-07-23 15:29:38 -0700347// Deprecated support for this callback
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700348Return<void> WifiChip::registerEventCallback(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800349 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700350 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700351 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
352 &WifiChip::registerEventCallbackInternal,
353 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700354}
355
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700356Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700357 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
358 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700359}
360
Roshan Pius5c055462016-10-11 08:27:27 -0700361Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700362 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
363 &WifiChip::getAvailableModesInternal,
364 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700365}
366
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800367Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700368 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800369 return validateAndCallWithLock(
370 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
371 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700372}
373
Roshan Pius5c055462016-10-11 08:27:27 -0700374Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700375 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
376 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700377}
378
Roshan Pius5c055462016-10-11 08:27:27 -0700379Return<void> WifiChip::requestChipDebugInfo(
380 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700381 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
382 &WifiChip::requestChipDebugInfoInternal,
383 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700384}
385
386Return<void> WifiChip::requestDriverDebugDump(
387 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700388 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
389 &WifiChip::requestDriverDebugDumpInternal,
390 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700391}
392
Roshan Pius5c055462016-10-11 08:27:27 -0700393Return<void> WifiChip::requestFirmwareDebugDump(
394 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700395 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
396 &WifiChip::requestFirmwareDebugDumpInternal,
397 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700398}
399
Roshan Pius5c055462016-10-11 08:27:27 -0700400Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700401 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
402 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700403}
404
Roshan Pius5c055462016-10-11 08:27:27 -0700405Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700406 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
407 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700408}
409
Roshan Pius5c055462016-10-11 08:27:27 -0700410Return<void> WifiChip::getApIface(const hidl_string& ifname,
411 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700412 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
413 &WifiChip::getApIfaceInternal, hidl_status_cb,
414 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700415}
416
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800417Return<void> WifiChip::removeApIface(const hidl_string& ifname,
418 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700419 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
420 &WifiChip::removeApIfaceInternal, hidl_status_cb,
421 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800422}
423
Roshan Pius5c055462016-10-11 08:27:27 -0700424Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700425 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
426 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700427}
428
Roshan Pius5c055462016-10-11 08:27:27 -0700429Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700430 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
431 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700432}
433
434Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700435 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700436 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
437 &WifiChip::getNanIfaceInternal, hidl_status_cb,
438 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700439}
440
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800441Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
442 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700443 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
444 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
445 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800446}
447
Roshan Pius5c055462016-10-11 08:27:27 -0700448Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700449 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
450 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700451}
452
Roshan Pius5c055462016-10-11 08:27:27 -0700453Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700454 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
455 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700456}
457
458Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700459 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700460 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
461 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
462 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700463}
464
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800465Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
466 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700467 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
468 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
469 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800470}
471
Roshan Pius5c055462016-10-11 08:27:27 -0700472Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700473 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
474 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700475}
476
Roshan Pius5c055462016-10-11 08:27:27 -0700477Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700478 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
479 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700480}
481
482Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700483 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700484 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
485 &WifiChip::getStaIfaceInternal, hidl_status_cb,
486 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700487}
488
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800489Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
490 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700491 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
492 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
493 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800494}
495
Roshan Pius5c055462016-10-11 08:27:27 -0700496Return<void> WifiChip::createRttController(
497 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700498 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
499 &WifiChip::createRttControllerInternal,
500 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700501}
502
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700503Return<void> WifiChip::getDebugRingBuffersStatus(
504 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700505 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
506 &WifiChip::getDebugRingBuffersStatusInternal,
507 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700508}
509
510Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700511 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
512 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700513 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700514 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
515 &WifiChip::startLoggingToDebugRingBufferInternal,
516 hidl_status_cb, ring_name, verbose_level,
517 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700518}
519
520Return<void> WifiChip::forceDumpToDebugRingBuffer(
521 const hidl_string& ring_name,
522 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700523 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
524 &WifiChip::forceDumpToDebugRingBufferInternal,
525 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700526}
527
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800528Return<void> WifiChip::stopLoggingToDebugRingBuffer(
529 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700530 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
531 &WifiChip::stopLoggingToDebugRingBufferInternal,
532 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800533}
534
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700535Return<void> WifiChip::getDebugHostWakeReasonStats(
536 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700537 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
538 &WifiChip::getDebugHostWakeReasonStatsInternal,
539 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700540}
541
Roshan Pius203cb032016-12-14 17:41:20 -0800542Return<void> WifiChip::enableDebugErrorAlerts(
543 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700544 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
545 &WifiChip::enableDebugErrorAlertsInternal,
546 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800547}
548
Roshan Pius735ff432017-07-25 08:48:08 -0700549Return<void> WifiChip::selectTxPowerScenario(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700550 V1_1::IWifiChip::TxPowerScenario scenario,
551 selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700552 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
553 &WifiChip::selectTxPowerScenarioInternal,
554 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700555}
556
Roshan Pius735ff432017-07-25 08:48:08 -0700557Return<void> WifiChip::resetTxPowerScenario(
558 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700559 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
560 &WifiChip::resetTxPowerScenarioInternal,
561 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700562}
563
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800564Return<void> WifiChip::registerEventCallback_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700565 const sp<V1_2::IWifiChipEventCallback>& event_callback,
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800566 registerEventCallback_cb hidl_status_cb) {
567 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
568 &WifiChip::registerEventCallbackInternal_1_2,
569 hidl_status_cb, event_callback);
570}
571
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800572Return<void> WifiChip::selectTxPowerScenario_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -0700573 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800574 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
Jong Wook Kimda830c92018-07-23 15:29:38 -0700575 &WifiChip::selectTxPowerScenarioInternal_1_2,
576 hidl_status_cb, scenario);
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800577}
578
xshu5899e8e2018-01-09 15:36:03 -0800579Return<void> WifiChip::debug(const hidl_handle& handle,
580 const hidl_vec<hidl_string>&) {
581 if (handle != nullptr && handle->numFds >= 1) {
582 int fd = handle->data[0];
583 if (!writeRingbufferFilesInternal()) {
584 LOG(ERROR) << "Error writing files to flash";
585 }
xshu4cb33162018-01-24 15:40:06 -0800586 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800587 if (n_error != 0) {
588 LOG(ERROR) << n_error << " errors occured in cpio function";
589 }
590 fsync(fd);
591 } else {
592 LOG(ERROR) << "File handle error";
593 }
594 return Void();
595}
596
Roshan Pius35d958c2016-10-06 16:47:38 -0700597void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700598 invalidateAndClearAll(ap_ifaces_);
599 invalidateAndClearAll(nan_ifaces_);
600 invalidateAndClearAll(p2p_ifaces_);
601 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700602 // Since all the ifaces are invalid now, all RTT controller objects
603 // using those ifaces also need to be invalidated.
604 for (const auto& rtt : rtt_controllers_) {
605 rtt->invalidate();
606 }
607 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700608}
609
Roshan Pius3c868522016-10-27 12:43:49 -0700610std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700611 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700612}
613
614WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800615 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
616 // Deprecated support for this callback.
617 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700618}
619
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700620std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700621 legacy_hal::wifi_error legacy_status;
622 uint32_t legacy_feature_set;
623 uint32_t legacy_logger_feature_set;
624 std::tie(legacy_status, legacy_feature_set) =
625 legacy_hal_.lock()->getSupportedFeatureSet(getWlan0IfaceName());
626 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
627 return {createWifiStatusFromLegacyError(legacy_status), 0};
628 }
629 std::tie(legacy_status, legacy_logger_feature_set) =
630 legacy_hal_.lock()->getLoggerSupportedFeatureSet(getWlan0IfaceName());
631 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius876220f2017-12-28 16:19:06 -0800632 // some devices don't support querying logger feature set
633 legacy_logger_feature_set = 0;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700634 }
635 uint32_t hidl_caps;
636 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
637 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
638 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
639 }
640 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700641}
642
Roshan Pius3c868522016-10-27 12:43:49 -0700643std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
644WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700645 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700646}
647
Roshan Piusba38d9c2017-12-08 07:32:08 -0800648WifiStatus WifiChip::configureChipInternal(
649 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
650 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700651 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700652 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
653 }
654 if (mode_id == current_mode_id_) {
655 LOG(DEBUG) << "Already in the specified mode " << mode_id;
656 return createWifiStatus(WifiStatusCode::SUCCESS);
657 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800658 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700659 if (status.code != WifiStatusCode::SUCCESS) {
660 for (const auto& callback : event_cb_handler_.getCallbacks()) {
661 if (!callback->onChipReconfigureFailure(status).isOk()) {
662 LOG(ERROR)
663 << "Failed to invoke onChipReconfigureFailure callback";
664 }
665 }
666 return status;
667 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800668 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700669 if (!callback->onChipReconfigured(mode_id).isOk()) {
670 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
671 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800672 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700673 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800674 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800675 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700676}
677
678std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700679 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700680 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
681 current_mode_id_};
682 }
683 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700684}
685
686std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
687WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700688 IWifiChip::ChipDebugInfo result;
689 legacy_hal::wifi_error legacy_status;
690 std::string driver_desc;
691 std::tie(legacy_status, driver_desc) =
692 legacy_hal_.lock()->getDriverVersion(getWlan0IfaceName());
693 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
694 LOG(ERROR) << "Failed to get driver version: "
695 << legacyErrorToString(legacy_status);
696 WifiStatus status = createWifiStatusFromLegacyError(
697 legacy_status, "failed to get driver version");
698 return {status, result};
699 }
700 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700701
Roshan Piusabcf78f2017-10-06 16:30:38 -0700702 std::string firmware_desc;
703 std::tie(legacy_status, firmware_desc) =
704 legacy_hal_.lock()->getFirmwareVersion(getWlan0IfaceName());
705 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
706 LOG(ERROR) << "Failed to get firmware version: "
707 << legacyErrorToString(legacy_status);
708 WifiStatus status = createWifiStatusFromLegacyError(
709 legacy_status, "failed to get firmware version");
710 return {status, result};
711 }
712 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700713
Roshan Piusabcf78f2017-10-06 16:30:38 -0700714 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700715}
716
717std::pair<WifiStatus, std::vector<uint8_t>>
718WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700719 legacy_hal::wifi_error legacy_status;
720 std::vector<uint8_t> driver_dump;
721 std::tie(legacy_status, driver_dump) =
722 legacy_hal_.lock()->requestDriverMemoryDump(getWlan0IfaceName());
723 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
724 LOG(ERROR) << "Failed to get driver debug dump: "
725 << legacyErrorToString(legacy_status);
726 return {createWifiStatusFromLegacyError(legacy_status),
727 std::vector<uint8_t>()};
728 }
729 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700730}
731
732std::pair<WifiStatus, std::vector<uint8_t>>
733WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700734 legacy_hal::wifi_error legacy_status;
735 std::vector<uint8_t> firmware_dump;
736 std::tie(legacy_status, firmware_dump) =
737 legacy_hal_.lock()->requestFirmwareMemoryDump(getWlan0IfaceName());
738 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
739 LOG(ERROR) << "Failed to get firmware debug dump: "
740 << legacyErrorToString(legacy_status);
741 return {createWifiStatusFromLegacyError(legacy_status), {}};
742 }
743 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700744}
745
746std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700747 if (!canCurrentModeSupportIfaceOfType(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700748 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800749 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700750 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700751 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_);
752 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700753 for (const auto& callback : event_cb_handler_.getCallbacks()) {
754 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
755 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
756 }
757 }
Roshan Pius675609b2017-10-31 14:24:58 -0700758 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700759}
760
761std::pair<WifiStatus, std::vector<hidl_string>>
762WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700763 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700764 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
765 }
Roshan Pius675609b2017-10-31 14:24:58 -0700766 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700767}
768
769std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800770 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700771 const auto iface = findUsingName(ap_ifaces_, ifname);
772 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700773 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
774 }
Roshan Pius675609b2017-10-31 14:24:58 -0700775 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700776}
777
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800778WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700779 const auto iface = findUsingName(ap_ifaces_, ifname);
780 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700781 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800782 }
Roshan Pius675609b2017-10-31 14:24:58 -0700783 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700784 for (const auto& callback : event_cb_handler_.getCallbacks()) {
785 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
786 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
787 }
788 }
789 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800790}
791
Roshan Pius3c868522016-10-27 12:43:49 -0700792std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700793 if (!canCurrentModeSupportIfaceOfType(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700794 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800795 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700796 // These are still assumed to be based on wlan0.
Roshan Piuscc338202017-11-02 13:54:09 -0700797 std::string ifname = getWlan0IfaceName();
798 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_);
799 nan_ifaces_.push_back(iface);
800 for (const auto& callback : event_cb_handler_.getCallbacks()) {
801 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
802 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
803 }
804 }
805 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700806}
807
808std::pair<WifiStatus, std::vector<hidl_string>>
809WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700810 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700811 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
812 }
Roshan Pius675609b2017-10-31 14:24:58 -0700813 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700814}
815
816std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800817 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700818 const auto iface = findUsingName(nan_ifaces_, ifname);
819 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700820 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
821 }
Roshan Pius675609b2017-10-31 14:24:58 -0700822 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700823}
824
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800825WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700826 const auto iface = findUsingName(nan_ifaces_, ifname);
827 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700828 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800829 }
Roshan Pius675609b2017-10-31 14:24:58 -0700830 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700831 for (const auto& callback : event_cb_handler_.getCallbacks()) {
832 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
833 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
834 }
835 }
836 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800837}
838
Roshan Pius3c868522016-10-27 12:43:49 -0700839std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700840 if (!canCurrentModeSupportIfaceOfType(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700841 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800842 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700843 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700844 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
845 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700846 for (const auto& callback : event_cb_handler_.getCallbacks()) {
847 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
848 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
849 }
850 }
Roshan Pius675609b2017-10-31 14:24:58 -0700851 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700852}
853
854std::pair<WifiStatus, std::vector<hidl_string>>
855WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700856 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700857 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
858 }
Roshan Pius675609b2017-10-31 14:24:58 -0700859 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700860}
861
862std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800863 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700864 const auto iface = findUsingName(p2p_ifaces_, ifname);
865 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700866 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
867 }
Roshan Pius675609b2017-10-31 14:24:58 -0700868 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700869}
870
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800871WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700872 const auto iface = findUsingName(p2p_ifaces_, ifname);
873 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700874 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800875 }
Roshan Pius675609b2017-10-31 14:24:58 -0700876 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700877 for (const auto& callback : event_cb_handler_.getCallbacks()) {
878 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
879 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
880 }
881 }
882 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800883}
884
Roshan Pius3c868522016-10-27 12:43:49 -0700885std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700886 if (!canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700887 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800888 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700889 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700890 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_);
891 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700892 for (const auto& callback : event_cb_handler_.getCallbacks()) {
893 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
894 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
895 }
896 }
Roshan Pius675609b2017-10-31 14:24:58 -0700897 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700898}
899
900std::pair<WifiStatus, std::vector<hidl_string>>
901WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700902 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700903 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
904 }
Roshan Pius675609b2017-10-31 14:24:58 -0700905 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700906}
907
908std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800909 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700910 const auto iface = findUsingName(sta_ifaces_, ifname);
911 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700912 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
913 }
Roshan Pius675609b2017-10-31 14:24:58 -0700914 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700915}
916
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800917WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700918 const auto iface = findUsingName(sta_ifaces_, ifname);
919 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700920 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800921 }
Roshan Pius675609b2017-10-31 14:24:58 -0700922 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700923 for (const auto& callback : event_cb_handler_.getCallbacks()) {
924 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
925 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
926 }
927 }
928 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800929}
930
Roshan Pius3c868522016-10-27 12:43:49 -0700931std::pair<WifiStatus, sp<IWifiRttController>>
932WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
Etan Cohen7240a1a2018-08-29 08:14:09 -0700933 if (sta_ifaces_.size() == 0 &&
934 !canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
935 LOG(ERROR) << "createRttControllerInternal: Chip cannot support STAs "
936 "(and RTT by extension)";
937 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
938 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700939 sp<WifiRttController> rtt =
940 new WifiRttController(getWlan0IfaceName(), bound_iface, legacy_hal_);
941 rtt_controllers_.emplace_back(rtt);
942 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
Roshan Pius3c868522016-10-27 12:43:49 -0700943}
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700944
945std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
946WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700947 legacy_hal::wifi_error legacy_status;
948 std::vector<legacy_hal::wifi_ring_buffer_status>
949 legacy_ring_buffer_status_vec;
950 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
951 legacy_hal_.lock()->getRingBuffersStatus(getWlan0IfaceName());
952 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
953 return {createWifiStatusFromLegacyError(legacy_status), {}};
954 }
955 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
956 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
957 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
958 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
959 }
960 return {createWifiStatus(WifiStatusCode::SUCCESS),
961 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700962}
963
964WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700965 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
966 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
967 WifiStatus status = registerDebugRingBufferCallback();
968 if (status.code != WifiStatusCode::SUCCESS) {
969 return status;
970 }
971 legacy_hal::wifi_error legacy_status =
972 legacy_hal_.lock()->startRingBufferLogging(
973 getWlan0IfaceName(), ring_name,
974 static_cast<
975 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
976 verbose_level),
977 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -0800978 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
979 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusabcf78f2017-10-06 16:30:38 -0700980 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700981}
982
983WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -0800984 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700985 WifiStatus status = registerDebugRingBufferCallback();
986 if (status.code != WifiStatusCode::SUCCESS) {
987 return status;
988 }
989 legacy_hal::wifi_error legacy_status =
990 legacy_hal_.lock()->getRingBufferData(getWlan0IfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -0800991
Roshan Piusabcf78f2017-10-06 16:30:38 -0700992 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700993}
994
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800995WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700996 legacy_hal::wifi_error legacy_status =
997 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
998 getWlan0IfaceName());
999 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -08001000}
1001
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001002std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
1003WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001004 legacy_hal::wifi_error legacy_status;
1005 legacy_hal::WakeReasonStats legacy_stats;
1006 std::tie(legacy_status, legacy_stats) =
1007 legacy_hal_.lock()->getWakeReasonStats(getWlan0IfaceName());
1008 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1009 return {createWifiStatusFromLegacyError(legacy_status), {}};
1010 }
1011 WifiDebugHostWakeReasonStats hidl_stats;
1012 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
1013 &hidl_stats)) {
1014 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
1015 }
1016 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -07001017}
1018
Roshan Pius203cb032016-12-14 17:41:20 -08001019WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001020 legacy_hal::wifi_error legacy_status;
1021 if (enable) {
1022 android::wp<WifiChip> weak_ptr_this(this);
1023 const auto& on_alert_callback = [weak_ptr_this](
1024 int32_t error_code,
1025 std::vector<uint8_t> debug_data) {
1026 const auto shared_ptr_this = weak_ptr_this.promote();
1027 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1028 LOG(ERROR) << "Callback invoked on an invalid object";
1029 return;
1030 }
1031 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1032 if (!callback->onDebugErrorAlert(error_code, debug_data)
1033 .isOk()) {
1034 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1035 }
1036 }
1037 };
1038 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -07001039 getWlan0IfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001040 } else {
1041 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -07001042 getWlan0IfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001043 }
1044 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001045}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001046
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001047WifiStatus WifiChip::selectTxPowerScenarioInternal(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001048 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001049 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1050 getWlan0IfaceName(),
1051 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1052 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001053}
1054
Roshan Pius735ff432017-07-25 08:48:08 -07001055WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001056 auto legacy_status =
1057 legacy_hal_.lock()->resetTxPowerScenario(getWlan0IfaceName());
1058 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001059}
1060
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001061WifiStatus WifiChip::registerEventCallbackInternal_1_2(
Jong Wook Kimda830c92018-07-23 15:29:38 -07001062 const sp<V1_2::IWifiChipEventCallback>& event_callback) {
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001063 if (!event_cb_handler_.addCallback(event_callback)) {
1064 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1065 }
1066 return createWifiStatus(WifiStatusCode::SUCCESS);
1067}
1068
Jong Wook Kimda830c92018-07-23 15:29:38 -07001069WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(
1070 TxPowerScenario scenario) {
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001071 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1072 getWlan0IfaceName(),
1073 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1074 return createWifiStatusFromLegacyError(legacy_status);
1075}
1076
Roshan Piusba38d9c2017-12-08 07:32:08 -08001077WifiStatus WifiChip::handleChipConfiguration(
1078 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1079 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001080 // If the chip is already configured in a different mode, stop
1081 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001082 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001083 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1084 << " to mode " << mode_id;
1085 invalidateAndRemoveAllIfaces();
1086 legacy_hal::wifi_error legacy_status =
1087 legacy_hal_.lock()->stop(lock, []() {});
1088 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1089 LOG(ERROR) << "Failed to stop legacy HAL: "
1090 << legacyErrorToString(legacy_status);
1091 return createWifiStatusFromLegacyError(legacy_status);
1092 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001093 }
Roshan Piuscc338202017-11-02 13:54:09 -07001094 // Firmware mode change not needed for V2 devices.
1095 bool success = true;
1096 if (mode_id == kV1StaChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001097 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Roshan Piuscc338202017-11-02 13:54:09 -07001098 } else if (mode_id == kV1ApChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001099 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1100 }
1101 if (!success) {
1102 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1103 }
1104 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1105 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1106 LOG(ERROR) << "Failed to start legacy HAL: "
1107 << legacyErrorToString(legacy_status);
1108 return createWifiStatusFromLegacyError(legacy_status);
1109 }
Roshan Pius85c64412018-01-22 17:58:40 -08001110 // Every time the HAL is restarted, we need to register the
1111 // radio mode change callback.
1112 WifiStatus status = registerRadioModeChangeCallback();
1113 if (status.code != WifiStatusCode::SUCCESS) {
1114 // This probably is not a critical failure?
1115 LOG(ERROR) << "Failed to register radio mode change callback";
1116 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001117 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001118}
Roshan Pius48185b22016-12-15 19:10:30 -08001119
1120WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001121 if (debug_ring_buffer_cb_registered_) {
1122 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001123 }
Roshan Pius3797e182017-03-30 18:01:54 -07001124
Roshan Piusabcf78f2017-10-06 16:30:38 -07001125 android::wp<WifiChip> weak_ptr_this(this);
1126 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001127 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001128 const std::vector<uint8_t>& data,
1129 const legacy_hal::wifi_ring_buffer_status& status) {
1130 const auto shared_ptr_this = weak_ptr_this.promote();
1131 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1132 LOG(ERROR) << "Callback invoked on an invalid object";
1133 return;
1134 }
1135 WifiDebugRingBufferStatus hidl_status;
1136 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1137 status, &hidl_status)) {
1138 LOG(ERROR) << "Error converting ring buffer status";
1139 return;
1140 }
xshu5899e8e2018-01-09 15:36:03 -08001141 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1142 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1143 Ringbuffer& cur_buffer = target->second;
1144 cur_buffer.append(data);
1145 } else {
1146 LOG(ERROR) << "Ringname " << name << " not found";
1147 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001148 }
1149 };
1150 legacy_hal::wifi_error legacy_status =
1151 legacy_hal_.lock()->registerRingBufferCallbackHandler(
1152 getWlan0IfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001153
Roshan Piusabcf78f2017-10-06 16:30:38 -07001154 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1155 debug_ring_buffer_cb_registered_ = true;
1156 }
1157 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001158}
1159
Roshan Pius85c64412018-01-22 17:58:40 -08001160WifiStatus WifiChip::registerRadioModeChangeCallback() {
1161 android::wp<WifiChip> weak_ptr_this(this);
1162 const auto& on_radio_mode_change_callback =
1163 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1164 const auto shared_ptr_this = weak_ptr_this.promote();
1165 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1166 LOG(ERROR) << "Callback invoked on an invalid object";
1167 return;
1168 }
Jong Wook Kimda830c92018-07-23 15:29:38 -07001169 std::vector<V1_2::IWifiChipEventCallback::RadioModeInfo>
Roshan Pius85c64412018-01-22 17:58:40 -08001170 hidl_radio_mode_infos;
1171 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1172 mac_infos, &hidl_radio_mode_infos)) {
1173 LOG(ERROR) << "Error converting wifi mac info";
1174 return;
1175 }
1176 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1177 if (!callback->onRadioModeChange(hidl_radio_mode_infos)
1178 .isOk()) {
1179 LOG(ERROR) << "Failed to invoke onRadioModeChange"
1180 << " callback on: " << toString(callback);
1181 }
1182 }
1183 };
1184 legacy_hal::wifi_error legacy_status =
1185 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1186 getWlan0IfaceName(), on_radio_mode_change_callback);
1187 return createWifiStatusFromLegacyError(legacy_status);
1188}
1189
Roshan Piuscc338202017-11-02 13:54:09 -07001190void WifiChip::populateModes() {
1191 // The chip combination supported for current devices is fixed.
1192 // They can be one of the following based on device features:
1193 // a) 2 separate modes of operation with 1 interface combination each:
1194 // Mode 1 (STA mode): Will support 1 STA and 1 P2P or NAN(optional)
1195 // concurrent iface operations.
1196 // Mode 2 (AP mode): Will support 1 AP iface operation.
1197 //
1198 // b) 1 mode of operation with 2 interface combinations
1199 // (conditional on isDualInterfaceSupported()):
1200 // Interface Combination 1: Will support 1 STA and 1 P2P or NAN(optional)
1201 // concurrent iface operations.
Roshan Piusaceecb02018-03-01 14:54:26 -08001202 // Interface Combination 2: Will support 1 STA and 1 AP concurrent
Roshan Piuscc338202017-11-02 13:54:09 -07001203 // iface operations.
1204 // If Aware is enabled (conditional on isAwareSupported()), the iface
1205 // combination will be modified to support either P2P or NAN in place of
1206 // just P2P.
1207 if (feature_flags_.lock()->isDualInterfaceSupported()) {
1208 // V2 Iface combinations for Mode Id = 2.
1209 const IWifiChip::ChipIfaceCombinationLimit
1210 chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1211 const IWifiChip::ChipIfaceCombinationLimit
Roshan Piusaceecb02018-03-01 14:54:26 -08001212 chip_iface_combination_limit_2 = {{IfaceType::AP}, 1};
Roshan Piuscc338202017-11-02 13:54:09 -07001213 IWifiChip::ChipIfaceCombinationLimit chip_iface_combination_limit_3;
1214 if (feature_flags_.lock()->isAwareSupported()) {
1215 chip_iface_combination_limit_3 = {{IfaceType::P2P, IfaceType::NAN},
1216 1};
1217 } else {
1218 chip_iface_combination_limit_3 = {{IfaceType::P2P}, 1};
1219 }
1220 const IWifiChip::ChipIfaceCombination chip_iface_combination_1 = {
1221 {chip_iface_combination_limit_1, chip_iface_combination_limit_2}};
1222 const IWifiChip::ChipIfaceCombination chip_iface_combination_2 = {
1223 {chip_iface_combination_limit_1, chip_iface_combination_limit_3}};
Nickc52e8082018-05-02 13:54:28 -07001224 if (feature_flags_.lock()->isApDisabled()) {
Jong Wook Kimda830c92018-07-23 15:29:38 -07001225 const IWifiChip::ChipMode chip_mode = {kV2ChipModeId,
1226 {chip_iface_combination_2}};
1227 modes_ = {chip_mode};
Nickc52e8082018-05-02 13:54:28 -07001228 } else {
Jong Wook Kimda830c92018-07-23 15:29:38 -07001229 const IWifiChip::ChipMode chip_mode = {
1230 kV2ChipModeId,
1231 {chip_iface_combination_1, chip_iface_combination_2}};
1232 modes_ = {chip_mode};
Nickc52e8082018-05-02 13:54:28 -07001233 }
Roshan Piuscc338202017-11-02 13:54:09 -07001234 } else {
1235 // V1 Iface combinations for Mode Id = 0. (STA Mode)
1236 const IWifiChip::ChipIfaceCombinationLimit
1237 sta_chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1238 IWifiChip::ChipIfaceCombinationLimit sta_chip_iface_combination_limit_2;
1239 if (feature_flags_.lock()->isAwareSupported()) {
1240 sta_chip_iface_combination_limit_2 = {
1241 {IfaceType::P2P, IfaceType::NAN}, 1};
1242 } else {
1243 sta_chip_iface_combination_limit_2 = {{IfaceType::P2P}, 1};
1244 }
1245 const IWifiChip::ChipIfaceCombination sta_chip_iface_combination = {
1246 {sta_chip_iface_combination_limit_1,
1247 sta_chip_iface_combination_limit_2}};
1248 const IWifiChip::ChipMode sta_chip_mode = {
1249 kV1StaChipModeId, {sta_chip_iface_combination}};
1250 // Iface combinations for Mode Id = 1. (AP Mode)
1251 const IWifiChip::ChipIfaceCombinationLimit
1252 ap_chip_iface_combination_limit = {{IfaceType::AP}, 1};
1253 const IWifiChip::ChipIfaceCombination ap_chip_iface_combination = {
1254 {ap_chip_iface_combination_limit}};
1255 const IWifiChip::ChipMode ap_chip_mode = {kV1ApChipModeId,
1256 {ap_chip_iface_combination}};
Nickc52e8082018-05-02 13:54:28 -07001257 if (feature_flags_.lock()->isApDisabled()) {
Jong Wook Kimda830c92018-07-23 15:29:38 -07001258 modes_ = {sta_chip_mode};
Nickc52e8082018-05-02 13:54:28 -07001259 } else {
Jong Wook Kimda830c92018-07-23 15:29:38 -07001260 modes_ = {sta_chip_mode, ap_chip_mode};
Nickc52e8082018-05-02 13:54:28 -07001261 }
Roshan Piuscc338202017-11-02 13:54:09 -07001262 }
1263}
1264
1265std::vector<IWifiChip::ChipIfaceCombination>
1266WifiChip::getCurrentModeIfaceCombinations() {
1267 if (!isValidModeId(current_mode_id_)) {
1268 LOG(ERROR) << "Chip not configured in a mode yet";
1269 return {};
1270 }
1271 for (const auto& mode : modes_) {
1272 if (mode.id == current_mode_id_) {
1273 return mode.availableCombinations;
1274 }
1275 }
1276 CHECK(0) << "Expected to find iface combinations for current mode!";
1277 return {};
1278}
1279
1280// Returns a map indexed by IfaceType with the number of ifaces currently
1281// created of the corresponding type.
1282std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1283 std::map<IfaceType, size_t> iface_counts;
1284 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1285 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1286 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1287 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1288 return iface_counts;
1289}
1290
1291// This expands the provided iface combinations to a more parseable
1292// form. Returns a vector of available combinations possible with the number
1293// of ifaces of each type in the combination.
1294// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1295std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1296 const IWifiChip::ChipIfaceCombination& combination) {
1297 uint32_t num_expanded_combos = 1;
1298 for (const auto& limit : combination.limits) {
1299 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1300 num_expanded_combos *= limit.types.size();
1301 }
1302 }
1303
1304 // Allocate the vector of expanded combos and reset all iface counts to 0
1305 // in each combo.
1306 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1307 expanded_combos.resize(num_expanded_combos);
1308 for (auto& expanded_combo : expanded_combos) {
1309 for (const auto type :
1310 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1311 expanded_combo[type] = 0;
1312 }
1313 }
1314 uint32_t span = num_expanded_combos;
1315 for (const auto& limit : combination.limits) {
1316 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1317 span /= limit.types.size();
1318 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1319 const auto iface_type =
1320 limit.types[(k / span) % limit.types.size()];
1321 expanded_combos[k][iface_type]++;
1322 }
1323 }
1324 }
1325 return expanded_combos;
1326}
1327
1328bool WifiChip::canExpandedIfaceCombinationSupportIfaceOfType(
1329 const std::map<IfaceType, size_t>& combo, IfaceType requested_type) {
1330 const auto current_combo = getCurrentIfaceCombination();
1331
1332 // Check if we have space for 1 more iface of |type| in this combo
1333 for (const auto type :
1334 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1335 size_t num_ifaces_needed = current_combo.at(type);
1336 if (type == requested_type) {
1337 num_ifaces_needed++;
1338 }
1339 size_t num_ifaces_allowed = combo.at(type);
1340 if (num_ifaces_needed > num_ifaces_allowed) {
1341 return false;
1342 }
1343 }
1344 return true;
1345}
1346
1347// This method does the following:
1348// a) Enumerate all possible iface combos by expanding the current
1349// ChipIfaceCombination.
1350// b) Check if the requested iface type can be added to the current mode.
1351bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType type) {
1352 if (!isValidModeId(current_mode_id_)) {
1353 LOG(ERROR) << "Chip not configured in a mode yet";
1354 return false;
1355 }
1356 const auto combinations = getCurrentModeIfaceCombinations();
1357 for (const auto& combination : combinations) {
1358 const auto expanded_combos = expandIfaceCombinations(combination);
1359 for (const auto& expanded_combo : expanded_combos) {
1360 if (canExpandedIfaceCombinationSupportIfaceOfType(expanded_combo,
1361 type)) {
1362 return true;
1363 }
1364 }
1365 }
1366 return false;
1367}
1368
1369bool WifiChip::isValidModeId(ChipModeId mode_id) {
1370 for (const auto& mode : modes_) {
1371 if (mode.id == mode_id) {
1372 return true;
1373 }
1374 }
1375 return false;
1376}
1377
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001378// Return "wlan0", if "wlan0" is not already in use, else return "wlan1".
1379// This is based on the assumption that we'll have a max of 2 concurrent
1380// AP/STA ifaces.
1381std::string WifiChip::allocateApOrStaIfaceName() {
1382 auto ap_iface = findUsingName(ap_ifaces_, getWlan0IfaceName());
1383 auto sta_iface = findUsingName(sta_ifaces_, getWlan0IfaceName());
1384 if (!ap_iface.get() && !sta_iface.get()) {
1385 return getWlan0IfaceName();
1386 }
1387 ap_iface = findUsingName(ap_ifaces_, getWlan1IfaceName());
1388 sta_iface = findUsingName(sta_ifaces_, getWlan1IfaceName());
1389 if (!ap_iface.get() && !sta_iface.get()) {
1390 return getWlan1IfaceName();
1391 }
1392 // This should never happen. We screwed up somewhere if it did.
1393 CHECK(0) << "wlan0 and wlan1 in use already!";
1394 return {};
1395}
1396
xshu5899e8e2018-01-09 15:36:03 -08001397bool WifiChip::writeRingbufferFilesInternal() {
1398 if (!removeOldFilesInternal()) {
1399 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1400 return false;
1401 }
1402 // write ringbuffers to file
1403 for (const auto& item : ringbuffer_map_) {
1404 const Ringbuffer& cur_buffer = item.second;
1405 if (cur_buffer.getData().empty()) {
1406 continue;
1407 }
1408 const std::string file_path_raw =
1409 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1410 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1411 if (dump_fd == -1) {
1412 LOG(ERROR) << "create file failed: " << strerror(errno);
1413 return false;
1414 }
1415 unique_fd file_auto_closer(dump_fd);
1416 for (const auto& cur_block : cur_buffer.getData()) {
1417 if (write(dump_fd, cur_block.data(),
1418 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1419 LOG(ERROR) << "Error writing to file " << strerror(errno);
1420 }
1421 }
1422 }
1423 return true;
1424}
1425
Roshan Pius79a99752016-10-04 13:03:58 -07001426} // namespace implementation
Jong Wook Kimda830c92018-07-23 15:29:38 -07001427} // namespace V1_3
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001428} // namespace wifi
1429} // namespace hardware
1430} // namespace android