blob: dcd0a3ce8e417d62e980bdd7b590221b46c0ecdd [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 Pius1ce92cf2018-01-22 16:12:19 -0800330 const sp<V1_0::IWifiChipEventCallback>& event_callback,
Roshan Pius5c055462016-10-11 08:27:27 -0700331 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(
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800531 V1_1::IWifiChip::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
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800544Return<void> WifiChip::registerEventCallback_1_2(
545 const sp<IWifiChipEventCallback>& event_callback,
546 registerEventCallback_cb hidl_status_cb) {
547 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
548 &WifiChip::registerEventCallbackInternal_1_2,
549 hidl_status_cb, event_callback);
550}
551
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -0800552Return<void> WifiChip::selectTxPowerScenario_1_2(
553 TxPowerScenario scenario, selectTxPowerScenario_cb hidl_status_cb) {
554 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID,
555 &WifiChip::selectTxPowerScenarioInternal_1_2, hidl_status_cb, scenario);
556}
557
xshu5899e8e2018-01-09 15:36:03 -0800558Return<void> WifiChip::debug(const hidl_handle& handle,
559 const hidl_vec<hidl_string>&) {
560 if (handle != nullptr && handle->numFds >= 1) {
561 int fd = handle->data[0];
562 if (!writeRingbufferFilesInternal()) {
563 LOG(ERROR) << "Error writing files to flash";
564 }
xshu4cb33162018-01-24 15:40:06 -0800565 uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath);
xshu5899e8e2018-01-09 15:36:03 -0800566 if (n_error != 0) {
567 LOG(ERROR) << n_error << " errors occured in cpio function";
568 }
569 fsync(fd);
570 } else {
571 LOG(ERROR) << "File handle error";
572 }
573 return Void();
574}
575
Roshan Pius35d958c2016-10-06 16:47:38 -0700576void WifiChip::invalidateAndRemoveAllIfaces() {
Roshan Pius675609b2017-10-31 14:24:58 -0700577 invalidateAndClearAll(ap_ifaces_);
578 invalidateAndClearAll(nan_ifaces_);
579 invalidateAndClearAll(p2p_ifaces_);
580 invalidateAndClearAll(sta_ifaces_);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700581 // Since all the ifaces are invalid now, all RTT controller objects
582 // using those ifaces also need to be invalidated.
583 for (const auto& rtt : rtt_controllers_) {
584 rtt->invalidate();
585 }
586 rtt_controllers_.clear();
Roshan Pius35d958c2016-10-06 16:47:38 -0700587}
588
Roshan Pius3c868522016-10-27 12:43:49 -0700589std::pair<WifiStatus, ChipId> WifiChip::getIdInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700590 return {createWifiStatus(WifiStatusCode::SUCCESS), chip_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700591}
592
593WifiStatus WifiChip::registerEventCallbackInternal(
Roshan Pius1ce92cf2018-01-22 16:12:19 -0800594 const sp<V1_0::IWifiChipEventCallback>& /* event_callback */) {
595 // Deprecated support for this callback.
596 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
Roshan Pius3c868522016-10-27 12:43:49 -0700597}
598
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700599std::pair<WifiStatus, uint32_t> WifiChip::getCapabilitiesInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700600 legacy_hal::wifi_error legacy_status;
601 uint32_t legacy_feature_set;
602 uint32_t legacy_logger_feature_set;
603 std::tie(legacy_status, legacy_feature_set) =
604 legacy_hal_.lock()->getSupportedFeatureSet(getWlan0IfaceName());
605 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
606 return {createWifiStatusFromLegacyError(legacy_status), 0};
607 }
608 std::tie(legacy_status, legacy_logger_feature_set) =
609 legacy_hal_.lock()->getLoggerSupportedFeatureSet(getWlan0IfaceName());
610 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Roshan Pius876220f2017-12-28 16:19:06 -0800611 // some devices don't support querying logger feature set
612 legacy_logger_feature_set = 0;
Roshan Piusabcf78f2017-10-06 16:30:38 -0700613 }
614 uint32_t hidl_caps;
615 if (!hidl_struct_util::convertLegacyFeaturesToHidlChipCapabilities(
616 legacy_feature_set, legacy_logger_feature_set, &hidl_caps)) {
617 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), 0};
618 }
619 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_caps};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700620}
621
Roshan Pius3c868522016-10-27 12:43:49 -0700622std::pair<WifiStatus, std::vector<IWifiChip::ChipMode>>
623WifiChip::getAvailableModesInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700624 return {createWifiStatus(WifiStatusCode::SUCCESS), modes_};
Roshan Pius3c868522016-10-27 12:43:49 -0700625}
626
Roshan Piusba38d9c2017-12-08 07:32:08 -0800627WifiStatus WifiChip::configureChipInternal(
628 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
629 ChipModeId mode_id) {
Roshan Piuscc338202017-11-02 13:54:09 -0700630 if (!isValidModeId(mode_id)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700631 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
632 }
633 if (mode_id == current_mode_id_) {
634 LOG(DEBUG) << "Already in the specified mode " << mode_id;
635 return createWifiStatus(WifiStatusCode::SUCCESS);
636 }
Roshan Piusba38d9c2017-12-08 07:32:08 -0800637 WifiStatus status = handleChipConfiguration(lock, mode_id);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700638 if (status.code != WifiStatusCode::SUCCESS) {
639 for (const auto& callback : event_cb_handler_.getCallbacks()) {
640 if (!callback->onChipReconfigureFailure(status).isOk()) {
641 LOG(ERROR)
642 << "Failed to invoke onChipReconfigureFailure callback";
643 }
644 }
645 return status;
646 }
Roshan Piusd37341f2017-01-31 13:13:28 -0800647 for (const auto& callback : event_cb_handler_.getCallbacks()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700648 if (!callback->onChipReconfigured(mode_id).isOk()) {
649 LOG(ERROR) << "Failed to invoke onChipReconfigured callback";
650 }
Roshan Pius52947fb2016-11-18 11:38:07 -0800651 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700652 current_mode_id_ = mode_id;
Roshan Piusba38d9c2017-12-08 07:32:08 -0800653 LOG(INFO) << "Configured chip in mode " << mode_id;
Roshan Pius2c06a3f2016-12-15 17:51:40 -0800654 return status;
Roshan Pius3c868522016-10-27 12:43:49 -0700655}
656
657std::pair<WifiStatus, uint32_t> WifiChip::getModeInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700658 if (!isValidModeId(current_mode_id_)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700659 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE),
660 current_mode_id_};
661 }
662 return {createWifiStatus(WifiStatusCode::SUCCESS), current_mode_id_};
Roshan Pius3c868522016-10-27 12:43:49 -0700663}
664
665std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
666WifiChip::requestChipDebugInfoInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700667 IWifiChip::ChipDebugInfo result;
668 legacy_hal::wifi_error legacy_status;
669 std::string driver_desc;
670 std::tie(legacy_status, driver_desc) =
671 legacy_hal_.lock()->getDriverVersion(getWlan0IfaceName());
672 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
673 LOG(ERROR) << "Failed to get driver version: "
674 << legacyErrorToString(legacy_status);
675 WifiStatus status = createWifiStatusFromLegacyError(
676 legacy_status, "failed to get driver version");
677 return {status, result};
678 }
679 result.driverDescription = driver_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700680
Roshan Piusabcf78f2017-10-06 16:30:38 -0700681 std::string firmware_desc;
682 std::tie(legacy_status, firmware_desc) =
683 legacy_hal_.lock()->getFirmwareVersion(getWlan0IfaceName());
684 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
685 LOG(ERROR) << "Failed to get firmware version: "
686 << legacyErrorToString(legacy_status);
687 WifiStatus status = createWifiStatusFromLegacyError(
688 legacy_status, "failed to get firmware version");
689 return {status, result};
690 }
691 result.firmwareDescription = firmware_desc.c_str();
Roshan Pius3c868522016-10-27 12:43:49 -0700692
Roshan Piusabcf78f2017-10-06 16:30:38 -0700693 return {createWifiStatus(WifiStatusCode::SUCCESS), result};
Roshan Pius3c868522016-10-27 12:43:49 -0700694}
695
696std::pair<WifiStatus, std::vector<uint8_t>>
697WifiChip::requestDriverDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700698 legacy_hal::wifi_error legacy_status;
699 std::vector<uint8_t> driver_dump;
700 std::tie(legacy_status, driver_dump) =
701 legacy_hal_.lock()->requestDriverMemoryDump(getWlan0IfaceName());
702 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
703 LOG(ERROR) << "Failed to get driver debug dump: "
704 << legacyErrorToString(legacy_status);
705 return {createWifiStatusFromLegacyError(legacy_status),
706 std::vector<uint8_t>()};
707 }
708 return {createWifiStatus(WifiStatusCode::SUCCESS), driver_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700709}
710
711std::pair<WifiStatus, std::vector<uint8_t>>
712WifiChip::requestFirmwareDebugDumpInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700713 legacy_hal::wifi_error legacy_status;
714 std::vector<uint8_t> firmware_dump;
715 std::tie(legacy_status, firmware_dump) =
716 legacy_hal_.lock()->requestFirmwareMemoryDump(getWlan0IfaceName());
717 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
718 LOG(ERROR) << "Failed to get firmware debug dump: "
719 << legacyErrorToString(legacy_status);
720 return {createWifiStatusFromLegacyError(legacy_status), {}};
721 }
722 return {createWifiStatus(WifiStatusCode::SUCCESS), firmware_dump};
Roshan Pius3c868522016-10-27 12:43:49 -0700723}
724
725std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::createApIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700726 if (!canCurrentModeSupportIfaceOfType(IfaceType::AP)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700727 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800728 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700729 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700730 sp<WifiApIface> iface = new WifiApIface(ifname, legacy_hal_);
731 ap_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700732 for (const auto& callback : event_cb_handler_.getCallbacks()) {
733 if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) {
734 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
735 }
736 }
Roshan Pius675609b2017-10-31 14:24:58 -0700737 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700738}
739
740std::pair<WifiStatus, std::vector<hidl_string>>
741WifiChip::getApIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700742 if (ap_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700743 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
744 }
Roshan Pius675609b2017-10-31 14:24:58 -0700745 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(ap_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700746}
747
748std::pair<WifiStatus, sp<IWifiApIface>> WifiChip::getApIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800749 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700750 const auto iface = findUsingName(ap_ifaces_, ifname);
751 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700752 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
753 }
Roshan Pius675609b2017-10-31 14:24:58 -0700754 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700755}
756
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800757WifiStatus WifiChip::removeApIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700758 const auto iface = findUsingName(ap_ifaces_, ifname);
759 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700760 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800761 }
Roshan Pius675609b2017-10-31 14:24:58 -0700762 invalidateAndClear(ap_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700763 for (const auto& callback : event_cb_handler_.getCallbacks()) {
764 if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) {
765 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
766 }
767 }
768 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800769}
770
Roshan Pius3c868522016-10-27 12:43:49 -0700771std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::createNanIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700772 if (!canCurrentModeSupportIfaceOfType(IfaceType::NAN)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700773 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Etan Cohenc5700402017-03-08 16:43:38 -0800774 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700775 // These are still assumed to be based on wlan0.
Roshan Piuscc338202017-11-02 13:54:09 -0700776 std::string ifname = getWlan0IfaceName();
777 sp<WifiNanIface> iface = new WifiNanIface(ifname, legacy_hal_);
778 nan_ifaces_.push_back(iface);
779 for (const auto& callback : event_cb_handler_.getCallbacks()) {
780 if (!callback->onIfaceAdded(IfaceType::NAN, ifname).isOk()) {
781 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
782 }
783 }
784 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700785}
786
787std::pair<WifiStatus, std::vector<hidl_string>>
788WifiChip::getNanIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700789 if (nan_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700790 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
791 }
Roshan Pius675609b2017-10-31 14:24:58 -0700792 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(nan_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700793}
794
795std::pair<WifiStatus, sp<IWifiNanIface>> WifiChip::getNanIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800796 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700797 const auto iface = findUsingName(nan_ifaces_, ifname);
798 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700799 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
800 }
Roshan Pius675609b2017-10-31 14:24:58 -0700801 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700802}
803
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800804WifiStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700805 const auto iface = findUsingName(nan_ifaces_, ifname);
806 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700807 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800808 }
Roshan Pius675609b2017-10-31 14:24:58 -0700809 invalidateAndClear(nan_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700810 for (const auto& callback : event_cb_handler_.getCallbacks()) {
811 if (!callback->onIfaceRemoved(IfaceType::NAN, ifname).isOk()) {
812 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
813 }
814 }
815 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800816}
817
Roshan Pius3c868522016-10-27 12:43:49 -0700818std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::createP2pIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700819 if (!canCurrentModeSupportIfaceOfType(IfaceType::P2P)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700820 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800821 }
Roshan Piusabcf78f2017-10-06 16:30:38 -0700822 std::string ifname = getP2pIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700823 sp<WifiP2pIface> iface = new WifiP2pIface(ifname, legacy_hal_);
824 p2p_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700825 for (const auto& callback : event_cb_handler_.getCallbacks()) {
826 if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) {
827 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
828 }
829 }
Roshan Pius675609b2017-10-31 14:24:58 -0700830 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700831}
832
833std::pair<WifiStatus, std::vector<hidl_string>>
834WifiChip::getP2pIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700835 if (p2p_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700836 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
837 }
Roshan Pius675609b2017-10-31 14:24:58 -0700838 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(p2p_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700839}
840
841std::pair<WifiStatus, sp<IWifiP2pIface>> WifiChip::getP2pIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800842 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700843 const auto iface = findUsingName(p2p_ifaces_, ifname);
844 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700845 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
846 }
Roshan Pius675609b2017-10-31 14:24:58 -0700847 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700848}
849
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800850WifiStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700851 const auto iface = findUsingName(p2p_ifaces_, ifname);
852 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700853 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800854 }
Roshan Pius675609b2017-10-31 14:24:58 -0700855 invalidateAndClear(p2p_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700856 for (const auto& callback : event_cb_handler_.getCallbacks()) {
857 if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) {
858 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
859 }
860 }
861 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800862}
863
Roshan Pius3c868522016-10-27 12:43:49 -0700864std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::createStaIfaceInternal() {
Roshan Piuscc338202017-11-02 13:54:09 -0700865 if (!canCurrentModeSupportIfaceOfType(IfaceType::STA)) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700866 return {createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE), {}};
Roshan Piusbc662202017-01-30 17:07:42 -0800867 }
Roshan Pius8e3c7ef2017-11-03 09:43:08 -0700868 std::string ifname = allocateApOrStaIfaceName();
Roshan Pius675609b2017-10-31 14:24:58 -0700869 sp<WifiStaIface> iface = new WifiStaIface(ifname, legacy_hal_);
870 sta_ifaces_.push_back(iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700871 for (const auto& callback : event_cb_handler_.getCallbacks()) {
872 if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) {
873 LOG(ERROR) << "Failed to invoke onIfaceAdded callback";
874 }
875 }
Roshan Pius675609b2017-10-31 14:24:58 -0700876 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700877}
878
879std::pair<WifiStatus, std::vector<hidl_string>>
880WifiChip::getStaIfaceNamesInternal() {
Roshan Pius675609b2017-10-31 14:24:58 -0700881 if (sta_ifaces_.empty()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700882 return {createWifiStatus(WifiStatusCode::SUCCESS), {}};
883 }
Roshan Pius675609b2017-10-31 14:24:58 -0700884 return {createWifiStatus(WifiStatusCode::SUCCESS), getNames(sta_ifaces_)};
Roshan Pius3c868522016-10-27 12:43:49 -0700885}
886
887std::pair<WifiStatus, sp<IWifiStaIface>> WifiChip::getStaIfaceInternal(
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800888 const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700889 const auto iface = findUsingName(sta_ifaces_, ifname);
890 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700891 return {createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS), nullptr};
892 }
Roshan Pius675609b2017-10-31 14:24:58 -0700893 return {createWifiStatus(WifiStatusCode::SUCCESS), iface};
Roshan Pius3c868522016-10-27 12:43:49 -0700894}
895
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800896WifiStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) {
Roshan Pius675609b2017-10-31 14:24:58 -0700897 const auto iface = findUsingName(sta_ifaces_, ifname);
898 if (!iface.get()) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700899 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
Roshan Piusbc662202017-01-30 17:07:42 -0800900 }
Roshan Pius675609b2017-10-31 14:24:58 -0700901 invalidateAndClear(sta_ifaces_, iface);
Roshan Piusabcf78f2017-10-06 16:30:38 -0700902 for (const auto& callback : event_cb_handler_.getCallbacks()) {
903 if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) {
904 LOG(ERROR) << "Failed to invoke onIfaceRemoved callback";
905 }
906 }
907 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius8b55e6f2016-12-09 12:05:12 -0800908}
909
Roshan Pius3c868522016-10-27 12:43:49 -0700910std::pair<WifiStatus, sp<IWifiRttController>>
911WifiChip::createRttControllerInternal(const sp<IWifiIface>& bound_iface) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700912 sp<WifiRttController> rtt =
913 new WifiRttController(getWlan0IfaceName(), bound_iface, legacy_hal_);
914 rtt_controllers_.emplace_back(rtt);
915 return {createWifiStatus(WifiStatusCode::SUCCESS), rtt};
Roshan Pius3c868522016-10-27 12:43:49 -0700916}
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700917
918std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
919WifiChip::getDebugRingBuffersStatusInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700920 legacy_hal::wifi_error legacy_status;
921 std::vector<legacy_hal::wifi_ring_buffer_status>
922 legacy_ring_buffer_status_vec;
923 std::tie(legacy_status, legacy_ring_buffer_status_vec) =
924 legacy_hal_.lock()->getRingBuffersStatus(getWlan0IfaceName());
925 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
926 return {createWifiStatusFromLegacyError(legacy_status), {}};
927 }
928 std::vector<WifiDebugRingBufferStatus> hidl_ring_buffer_status_vec;
929 if (!hidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToHidl(
930 legacy_ring_buffer_status_vec, &hidl_ring_buffer_status_vec)) {
931 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
932 }
933 return {createWifiStatus(WifiStatusCode::SUCCESS),
934 hidl_ring_buffer_status_vec};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700935}
936
937WifiStatus WifiChip::startLoggingToDebugRingBufferInternal(
Roshan Piusabcf78f2017-10-06 16:30:38 -0700938 const hidl_string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
939 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) {
940 WifiStatus status = registerDebugRingBufferCallback();
941 if (status.code != WifiStatusCode::SUCCESS) {
942 return status;
943 }
944 legacy_hal::wifi_error legacy_status =
945 legacy_hal_.lock()->startRingBufferLogging(
946 getWlan0IfaceName(), ring_name,
947 static_cast<
948 std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(
949 verbose_level),
950 max_interval_in_sec, min_data_size_in_bytes);
xshu5899e8e2018-01-09 15:36:03 -0800951 ringbuffer_map_.insert(std::pair<std::string, Ringbuffer>(
952 ring_name, Ringbuffer(kMaxBufferSizeBytes)));
Roshan Piusabcf78f2017-10-06 16:30:38 -0700953 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700954}
955
956WifiStatus WifiChip::forceDumpToDebugRingBufferInternal(
Roshan Piuse2d0ab52016-12-05 15:24:20 -0800957 const hidl_string& ring_name) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700958 WifiStatus status = registerDebugRingBufferCallback();
959 if (status.code != WifiStatusCode::SUCCESS) {
960 return status;
961 }
962 legacy_hal::wifi_error legacy_status =
963 legacy_hal_.lock()->getRingBufferData(getWlan0IfaceName(), ring_name);
xshu5899e8e2018-01-09 15:36:03 -0800964
Roshan Piusabcf78f2017-10-06 16:30:38 -0700965 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700966}
967
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800968WifiStatus WifiChip::stopLoggingToDebugRingBufferInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700969 legacy_hal::wifi_error legacy_status =
970 legacy_hal_.lock()->deregisterRingBufferCallbackHandler(
971 getWlan0IfaceName());
972 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius8c0c8e92017-02-24 08:07:42 -0800973}
974
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700975std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
976WifiChip::getDebugHostWakeReasonStatsInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700977 legacy_hal::wifi_error legacy_status;
978 legacy_hal::WakeReasonStats legacy_stats;
979 std::tie(legacy_status, legacy_stats) =
980 legacy_hal_.lock()->getWakeReasonStats(getWlan0IfaceName());
981 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
982 return {createWifiStatusFromLegacyError(legacy_status), {}};
983 }
984 WifiDebugHostWakeReasonStats hidl_stats;
985 if (!hidl_struct_util::convertLegacyWakeReasonStatsToHidl(legacy_stats,
986 &hidl_stats)) {
987 return {createWifiStatus(WifiStatusCode::ERROR_UNKNOWN), {}};
988 }
989 return {createWifiStatus(WifiStatusCode::SUCCESS), hidl_stats};
Roshan Pius7d08d7a2016-10-27 14:35:05 -0700990}
991
Roshan Pius203cb032016-12-14 17:41:20 -0800992WifiStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) {
Roshan Piusabcf78f2017-10-06 16:30:38 -0700993 legacy_hal::wifi_error legacy_status;
994 if (enable) {
995 android::wp<WifiChip> weak_ptr_this(this);
996 const auto& on_alert_callback = [weak_ptr_this](
997 int32_t error_code,
998 std::vector<uint8_t> debug_data) {
999 const auto shared_ptr_this = weak_ptr_this.promote();
1000 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1001 LOG(ERROR) << "Callback invoked on an invalid object";
1002 return;
1003 }
1004 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1005 if (!callback->onDebugErrorAlert(error_code, debug_data)
1006 .isOk()) {
1007 LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback";
1008 }
1009 }
1010 };
1011 legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -07001012 getWlan0IfaceName(), on_alert_callback);
Roshan Piusabcf78f2017-10-06 16:30:38 -07001013 } else {
1014 legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler(
Roshan Piusacededb2017-10-06 14:59:26 -07001015 getWlan0IfaceName());
Roshan Piusabcf78f2017-10-06 16:30:38 -07001016 }
1017 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius203cb032016-12-14 17:41:20 -08001018}
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001019
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001020WifiStatus WifiChip::selectTxPowerScenarioInternal(
1021 V1_1::IWifiChip::TxPowerScenario scenario) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001022 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1023 getWlan0IfaceName(),
1024 hidl_struct_util::convertHidlTxPowerScenarioToLegacy(scenario));
1025 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001026}
1027
Roshan Pius735ff432017-07-25 08:48:08 -07001028WifiStatus WifiChip::resetTxPowerScenarioInternal() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001029 auto legacy_status =
1030 legacy_hal_.lock()->resetTxPowerScenario(getWlan0IfaceName());
1031 return createWifiStatusFromLegacyError(legacy_status);
Roshan Piusdbd83ef2017-06-20 12:05:40 -07001032}
1033
Roshan Pius1ce92cf2018-01-22 16:12:19 -08001034WifiStatus WifiChip::registerEventCallbackInternal_1_2(
1035 const sp<IWifiChipEventCallback>& event_callback) {
1036 if (!event_cb_handler_.addCallback(event_callback)) {
1037 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1038 }
1039 return createWifiStatus(WifiStatusCode::SUCCESS);
1040}
1041
Ahmed ElArabawy6a1accf2018-01-23 10:57:29 -08001042WifiStatus WifiChip::selectTxPowerScenarioInternal_1_2(TxPowerScenario scenario) {
1043 auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario(
1044 getWlan0IfaceName(),
1045 hidl_struct_util::convertHidlTxPowerScenarioToLegacy_1_2(scenario));
1046 return createWifiStatusFromLegacyError(legacy_status);
1047}
1048
Roshan Piusba38d9c2017-12-08 07:32:08 -08001049WifiStatus WifiChip::handleChipConfiguration(
1050 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
1051 ChipModeId mode_id) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001052 // If the chip is already configured in a different mode, stop
1053 // the legacy HAL and then start it after firmware mode change.
Roshan Piuscc338202017-11-02 13:54:09 -07001054 if (isValidModeId(current_mode_id_)) {
Roshan Piusba38d9c2017-12-08 07:32:08 -08001055 LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_
1056 << " to mode " << mode_id;
1057 invalidateAndRemoveAllIfaces();
1058 legacy_hal::wifi_error legacy_status =
1059 legacy_hal_.lock()->stop(lock, []() {});
1060 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1061 LOG(ERROR) << "Failed to stop legacy HAL: "
1062 << legacyErrorToString(legacy_status);
1063 return createWifiStatusFromLegacyError(legacy_status);
1064 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001065 }
Roshan Piuscc338202017-11-02 13:54:09 -07001066 // Firmware mode change not needed for V2 devices.
1067 bool success = true;
1068 if (mode_id == kV1StaChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001069 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA);
Roshan Piuscc338202017-11-02 13:54:09 -07001070 } else if (mode_id == kV1ApChipModeId) {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001071 success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP);
1072 }
1073 if (!success) {
1074 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
1075 }
1076 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start();
1077 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
1078 LOG(ERROR) << "Failed to start legacy HAL: "
1079 << legacyErrorToString(legacy_status);
1080 return createWifiStatusFromLegacyError(legacy_status);
1081 }
Roshan Pius85c64412018-01-22 17:58:40 -08001082 // Every time the HAL is restarted, we need to register the
1083 // radio mode change callback.
1084 WifiStatus status = registerRadioModeChangeCallback();
1085 if (status.code != WifiStatusCode::SUCCESS) {
1086 // This probably is not a critical failure?
1087 LOG(ERROR) << "Failed to register radio mode change callback";
1088 }
Roshan Piusabcf78f2017-10-06 16:30:38 -07001089 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius2c06a3f2016-12-15 17:51:40 -08001090}
Roshan Pius48185b22016-12-15 19:10:30 -08001091
1092WifiStatus WifiChip::registerDebugRingBufferCallback() {
Roshan Piusabcf78f2017-10-06 16:30:38 -07001093 if (debug_ring_buffer_cb_registered_) {
1094 return createWifiStatus(WifiStatusCode::SUCCESS);
Roshan Pius48185b22016-12-15 19:10:30 -08001095 }
Roshan Pius3797e182017-03-30 18:01:54 -07001096
Roshan Piusabcf78f2017-10-06 16:30:38 -07001097 android::wp<WifiChip> weak_ptr_this(this);
1098 const auto& on_ring_buffer_data_callback =
xshu5899e8e2018-01-09 15:36:03 -08001099 [weak_ptr_this](const std::string& name,
Roshan Piusabcf78f2017-10-06 16:30:38 -07001100 const std::vector<uint8_t>& data,
1101 const legacy_hal::wifi_ring_buffer_status& status) {
1102 const auto shared_ptr_this = weak_ptr_this.promote();
1103 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1104 LOG(ERROR) << "Callback invoked on an invalid object";
1105 return;
1106 }
1107 WifiDebugRingBufferStatus hidl_status;
1108 if (!hidl_struct_util::convertLegacyDebugRingBufferStatusToHidl(
1109 status, &hidl_status)) {
1110 LOG(ERROR) << "Error converting ring buffer status";
1111 return;
1112 }
xshu5899e8e2018-01-09 15:36:03 -08001113 const auto& target = shared_ptr_this->ringbuffer_map_.find(name);
1114 if (target != shared_ptr_this->ringbuffer_map_.end()) {
1115 Ringbuffer& cur_buffer = target->second;
1116 cur_buffer.append(data);
1117 } else {
1118 LOG(ERROR) << "Ringname " << name << " not found";
1119 return;
Roshan Piusabcf78f2017-10-06 16:30:38 -07001120 }
1121 };
1122 legacy_hal::wifi_error legacy_status =
1123 legacy_hal_.lock()->registerRingBufferCallbackHandler(
1124 getWlan0IfaceName(), on_ring_buffer_data_callback);
Roshan Pius48185b22016-12-15 19:10:30 -08001125
Roshan Piusabcf78f2017-10-06 16:30:38 -07001126 if (legacy_status == legacy_hal::WIFI_SUCCESS) {
1127 debug_ring_buffer_cb_registered_ = true;
1128 }
1129 return createWifiStatusFromLegacyError(legacy_status);
Roshan Pius48185b22016-12-15 19:10:30 -08001130}
1131
Roshan Pius85c64412018-01-22 17:58:40 -08001132WifiStatus WifiChip::registerRadioModeChangeCallback() {
1133 android::wp<WifiChip> weak_ptr_this(this);
1134 const auto& on_radio_mode_change_callback =
1135 [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) {
1136 const auto shared_ptr_this = weak_ptr_this.promote();
1137 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
1138 LOG(ERROR) << "Callback invoked on an invalid object";
1139 return;
1140 }
1141 std::vector<IWifiChipEventCallback::RadioModeInfo>
1142 hidl_radio_mode_infos;
1143 if (!hidl_struct_util::convertLegacyWifiMacInfosToHidl(
1144 mac_infos, &hidl_radio_mode_infos)) {
1145 LOG(ERROR) << "Error converting wifi mac info";
1146 return;
1147 }
1148 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
1149 if (!callback->onRadioModeChange(hidl_radio_mode_infos)
1150 .isOk()) {
1151 LOG(ERROR) << "Failed to invoke onRadioModeChange"
1152 << " callback on: " << toString(callback);
1153 }
1154 }
1155 };
1156 legacy_hal::wifi_error legacy_status =
1157 legacy_hal_.lock()->registerRadioModeChangeCallbackHandler(
1158 getWlan0IfaceName(), on_radio_mode_change_callback);
1159 return createWifiStatusFromLegacyError(legacy_status);
1160}
1161
Roshan Piuscc338202017-11-02 13:54:09 -07001162void WifiChip::populateModes() {
1163 // The chip combination supported for current devices is fixed.
1164 // They can be one of the following based on device features:
1165 // a) 2 separate modes of operation with 1 interface combination each:
1166 // Mode 1 (STA mode): Will support 1 STA and 1 P2P or NAN(optional)
1167 // concurrent iface operations.
1168 // Mode 2 (AP mode): Will support 1 AP iface operation.
1169 //
1170 // b) 1 mode of operation with 2 interface combinations
1171 // (conditional on isDualInterfaceSupported()):
1172 // Interface Combination 1: Will support 1 STA and 1 P2P or NAN(optional)
1173 // concurrent iface operations.
Roshan Piusaceecb02018-03-01 14:54:26 -08001174 // Interface Combination 2: Will support 1 STA and 1 AP concurrent
Roshan Piuscc338202017-11-02 13:54:09 -07001175 // iface operations.
1176 // If Aware is enabled (conditional on isAwareSupported()), the iface
1177 // combination will be modified to support either P2P or NAN in place of
1178 // just P2P.
1179 if (feature_flags_.lock()->isDualInterfaceSupported()) {
1180 // V2 Iface combinations for Mode Id = 2.
1181 const IWifiChip::ChipIfaceCombinationLimit
1182 chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1183 const IWifiChip::ChipIfaceCombinationLimit
Roshan Piusaceecb02018-03-01 14:54:26 -08001184 chip_iface_combination_limit_2 = {{IfaceType::AP}, 1};
Roshan Piuscc338202017-11-02 13:54:09 -07001185 IWifiChip::ChipIfaceCombinationLimit chip_iface_combination_limit_3;
1186 if (feature_flags_.lock()->isAwareSupported()) {
1187 chip_iface_combination_limit_3 = {{IfaceType::P2P, IfaceType::NAN},
1188 1};
1189 } else {
1190 chip_iface_combination_limit_3 = {{IfaceType::P2P}, 1};
1191 }
1192 const IWifiChip::ChipIfaceCombination chip_iface_combination_1 = {
1193 {chip_iface_combination_limit_1, chip_iface_combination_limit_2}};
1194 const IWifiChip::ChipIfaceCombination chip_iface_combination_2 = {
1195 {chip_iface_combination_limit_1, chip_iface_combination_limit_3}};
1196 const IWifiChip::ChipMode chip_mode = {
1197 kV2ChipModeId,
1198 {chip_iface_combination_1, chip_iface_combination_2}};
1199 modes_ = {chip_mode};
1200 } else {
1201 // V1 Iface combinations for Mode Id = 0. (STA Mode)
1202 const IWifiChip::ChipIfaceCombinationLimit
1203 sta_chip_iface_combination_limit_1 = {{IfaceType::STA}, 1};
1204 IWifiChip::ChipIfaceCombinationLimit sta_chip_iface_combination_limit_2;
1205 if (feature_flags_.lock()->isAwareSupported()) {
1206 sta_chip_iface_combination_limit_2 = {
1207 {IfaceType::P2P, IfaceType::NAN}, 1};
1208 } else {
1209 sta_chip_iface_combination_limit_2 = {{IfaceType::P2P}, 1};
1210 }
1211 const IWifiChip::ChipIfaceCombination sta_chip_iface_combination = {
1212 {sta_chip_iface_combination_limit_1,
1213 sta_chip_iface_combination_limit_2}};
1214 const IWifiChip::ChipMode sta_chip_mode = {
1215 kV1StaChipModeId, {sta_chip_iface_combination}};
1216 // Iface combinations for Mode Id = 1. (AP Mode)
1217 const IWifiChip::ChipIfaceCombinationLimit
1218 ap_chip_iface_combination_limit = {{IfaceType::AP}, 1};
1219 const IWifiChip::ChipIfaceCombination ap_chip_iface_combination = {
1220 {ap_chip_iface_combination_limit}};
1221 const IWifiChip::ChipMode ap_chip_mode = {kV1ApChipModeId,
1222 {ap_chip_iface_combination}};
1223 modes_ = {sta_chip_mode, ap_chip_mode};
1224 }
1225}
1226
1227std::vector<IWifiChip::ChipIfaceCombination>
1228WifiChip::getCurrentModeIfaceCombinations() {
1229 if (!isValidModeId(current_mode_id_)) {
1230 LOG(ERROR) << "Chip not configured in a mode yet";
1231 return {};
1232 }
1233 for (const auto& mode : modes_) {
1234 if (mode.id == current_mode_id_) {
1235 return mode.availableCombinations;
1236 }
1237 }
1238 CHECK(0) << "Expected to find iface combinations for current mode!";
1239 return {};
1240}
1241
1242// Returns a map indexed by IfaceType with the number of ifaces currently
1243// created of the corresponding type.
1244std::map<IfaceType, size_t> WifiChip::getCurrentIfaceCombination() {
1245 std::map<IfaceType, size_t> iface_counts;
1246 iface_counts[IfaceType::AP] = ap_ifaces_.size();
1247 iface_counts[IfaceType::NAN] = nan_ifaces_.size();
1248 iface_counts[IfaceType::P2P] = p2p_ifaces_.size();
1249 iface_counts[IfaceType::STA] = sta_ifaces_.size();
1250 return iface_counts;
1251}
1252
1253// This expands the provided iface combinations to a more parseable
1254// form. Returns a vector of available combinations possible with the number
1255// of ifaces of each type in the combination.
1256// This method is a port of HalDeviceManager.expandIfaceCombos() from framework.
1257std::vector<std::map<IfaceType, size_t>> WifiChip::expandIfaceCombinations(
1258 const IWifiChip::ChipIfaceCombination& combination) {
1259 uint32_t num_expanded_combos = 1;
1260 for (const auto& limit : combination.limits) {
1261 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1262 num_expanded_combos *= limit.types.size();
1263 }
1264 }
1265
1266 // Allocate the vector of expanded combos and reset all iface counts to 0
1267 // in each combo.
1268 std::vector<std::map<IfaceType, size_t>> expanded_combos;
1269 expanded_combos.resize(num_expanded_combos);
1270 for (auto& expanded_combo : expanded_combos) {
1271 for (const auto type :
1272 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1273 expanded_combo[type] = 0;
1274 }
1275 }
1276 uint32_t span = num_expanded_combos;
1277 for (const auto& limit : combination.limits) {
1278 for (uint32_t i = 0; i < limit.maxIfaces; i++) {
1279 span /= limit.types.size();
1280 for (uint32_t k = 0; k < num_expanded_combos; ++k) {
1281 const auto iface_type =
1282 limit.types[(k / span) % limit.types.size()];
1283 expanded_combos[k][iface_type]++;
1284 }
1285 }
1286 }
1287 return expanded_combos;
1288}
1289
1290bool WifiChip::canExpandedIfaceCombinationSupportIfaceOfType(
1291 const std::map<IfaceType, size_t>& combo, IfaceType requested_type) {
1292 const auto current_combo = getCurrentIfaceCombination();
1293
1294 // Check if we have space for 1 more iface of |type| in this combo
1295 for (const auto type :
1296 {IfaceType::AP, IfaceType::NAN, IfaceType::P2P, IfaceType::STA}) {
1297 size_t num_ifaces_needed = current_combo.at(type);
1298 if (type == requested_type) {
1299 num_ifaces_needed++;
1300 }
1301 size_t num_ifaces_allowed = combo.at(type);
1302 if (num_ifaces_needed > num_ifaces_allowed) {
1303 return false;
1304 }
1305 }
1306 return true;
1307}
1308
1309// This method does the following:
1310// a) Enumerate all possible iface combos by expanding the current
1311// ChipIfaceCombination.
1312// b) Check if the requested iface type can be added to the current mode.
1313bool WifiChip::canCurrentModeSupportIfaceOfType(IfaceType type) {
1314 if (!isValidModeId(current_mode_id_)) {
1315 LOG(ERROR) << "Chip not configured in a mode yet";
1316 return false;
1317 }
1318 const auto combinations = getCurrentModeIfaceCombinations();
1319 for (const auto& combination : combinations) {
1320 const auto expanded_combos = expandIfaceCombinations(combination);
1321 for (const auto& expanded_combo : expanded_combos) {
1322 if (canExpandedIfaceCombinationSupportIfaceOfType(expanded_combo,
1323 type)) {
1324 return true;
1325 }
1326 }
1327 }
1328 return false;
1329}
1330
1331bool WifiChip::isValidModeId(ChipModeId mode_id) {
1332 for (const auto& mode : modes_) {
1333 if (mode.id == mode_id) {
1334 return true;
1335 }
1336 }
1337 return false;
1338}
1339
Roshan Pius8e3c7ef2017-11-03 09:43:08 -07001340// Return "wlan0", if "wlan0" is not already in use, else return "wlan1".
1341// This is based on the assumption that we'll have a max of 2 concurrent
1342// AP/STA ifaces.
1343std::string WifiChip::allocateApOrStaIfaceName() {
1344 auto ap_iface = findUsingName(ap_ifaces_, getWlan0IfaceName());
1345 auto sta_iface = findUsingName(sta_ifaces_, getWlan0IfaceName());
1346 if (!ap_iface.get() && !sta_iface.get()) {
1347 return getWlan0IfaceName();
1348 }
1349 ap_iface = findUsingName(ap_ifaces_, getWlan1IfaceName());
1350 sta_iface = findUsingName(sta_ifaces_, getWlan1IfaceName());
1351 if (!ap_iface.get() && !sta_iface.get()) {
1352 return getWlan1IfaceName();
1353 }
1354 // This should never happen. We screwed up somewhere if it did.
1355 CHECK(0) << "wlan0 and wlan1 in use already!";
1356 return {};
1357}
1358
xshu5899e8e2018-01-09 15:36:03 -08001359bool WifiChip::writeRingbufferFilesInternal() {
1360 if (!removeOldFilesInternal()) {
1361 LOG(ERROR) << "Error occurred while deleting old tombstone files";
1362 return false;
1363 }
1364 // write ringbuffers to file
1365 for (const auto& item : ringbuffer_map_) {
1366 const Ringbuffer& cur_buffer = item.second;
1367 if (cur_buffer.getData().empty()) {
1368 continue;
1369 }
1370 const std::string file_path_raw =
1371 kTombstoneFolderPath + item.first + "XXXXXXXXXX";
1372 const int dump_fd = mkstemp(makeCharVec(file_path_raw).data());
1373 if (dump_fd == -1) {
1374 LOG(ERROR) << "create file failed: " << strerror(errno);
1375 return false;
1376 }
1377 unique_fd file_auto_closer(dump_fd);
1378 for (const auto& cur_block : cur_buffer.getData()) {
1379 if (write(dump_fd, cur_block.data(),
1380 sizeof(cur_block[0]) * cur_block.size()) == -1) {
1381 LOG(ERROR) << "Error writing to file " << strerror(errno);
1382 }
1383 }
1384 }
1385 return true;
1386}
1387
Roshan Pius79a99752016-10-04 13:03:58 -07001388} // namespace implementation
Etan Cohen6ce50902017-09-14 07:30:57 -07001389} // namespace V1_2
Roshan Pius3c4e8a32016-10-03 14:53:58 -07001390} // namespace wifi
1391} // namespace hardware
1392} // namespace android