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