blob: 25f18b5484f6470b91ba2aed46b2fffa5889aae6 [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_legacy_hal.h"
18
19#include <android-base/logging.h>
20#include <cutils/properties.h>
21#include <net/if.h>
22
23#include <array>
24#include <chrono>
25
26#include "aidl_sync_util.h"
27#include "wifi_legacy_hal_stubs.h"
28
29namespace {
30// Constants ported over from the legacy HAL calling code
31// (com_android_server_wifi_WifiNative.cpp). This will all be thrown
32// away when this shim layer is replaced by the real vendor
33// implementation.
34static constexpr uint32_t kMaxVersionStringLength = 256;
35static constexpr uint32_t kMaxCachedGscanResults = 64;
36static constexpr uint32_t kMaxGscanFrequenciesForBand = 64;
37static constexpr uint32_t kLinkLayerStatsDataMpduSizeThreshold = 128;
38static constexpr uint32_t kMaxWakeReasonStatsArraySize = 32;
39static constexpr uint32_t kMaxRingBuffers = 10;
40static constexpr uint32_t kMaxWifiUsableChannels = 256;
41static constexpr uint32_t kMaxSupportedRadioCombinationsMatrixLength = 256;
42// Need a long timeout (1000ms) for chips that unload their driver.
43static constexpr uint32_t kMaxStopCompleteWaitMs = 1000;
44static constexpr char kDriverPropName[] = "wlan.driver.status";
45
46// Helper function to create a non-const char* for legacy Hal API's.
47std::vector<char> makeCharVec(const std::string& str) {
48 std::vector<char> vec(str.size() + 1);
49 vec.assign(str.begin(), str.end());
50 vec.push_back('\0');
51 return vec;
52}
53} // namespace
54
55namespace aidl {
56namespace android {
57namespace hardware {
58namespace wifi {
59namespace legacy_hal {
60
61// Legacy HAL functions accept "C" style function pointers, so use global
62// functions to pass to the legacy HAL function and store the corresponding
63// std::function methods to be invoked.
64//
65// Callback to be invoked once |stop| is complete
66std::function<void(wifi_handle handle)> on_stop_complete_internal_callback;
67void onAsyncStopComplete(wifi_handle handle) {
68 const auto lock = aidl_sync_util::acquireGlobalLock();
69 if (on_stop_complete_internal_callback) {
70 on_stop_complete_internal_callback(handle);
71 // Invalidate this callback since we don't want this firing again.
72 on_stop_complete_internal_callback = nullptr;
73 }
74}
75
76// Callback to be invoked for driver dump.
77std::function<void(char*, int)> on_driver_memory_dump_internal_callback;
78void onSyncDriverMemoryDump(char* buffer, int buffer_size) {
79 if (on_driver_memory_dump_internal_callback) {
80 on_driver_memory_dump_internal_callback(buffer, buffer_size);
81 }
82}
83
84// Callback to be invoked for firmware dump.
85std::function<void(char*, int)> on_firmware_memory_dump_internal_callback;
86void onSyncFirmwareMemoryDump(char* buffer, int buffer_size) {
87 if (on_firmware_memory_dump_internal_callback) {
88 on_firmware_memory_dump_internal_callback(buffer, buffer_size);
89 }
90}
91
92// Callback to be invoked for Gscan events.
93std::function<void(wifi_request_id, wifi_scan_event)> on_gscan_event_internal_callback;
94void onAsyncGscanEvent(wifi_request_id id, wifi_scan_event event) {
95 const auto lock = aidl_sync_util::acquireGlobalLock();
96 if (on_gscan_event_internal_callback) {
97 on_gscan_event_internal_callback(id, event);
98 }
99}
100
101// Callback to be invoked for Gscan full results.
102std::function<void(wifi_request_id, wifi_scan_result*, uint32_t)>
103 on_gscan_full_result_internal_callback;
104void onAsyncGscanFullResult(wifi_request_id id, wifi_scan_result* result,
105 uint32_t buckets_scanned) {
106 const auto lock = aidl_sync_util::acquireGlobalLock();
107 if (on_gscan_full_result_internal_callback) {
108 on_gscan_full_result_internal_callback(id, result, buckets_scanned);
109 }
110}
111
112// Callback to be invoked for link layer stats results.
113std::function<void((wifi_request_id, wifi_iface_stat*, int, wifi_radio_stat*))>
114 on_link_layer_stats_result_internal_callback;
115void onSyncLinkLayerStatsResult(wifi_request_id id, wifi_iface_stat* iface_stat, int num_radios,
116 wifi_radio_stat* radio_stat) {
117 if (on_link_layer_stats_result_internal_callback) {
118 on_link_layer_stats_result_internal_callback(id, iface_stat, num_radios, radio_stat);
119 }
120}
121
122// Callback to be invoked for rssi threshold breach.
123std::function<void((wifi_request_id, uint8_t*, int8_t))>
124 on_rssi_threshold_breached_internal_callback;
125void onAsyncRssiThresholdBreached(wifi_request_id id, uint8_t* bssid, int8_t rssi) {
126 const auto lock = aidl_sync_util::acquireGlobalLock();
127 if (on_rssi_threshold_breached_internal_callback) {
128 on_rssi_threshold_breached_internal_callback(id, bssid, rssi);
129 }
130}
131
132// Callback to be invoked for ring buffer data indication.
133std::function<void(char*, char*, int, wifi_ring_buffer_status*)>
134 on_ring_buffer_data_internal_callback;
135void onAsyncRingBufferData(char* ring_name, char* buffer, int buffer_size,
136 wifi_ring_buffer_status* status) {
137 const auto lock = aidl_sync_util::acquireGlobalLock();
138 if (on_ring_buffer_data_internal_callback) {
139 on_ring_buffer_data_internal_callback(ring_name, buffer, buffer_size, status);
140 }
141}
142
143// Callback to be invoked for error alert indication.
144std::function<void(wifi_request_id, char*, int, int)> on_error_alert_internal_callback;
145void onAsyncErrorAlert(wifi_request_id id, char* buffer, int buffer_size, int err_code) {
146 const auto lock = aidl_sync_util::acquireGlobalLock();
147 if (on_error_alert_internal_callback) {
148 on_error_alert_internal_callback(id, buffer, buffer_size, err_code);
149 }
150}
151
152// Callback to be invoked for radio mode change indication.
153std::function<void(wifi_request_id, uint32_t, wifi_mac_info*)>
154 on_radio_mode_change_internal_callback;
155void onAsyncRadioModeChange(wifi_request_id id, uint32_t num_macs, wifi_mac_info* mac_infos) {
156 const auto lock = aidl_sync_util::acquireGlobalLock();
157 if (on_radio_mode_change_internal_callback) {
158 on_radio_mode_change_internal_callback(id, num_macs, mac_infos);
159 }
160}
161
162// Callback to be invoked to report subsystem restart
163std::function<void(const char*)> on_subsystem_restart_internal_callback;
164void onAsyncSubsystemRestart(const char* error) {
165 const auto lock = aidl_sync_util::acquireGlobalLock();
166 if (on_subsystem_restart_internal_callback) {
167 on_subsystem_restart_internal_callback(error);
168 }
169}
170
171// Callback to be invoked for rtt results results.
172std::function<void(wifi_request_id, unsigned num_results, wifi_rtt_result* rtt_results[])>
173 on_rtt_results_internal_callback;
Sunil Ravif8fc2372022-11-10 18:37:41 +0000174std::function<void(wifi_request_id, unsigned num_results, wifi_rtt_result_v2* rtt_results_v2[])>
175 on_rtt_results_internal_callback_v2;
176
177void invalidateRttResultsCallbacks() {
178 on_rtt_results_internal_callback = nullptr;
179 on_rtt_results_internal_callback_v2 = nullptr;
180};
181
Gabriel Birenf3262f92022-07-15 23:25:39 +0000182void onAsyncRttResults(wifi_request_id id, unsigned num_results, wifi_rtt_result* rtt_results[]) {
183 const auto lock = aidl_sync_util::acquireGlobalLock();
184 if (on_rtt_results_internal_callback) {
185 on_rtt_results_internal_callback(id, num_results, rtt_results);
Sunil Ravif8fc2372022-11-10 18:37:41 +0000186 invalidateRttResultsCallbacks();
187 }
188}
189
190void onAsyncRttResultsV2(wifi_request_id id, unsigned num_results,
191 wifi_rtt_result_v2* rtt_results_v2[]) {
192 const auto lock = aidl_sync_util::acquireGlobalLock();
193 if (on_rtt_results_internal_callback_v2) {
194 on_rtt_results_internal_callback_v2(id, num_results, rtt_results_v2);
195 invalidateRttResultsCallbacks();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000196 }
197}
198
199// Callbacks for the various NAN operations.
200// NOTE: These have very little conversions to perform before invoking the user
201// callbacks.
202// So, handle all of them here directly to avoid adding an unnecessary layer.
203std::function<void(transaction_id, const NanResponseMsg&)> on_nan_notify_response_user_callback;
204void onAsyncNanNotifyResponse(transaction_id id, NanResponseMsg* msg) {
205 const auto lock = aidl_sync_util::acquireGlobalLock();
206 if (on_nan_notify_response_user_callback && msg) {
207 on_nan_notify_response_user_callback(id, *msg);
208 }
209}
210
211std::function<void(const NanPublishRepliedInd&)> on_nan_event_publish_replied_user_callback;
212void onAsyncNanEventPublishReplied(NanPublishRepliedInd* /* event */) {
213 LOG(ERROR) << "onAsyncNanEventPublishReplied triggered";
214}
215
216std::function<void(const NanPublishTerminatedInd&)> on_nan_event_publish_terminated_user_callback;
217void onAsyncNanEventPublishTerminated(NanPublishTerminatedInd* event) {
218 const auto lock = aidl_sync_util::acquireGlobalLock();
219 if (on_nan_event_publish_terminated_user_callback && event) {
220 on_nan_event_publish_terminated_user_callback(*event);
221 }
222}
223
224std::function<void(const NanMatchInd&)> on_nan_event_match_user_callback;
225void onAsyncNanEventMatch(NanMatchInd* event) {
226 const auto lock = aidl_sync_util::acquireGlobalLock();
227 if (on_nan_event_match_user_callback && event) {
228 on_nan_event_match_user_callback(*event);
229 }
230}
231
232std::function<void(const NanMatchExpiredInd&)> on_nan_event_match_expired_user_callback;
233void onAsyncNanEventMatchExpired(NanMatchExpiredInd* event) {
234 const auto lock = aidl_sync_util::acquireGlobalLock();
235 if (on_nan_event_match_expired_user_callback && event) {
236 on_nan_event_match_expired_user_callback(*event);
237 }
238}
239
240std::function<void(const NanSubscribeTerminatedInd&)>
241 on_nan_event_subscribe_terminated_user_callback;
242void onAsyncNanEventSubscribeTerminated(NanSubscribeTerminatedInd* event) {
243 const auto lock = aidl_sync_util::acquireGlobalLock();
244 if (on_nan_event_subscribe_terminated_user_callback && event) {
245 on_nan_event_subscribe_terminated_user_callback(*event);
246 }
247}
248
249std::function<void(const NanFollowupInd&)> on_nan_event_followup_user_callback;
250void onAsyncNanEventFollowup(NanFollowupInd* event) {
251 const auto lock = aidl_sync_util::acquireGlobalLock();
252 if (on_nan_event_followup_user_callback && event) {
253 on_nan_event_followup_user_callback(*event);
254 }
255}
256
257std::function<void(const NanDiscEngEventInd&)> on_nan_event_disc_eng_event_user_callback;
258void onAsyncNanEventDiscEngEvent(NanDiscEngEventInd* event) {
259 const auto lock = aidl_sync_util::acquireGlobalLock();
260 if (on_nan_event_disc_eng_event_user_callback && event) {
261 on_nan_event_disc_eng_event_user_callback(*event);
262 }
263}
264
265std::function<void(const NanDisabledInd&)> on_nan_event_disabled_user_callback;
266void onAsyncNanEventDisabled(NanDisabledInd* event) {
267 const auto lock = aidl_sync_util::acquireGlobalLock();
268 if (on_nan_event_disabled_user_callback && event) {
269 on_nan_event_disabled_user_callback(*event);
270 }
271}
272
273std::function<void(const NanTCAInd&)> on_nan_event_tca_user_callback;
274void onAsyncNanEventTca(NanTCAInd* event) {
275 const auto lock = aidl_sync_util::acquireGlobalLock();
276 if (on_nan_event_tca_user_callback && event) {
277 on_nan_event_tca_user_callback(*event);
278 }
279}
280
281std::function<void(const NanBeaconSdfPayloadInd&)> on_nan_event_beacon_sdf_payload_user_callback;
282void onAsyncNanEventBeaconSdfPayload(NanBeaconSdfPayloadInd* event) {
283 const auto lock = aidl_sync_util::acquireGlobalLock();
284 if (on_nan_event_beacon_sdf_payload_user_callback && event) {
285 on_nan_event_beacon_sdf_payload_user_callback(*event);
286 }
287}
288
289std::function<void(const NanDataPathRequestInd&)> on_nan_event_data_path_request_user_callback;
290void onAsyncNanEventDataPathRequest(NanDataPathRequestInd* event) {
291 const auto lock = aidl_sync_util::acquireGlobalLock();
292 if (on_nan_event_data_path_request_user_callback && event) {
293 on_nan_event_data_path_request_user_callback(*event);
294 }
295}
296std::function<void(const NanDataPathConfirmInd&)> on_nan_event_data_path_confirm_user_callback;
297void onAsyncNanEventDataPathConfirm(NanDataPathConfirmInd* event) {
298 const auto lock = aidl_sync_util::acquireGlobalLock();
299 if (on_nan_event_data_path_confirm_user_callback && event) {
300 on_nan_event_data_path_confirm_user_callback(*event);
301 }
302}
303
304std::function<void(const NanDataPathEndInd&)> on_nan_event_data_path_end_user_callback;
305void onAsyncNanEventDataPathEnd(NanDataPathEndInd* event) {
306 const auto lock = aidl_sync_util::acquireGlobalLock();
307 if (on_nan_event_data_path_end_user_callback && event) {
308 on_nan_event_data_path_end_user_callback(*event);
309 }
310}
311
312std::function<void(const NanTransmitFollowupInd&)> on_nan_event_transmit_follow_up_user_callback;
313void onAsyncNanEventTransmitFollowUp(NanTransmitFollowupInd* event) {
314 const auto lock = aidl_sync_util::acquireGlobalLock();
315 if (on_nan_event_transmit_follow_up_user_callback && event) {
316 on_nan_event_transmit_follow_up_user_callback(*event);
317 }
318}
319
320std::function<void(const NanRangeRequestInd&)> on_nan_event_range_request_user_callback;
321void onAsyncNanEventRangeRequest(NanRangeRequestInd* event) {
322 const auto lock = aidl_sync_util::acquireGlobalLock();
323 if (on_nan_event_range_request_user_callback && event) {
324 on_nan_event_range_request_user_callback(*event);
325 }
326}
327
328std::function<void(const NanRangeReportInd&)> on_nan_event_range_report_user_callback;
329void onAsyncNanEventRangeReport(NanRangeReportInd* event) {
330 const auto lock = aidl_sync_util::acquireGlobalLock();
331 if (on_nan_event_range_report_user_callback && event) {
332 on_nan_event_range_report_user_callback(*event);
333 }
334}
335
336std::function<void(const NanDataPathScheduleUpdateInd&)> on_nan_event_schedule_update_user_callback;
337void onAsyncNanEventScheduleUpdate(NanDataPathScheduleUpdateInd* event) {
338 const auto lock = aidl_sync_util::acquireGlobalLock();
339 if (on_nan_event_schedule_update_user_callback && event) {
340 on_nan_event_schedule_update_user_callback(*event);
341 }
342}
343
344// Callbacks for the various TWT operations.
345std::function<void(const TwtSetupResponse&)> on_twt_event_setup_response_callback;
346void onAsyncTwtEventSetupResponse(TwtSetupResponse* event) {
347 const auto lock = aidl_sync_util::acquireGlobalLock();
348 if (on_twt_event_setup_response_callback && event) {
349 on_twt_event_setup_response_callback(*event);
350 }
351}
352
353std::function<void(const TwtTeardownCompletion&)> on_twt_event_teardown_completion_callback;
354void onAsyncTwtEventTeardownCompletion(TwtTeardownCompletion* event) {
355 const auto lock = aidl_sync_util::acquireGlobalLock();
356 if (on_twt_event_teardown_completion_callback && event) {
357 on_twt_event_teardown_completion_callback(*event);
358 }
359}
360
361std::function<void(const TwtInfoFrameReceived&)> on_twt_event_info_frame_received_callback;
362void onAsyncTwtEventInfoFrameReceived(TwtInfoFrameReceived* event) {
363 const auto lock = aidl_sync_util::acquireGlobalLock();
364 if (on_twt_event_info_frame_received_callback && event) {
365 on_twt_event_info_frame_received_callback(*event);
366 }
367}
368
369std::function<void(const TwtDeviceNotify&)> on_twt_event_device_notify_callback;
370void onAsyncTwtEventDeviceNotify(TwtDeviceNotify* event) {
371 const auto lock = aidl_sync_util::acquireGlobalLock();
372 if (on_twt_event_device_notify_callback && event) {
373 on_twt_event_device_notify_callback(*event);
374 }
375}
376
377// Callback to report current CHRE NAN state
378std::function<void(chre_nan_rtt_state)> on_chre_nan_rtt_internal_callback;
379void onAsyncChreNanRttState(chre_nan_rtt_state state) {
380 const auto lock = aidl_sync_util::acquireGlobalLock();
381 if (on_chre_nan_rtt_internal_callback) {
382 on_chre_nan_rtt_internal_callback(state);
383 }
384}
385
386// Callback to report cached scan results
387std::function<void(wifi_cached_scan_report*)> on_cached_scan_results_internal_callback;
388void onSyncCachedScanResults(wifi_cached_scan_report* cache_report) {
389 if (on_cached_scan_results_internal_callback) {
390 on_cached_scan_results_internal_callback(cache_report);
391 }
392}
393
394// End of the free-standing "C" style callbacks.
395
396WifiLegacyHal::WifiLegacyHal(const std::weak_ptr<::android::wifi_system::InterfaceTool> iface_tool,
397 const wifi_hal_fn& fn, bool is_primary)
398 : global_func_table_(fn),
399 global_handle_(nullptr),
400 awaiting_event_loop_termination_(false),
401 is_started_(false),
402 iface_tool_(iface_tool),
403 is_primary_(is_primary) {}
404
405wifi_error WifiLegacyHal::initialize() {
406 LOG(DEBUG) << "Initialize legacy HAL";
407 // this now does nothing, since HAL function table is provided
408 // to the constructor
409 return WIFI_SUCCESS;
410}
411
412wifi_error WifiLegacyHal::start() {
413 // Ensure that we're starting in a good state.
414 CHECK(global_func_table_.wifi_initialize && !global_handle_ && iface_name_to_handle_.empty() &&
415 !awaiting_event_loop_termination_);
416 if (is_started_) {
417 LOG(DEBUG) << "Legacy HAL already started";
418 return WIFI_SUCCESS;
419 }
420 LOG(DEBUG) << "Waiting for the driver ready";
421 wifi_error status = global_func_table_.wifi_wait_for_driver_ready();
422 if (status == WIFI_ERROR_TIMED_OUT || status == WIFI_ERROR_UNKNOWN) {
423 LOG(ERROR) << "Failed or timed out awaiting driver ready";
424 return status;
425 }
426
427 if (is_primary_) {
428 property_set(kDriverPropName, "ok");
429
430 if (!iface_tool_.lock()->SetWifiUpState(true)) {
431 LOG(ERROR) << "Failed to set WiFi interface up";
432 return WIFI_ERROR_UNKNOWN;
433 }
434 }
435
436 LOG(DEBUG) << "Starting legacy HAL";
437 status = global_func_table_.wifi_initialize(&global_handle_);
438 if (status != WIFI_SUCCESS || !global_handle_) {
439 LOG(ERROR) << "Failed to retrieve global handle";
440 return status;
441 }
442 std::thread(&WifiLegacyHal::runEventLoop, this).detach();
443 status = retrieveIfaceHandles();
444 if (status != WIFI_SUCCESS || iface_name_to_handle_.empty()) {
445 LOG(ERROR) << "Failed to retrieve wlan interface handle";
446 return status;
447 }
448 LOG(DEBUG) << "Legacy HAL start complete";
449 is_started_ = true;
450 return WIFI_SUCCESS;
451}
452
453wifi_error WifiLegacyHal::stop(
454 /* NONNULL */ std::unique_lock<std::recursive_mutex>* lock,
455 const std::function<void()>& on_stop_complete_user_callback) {
456 if (!is_started_) {
457 LOG(DEBUG) << "Legacy HAL already stopped";
458 on_stop_complete_user_callback();
459 return WIFI_SUCCESS;
460 }
461 LOG(DEBUG) << "Stopping legacy HAL";
462 on_stop_complete_internal_callback = [on_stop_complete_user_callback,
463 this](wifi_handle handle) {
464 CHECK_EQ(global_handle_, handle) << "Handle mismatch";
465 LOG(INFO) << "Legacy HAL stop complete callback received";
466 // Invalidate all the internal pointers now that the HAL is
467 // stopped.
468 invalidate();
469 if (is_primary_) iface_tool_.lock()->SetWifiUpState(false);
470 on_stop_complete_user_callback();
471 is_started_ = false;
472 };
473 awaiting_event_loop_termination_ = true;
474 global_func_table_.wifi_cleanup(global_handle_, onAsyncStopComplete);
475 const auto status =
476 stop_wait_cv_.wait_for(*lock, std::chrono::milliseconds(kMaxStopCompleteWaitMs),
477 [this] { return !awaiting_event_loop_termination_; });
478 if (!status) {
479 LOG(ERROR) << "Legacy HAL stop failed or timed out";
480 return WIFI_ERROR_UNKNOWN;
481 }
482 LOG(DEBUG) << "Legacy HAL stop complete";
483 return WIFI_SUCCESS;
484}
485
486bool WifiLegacyHal::isStarted() {
487 return is_started_;
488}
489
490wifi_error WifiLegacyHal::waitForDriverReady() {
491 return global_func_table_.wifi_wait_for_driver_ready();
492}
493
494std::pair<wifi_error, std::string> WifiLegacyHal::getDriverVersion(const std::string& iface_name) {
495 std::array<char, kMaxVersionStringLength> buffer;
496 buffer.fill(0);
497 wifi_error status = global_func_table_.wifi_get_driver_version(getIfaceHandle(iface_name),
498 buffer.data(), buffer.size());
499 return {status, buffer.data()};
500}
501
502std::pair<wifi_error, std::string> WifiLegacyHal::getFirmwareVersion(
503 const std::string& iface_name) {
504 std::array<char, kMaxVersionStringLength> buffer;
505 buffer.fill(0);
506 wifi_error status = global_func_table_.wifi_get_firmware_version(getIfaceHandle(iface_name),
507 buffer.data(), buffer.size());
508 return {status, buffer.data()};
509}
510
511std::pair<wifi_error, std::vector<uint8_t>> WifiLegacyHal::requestDriverMemoryDump(
512 const std::string& iface_name) {
513 std::vector<uint8_t> driver_dump;
514 on_driver_memory_dump_internal_callback = [&driver_dump](char* buffer, int buffer_size) {
515 driver_dump.insert(driver_dump.end(), reinterpret_cast<uint8_t*>(buffer),
516 reinterpret_cast<uint8_t*>(buffer) + buffer_size);
517 };
518 wifi_error status = global_func_table_.wifi_get_driver_memory_dump(getIfaceHandle(iface_name),
519 {onSyncDriverMemoryDump});
520 on_driver_memory_dump_internal_callback = nullptr;
521 return {status, std::move(driver_dump)};
522}
523
524std::pair<wifi_error, std::vector<uint8_t>> WifiLegacyHal::requestFirmwareMemoryDump(
525 const std::string& iface_name) {
526 std::vector<uint8_t> firmware_dump;
527 on_firmware_memory_dump_internal_callback = [&firmware_dump](char* buffer, int buffer_size) {
528 firmware_dump.insert(firmware_dump.end(), reinterpret_cast<uint8_t*>(buffer),
529 reinterpret_cast<uint8_t*>(buffer) + buffer_size);
530 };
531 wifi_error status = global_func_table_.wifi_get_firmware_memory_dump(
532 getIfaceHandle(iface_name), {onSyncFirmwareMemoryDump});
533 on_firmware_memory_dump_internal_callback = nullptr;
534 return {status, std::move(firmware_dump)};
535}
536
537std::pair<wifi_error, uint64_t> WifiLegacyHal::getSupportedFeatureSet(
538 const std::string& iface_name) {
539 feature_set set = 0, chip_set = 0;
540 wifi_error status = WIFI_SUCCESS;
541
542 static_assert(sizeof(set) == sizeof(uint64_t),
543 "Some feature_flags can not be represented in output");
544 wifi_interface_handle iface_handle = getIfaceHandle(iface_name);
545
546 global_func_table_.wifi_get_chip_feature_set(
547 global_handle_, &chip_set); /* ignore error, chip_set will stay 0 */
548
549 if (iface_handle) {
550 status = global_func_table_.wifi_get_supported_feature_set(iface_handle, &set);
551 }
552 return {status, static_cast<uint64_t>(set | chip_set)};
553}
554
555std::pair<wifi_error, PacketFilterCapabilities> WifiLegacyHal::getPacketFilterCapabilities(
556 const std::string& iface_name) {
557 PacketFilterCapabilities caps;
558 wifi_error status = global_func_table_.wifi_get_packet_filter_capabilities(
559 getIfaceHandle(iface_name), &caps.version, &caps.max_len);
560 return {status, caps};
561}
562
563wifi_error WifiLegacyHal::setPacketFilter(const std::string& iface_name,
564 const std::vector<uint8_t>& program) {
565 return global_func_table_.wifi_set_packet_filter(getIfaceHandle(iface_name), program.data(),
566 program.size());
567}
568
569std::pair<wifi_error, std::vector<uint8_t>> WifiLegacyHal::readApfPacketFilterData(
570 const std::string& iface_name) {
571 PacketFilterCapabilities caps;
572 wifi_error status = global_func_table_.wifi_get_packet_filter_capabilities(
573 getIfaceHandle(iface_name), &caps.version, &caps.max_len);
574 if (status != WIFI_SUCCESS) {
575 return {status, {}};
576 }
577
578 // Size the buffer to read the entire program & work memory.
579 std::vector<uint8_t> buffer(caps.max_len);
580
581 status = global_func_table_.wifi_read_packet_filter(
582 getIfaceHandle(iface_name), /*src_offset=*/0, buffer.data(), buffer.size());
583 return {status, std::move(buffer)};
584}
585
586std::pair<wifi_error, wifi_gscan_capabilities> WifiLegacyHal::getGscanCapabilities(
587 const std::string& iface_name) {
588 wifi_gscan_capabilities caps;
589 wifi_error status =
590 global_func_table_.wifi_get_gscan_capabilities(getIfaceHandle(iface_name), &caps);
591 return {status, caps};
592}
593
594wifi_error WifiLegacyHal::startGscan(
595 const std::string& iface_name, wifi_request_id id, const wifi_scan_cmd_params& params,
596 const std::function<void(wifi_request_id)>& on_failure_user_callback,
597 const on_gscan_results_callback& on_results_user_callback,
598 const on_gscan_full_result_callback& on_full_result_user_callback) {
599 // If there is already an ongoing background scan, reject new scan requests.
600 if (on_gscan_event_internal_callback || on_gscan_full_result_internal_callback) {
601 return WIFI_ERROR_NOT_AVAILABLE;
602 }
603
604 // This callback will be used to either trigger |on_results_user_callback|
605 // or |on_failure_user_callback|.
606 on_gscan_event_internal_callback = [iface_name, on_failure_user_callback,
607 on_results_user_callback,
608 this](wifi_request_id id, wifi_scan_event event) {
609 switch (event) {
610 case WIFI_SCAN_RESULTS_AVAILABLE:
611 case WIFI_SCAN_THRESHOLD_NUM_SCANS:
612 case WIFI_SCAN_THRESHOLD_PERCENT: {
613 wifi_error status;
614 std::vector<wifi_cached_scan_results> cached_scan_results;
615 std::tie(status, cached_scan_results) = getGscanCachedResults(iface_name);
616 if (status == WIFI_SUCCESS) {
617 on_results_user_callback(id, cached_scan_results);
618 return;
619 }
620 FALLTHROUGH_INTENDED;
621 }
622 // Fall through if failed. Failure to retrieve cached scan
623 // results should trigger a background scan failure.
624 case WIFI_SCAN_FAILED:
625 on_failure_user_callback(id);
626 on_gscan_event_internal_callback = nullptr;
627 on_gscan_full_result_internal_callback = nullptr;
628 return;
629 }
630 LOG(FATAL) << "Unexpected gscan event received: " << event;
631 };
632
633 on_gscan_full_result_internal_callback = [on_full_result_user_callback](
634 wifi_request_id id, wifi_scan_result* result,
635 uint32_t buckets_scanned) {
636 if (result) {
637 on_full_result_user_callback(id, result, buckets_scanned);
638 }
639 };
640
641 wifi_scan_result_handler handler = {onAsyncGscanFullResult, onAsyncGscanEvent};
642 wifi_error status =
643 global_func_table_.wifi_start_gscan(id, getIfaceHandle(iface_name), params, handler);
644 if (status != WIFI_SUCCESS) {
645 on_gscan_event_internal_callback = nullptr;
646 on_gscan_full_result_internal_callback = nullptr;
647 }
648 return status;
649}
650
651wifi_error WifiLegacyHal::stopGscan(const std::string& iface_name, wifi_request_id id) {
652 // If there is no an ongoing background scan, reject stop requests.
653 // TODO(b/32337212): This needs to be handled by the HIDL object because we
654 // need to return the NOT_STARTED error code.
655 if (!on_gscan_event_internal_callback && !on_gscan_full_result_internal_callback) {
656 return WIFI_ERROR_NOT_AVAILABLE;
657 }
658 wifi_error status = global_func_table_.wifi_stop_gscan(id, getIfaceHandle(iface_name));
659 // If the request Id is wrong, don't stop the ongoing background scan. Any
660 // other error should be treated as the end of background scan.
661 if (status != WIFI_ERROR_INVALID_REQUEST_ID) {
662 on_gscan_event_internal_callback = nullptr;
663 on_gscan_full_result_internal_callback = nullptr;
664 }
665 return status;
666}
667
668std::pair<wifi_error, std::vector<uint32_t>> WifiLegacyHal::getValidFrequenciesForBand(
669 const std::string& iface_name, wifi_band band) {
670 static_assert(sizeof(uint32_t) >= sizeof(wifi_channel),
671 "Wifi Channel cannot be represented in output");
672 std::vector<uint32_t> freqs;
673 freqs.resize(kMaxGscanFrequenciesForBand);
674 int32_t num_freqs = 0;
675 wifi_error status = global_func_table_.wifi_get_valid_channels(
676 getIfaceHandle(iface_name), band, freqs.size(),
677 reinterpret_cast<wifi_channel*>(freqs.data()), &num_freqs);
678 CHECK(num_freqs >= 0 && static_cast<uint32_t>(num_freqs) <= kMaxGscanFrequenciesForBand);
679 freqs.resize(num_freqs);
680 return {status, std::move(freqs)};
681}
682
683wifi_error WifiLegacyHal::setDfsFlag(const std::string& iface_name, bool dfs_on) {
684 return global_func_table_.wifi_set_nodfs_flag(getIfaceHandle(iface_name), dfs_on ? 0 : 1);
685}
686
687wifi_error WifiLegacyHal::enableLinkLayerStats(const std::string& iface_name, bool debug) {
688 wifi_link_layer_params params;
689 params.mpdu_size_threshold = kLinkLayerStatsDataMpduSizeThreshold;
690 params.aggressive_statistics_gathering = debug;
691 return global_func_table_.wifi_set_link_stats(getIfaceHandle(iface_name), params);
692}
693
694wifi_error WifiLegacyHal::disableLinkLayerStats(const std::string& iface_name) {
695 // TODO: Do we care about these responses?
696 uint32_t clear_mask_rsp;
697 uint8_t stop_rsp;
698 return global_func_table_.wifi_clear_link_stats(getIfaceHandle(iface_name), 0xFFFFFFFF,
699 &clear_mask_rsp, 1, &stop_rsp);
700}
701
702std::pair<wifi_error, LinkLayerStats> WifiLegacyHal::getLinkLayerStats(
703 const std::string& iface_name) {
704 LinkLayerStats link_stats{};
705 LinkLayerStats* link_stats_ptr = &link_stats;
706
707 on_link_layer_stats_result_internal_callback = [&link_stats_ptr](
708 wifi_request_id /* id */,
709 wifi_iface_stat* iface_stats_ptr,
710 int num_radios,
711 wifi_radio_stat* radio_stats_ptr) {
712 wifi_radio_stat* l_radio_stats_ptr;
713 wifi_peer_info* l_peer_info_stats_ptr;
714
715 if (iface_stats_ptr != nullptr) {
716 link_stats_ptr->iface = *iface_stats_ptr;
717 l_peer_info_stats_ptr = iface_stats_ptr->peer_info;
718 for (uint32_t i = 0; i < iface_stats_ptr->num_peers; i++) {
719 WifiPeerInfo peer;
720 peer.peer_info = *l_peer_info_stats_ptr;
721 if (l_peer_info_stats_ptr->num_rate > 0) {
722 /* Copy the rate stats */
723 peer.rate_stats.assign(
724 l_peer_info_stats_ptr->rate_stats,
725 l_peer_info_stats_ptr->rate_stats + l_peer_info_stats_ptr->num_rate);
726 }
727 peer.peer_info.num_rate = 0;
728 link_stats_ptr->peers.push_back(peer);
729 l_peer_info_stats_ptr =
730 (wifi_peer_info*)((u8*)l_peer_info_stats_ptr + sizeof(wifi_peer_info) +
731 (sizeof(wifi_rate_stat) *
732 l_peer_info_stats_ptr->num_rate));
733 }
734 link_stats_ptr->iface.num_peers = 0;
735 } else {
736 LOG(ERROR) << "Invalid iface stats in link layer stats";
737 }
738 if (num_radios <= 0 || radio_stats_ptr == nullptr) {
739 LOG(ERROR) << "Invalid radio stats in link layer stats";
740 return;
741 }
742 l_radio_stats_ptr = radio_stats_ptr;
743 for (int i = 0; i < num_radios; i++) {
744 LinkLayerRadioStats radio;
745
746 radio.stats = *l_radio_stats_ptr;
747 // Copy over the tx level array to the separate vector.
748 if (l_radio_stats_ptr->num_tx_levels > 0 &&
749 l_radio_stats_ptr->tx_time_per_levels != nullptr) {
750 radio.tx_time_per_levels.assign(
751 l_radio_stats_ptr->tx_time_per_levels,
752 l_radio_stats_ptr->tx_time_per_levels + l_radio_stats_ptr->num_tx_levels);
753 }
754 radio.stats.num_tx_levels = 0;
755 radio.stats.tx_time_per_levels = nullptr;
756 /* Copy over the channel stat to separate vector */
757 if (l_radio_stats_ptr->num_channels > 0) {
758 /* Copy the channel stats */
759 radio.channel_stats.assign(
760 l_radio_stats_ptr->channels,
761 l_radio_stats_ptr->channels + l_radio_stats_ptr->num_channels);
762 }
763 link_stats_ptr->radios.push_back(radio);
764 l_radio_stats_ptr =
765 (wifi_radio_stat*)((u8*)l_radio_stats_ptr + sizeof(wifi_radio_stat) +
766 (sizeof(wifi_channel_stat) *
767 l_radio_stats_ptr->num_channels));
768 }
769 };
770
771 wifi_error status = global_func_table_.wifi_get_link_stats(0, getIfaceHandle(iface_name),
772 {onSyncLinkLayerStatsResult});
773 on_link_layer_stats_result_internal_callback = nullptr;
774 return {status, link_stats};
775}
776
777wifi_error WifiLegacyHal::startRssiMonitoring(
778 const std::string& iface_name, wifi_request_id id, int8_t max_rssi, int8_t min_rssi,
779 const on_rssi_threshold_breached_callback& on_threshold_breached_user_callback) {
780 if (on_rssi_threshold_breached_internal_callback) {
781 return WIFI_ERROR_NOT_AVAILABLE;
782 }
783 on_rssi_threshold_breached_internal_callback = [on_threshold_breached_user_callback](
784 wifi_request_id id, uint8_t* bssid_ptr,
785 int8_t rssi) {
786 if (!bssid_ptr) {
787 return;
788 }
789 std::array<uint8_t, ETH_ALEN> bssid_arr;
790 // |bssid_ptr| pointer is assumed to have 6 bytes for the mac
791 // address.
792 std::copy(bssid_ptr, bssid_ptr + 6, std::begin(bssid_arr));
793 on_threshold_breached_user_callback(id, bssid_arr, rssi);
794 };
795 wifi_error status = global_func_table_.wifi_start_rssi_monitoring(
796 id, getIfaceHandle(iface_name), max_rssi, min_rssi, {onAsyncRssiThresholdBreached});
797 if (status != WIFI_SUCCESS) {
798 on_rssi_threshold_breached_internal_callback = nullptr;
799 }
800 return status;
801}
802
803wifi_error WifiLegacyHal::stopRssiMonitoring(const std::string& iface_name, wifi_request_id id) {
804 if (!on_rssi_threshold_breached_internal_callback) {
805 return WIFI_ERROR_NOT_AVAILABLE;
806 }
807 wifi_error status =
808 global_func_table_.wifi_stop_rssi_monitoring(id, getIfaceHandle(iface_name));
809 // If the request Id is wrong, don't stop the ongoing rssi monitoring. Any
810 // other error should be treated as the end of background scan.
811 if (status != WIFI_ERROR_INVALID_REQUEST_ID) {
812 on_rssi_threshold_breached_internal_callback = nullptr;
813 }
814 return status;
815}
816
817std::pair<wifi_error, wifi_roaming_capabilities> WifiLegacyHal::getRoamingCapabilities(
818 const std::string& iface_name) {
819 wifi_roaming_capabilities caps;
820 wifi_error status =
821 global_func_table_.wifi_get_roaming_capabilities(getIfaceHandle(iface_name), &caps);
822 return {status, caps};
823}
824
825wifi_error WifiLegacyHal::configureRoaming(const std::string& iface_name,
826 const wifi_roaming_config& config) {
827 wifi_roaming_config config_internal = config;
828 return global_func_table_.wifi_configure_roaming(getIfaceHandle(iface_name), &config_internal);
829}
830
831wifi_error WifiLegacyHal::enableFirmwareRoaming(const std::string& iface_name,
832 fw_roaming_state_t state) {
833 return global_func_table_.wifi_enable_firmware_roaming(getIfaceHandle(iface_name), state);
834}
835
836wifi_error WifiLegacyHal::configureNdOffload(const std::string& iface_name, bool enable) {
837 return global_func_table_.wifi_configure_nd_offload(getIfaceHandle(iface_name), enable);
838}
839
840wifi_error WifiLegacyHal::startSendingOffloadedPacket(const std::string& iface_name, int32_t cmd_id,
841 uint16_t ether_type,
842 const std::vector<uint8_t>& ip_packet_data,
843 const std::array<uint8_t, 6>& src_address,
844 const std::array<uint8_t, 6>& dst_address,
845 int32_t period_in_ms) {
846 std::vector<uint8_t> ip_packet_data_internal(ip_packet_data);
847 std::vector<uint8_t> src_address_internal(src_address.data(),
848 src_address.data() + src_address.size());
849 std::vector<uint8_t> dst_address_internal(dst_address.data(),
850 dst_address.data() + dst_address.size());
851 return global_func_table_.wifi_start_sending_offloaded_packet(
852 cmd_id, getIfaceHandle(iface_name), ether_type, ip_packet_data_internal.data(),
853 ip_packet_data_internal.size(), src_address_internal.data(),
854 dst_address_internal.data(), period_in_ms);
855}
856
857wifi_error WifiLegacyHal::stopSendingOffloadedPacket(const std::string& iface_name,
858 uint32_t cmd_id) {
859 return global_func_table_.wifi_stop_sending_offloaded_packet(cmd_id,
860 getIfaceHandle(iface_name));
861}
862
863wifi_error WifiLegacyHal::selectTxPowerScenario(const std::string& iface_name,
864 wifi_power_scenario scenario) {
865 return global_func_table_.wifi_select_tx_power_scenario(getIfaceHandle(iface_name), scenario);
866}
867
868wifi_error WifiLegacyHal::resetTxPowerScenario(const std::string& iface_name) {
869 return global_func_table_.wifi_reset_tx_power_scenario(getIfaceHandle(iface_name));
870}
871
872wifi_error WifiLegacyHal::setLatencyMode(const std::string& iface_name, wifi_latency_mode mode) {
873 return global_func_table_.wifi_set_latency_mode(getIfaceHandle(iface_name), mode);
874}
875
876wifi_error WifiLegacyHal::setThermalMitigationMode(wifi_thermal_mode mode,
877 uint32_t completion_window) {
878 return global_func_table_.wifi_set_thermal_mitigation_mode(global_handle_, mode,
879 completion_window);
880}
881
882wifi_error WifiLegacyHal::setDscpToAccessCategoryMapping(uint32_t start, uint32_t end,
883 uint32_t access_category) {
884 return global_func_table_.wifi_map_dscp_access_category(global_handle_, start, end,
885 access_category);
886}
887
888wifi_error WifiLegacyHal::resetDscpToAccessCategoryMapping() {
889 return global_func_table_.wifi_reset_dscp_mapping(global_handle_);
890}
891
892std::pair<wifi_error, uint32_t> WifiLegacyHal::getLoggerSupportedFeatureSet(
893 const std::string& iface_name) {
894 uint32_t supported_feature_flags = 0;
895 wifi_error status = WIFI_SUCCESS;
896
897 wifi_interface_handle iface_handle = getIfaceHandle(iface_name);
898
899 if (iface_handle) {
900 status = global_func_table_.wifi_get_logger_supported_feature_set(iface_handle,
901 &supported_feature_flags);
902 }
903 return {status, supported_feature_flags};
904}
905
906wifi_error WifiLegacyHal::startPktFateMonitoring(const std::string& iface_name) {
907 return global_func_table_.wifi_start_pkt_fate_monitoring(getIfaceHandle(iface_name));
908}
909
910std::pair<wifi_error, std::vector<wifi_tx_report>> WifiLegacyHal::getTxPktFates(
911 const std::string& iface_name) {
912 std::vector<wifi_tx_report> tx_pkt_fates;
913 tx_pkt_fates.resize(MAX_FATE_LOG_LEN);
914 size_t num_fates = 0;
915 wifi_error status = global_func_table_.wifi_get_tx_pkt_fates(
916 getIfaceHandle(iface_name), tx_pkt_fates.data(), tx_pkt_fates.size(), &num_fates);
917 CHECK(num_fates <= MAX_FATE_LOG_LEN);
918 tx_pkt_fates.resize(num_fates);
919 return {status, std::move(tx_pkt_fates)};
920}
921
922std::pair<wifi_error, std::vector<wifi_rx_report>> WifiLegacyHal::getRxPktFates(
923 const std::string& iface_name) {
924 std::vector<wifi_rx_report> rx_pkt_fates;
925 rx_pkt_fates.resize(MAX_FATE_LOG_LEN);
926 size_t num_fates = 0;
927 wifi_error status = global_func_table_.wifi_get_rx_pkt_fates(
928 getIfaceHandle(iface_name), rx_pkt_fates.data(), rx_pkt_fates.size(), &num_fates);
929 CHECK(num_fates <= MAX_FATE_LOG_LEN);
930 rx_pkt_fates.resize(num_fates);
931 return {status, std::move(rx_pkt_fates)};
932}
933
934std::pair<wifi_error, WakeReasonStats> WifiLegacyHal::getWakeReasonStats(
935 const std::string& iface_name) {
936 WakeReasonStats stats;
937 stats.cmd_event_wake_cnt.resize(kMaxWakeReasonStatsArraySize);
938 stats.driver_fw_local_wake_cnt.resize(kMaxWakeReasonStatsArraySize);
939
940 // This legacy struct needs separate memory to store the variable sized wake
941 // reason types.
942 stats.wake_reason_cnt.cmd_event_wake_cnt =
943 reinterpret_cast<int32_t*>(stats.cmd_event_wake_cnt.data());
944 stats.wake_reason_cnt.cmd_event_wake_cnt_sz = stats.cmd_event_wake_cnt.size();
945 stats.wake_reason_cnt.cmd_event_wake_cnt_used = 0;
946 stats.wake_reason_cnt.driver_fw_local_wake_cnt =
947 reinterpret_cast<int32_t*>(stats.driver_fw_local_wake_cnt.data());
948 stats.wake_reason_cnt.driver_fw_local_wake_cnt_sz = stats.driver_fw_local_wake_cnt.size();
949 stats.wake_reason_cnt.driver_fw_local_wake_cnt_used = 0;
950
951 wifi_error status = global_func_table_.wifi_get_wake_reason_stats(getIfaceHandle(iface_name),
952 &stats.wake_reason_cnt);
953
954 CHECK(stats.wake_reason_cnt.cmd_event_wake_cnt_used >= 0 &&
955 static_cast<uint32_t>(stats.wake_reason_cnt.cmd_event_wake_cnt_used) <=
956 kMaxWakeReasonStatsArraySize);
957 stats.cmd_event_wake_cnt.resize(stats.wake_reason_cnt.cmd_event_wake_cnt_used);
958 stats.wake_reason_cnt.cmd_event_wake_cnt = nullptr;
959
960 CHECK(stats.wake_reason_cnt.driver_fw_local_wake_cnt_used >= 0 &&
961 static_cast<uint32_t>(stats.wake_reason_cnt.driver_fw_local_wake_cnt_used) <=
962 kMaxWakeReasonStatsArraySize);
963 stats.driver_fw_local_wake_cnt.resize(stats.wake_reason_cnt.driver_fw_local_wake_cnt_used);
964 stats.wake_reason_cnt.driver_fw_local_wake_cnt = nullptr;
965
966 return {status, stats};
967}
968
969wifi_error WifiLegacyHal::registerRingBufferCallbackHandler(
970 const std::string& iface_name, const on_ring_buffer_data_callback& on_user_data_callback) {
971 if (on_ring_buffer_data_internal_callback) {
972 return WIFI_ERROR_NOT_AVAILABLE;
973 }
974 on_ring_buffer_data_internal_callback = [on_user_data_callback](
975 char* ring_name, char* buffer, int buffer_size,
976 wifi_ring_buffer_status* status) {
977 if (status && buffer) {
978 std::vector<uint8_t> buffer_vector(reinterpret_cast<uint8_t*>(buffer),
979 reinterpret_cast<uint8_t*>(buffer) + buffer_size);
980 on_user_data_callback(ring_name, buffer_vector, *status);
981 }
982 };
983 wifi_error status = global_func_table_.wifi_set_log_handler(0, getIfaceHandle(iface_name),
984 {onAsyncRingBufferData});
985 if (status != WIFI_SUCCESS) {
986 on_ring_buffer_data_internal_callback = nullptr;
987 }
988 return status;
989}
990
991wifi_error WifiLegacyHal::deregisterRingBufferCallbackHandler(const std::string& iface_name) {
992 if (!on_ring_buffer_data_internal_callback) {
993 return WIFI_ERROR_NOT_AVAILABLE;
994 }
995 on_ring_buffer_data_internal_callback = nullptr;
996 return global_func_table_.wifi_reset_log_handler(0, getIfaceHandle(iface_name));
997}
998
999std::pair<wifi_error, std::vector<wifi_ring_buffer_status>> WifiLegacyHal::getRingBuffersStatus(
1000 const std::string& iface_name) {
1001 std::vector<wifi_ring_buffer_status> ring_buffers_status;
1002 ring_buffers_status.resize(kMaxRingBuffers);
1003 uint32_t num_rings = kMaxRingBuffers;
1004 wifi_error status = global_func_table_.wifi_get_ring_buffers_status(
1005 getIfaceHandle(iface_name), &num_rings, ring_buffers_status.data());
1006 CHECK(num_rings <= kMaxRingBuffers);
1007 ring_buffers_status.resize(num_rings);
1008 return {status, std::move(ring_buffers_status)};
1009}
1010
1011wifi_error WifiLegacyHal::startRingBufferLogging(const std::string& iface_name,
1012 const std::string& ring_name,
1013 uint32_t verbose_level, uint32_t max_interval_sec,
1014 uint32_t min_data_size) {
1015 return global_func_table_.wifi_start_logging(getIfaceHandle(iface_name), verbose_level, 0,
1016 max_interval_sec, min_data_size,
1017 makeCharVec(ring_name).data());
1018}
1019
1020wifi_error WifiLegacyHal::getRingBufferData(const std::string& iface_name,
1021 const std::string& ring_name) {
1022 return global_func_table_.wifi_get_ring_data(getIfaceHandle(iface_name),
1023 makeCharVec(ring_name).data());
1024}
1025
1026wifi_error WifiLegacyHal::registerErrorAlertCallbackHandler(
1027 const std::string& iface_name, const on_error_alert_callback& on_user_alert_callback) {
1028 if (on_error_alert_internal_callback) {
1029 return WIFI_ERROR_NOT_AVAILABLE;
1030 }
1031 on_error_alert_internal_callback = [on_user_alert_callback](wifi_request_id id, char* buffer,
1032 int buffer_size, int err_code) {
1033 if (buffer) {
1034 CHECK(id == 0);
1035 on_user_alert_callback(
1036 err_code,
1037 std::vector<uint8_t>(reinterpret_cast<uint8_t*>(buffer),
1038 reinterpret_cast<uint8_t*>(buffer) + buffer_size));
1039 }
1040 };
1041 wifi_error status = global_func_table_.wifi_set_alert_handler(0, getIfaceHandle(iface_name),
1042 {onAsyncErrorAlert});
1043 if (status != WIFI_SUCCESS) {
1044 on_error_alert_internal_callback = nullptr;
1045 }
1046 return status;
1047}
1048
1049wifi_error WifiLegacyHal::deregisterErrorAlertCallbackHandler(const std::string& iface_name) {
1050 if (!on_error_alert_internal_callback) {
1051 return WIFI_ERROR_NOT_AVAILABLE;
1052 }
1053 on_error_alert_internal_callback = nullptr;
1054 return global_func_table_.wifi_reset_alert_handler(0, getIfaceHandle(iface_name));
1055}
1056
1057wifi_error WifiLegacyHal::registerRadioModeChangeCallbackHandler(
1058 const std::string& iface_name,
1059 const on_radio_mode_change_callback& on_user_change_callback) {
1060 if (on_radio_mode_change_internal_callback) {
1061 return WIFI_ERROR_NOT_AVAILABLE;
1062 }
1063 on_radio_mode_change_internal_callback = [on_user_change_callback](
1064 wifi_request_id /* id */, uint32_t num_macs,
1065 wifi_mac_info* mac_infos_arr) {
1066 if (num_macs > 0 && mac_infos_arr) {
1067 std::vector<WifiMacInfo> mac_infos_vec;
1068 for (uint32_t i = 0; i < num_macs; i++) {
1069 WifiMacInfo mac_info;
1070 mac_info.wlan_mac_id = mac_infos_arr[i].wlan_mac_id;
1071 mac_info.mac_band = mac_infos_arr[i].mac_band;
1072 for (int32_t j = 0; j < mac_infos_arr[i].num_iface; j++) {
1073 WifiIfaceInfo iface_info;
1074 iface_info.name = mac_infos_arr[i].iface_info[j].iface_name;
1075 iface_info.channel = mac_infos_arr[i].iface_info[j].channel;
1076 mac_info.iface_infos.push_back(iface_info);
1077 }
1078 mac_infos_vec.push_back(mac_info);
1079 }
1080 on_user_change_callback(mac_infos_vec);
1081 }
1082 };
1083 wifi_error status = global_func_table_.wifi_set_radio_mode_change_handler(
1084 0, getIfaceHandle(iface_name), {onAsyncRadioModeChange});
1085 if (status != WIFI_SUCCESS) {
1086 on_radio_mode_change_internal_callback = nullptr;
1087 }
1088 return status;
1089}
1090
1091wifi_error WifiLegacyHal::registerSubsystemRestartCallbackHandler(
1092 const on_subsystem_restart_callback& on_restart_callback) {
1093 if (on_subsystem_restart_internal_callback) {
1094 return WIFI_ERROR_NOT_AVAILABLE;
1095 }
1096 on_subsystem_restart_internal_callback = [on_restart_callback](const char* error) {
1097 on_restart_callback(error);
1098 };
1099 wifi_error status = global_func_table_.wifi_set_subsystem_restart_handler(
1100 global_handle_, {onAsyncSubsystemRestart});
1101 if (status != WIFI_SUCCESS) {
1102 on_subsystem_restart_internal_callback = nullptr;
1103 }
1104 return status;
1105}
1106
1107wifi_error WifiLegacyHal::startRttRangeRequest(
1108 const std::string& iface_name, wifi_request_id id,
1109 const std::vector<wifi_rtt_config>& rtt_configs,
Sunil Ravif8fc2372022-11-10 18:37:41 +00001110 const on_rtt_results_callback& on_results_user_callback,
1111 const on_rtt_results_callback_v2& on_results_user_callback_v2) {
1112 if (on_rtt_results_internal_callback || on_rtt_results_internal_callback_v2) {
Gabriel Birenf3262f92022-07-15 23:25:39 +00001113 return WIFI_ERROR_NOT_AVAILABLE;
1114 }
1115
1116 on_rtt_results_internal_callback = [on_results_user_callback](wifi_request_id id,
1117 unsigned num_results,
1118 wifi_rtt_result* rtt_results[]) {
1119 if (num_results > 0 && !rtt_results) {
1120 LOG(ERROR) << "Unexpected nullptr in RTT results";
1121 return;
1122 }
1123 std::vector<const wifi_rtt_result*> rtt_results_vec;
1124 std::copy_if(rtt_results, rtt_results + num_results, back_inserter(rtt_results_vec),
1125 [](wifi_rtt_result* rtt_result) { return rtt_result != nullptr; });
1126 on_results_user_callback(id, rtt_results_vec);
1127 };
1128
Sunil Ravif8fc2372022-11-10 18:37:41 +00001129 on_rtt_results_internal_callback_v2 = [on_results_user_callback_v2](
1130 wifi_request_id id, unsigned num_results,
1131 wifi_rtt_result_v2* rtt_results_v2[]) {
1132 if (num_results > 0 && !rtt_results_v2) {
1133 LOG(ERROR) << "Unexpected nullptr in RTT results";
1134 return;
1135 }
1136 std::vector<const wifi_rtt_result_v2*> rtt_results_vec_v2;
1137 std::copy_if(rtt_results_v2, rtt_results_v2 + num_results,
1138 back_inserter(rtt_results_vec_v2),
1139 [](wifi_rtt_result_v2* rtt_result_v2) { return rtt_result_v2 != nullptr; });
1140 on_results_user_callback_v2(id, rtt_results_vec_v2);
1141 };
1142
Gabriel Birenf3262f92022-07-15 23:25:39 +00001143 std::vector<wifi_rtt_config> rtt_configs_internal(rtt_configs);
1144 wifi_error status = global_func_table_.wifi_rtt_range_request(
1145 id, getIfaceHandle(iface_name), rtt_configs.size(), rtt_configs_internal.data(),
Sunil Ravif8fc2372022-11-10 18:37:41 +00001146 {onAsyncRttResults, onAsyncRttResultsV2});
Gabriel Birenf3262f92022-07-15 23:25:39 +00001147 if (status != WIFI_SUCCESS) {
Sunil Ravif8fc2372022-11-10 18:37:41 +00001148 invalidateRttResultsCallbacks();
Gabriel Birenf3262f92022-07-15 23:25:39 +00001149 }
1150 return status;
1151}
1152
1153wifi_error WifiLegacyHal::cancelRttRangeRequest(
1154 const std::string& iface_name, wifi_request_id id,
1155 const std::vector<std::array<uint8_t, ETH_ALEN>>& mac_addrs) {
Sunil Ravif8fc2372022-11-10 18:37:41 +00001156 if (!on_rtt_results_internal_callback && !on_rtt_results_internal_callback_v2) {
Gabriel Birenf3262f92022-07-15 23:25:39 +00001157 return WIFI_ERROR_NOT_AVAILABLE;
1158 }
1159 static_assert(sizeof(mac_addr) == sizeof(std::array<uint8_t, ETH_ALEN>),
1160 "MAC address size mismatch");
1161 // TODO: How do we handle partial cancels (i.e only a subset of enabled mac
1162 // addressed are cancelled).
1163 std::vector<std::array<uint8_t, ETH_ALEN>> mac_addrs_internal(mac_addrs);
1164 wifi_error status = global_func_table_.wifi_rtt_range_cancel(
1165 id, getIfaceHandle(iface_name), mac_addrs.size(),
1166 reinterpret_cast<mac_addr*>(mac_addrs_internal.data()));
1167 // If the request Id is wrong, don't stop the ongoing range request. Any
1168 // other error should be treated as the end of rtt ranging.
1169 if (status != WIFI_ERROR_INVALID_REQUEST_ID) {
Sunil Ravif8fc2372022-11-10 18:37:41 +00001170 invalidateRttResultsCallbacks();
Gabriel Birenf3262f92022-07-15 23:25:39 +00001171 }
1172 return status;
1173}
1174
1175std::pair<wifi_error, wifi_rtt_capabilities> WifiLegacyHal::getRttCapabilities(
1176 const std::string& iface_name) {
1177 wifi_rtt_capabilities rtt_caps;
1178 wifi_error status =
1179 global_func_table_.wifi_get_rtt_capabilities(getIfaceHandle(iface_name), &rtt_caps);
1180 return {status, rtt_caps};
1181}
1182
1183std::pair<wifi_error, wifi_rtt_responder> WifiLegacyHal::getRttResponderInfo(
1184 const std::string& iface_name) {
1185 wifi_rtt_responder rtt_responder;
1186 wifi_error status = global_func_table_.wifi_rtt_get_responder_info(getIfaceHandle(iface_name),
1187 &rtt_responder);
1188 return {status, rtt_responder};
1189}
1190
1191wifi_error WifiLegacyHal::enableRttResponder(const std::string& iface_name, wifi_request_id id,
1192 const wifi_channel_info& channel_hint,
1193 uint32_t max_duration_secs,
1194 const wifi_rtt_responder& info) {
1195 wifi_rtt_responder info_internal(info);
1196 return global_func_table_.wifi_enable_responder(id, getIfaceHandle(iface_name), channel_hint,
1197 max_duration_secs, &info_internal);
1198}
1199
1200wifi_error WifiLegacyHal::disableRttResponder(const std::string& iface_name, wifi_request_id id) {
1201 return global_func_table_.wifi_disable_responder(id, getIfaceHandle(iface_name));
1202}
1203
1204wifi_error WifiLegacyHal::setRttLci(const std::string& iface_name, wifi_request_id id,
1205 const wifi_lci_information& info) {
1206 wifi_lci_information info_internal(info);
1207 return global_func_table_.wifi_set_lci(id, getIfaceHandle(iface_name), &info_internal);
1208}
1209
1210wifi_error WifiLegacyHal::setRttLcr(const std::string& iface_name, wifi_request_id id,
1211 const wifi_lcr_information& info) {
1212 wifi_lcr_information info_internal(info);
1213 return global_func_table_.wifi_set_lcr(id, getIfaceHandle(iface_name), &info_internal);
1214}
1215
1216wifi_error WifiLegacyHal::nanRegisterCallbackHandlers(const std::string& iface_name,
1217 const NanCallbackHandlers& user_callbacks) {
1218 on_nan_notify_response_user_callback = user_callbacks.on_notify_response;
1219 on_nan_event_publish_terminated_user_callback = user_callbacks.on_event_publish_terminated;
1220 on_nan_event_match_user_callback = user_callbacks.on_event_match;
1221 on_nan_event_match_expired_user_callback = user_callbacks.on_event_match_expired;
1222 on_nan_event_subscribe_terminated_user_callback = user_callbacks.on_event_subscribe_terminated;
1223 on_nan_event_followup_user_callback = user_callbacks.on_event_followup;
1224 on_nan_event_disc_eng_event_user_callback = user_callbacks.on_event_disc_eng_event;
1225 on_nan_event_disabled_user_callback = user_callbacks.on_event_disabled;
1226 on_nan_event_tca_user_callback = user_callbacks.on_event_tca;
1227 on_nan_event_beacon_sdf_payload_user_callback = user_callbacks.on_event_beacon_sdf_payload;
1228 on_nan_event_data_path_request_user_callback = user_callbacks.on_event_data_path_request;
1229 on_nan_event_data_path_confirm_user_callback = user_callbacks.on_event_data_path_confirm;
1230 on_nan_event_data_path_end_user_callback = user_callbacks.on_event_data_path_end;
1231 on_nan_event_transmit_follow_up_user_callback = user_callbacks.on_event_transmit_follow_up;
1232 on_nan_event_range_request_user_callback = user_callbacks.on_event_range_request;
1233 on_nan_event_range_report_user_callback = user_callbacks.on_event_range_report;
1234 on_nan_event_schedule_update_user_callback = user_callbacks.on_event_schedule_update;
1235
1236 return global_func_table_.wifi_nan_register_handler(
1237 getIfaceHandle(iface_name),
1238 {onAsyncNanNotifyResponse, onAsyncNanEventPublishReplied,
1239 onAsyncNanEventPublishTerminated, onAsyncNanEventMatch, onAsyncNanEventMatchExpired,
1240 onAsyncNanEventSubscribeTerminated, onAsyncNanEventFollowup,
1241 onAsyncNanEventDiscEngEvent, onAsyncNanEventDisabled, onAsyncNanEventTca,
1242 onAsyncNanEventBeaconSdfPayload, onAsyncNanEventDataPathRequest,
1243 onAsyncNanEventDataPathConfirm, onAsyncNanEventDataPathEnd,
1244 onAsyncNanEventTransmitFollowUp, onAsyncNanEventRangeRequest,
1245 onAsyncNanEventRangeReport, onAsyncNanEventScheduleUpdate});
1246}
1247
1248wifi_error WifiLegacyHal::nanEnableRequest(const std::string& iface_name, transaction_id id,
1249 const NanEnableRequest& msg) {
1250 NanEnableRequest msg_internal(msg);
1251 return global_func_table_.wifi_nan_enable_request(id, getIfaceHandle(iface_name),
1252 &msg_internal);
1253}
1254
1255wifi_error WifiLegacyHal::nanDisableRequest(const std::string& iface_name, transaction_id id) {
1256 return global_func_table_.wifi_nan_disable_request(id, getIfaceHandle(iface_name));
1257}
1258
1259wifi_error WifiLegacyHal::nanPublishRequest(const std::string& iface_name, transaction_id id,
1260 const NanPublishRequest& msg) {
1261 NanPublishRequest msg_internal(msg);
1262 return global_func_table_.wifi_nan_publish_request(id, getIfaceHandle(iface_name),
1263 &msg_internal);
1264}
1265
1266wifi_error WifiLegacyHal::nanPublishCancelRequest(const std::string& iface_name, transaction_id id,
1267 const NanPublishCancelRequest& msg) {
1268 NanPublishCancelRequest msg_internal(msg);
1269 return global_func_table_.wifi_nan_publish_cancel_request(id, getIfaceHandle(iface_name),
1270 &msg_internal);
1271}
1272
1273wifi_error WifiLegacyHal::nanSubscribeRequest(const std::string& iface_name, transaction_id id,
1274 const NanSubscribeRequest& msg) {
1275 NanSubscribeRequest msg_internal(msg);
1276 return global_func_table_.wifi_nan_subscribe_request(id, getIfaceHandle(iface_name),
1277 &msg_internal);
1278}
1279
1280wifi_error WifiLegacyHal::nanSubscribeCancelRequest(const std::string& iface_name,
1281 transaction_id id,
1282 const NanSubscribeCancelRequest& msg) {
1283 NanSubscribeCancelRequest msg_internal(msg);
1284 return global_func_table_.wifi_nan_subscribe_cancel_request(id, getIfaceHandle(iface_name),
1285 &msg_internal);
1286}
1287
1288wifi_error WifiLegacyHal::nanTransmitFollowupRequest(const std::string& iface_name,
1289 transaction_id id,
1290 const NanTransmitFollowupRequest& msg) {
1291 NanTransmitFollowupRequest msg_internal(msg);
1292 return global_func_table_.wifi_nan_transmit_followup_request(id, getIfaceHandle(iface_name),
1293 &msg_internal);
1294}
1295
1296wifi_error WifiLegacyHal::nanStatsRequest(const std::string& iface_name, transaction_id id,
1297 const NanStatsRequest& msg) {
1298 NanStatsRequest msg_internal(msg);
1299 return global_func_table_.wifi_nan_stats_request(id, getIfaceHandle(iface_name), &msg_internal);
1300}
1301
1302wifi_error WifiLegacyHal::nanConfigRequest(const std::string& iface_name, transaction_id id,
1303 const NanConfigRequest& msg) {
1304 NanConfigRequest msg_internal(msg);
1305 return global_func_table_.wifi_nan_config_request(id, getIfaceHandle(iface_name),
1306 &msg_internal);
1307}
1308
1309wifi_error WifiLegacyHal::nanTcaRequest(const std::string& iface_name, transaction_id id,
1310 const NanTCARequest& msg) {
1311 NanTCARequest msg_internal(msg);
1312 return global_func_table_.wifi_nan_tca_request(id, getIfaceHandle(iface_name), &msg_internal);
1313}
1314
1315wifi_error WifiLegacyHal::nanBeaconSdfPayloadRequest(const std::string& iface_name,
1316 transaction_id id,
1317 const NanBeaconSdfPayloadRequest& msg) {
1318 NanBeaconSdfPayloadRequest msg_internal(msg);
1319 return global_func_table_.wifi_nan_beacon_sdf_payload_request(id, getIfaceHandle(iface_name),
1320 &msg_internal);
1321}
1322
1323std::pair<wifi_error, NanVersion> WifiLegacyHal::nanGetVersion() {
1324 NanVersion version;
1325 wifi_error status = global_func_table_.wifi_nan_get_version(global_handle_, &version);
1326 return {status, version};
1327}
1328
1329wifi_error WifiLegacyHal::nanGetCapabilities(const std::string& iface_name, transaction_id id) {
1330 return global_func_table_.wifi_nan_get_capabilities(id, getIfaceHandle(iface_name));
1331}
1332
1333wifi_error WifiLegacyHal::nanDataInterfaceCreate(const std::string& iface_name, transaction_id id,
1334 const std::string& data_iface_name) {
1335 return global_func_table_.wifi_nan_data_interface_create(id, getIfaceHandle(iface_name),
1336 makeCharVec(data_iface_name).data());
1337}
1338
1339wifi_error WifiLegacyHal::nanDataInterfaceDelete(const std::string& iface_name, transaction_id id,
1340 const std::string& data_iface_name) {
1341 return global_func_table_.wifi_nan_data_interface_delete(id, getIfaceHandle(iface_name),
1342 makeCharVec(data_iface_name).data());
1343}
1344
1345wifi_error WifiLegacyHal::nanDataRequestInitiator(const std::string& iface_name, transaction_id id,
1346 const NanDataPathInitiatorRequest& msg) {
1347 NanDataPathInitiatorRequest msg_internal(msg);
1348 return global_func_table_.wifi_nan_data_request_initiator(id, getIfaceHandle(iface_name),
1349 &msg_internal);
1350}
1351
1352wifi_error WifiLegacyHal::nanDataIndicationResponse(const std::string& iface_name,
1353 transaction_id id,
1354 const NanDataPathIndicationResponse& msg) {
1355 NanDataPathIndicationResponse msg_internal(msg);
1356 return global_func_table_.wifi_nan_data_indication_response(id, getIfaceHandle(iface_name),
1357 &msg_internal);
1358}
1359
1360typedef struct {
1361 u8 num_ndp_instances;
1362 NanDataPathId ndp_instance_id;
1363} NanDataPathEndSingleNdpIdRequest;
1364
1365wifi_error WifiLegacyHal::nanDataEnd(const std::string& iface_name, transaction_id id,
1366 uint32_t ndpInstanceId) {
1367 NanDataPathEndSingleNdpIdRequest msg;
1368 msg.num_ndp_instances = 1;
1369 msg.ndp_instance_id = ndpInstanceId;
1370 wifi_error status = global_func_table_.wifi_nan_data_end(id, getIfaceHandle(iface_name),
1371 (NanDataPathEndRequest*)&msg);
1372 return status;
1373}
1374
1375wifi_error WifiLegacyHal::setCountryCode(const std::string& iface_name,
1376 const std::array<uint8_t, 2> code) {
1377 std::string code_str(code.data(), code.data() + code.size());
1378 return global_func_table_.wifi_set_country_code(getIfaceHandle(iface_name), code_str.c_str());
1379}
1380
1381wifi_error WifiLegacyHal::retrieveIfaceHandles() {
1382 wifi_interface_handle* iface_handles = nullptr;
1383 int num_iface_handles = 0;
1384 wifi_error status =
1385 global_func_table_.wifi_get_ifaces(global_handle_, &num_iface_handles, &iface_handles);
1386 if (status != WIFI_SUCCESS) {
1387 LOG(ERROR) << "Failed to enumerate interface handles";
1388 return status;
1389 }
1390 iface_name_to_handle_.clear();
1391 for (int i = 0; i < num_iface_handles; ++i) {
1392 std::array<char, IFNAMSIZ> iface_name_arr = {};
1393 status = global_func_table_.wifi_get_iface_name(iface_handles[i], iface_name_arr.data(),
1394 iface_name_arr.size());
1395 if (status != WIFI_SUCCESS) {
1396 LOG(WARNING) << "Failed to get interface handle name";
1397 continue;
1398 }
1399 // Assuming the interface name is null terminated since the legacy HAL
1400 // API does not return a size.
1401 std::string iface_name(iface_name_arr.data());
1402 LOG(INFO) << "Adding interface handle for " << iface_name;
1403 iface_name_to_handle_[iface_name] = iface_handles[i];
1404 }
1405 return WIFI_SUCCESS;
1406}
1407
1408wifi_interface_handle WifiLegacyHal::getIfaceHandle(const std::string& iface_name) {
1409 const auto iface_handle_iter = iface_name_to_handle_.find(iface_name);
1410 if (iface_handle_iter == iface_name_to_handle_.end()) {
1411 LOG(ERROR) << "Unknown iface name: " << iface_name;
1412 return nullptr;
1413 }
1414 return iface_handle_iter->second;
1415}
1416
1417void WifiLegacyHal::runEventLoop() {
1418 LOG(DEBUG) << "Starting legacy HAL event loop";
1419 global_func_table_.wifi_event_loop(global_handle_);
1420 const auto lock = aidl_sync_util::acquireGlobalLock();
1421 if (!awaiting_event_loop_termination_) {
1422 LOG(FATAL) << "Legacy HAL event loop terminated, but HAL was not stopping";
1423 }
1424 LOG(DEBUG) << "Legacy HAL event loop terminated";
1425 awaiting_event_loop_termination_ = false;
1426 stop_wait_cv_.notify_one();
1427}
1428
1429std::pair<wifi_error, std::vector<wifi_cached_scan_results>> WifiLegacyHal::getGscanCachedResults(
1430 const std::string& iface_name) {
1431 std::vector<wifi_cached_scan_results> cached_scan_results;
1432 cached_scan_results.resize(kMaxCachedGscanResults);
1433 int32_t num_results = 0;
1434 wifi_error status = global_func_table_.wifi_get_cached_gscan_results(
1435 getIfaceHandle(iface_name), true /* always flush */, cached_scan_results.size(),
1436 cached_scan_results.data(), &num_results);
1437 CHECK(num_results >= 0 && static_cast<uint32_t>(num_results) <= kMaxCachedGscanResults);
1438 cached_scan_results.resize(num_results);
1439 // Check for invalid IE lengths in these cached scan results and correct it.
1440 for (auto& cached_scan_result : cached_scan_results) {
1441 int num_scan_results = cached_scan_result.num_results;
1442 for (int i = 0; i < num_scan_results; i++) {
1443 auto& scan_result = cached_scan_result.results[i];
1444 if (scan_result.ie_length > 0) {
1445 LOG(DEBUG) << "Cached scan result has non-zero IE length " << scan_result.ie_length;
1446 scan_result.ie_length = 0;
1447 }
1448 }
1449 }
1450 return {status, std::move(cached_scan_results)};
1451}
1452
1453wifi_error WifiLegacyHal::createVirtualInterface(const std::string& ifname,
1454 wifi_interface_type iftype) {
1455 // Create the interface if it doesn't exist. If interface already exist,
1456 // Vendor Hal should return WIFI_SUCCESS.
1457 wifi_error status = global_func_table_.wifi_virtual_interface_create(global_handle_,
1458 ifname.c_str(), iftype);
1459 return handleVirtualInterfaceCreateOrDeleteStatus(ifname, status);
1460}
1461
1462wifi_error WifiLegacyHal::deleteVirtualInterface(const std::string& ifname) {
1463 // Delete the interface if it was created dynamically.
1464 wifi_error status =
1465 global_func_table_.wifi_virtual_interface_delete(global_handle_, ifname.c_str());
1466 return handleVirtualInterfaceCreateOrDeleteStatus(ifname, status);
1467}
1468
1469wifi_error WifiLegacyHal::handleVirtualInterfaceCreateOrDeleteStatus(const std::string& ifname,
1470 wifi_error status) {
1471 if (status == WIFI_SUCCESS) {
1472 // refresh list of handlers now.
1473 status = retrieveIfaceHandles();
1474 } else if (status == WIFI_ERROR_NOT_SUPPORTED) {
1475 // Vendor hal does not implement this API. Such vendor implementations
1476 // are expected to create / delete interface by other means.
1477
1478 // check if interface exists.
1479 if (if_nametoindex(ifname.c_str())) {
1480 status = retrieveIfaceHandles();
1481 }
1482 }
1483 return status;
1484}
1485
1486wifi_error WifiLegacyHal::getSupportedIfaceName(uint32_t iface_type, std::string& ifname) {
1487 std::array<char, IFNAMSIZ> buffer;
1488
1489 wifi_error res = global_func_table_.wifi_get_supported_iface_name(
1490 global_handle_, (uint32_t)iface_type, buffer.data(), buffer.size());
1491 if (res == WIFI_SUCCESS) ifname = buffer.data();
1492
1493 return res;
1494}
1495
1496wifi_error WifiLegacyHal::multiStaSetPrimaryConnection(const std::string& ifname) {
1497 return global_func_table_.wifi_multi_sta_set_primary_connection(global_handle_,
1498 getIfaceHandle(ifname));
1499}
1500
1501wifi_error WifiLegacyHal::multiStaSetUseCase(wifi_multi_sta_use_case use_case) {
1502 return global_func_table_.wifi_multi_sta_set_use_case(global_handle_, use_case);
1503}
1504
1505wifi_error WifiLegacyHal::setCoexUnsafeChannels(
1506 std::vector<wifi_coex_unsafe_channel> unsafe_channels, uint32_t restrictions) {
1507 return global_func_table_.wifi_set_coex_unsafe_channels(global_handle_, unsafe_channels.size(),
1508 unsafe_channels.data(), restrictions);
1509}
1510
1511wifi_error WifiLegacyHal::setVoipMode(const std::string& iface_name, wifi_voip_mode mode) {
1512 return global_func_table_.wifi_set_voip_mode(getIfaceHandle(iface_name), mode);
1513}
1514
1515wifi_error WifiLegacyHal::twtRegisterHandler(const std::string& iface_name,
1516 const TwtCallbackHandlers& user_callbacks) {
1517 on_twt_event_setup_response_callback = user_callbacks.on_setup_response;
1518 on_twt_event_teardown_completion_callback = user_callbacks.on_teardown_completion;
1519 on_twt_event_info_frame_received_callback = user_callbacks.on_info_frame_received;
1520 on_twt_event_device_notify_callback = user_callbacks.on_device_notify;
1521
1522 return global_func_table_.wifi_twt_register_handler(
1523 getIfaceHandle(iface_name),
1524 {onAsyncTwtEventSetupResponse, onAsyncTwtEventTeardownCompletion,
1525 onAsyncTwtEventInfoFrameReceived, onAsyncTwtEventDeviceNotify});
1526}
1527
1528std::pair<wifi_error, TwtCapabilitySet> WifiLegacyHal::twtGetCapability(
1529 const std::string& iface_name) {
1530 TwtCapabilitySet capSet;
1531 wifi_error status =
1532 global_func_table_.wifi_twt_get_capability(getIfaceHandle(iface_name), &capSet);
1533 return {status, capSet};
1534}
1535
1536wifi_error WifiLegacyHal::twtSetupRequest(const std::string& iface_name,
1537 const TwtSetupRequest& msg) {
1538 TwtSetupRequest msgInternal(msg);
1539 return global_func_table_.wifi_twt_setup_request(getIfaceHandle(iface_name), &msgInternal);
1540}
1541
1542wifi_error WifiLegacyHal::twtTearDownRequest(const std::string& iface_name,
1543 const TwtTeardownRequest& msg) {
1544 TwtTeardownRequest msgInternal(msg);
1545 return global_func_table_.wifi_twt_teardown_request(getIfaceHandle(iface_name), &msgInternal);
1546}
1547
1548wifi_error WifiLegacyHal::twtInfoFrameRequest(const std::string& iface_name,
1549 const TwtInfoFrameRequest& msg) {
1550 TwtInfoFrameRequest msgInternal(msg);
1551 return global_func_table_.wifi_twt_info_frame_request(getIfaceHandle(iface_name), &msgInternal);
1552}
1553
1554std::pair<wifi_error, TwtStats> WifiLegacyHal::twtGetStats(const std::string& iface_name,
1555 uint8_t configId) {
1556 TwtStats stats;
1557 wifi_error status =
1558 global_func_table_.wifi_twt_get_stats(getIfaceHandle(iface_name), configId, &stats);
1559 return {status, stats};
1560}
1561
1562wifi_error WifiLegacyHal::twtClearStats(const std::string& iface_name, uint8_t configId) {
1563 return global_func_table_.wifi_twt_clear_stats(getIfaceHandle(iface_name), configId);
1564}
1565
1566wifi_error WifiLegacyHal::setDtimConfig(const std::string& iface_name, uint32_t multiplier) {
1567 return global_func_table_.wifi_set_dtim_config(getIfaceHandle(iface_name), multiplier);
1568}
1569
1570std::pair<wifi_error, std::vector<wifi_usable_channel>> WifiLegacyHal::getUsableChannels(
1571 uint32_t band_mask, uint32_t iface_mode_mask, uint32_t filter_mask) {
1572 std::vector<wifi_usable_channel> channels;
1573 channels.resize(kMaxWifiUsableChannels);
1574 uint32_t size = 0;
1575 wifi_error status = global_func_table_.wifi_get_usable_channels(
1576 global_handle_, band_mask, iface_mode_mask, filter_mask, channels.size(), &size,
1577 reinterpret_cast<wifi_usable_channel*>(channels.data()));
1578 CHECK(size >= 0 && size <= kMaxWifiUsableChannels);
1579 channels.resize(size);
1580 return {status, std::move(channels)};
1581}
1582
1583wifi_error WifiLegacyHal::triggerSubsystemRestart() {
1584 return global_func_table_.wifi_trigger_subsystem_restart(global_handle_);
1585}
1586
1587wifi_error WifiLegacyHal::setIndoorState(bool isIndoor) {
1588 return global_func_table_.wifi_set_indoor_state(global_handle_, isIndoor);
1589}
1590
1591std::pair<wifi_error, wifi_radio_combination_matrix*>
1592WifiLegacyHal::getSupportedRadioCombinationsMatrix() {
1593 char* buffer = new char[kMaxSupportedRadioCombinationsMatrixLength];
1594 std::fill(buffer, buffer + kMaxSupportedRadioCombinationsMatrixLength, 0);
1595 uint32_t size = 0;
1596 wifi_radio_combination_matrix* radio_combination_matrix_ptr =
1597 reinterpret_cast<wifi_radio_combination_matrix*>(buffer);
1598 wifi_error status = global_func_table_.wifi_get_supported_radio_combinations_matrix(
1599 global_handle_, kMaxSupportedRadioCombinationsMatrixLength, &size,
1600 radio_combination_matrix_ptr);
1601 CHECK(size >= 0 && size <= kMaxSupportedRadioCombinationsMatrixLength);
1602 return {status, radio_combination_matrix_ptr};
1603}
1604
1605wifi_error WifiLegacyHal::chreNanRttRequest(const std::string& iface_name, bool enable) {
1606 if (enable)
1607 return global_func_table_.wifi_nan_rtt_chre_enable_request(0, getIfaceHandle(iface_name),
1608 NULL);
1609 else
1610 return global_func_table_.wifi_nan_rtt_chre_disable_request(0, getIfaceHandle(iface_name));
1611}
1612
1613wifi_error WifiLegacyHal::chreRegisterHandler(const std::string& iface_name,
1614 const ChreCallbackHandlers& handler) {
1615 if (on_chre_nan_rtt_internal_callback) {
1616 return WIFI_ERROR_NOT_AVAILABLE;
1617 }
1618
1619 on_chre_nan_rtt_internal_callback = handler.on_wifi_chre_nan_rtt_state;
1620
1621 wifi_error status = global_func_table_.wifi_chre_register_handler(getIfaceHandle(iface_name),
1622 {onAsyncChreNanRttState});
1623 if (status != WIFI_SUCCESS) {
1624 on_chre_nan_rtt_internal_callback = nullptr;
1625 }
1626 return status;
1627}
1628
1629wifi_error WifiLegacyHal::enableWifiTxPowerLimits(const std::string& iface_name, bool enable) {
1630 return global_func_table_.wifi_enable_tx_power_limits(getIfaceHandle(iface_name), enable);
1631}
1632
1633wifi_error WifiLegacyHal::getWifiCachedScanResults(
1634 const std::string& iface_name, const CachedScanResultsCallbackHandlers& handler) {
1635 on_cached_scan_results_internal_callback = handler.on_cached_scan_results;
1636
1637 wifi_error status = global_func_table_.wifi_get_cached_scan_results(getIfaceHandle(iface_name),
1638 {onSyncCachedScanResults});
1639
1640 on_cached_scan_results_internal_callback = nullptr;
1641 return status;
1642}
1643
Mahesh KKVc84d3772022-12-02 16:53:28 -08001644std::pair<wifi_error, wifi_chip_capabilities> WifiLegacyHal::getWifiChipCapabilities() {
1645 wifi_chip_capabilities chip_capabilities;
1646 wifi_error status =
1647 global_func_table_.wifi_get_chip_capabilities(global_handle_, &chip_capabilities);
1648 return {status, chip_capabilities};
1649}
1650
Gabriel Birenf3262f92022-07-15 23:25:39 +00001651void WifiLegacyHal::invalidate() {
1652 global_handle_ = nullptr;
1653 iface_name_to_handle_.clear();
1654 on_driver_memory_dump_internal_callback = nullptr;
1655 on_firmware_memory_dump_internal_callback = nullptr;
1656 on_gscan_event_internal_callback = nullptr;
1657 on_gscan_full_result_internal_callback = nullptr;
1658 on_link_layer_stats_result_internal_callback = nullptr;
1659 on_rssi_threshold_breached_internal_callback = nullptr;
1660 on_ring_buffer_data_internal_callback = nullptr;
1661 on_error_alert_internal_callback = nullptr;
1662 on_radio_mode_change_internal_callback = nullptr;
1663 on_subsystem_restart_internal_callback = nullptr;
Sunil Ravif8fc2372022-11-10 18:37:41 +00001664 invalidateRttResultsCallbacks();
Gabriel Birenf3262f92022-07-15 23:25:39 +00001665 on_nan_notify_response_user_callback = nullptr;
1666 on_nan_event_publish_terminated_user_callback = nullptr;
1667 on_nan_event_match_user_callback = nullptr;
1668 on_nan_event_match_expired_user_callback = nullptr;
1669 on_nan_event_subscribe_terminated_user_callback = nullptr;
1670 on_nan_event_followup_user_callback = nullptr;
1671 on_nan_event_disc_eng_event_user_callback = nullptr;
1672 on_nan_event_disabled_user_callback = nullptr;
1673 on_nan_event_tca_user_callback = nullptr;
1674 on_nan_event_beacon_sdf_payload_user_callback = nullptr;
1675 on_nan_event_data_path_request_user_callback = nullptr;
1676 on_nan_event_data_path_confirm_user_callback = nullptr;
1677 on_nan_event_data_path_end_user_callback = nullptr;
1678 on_nan_event_transmit_follow_up_user_callback = nullptr;
1679 on_nan_event_range_request_user_callback = nullptr;
1680 on_nan_event_range_report_user_callback = nullptr;
1681 on_nan_event_schedule_update_user_callback = nullptr;
1682 on_twt_event_setup_response_callback = nullptr;
1683 on_twt_event_teardown_completion_callback = nullptr;
1684 on_twt_event_info_frame_received_callback = nullptr;
1685 on_twt_event_device_notify_callback = nullptr;
1686 on_chre_nan_rtt_internal_callback = nullptr;
1687}
1688
1689} // namespace legacy_hal
1690} // namespace wifi
1691} // namespace hardware
1692} // namespace android
1693} // namespace aidl