Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 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 | |
| 17 | #include "wifi_chip.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/unique_fd.h> |
| 21 | #include <cutils/properties.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <net/if.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/sysmacros.h> |
| 26 | |
| 27 | #include "aidl_return_util.h" |
| 28 | #include "aidl_struct_util.h" |
| 29 | #include "wifi_status_util.h" |
| 30 | |
| 31 | #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-" |
| 32 | |
| 33 | namespace { |
| 34 | using aidl::android::hardware::wifi::IfaceType; |
| 35 | using aidl::android::hardware::wifi::IWifiChip; |
| 36 | using CoexRestriction = aidl::android::hardware::wifi::IWifiChip::CoexRestriction; |
Shuibing Dai | e5fbcab | 2022-12-19 15:37:19 -0800 | [diff] [blame] | 37 | using ChannelCategoryMask = aidl::android::hardware::wifi::IWifiChip::ChannelCategoryMask; |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 38 | using android::base::unique_fd; |
| 39 | |
| 40 | constexpr char kCpioMagic[] = "070701"; |
| 41 | constexpr size_t kMaxBufferSizeBytes = 1024 * 1024 * 3; |
| 42 | constexpr uint32_t kMaxRingBufferFileAgeSeconds = 60 * 60 * 10; |
| 43 | constexpr uint32_t kMaxRingBufferFileNum = 20; |
| 44 | constexpr char kTombstoneFolderPath[] = "/data/vendor/tombstones/wifi/"; |
| 45 | constexpr char kActiveWlanIfaceNameProperty[] = "wifi.active.interface"; |
| 46 | constexpr char kNoActiveWlanIfaceNamePropertyValue[] = ""; |
| 47 | constexpr unsigned kMaxWlanIfaces = 5; |
| 48 | constexpr char kApBridgeIfacePrefix[] = "ap_br_"; |
| 49 | |
| 50 | template <typename Iface> |
| 51 | void invalidateAndClear(std::vector<std::shared_ptr<Iface>>& ifaces, std::shared_ptr<Iface> iface) { |
| 52 | iface->invalidate(); |
| 53 | ifaces.erase(std::remove(ifaces.begin(), ifaces.end(), iface), ifaces.end()); |
| 54 | } |
| 55 | |
| 56 | template <typename Iface> |
| 57 | void invalidateAndClearAll(std::vector<std::shared_ptr<Iface>>& ifaces) { |
| 58 | for (const auto& iface : ifaces) { |
| 59 | iface->invalidate(); |
| 60 | } |
| 61 | ifaces.clear(); |
| 62 | } |
| 63 | |
| 64 | template <typename Iface> |
| 65 | std::vector<std::string> getNames(std::vector<std::shared_ptr<Iface>>& ifaces) { |
| 66 | std::vector<std::string> names; |
| 67 | for (const auto& iface : ifaces) { |
| 68 | names.emplace_back(iface->getName()); |
| 69 | } |
| 70 | return names; |
| 71 | } |
| 72 | |
| 73 | template <typename Iface> |
| 74 | std::shared_ptr<Iface> findUsingName(std::vector<std::shared_ptr<Iface>>& ifaces, |
| 75 | const std::string& name) { |
| 76 | std::vector<std::string> names; |
| 77 | for (const auto& iface : ifaces) { |
| 78 | if (name == iface->getName()) { |
| 79 | return iface; |
| 80 | } |
| 81 | } |
| 82 | return nullptr; |
| 83 | } |
| 84 | |
| 85 | std::string getWlanIfaceName(unsigned idx) { |
| 86 | if (idx >= kMaxWlanIfaces) { |
| 87 | CHECK(false) << "Requested interface beyond wlan" << kMaxWlanIfaces; |
| 88 | return {}; |
| 89 | } |
| 90 | |
| 91 | std::array<char, PROPERTY_VALUE_MAX> buffer; |
| 92 | if (idx == 0 || idx == 1) { |
| 93 | const char* altPropName = (idx == 0) ? "wifi.interface" : "wifi.concurrent.interface"; |
| 94 | auto res = property_get(altPropName, buffer.data(), nullptr); |
| 95 | if (res > 0) return buffer.data(); |
| 96 | } |
| 97 | std::string propName = "wifi.interface." + std::to_string(idx); |
| 98 | auto res = property_get(propName.c_str(), buffer.data(), nullptr); |
| 99 | if (res > 0) return buffer.data(); |
| 100 | |
| 101 | return "wlan" + std::to_string(idx); |
| 102 | } |
| 103 | |
| 104 | // Returns the dedicated iface name if defined. |
| 105 | // Returns two ifaces in bridged mode. |
| 106 | std::vector<std::string> getPredefinedApIfaceNames(bool is_bridged) { |
| 107 | std::vector<std::string> ifnames; |
| 108 | std::array<char, PROPERTY_VALUE_MAX> buffer; |
| 109 | buffer.fill(0); |
| 110 | if (property_get("ro.vendor.wifi.sap.interface", buffer.data(), nullptr) == 0) { |
| 111 | return ifnames; |
| 112 | } |
| 113 | ifnames.push_back(buffer.data()); |
| 114 | if (is_bridged) { |
| 115 | buffer.fill(0); |
| 116 | if (property_get("ro.vendor.wifi.sap.concurrent.iface", buffer.data(), nullptr) == 0) { |
| 117 | return ifnames; |
| 118 | } |
| 119 | ifnames.push_back(buffer.data()); |
| 120 | } |
| 121 | return ifnames; |
| 122 | } |
| 123 | |
| 124 | std::string getPredefinedP2pIfaceName() { |
| 125 | std::array<char, PROPERTY_VALUE_MAX> primaryIfaceName; |
| 126 | char p2pParentIfname[100]; |
| 127 | std::string p2pDevIfName = ""; |
| 128 | std::array<char, PROPERTY_VALUE_MAX> buffer; |
| 129 | property_get("wifi.direct.interface", buffer.data(), "p2p0"); |
| 130 | if (strncmp(buffer.data(), P2P_MGMT_DEVICE_PREFIX, strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) { |
| 131 | /* Get the p2p parent interface name from p2p device interface name set |
| 132 | * in property */ |
| 133 | strlcpy(p2pParentIfname, buffer.data() + strlen(P2P_MGMT_DEVICE_PREFIX), |
| 134 | strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX)); |
| 135 | if (property_get(kActiveWlanIfaceNameProperty, primaryIfaceName.data(), nullptr) == 0) { |
| 136 | return buffer.data(); |
| 137 | } |
| 138 | /* Check if the parent interface derived from p2p device interface name |
| 139 | * is active */ |
| 140 | if (strncmp(p2pParentIfname, primaryIfaceName.data(), |
| 141 | strlen(buffer.data()) - strlen(P2P_MGMT_DEVICE_PREFIX)) != 0) { |
| 142 | /* |
| 143 | * Update the predefined p2p device interface parent interface name |
| 144 | * with current active wlan interface |
| 145 | */ |
| 146 | p2pDevIfName += P2P_MGMT_DEVICE_PREFIX; |
| 147 | p2pDevIfName += primaryIfaceName.data(); |
| 148 | LOG(INFO) << "update the p2p device interface name to " << p2pDevIfName.c_str(); |
| 149 | return p2pDevIfName; |
| 150 | } |
| 151 | } |
| 152 | return buffer.data(); |
| 153 | } |
| 154 | |
| 155 | // Returns the dedicated iface name if one is defined. |
| 156 | std::string getPredefinedNanIfaceName() { |
| 157 | std::array<char, PROPERTY_VALUE_MAX> buffer; |
| 158 | if (property_get("wifi.aware.interface", buffer.data(), nullptr) == 0) { |
| 159 | return {}; |
| 160 | } |
| 161 | return buffer.data(); |
| 162 | } |
| 163 | |
| 164 | void setActiveWlanIfaceNameProperty(const std::string& ifname) { |
| 165 | auto res = property_set(kActiveWlanIfaceNameProperty, ifname.data()); |
| 166 | if (res != 0) { |
| 167 | PLOG(ERROR) << "Failed to set active wlan iface name property"; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // Delete files that meet either condition: |
| 172 | // 1. Older than a predefined time in the wifi tombstone dir. |
| 173 | // 2. Files in excess to a predefined amount, starting from the oldest ones |
| 174 | bool removeOldFilesInternal() { |
| 175 | time_t now = time(0); |
| 176 | const time_t delete_files_before = now - kMaxRingBufferFileAgeSeconds; |
| 177 | std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(kTombstoneFolderPath), closedir); |
| 178 | if (!dir_dump) { |
| 179 | PLOG(ERROR) << "Failed to open directory"; |
| 180 | return false; |
| 181 | } |
| 182 | struct dirent* dp; |
| 183 | bool success = true; |
| 184 | std::list<std::pair<const time_t, std::string>> valid_files; |
| 185 | while ((dp = readdir(dir_dump.get()))) { |
| 186 | if (dp->d_type != DT_REG) { |
| 187 | continue; |
| 188 | } |
| 189 | std::string cur_file_name(dp->d_name); |
| 190 | struct stat cur_file_stat; |
| 191 | std::string cur_file_path = kTombstoneFolderPath + cur_file_name; |
| 192 | if (stat(cur_file_path.c_str(), &cur_file_stat) == -1) { |
| 193 | PLOG(ERROR) << "Failed to get file stat for " << cur_file_path; |
| 194 | success = false; |
| 195 | continue; |
| 196 | } |
| 197 | const time_t cur_file_time = cur_file_stat.st_mtime; |
| 198 | valid_files.push_back(std::pair<const time_t, std::string>(cur_file_time, cur_file_path)); |
| 199 | } |
| 200 | valid_files.sort(); // sort the list of files by last modified time from |
| 201 | // small to big. |
| 202 | uint32_t cur_file_count = valid_files.size(); |
| 203 | for (auto cur_file : valid_files) { |
| 204 | if (cur_file_count > kMaxRingBufferFileNum || cur_file.first < delete_files_before) { |
| 205 | if (unlink(cur_file.second.c_str()) != 0) { |
| 206 | PLOG(ERROR) << "Error deleting file"; |
| 207 | success = false; |
| 208 | } |
| 209 | cur_file_count--; |
| 210 | } else { |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | return success; |
| 215 | } |
| 216 | |
| 217 | // Helper function for |cpioArchiveFilesInDir| |
| 218 | bool cpioWriteHeader(int out_fd, struct stat& st, const char* file_name, size_t file_name_len) { |
| 219 | const int buf_size = 32 * 1024; |
| 220 | std::array<char, buf_size> read_buf; |
| 221 | ssize_t llen = snprintf( |
| 222 | read_buf.data(), buf_size, "%s%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X%08X", |
| 223 | kCpioMagic, static_cast<int>(st.st_ino), st.st_mode, st.st_uid, st.st_gid, |
| 224 | static_cast<int>(st.st_nlink), static_cast<int>(st.st_mtime), |
| 225 | static_cast<int>(st.st_size), major(st.st_dev), minor(st.st_dev), major(st.st_rdev), |
| 226 | minor(st.st_rdev), static_cast<uint32_t>(file_name_len), 0); |
| 227 | if (write(out_fd, read_buf.data(), llen < buf_size ? llen : buf_size - 1) == -1) { |
| 228 | PLOG(ERROR) << "Error writing cpio header to file " << file_name; |
| 229 | return false; |
| 230 | } |
| 231 | if (write(out_fd, file_name, file_name_len) == -1) { |
| 232 | PLOG(ERROR) << "Error writing filename to file " << file_name; |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | // NUL Pad header up to 4 multiple bytes. |
| 237 | llen = (llen + file_name_len) % 4; |
| 238 | if (llen != 0) { |
| 239 | const uint32_t zero = 0; |
| 240 | if (write(out_fd, &zero, 4 - llen) == -1) { |
| 241 | PLOG(ERROR) << "Error padding 0s to file " << file_name; |
| 242 | return false; |
| 243 | } |
| 244 | } |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | // Helper function for |cpioArchiveFilesInDir| |
| 249 | size_t cpioWriteFileContent(int fd_read, int out_fd, struct stat& st) { |
| 250 | // writing content of file |
| 251 | std::array<char, 32 * 1024> read_buf; |
| 252 | ssize_t llen = st.st_size; |
| 253 | size_t n_error = 0; |
| 254 | while (llen > 0) { |
| 255 | ssize_t bytes_read = read(fd_read, read_buf.data(), read_buf.size()); |
| 256 | if (bytes_read == -1) { |
| 257 | PLOG(ERROR) << "Error reading file"; |
| 258 | return ++n_error; |
| 259 | } |
| 260 | llen -= bytes_read; |
| 261 | if (write(out_fd, read_buf.data(), bytes_read) == -1) { |
| 262 | PLOG(ERROR) << "Error writing data to file"; |
| 263 | return ++n_error; |
| 264 | } |
| 265 | if (bytes_read == 0) { // this should never happen, but just in case |
| 266 | // to unstuck from while loop |
| 267 | PLOG(ERROR) << "Unexpected read result"; |
| 268 | n_error++; |
| 269 | break; |
| 270 | } |
| 271 | } |
| 272 | llen = st.st_size % 4; |
| 273 | if (llen != 0) { |
| 274 | const uint32_t zero = 0; |
| 275 | if (write(out_fd, &zero, 4 - llen) == -1) { |
| 276 | PLOG(ERROR) << "Error padding 0s to file"; |
| 277 | return ++n_error; |
| 278 | } |
| 279 | } |
| 280 | return n_error; |
| 281 | } |
| 282 | |
| 283 | // Helper function for |cpioArchiveFilesInDir| |
| 284 | bool cpioWriteFileTrailer(int out_fd) { |
| 285 | const int buf_size = 4096; |
| 286 | std::array<char, buf_size> read_buf; |
| 287 | read_buf.fill(0); |
| 288 | ssize_t llen = snprintf(read_buf.data(), 4096, "070701%040X%056X%08XTRAILER!!!", 1, 0x0b, 0); |
| 289 | if (write(out_fd, read_buf.data(), (llen < buf_size ? llen : buf_size - 1) + 4) == -1) { |
| 290 | PLOG(ERROR) << "Error writing trailing bytes"; |
| 291 | return false; |
| 292 | } |
| 293 | return true; |
| 294 | } |
| 295 | |
| 296 | // Archives all files in |input_dir| and writes result into |out_fd| |
| 297 | // Logic obtained from //external/toybox/toys/posix/cpio.c "Output cpio archive" |
| 298 | // portion |
| 299 | size_t cpioArchiveFilesInDir(int out_fd, const char* input_dir) { |
| 300 | struct dirent* dp; |
| 301 | size_t n_error = 0; |
| 302 | std::unique_ptr<DIR, decltype(&closedir)> dir_dump(opendir(input_dir), closedir); |
| 303 | if (!dir_dump) { |
| 304 | PLOG(ERROR) << "Failed to open directory"; |
| 305 | return ++n_error; |
| 306 | } |
| 307 | while ((dp = readdir(dir_dump.get()))) { |
| 308 | if (dp->d_type != DT_REG) { |
| 309 | continue; |
| 310 | } |
| 311 | std::string cur_file_name(dp->d_name); |
| 312 | struct stat st; |
| 313 | const std::string cur_file_path = kTombstoneFolderPath + cur_file_name; |
| 314 | if (stat(cur_file_path.c_str(), &st) == -1) { |
| 315 | PLOG(ERROR) << "Failed to get file stat for " << cur_file_path; |
| 316 | n_error++; |
| 317 | continue; |
| 318 | } |
| 319 | const int fd_read = open(cur_file_path.c_str(), O_RDONLY); |
| 320 | if (fd_read == -1) { |
| 321 | PLOG(ERROR) << "Failed to open file " << cur_file_path; |
| 322 | n_error++; |
| 323 | continue; |
| 324 | } |
| 325 | std::string file_name_with_last_modified_time = |
| 326 | cur_file_name + "-" + std::to_string(st.st_mtime); |
| 327 | // string.size() does not include the null terminator. The cpio FreeBSD |
| 328 | // file header expects the null character to be included in the length. |
| 329 | const size_t file_name_len = file_name_with_last_modified_time.size() + 1; |
| 330 | unique_fd file_auto_closer(fd_read); |
| 331 | if (!cpioWriteHeader(out_fd, st, file_name_with_last_modified_time.c_str(), |
| 332 | file_name_len)) { |
| 333 | return ++n_error; |
| 334 | } |
| 335 | size_t write_error = cpioWriteFileContent(fd_read, out_fd, st); |
| 336 | if (write_error) { |
| 337 | return n_error + write_error; |
| 338 | } |
| 339 | } |
| 340 | if (!cpioWriteFileTrailer(out_fd)) { |
| 341 | return ++n_error; |
| 342 | } |
| 343 | return n_error; |
| 344 | } |
| 345 | |
| 346 | // Helper function to create a non-const char*. |
| 347 | std::vector<char> makeCharVec(const std::string& str) { |
| 348 | std::vector<char> vec(str.size() + 1); |
| 349 | vec.assign(str.begin(), str.end()); |
| 350 | vec.push_back('\0'); |
| 351 | return vec; |
| 352 | } |
| 353 | |
| 354 | } // namespace |
| 355 | |
| 356 | namespace aidl { |
| 357 | namespace android { |
| 358 | namespace hardware { |
| 359 | namespace wifi { |
| 360 | using aidl_return_util::validateAndCall; |
| 361 | using aidl_return_util::validateAndCallWithLock; |
| 362 | |
| 363 | WifiChip::WifiChip(int32_t chip_id, bool is_primary, |
| 364 | const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal, |
| 365 | const std::weak_ptr<mode_controller::WifiModeController> mode_controller, |
| 366 | const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util, |
| 367 | const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags, |
| 368 | const std::function<void(const std::string&)>& handler) |
| 369 | : chip_id_(chip_id), |
| 370 | legacy_hal_(legacy_hal), |
| 371 | mode_controller_(mode_controller), |
| 372 | iface_util_(iface_util), |
| 373 | is_valid_(true), |
| 374 | current_mode_id_(feature_flags::chip_mode_ids::kInvalid), |
| 375 | modes_(feature_flags.lock()->getChipModes(is_primary)), |
| 376 | debug_ring_buffer_cb_registered_(false), |
| 377 | subsystemCallbackHandler_(handler) { |
| 378 | setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue); |
| 379 | } |
| 380 | |
| 381 | std::shared_ptr<WifiChip> WifiChip::create( |
| 382 | int32_t chip_id, bool is_primary, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal, |
| 383 | const std::weak_ptr<mode_controller::WifiModeController> mode_controller, |
| 384 | const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util, |
| 385 | const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags, |
| 386 | const std::function<void(const std::string&)>& handler) { |
| 387 | std::shared_ptr<WifiChip> ptr = ndk::SharedRefBase::make<WifiChip>( |
| 388 | chip_id, is_primary, legacy_hal, mode_controller, iface_util, feature_flags, handler); |
| 389 | std::weak_ptr<WifiChip> weak_ptr_this(ptr); |
| 390 | ptr->setWeakPtr(weak_ptr_this); |
| 391 | return ptr; |
| 392 | } |
| 393 | |
| 394 | void WifiChip::invalidate() { |
| 395 | if (!writeRingbufferFilesInternal()) { |
| 396 | LOG(ERROR) << "Error writing files to flash"; |
| 397 | } |
| 398 | invalidateAndRemoveAllIfaces(); |
| 399 | setActiveWlanIfaceNameProperty(kNoActiveWlanIfaceNamePropertyValue); |
| 400 | legacy_hal_.reset(); |
| 401 | event_cb_handler_.invalidate(); |
| 402 | is_valid_ = false; |
| 403 | } |
| 404 | |
| 405 | void WifiChip::setWeakPtr(std::weak_ptr<WifiChip> ptr) { |
| 406 | weak_ptr_this_ = ptr; |
| 407 | } |
| 408 | |
| 409 | bool WifiChip::isValid() { |
| 410 | return is_valid_; |
| 411 | } |
| 412 | |
| 413 | std::set<std::shared_ptr<IWifiChipEventCallback>> WifiChip::getEventCallbacks() { |
| 414 | return event_cb_handler_.getCallbacks(); |
| 415 | } |
| 416 | |
| 417 | ndk::ScopedAStatus WifiChip::getId(int32_t* _aidl_return) { |
| 418 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, &WifiChip::getIdInternal, |
| 419 | _aidl_return); |
| 420 | } |
| 421 | |
| 422 | ndk::ScopedAStatus WifiChip::registerEventCallback( |
| 423 | const std::shared_ptr<IWifiChipEventCallback>& event_callback) { |
| 424 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 425 | &WifiChip::registerEventCallbackInternal, event_callback); |
| 426 | } |
| 427 | |
| 428 | ndk::ScopedAStatus WifiChip::getCapabilities(IWifiChip::ChipCapabilityMask* _aidl_return) { |
| 429 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 430 | &WifiChip::getCapabilitiesInternal, _aidl_return); |
| 431 | } |
| 432 | |
| 433 | ndk::ScopedAStatus WifiChip::getAvailableModes(std::vector<IWifiChip::ChipMode>* _aidl_return) { |
| 434 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 435 | &WifiChip::getAvailableModesInternal, _aidl_return); |
| 436 | } |
| 437 | |
| 438 | ndk::ScopedAStatus WifiChip::configureChip(int32_t in_modeId) { |
| 439 | return validateAndCallWithLock(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 440 | &WifiChip::configureChipInternal, in_modeId); |
| 441 | } |
| 442 | |
| 443 | ndk::ScopedAStatus WifiChip::getMode(int32_t* _aidl_return) { |
| 444 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 445 | &WifiChip::getModeInternal, _aidl_return); |
| 446 | } |
| 447 | |
| 448 | ndk::ScopedAStatus WifiChip::requestChipDebugInfo(IWifiChip::ChipDebugInfo* _aidl_return) { |
| 449 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 450 | &WifiChip::requestChipDebugInfoInternal, _aidl_return); |
| 451 | } |
| 452 | |
| 453 | ndk::ScopedAStatus WifiChip::requestDriverDebugDump(std::vector<uint8_t>* _aidl_return) { |
| 454 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 455 | &WifiChip::requestDriverDebugDumpInternal, _aidl_return); |
| 456 | } |
| 457 | |
| 458 | ndk::ScopedAStatus WifiChip::requestFirmwareDebugDump(std::vector<uint8_t>* _aidl_return) { |
| 459 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 460 | &WifiChip::requestFirmwareDebugDumpInternal, _aidl_return); |
| 461 | } |
| 462 | |
| 463 | ndk::ScopedAStatus WifiChip::createApIface(std::shared_ptr<IWifiApIface>* _aidl_return) { |
| 464 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 465 | &WifiChip::createApIfaceInternal, _aidl_return); |
| 466 | } |
| 467 | |
| 468 | ndk::ScopedAStatus WifiChip::createBridgedApIface(std::shared_ptr<IWifiApIface>* _aidl_return) { |
| 469 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 470 | &WifiChip::createBridgedApIfaceInternal, _aidl_return); |
| 471 | } |
| 472 | |
| 473 | ndk::ScopedAStatus WifiChip::getApIfaceNames(std::vector<std::string>* _aidl_return) { |
| 474 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 475 | &WifiChip::getApIfaceNamesInternal, _aidl_return); |
| 476 | } |
| 477 | |
| 478 | ndk::ScopedAStatus WifiChip::getApIface(const std::string& in_ifname, |
| 479 | std::shared_ptr<IWifiApIface>* _aidl_return) { |
| 480 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 481 | &WifiChip::getApIfaceInternal, _aidl_return, in_ifname); |
| 482 | } |
| 483 | |
| 484 | ndk::ScopedAStatus WifiChip::removeApIface(const std::string& in_ifname) { |
| 485 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 486 | &WifiChip::removeApIfaceInternal, in_ifname); |
| 487 | } |
| 488 | |
| 489 | ndk::ScopedAStatus WifiChip::removeIfaceInstanceFromBridgedApIface( |
| 490 | const std::string& in_brIfaceName, const std::string& in_ifaceInstanceName) { |
| 491 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 492 | &WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal, in_brIfaceName, |
| 493 | in_ifaceInstanceName); |
| 494 | } |
| 495 | |
| 496 | ndk::ScopedAStatus WifiChip::createNanIface(std::shared_ptr<IWifiNanIface>* _aidl_return) { |
| 497 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 498 | &WifiChip::createNanIfaceInternal, _aidl_return); |
| 499 | } |
| 500 | |
| 501 | ndk::ScopedAStatus WifiChip::getNanIfaceNames(std::vector<std::string>* _aidl_return) { |
| 502 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 503 | &WifiChip::getNanIfaceNamesInternal, _aidl_return); |
| 504 | } |
| 505 | |
| 506 | ndk::ScopedAStatus WifiChip::getNanIface(const std::string& in_ifname, |
| 507 | std::shared_ptr<IWifiNanIface>* _aidl_return) { |
| 508 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 509 | &WifiChip::getNanIfaceInternal, _aidl_return, in_ifname); |
| 510 | } |
| 511 | |
| 512 | ndk::ScopedAStatus WifiChip::removeNanIface(const std::string& in_ifname) { |
| 513 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 514 | &WifiChip::removeNanIfaceInternal, in_ifname); |
| 515 | } |
| 516 | |
| 517 | ndk::ScopedAStatus WifiChip::createP2pIface(std::shared_ptr<IWifiP2pIface>* _aidl_return) { |
| 518 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 519 | &WifiChip::createP2pIfaceInternal, _aidl_return); |
| 520 | } |
| 521 | |
| 522 | ndk::ScopedAStatus WifiChip::getP2pIfaceNames(std::vector<std::string>* _aidl_return) { |
| 523 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 524 | &WifiChip::getP2pIfaceNamesInternal, _aidl_return); |
| 525 | } |
| 526 | |
| 527 | ndk::ScopedAStatus WifiChip::getP2pIface(const std::string& in_ifname, |
| 528 | std::shared_ptr<IWifiP2pIface>* _aidl_return) { |
| 529 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 530 | &WifiChip::getP2pIfaceInternal, _aidl_return, in_ifname); |
| 531 | } |
| 532 | |
| 533 | ndk::ScopedAStatus WifiChip::removeP2pIface(const std::string& in_ifname) { |
| 534 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 535 | &WifiChip::removeP2pIfaceInternal, in_ifname); |
| 536 | } |
| 537 | |
| 538 | ndk::ScopedAStatus WifiChip::createStaIface(std::shared_ptr<IWifiStaIface>* _aidl_return) { |
| 539 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 540 | &WifiChip::createStaIfaceInternal, _aidl_return); |
| 541 | } |
| 542 | |
| 543 | ndk::ScopedAStatus WifiChip::getStaIfaceNames(std::vector<std::string>* _aidl_return) { |
| 544 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 545 | &WifiChip::getStaIfaceNamesInternal, _aidl_return); |
| 546 | } |
| 547 | |
| 548 | ndk::ScopedAStatus WifiChip::getStaIface(const std::string& in_ifname, |
| 549 | std::shared_ptr<IWifiStaIface>* _aidl_return) { |
| 550 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 551 | &WifiChip::getStaIfaceInternal, _aidl_return, in_ifname); |
| 552 | } |
| 553 | |
| 554 | ndk::ScopedAStatus WifiChip::removeStaIface(const std::string& in_ifname) { |
| 555 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 556 | &WifiChip::removeStaIfaceInternal, in_ifname); |
| 557 | } |
| 558 | |
| 559 | ndk::ScopedAStatus WifiChip::createRttController( |
| 560 | const std::shared_ptr<IWifiStaIface>& in_boundIface, |
| 561 | std::shared_ptr<IWifiRttController>* _aidl_return) { |
| 562 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 563 | &WifiChip::createRttControllerInternal, _aidl_return, in_boundIface); |
| 564 | } |
| 565 | |
| 566 | ndk::ScopedAStatus WifiChip::getDebugRingBuffersStatus( |
| 567 | std::vector<WifiDebugRingBufferStatus>* _aidl_return) { |
| 568 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 569 | &WifiChip::getDebugRingBuffersStatusInternal, _aidl_return); |
| 570 | } |
| 571 | |
| 572 | ndk::ScopedAStatus WifiChip::startLoggingToDebugRingBuffer( |
| 573 | const std::string& in_ringName, WifiDebugRingBufferVerboseLevel in_verboseLevel, |
| 574 | int32_t in_maxIntervalInSec, int32_t in_minDataSizeInBytes) { |
| 575 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 576 | &WifiChip::startLoggingToDebugRingBufferInternal, in_ringName, |
| 577 | in_verboseLevel, in_maxIntervalInSec, in_minDataSizeInBytes); |
| 578 | } |
| 579 | |
| 580 | ndk::ScopedAStatus WifiChip::forceDumpToDebugRingBuffer(const std::string& in_ringName) { |
| 581 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 582 | &WifiChip::forceDumpToDebugRingBufferInternal, in_ringName); |
| 583 | } |
| 584 | |
| 585 | ndk::ScopedAStatus WifiChip::flushRingBufferToFile() { |
| 586 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 587 | &WifiChip::flushRingBufferToFileInternal); |
| 588 | } |
| 589 | |
| 590 | ndk::ScopedAStatus WifiChip::stopLoggingToDebugRingBuffer() { |
| 591 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 592 | &WifiChip::stopLoggingToDebugRingBufferInternal); |
| 593 | } |
| 594 | |
| 595 | ndk::ScopedAStatus WifiChip::getDebugHostWakeReasonStats( |
| 596 | WifiDebugHostWakeReasonStats* _aidl_return) { |
| 597 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 598 | &WifiChip::getDebugHostWakeReasonStatsInternal, _aidl_return); |
| 599 | } |
| 600 | |
| 601 | ndk::ScopedAStatus WifiChip::enableDebugErrorAlerts(bool in_enable) { |
| 602 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 603 | &WifiChip::enableDebugErrorAlertsInternal, in_enable); |
| 604 | } |
| 605 | |
| 606 | ndk::ScopedAStatus WifiChip::selectTxPowerScenario(IWifiChip::TxPowerScenario in_scenario) { |
| 607 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 608 | &WifiChip::selectTxPowerScenarioInternal, in_scenario); |
| 609 | } |
| 610 | |
| 611 | ndk::ScopedAStatus WifiChip::resetTxPowerScenario() { |
| 612 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 613 | &WifiChip::resetTxPowerScenarioInternal); |
| 614 | } |
| 615 | |
| 616 | ndk::ScopedAStatus WifiChip::setLatencyMode(IWifiChip::LatencyMode in_mode) { |
| 617 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 618 | &WifiChip::setLatencyModeInternal, in_mode); |
| 619 | } |
| 620 | |
| 621 | binder_status_t WifiChip::dump(int fd, const char**, uint32_t) { |
| 622 | { |
| 623 | std::unique_lock<std::mutex> lk(lock_t); |
| 624 | for (const auto& item : ringbuffer_map_) { |
| 625 | forceDumpToDebugRingBufferInternal(item.first); |
| 626 | } |
| 627 | // unique_lock unlocked here |
| 628 | } |
| 629 | usleep(100 * 1000); // sleep for 100 milliseconds to wait for |
| 630 | // ringbuffer updates. |
| 631 | if (!writeRingbufferFilesInternal()) { |
| 632 | LOG(ERROR) << "Error writing files to flash"; |
| 633 | } |
| 634 | uint32_t n_error = cpioArchiveFilesInDir(fd, kTombstoneFolderPath); |
| 635 | if (n_error != 0) { |
| 636 | LOG(ERROR) << n_error << " errors occurred in cpio function"; |
| 637 | } |
| 638 | fsync(fd); |
| 639 | return STATUS_OK; |
| 640 | } |
| 641 | |
| 642 | ndk::ScopedAStatus WifiChip::setMultiStaPrimaryConnection(const std::string& in_ifName) { |
| 643 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 644 | &WifiChip::setMultiStaPrimaryConnectionInternal, in_ifName); |
| 645 | } |
| 646 | |
| 647 | ndk::ScopedAStatus WifiChip::setMultiStaUseCase(IWifiChip::MultiStaUseCase in_useCase) { |
| 648 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 649 | &WifiChip::setMultiStaUseCaseInternal, in_useCase); |
| 650 | } |
| 651 | |
| 652 | ndk::ScopedAStatus WifiChip::setCoexUnsafeChannels( |
| 653 | const std::vector<IWifiChip::CoexUnsafeChannel>& in_unsafeChannels, |
| 654 | CoexRestriction in_restrictions) { |
| 655 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 656 | &WifiChip::setCoexUnsafeChannelsInternal, in_unsafeChannels, |
| 657 | in_restrictions); |
| 658 | } |
| 659 | |
| 660 | ndk::ScopedAStatus WifiChip::setCountryCode(const std::array<uint8_t, 2>& in_code) { |
| 661 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID, |
| 662 | &WifiChip::setCountryCodeInternal, in_code); |
| 663 | } |
| 664 | |
| 665 | ndk::ScopedAStatus WifiChip::getUsableChannels(WifiBand in_band, WifiIfaceMode in_ifaceModeMask, |
| 666 | UsableChannelFilter in_filterMask, |
| 667 | std::vector<WifiUsableChannel>* _aidl_return) { |
| 668 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 669 | &WifiChip::getUsableChannelsInternal, _aidl_return, in_band, |
| 670 | in_ifaceModeMask, in_filterMask); |
| 671 | } |
| 672 | |
Oscar Shu | ab8313c | 2022-12-13 00:55:11 +0000 | [diff] [blame] | 673 | ndk::ScopedAStatus WifiChip::setAfcChannelAllowance( |
| 674 | const std::vector<AvailableAfcFrequencyInfo>& availableAfcFrequencyInfo) { |
| 675 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 676 | &WifiChip::setAfcChannelAllowanceInternal, availableAfcFrequencyInfo); |
| 677 | } |
| 678 | |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 679 | ndk::ScopedAStatus WifiChip::triggerSubsystemRestart() { |
| 680 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 681 | &WifiChip::triggerSubsystemRestartInternal); |
| 682 | } |
| 683 | |
| 684 | ndk::ScopedAStatus WifiChip::getSupportedRadioCombinationsMatrix( |
| 685 | WifiRadioCombinationMatrix* _aidl_return) { |
| 686 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 687 | &WifiChip::getSupportedRadioCombinationsMatrixInternal, _aidl_return); |
| 688 | } |
| 689 | |
Mahesh KKV | c84d377 | 2022-12-02 16:53:28 -0800 | [diff] [blame] | 690 | ndk::ScopedAStatus WifiChip::getWifiChipCapabilities(WifiChipCapabilities* _aidl_return) { |
| 691 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 692 | &WifiChip::getWifiChipCapabilitiesInternal, _aidl_return); |
| 693 | } |
| 694 | |
Shuibing Dai | e5fbcab | 2022-12-19 15:37:19 -0800 | [diff] [blame] | 695 | ndk::ScopedAStatus WifiChip::enableStaChannelForPeerNetwork( |
| 696 | ChannelCategoryMask in_channelCategoryEnableFlag) { |
| 697 | return validateAndCall(this, WifiStatusCode::ERROR_WIFI_CHIP_INVALID, |
| 698 | &WifiChip::enableStaChannelForPeerNetworkInternal, |
| 699 | in_channelCategoryEnableFlag); |
| 700 | } |
| 701 | |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 702 | void WifiChip::invalidateAndRemoveAllIfaces() { |
| 703 | invalidateAndClearBridgedApAll(); |
| 704 | invalidateAndClearAll(ap_ifaces_); |
| 705 | invalidateAndClearAll(nan_ifaces_); |
| 706 | invalidateAndClearAll(p2p_ifaces_); |
| 707 | invalidateAndClearAll(sta_ifaces_); |
| 708 | // Since all the ifaces are invalid now, all RTT controller objects |
| 709 | // using those ifaces also need to be invalidated. |
| 710 | for (const auto& rtt : rtt_controllers_) { |
| 711 | rtt->invalidate(); |
| 712 | } |
| 713 | rtt_controllers_.clear(); |
| 714 | } |
| 715 | |
| 716 | void WifiChip::invalidateAndRemoveDependencies(const std::string& removed_iface_name) { |
| 717 | for (auto it = nan_ifaces_.begin(); it != nan_ifaces_.end();) { |
| 718 | auto nan_iface = *it; |
| 719 | if (nan_iface->getName() == removed_iface_name) { |
| 720 | nan_iface->invalidate(); |
| 721 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 722 | if (!callback->onIfaceRemoved(IfaceType::NAN_IFACE, removed_iface_name).isOk()) { |
| 723 | LOG(ERROR) << "Failed to invoke onIfaceRemoved callback"; |
| 724 | } |
| 725 | } |
| 726 | it = nan_ifaces_.erase(it); |
| 727 | } else { |
| 728 | ++it; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | for (auto it = rtt_controllers_.begin(); it != rtt_controllers_.end();) { |
| 733 | auto rtt = *it; |
| 734 | if (rtt->getIfaceName() == removed_iface_name) { |
| 735 | rtt->invalidate(); |
| 736 | it = rtt_controllers_.erase(it); |
| 737 | } else { |
| 738 | ++it; |
| 739 | } |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | std::pair<int32_t, ndk::ScopedAStatus> WifiChip::getIdInternal() { |
| 744 | return {chip_id_, ndk::ScopedAStatus::ok()}; |
| 745 | } |
| 746 | |
| 747 | ndk::ScopedAStatus WifiChip::registerEventCallbackInternal( |
| 748 | const std::shared_ptr<IWifiChipEventCallback>& event_callback) { |
| 749 | if (!event_cb_handler_.addCallback(event_callback)) { |
| 750 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 751 | } |
| 752 | return ndk::ScopedAStatus::ok(); |
| 753 | } |
| 754 | |
| 755 | std::pair<IWifiChip::ChipCapabilityMask, ndk::ScopedAStatus> WifiChip::getCapabilitiesInternal() { |
| 756 | legacy_hal::wifi_error legacy_status; |
| 757 | uint64_t legacy_feature_set; |
| 758 | uint32_t legacy_logger_feature_set; |
| 759 | const auto ifname = getFirstActiveWlanIfaceName(); |
| 760 | std::tie(legacy_status, legacy_feature_set) = |
| 761 | legacy_hal_.lock()->getSupportedFeatureSet(ifname); |
| 762 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 763 | return {IWifiChip::ChipCapabilityMask{}, createWifiStatusFromLegacyError(legacy_status)}; |
| 764 | } |
| 765 | std::tie(legacy_status, legacy_logger_feature_set) = |
| 766 | legacy_hal_.lock()->getLoggerSupportedFeatureSet(ifname); |
| 767 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 768 | // some devices don't support querying logger feature set |
| 769 | legacy_logger_feature_set = 0; |
| 770 | } |
| 771 | uint32_t aidl_caps; |
| 772 | if (!aidl_struct_util::convertLegacyFeaturesToAidlChipCapabilities( |
| 773 | legacy_feature_set, legacy_logger_feature_set, &aidl_caps)) { |
| 774 | return {IWifiChip::ChipCapabilityMask{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)}; |
| 775 | } |
| 776 | return {static_cast<IWifiChip::ChipCapabilityMask>(aidl_caps), ndk::ScopedAStatus::ok()}; |
| 777 | } |
| 778 | |
| 779 | std::pair<std::vector<IWifiChip::ChipMode>, ndk::ScopedAStatus> |
| 780 | WifiChip::getAvailableModesInternal() { |
| 781 | return {modes_, ndk::ScopedAStatus::ok()}; |
| 782 | } |
| 783 | |
| 784 | ndk::ScopedAStatus WifiChip::configureChipInternal( |
| 785 | /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, int32_t mode_id) { |
| 786 | if (!isValidModeId(mode_id)) { |
| 787 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 788 | } |
| 789 | if (mode_id == current_mode_id_) { |
| 790 | LOG(DEBUG) << "Already in the specified mode " << mode_id; |
| 791 | return ndk::ScopedAStatus::ok(); |
| 792 | } |
| 793 | ndk::ScopedAStatus status = handleChipConfiguration(lock, mode_id); |
| 794 | if (!status.isOk()) { |
| 795 | WifiStatusCode errorCode = static_cast<WifiStatusCode>(status.getServiceSpecificError()); |
| 796 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 797 | if (!callback->onChipReconfigureFailure(errorCode).isOk()) { |
| 798 | LOG(ERROR) << "Failed to invoke onChipReconfigureFailure callback"; |
| 799 | } |
| 800 | } |
| 801 | return status; |
| 802 | } |
| 803 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 804 | if (!callback->onChipReconfigured(mode_id).isOk()) { |
| 805 | LOG(ERROR) << "Failed to invoke onChipReconfigured callback"; |
| 806 | } |
| 807 | } |
| 808 | current_mode_id_ = mode_id; |
| 809 | LOG(INFO) << "Configured chip in mode " << mode_id; |
| 810 | setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName()); |
| 811 | |
| 812 | legacy_hal_.lock()->registerSubsystemRestartCallbackHandler(subsystemCallbackHandler_); |
| 813 | |
| 814 | return status; |
| 815 | } |
| 816 | |
| 817 | std::pair<int32_t, ndk::ScopedAStatus> WifiChip::getModeInternal() { |
| 818 | if (!isValidModeId(current_mode_id_)) { |
| 819 | return {current_mode_id_, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 820 | } |
| 821 | return {current_mode_id_, ndk::ScopedAStatus::ok()}; |
| 822 | } |
| 823 | |
| 824 | std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> WifiChip::requestChipDebugInfoInternal() { |
| 825 | IWifiChip::ChipDebugInfo result; |
| 826 | legacy_hal::wifi_error legacy_status; |
| 827 | std::string driver_desc; |
| 828 | const auto ifname = getFirstActiveWlanIfaceName(); |
| 829 | std::tie(legacy_status, driver_desc) = legacy_hal_.lock()->getDriverVersion(ifname); |
| 830 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 831 | LOG(ERROR) << "Failed to get driver version: " << legacyErrorToString(legacy_status); |
| 832 | ndk::ScopedAStatus status = |
| 833 | createWifiStatusFromLegacyError(legacy_status, "failed to get driver version"); |
| 834 | return {std::move(result), std::move(status)}; |
| 835 | } |
| 836 | result.driverDescription = driver_desc.c_str(); |
| 837 | |
| 838 | std::string firmware_desc; |
| 839 | std::tie(legacy_status, firmware_desc) = legacy_hal_.lock()->getFirmwareVersion(ifname); |
| 840 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 841 | LOG(ERROR) << "Failed to get firmware version: " << legacyErrorToString(legacy_status); |
| 842 | ndk::ScopedAStatus status = |
| 843 | createWifiStatusFromLegacyError(legacy_status, "failed to get firmware version"); |
| 844 | return {std::move(result), std::move(status)}; |
| 845 | } |
| 846 | result.firmwareDescription = firmware_desc.c_str(); |
| 847 | |
| 848 | return {std::move(result), ndk::ScopedAStatus::ok()}; |
| 849 | } |
| 850 | |
| 851 | std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> WifiChip::requestDriverDebugDumpInternal() { |
| 852 | legacy_hal::wifi_error legacy_status; |
| 853 | std::vector<uint8_t> driver_dump; |
| 854 | std::tie(legacy_status, driver_dump) = |
| 855 | legacy_hal_.lock()->requestDriverMemoryDump(getFirstActiveWlanIfaceName()); |
| 856 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 857 | LOG(ERROR) << "Failed to get driver debug dump: " << legacyErrorToString(legacy_status); |
| 858 | return {std::vector<uint8_t>(), createWifiStatusFromLegacyError(legacy_status)}; |
| 859 | } |
| 860 | return {driver_dump, ndk::ScopedAStatus::ok()}; |
| 861 | } |
| 862 | |
| 863 | std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> WifiChip::requestFirmwareDebugDumpInternal() { |
| 864 | legacy_hal::wifi_error legacy_status; |
| 865 | std::vector<uint8_t> firmware_dump; |
| 866 | std::tie(legacy_status, firmware_dump) = |
| 867 | legacy_hal_.lock()->requestFirmwareMemoryDump(getFirstActiveWlanIfaceName()); |
| 868 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 869 | LOG(ERROR) << "Failed to get firmware debug dump: " << legacyErrorToString(legacy_status); |
| 870 | return {std::vector<uint8_t>(), createWifiStatusFromLegacyError(legacy_status)}; |
| 871 | } |
| 872 | return {firmware_dump, ndk::ScopedAStatus::ok()}; |
| 873 | } |
| 874 | |
| 875 | ndk::ScopedAStatus WifiChip::createVirtualApInterface(const std::string& apVirtIf) { |
| 876 | legacy_hal::wifi_error legacy_status; |
| 877 | legacy_status = legacy_hal_.lock()->createVirtualInterface( |
| 878 | apVirtIf, aidl_struct_util::convertAidlIfaceTypeToLegacy(IfaceType::AP)); |
| 879 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 880 | LOG(ERROR) << "Failed to add interface: " << apVirtIf << " " |
| 881 | << legacyErrorToString(legacy_status); |
| 882 | return createWifiStatusFromLegacyError(legacy_status); |
| 883 | } |
| 884 | return ndk::ScopedAStatus::ok(); |
| 885 | } |
| 886 | |
| 887 | std::shared_ptr<WifiApIface> WifiChip::newWifiApIface(std::string& ifname) { |
| 888 | std::vector<std::string> ap_instances; |
| 889 | for (auto const& it : br_ifaces_ap_instances_) { |
| 890 | if (it.first == ifname) { |
| 891 | ap_instances = it.second; |
| 892 | } |
| 893 | } |
| 894 | std::shared_ptr<WifiApIface> iface = |
| 895 | ndk::SharedRefBase::make<WifiApIface>(ifname, ap_instances, legacy_hal_, iface_util_); |
| 896 | ap_ifaces_.push_back(iface); |
| 897 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 898 | if (!callback->onIfaceAdded(IfaceType::AP, ifname).isOk()) { |
| 899 | LOG(ERROR) << "Failed to invoke onIfaceAdded callback"; |
| 900 | } |
| 901 | } |
| 902 | setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName()); |
| 903 | return iface; |
| 904 | } |
| 905 | |
| 906 | std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> WifiChip::createApIfaceInternal() { |
| 907 | if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP)) { |
| 908 | return {std::shared_ptr<WifiApIface>(), |
| 909 | createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 910 | } |
| 911 | std::string ifname = allocateApIfaceName(); |
| 912 | ndk::ScopedAStatus status = createVirtualApInterface(ifname); |
| 913 | if (!status.isOk()) { |
| 914 | return {std::shared_ptr<WifiApIface>(), std::move(status)}; |
| 915 | } |
| 916 | std::shared_ptr<WifiApIface> iface = newWifiApIface(ifname); |
| 917 | return {iface, ndk::ScopedAStatus::ok()}; |
| 918 | } |
| 919 | |
| 920 | std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> |
| 921 | WifiChip::createBridgedApIfaceInternal() { |
| 922 | if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::AP_BRIDGED)) { |
| 923 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 924 | } |
| 925 | std::vector<std::string> ap_instances = allocateBridgedApInstanceNames(); |
| 926 | if (ap_instances.size() < 2) { |
| 927 | LOG(ERROR) << "Fail to allocate two instances"; |
| 928 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 929 | } |
| 930 | std::string br_ifname = kApBridgeIfacePrefix + ap_instances[0]; |
| 931 | for (int i = 0; i < 2; i++) { |
| 932 | ndk::ScopedAStatus status = createVirtualApInterface(ap_instances[i]); |
| 933 | if (!status.isOk()) { |
| 934 | if (i != 0) { // The failure happened when creating second virtual |
| 935 | // iface. |
| 936 | legacy_hal_.lock()->deleteVirtualInterface( |
| 937 | ap_instances.front()); // Remove the first virtual iface. |
| 938 | } |
| 939 | return {nullptr, std::move(status)}; |
| 940 | } |
| 941 | } |
| 942 | br_ifaces_ap_instances_[br_ifname] = ap_instances; |
| 943 | if (!iface_util_->createBridge(br_ifname)) { |
| 944 | LOG(ERROR) << "Failed createBridge - br_name=" << br_ifname.c_str(); |
| 945 | invalidateAndClearBridgedAp(br_ifname); |
| 946 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 947 | } |
| 948 | for (auto const& instance : ap_instances) { |
| 949 | // Bind ap instance interface to AP bridge |
| 950 | if (!iface_util_->addIfaceToBridge(br_ifname, instance)) { |
| 951 | LOG(ERROR) << "Failed add if to Bridge - if_name=" << instance.c_str(); |
| 952 | invalidateAndClearBridgedAp(br_ifname); |
| 953 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 954 | } |
| 955 | } |
| 956 | std::shared_ptr<WifiApIface> iface = newWifiApIface(br_ifname); |
| 957 | return {iface, ndk::ScopedAStatus::ok()}; |
| 958 | } |
| 959 | |
| 960 | std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getApIfaceNamesInternal() { |
| 961 | if (ap_ifaces_.empty()) { |
| 962 | return {std::vector<std::string>(), ndk::ScopedAStatus::ok()}; |
| 963 | } |
| 964 | return {getNames(ap_ifaces_), ndk::ScopedAStatus::ok()}; |
| 965 | } |
| 966 | |
| 967 | std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> WifiChip::getApIfaceInternal( |
| 968 | const std::string& ifname) { |
| 969 | const auto iface = findUsingName(ap_ifaces_, ifname); |
| 970 | if (!iface.get()) { |
| 971 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)}; |
| 972 | } |
| 973 | return {iface, ndk::ScopedAStatus::ok()}; |
| 974 | } |
| 975 | |
| 976 | ndk::ScopedAStatus WifiChip::removeApIfaceInternal(const std::string& ifname) { |
| 977 | const auto iface = findUsingName(ap_ifaces_, ifname); |
| 978 | if (!iface.get()) { |
| 979 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 980 | } |
| 981 | // Invalidate & remove any dependent objects first. |
| 982 | // Note: This is probably not required because we never create |
| 983 | // nan/rtt objects over AP iface. But, there is no harm to do it |
| 984 | // here and not make that assumption all over the place. |
| 985 | invalidateAndRemoveDependencies(ifname); |
| 986 | // Clear the bridge interface and the iface instance. |
| 987 | invalidateAndClearBridgedAp(ifname); |
| 988 | invalidateAndClear(ap_ifaces_, iface); |
| 989 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 990 | if (!callback->onIfaceRemoved(IfaceType::AP, ifname).isOk()) { |
| 991 | LOG(ERROR) << "Failed to invoke onIfaceRemoved callback"; |
| 992 | } |
| 993 | } |
| 994 | setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName()); |
| 995 | return ndk::ScopedAStatus::ok(); |
| 996 | } |
| 997 | |
| 998 | ndk::ScopedAStatus WifiChip::removeIfaceInstanceFromBridgedApIfaceInternal( |
| 999 | const std::string& ifname, const std::string& ifInstanceName) { |
| 1000 | const auto iface = findUsingName(ap_ifaces_, ifname); |
| 1001 | if (!iface.get() || ifInstanceName.empty()) { |
| 1002 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 1003 | } |
| 1004 | // Requires to remove one of the instance in bridge mode |
| 1005 | for (auto const& it : br_ifaces_ap_instances_) { |
| 1006 | if (it.first == ifname) { |
| 1007 | std::vector<std::string> ap_instances = it.second; |
| 1008 | for (auto const& iface : ap_instances) { |
| 1009 | if (iface == ifInstanceName) { |
| 1010 | if (!iface_util_->removeIfaceFromBridge(it.first, iface)) { |
| 1011 | LOG(ERROR) << "Failed to remove interface: " << ifInstanceName << " from " |
| 1012 | << ifname; |
| 1013 | return createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE); |
| 1014 | } |
| 1015 | legacy_hal::wifi_error legacy_status = |
| 1016 | legacy_hal_.lock()->deleteVirtualInterface(iface); |
| 1017 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1018 | LOG(ERROR) << "Failed to del interface: " << iface << " " |
| 1019 | << legacyErrorToString(legacy_status); |
| 1020 | return createWifiStatusFromLegacyError(legacy_status); |
| 1021 | } |
| 1022 | ap_instances.erase( |
| 1023 | std::remove(ap_instances.begin(), ap_instances.end(), ifInstanceName), |
| 1024 | ap_instances.end()); |
| 1025 | br_ifaces_ap_instances_[ifname] = ap_instances; |
| 1026 | break; |
| 1027 | } |
| 1028 | } |
| 1029 | break; |
| 1030 | } |
| 1031 | } |
| 1032 | iface->removeInstance(ifInstanceName); |
| 1033 | setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName()); |
| 1034 | |
| 1035 | return ndk::ScopedAStatus::ok(); |
| 1036 | } |
| 1037 | |
| 1038 | std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> WifiChip::createNanIfaceInternal() { |
| 1039 | if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::NAN_IFACE)) { |
| 1040 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 1041 | } |
| 1042 | bool is_dedicated_iface = true; |
| 1043 | std::string ifname = getPredefinedNanIfaceName(); |
| 1044 | if (ifname.empty() || !iface_util_->ifNameToIndex(ifname)) { |
| 1045 | // Use the first shared STA iface (wlan0) if a dedicated aware iface is |
| 1046 | // not defined. |
| 1047 | ifname = getFirstActiveWlanIfaceName(); |
| 1048 | is_dedicated_iface = false; |
| 1049 | } |
| 1050 | std::shared_ptr<WifiNanIface> iface = |
| 1051 | WifiNanIface::create(ifname, is_dedicated_iface, legacy_hal_, iface_util_); |
| 1052 | nan_ifaces_.push_back(iface); |
| 1053 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 1054 | if (!callback->onIfaceAdded(IfaceType::NAN_IFACE, ifname).isOk()) { |
| 1055 | LOG(ERROR) << "Failed to invoke onIfaceAdded callback"; |
| 1056 | } |
| 1057 | } |
| 1058 | return {iface, ndk::ScopedAStatus::ok()}; |
| 1059 | } |
| 1060 | |
| 1061 | std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getNanIfaceNamesInternal() { |
| 1062 | if (nan_ifaces_.empty()) { |
| 1063 | return {std::vector<std::string>(), ndk::ScopedAStatus::ok()}; |
| 1064 | } |
| 1065 | return {getNames(nan_ifaces_), ndk::ScopedAStatus::ok()}; |
| 1066 | } |
| 1067 | |
| 1068 | std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> WifiChip::getNanIfaceInternal( |
| 1069 | const std::string& ifname) { |
| 1070 | const auto iface = findUsingName(nan_ifaces_, ifname); |
| 1071 | if (!iface.get()) { |
| 1072 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)}; |
| 1073 | } |
| 1074 | return {iface, ndk::ScopedAStatus::ok()}; |
| 1075 | } |
| 1076 | |
| 1077 | ndk::ScopedAStatus WifiChip::removeNanIfaceInternal(const std::string& ifname) { |
| 1078 | const auto iface = findUsingName(nan_ifaces_, ifname); |
| 1079 | if (!iface.get()) { |
| 1080 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 1081 | } |
| 1082 | invalidateAndClear(nan_ifaces_, iface); |
| 1083 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 1084 | if (!callback->onIfaceRemoved(IfaceType::NAN_IFACE, ifname).isOk()) { |
| 1085 | LOG(ERROR) << "Failed to invoke onIfaceAdded callback"; |
| 1086 | } |
| 1087 | } |
| 1088 | return ndk::ScopedAStatus::ok(); |
| 1089 | } |
| 1090 | |
| 1091 | std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> WifiChip::createP2pIfaceInternal() { |
| 1092 | if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::P2P)) { |
| 1093 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 1094 | } |
| 1095 | std::string ifname = getPredefinedP2pIfaceName(); |
| 1096 | std::shared_ptr<WifiP2pIface> iface = |
| 1097 | ndk::SharedRefBase::make<WifiP2pIface>(ifname, legacy_hal_); |
| 1098 | p2p_ifaces_.push_back(iface); |
| 1099 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 1100 | if (!callback->onIfaceAdded(IfaceType::P2P, ifname).isOk()) { |
| 1101 | LOG(ERROR) << "Failed to invoke onIfaceAdded callback"; |
| 1102 | } |
| 1103 | } |
| 1104 | return {iface, ndk::ScopedAStatus::ok()}; |
| 1105 | } |
| 1106 | |
| 1107 | std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getP2pIfaceNamesInternal() { |
| 1108 | if (p2p_ifaces_.empty()) { |
| 1109 | return {std::vector<std::string>(), ndk::ScopedAStatus::ok()}; |
| 1110 | } |
| 1111 | return {getNames(p2p_ifaces_), ndk::ScopedAStatus::ok()}; |
| 1112 | } |
| 1113 | |
| 1114 | std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> WifiChip::getP2pIfaceInternal( |
| 1115 | const std::string& ifname) { |
| 1116 | const auto iface = findUsingName(p2p_ifaces_, ifname); |
| 1117 | if (!iface.get()) { |
| 1118 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)}; |
| 1119 | } |
| 1120 | return {iface, ndk::ScopedAStatus::ok()}; |
| 1121 | } |
| 1122 | |
| 1123 | ndk::ScopedAStatus WifiChip::removeP2pIfaceInternal(const std::string& ifname) { |
| 1124 | const auto iface = findUsingName(p2p_ifaces_, ifname); |
| 1125 | if (!iface.get()) { |
| 1126 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 1127 | } |
| 1128 | invalidateAndClear(p2p_ifaces_, iface); |
| 1129 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 1130 | if (!callback->onIfaceRemoved(IfaceType::P2P, ifname).isOk()) { |
| 1131 | LOG(ERROR) << "Failed to invoke onIfaceRemoved callback"; |
| 1132 | } |
| 1133 | } |
| 1134 | return ndk::ScopedAStatus::ok(); |
| 1135 | } |
| 1136 | |
| 1137 | std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> WifiChip::createStaIfaceInternal() { |
| 1138 | if (!canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) { |
| 1139 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 1140 | } |
| 1141 | std::string ifname = allocateStaIfaceName(); |
| 1142 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->createVirtualInterface( |
| 1143 | ifname, aidl_struct_util::convertAidlIfaceTypeToLegacy(IfaceType::STA)); |
| 1144 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1145 | LOG(ERROR) << "Failed to add interface: " << ifname << " " |
| 1146 | << legacyErrorToString(legacy_status); |
| 1147 | return {nullptr, createWifiStatusFromLegacyError(legacy_status)}; |
| 1148 | } |
Gabriel Biren | 2f7bec81 | 2023-01-31 01:07:38 +0000 | [diff] [blame] | 1149 | std::shared_ptr<WifiStaIface> iface = WifiStaIface::create(ifname, legacy_hal_, iface_util_); |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 1150 | sta_ifaces_.push_back(iface); |
| 1151 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 1152 | if (!callback->onIfaceAdded(IfaceType::STA, ifname).isOk()) { |
| 1153 | LOG(ERROR) << "Failed to invoke onIfaceAdded callback"; |
| 1154 | } |
| 1155 | } |
| 1156 | setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName()); |
| 1157 | return {iface, ndk::ScopedAStatus::ok()}; |
| 1158 | } |
| 1159 | |
| 1160 | std::pair<std::vector<std::string>, ndk::ScopedAStatus> WifiChip::getStaIfaceNamesInternal() { |
| 1161 | if (sta_ifaces_.empty()) { |
| 1162 | return {std::vector<std::string>(), ndk::ScopedAStatus::ok()}; |
| 1163 | } |
| 1164 | return {getNames(sta_ifaces_), ndk::ScopedAStatus::ok()}; |
| 1165 | } |
| 1166 | |
| 1167 | std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> WifiChip::getStaIfaceInternal( |
| 1168 | const std::string& ifname) { |
| 1169 | const auto iface = findUsingName(sta_ifaces_, ifname); |
| 1170 | if (!iface.get()) { |
| 1171 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)}; |
| 1172 | } |
| 1173 | return {iface, ndk::ScopedAStatus::ok()}; |
| 1174 | } |
| 1175 | |
| 1176 | ndk::ScopedAStatus WifiChip::removeStaIfaceInternal(const std::string& ifname) { |
| 1177 | const auto iface = findUsingName(sta_ifaces_, ifname); |
| 1178 | if (!iface.get()) { |
| 1179 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 1180 | } |
| 1181 | // Invalidate & remove any dependent objects first. |
| 1182 | invalidateAndRemoveDependencies(ifname); |
| 1183 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->deleteVirtualInterface(ifname); |
| 1184 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1185 | LOG(ERROR) << "Failed to remove interface: " << ifname << " " |
| 1186 | << legacyErrorToString(legacy_status); |
| 1187 | } |
| 1188 | invalidateAndClear(sta_ifaces_, iface); |
| 1189 | for (const auto& callback : event_cb_handler_.getCallbacks()) { |
| 1190 | if (!callback->onIfaceRemoved(IfaceType::STA, ifname).isOk()) { |
| 1191 | LOG(ERROR) << "Failed to invoke onIfaceRemoved callback"; |
| 1192 | } |
| 1193 | } |
| 1194 | setActiveWlanIfaceNameProperty(getFirstActiveWlanIfaceName()); |
| 1195 | return ndk::ScopedAStatus::ok(); |
| 1196 | } |
| 1197 | |
| 1198 | std::pair<std::shared_ptr<IWifiRttController>, ndk::ScopedAStatus> |
| 1199 | WifiChip::createRttControllerInternal(const std::shared_ptr<IWifiStaIface>& bound_iface) { |
| 1200 | if (sta_ifaces_.size() == 0 && |
| 1201 | !canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType::STA)) { |
| 1202 | LOG(ERROR) << "createRttControllerInternal: Chip cannot support STAs " |
| 1203 | "(and RTT by extension)"; |
| 1204 | return {nullptr, createWifiStatus(WifiStatusCode::ERROR_NOT_AVAILABLE)}; |
| 1205 | } |
| 1206 | std::shared_ptr<WifiRttController> rtt = |
| 1207 | WifiRttController::create(getFirstActiveWlanIfaceName(), bound_iface, legacy_hal_); |
| 1208 | rtt_controllers_.emplace_back(rtt); |
| 1209 | return {rtt, ndk::ScopedAStatus::ok()}; |
| 1210 | } |
| 1211 | |
| 1212 | std::pair<std::vector<WifiDebugRingBufferStatus>, ndk::ScopedAStatus> |
| 1213 | WifiChip::getDebugRingBuffersStatusInternal() { |
| 1214 | legacy_hal::wifi_error legacy_status; |
| 1215 | std::vector<legacy_hal::wifi_ring_buffer_status> legacy_ring_buffer_status_vec; |
| 1216 | std::tie(legacy_status, legacy_ring_buffer_status_vec) = |
| 1217 | legacy_hal_.lock()->getRingBuffersStatus(getFirstActiveWlanIfaceName()); |
| 1218 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1219 | return {std::vector<WifiDebugRingBufferStatus>(), |
| 1220 | createWifiStatusFromLegacyError(legacy_status)}; |
| 1221 | } |
| 1222 | std::vector<WifiDebugRingBufferStatus> aidl_ring_buffer_status_vec; |
| 1223 | if (!aidl_struct_util::convertLegacyVectorOfDebugRingBufferStatusToAidl( |
| 1224 | legacy_ring_buffer_status_vec, &aidl_ring_buffer_status_vec)) { |
| 1225 | return {std::vector<WifiDebugRingBufferStatus>(), |
| 1226 | createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)}; |
| 1227 | } |
| 1228 | return {aidl_ring_buffer_status_vec, ndk::ScopedAStatus::ok()}; |
| 1229 | } |
| 1230 | |
| 1231 | ndk::ScopedAStatus WifiChip::startLoggingToDebugRingBufferInternal( |
| 1232 | const std::string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level, |
| 1233 | uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes) { |
| 1234 | ndk::ScopedAStatus status = registerDebugRingBufferCallback(); |
| 1235 | if (!status.isOk()) { |
| 1236 | return status; |
| 1237 | } |
| 1238 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRingBufferLogging( |
| 1239 | getFirstActiveWlanIfaceName(), ring_name, |
| 1240 | static_cast<std::underlying_type<WifiDebugRingBufferVerboseLevel>::type>(verbose_level), |
| 1241 | max_interval_in_sec, min_data_size_in_bytes); |
| 1242 | ringbuffer_map_.insert( |
| 1243 | std::pair<std::string, Ringbuffer>(ring_name, Ringbuffer(kMaxBufferSizeBytes))); |
| 1244 | // if verbose logging enabled, turn up HAL daemon logging as well. |
| 1245 | if (verbose_level < WifiDebugRingBufferVerboseLevel::VERBOSE) { |
| 1246 | ::android::base::SetMinimumLogSeverity(::android::base::DEBUG); |
| 1247 | } else { |
| 1248 | ::android::base::SetMinimumLogSeverity(::android::base::VERBOSE); |
| 1249 | } |
| 1250 | return createWifiStatusFromLegacyError(legacy_status); |
| 1251 | } |
| 1252 | |
| 1253 | ndk::ScopedAStatus WifiChip::forceDumpToDebugRingBufferInternal(const std::string& ring_name) { |
| 1254 | ndk::ScopedAStatus status = registerDebugRingBufferCallback(); |
| 1255 | if (!status.isOk()) { |
| 1256 | return status; |
| 1257 | } |
| 1258 | legacy_hal::wifi_error legacy_status = |
| 1259 | legacy_hal_.lock()->getRingBufferData(getFirstActiveWlanIfaceName(), ring_name); |
| 1260 | |
| 1261 | return createWifiStatusFromLegacyError(legacy_status); |
| 1262 | } |
| 1263 | |
| 1264 | ndk::ScopedAStatus WifiChip::flushRingBufferToFileInternal() { |
| 1265 | if (!writeRingbufferFilesInternal()) { |
| 1266 | LOG(ERROR) << "Error writing files to flash"; |
| 1267 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 1268 | } |
| 1269 | return ndk::ScopedAStatus::ok(); |
| 1270 | } |
| 1271 | |
| 1272 | ndk::ScopedAStatus WifiChip::stopLoggingToDebugRingBufferInternal() { |
| 1273 | legacy_hal::wifi_error legacy_status = |
| 1274 | legacy_hal_.lock()->deregisterRingBufferCallbackHandler(getFirstActiveWlanIfaceName()); |
| 1275 | if (legacy_status == legacy_hal::WIFI_SUCCESS) { |
| 1276 | debug_ring_buffer_cb_registered_ = false; |
| 1277 | } |
| 1278 | return createWifiStatusFromLegacyError(legacy_status); |
| 1279 | } |
| 1280 | |
| 1281 | std::pair<WifiDebugHostWakeReasonStats, ndk::ScopedAStatus> |
| 1282 | WifiChip::getDebugHostWakeReasonStatsInternal() { |
| 1283 | legacy_hal::wifi_error legacy_status; |
| 1284 | legacy_hal::WakeReasonStats legacy_stats; |
| 1285 | std::tie(legacy_status, legacy_stats) = |
| 1286 | legacy_hal_.lock()->getWakeReasonStats(getFirstActiveWlanIfaceName()); |
| 1287 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1288 | return {WifiDebugHostWakeReasonStats{}, createWifiStatusFromLegacyError(legacy_status)}; |
| 1289 | } |
| 1290 | WifiDebugHostWakeReasonStats aidl_stats; |
| 1291 | if (!aidl_struct_util::convertLegacyWakeReasonStatsToAidl(legacy_stats, &aidl_stats)) { |
| 1292 | return {WifiDebugHostWakeReasonStats{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)}; |
| 1293 | } |
| 1294 | return {aidl_stats, ndk::ScopedAStatus::ok()}; |
| 1295 | } |
| 1296 | |
| 1297 | ndk::ScopedAStatus WifiChip::enableDebugErrorAlertsInternal(bool enable) { |
| 1298 | legacy_hal::wifi_error legacy_status; |
| 1299 | if (enable) { |
| 1300 | std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_; |
| 1301 | const auto& on_alert_callback = [weak_ptr_this](int32_t error_code, |
| 1302 | std::vector<uint8_t> debug_data) { |
| 1303 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 1304 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 1305 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 1306 | return; |
| 1307 | } |
| 1308 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 1309 | if (!callback->onDebugErrorAlert(error_code, debug_data).isOk()) { |
| 1310 | LOG(ERROR) << "Failed to invoke onDebugErrorAlert callback"; |
| 1311 | } |
| 1312 | } |
| 1313 | }; |
| 1314 | legacy_status = legacy_hal_.lock()->registerErrorAlertCallbackHandler( |
| 1315 | getFirstActiveWlanIfaceName(), on_alert_callback); |
| 1316 | } else { |
| 1317 | legacy_status = legacy_hal_.lock()->deregisterErrorAlertCallbackHandler( |
| 1318 | getFirstActiveWlanIfaceName()); |
| 1319 | } |
| 1320 | return createWifiStatusFromLegacyError(legacy_status); |
| 1321 | } |
| 1322 | |
| 1323 | ndk::ScopedAStatus WifiChip::selectTxPowerScenarioInternal(IWifiChip::TxPowerScenario scenario) { |
| 1324 | auto legacy_status = legacy_hal_.lock()->selectTxPowerScenario( |
| 1325 | getFirstActiveWlanIfaceName(), |
| 1326 | aidl_struct_util::convertAidlTxPowerScenarioToLegacy(scenario)); |
| 1327 | return createWifiStatusFromLegacyError(legacy_status); |
| 1328 | } |
| 1329 | |
| 1330 | ndk::ScopedAStatus WifiChip::resetTxPowerScenarioInternal() { |
| 1331 | auto legacy_status = legacy_hal_.lock()->resetTxPowerScenario(getFirstActiveWlanIfaceName()); |
| 1332 | return createWifiStatusFromLegacyError(legacy_status); |
| 1333 | } |
| 1334 | |
| 1335 | ndk::ScopedAStatus WifiChip::setLatencyModeInternal(IWifiChip::LatencyMode mode) { |
| 1336 | auto legacy_status = legacy_hal_.lock()->setLatencyMode( |
| 1337 | getFirstActiveWlanIfaceName(), aidl_struct_util::convertAidlLatencyModeToLegacy(mode)); |
| 1338 | return createWifiStatusFromLegacyError(legacy_status); |
| 1339 | } |
| 1340 | |
| 1341 | ndk::ScopedAStatus WifiChip::setMultiStaPrimaryConnectionInternal(const std::string& ifname) { |
| 1342 | auto legacy_status = legacy_hal_.lock()->multiStaSetPrimaryConnection(ifname); |
| 1343 | return createWifiStatusFromLegacyError(legacy_status); |
| 1344 | } |
| 1345 | |
| 1346 | ndk::ScopedAStatus WifiChip::setMultiStaUseCaseInternal(IWifiChip::MultiStaUseCase use_case) { |
| 1347 | auto legacy_status = legacy_hal_.lock()->multiStaSetUseCase( |
| 1348 | aidl_struct_util::convertAidlMultiStaUseCaseToLegacy(use_case)); |
| 1349 | return createWifiStatusFromLegacyError(legacy_status); |
| 1350 | } |
| 1351 | |
| 1352 | ndk::ScopedAStatus WifiChip::setCoexUnsafeChannelsInternal( |
| 1353 | std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, CoexRestriction restrictions) { |
| 1354 | std::vector<legacy_hal::wifi_coex_unsafe_channel> legacy_unsafe_channels; |
| 1355 | if (!aidl_struct_util::convertAidlVectorOfCoexUnsafeChannelToLegacy(unsafe_channels, |
| 1356 | &legacy_unsafe_channels)) { |
| 1357 | return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS); |
| 1358 | } |
| 1359 | uint32_t aidl_restrictions = static_cast<uint32_t>(restrictions); |
| 1360 | uint32_t legacy_restrictions = 0; |
| 1361 | if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::WIFI_DIRECT)) { |
| 1362 | legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_DIRECT; |
| 1363 | } |
| 1364 | if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::SOFTAP)) { |
| 1365 | legacy_restrictions |= legacy_hal::wifi_coex_restriction::SOFTAP; |
| 1366 | } |
| 1367 | if (aidl_restrictions & static_cast<uint32_t>(CoexRestriction::WIFI_AWARE)) { |
| 1368 | legacy_restrictions |= legacy_hal::wifi_coex_restriction::WIFI_AWARE; |
| 1369 | } |
| 1370 | auto legacy_status = |
| 1371 | legacy_hal_.lock()->setCoexUnsafeChannels(legacy_unsafe_channels, legacy_restrictions); |
| 1372 | return createWifiStatusFromLegacyError(legacy_status); |
| 1373 | } |
| 1374 | |
| 1375 | ndk::ScopedAStatus WifiChip::setCountryCodeInternal(const std::array<uint8_t, 2>& code) { |
| 1376 | auto legacy_status = legacy_hal_.lock()->setCountryCode(getFirstActiveWlanIfaceName(), code); |
| 1377 | return createWifiStatusFromLegacyError(legacy_status); |
| 1378 | } |
| 1379 | |
| 1380 | std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> WifiChip::getUsableChannelsInternal( |
| 1381 | WifiBand band, WifiIfaceMode ifaceModeMask, UsableChannelFilter filterMask) { |
| 1382 | legacy_hal::wifi_error legacy_status; |
| 1383 | std::vector<legacy_hal::wifi_usable_channel> legacy_usable_channels; |
| 1384 | std::tie(legacy_status, legacy_usable_channels) = legacy_hal_.lock()->getUsableChannels( |
| 1385 | aidl_struct_util::convertAidlWifiBandToLegacyMacBand(band), |
| 1386 | aidl_struct_util::convertAidlWifiIfaceModeToLegacy( |
| 1387 | static_cast<uint32_t>(ifaceModeMask)), |
| 1388 | aidl_struct_util::convertAidlUsableChannelFilterToLegacy( |
| 1389 | static_cast<uint32_t>(filterMask))); |
| 1390 | |
| 1391 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1392 | return {std::vector<WifiUsableChannel>(), createWifiStatusFromLegacyError(legacy_status)}; |
| 1393 | } |
| 1394 | std::vector<WifiUsableChannel> aidl_usable_channels; |
| 1395 | if (!aidl_struct_util::convertLegacyWifiUsableChannelsToAidl(legacy_usable_channels, |
| 1396 | &aidl_usable_channels)) { |
| 1397 | return {std::vector<WifiUsableChannel>(), createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)}; |
| 1398 | } |
| 1399 | return {aidl_usable_channels, ndk::ScopedAStatus::ok()}; |
| 1400 | } |
| 1401 | |
Oscar Shu | ab8313c | 2022-12-13 00:55:11 +0000 | [diff] [blame] | 1402 | ndk::ScopedAStatus WifiChip::setAfcChannelAllowanceInternal( |
| 1403 | const std::vector<AvailableAfcFrequencyInfo>& availableAfcFrequencyInfo) { |
| 1404 | LOG(INFO) << "setAfcChannelAllowance is not yet supported " << availableAfcFrequencyInfo.size(); |
| 1405 | return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED); |
| 1406 | } |
| 1407 | |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 1408 | std::pair<WifiRadioCombinationMatrix, ndk::ScopedAStatus> |
| 1409 | WifiChip::getSupportedRadioCombinationsMatrixInternal() { |
| 1410 | legacy_hal::wifi_error legacy_status; |
| 1411 | legacy_hal::wifi_radio_combination_matrix* legacy_matrix; |
| 1412 | |
| 1413 | std::tie(legacy_status, legacy_matrix) = |
| 1414 | legacy_hal_.lock()->getSupportedRadioCombinationsMatrix(); |
| 1415 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1416 | LOG(ERROR) << "Failed to get SupportedRadioCombinations matrix from legacy HAL: " |
| 1417 | << legacyErrorToString(legacy_status); |
| 1418 | return {WifiRadioCombinationMatrix{}, createWifiStatusFromLegacyError(legacy_status)}; |
| 1419 | } |
| 1420 | |
| 1421 | WifiRadioCombinationMatrix aidl_matrix; |
| 1422 | if (!aidl_struct_util::convertLegacyRadioCombinationsMatrixToAidl(legacy_matrix, |
| 1423 | &aidl_matrix)) { |
| 1424 | LOG(ERROR) << "Failed convertLegacyRadioCombinationsMatrixToAidl() "; |
| 1425 | return {WifiRadioCombinationMatrix(), createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)}; |
| 1426 | } |
| 1427 | return {aidl_matrix, ndk::ScopedAStatus::ok()}; |
| 1428 | } |
| 1429 | |
Mahesh KKV | c84d377 | 2022-12-02 16:53:28 -0800 | [diff] [blame] | 1430 | std::pair<WifiChipCapabilities, ndk::ScopedAStatus> WifiChip::getWifiChipCapabilitiesInternal() { |
| 1431 | legacy_hal::wifi_error legacy_status; |
| 1432 | legacy_hal::wifi_chip_capabilities legacy_chip_capabilities; |
| 1433 | std::tie(legacy_status, legacy_chip_capabilities) = |
| 1434 | legacy_hal_.lock()->getWifiChipCapabilities(); |
| 1435 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1436 | LOG(ERROR) << "Failed to get chip capabilities from legacy HAL: " |
| 1437 | << legacyErrorToString(legacy_status); |
| 1438 | return {WifiChipCapabilities(), createWifiStatusFromLegacyError(legacy_status)}; |
| 1439 | } |
| 1440 | WifiChipCapabilities aidl_chip_capabilities; |
| 1441 | if (!aidl_struct_util::convertLegacyWifiChipCapabilitiesToAidl(legacy_chip_capabilities, |
| 1442 | aidl_chip_capabilities)) { |
| 1443 | LOG(ERROR) << "Failed convertLegacyWifiChipCapabilitiesToAidl() "; |
| 1444 | return {WifiChipCapabilities(), createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)}; |
| 1445 | } |
| 1446 | |
| 1447 | return {aidl_chip_capabilities, ndk::ScopedAStatus::ok()}; |
| 1448 | } |
| 1449 | |
Shuibing Dai | e5fbcab | 2022-12-19 15:37:19 -0800 | [diff] [blame] | 1450 | ndk::ScopedAStatus WifiChip::enableStaChannelForPeerNetworkInternal( |
| 1451 | ChannelCategoryMask channelCategoryEnableFlag) { |
| 1452 | auto legacy_status = legacy_hal_.lock()->enableStaChannelForPeerNetwork( |
| 1453 | aidl_struct_util::convertAidlChannelCategoryToLegacy( |
| 1454 | static_cast<uint32_t>(channelCategoryEnableFlag))); |
| 1455 | return createWifiStatusFromLegacyError(legacy_status); |
| 1456 | } |
| 1457 | |
Gabriel Biren | f3262f9 | 2022-07-15 23:25:39 +0000 | [diff] [blame] | 1458 | ndk::ScopedAStatus WifiChip::triggerSubsystemRestartInternal() { |
| 1459 | auto legacy_status = legacy_hal_.lock()->triggerSubsystemRestart(); |
| 1460 | return createWifiStatusFromLegacyError(legacy_status); |
| 1461 | } |
| 1462 | |
| 1463 | ndk::ScopedAStatus WifiChip::handleChipConfiguration( |
| 1464 | /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock, int32_t mode_id) { |
| 1465 | // If the chip is already configured in a different mode, stop |
| 1466 | // the legacy HAL and then start it after firmware mode change. |
| 1467 | if (isValidModeId(current_mode_id_)) { |
| 1468 | LOG(INFO) << "Reconfiguring chip from mode " << current_mode_id_ << " to mode " << mode_id; |
| 1469 | invalidateAndRemoveAllIfaces(); |
| 1470 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stop(lock, []() {}); |
| 1471 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1472 | LOG(ERROR) << "Failed to stop legacy HAL: " << legacyErrorToString(legacy_status); |
| 1473 | return createWifiStatusFromLegacyError(legacy_status); |
| 1474 | } |
| 1475 | } |
| 1476 | // Firmware mode change not needed for V2 devices. |
| 1477 | bool success = true; |
| 1478 | if (mode_id == feature_flags::chip_mode_ids::kV1Sta) { |
| 1479 | success = mode_controller_.lock()->changeFirmwareMode(IfaceType::STA); |
| 1480 | } else if (mode_id == feature_flags::chip_mode_ids::kV1Ap) { |
| 1481 | success = mode_controller_.lock()->changeFirmwareMode(IfaceType::AP); |
| 1482 | } |
| 1483 | if (!success) { |
| 1484 | return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN); |
| 1485 | } |
| 1486 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->start(); |
| 1487 | if (legacy_status != legacy_hal::WIFI_SUCCESS) { |
| 1488 | LOG(ERROR) << "Failed to start legacy HAL: " << legacyErrorToString(legacy_status); |
| 1489 | return createWifiStatusFromLegacyError(legacy_status); |
| 1490 | } |
| 1491 | // Every time the HAL is restarted, we need to register the |
| 1492 | // radio mode change callback. |
| 1493 | ndk::ScopedAStatus status = registerRadioModeChangeCallback(); |
| 1494 | if (!status.isOk()) { |
| 1495 | // This is probably not a critical failure? |
| 1496 | LOG(ERROR) << "Failed to register radio mode change callback"; |
| 1497 | } |
| 1498 | // Extract and save the version information into property. |
| 1499 | std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> version_info; |
| 1500 | version_info = WifiChip::requestChipDebugInfoInternal(); |
| 1501 | if (version_info.second.isOk()) { |
| 1502 | property_set("vendor.wlan.firmware.version", |
| 1503 | version_info.first.firmwareDescription.c_str()); |
| 1504 | property_set("vendor.wlan.driver.version", version_info.first.driverDescription.c_str()); |
| 1505 | } |
| 1506 | |
| 1507 | return ndk::ScopedAStatus::ok(); |
| 1508 | } |
| 1509 | |
| 1510 | ndk::ScopedAStatus WifiChip::registerDebugRingBufferCallback() { |
| 1511 | if (debug_ring_buffer_cb_registered_) { |
| 1512 | return ndk::ScopedAStatus::ok(); |
| 1513 | } |
| 1514 | |
| 1515 | std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_; |
| 1516 | const auto& on_ring_buffer_data_callback = |
| 1517 | [weak_ptr_this](const std::string& name, const std::vector<uint8_t>& data, |
| 1518 | const legacy_hal::wifi_ring_buffer_status& status) { |
| 1519 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 1520 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 1521 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 1522 | return; |
| 1523 | } |
| 1524 | WifiDebugRingBufferStatus aidl_status; |
| 1525 | Ringbuffer::AppendStatus appendstatus; |
| 1526 | if (!aidl_struct_util::convertLegacyDebugRingBufferStatusToAidl(status, |
| 1527 | &aidl_status)) { |
| 1528 | LOG(ERROR) << "Error converting ring buffer status"; |
| 1529 | return; |
| 1530 | } |
| 1531 | { |
| 1532 | std::unique_lock<std::mutex> lk(shared_ptr_this->lock_t); |
| 1533 | const auto& target = shared_ptr_this->ringbuffer_map_.find(name); |
| 1534 | if (target != shared_ptr_this->ringbuffer_map_.end()) { |
| 1535 | Ringbuffer& cur_buffer = target->second; |
| 1536 | appendstatus = cur_buffer.append(data); |
| 1537 | } else { |
| 1538 | LOG(ERROR) << "Ringname " << name << " not found"; |
| 1539 | return; |
| 1540 | } |
| 1541 | // unique_lock unlocked here |
| 1542 | } |
| 1543 | if (appendstatus == Ringbuffer::AppendStatus::FAIL_RING_BUFFER_CORRUPTED) { |
| 1544 | LOG(ERROR) << "Ringname " << name << " is corrupted. Clear the ring buffer"; |
| 1545 | shared_ptr_this->writeRingbufferFilesInternal(); |
| 1546 | return; |
| 1547 | } |
| 1548 | }; |
| 1549 | legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->registerRingBufferCallbackHandler( |
| 1550 | getFirstActiveWlanIfaceName(), on_ring_buffer_data_callback); |
| 1551 | |
| 1552 | if (legacy_status == legacy_hal::WIFI_SUCCESS) { |
| 1553 | debug_ring_buffer_cb_registered_ = true; |
| 1554 | } |
| 1555 | return createWifiStatusFromLegacyError(legacy_status); |
| 1556 | } |
| 1557 | |
| 1558 | ndk::ScopedAStatus WifiChip::registerRadioModeChangeCallback() { |
| 1559 | std::weak_ptr<WifiChip> weak_ptr_this = weak_ptr_this_; |
| 1560 | const auto& on_radio_mode_change_callback = |
| 1561 | [weak_ptr_this](const std::vector<legacy_hal::WifiMacInfo>& mac_infos) { |
| 1562 | const auto shared_ptr_this = weak_ptr_this.lock(); |
| 1563 | if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) { |
| 1564 | LOG(ERROR) << "Callback invoked on an invalid object"; |
| 1565 | return; |
| 1566 | } |
| 1567 | std::vector<IWifiChipEventCallback::RadioModeInfo> aidl_radio_mode_infos; |
| 1568 | if (!aidl_struct_util::convertLegacyWifiMacInfosToAidl(mac_infos, |
| 1569 | &aidl_radio_mode_infos)) { |
| 1570 | LOG(ERROR) << "Error converting wifi mac info"; |
| 1571 | return; |
| 1572 | } |
| 1573 | for (const auto& callback : shared_ptr_this->getEventCallbacks()) { |
| 1574 | if (!callback->onRadioModeChange(aidl_radio_mode_infos).isOk()) { |
| 1575 | LOG(ERROR) << "Failed to invoke onRadioModeChange callback"; |
| 1576 | } |
| 1577 | } |
| 1578 | }; |
| 1579 | legacy_hal::wifi_error legacy_status = |
| 1580 | legacy_hal_.lock()->registerRadioModeChangeCallbackHandler( |
| 1581 | getFirstActiveWlanIfaceName(), on_radio_mode_change_callback); |
| 1582 | return createWifiStatusFromLegacyError(legacy_status); |
| 1583 | } |
| 1584 | |
| 1585 | std::vector<IWifiChip::ChipConcurrencyCombination> |
| 1586 | WifiChip::getCurrentModeConcurrencyCombinations() { |
| 1587 | if (!isValidModeId(current_mode_id_)) { |
| 1588 | LOG(ERROR) << "Chip not configured in a mode yet"; |
| 1589 | return std::vector<IWifiChip::ChipConcurrencyCombination>(); |
| 1590 | } |
| 1591 | for (const auto& mode : modes_) { |
| 1592 | if (mode.id == current_mode_id_) { |
| 1593 | return mode.availableCombinations; |
| 1594 | } |
| 1595 | } |
| 1596 | CHECK(0) << "Expected to find concurrency combinations for current mode!"; |
| 1597 | return std::vector<IWifiChip::ChipConcurrencyCombination>(); |
| 1598 | } |
| 1599 | |
| 1600 | // Returns a map indexed by IfaceConcurrencyType with the number of ifaces currently |
| 1601 | // created of the corresponding concurrency type. |
| 1602 | std::map<IfaceConcurrencyType, size_t> WifiChip::getCurrentConcurrencyCombination() { |
| 1603 | std::map<IfaceConcurrencyType, size_t> iface_counts; |
| 1604 | uint32_t num_ap = 0; |
| 1605 | uint32_t num_ap_bridged = 0; |
| 1606 | for (const auto& ap_iface : ap_ifaces_) { |
| 1607 | std::string ap_iface_name = ap_iface->getName(); |
| 1608 | if (br_ifaces_ap_instances_.count(ap_iface_name) > 0 && |
| 1609 | br_ifaces_ap_instances_[ap_iface_name].size() > 1) { |
| 1610 | num_ap_bridged++; |
| 1611 | } else { |
| 1612 | num_ap++; |
| 1613 | } |
| 1614 | } |
| 1615 | iface_counts[IfaceConcurrencyType::AP] = num_ap; |
| 1616 | iface_counts[IfaceConcurrencyType::AP_BRIDGED] = num_ap_bridged; |
| 1617 | iface_counts[IfaceConcurrencyType::NAN_IFACE] = nan_ifaces_.size(); |
| 1618 | iface_counts[IfaceConcurrencyType::P2P] = p2p_ifaces_.size(); |
| 1619 | iface_counts[IfaceConcurrencyType::STA] = sta_ifaces_.size(); |
| 1620 | return iface_counts; |
| 1621 | } |
| 1622 | |
| 1623 | // This expands the provided concurrency combinations to a more parseable |
| 1624 | // form. Returns a vector of available combinations possible with the number |
| 1625 | // of each concurrency type in the combination. |
| 1626 | // This method is a port of HalDeviceManager.expandConcurrencyCombos() from framework. |
| 1627 | std::vector<std::map<IfaceConcurrencyType, size_t>> WifiChip::expandConcurrencyCombinations( |
| 1628 | const IWifiChip::ChipConcurrencyCombination& combination) { |
| 1629 | int32_t num_expanded_combos = 1; |
| 1630 | for (const auto& limit : combination.limits) { |
| 1631 | for (int32_t i = 0; i < limit.maxIfaces; i++) { |
| 1632 | num_expanded_combos *= limit.types.size(); |
| 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | // Allocate the vector of expanded combos and reset all concurrency type counts to 0 |
| 1637 | // in each combo. |
| 1638 | std::vector<std::map<IfaceConcurrencyType, size_t>> expanded_combos; |
| 1639 | expanded_combos.resize(num_expanded_combos); |
| 1640 | for (auto& expanded_combo : expanded_combos) { |
| 1641 | for (const auto type : {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, |
| 1642 | IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P, |
| 1643 | IfaceConcurrencyType::STA}) { |
| 1644 | expanded_combo[type] = 0; |
| 1645 | } |
| 1646 | } |
| 1647 | int32_t span = num_expanded_combos; |
| 1648 | for (const auto& limit : combination.limits) { |
| 1649 | for (int32_t i = 0; i < limit.maxIfaces; i++) { |
| 1650 | span /= limit.types.size(); |
| 1651 | for (int32_t k = 0; k < num_expanded_combos; ++k) { |
| 1652 | const auto iface_type = limit.types[(k / span) % limit.types.size()]; |
| 1653 | expanded_combos[k][iface_type]++; |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | return expanded_combos; |
| 1658 | } |
| 1659 | |
| 1660 | bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes( |
| 1661 | const std::map<IfaceConcurrencyType, size_t>& expanded_combo, |
| 1662 | IfaceConcurrencyType requested_type) { |
| 1663 | const auto current_combo = getCurrentConcurrencyCombination(); |
| 1664 | |
| 1665 | // Check if we have space for 1 more iface of |type| in this combo |
| 1666 | for (const auto type : |
| 1667 | {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, |
| 1668 | IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) { |
| 1669 | size_t num_ifaces_needed = current_combo.at(type); |
| 1670 | if (type == requested_type) { |
| 1671 | num_ifaces_needed++; |
| 1672 | } |
| 1673 | size_t num_ifaces_allowed = expanded_combo.at(type); |
| 1674 | if (num_ifaces_needed > num_ifaces_allowed) { |
| 1675 | return false; |
| 1676 | } |
| 1677 | } |
| 1678 | return true; |
| 1679 | } |
| 1680 | |
| 1681 | // This method does the following: |
| 1682 | // a) Enumerate all possible concurrency combos by expanding the current |
| 1683 | // ChipConcurrencyCombination. |
| 1684 | // b) Check if the requested concurrency type can be added to the current mode |
| 1685 | // with the concurrency combination that is already active. |
| 1686 | bool WifiChip::canCurrentModeSupportConcurrencyTypeWithCurrentTypes( |
| 1687 | IfaceConcurrencyType requested_type) { |
| 1688 | if (!isValidModeId(current_mode_id_)) { |
| 1689 | LOG(ERROR) << "Chip not configured in a mode yet"; |
| 1690 | return false; |
| 1691 | } |
| 1692 | const auto combinations = getCurrentModeConcurrencyCombinations(); |
| 1693 | for (const auto& combination : combinations) { |
| 1694 | const auto expanded_combos = expandConcurrencyCombinations(combination); |
| 1695 | for (const auto& expanded_combo : expanded_combos) { |
| 1696 | if (canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(expanded_combo, |
| 1697 | requested_type)) { |
| 1698 | return true; |
| 1699 | } |
| 1700 | } |
| 1701 | } |
| 1702 | return false; |
| 1703 | } |
| 1704 | |
| 1705 | // Note: This does not consider concurrency types already active. It only checks if the |
| 1706 | // provided expanded concurrency combination can support the requested combo. |
| 1707 | bool WifiChip::canExpandedConcurrencyComboSupportConcurrencyCombo( |
| 1708 | const std::map<IfaceConcurrencyType, size_t>& expanded_combo, |
| 1709 | const std::map<IfaceConcurrencyType, size_t>& req_combo) { |
| 1710 | // Check if we have space for 1 more |type| in this combo |
| 1711 | for (const auto type : |
| 1712 | {IfaceConcurrencyType::AP, IfaceConcurrencyType::AP_BRIDGED, |
| 1713 | IfaceConcurrencyType::NAN_IFACE, IfaceConcurrencyType::P2P, IfaceConcurrencyType::STA}) { |
| 1714 | if (req_combo.count(type) == 0) { |
| 1715 | // Concurrency type not in the req_combo. |
| 1716 | continue; |
| 1717 | } |
| 1718 | size_t num_ifaces_needed = req_combo.at(type); |
| 1719 | size_t num_ifaces_allowed = expanded_combo.at(type); |
| 1720 | if (num_ifaces_needed > num_ifaces_allowed) { |
| 1721 | return false; |
| 1722 | } |
| 1723 | } |
| 1724 | return true; |
| 1725 | } |
| 1726 | |
| 1727 | // This method does the following: |
| 1728 | // a) Enumerate all possible concurrency combos by expanding the current |
| 1729 | // ChipConcurrencyCombination. |
| 1730 | // b) Check if the requested concurrency combo can be added to the current mode. |
| 1731 | // Note: This does not consider concurrency types already active. It only checks if the |
| 1732 | // current mode can support the requested combo. |
| 1733 | bool WifiChip::canCurrentModeSupportConcurrencyCombo( |
| 1734 | const std::map<IfaceConcurrencyType, size_t>& req_combo) { |
| 1735 | if (!isValidModeId(current_mode_id_)) { |
| 1736 | LOG(ERROR) << "Chip not configured in a mode yet"; |
| 1737 | return false; |
| 1738 | } |
| 1739 | const auto combinations = getCurrentModeConcurrencyCombinations(); |
| 1740 | for (const auto& combination : combinations) { |
| 1741 | const auto expanded_combos = expandConcurrencyCombinations(combination); |
| 1742 | for (const auto& expanded_combo : expanded_combos) { |
| 1743 | if (canExpandedConcurrencyComboSupportConcurrencyCombo(expanded_combo, req_combo)) { |
| 1744 | return true; |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | return false; |
| 1749 | } |
| 1750 | |
| 1751 | // This method does the following: |
| 1752 | // a) Enumerate all possible concurrency combos by expanding the current |
| 1753 | // ChipConcurrencyCombination. |
| 1754 | // b) Check if the requested concurrency type can be added to the current mode. |
| 1755 | bool WifiChip::canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type) { |
| 1756 | // Check if we can support at least 1 of the requested concurrency type. |
| 1757 | std::map<IfaceConcurrencyType, size_t> req_iface_combo; |
| 1758 | req_iface_combo[requested_type] = 1; |
| 1759 | return canCurrentModeSupportConcurrencyCombo(req_iface_combo); |
| 1760 | } |
| 1761 | |
| 1762 | bool WifiChip::isValidModeId(int32_t mode_id) { |
| 1763 | for (const auto& mode : modes_) { |
| 1764 | if (mode.id == mode_id) { |
| 1765 | return true; |
| 1766 | } |
| 1767 | } |
| 1768 | return false; |
| 1769 | } |
| 1770 | |
| 1771 | bool WifiChip::isStaApConcurrencyAllowedInCurrentMode() { |
| 1772 | // Check if we can support at least 1 STA & 1 AP concurrently. |
| 1773 | std::map<IfaceConcurrencyType, size_t> req_iface_combo; |
| 1774 | req_iface_combo[IfaceConcurrencyType::STA] = 1; |
| 1775 | req_iface_combo[IfaceConcurrencyType::AP] = 1; |
| 1776 | return canCurrentModeSupportConcurrencyCombo(req_iface_combo); |
| 1777 | } |
| 1778 | |
| 1779 | bool WifiChip::isDualStaConcurrencyAllowedInCurrentMode() { |
| 1780 | // Check if we can support at least 2 STA concurrently. |
| 1781 | std::map<IfaceConcurrencyType, size_t> req_iface_combo; |
| 1782 | req_iface_combo[IfaceConcurrencyType::STA] = 2; |
| 1783 | return canCurrentModeSupportConcurrencyCombo(req_iface_combo); |
| 1784 | } |
| 1785 | |
| 1786 | std::string WifiChip::getFirstActiveWlanIfaceName() { |
| 1787 | if (sta_ifaces_.size() > 0) return sta_ifaces_[0]->getName(); |
| 1788 | if (ap_ifaces_.size() > 0) { |
| 1789 | // If the first active wlan iface is bridged iface. |
| 1790 | // Return first instance name. |
| 1791 | for (auto const& it : br_ifaces_ap_instances_) { |
| 1792 | if (it.first == ap_ifaces_[0]->getName()) { |
| 1793 | return it.second[0]; |
| 1794 | } |
| 1795 | } |
| 1796 | return ap_ifaces_[0]->getName(); |
| 1797 | } |
| 1798 | // This could happen if the chip call is made before any STA/AP |
| 1799 | // iface is created. Default to wlan0 for such cases. |
| 1800 | LOG(WARNING) << "No active wlan interfaces in use! Using default"; |
| 1801 | return getWlanIfaceNameWithType(IfaceType::STA, 0); |
| 1802 | } |
| 1803 | |
| 1804 | // Return the first wlan (wlan0, wlan1 etc.) starting from |start_idx| |
| 1805 | // not already in use. |
| 1806 | // Note: This doesn't check the actual presence of these interfaces. |
| 1807 | std::string WifiChip::allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx) { |
| 1808 | for (unsigned idx = start_idx; idx < kMaxWlanIfaces; idx++) { |
| 1809 | const auto ifname = getWlanIfaceNameWithType(type, idx); |
| 1810 | if (findUsingNameFromBridgedApInstances(ifname)) continue; |
| 1811 | if (findUsingName(ap_ifaces_, ifname)) continue; |
| 1812 | if (findUsingName(sta_ifaces_, ifname)) continue; |
| 1813 | return ifname; |
| 1814 | } |
| 1815 | // This should never happen. We screwed up somewhere if it did. |
| 1816 | CHECK(false) << "All wlan interfaces in use already!"; |
| 1817 | return {}; |
| 1818 | } |
| 1819 | |
| 1820 | uint32_t WifiChip::startIdxOfApIface() { |
| 1821 | if (isDualStaConcurrencyAllowedInCurrentMode()) { |
| 1822 | // When the HAL support dual STAs, AP should start with idx 2. |
| 1823 | return 2; |
| 1824 | } else if (isStaApConcurrencyAllowedInCurrentMode()) { |
| 1825 | // When the HAL support STA + AP but it doesn't support dual STAs. |
| 1826 | // AP should start with idx 1. |
| 1827 | return 1; |
| 1828 | } |
| 1829 | // No concurrency support. |
| 1830 | return 0; |
| 1831 | } |
| 1832 | |
| 1833 | // AP iface names start with idx 1 for modes supporting |
| 1834 | // concurrent STA and not dual AP, else start with idx 0. |
| 1835 | std::string WifiChip::allocateApIfaceName() { |
| 1836 | // Check if we have a dedicated iface for AP. |
| 1837 | std::vector<std::string> ifnames = getPredefinedApIfaceNames(true); |
| 1838 | for (auto const& ifname : ifnames) { |
| 1839 | if (findUsingName(ap_ifaces_, ifname)) continue; |
| 1840 | return ifname; |
| 1841 | } |
| 1842 | return allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface()); |
| 1843 | } |
| 1844 | |
| 1845 | std::vector<std::string> WifiChip::allocateBridgedApInstanceNames() { |
| 1846 | // Check if we have a dedicated iface for AP. |
| 1847 | std::vector<std::string> instances = getPredefinedApIfaceNames(true); |
| 1848 | if (instances.size() == 2) { |
| 1849 | return instances; |
| 1850 | } else { |
| 1851 | int num_ifaces_need_to_allocate = 2 - instances.size(); |
| 1852 | for (int i = 0; i < num_ifaces_need_to_allocate; i++) { |
| 1853 | std::string instance_name = |
| 1854 | allocateApOrStaIfaceName(IfaceType::AP, startIdxOfApIface() + i); |
| 1855 | if (!instance_name.empty()) { |
| 1856 | instances.push_back(instance_name); |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | return instances; |
| 1861 | } |
| 1862 | |
| 1863 | // STA iface names start with idx 0. |
| 1864 | // Primary STA iface will always be 0. |
| 1865 | std::string WifiChip::allocateStaIfaceName() { |
| 1866 | return allocateApOrStaIfaceName(IfaceType::STA, 0); |
| 1867 | } |
| 1868 | |
| 1869 | bool WifiChip::writeRingbufferFilesInternal() { |
| 1870 | if (!removeOldFilesInternal()) { |
| 1871 | LOG(ERROR) << "Error occurred while deleting old tombstone files"; |
| 1872 | return false; |
| 1873 | } |
| 1874 | // write ringbuffers to file |
| 1875 | { |
| 1876 | std::unique_lock<std::mutex> lk(lock_t); |
| 1877 | for (auto& item : ringbuffer_map_) { |
| 1878 | Ringbuffer& cur_buffer = item.second; |
| 1879 | if (cur_buffer.getData().empty()) { |
| 1880 | continue; |
| 1881 | } |
| 1882 | const std::string file_path_raw = kTombstoneFolderPath + item.first + "XXXXXXXXXX"; |
| 1883 | const int dump_fd = mkstemp(makeCharVec(file_path_raw).data()); |
| 1884 | if (dump_fd == -1) { |
| 1885 | PLOG(ERROR) << "create file failed"; |
| 1886 | return false; |
| 1887 | } |
| 1888 | unique_fd file_auto_closer(dump_fd); |
| 1889 | for (const auto& cur_block : cur_buffer.getData()) { |
| 1890 | if (cur_block.size() <= 0 || cur_block.size() > kMaxBufferSizeBytes) { |
| 1891 | PLOG(ERROR) << "Ring buffer: " << item.first |
| 1892 | << " is corrupted. Invalid block size: " << cur_block.size(); |
| 1893 | break; |
| 1894 | } |
| 1895 | if (write(dump_fd, cur_block.data(), sizeof(cur_block[0]) * cur_block.size()) == |
| 1896 | -1) { |
| 1897 | PLOG(ERROR) << "Error writing to file"; |
| 1898 | } |
| 1899 | } |
| 1900 | cur_buffer.clear(); |
| 1901 | } |
| 1902 | // unique_lock unlocked here |
| 1903 | } |
| 1904 | return true; |
| 1905 | } |
| 1906 | |
| 1907 | std::string WifiChip::getWlanIfaceNameWithType(IfaceType type, unsigned idx) { |
| 1908 | std::string ifname; |
| 1909 | |
| 1910 | // let the legacy hal override the interface name |
| 1911 | legacy_hal::wifi_error err = legacy_hal_.lock()->getSupportedIfaceName((uint32_t)type, ifname); |
| 1912 | if (err == legacy_hal::WIFI_SUCCESS) return ifname; |
| 1913 | |
| 1914 | return getWlanIfaceName(idx); |
| 1915 | } |
| 1916 | |
| 1917 | void WifiChip::invalidateAndClearBridgedApAll() { |
| 1918 | for (auto const& it : br_ifaces_ap_instances_) { |
| 1919 | for (auto const& iface : it.second) { |
| 1920 | iface_util_->removeIfaceFromBridge(it.first, iface); |
| 1921 | legacy_hal_.lock()->deleteVirtualInterface(iface); |
| 1922 | } |
| 1923 | iface_util_->deleteBridge(it.first); |
| 1924 | } |
| 1925 | br_ifaces_ap_instances_.clear(); |
| 1926 | } |
| 1927 | |
| 1928 | void WifiChip::invalidateAndClearBridgedAp(const std::string& br_name) { |
| 1929 | if (br_name.empty()) return; |
| 1930 | // delete managed interfaces |
| 1931 | for (auto const& it : br_ifaces_ap_instances_) { |
| 1932 | if (it.first == br_name) { |
| 1933 | for (auto const& iface : it.second) { |
| 1934 | iface_util_->removeIfaceFromBridge(br_name, iface); |
| 1935 | legacy_hal_.lock()->deleteVirtualInterface(iface); |
| 1936 | } |
| 1937 | iface_util_->deleteBridge(br_name); |
| 1938 | br_ifaces_ap_instances_.erase(br_name); |
| 1939 | break; |
| 1940 | } |
| 1941 | } |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | bool WifiChip::findUsingNameFromBridgedApInstances(const std::string& name) { |
| 1946 | for (auto const& it : br_ifaces_ap_instances_) { |
| 1947 | if (it.first == name) { |
| 1948 | return true; |
| 1949 | } |
| 1950 | for (auto const& iface : it.second) { |
| 1951 | if (iface == name) { |
| 1952 | return true; |
| 1953 | } |
| 1954 | } |
| 1955 | } |
| 1956 | return false; |
| 1957 | } |
| 1958 | |
| 1959 | } // namespace wifi |
| 1960 | } // namespace hardware |
| 1961 | } // namespace android |
| 1962 | } // namespace aidl |