blob: 15f8cf3a95c13070f8b1e9be3980d7d557b4855b [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 {
xshu5899e8e2018-01-09 15:36:03 -080031using android::base::unique_fd;
Roshan Pius35d958c2016-10-06 16:47:38 -070032using android::hardware::hidl_string;
Roshan Piusabcf78f2017-10-06 16:30:38 -070033using android::hardware::hidl_vec;
Roshan Pius2c06a3f2016-12-15 17:51:40 -080034using android::hardware::wifi::V1_0::ChipModeId;
Roshan Pius52947fb2016-11-18 11:38:07 -080035using android::hardware::wifi::V1_0::IfaceType;
Roshan Pius675609b2017-10-31 14:24:58 -070036using android::hardware::wifi::V1_0::IWifiChip;
Roshan Piusabcf78f2017-10-06 16:30:38 -070037using android::sp;
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;
51constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/";
52
Roshan Pius35d958c2016-10-06 16:47:38 -070053template <typename Iface>
Roshan Pius675609b2017-10-31 14:24:58 -070054void invalidateAndClear(std::vector<sp<Iface>>& ifaces, sp<Iface> iface) {
55 iface->invalidate();
56 ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface),
57 ifaces.end());
58}
59
60template <typename Iface>
61void invalidateAndClearAll(std::vector<sp<Iface>>& ifaces) {
62 for (const auto& iface : ifaces) {
Roshan Piusabcf78f2017-10-06 16:30:38 -070063 iface->invalidate();
Roshan Piusabcf78f2017-10-06 16:30:38 -070064 }
Roshan Pius675609b2017-10-31 14:24:58 -070065 ifaces.clear();
66}
67
68template <typename Iface>
69std::vector<hidl_string> getNames(std::vector<sp<Iface>>& ifaces) {
70 std::vector<hidl_string> names;
71 for (const auto& iface : ifaces) {
72 names.emplace_back(iface->getName());
73 }
74 return names;
75}
76
77template <typename Iface>
78sp<Iface> findUsingName(std::vector<sp<Iface>>& ifaces,
79 const std::string& name) {
80 std::vector<hidl_string> names;
81 for (const auto& iface : ifaces) {
82 if (name == iface->getName()) {
83 return iface;
84 }
85 }
86 return nullptr;
Roshan Pius35d958c2016-10-06 16:47:38 -070087}
Roshan Pius9377a0d2017-10-06 13:18:54 -070088
89std::string getWlan0IfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -070090 std::array<char, PROPERTY_VALUE_MAX> buffer;
91 property_get("wifi.interface", buffer.data(), "wlan0");
92 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -070093}
94
Roshan Pius9377a0d2017-10-06 13:18:54 -070095std::string getWlan1IfaceName() {
Roshan Pius8e3c7ef2017-11-03 09:43:08 -070096 std::array<char, PROPERTY_VALUE_MAX> buffer;
97 property_get("wifi.concurrent.interface", buffer.data(), "wlan1");
98 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -070099}
Roshan Pius9377a0d2017-10-06 13:18:54 -0700100
101std::string getP2pIfaceName() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700102 std::array<char, PROPERTY_VALUE_MAX> buffer;
103 property_get("wifi.direct.interface", buffer.data(), "p2p0");
104 return buffer.data();
Roshan Pius9377a0d2017-10-06 13:18:54 -0700105}
106
xshu5899e8e2018-01-09 15:36:03 -0800107// delete files older than a predefined time in the wifi tombstone dir
108bool removeOldFilesInternal() {
109 time_t now = time(0);
110 const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds;
111 DIR* dir_dump = opendir(kTombstoneFolderPath);
112 if (!dir_dump) {
113 LOG(ERROR) << "Failed to open directory: " << strerror(errno);
114 return false;
115 }
116 unique_fd dir_auto_closer(dirfd(dir_dump));
117 struct dirent* dp;
118 bool success = true;
119 while ((dp = readdir(dir_dump))) {
120 if (dp->d_type != DT_REG) {
121 continue;
122 }
123 std::string cur_file_name(dp->d_name);
124 struct stat cur_file_stat;
125 std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800126 if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) {
xshu5899e8e2018-01-09 15:36:03 -0800127 LOG(ERROR) << "Failed to get file stat for " << cur_file_path
128 << ": " << strerror(errno);
129 success = false;
xshu4cb33162018-01-24 15:40:06 -0800130 continue;
131 }
132 if (cur_file_stat.st_mtime >= delete_files_before) {
133 continue;
134 }
135 if (unlink(cur_file_path.c_str()) != 0) {
136 LOG(ERROR) << "Error deleting file " << strerror(errno);
137 success = false;
xshu5899e8e2018-01-09 15:36:03 -0800138 }
139 }
140 return success;
141}
142
xshu4cb33162018-01-24 15:40:06 -0800143// Helper function for |cpioArchiveFilesInDir|
144bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name,
145 size_t file_name_len) {
146 std::array<char, 32 * 1024> read_buf;
147 ssize_t llen =
148 sprintf(read_buf.data(),
149 "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X",
150 kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid,
151 st.st_gid, static_cast<int>(st.st_nlink),
152 static_cast<int>(st.st_mtime), static_cast<int>(st.st_size),
153 major(st.st_dev), minor(st.st_dev), major(st.st_rdev),
154 minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0);
155 if (write(out_fd, read_buf.data(), llen) == -1) {
156 LOG(ERROR) << "Error writing cpio header to file " << file_name << " "
157 << strerror(errno);
158 return false;
159 }
160 if (write(out_fd, file_name, file_name_len) == -1) {
161 LOG(ERROR) << "Error writing filename to file " << file_name << " "
162 << strerror(errno);
163 return false;
164 }
165
166 // NUL Pad header up to 4 multiple bytes.
167 llen = (llen + file_name_len) % 4;
168 if (llen != 0) {
169 const uint32_t zero = 0;
170 if (write(out_fd, &zero, 4 - llen) == -1) {
171 LOG(ERROR) << "Error padding 0s to file " << file_name << " "
172 << strerror(errno);
173 return false;
174 }
175 }
176 return true;
177}
178
179// Helper function for |cpioArchiveFilesInDir|
180size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) {
181 // writing content of file
182 std::array<char, 32 * 1024> read_buf;
183 ssize_t llen = st.st_size;
184 size_t n_error = 0;
185 while (llen > 0) {
186 ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size());
187 if (bytes_read == -1) {
188 LOG(ERROR) << "Error reading file " << strerror(errno);
189 return ++n_error;
190 }
191 llen -= bytes_read;
192 if (write(out_fd, read_buf.data(), bytes_read) == -1) {
193 LOG(ERROR) << "Error writing data to file " << strerror(errno);
194 return ++n_error;
195 }
196 if (bytes_read == 0) { // this should never happen, but just in case
197 // to unstuck from while loop
198 LOG(ERROR) << "Unexpected read result for " << strerror(errno);
199 n_error++;
200 break;
201 }
202 }
203 llen = st.st_size % 4;
204 if (llen != 0) {
205 const uint32_t zero = 0;
206 if (write(out_fd, &zero, 4 - llen) == -1) {
207 LOG(ERROR) << "Error padding 0s to file " << strerror(errno);
208 return ++n_error;
209 }
210 }
211 return n_error;
212}
213
214// Helper function for |cpioArchiveFilesInDir|
215bool cpioWriteFileTrailer(int out_fd) {
216 std::array<char, 4096> read_buf;
217 read_buf.fill(0);
218 if (write(out_fd, read_buf.data(),
219 sprintf(read_buf.data(), "070701%040X%056X%08XTRAILER!!!", 1,
220 0x0b, 0) +
221 4) == -1) {
222 LOG(ERROR) << "Error writing trailing bytes " << strerror(errno);
223 return false;
224 }
225 return true;
226}
227
xshu5899e8e2018-01-09 15:36:03 -0800228// Archives all files in |input_dir| and writes result into |out_fd|
229// Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive"
230// portion
xshu4cb33162018-01-24 15:40:06 -0800231size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) {
xshu5899e8e2018-01-09 15:36:03 -0800232 struct dirent* dp;
233 size_t n_error = 0;
xshu5899e8e2018-01-09 15:36:03 -0800234 DIR* dir_dump = opendir(input_dir);
235 if (!dir_dump) {
236 LOG(ERROR) << "Failed to open directory: " << strerror(errno);
xshu4cb33162018-01-24 15:40:06 -0800237 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800238 }
239 unique_fd dir_auto_closer(dirfd(dir_dump));
240 while ((dp = readdir(dir_dump))) {
241 if (dp->d_type != DT_REG) {
242 continue;
243 }
244 std::string cur_file_name(dp->d_name);
xshu4cb33162018-01-24 15:40:06 -0800245 // string.size() does not include the null terminator. The cpio FreeBSD
246 // file header expects the null character to be included in the length.
247 const size_t file_name_len = cur_file_name.size() + 1;
xshu5899e8e2018-01-09 15:36:03 -0800248 struct stat st;
xshu5899e8e2018-01-09 15:36:03 -0800249 const std::string cur_file_path = kTombstoneFolderPath + cur_file_name;
xshu4cb33162018-01-24 15:40:06 -0800250 if (stat(cur_file_path.c_str(), &st) == -1) {
xshu5899e8e2018-01-09 15:36:03 -0800251 LOG(ERROR) << "Failed to get file stat for " << cur_file_path
252 << ": " << strerror(errno);
253 n_error++;
xshu4cb33162018-01-24 15:40:06 -0800254 continue;
255 }
256 const int fd_read = open(cur_file_path.c_str(), O_RDONLY);
257 if (fd_read == -1) {
258 LOG(ERROR) << "Failed to open file " << cur_file_path << " "
259 << strerror(errno);
260 n_error++;
261 continue;
262 }
263 unique_fd file_auto_closer(fd_read);
264 if (!cpioWriteHeader(out_fd, st, cur_file_name.c_str(),
265 file_name_len)) {
266 return ++n_error;
267 }
268 size_t write_error = cpioWriteFileContent(fd_read, out_fd, st);
269 if (write_error) {
270 return n_error + write_error;
xshu5899e8e2018-01-09 15:36:03 -0800271 }
272 }
xshu4cb33162018-01-24 15:40:06 -0800273 if (!cpioWriteFileTrailer(out_fd)) {
274 return ++n_error;
xshu5899e8e2018-01-09 15:36:03 -0800275 }
276 return n_error;
277}
278
279// Helper function to create a non-const char*.
280std::vector<char> makeCharVec(const std::string& str) {
281 std::vector<char> vec(str.size() + 1);
282 vec.assign(str.begin(), str.end());
283 vec.push_back('\0');
284 return vec;
285}
286
Roshan Piusabcf78f2017-10-06 16:30:38 -0700287} // namespace
Roshan Pius35d958c2016-10-06 16:47:38 -0700288
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700289namespace android {
290namespace hardware {
291namespace wifi {
Etan Cohen6ce50902017-09-14 07:30:57 -0700292namespace V1_2 {
Roshan Pius79a99752016-10-04 13:03:58 -0700293namespace implementation {
Roshan Pius3c868522016-10-27 12:43:49 -0700294using hidl_return_util::validateAndCall;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800295using hidl_return_util::validateAndCallWithLock;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700296
Roshan Pius52947fb2016-11-18 11:38:07 -0800297WifiChip::WifiChip(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700298 ChipId chip_id, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
Roshan Pius200a17d2017-11-01 13:03:35 -0700299 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
300 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags)
Roshan Pius52947fb2016-11-18 11:38:07 -0800301 : chip_id_(chip_id),
302 legacy_hal_(legacy_hal),
303 mode_controller_(mode_controller),
Roshan Pius200a17d2017-11-01 13:03:35 -0700304 feature_flags_(feature_flags),
Roshan Pius52947fb2016-11-18 11:38:07 -0800305 is_valid_(true),
Roshan Pius48185b22016-12-15 19:10:30 -0800306 current_mode_id_(kInvalidModeId),
Roshan Piuscc338202017-11-02 13:54:09 -0700307 debug_ring_buffer_cb_registered_(false) {
308 populateModes();
309}
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700310
Roshan Piusaabe5752016-09-29 09:03:59 -0700311void WifiChip::invalidate() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700312 invalidateAndRemoveAllIfaces();
313 legacy_hal_.reset();
314 event_cb_handler_.invalidate();
315 is_valid_ = false;
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700316}
317
Roshan Piusabcf78f2017-10-06 16:30:38 -0700318bool WifiChip::isValid() { return is_valid_; }
Roshan Pius3c868522016-10-27 12:43:49 -0700319
Roshan Piusd37341f2017-01-31 13:13:28 -0800320std::set<sp<IWifiChipEventCallback>> WifiChip::getEventCallbacks() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700321 return event_cb_handler_.getCallbacks();
Roshan Pius203cb032016-12-14 17:41:20 -0800322}
323
Roshan Pius5c055462016-10-11 08:27:27 -0700324Return<void> WifiChip::getId(getId_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700325 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
326 &WifiChip::getIdInternal, hidl_status_cb);
Roshan Piuscd566bd2016-10-10 08:03:42 -0700327}
328
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700329Return<void> WifiChip::registerEventCallback(
Roshan Pius5c055462016-10-11 08:27:27 -0700330 const sp<IWifiChipEventCallback>& event_callback,
331 registerEventCallback_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700332 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
333 &WifiChip::registerEventCallbackInternal,
334 hidl_status_cb, event_callback);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700335}
336
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700337Return<void> WifiChip::getCapabilities(getCapabilities_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700338 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
339 &WifiChip::getCapabilitiesInternal, hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700340}
341
Roshan Pius5c055462016-10-11 08:27:27 -0700342Return<void> WifiChip::getAvailableModes(getAvailableModes_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700343 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
344 &WifiChip::getAvailableModesInternal,
345 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700346}
347
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800348Return<void> WifiChip::configureChip(ChipModeId mode_id,
Roshan Pius5c055462016-10-11 08:27:27 -0700349 configureChip_cb hidl_status_cb) {
Roshan Piusba38d9c2017-12-08 07:32:08 -0800350 return validateAndCallWithLock(
351 this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
352 &WifiChip::configureChipInternal, hidl_status_cb, mode_id);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700353}
354
Roshan Pius5c055462016-10-11 08:27:27 -0700355Return<void> WifiChip::getMode(getMode_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700356 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
357 &WifiChip::getModeInternal, hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700358}
359
Roshan Pius5c055462016-10-11 08:27:27 -0700360Return<void> WifiChip::requestChipDebugInfo(
361 requestChipDebugInfo_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700362 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
363 &WifiChip::requestChipDebugInfoInternal,
364 hidl_status_cb);
Roshan Pius5c055462016-10-11 08:27:27 -0700365}
366
367Return<void> WifiChip::requestDriverDebugDump(
368 requestDriverDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700369 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
370 &WifiChip::requestDriverDebugDumpInternal,
371 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700372}
373
Roshan Pius5c055462016-10-11 08:27:27 -0700374Return<void> WifiChip::requestFirmwareDebugDump(
375 requestFirmwareDebugDump_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700376 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
377 &WifiChip::requestFirmwareDebugDumpInternal,
378 hidl_status_cb);
Roshan Pius3c4e8a32016-10-03 14:53:58 -0700379}
380
Roshan Pius5c055462016-10-11 08:27:27 -0700381Return<void> WifiChip::createApIface(createApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700382 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
383 &WifiChip::createApIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700384}
385
Roshan Pius5c055462016-10-11 08:27:27 -0700386Return<void> WifiChip::getApIfaceNames(getApIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700387 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
388 &WifiChip::getApIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700389}
390
Roshan Pius5c055462016-10-11 08:27:27 -0700391Return<void> WifiChip::getApIface(const hidl_string& ifname,
392 getApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700393 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
394 &WifiChip::getApIfaceInternal, hidl_status_cb,
395 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700396}
397
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800398Return<void> WifiChip::removeApIface(const hidl_string& ifname,
399 removeApIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700400 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
401 &WifiChip::removeApIfaceInternal, hidl_status_cb,
402 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800403}
404
Roshan Pius5c055462016-10-11 08:27:27 -0700405Return<void> WifiChip::createNanIface(createNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700406 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
407 &WifiChip::createNanIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700408}
409
Roshan Pius5c055462016-10-11 08:27:27 -0700410Return<void> WifiChip::getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700411 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
412 &WifiChip::getNanIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700413}
414
415Return<void> WifiChip::getNanIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700416 getNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700417 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
418 &WifiChip::getNanIfaceInternal, hidl_status_cb,
419 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700420}
421
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800422Return<void> WifiChip::removeNanIface(const hidl_string& ifname,
423 removeNanIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700424 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
425 &WifiChip::removeNanIfaceInternal, hidl_status_cb,
426 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800427}
428
Roshan Pius5c055462016-10-11 08:27:27 -0700429Return<void> WifiChip::createP2pIface(createP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700430 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
431 &WifiChip::createP2pIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700432}
433
Roshan Pius5c055462016-10-11 08:27:27 -0700434Return<void> WifiChip::getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700435 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
436 &WifiChip::getP2pIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700437}
438
439Return<void> WifiChip::getP2pIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700440 getP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700441 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
442 &WifiChip::getP2pIfaceInternal, hidl_status_cb,
443 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700444}
445
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800446Return<void> WifiChip::removeP2pIface(const hidl_string& ifname,
447 removeP2pIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700448 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
449 &WifiChip::removeP2pIfaceInternal, hidl_status_cb,
450 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800451}
452
Roshan Pius5c055462016-10-11 08:27:27 -0700453Return<void> WifiChip::createStaIface(createStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700454 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
455 &WifiChip::createStaIfaceInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700456}
457
Roshan Pius5c055462016-10-11 08:27:27 -0700458Return<void> WifiChip::getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700459 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
460 &WifiChip::getStaIfaceNamesInternal, hidl_status_cb);
Roshan Pius35d958c2016-10-06 16:47:38 -0700461}
462
463Return<void> WifiChip::getStaIface(const hidl_string& ifname,
Roshan Pius5c055462016-10-11 08:27:27 -0700464 getStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700465 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
466 &WifiChip::getStaIfaceInternal, hidl_status_cb,
467 ifname);
Roshan Pius35d958c2016-10-06 16:47:38 -0700468}
469
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800470Return<void> WifiChip::removeStaIface(const hidl_string& ifname,
471 removeStaIface_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700472 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
473 &WifiChip::removeStaIfaceInternal, hidl_status_cb,
474 ifname);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800475}
476
Roshan Pius5c055462016-10-11 08:27:27 -0700477Return<void> WifiChip::createRttController(
478 const sp<IWifiIface>& bound_iface, createRttController_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700479 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
480 &WifiChip::createRttControllerInternal,
481 hidl_status_cb, bound_iface);
Roshan Pius59268282016-10-06 20:23:47 -0700482}
483
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700484Return<void> WifiChip::getDebugRingBuffersStatus(
485 getDebugRingBuffersStatus_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700486 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
487 &WifiChip::getDebugRingBuffersStatusInternal,
488 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700489}
490
491Return<void> WifiChip::startLoggingToDebugRingBuffer(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700492 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
493 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700494 startLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700495 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
496 &WifiChip::startLoggingToDebugRingBufferInternal,
497 hidl_status_cb, ring_name, verbose_level,
498 max_interval_in_sec, min_data_size_in_bytes);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700499}
500
501Return<void> WifiChip::forceDumpToDebugRingBuffer(
502 const hidl_string& ring_name,
503 forceDumpToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700504 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
505 &WifiChip::forceDumpToDebugRingBufferInternal,
506 hidl_status_cb, ring_name);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700507}
508
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800509Return<void> WifiChip::stopLoggingToDebugRingBuffer(
510 stopLoggingToDebugRingBuffer_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700511 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
512 &WifiChip::stopLoggingToDebugRingBufferInternal,
513 hidl_status_cb);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800514}
515
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700516Return<void> WifiChip::getDebugHostWakeReasonStats(
517 getDebugHostWakeReasonStats_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700518 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
519 &WifiChip::getDebugHostWakeReasonStatsInternal,
520 hidl_status_cb);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700521}
522
Roshan Pius203cb032016-12-14 17:41:20 -0800523Return<void> WifiChip::enableDebugErrorAlerts(
524 bool enable, enableDebugErrorAlerts_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700525 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
526 &WifiChip::enableDebugErrorAlertsInternal,
527 hidl_status_cb, enable);
Roshan Pius203cb032016-12-14 17:41:20 -0800528}
529
Roshan Pius735ff432017-07-25 08:48:08 -0700530Return<void> WifiChip::selectTxPowerScenario(
531 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700532 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
533 &WifiChip::selectTxPowerScenarioInternal,
534 hidl_status_cb, scenario);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700535}
536
Roshan Pius735ff432017-07-25 08:48:08 -0700537Return<void> WifiChip::resetTxPowerScenario(
538 resetTxPowerScenario_cb hidl_status_cb) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700539 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
540 &WifiChip::resetTxPowerScenarioInternal,
541 hidl_status_cb);
Roshan Piusdbd83ef2017-06-20 12:05:40 -0700542}
543
xshu5899e8e2018-01-09 15:36:03 -0800544Return<void> WifiChip::debug(const hidl_handle& handle,
545 const hidl_vec<hidl_string>&) {
546 if (handle != nullptr && handle->numFds >= 1) {
547 int fd = handle->data[0];
548 if (!writeRingbufferFilesInternal()) {
549 LOG(ERROR) << "Error writing files to flash";
550 }
xshu4cb33162018-01-24 15:40:06 -0800551 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800552 if (n_error != 0) {
553 LOG(ERROR) << n_error << " errors occured in cpio function";
554 }
555 fsync(fd);
556 } else {
557 LOG(ERROR) << "File handle error";
558 }
559 return Void();
560}
561
Roshan Pius35d958c2016-10-06 16:47:38 -0700562void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700563 invalidateAndClearAll(ap_ifaces_);
564 invalidateAndClearAll(nan_ifaces_);
565 invalidateAndClearAll(p2p_ifaces_);
566 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700567 // Since all the ifaces are invalid now, all RTT controller objects
568 // using those ifaces also need to be invalidated.
569 for (const auto& rtt : rtt_controllers_) {
570 rtt->invalidate();
571 }
572 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700573}
574
Roshan Pius3c868522016-10-27 12:43:49 -0700575std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700576 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700577}
578
579WifiStatus WifiChip::registerEventCallbackInternal(
580 const sp<IWifiChipEventCallback>& event_callback) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700581 if (!event_cb_handler_.addCallback(event_callback)) {
582 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
583 }
584 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius3c868522016-10-27 12:43:49 -0700585}
586
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700587std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700588 legacy_hal::wifi_error legacy_status;
589 uint32_t legacy_feature_set;
590 uint32_t legacy_logger_feature_set;
591 std::tie(legacy_status, legacy_feature_set) =
592 legacy_hal_.lock()->getSupportedFeatureSet(getWlan0IfaceName());
593 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
594 return {createWifiStatusFromLegacyError(legacy_status), 0};
595 }
596 std::tie(legacy_status, legacy_logger_feature_set) =
597 legacy_hal_.lock()->getLoggerSupportedFeatureSet(getWlan0IfaceName());
598 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius876220f2017-12-28 16:19:06 -0800599 // some devices don't support querying logger feature set
600 legacy_logger_feature_set = 0;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700601 }
602 uint32_t hidl_caps;
603 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
604 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
605 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
606 }
607 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700608}
609
Roshan Pius3c868522016-10-27 12:43:49 -0700610std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
611WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700612 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700613}
614
Roshan Piusba38d9c2017-12-08 07:32:08 -0800615WifiStatus WifiChip::configureChipInternal(
616 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
617 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700618 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700619 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
620 }
621 if (mode_id == current_mode_id_) {
622 LOG(DEBUG) << "Already in the specified mode " << mode_id;
623 return createWifiStatus(WifiStatusCode::SUCCESS);
624 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800625 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700626 if (status.code != WifiStatusCode::SUCCESS) {
627 for (const auto& callback : event_cb_handler_.getCallbacks()) {
628 if (!callback->onChipReconfigureFailure(status).isOk()) {
629 LOG(ERROR)
630 << "Failed to invoke onChipReconfigureFailure callback";
631 }
632 }
633 return status;
634 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800635 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700636 if (!callback->onChipReconfigured(mode_id).isOk()) {
637 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
638 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800639 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700640 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800641 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800642 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700643}
644
645std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700646 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700647 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
648 current_mode_id_};
649 }
650 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700651}
652
653std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
654WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700655 IWifiChip::ChipDebugInfo result;
656 legacy_hal::wifi_error legacy_status;
657 std::string driver_desc;
658 std::tie(legacy_status, driver_desc) =
659 legacy_hal_.lock()->getDriverVersion(getWlan0IfaceName());
660 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
661 LOG(ERROR) << "Failed to get driver version: "
662 << legacyErrorToString(legacy_status);
663 WifiStatus status = createWifiStatusFromLegacyError(
664 legacy_status, "failed to get driver version");
665 return {status, result};
666 }
667 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700668
Roshan Piusabcf78f2017-10-06 16:30:38 -0700669 std::string firmware_desc;
670 std::tie(legacy_status, firmware_desc) =
671 legacy_hal_.lock()->getFirmwareVersion(getWlan0IfaceName());
672 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
673 LOG(ERROR) << "Failed to get firmware version: "
674 << legacyErrorToString(legacy_status);
675 WifiStatus status = createWifiStatusFromLegacyError(
676 legacy_status, "failed to get firmware version");
677 return {status, result};
678 }
679 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700680
Roshan Piusabcf78f2017-10-06 16:30:38 -0700681 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700682}
683
684std::pair<WifiStatus, std::vector<uint8_t>>
685WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700686 legacy_hal::wifi_error legacy_status;
687 std::vector<uint8_t> driver_dump;
688 std::tie(legacy_status, driver_dump) =
689 legacy_hal_.lock()->requestDriverMemoryDump(getWlan0IfaceName());
690 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
691 LOG(ERROR) << "Failed to get driver debug dump: "
692 << legacyErrorToString(legacy_status);
693 return {createWifiStatusFromLegacyError(legacy_status),
694 std::vector<uint8_t>()};
695 }
696 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700697}
698
699std::pair<WifiStatus, std::vector<uint8_t>>
700WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700701 legacy_hal::wifi_error legacy_status;
702 std::vector<uint8_t> firmware_dump;
703 std::tie(legacy_status, firmware_dump) =
704 legacy_hal_.lock()->requestFirmwareMemoryDump(getWlan0IfaceName());
705 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
706 LOG(ERROR) << "Failed to get firmware debug dump: "
707 << legacyErrorToString(legacy_status);
708 return {createWifiStatusFromLegacyError(legacy_status), {}};
709 }
710 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700711}
712
713std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700714 if (!canCurrentModeSupportIfaceOfType(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700715 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800716 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700717 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700718 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_);
719 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700720 for (const auto& callback : event_cb_handler_.getCallbacks()) {
721 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
722 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
723 }
724 }
Roshan Pius675609b2017-10-31 14:24:58 -0700725 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700726}
727
728std::pair<WifiStatus, std::vector<hidl_string>>
729WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700730 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700731 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
732 }
Roshan Pius675609b2017-10-31 14:24:58 -0700733 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700734}
735
736std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800737 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700738 const auto iface = findUsingName(ap_ifaces_, ifname);
739 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700740 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
741 }
Roshan Pius675609b2017-10-31 14:24:58 -0700742 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700743}
744
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800745WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700746 const auto iface = findUsingName(ap_ifaces_, ifname);
747 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700748 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800749 }
Roshan Pius675609b2017-10-31 14:24:58 -0700750 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700751 for (const auto& callback : event_cb_handler_.getCallbacks()) {
752 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
753 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
754 }
755 }
756 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800757}
758
Roshan Pius3c868522016-10-27 12:43:49 -0700759std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700760 if (!canCurrentModeSupportIfaceOfType(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700761 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800762 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700763 // These are still assumed to be based on wlan0.
Roshan Piuscc338202017-11-02 13:54:09 -0700764 std::string ifname = getWlan0IfaceName();
765 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_);
766 nan_ifaces_.push_back(iface);
767 for (const auto& callback : event_cb_handler_.getCallbacks()) {
768 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
769 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
770 }
771 }
772 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700773}
774
775std::pair<WifiStatus, std::vector<hidl_string>>
776WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700777 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700778 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
779 }
Roshan Pius675609b2017-10-31 14:24:58 -0700780 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700781}
782
783std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800784 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700785 const auto iface = findUsingName(nan_ifaces_, ifname);
786 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700787 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
788 }
Roshan Pius675609b2017-10-31 14:24:58 -0700789 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700790}
791
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800792WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700793 const auto iface = findUsingName(nan_ifaces_, ifname);
794 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700795 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800796 }
Roshan Pius675609b2017-10-31 14:24:58 -0700797 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700798 for (const auto& callback : event_cb_handler_.getCallbacks()) {
799 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
800 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
801 }
802 }
803 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800804}
805
Roshan Pius3c868522016-10-27 12:43:49 -0700806std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700807 if (!canCurrentModeSupportIfaceOfType(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700808 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800809 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700810 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700811 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
812 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700813 for (const auto& callback : event_cb_handler_.getCallbacks()) {
814 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
815 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
816 }
817 }
Roshan Pius675609b2017-10-31 14:24:58 -0700818 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700819}
820
821std::pair<WifiStatus, std::vector<hidl_string>>
822WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700823 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700824 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
825 }
Roshan Pius675609b2017-10-31 14:24:58 -0700826 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700827}
828
829std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800830 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700831 const auto iface = findUsingName(p2p_ifaces_, ifname);
832 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700833 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
834 }
Roshan Pius675609b2017-10-31 14:24:58 -0700835 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700836}
837
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800838WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700839 const auto iface = findUsingName(p2p_ifaces_, ifname);
840 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700841 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800842 }
Roshan Pius675609b2017-10-31 14:24:58 -0700843 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700844 for (const auto& callback : event_cb_handler_.getCallbacks()) {
845 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
846 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
847 }
848 }
849 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800850}
851
Roshan Pius3c868522016-10-27 12:43:49 -0700852std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700853 if (!canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700854 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800855 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700856 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700857 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_);
858 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700859 for (const auto& callback : event_cb_handler_.getCallbacks()) {
860 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
861 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
862 }
863 }
Roshan Pius675609b2017-10-31 14:24:58 -0700864 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700865}
866
867std::pair<WifiStatus, std::vector<hidl_string>>
868WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700869 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700870 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
871 }
Roshan Pius675609b2017-10-31 14:24:58 -0700872 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700873}
874
875std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800876 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700877 const auto iface = findUsingName(sta_ifaces_, ifname);
878 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700879 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
880 }
Roshan Pius675609b2017-10-31 14:24:58 -0700881 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700882}
883
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800884WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700885 const auto iface = findUsingName(sta_ifaces_, ifname);
886 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700887 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800888 }
Roshan Pius675609b2017-10-31 14:24:58 -0700889 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700890 for (const auto& callback : event_cb_handler_.getCallbacks()) {
891 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
892 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
893 }
894 }
895 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800896}
897
Roshan Pius3c868522016-10-27 12:43:49 -0700898std::pair<WifiStatus, sp<IWifiRttController>>
899WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700900 sp<WifiRttController> rtt =
901 new WifiRttController(getWlan0IfaceName(), bound_iface, legacy_hal_);
902 rtt_controllers_.emplace_back(rtt);
903 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
Roshan Pius3c868522016-10-27 12:43:49 -0700904}
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700905
906std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
907WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700908 legacy_hal::wifi_error legacy_status;
909 std::vector<legacy_hal::wifi_ring_buffer_status>
910 legacy_ring_buffer_status_vec;
911 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
912 legacy_hal_.lock()->getRingBuffersStatus(getWlan0IfaceName());
913 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
914 return {createWifiStatusFromLegacyError(legacy_status), {}};
915 }
916 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
917 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
918 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
919 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
920 }
921 return {createWifiStatus(WifiStatusCode::SUCCESS),
922 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700923}
924
925WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700926 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
927 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
928 WifiStatus status = registerDebugRingBufferCallback();
929 if (status.code != WifiStatusCode::SUCCESS) {
930 return status;
931 }
932 legacy_hal::wifi_error legacy_status =
933 legacy_hal_.lock()->startRingBufferLogging(
934 getWlan0IfaceName(), ring_name,
935 static_cast<
936 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
937 verbose_level),
938 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -0800939 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
940 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusabcf78f2017-10-06 16:30:38 -0700941 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700942}
943
944WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -0800945 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700946 WifiStatus status = registerDebugRingBufferCallback();
947 if (status.code != WifiStatusCode::SUCCESS) {
948 return status;
949 }
950 legacy_hal::wifi_error legacy_status =
951 legacy_hal_.lock()->getRingBufferData(getWlan0IfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -0800952
Roshan Piusabcf78f2017-10-06 16:30:38 -0700953 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700954}
955
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800956WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700957 legacy_hal::wifi_error legacy_status =
958 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
959 getWlan0IfaceName());
960 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800961}
962
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700963std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
964WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700965 legacy_hal::wifi_error legacy_status;
966 legacy_hal::WakeReasonStats legacy_stats;
967 std::tie(legacy_status, legacy_stats) =
968 legacy_hal_.lock()->getWakeReasonStats(getWlan0IfaceName());
969 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
970 return {createWifiStatusFromLegacyError(legacy_status), {}};
971 }
972 WifiDebugHostWakeReasonStats hidl_stats;
973 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
974 &hidl_stats)) {
975 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
976 }
977 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700978}
979
Roshan Pius203cb032016-12-14 17:41:20 -0800980WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700981 legacy_hal::wifi_error legacy_status;
982 if (enable) {
983 android::wp<WifiChip> weak_ptr_this(this);
984 const auto& on_alert_callback = [weak_ptr_this](
985 int32_t error_code,
986 std::vector<uint8_t> debug_data) {
987 const auto shared_ptr_this = weak_ptr_this.promote();
988 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
989 LOG(ERROR) << "Callback invoked on an invalid object";
990 return;
991 }
992 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
993 if (!callback->onDebugErrorAlert(error_code, debug_data)
994 .isOk()) {
995 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
996 }
997 }
998 };
999 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -07001000 getWlan0IfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001001 } else {
1002 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -07001003 getWlan0IfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001004 }
1005 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001006}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001007
Roshan Pius735ff432017-07-25 08:48:08 -07001008WifiStatus WifiChip::selectTxPowerScenarioInternal(TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001009 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1010 getWlan0IfaceName(),
1011 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1012 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001013}
1014
Roshan Pius735ff432017-07-25 08:48:08 -07001015WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001016 auto legacy_status =
1017 legacy_hal_.lock()->resetTxPowerScenario(getWlan0IfaceName());
1018 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001019}
1020
Roshan Piusba38d9c2017-12-08 07:32:08 -08001021WifiStatus WifiChip::handleChipConfiguration(
1022 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1023 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001024 // If the chip is already configured in a different mode, stop
1025 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001026 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001027 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1028 << " to mode " << mode_id;
1029 invalidateAndRemoveAllIfaces();
1030 legacy_hal::wifi_error legacy_status =
1031 legacy_hal_.lock()->stop(lock, []() {});
1032 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1033 LOG(ERROR) << "Failed to stop legacy HAL: "
1034 << legacyErrorToString(legacy_status);
1035 return createWifiStatusFromLegacyError(legacy_status);
1036 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001037 }
Roshan Piuscc338202017-11-02 13:54:09 -07001038 // Firmware mode change not needed for V2 devices.
1039 bool success = true;
1040 if (mode_id == kV1StaChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001041 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Roshan Piuscc338202017-11-02 13:54:09 -07001042 } else if (mode_id == kV1ApChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001043 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1044 }
1045 if (!success) {
1046 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1047 }
1048 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1049 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1050 LOG(ERROR) << "Failed to start legacy HAL: "
1051 << legacyErrorToString(legacy_status);
1052 return createWifiStatusFromLegacyError(legacy_status);
1053 }
1054 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001055}
Roshan Pius48185b22016-12-15 19:10:30 -08001056
1057WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001058 if (debug_ring_buffer_cb_registered_) {
1059 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001060 }
Roshan Pius3797e182017-03-30 18:01:54 -07001061
Roshan Piusabcf78f2017-10-06 16:30:38 -07001062 android::wp<WifiChip> weak_ptr_this(this);
1063 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001064 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001065 const std::vector<uint8_t>& data,
1066 const legacy_hal::wifi_ring_buffer_status& status) {
1067 const auto shared_ptr_this = weak_ptr_this.promote();
1068 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1069 LOG(ERROR) << "Callback invoked on an invalid object";
1070 return;
1071 }
1072 WifiDebugRingBufferStatus hidl_status;
1073 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1074 status, &hidl_status)) {
1075 LOG(ERROR) << "Error converting ring buffer status";
1076 return;
1077 }
xshu5899e8e2018-01-09 15:36:03 -08001078 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1079 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1080 Ringbuffer& cur_buffer = target->second;
1081 cur_buffer.append(data);
1082 } else {
1083 LOG(ERROR) << "Ringname " << name << " not found";
1084 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001085 }
1086 };
1087 legacy_hal::wifi_error legacy_status =
1088 legacy_hal_.lock()->registerRingBufferCallbackHandler(
1089 getWlan0IfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001090
Roshan Piusabcf78f2017-10-06 16:30:38 -07001091 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1092 debug_ring_buffer_cb_registered_ = true;
1093 }
1094 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001095}
1096
Roshan Piuscc338202017-11-02 13:54:09 -07001097void WifiChip::populateModes() {
1098 // The chip combination supported for current devices is fixed.
1099 // They can be one of the following based on device features:
1100 // a) 2 separate modes of operation with 1 interface combination each:
1101 // Mode 1 (STA mode): Will support 1 STA and 1 P2P or NAN(optional)
1102 // concurrent iface operations.
1103 // Mode 2 (AP mode): Will support 1 AP iface operation.
1104 //
1105 // b) 1 mode of operation with 2 interface combinations
1106 // (conditional on isDualInterfaceSupported()):
1107 // Interface Combination 1: Will support 1 STA and 1 P2P or NAN(optional)
1108 // concurrent iface operations.
1109 // Interface Combination 2: Will support 1 STA and 1 STA or AP concurrent
1110 // iface operations.
1111 // If Aware is enabled (conditional on isAwareSupported()), the iface
1112 // combination will be modified to support either P2P or NAN in place of
1113 // just P2P.
1114 if (feature_flags_.lock()->isDualInterfaceSupported()) {
1115 // V2 Iface combinations for Mode Id = 2.
1116 const IWifiChip::ChipIfaceCombinationLimit
1117 chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1118 const IWifiChip::ChipIfaceCombinationLimit
1119 chip_iface_combination_limit_2 = {{IfaceType::STA, IfaceType::AP},
1120 1};
1121 IWifiChip::ChipIfaceCombinationLimit chip_iface_combination_limit_3;
1122 if (feature_flags_.lock()->isAwareSupported()) {
1123 chip_iface_combination_limit_3 = {{IfaceType::P2P, IfaceType::NAN},
1124 1};
1125 } else {
1126 chip_iface_combination_limit_3 = {{IfaceType::P2P}, 1};
1127 }
1128 const IWifiChip::ChipIfaceCombination chip_iface_combination_1 = {
1129 {chip_iface_combination_limit_1, chip_iface_combination_limit_2}};
1130 const IWifiChip::ChipIfaceCombination chip_iface_combination_2 = {
1131 {chip_iface_combination_limit_1, chip_iface_combination_limit_3}};
1132 const IWifiChip::ChipMode chip_mode = {
1133 kV2ChipModeId,
1134 {chip_iface_combination_1, chip_iface_combination_2}};
1135 modes_ = {chip_mode};
1136 } else {
1137 // V1 Iface combinations for Mode Id = 0. (STA Mode)
1138 const IWifiChip::ChipIfaceCombinationLimit
1139 sta_chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1140 IWifiChip::ChipIfaceCombinationLimit sta_chip_iface_combination_limit_2;
1141 if (feature_flags_.lock()->isAwareSupported()) {
1142 sta_chip_iface_combination_limit_2 = {
1143 {IfaceType::P2P, IfaceType::NAN}, 1};
1144 } else {
1145 sta_chip_iface_combination_limit_2 = {{IfaceType::P2P}, 1};
1146 }
1147 const IWifiChip::ChipIfaceCombination sta_chip_iface_combination = {
1148 {sta_chip_iface_combination_limit_1,
1149 sta_chip_iface_combination_limit_2}};
1150 const IWifiChip::ChipMode sta_chip_mode = {
1151 kV1StaChipModeId, {sta_chip_iface_combination}};
1152 // Iface combinations for Mode Id = 1. (AP Mode)
1153 const IWifiChip::ChipIfaceCombinationLimit
1154 ap_chip_iface_combination_limit = {{IfaceType::AP}, 1};
1155 const IWifiChip::ChipIfaceCombination ap_chip_iface_combination = {
1156 {ap_chip_iface_combination_limit}};
1157 const IWifiChip::ChipMode ap_chip_mode = {kV1ApChipModeId,
1158 {ap_chip_iface_combination}};
1159 modes_ = {sta_chip_mode, ap_chip_mode};
1160 }
1161}
1162
1163std::vector<IWifiChip::ChipIfaceCombination>
1164WifiChip::getCurrentModeIfaceCombinations() {
1165 if (!isValidModeId(current_mode_id_)) {
1166 LOG(ERROR) << "Chip not configured in a mode yet";
1167 return {};
1168 }
1169 for (const auto& mode : modes_) {
1170 if (mode.id == current_mode_id_) {
1171 return mode.availableCombinations;
1172 }
1173 }
1174 CHECK(0) << "Expected to find iface combinations for current mode!";
1175 return {};
1176}
1177
1178// Returns a map indexed by IfaceType with the number of ifaces currently
1179// created of the corresponding type.
1180std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1181 std::map<IfaceType, size_t> iface_counts;
1182 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1183 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1184 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1185 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1186 return iface_counts;
1187}
1188
1189// This expands the provided iface combinations to a more parseable
1190// form. Returns a vector of available combinations possible with the number
1191// of ifaces of each type in the combination.
1192// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1193std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1194 const IWifiChip::ChipIfaceCombination& combination) {
1195 uint32_t num_expanded_combos = 1;
1196 for (const auto& limit : combination.limits) {
1197 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1198 num_expanded_combos *= limit.types.size();
1199 }
1200 }
1201
1202 // Allocate the vector of expanded combos and reset all iface counts to 0
1203 // in each combo.
1204 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1205 expanded_combos.resize(num_expanded_combos);
1206 for (auto& expanded_combo : expanded_combos) {
1207 for (const auto type :
1208 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1209 expanded_combo[type] = 0;
1210 }
1211 }
1212 uint32_t span = num_expanded_combos;
1213 for (const auto& limit : combination.limits) {
1214 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1215 span /= limit.types.size();
1216 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1217 const auto iface_type =
1218 limit.types[(k / span) % limit.types.size()];
1219 expanded_combos[k][iface_type]++;
1220 }
1221 }
1222 }
1223 return expanded_combos;
1224}
1225
1226bool WifiChip::canExpandedIfaceCombinationSupportIfaceOfType(
1227 const std::map<IfaceType, size_t>& combo, IfaceType requested_type) {
1228 const auto current_combo = getCurrentIfaceCombination();
1229
1230 // Check if we have space for 1 more iface of |type| in this combo
1231 for (const auto type :
1232 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1233 size_t num_ifaces_needed = current_combo.at(type);
1234 if (type == requested_type) {
1235 num_ifaces_needed++;
1236 }
1237 size_t num_ifaces_allowed = combo.at(type);
1238 if (num_ifaces_needed > num_ifaces_allowed) {
1239 return false;
1240 }
1241 }
1242 return true;
1243}
1244
1245// This method does the following:
1246// a) Enumerate all possible iface combos by expanding the current
1247// ChipIfaceCombination.
1248// b) Check if the requested iface type can be added to the current mode.
1249bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType type) {
1250 if (!isValidModeId(current_mode_id_)) {
1251 LOG(ERROR) << "Chip not configured in a mode yet";
1252 return false;
1253 }
1254 const auto combinations = getCurrentModeIfaceCombinations();
1255 for (const auto& combination : combinations) {
1256 const auto expanded_combos = expandIfaceCombinations(combination);
1257 for (const auto& expanded_combo : expanded_combos) {
1258 if (canExpandedIfaceCombinationSupportIfaceOfType(expanded_combo,
1259 type)) {
1260 return true;
1261 }
1262 }
1263 }
1264 return false;
1265}
1266
1267bool WifiChip::isValidModeId(ChipModeId mode_id) {
1268 for (const auto& mode : modes_) {
1269 if (mode.id == mode_id) {
1270 return true;
1271 }
1272 }
1273 return false;
1274}
1275
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001276// Return "wlan0", if "wlan0" is not already in use, else return "wlan1".
1277// This is based on the assumption that we'll have a max of 2 concurrent
1278// AP/STA ifaces.
1279std::string WifiChip::allocateApOrStaIfaceName() {
1280 auto ap_iface = findUsingName(ap_ifaces_, getWlan0IfaceName());
1281 auto sta_iface = findUsingName(sta_ifaces_, getWlan0IfaceName());
1282 if (!ap_iface.get() && !sta_iface.get()) {
1283 return getWlan0IfaceName();
1284 }
1285 ap_iface = findUsingName(ap_ifaces_, getWlan1IfaceName());
1286 sta_iface = findUsingName(sta_ifaces_, getWlan1IfaceName());
1287 if (!ap_iface.get() && !sta_iface.get()) {
1288 return getWlan1IfaceName();
1289 }
1290 // This should never happen. We screwed up somewhere if it did.
1291 CHECK(0) << "wlan0 and wlan1 in use already!";
1292 return {};
1293}
1294
xshu5899e8e2018-01-09 15:36:03 -08001295bool WifiChip::writeRingbufferFilesInternal() {
1296 if (!removeOldFilesInternal()) {
1297 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1298 return false;
1299 }
1300 // write ringbuffers to file
1301 for (const auto& item : ringbuffer_map_) {
1302 const Ringbuffer& cur_buffer = item.second;
1303 if (cur_buffer.getData().empty()) {
1304 continue;
1305 }
1306 const std::string file_path_raw =
1307 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1308 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1309 if (dump_fd == -1) {
1310 LOG(ERROR) << "create file failed: " << strerror(errno);
1311 return false;
1312 }
1313 unique_fd file_auto_closer(dump_fd);
1314 for (const auto& cur_block : cur_buffer.getData()) {
1315 if (write(dump_fd, cur_block.data(),
1316 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1317 LOG(ERROR) << "Error writing to file " << strerror(errno);
1318 }
1319 }
1320 }
1321 return true;
1322}
1323
Roshan Pius79a99752016-10-04 13:03:58 -07001324} // namespace implementation
Etan Cohen6ce50902017-09-14 07:30:57 -07001325} // namespace V1_2
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001326} // namespace wifi
1327} // namespace hardware
1328} // namespace android