blob: 85e373cde045dacbd5e8501e6cb6ba048cee206e [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_sta_iface.h"
18
19#include <android-base/logging.h>
20
21#include "aidl_return_util.h"
22#include "aidl_struct_util.h"
23#include "wifi_status_util.h"
24
25namespace aidl {
26namespace android {
27namespace hardware {
28namespace wifi {
29using aidl_return_util::validateAndCall;
30
31WifiStaIface::WifiStaIface(const std::string& ifname,
32 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
33 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util)
Xin Li64e598c2024-05-24 08:28:04 -070034 : ifname_(ifname),
35 legacy_hal_(legacy_hal),
36 iface_util_(iface_util),
37 is_valid_(true),
38 is_twt_registered_(false) {
Gabriel Birenf3262f92022-07-15 23:25:39 +000039 // Turn on DFS channel usage for STA iface.
40 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setDfsFlag(ifname_, true);
41 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
42 LOG(ERROR) << "Failed to set DFS flag; DFS channels may be unavailable.";
43 }
44}
45
46std::shared_ptr<WifiStaIface> WifiStaIface::create(
47 const std::string& ifname, const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
48 const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util) {
49 std::shared_ptr<WifiStaIface> ptr =
50 ndk::SharedRefBase::make<WifiStaIface>(ifname, legacy_hal, iface_util);
51 std::weak_ptr<WifiStaIface> weak_ptr_this(ptr);
52 ptr->setWeakPtr(weak_ptr_this);
53 return ptr;
54}
55
56void WifiStaIface::invalidate() {
57 legacy_hal_.reset();
58 event_cb_handler_.invalidate();
59 is_valid_ = false;
60}
61
62void WifiStaIface::setWeakPtr(std::weak_ptr<WifiStaIface> ptr) {
63 weak_ptr_this_ = ptr;
64}
65
66bool WifiStaIface::isValid() {
67 return is_valid_;
68}
69
70std::string WifiStaIface::getName() {
71 return ifname_;
72}
73
74std::set<std::shared_ptr<IWifiStaIfaceEventCallback>> WifiStaIface::getEventCallbacks() {
75 return event_cb_handler_.getCallbacks();
76}
77
78ndk::ScopedAStatus WifiStaIface::getName(std::string* _aidl_return) {
79 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
80 &WifiStaIface::getNameInternal, _aidl_return);
81}
82
83ndk::ScopedAStatus WifiStaIface::registerEventCallback(
84 const std::shared_ptr<IWifiStaIfaceEventCallback>& in_callback) {
85 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
86 &WifiStaIface::registerEventCallbackInternal, in_callback);
87}
88
Gabriel Biren2f862492023-03-09 19:13:07 +000089ndk::ScopedAStatus WifiStaIface::getFeatureSet(int32_t* _aidl_return) {
Gabriel Birenf3262f92022-07-15 23:25:39 +000090 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
Gabriel Biren2f862492023-03-09 19:13:07 +000091 &WifiStaIface::getFeatureSetInternal, _aidl_return);
Gabriel Birenf3262f92022-07-15 23:25:39 +000092}
93
94ndk::ScopedAStatus WifiStaIface::getApfPacketFilterCapabilities(
95 StaApfPacketFilterCapabilities* _aidl_return) {
96 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
97 &WifiStaIface::getApfPacketFilterCapabilitiesInternal, _aidl_return);
98}
99
100ndk::ScopedAStatus WifiStaIface::installApfPacketFilter(const std::vector<uint8_t>& in_program) {
101 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
102 &WifiStaIface::installApfPacketFilterInternal, in_program);
103}
104
105ndk::ScopedAStatus WifiStaIface::readApfPacketFilterData(std::vector<uint8_t>* _aidl_return) {
106 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
107 &WifiStaIface::readApfPacketFilterDataInternal, _aidl_return);
108}
109
110ndk::ScopedAStatus WifiStaIface::getBackgroundScanCapabilities(
111 StaBackgroundScanCapabilities* _aidl_return) {
112 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
113 &WifiStaIface::getBackgroundScanCapabilitiesInternal, _aidl_return);
114}
115
Gabriel Birenf3262f92022-07-15 23:25:39 +0000116ndk::ScopedAStatus WifiStaIface::startBackgroundScan(int32_t in_cmdId,
117 const StaBackgroundScanParameters& in_params) {
118 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
119 &WifiStaIface::startBackgroundScanInternal, in_cmdId, in_params);
120}
121
122ndk::ScopedAStatus WifiStaIface::stopBackgroundScan(int32_t in_cmdId) {
123 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
124 &WifiStaIface::stopBackgroundScanInternal, in_cmdId);
125}
126
127ndk::ScopedAStatus WifiStaIface::enableLinkLayerStatsCollection(bool in_debug) {
128 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
129 &WifiStaIface::enableLinkLayerStatsCollectionInternal, in_debug);
130}
131
132ndk::ScopedAStatus WifiStaIface::disableLinkLayerStatsCollection() {
133 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
134 &WifiStaIface::disableLinkLayerStatsCollectionInternal);
135}
136
137ndk::ScopedAStatus WifiStaIface::getLinkLayerStats(StaLinkLayerStats* _aidl_return) {
138 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
139 &WifiStaIface::getLinkLayerStatsInternal, _aidl_return);
140}
141
142ndk::ScopedAStatus WifiStaIface::startRssiMonitoring(int32_t in_cmdId, int32_t in_maxRssi,
143 int32_t in_minRssi) {
144 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
145 &WifiStaIface::startRssiMonitoringInternal, in_cmdId, in_maxRssi,
146 in_minRssi);
147}
148
149ndk::ScopedAStatus WifiStaIface::stopRssiMonitoring(int32_t in_cmdId) {
150 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
151 &WifiStaIface::stopRssiMonitoringInternal, in_cmdId);
152}
153
154ndk::ScopedAStatus WifiStaIface::getRoamingCapabilities(StaRoamingCapabilities* _aidl_return) {
155 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
156 &WifiStaIface::getRoamingCapabilitiesInternal, _aidl_return);
157}
158
159ndk::ScopedAStatus WifiStaIface::configureRoaming(const StaRoamingConfig& in_config) {
160 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
161 &WifiStaIface::configureRoamingInternal, in_config);
162}
163
164ndk::ScopedAStatus WifiStaIface::setRoamingState(StaRoamingState in_state) {
165 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
166 &WifiStaIface::setRoamingStateInternal, in_state);
167}
168
169ndk::ScopedAStatus WifiStaIface::enableNdOffload(bool in_enable) {
170 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
171 &WifiStaIface::enableNdOffloadInternal, in_enable);
172}
173
174ndk::ScopedAStatus WifiStaIface::startSendingKeepAlivePackets(
175 int32_t in_cmdId, const std::vector<uint8_t>& in_ipPacketData, char16_t in_etherType,
176 const std::array<uint8_t, 6>& in_srcAddress, const std::array<uint8_t, 6>& in_dstAddress,
177 int32_t in_periodInMs) {
178 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
179 &WifiStaIface::startSendingKeepAlivePacketsInternal, in_cmdId,
180 in_ipPacketData, in_etherType, in_srcAddress, in_dstAddress,
181 in_periodInMs);
182}
183
184ndk::ScopedAStatus WifiStaIface::stopSendingKeepAlivePackets(int32_t in_cmdId) {
185 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
186 &WifiStaIface::stopSendingKeepAlivePacketsInternal, in_cmdId);
187}
188
189ndk::ScopedAStatus WifiStaIface::startDebugPacketFateMonitoring() {
190 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
191 &WifiStaIface::startDebugPacketFateMonitoringInternal);
192}
193
194ndk::ScopedAStatus WifiStaIface::getDebugTxPacketFates(
195 std::vector<WifiDebugTxPacketFateReport>* _aidl_return) {
196 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
197 &WifiStaIface::getDebugTxPacketFatesInternal, _aidl_return);
198}
199
200ndk::ScopedAStatus WifiStaIface::getDebugRxPacketFates(
201 std::vector<WifiDebugRxPacketFateReport>* _aidl_return) {
202 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
203 &WifiStaIface::getDebugRxPacketFatesInternal, _aidl_return);
204}
205
206ndk::ScopedAStatus WifiStaIface::setMacAddress(const std::array<uint8_t, 6>& in_mac) {
207 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
208 &WifiStaIface::setMacAddressInternal, in_mac);
209}
210
211ndk::ScopedAStatus WifiStaIface::getFactoryMacAddress(std::array<uint8_t, 6>* _aidl_return) {
212 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
213 &WifiStaIface::getFactoryMacAddressInternal, _aidl_return);
214}
215
216ndk::ScopedAStatus WifiStaIface::setScanMode(bool in_enable) {
217 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
218 &WifiStaIface::setScanModeInternal, in_enable);
219}
220
Kai Shi6d02d402022-11-17 16:34:05 -0800221ndk::ScopedAStatus WifiStaIface::setDtimMultiplier(int32_t in_multiplier) {
222 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
223 &WifiStaIface::setDtimMultiplierInternal, in_multiplier);
224}
225
Kai Shi7d0e5e92023-11-20 19:23:36 -0800226ndk::ScopedAStatus WifiStaIface::getCachedScanData(CachedScanData* _aidl_return) {
227 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
228 &WifiStaIface::getCachedScanDataInternal, _aidl_return);
229}
230
maheshkkv39903822023-11-28 15:31:53 -0800231ndk::ScopedAStatus WifiStaIface::twtGetCapabilities(TwtCapabilities* _aidl_return) {
232 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
233 &WifiStaIface::twtGetCapabilitiesInternal, _aidl_return);
234}
235
236ndk::ScopedAStatus WifiStaIface::twtSessionSetup(int32_t in_cmdId,
237 const TwtRequest& in_twtRequest) {
238 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
239 &WifiStaIface::twtSessionSetupInternal, in_cmdId, in_twtRequest);
240}
241
242ndk::ScopedAStatus WifiStaIface::twtSessionUpdate(int32_t in_cmdId, int32_t in_sessionId,
243 const TwtRequest& in_twtRequest) {
244 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
245 &WifiStaIface::twtSessionUpdateInternal, in_cmdId, in_sessionId,
246 in_twtRequest);
247}
248
249ndk::ScopedAStatus WifiStaIface::twtSessionSuspend(int32_t in_cmdId, int32_t in_sessionId) {
250 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
251 &WifiStaIface::twtSessionSuspendInternal, in_cmdId, in_sessionId);
252}
253
254ndk::ScopedAStatus WifiStaIface::twtSessionResume(int32_t in_cmdId, int32_t in_sessionId) {
255 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
256 &WifiStaIface::twtSessionResumeInternal, in_cmdId, in_sessionId);
257}
258
259ndk::ScopedAStatus WifiStaIface::twtSessionTeardown(int32_t in_cmdId, int32_t in_sessionId) {
260 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
261 &WifiStaIface::twtSessionTeardownInternal, in_cmdId, in_sessionId);
262}
263
264ndk::ScopedAStatus WifiStaIface::twtSessionGetStats(int32_t in_cmdId, int32_t in_sessionId) {
265 return validateAndCall(this, WifiStatusCode::ERROR_WIFI_IFACE_INVALID,
266 &WifiStaIface::twtSessionGetStatsInternal, in_cmdId, in_sessionId);
267}
268
Gabriel Birenf3262f92022-07-15 23:25:39 +0000269std::pair<std::string, ndk::ScopedAStatus> WifiStaIface::getNameInternal() {
270 return {ifname_, ndk::ScopedAStatus::ok()};
271}
272
Xin Li64e598c2024-05-24 08:28:04 -0700273ndk::ScopedAStatus WifiStaIface::registerTwtEventCallbackInternal() {
274 std::weak_ptr<WifiStaIface> weak_ptr_this = weak_ptr_this_;
275
276 // onTwtFailure callback
277 const auto& on_twt_failure = [weak_ptr_this](legacy_hal::wifi_request_id id,
278 legacy_hal::wifi_twt_error_code error_code) {
279 const auto shared_ptr_this = weak_ptr_this.lock();
280 IWifiStaIfaceEventCallback::TwtErrorCode aidl_error_code =
281 aidl_struct_util::convertLegacyHalTwtErrorCodeToAidl(error_code);
282 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
283 LOG(ERROR) << "Callback invoked on an invalid object";
284 return;
285 }
286 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
287 if (!callback->onTwtFailure(id, aidl_error_code).isOk()) {
288 LOG(ERROR) << "Failed to invoke onTwtFailure callback";
289 }
290 }
291 };
292 // onTwtSessionCreate callback
293 const auto& on_twt_session_create = [weak_ptr_this](legacy_hal::wifi_request_id id,
294 legacy_hal::wifi_twt_session twt_session) {
295 const auto shared_ptr_this = weak_ptr_this.lock();
296 TwtSession aidl_twt_session;
297 if (!aidl_struct_util::convertLegacyHalTwtSessionToAidl(twt_session, &aidl_twt_session)) {
298 LOG(ERROR) << "convertLegacyHalTwtSessionToAidl failed";
299 return;
300 }
301
302 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
303 LOG(ERROR) << "Callback invoked on an invalid object";
304 return;
305 }
306 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
307 if (!callback->onTwtSessionCreate(id, aidl_twt_session).isOk()) {
308 LOG(ERROR) << "Failed to invoke onTwtSessionCreate callback";
309 }
310 }
311 };
312 // onTwtSessionUpdate callback
313 const auto& on_twt_session_update = [weak_ptr_this](legacy_hal::wifi_request_id id,
314 legacy_hal::wifi_twt_session twt_session) {
315 const auto shared_ptr_this = weak_ptr_this.lock();
316 TwtSession aidl_twt_session;
317 if (!aidl_struct_util::convertLegacyHalTwtSessionToAidl(twt_session, &aidl_twt_session)) {
318 LOG(ERROR) << "convertLegacyHalTwtSessionToAidl failed";
319 return;
320 }
321
322 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
323 LOG(ERROR) << "Callback invoked on an invalid object";
324 return;
325 }
326 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
327 if (!callback->onTwtSessionUpdate(id, aidl_twt_session).isOk()) {
328 LOG(ERROR) << "Failed to invoke onTwtSessionUpdate callback";
329 }
330 }
331 };
332 // onTwtSessionTeardown callback
333 const auto& on_twt_session_teardown =
334 [weak_ptr_this](legacy_hal::wifi_request_id id, int session_id,
335 legacy_hal::wifi_twt_teardown_reason_code reason_code) {
336 const auto shared_ptr_this = weak_ptr_this.lock();
337 IWifiStaIfaceEventCallback::TwtTeardownReasonCode aidl_reason_code =
338 aidl_struct_util::convertLegacyHalTwtReasonCodeToAidl(reason_code);
339 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
340 LOG(ERROR) << "Callback invoked on an invalid object";
341 return;
342 }
343 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
344 if (!callback->onTwtSessionTeardown(id, session_id, aidl_reason_code).isOk()) {
345 LOG(ERROR) << "Failed to invoke onTwtSessionTeardown callback";
346 }
347 }
348 };
349 // onTwtSessionStats callback
350 const auto& on_twt_session_stats = [weak_ptr_this](legacy_hal::wifi_request_id id,
351 int session_id,
352 legacy_hal::wifi_twt_session_stats stats) {
353 const auto shared_ptr_this = weak_ptr_this.lock();
354 TwtSessionStats aidl_session_stats;
355 if (!aidl_struct_util::convertLegacyHalTwtSessionStatsToAidl(stats, &aidl_session_stats)) {
356 LOG(ERROR) << "convertLegacyHalTwtSessionStatsToAidl failed";
357 return;
358 }
359 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
360 LOG(ERROR) << "Callback invoked on an invalid object";
361 return;
362 }
363 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
364 if (!callback->onTwtSessionStats(id, session_id, aidl_session_stats).isOk()) {
365 LOG(ERROR) << "Failed to invoke onTwtSessionStats callback";
366 }
367 }
368 };
369 // onTwtSessionSuspend callback
370 const auto& on_twt_session_suspend = [weak_ptr_this](legacy_hal::wifi_request_id id,
371 int session_id) {
372 const auto shared_ptr_this = weak_ptr_this.lock();
373 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
374 LOG(ERROR) << "Callback invoked on an invalid object";
375 return;
376 }
377 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
378 if (!callback->onTwtSessionSuspend(id, session_id).isOk()) {
379 LOG(ERROR) << "Failed to invoke onTwtSessionSuspend callback";
380 }
381 }
382 };
383 // onTwtSessionResume callback
384 const auto& on_twt_session_resume = [weak_ptr_this](legacy_hal::wifi_request_id id,
385 int session_id) {
386 const auto shared_ptr_this = weak_ptr_this.lock();
387 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
388 LOG(ERROR) << "Callback invoked on an invalid object";
389 return;
390 }
391 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
392 if (!callback->onTwtSessionResume(id, session_id).isOk()) {
393 LOG(ERROR) << "Failed to invoke onTwtSessionResume callback";
394 }
395 }
396 };
397
398 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->twtRegisterEvents(
399 ifname_, on_twt_failure, on_twt_session_create, on_twt_session_update,
400 on_twt_session_teardown, on_twt_session_stats, on_twt_session_suspend,
401 on_twt_session_resume);
402
403 if (legacy_status == legacy_hal::WIFI_ERROR_NOT_SUPPORTED) {
404 LOG(INFO) << "twtRegisterEvents is not supported" << legacy_status;
405 } else if (legacy_status != legacy_hal::WIFI_SUCCESS) {
406 LOG(ERROR) << "twtRegisterEvents failed - %d" << legacy_status;
407 }
408 return createWifiStatusFromLegacyError(legacy_status);
409}
410
Gabriel Birenf3262f92022-07-15 23:25:39 +0000411ndk::ScopedAStatus WifiStaIface::registerEventCallbackInternal(
412 const std::shared_ptr<IWifiStaIfaceEventCallback>& callback) {
413 if (!event_cb_handler_.addCallback(callback)) {
414 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
415 }
Xin Li64e598c2024-05-24 08:28:04 -0700416 is_twt_registered_ = registerTwtEventCallbackInternal().isOk();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000417 return ndk::ScopedAStatus::ok();
418}
419
Gabriel Biren2f862492023-03-09 19:13:07 +0000420std::pair<int32_t, ndk::ScopedAStatus> WifiStaIface::getFeatureSetInternal() {
Gabriel Birenf3262f92022-07-15 23:25:39 +0000421 legacy_hal::wifi_error legacy_status;
422 uint64_t legacy_feature_set;
423 std::tie(legacy_status, legacy_feature_set) =
424 legacy_hal_.lock()->getSupportedFeatureSet(ifname_);
425 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
Gabriel Biren3b86a782023-02-04 00:42:53 +0000426 return {0, createWifiStatusFromLegacyError(legacy_status)};
Gabriel Birenf3262f92022-07-15 23:25:39 +0000427 }
Gabriel Biren2f862492023-03-09 19:13:07 +0000428 uint32_t aidl_feature_set;
429 if (!aidl_struct_util::convertLegacyStaIfaceFeaturesToAidl(legacy_feature_set,
430 &aidl_feature_set)) {
Gabriel Biren3b86a782023-02-04 00:42:53 +0000431 return {0, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
Gabriel Birenf3262f92022-07-15 23:25:39 +0000432 }
Gabriel Biren2f862492023-03-09 19:13:07 +0000433 return {aidl_feature_set, ndk::ScopedAStatus::ok()};
Gabriel Birenf3262f92022-07-15 23:25:39 +0000434}
435
436std::pair<StaApfPacketFilterCapabilities, ndk::ScopedAStatus>
437WifiStaIface::getApfPacketFilterCapabilitiesInternal() {
438 legacy_hal::wifi_error legacy_status;
439 legacy_hal::PacketFilterCapabilities legacy_caps;
440 std::tie(legacy_status, legacy_caps) = legacy_hal_.lock()->getPacketFilterCapabilities(ifname_);
441 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
442 return {StaApfPacketFilterCapabilities{}, createWifiStatusFromLegacyError(legacy_status)};
443 }
444 StaApfPacketFilterCapabilities aidl_caps;
445 if (!aidl_struct_util::convertLegacyApfCapabilitiesToAidl(legacy_caps, &aidl_caps)) {
446 return {StaApfPacketFilterCapabilities{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
447 }
448 return {aidl_caps, ndk::ScopedAStatus::ok()};
449}
450
451ndk::ScopedAStatus WifiStaIface::installApfPacketFilterInternal(
452 const std::vector<uint8_t>& program) {
453 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setPacketFilter(ifname_, program);
454 return createWifiStatusFromLegacyError(legacy_status);
455}
456
457std::pair<std::vector<uint8_t>, ndk::ScopedAStatus>
458WifiStaIface::readApfPacketFilterDataInternal() {
459 const std::pair<legacy_hal::wifi_error, std::vector<uint8_t>> legacy_status_and_data =
460 legacy_hal_.lock()->readApfPacketFilterData(ifname_);
461 return {std::move(legacy_status_and_data.second),
462 createWifiStatusFromLegacyError(legacy_status_and_data.first)};
463}
464
465std::pair<StaBackgroundScanCapabilities, ndk::ScopedAStatus>
466WifiStaIface::getBackgroundScanCapabilitiesInternal() {
467 legacy_hal::wifi_error legacy_status;
468 legacy_hal::wifi_gscan_capabilities legacy_caps;
469 std::tie(legacy_status, legacy_caps) = legacy_hal_.lock()->getGscanCapabilities(ifname_);
470 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
471 return {StaBackgroundScanCapabilities{}, createWifiStatusFromLegacyError(legacy_status)};
472 }
473 StaBackgroundScanCapabilities aidl_caps;
474 if (!aidl_struct_util::convertLegacyGscanCapabilitiesToAidl(legacy_caps, &aidl_caps)) {
475 return {StaBackgroundScanCapabilities{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
476 }
477 return {aidl_caps, ndk::ScopedAStatus::ok()};
478}
479
Gabriel Birenf3262f92022-07-15 23:25:39 +0000480ndk::ScopedAStatus WifiStaIface::startBackgroundScanInternal(
481 int32_t cmd_id, const StaBackgroundScanParameters& params) {
482 legacy_hal::wifi_scan_cmd_params legacy_params;
483 if (!aidl_struct_util::convertAidlGscanParamsToLegacy(params, &legacy_params)) {
484 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
485 }
486 std::weak_ptr<WifiStaIface> weak_ptr_this = weak_ptr_this_;
487 const auto& on_failure_callback = [weak_ptr_this](legacy_hal::wifi_request_id id) {
488 const auto shared_ptr_this = weak_ptr_this.lock();
489 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
490 LOG(ERROR) << "Callback invoked on an invalid object";
491 return;
492 }
493 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
494 if (!callback->onBackgroundScanFailure(id).isOk()) {
495 LOG(ERROR) << "Failed to invoke onBackgroundScanFailure callback";
496 }
497 }
498 };
499 const auto& on_results_callback =
500 [weak_ptr_this](legacy_hal::wifi_request_id id,
501 const std::vector<legacy_hal::wifi_cached_scan_results>& results) {
502 const auto shared_ptr_this = weak_ptr_this.lock();
503 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
504 LOG(ERROR) << "Callback invoked on an invalid object";
505 return;
506 }
507 std::vector<StaScanData> aidl_scan_datas;
508 if (!aidl_struct_util::convertLegacyVectorOfCachedGscanResultsToAidl(
509 results, &aidl_scan_datas)) {
510 LOG(ERROR) << "Failed to convert scan results to AIDL structs";
511 return;
512 }
513 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
514 if (!callback->onBackgroundScanResults(id, aidl_scan_datas).isOk()) {
515 LOG(ERROR) << "Failed to invoke onBackgroundScanResults callback";
516 }
517 }
518 };
519 const auto& on_full_result_callback = [weak_ptr_this](
520 legacy_hal::wifi_request_id id,
521 const legacy_hal::wifi_scan_result* result,
522 uint32_t buckets_scanned) {
523 const auto shared_ptr_this = weak_ptr_this.lock();
524 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
525 LOG(ERROR) << "Callback invoked on an invalid object";
526 return;
527 }
528 StaScanResult aidl_scan_result;
529 if (!aidl_struct_util::convertLegacyGscanResultToAidl(*result, true, &aidl_scan_result)) {
530 LOG(ERROR) << "Failed to convert full scan results to AIDL structs";
531 return;
532 }
533 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
534 if (!callback->onBackgroundFullScanResult(id, buckets_scanned, aidl_scan_result)
535 .isOk()) {
536 LOG(ERROR) << "Failed to invoke onBackgroundFullScanResult callback";
537 }
538 }
539 };
540 legacy_hal::wifi_error legacy_status =
541 legacy_hal_.lock()->startGscan(ifname_, cmd_id, legacy_params, on_failure_callback,
542 on_results_callback, on_full_result_callback);
543 return createWifiStatusFromLegacyError(legacy_status);
544}
545
546ndk::ScopedAStatus WifiStaIface::stopBackgroundScanInternal(int32_t cmd_id) {
547 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stopGscan(ifname_, cmd_id);
548 return createWifiStatusFromLegacyError(legacy_status);
549}
550
551ndk::ScopedAStatus WifiStaIface::enableLinkLayerStatsCollectionInternal(bool debug) {
552 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->enableLinkLayerStats(ifname_, debug);
553 return createWifiStatusFromLegacyError(legacy_status);
554}
555
556ndk::ScopedAStatus WifiStaIface::disableLinkLayerStatsCollectionInternal() {
557 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->disableLinkLayerStats(ifname_);
558 return createWifiStatusFromLegacyError(legacy_status);
559}
560
561std::pair<StaLinkLayerStats, ndk::ScopedAStatus> WifiStaIface::getLinkLayerStatsInternal() {
562 legacy_hal::wifi_error legacy_status;
Mahesh KKV5f30d332022-10-26 14:07:44 -0700563 legacy_hal::LinkLayerStats legacy_stats{};
564 legacy_hal::LinkLayerMlStats legacy_ml_stats{};
565 legacy_status = legacy_hal_.lock()->getLinkLayerStats(ifname_, legacy_stats, legacy_ml_stats);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000566 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
567 return {StaLinkLayerStats{}, createWifiStatusFromLegacyError(legacy_status)};
568 }
569 StaLinkLayerStats aidl_stats;
Mahesh KKV5f30d332022-10-26 14:07:44 -0700570 if (legacy_stats.valid) {
571 if (!aidl_struct_util::convertLegacyLinkLayerStatsToAidl(legacy_stats, &aidl_stats)) {
572 return {StaLinkLayerStats{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
573 }
574 } else if (legacy_ml_stats.valid) {
575 if (!aidl_struct_util::convertLegacyLinkLayerMlStatsToAidl(legacy_ml_stats, &aidl_stats)) {
576 return {StaLinkLayerStats{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
577 }
578 } else {
Gabriel Birenf3262f92022-07-15 23:25:39 +0000579 return {StaLinkLayerStats{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
580 }
Gabriel Biren2ccf70f2024-02-14 22:52:58 +0000581 aidl_struct_util::logAidlLinkLayerStatsSize(aidl_stats);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000582 return {aidl_stats, ndk::ScopedAStatus::ok()};
583}
584
585ndk::ScopedAStatus WifiStaIface::startRssiMonitoringInternal(int32_t cmd_id, int32_t max_rssi,
586 int32_t min_rssi) {
587 std::weak_ptr<WifiStaIface> weak_ptr_this = weak_ptr_this_;
588 const auto& on_threshold_breached_callback =
589 [weak_ptr_this](legacy_hal::wifi_request_id id, std::array<uint8_t, ETH_ALEN> bssid,
590 int8_t rssi) {
591 const auto shared_ptr_this = weak_ptr_this.lock();
592 if (!shared_ptr_this.get() || !shared_ptr_this->isValid()) {
593 LOG(ERROR) << "Callback invoked on an invalid object";
594 return;
595 }
596 for (const auto& callback : shared_ptr_this->getEventCallbacks()) {
597 if (!callback->onRssiThresholdBreached(id, bssid, rssi).isOk()) {
598 LOG(ERROR) << "Failed to invoke onRssiThresholdBreached callback";
599 }
600 }
601 };
602 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startRssiMonitoring(
603 ifname_, cmd_id, max_rssi, min_rssi, on_threshold_breached_callback);
604 return createWifiStatusFromLegacyError(legacy_status);
605}
606
607ndk::ScopedAStatus WifiStaIface::stopRssiMonitoringInternal(int32_t cmd_id) {
608 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->stopRssiMonitoring(ifname_, cmd_id);
609 return createWifiStatusFromLegacyError(legacy_status);
610}
611
612std::pair<StaRoamingCapabilities, ndk::ScopedAStatus>
613WifiStaIface::getRoamingCapabilitiesInternal() {
614 legacy_hal::wifi_error legacy_status;
615 legacy_hal::wifi_roaming_capabilities legacy_caps;
616 std::tie(legacy_status, legacy_caps) = legacy_hal_.lock()->getRoamingCapabilities(ifname_);
617 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
618 return {StaRoamingCapabilities{}, createWifiStatusFromLegacyError(legacy_status)};
619 }
620 StaRoamingCapabilities aidl_caps;
621 if (!aidl_struct_util::convertLegacyRoamingCapabilitiesToAidl(legacy_caps, &aidl_caps)) {
622 return {StaRoamingCapabilities{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
623 }
624 return {aidl_caps, ndk::ScopedAStatus::ok()};
625}
626
627ndk::ScopedAStatus WifiStaIface::configureRoamingInternal(const StaRoamingConfig& config) {
628 legacy_hal::wifi_roaming_config legacy_config;
629 if (!aidl_struct_util::convertAidlRoamingConfigToLegacy(config, &legacy_config)) {
630 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
631 }
632 legacy_hal::wifi_error legacy_status =
633 legacy_hal_.lock()->configureRoaming(ifname_, legacy_config);
634 return createWifiStatusFromLegacyError(legacy_status);
635}
636
637ndk::ScopedAStatus WifiStaIface::setRoamingStateInternal(StaRoamingState state) {
638 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->enableFirmwareRoaming(
639 ifname_, aidl_struct_util::convertAidlRoamingStateToLegacy(state));
640 return createWifiStatusFromLegacyError(legacy_status);
641}
642
643ndk::ScopedAStatus WifiStaIface::enableNdOffloadInternal(bool enable) {
644 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->configureNdOffload(ifname_, enable);
645 return createWifiStatusFromLegacyError(legacy_status);
646}
647
648ndk::ScopedAStatus WifiStaIface::startSendingKeepAlivePacketsInternal(
649 int32_t cmd_id, const std::vector<uint8_t>& ip_packet_data, char16_t ether_type,
650 const std::array<uint8_t, 6>& src_address, const std::array<uint8_t, 6>& dst_address,
651 int32_t period_in_ms) {
652 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startSendingOffloadedPacket(
653 ifname_, cmd_id, ether_type, ip_packet_data, src_address, dst_address, period_in_ms);
654 return createWifiStatusFromLegacyError(legacy_status);
655}
656
657ndk::ScopedAStatus WifiStaIface::stopSendingKeepAlivePacketsInternal(int32_t cmd_id) {
658 legacy_hal::wifi_error legacy_status =
659 legacy_hal_.lock()->stopSendingOffloadedPacket(ifname_, cmd_id);
660 return createWifiStatusFromLegacyError(legacy_status);
661}
662
663ndk::ScopedAStatus WifiStaIface::startDebugPacketFateMonitoringInternal() {
664 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->startPktFateMonitoring(ifname_);
665 return createWifiStatusFromLegacyError(legacy_status);
666}
667
668std::pair<std::vector<WifiDebugTxPacketFateReport>, ndk::ScopedAStatus>
669WifiStaIface::getDebugTxPacketFatesInternal() {
670 legacy_hal::wifi_error legacy_status;
671 std::vector<legacy_hal::wifi_tx_report> legacy_fates;
672 std::tie(legacy_status, legacy_fates) = legacy_hal_.lock()->getTxPktFates(ifname_);
673 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
674 return {std::vector<WifiDebugTxPacketFateReport>(),
675 createWifiStatusFromLegacyError(legacy_status)};
676 }
677 std::vector<WifiDebugTxPacketFateReport> aidl_fates;
678 if (!aidl_struct_util::convertLegacyVectorOfDebugTxPacketFateToAidl(legacy_fates,
679 &aidl_fates)) {
680 return {std::vector<WifiDebugTxPacketFateReport>(),
681 createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
682 }
683 return {aidl_fates, ndk::ScopedAStatus::ok()};
684}
685
686std::pair<std::vector<WifiDebugRxPacketFateReport>, ndk::ScopedAStatus>
687WifiStaIface::getDebugRxPacketFatesInternal() {
688 legacy_hal::wifi_error legacy_status;
689 std::vector<legacy_hal::wifi_rx_report> legacy_fates;
690 std::tie(legacy_status, legacy_fates) = legacy_hal_.lock()->getRxPktFates(ifname_);
691 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
692 return {std::vector<WifiDebugRxPacketFateReport>(),
693 createWifiStatusFromLegacyError(legacy_status)};
694 }
695 std::vector<WifiDebugRxPacketFateReport> aidl_fates;
696 if (!aidl_struct_util::convertLegacyVectorOfDebugRxPacketFateToAidl(legacy_fates,
697 &aidl_fates)) {
698 return {std::vector<WifiDebugRxPacketFateReport>(),
699 createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
700 }
701 return {aidl_fates, ndk::ScopedAStatus::ok()};
702}
703
704ndk::ScopedAStatus WifiStaIface::setMacAddressInternal(const std::array<uint8_t, 6>& mac) {
705 bool status = iface_util_.lock()->setMacAddress(ifname_, mac);
706 if (!status) {
707 return createWifiStatus(WifiStatusCode::ERROR_UNKNOWN);
708 }
709 return ndk::ScopedAStatus::ok();
710}
711
712std::pair<std::array<uint8_t, 6>, ndk::ScopedAStatus> WifiStaIface::getFactoryMacAddressInternal() {
713 std::array<uint8_t, 6> mac = iface_util_.lock()->getFactoryMacAddress(ifname_);
714 if (mac[0] == 0 && mac[1] == 0 && mac[2] == 0 && mac[3] == 0 && mac[4] == 0 && mac[5] == 0) {
715 return {mac, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
716 }
717 return {mac, ndk::ScopedAStatus::ok()};
718}
719
720ndk::ScopedAStatus WifiStaIface::setScanModeInternal(bool enable) {
Ye Jiao50274f72023-01-17 14:53:22 +0800721 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setScanMode(ifname_, enable);
722 return createWifiStatusFromLegacyError(legacy_status);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000723}
724
Kai Shi6d02d402022-11-17 16:34:05 -0800725ndk::ScopedAStatus WifiStaIface::setDtimMultiplierInternal(const int multiplier) {
726 legacy_hal::wifi_error legacy_status = legacy_hal_.lock()->setDtimConfig(ifname_, multiplier);
727 return createWifiStatusFromLegacyError(legacy_status);
728}
729
Kai Shi7d0e5e92023-11-20 19:23:36 -0800730std::pair<CachedScanData, ndk::ScopedAStatus> WifiStaIface::getCachedScanDataInternal() {
731 legacy_hal::WifiCachedScanReport cached_scan_report;
732 legacy_hal::wifi_error legacy_status =
733 legacy_hal_.lock()->getWifiCachedScanResults(ifname_, cached_scan_report);
734 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
735 return {CachedScanData{}, createWifiStatusFromLegacyError(legacy_status)};
736 }
737 CachedScanData aidl_scan_data;
738 if (!aidl_struct_util::convertCachedScanReportToAidl(cached_scan_report, &aidl_scan_data)) {
739 return {CachedScanData{}, createWifiStatus(WifiStatusCode::ERROR_UNKNOWN)};
740 }
741
742 return {aidl_scan_data, ndk::ScopedAStatus::ok()};
743}
744
maheshkkv39903822023-11-28 15:31:53 -0800745std::pair<TwtCapabilities, ndk::ScopedAStatus> WifiStaIface::twtGetCapabilitiesInternal() {
746 legacy_hal::wifi_twt_capabilities legacyHaltwtCapabilities;
747 legacy_hal::wifi_error legacy_status;
748 std::tie(legacyHaltwtCapabilities, legacy_status) =
749 legacy_hal_.lock()->twtGetCapabilities(ifname_);
750 if (legacy_status != legacy_hal::WIFI_SUCCESS) {
751 return {TwtCapabilities{}, createWifiStatusFromLegacyError(legacy_status)};
752 }
753 TwtCapabilities aidlTwtCapabilities;
754 if (!aidl_struct_util::convertTwtCapabilitiesToAidl(legacyHaltwtCapabilities,
755 &aidlTwtCapabilities)) {
756 return {TwtCapabilities{}, createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS)};
757 }
758 return {aidlTwtCapabilities, ndk::ScopedAStatus::ok()};
759}
760
761ndk::ScopedAStatus WifiStaIface::twtSessionSetupInternal(int32_t cmdId,
762 const TwtRequest& aidlTwtRequest) {
Xin Li64e598c2024-05-24 08:28:04 -0700763 if (!is_twt_registered_) {
764 LOG(INFO) << "twtSessionSetup is not supported as twtRegisterEvents failed";
765 return createWifiStatus(WifiStatusCode::ERROR_NOT_SUPPORTED);
766 }
maheshkkv39903822023-11-28 15:31:53 -0800767 legacy_hal::wifi_twt_request legacyHalTwtRequest;
768 if (!aidl_struct_util::convertAidlTwtRequestToLegacy(aidlTwtRequest, &legacyHalTwtRequest)) {
769 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
770 }
Xin Li64e598c2024-05-24 08:28:04 -0700771 legacy_hal::wifi_error legacy_status =
772 legacy_hal_.lock()->twtSessionSetup(ifname_, cmdId, legacyHalTwtRequest);
maheshkkv39903822023-11-28 15:31:53 -0800773 return createWifiStatusFromLegacyError(legacy_status);
774}
775
776ndk::ScopedAStatus WifiStaIface::twtSessionUpdateInternal(int32_t cmdId, int32_t sessionId,
777 const TwtRequest& aidlTwtRequest) {
778 legacy_hal::wifi_twt_request legacyHalTwtRequest;
779 if (!aidl_struct_util::convertAidlTwtRequestToLegacy(aidlTwtRequest, &legacyHalTwtRequest)) {
780 return createWifiStatus(WifiStatusCode::ERROR_INVALID_ARGS);
781 }
782 legacy_hal::wifi_error legacy_status =
783 legacy_hal_.lock()->twtSessionUpdate(ifname_, cmdId, sessionId, legacyHalTwtRequest);
784 return createWifiStatusFromLegacyError(legacy_status);
785}
786
787ndk::ScopedAStatus WifiStaIface::twtSessionSuspendInternal(int32_t cmdId, int32_t sessionId) {
788 legacy_hal::wifi_error legacy_status =
789 legacy_hal_.lock()->twtSessionSuspend(ifname_, cmdId, sessionId);
790 return createWifiStatusFromLegacyError(legacy_status);
791}
792
793ndk::ScopedAStatus WifiStaIface::twtSessionResumeInternal(int32_t cmdId, int32_t sessionId) {
794 legacy_hal::wifi_error legacy_status =
795 legacy_hal_.lock()->twtSessionResume(ifname_, cmdId, sessionId);
796 return createWifiStatusFromLegacyError(legacy_status);
797}
798
799ndk::ScopedAStatus WifiStaIface::twtSessionTeardownInternal(int32_t cmdId, int32_t sessionId) {
800 legacy_hal::wifi_error legacy_status =
801 legacy_hal_.lock()->twtSessionTeardown(ifname_, cmdId, sessionId);
802 return createWifiStatusFromLegacyError(legacy_status);
803}
804
805ndk::ScopedAStatus WifiStaIface::twtSessionGetStatsInternal(int32_t cmdId, int32_t sessionId) {
806 legacy_hal::wifi_error legacy_status =
807 legacy_hal_.lock()->twtSessionGetStats(ifname_, cmdId, sessionId);
808 return createWifiStatusFromLegacyError(legacy_status);
809}
810
Gabriel Birenf3262f92022-07-15 23:25:39 +0000811} // namespace wifi
812} // namespace hardware
813} // namespace android
814} // namespace aidl