• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef WIFI_LEGACY_HAL_H_
18 #define WIFI_LEGACY_HAL_H_
19 
20 #include <condition_variable>
21 #include <functional>
22 #include <map>
23 #include <thread>
24 #include <vector>
25 
26 #include <hardware_legacy/wifi_hal.h>
27 #include <wifi_system/interface_tool.h>
28 
29 namespace android {
30 namespace hardware {
31 namespace wifi {
32 namespace V1_6 {
33 namespace implementation {
34 // This is in a separate namespace to prevent typename conflicts between
35 // the legacy HAL types and the HIDL interface types.
36 namespace legacy_hal {
37 // Import all the types defined inside the legacy HAL header files into this
38 // namespace.
39 using ::chre_nan_rtt_state;
40 using ::frame_info;
41 using ::frame_type;
42 using ::FRAME_TYPE_80211_MGMT;
43 using ::FRAME_TYPE_ETHERNET_II;
44 using ::FRAME_TYPE_UNKNOWN;
45 using ::fw_roaming_state_t;
46 using ::mac_addr;
47 using ::NAN_CHANNEL_24G_BAND;
48 using ::NAN_CHANNEL_5G_BAND_HIGH;
49 using ::NAN_CHANNEL_5G_BAND_LOW;
50 using ::NAN_DISABLE_RANGE_REPORT;
51 using ::NAN_DO_NOT_USE_SRF;
52 using ::NAN_DP_CHANNEL_NOT_REQUESTED;
53 using ::NAN_DP_CONFIG_NO_SECURITY;
54 using ::NAN_DP_CONFIG_SECURITY;
55 using ::NAN_DP_END;
56 using ::NAN_DP_FORCE_CHANNEL_SETUP;
57 using ::NAN_DP_INITIATOR_RESPONSE;
58 using ::NAN_DP_INTERFACE_CREATE;
59 using ::NAN_DP_INTERFACE_DELETE;
60 using ::NAN_DP_REQUEST_ACCEPT;
61 using ::NAN_DP_REQUEST_CHANNEL_SETUP;
62 using ::NAN_DP_REQUEST_REJECT;
63 using ::NAN_DP_RESPONDER_RESPONSE;
64 using ::NAN_GET_CAPABILITIES;
65 using ::NAN_MATCH_ALG_MATCH_CONTINUOUS;
66 using ::NAN_MATCH_ALG_MATCH_NEVER;
67 using ::NAN_MATCH_ALG_MATCH_ONCE;
68 using ::NAN_PUBLISH_TYPE_SOLICITED;
69 using ::NAN_PUBLISH_TYPE_UNSOLICITED;
70 using ::NAN_PUBLISH_TYPE_UNSOLICITED_SOLICITED;
71 using ::NAN_RANGING_AUTO_RESPONSE_DISABLE;
72 using ::NAN_RANGING_AUTO_RESPONSE_ENABLE;
73 using ::NAN_RANGING_DISABLE;
74 using ::NAN_RANGING_ENABLE;
75 using ::NAN_RESPONSE_BEACON_SDF_PAYLOAD;
76 using ::NAN_RESPONSE_CONFIG;
77 using ::NAN_RESPONSE_DISABLED;
78 using ::NAN_RESPONSE_ENABLED;
79 using ::NAN_RESPONSE_ERROR;
80 using ::NAN_RESPONSE_PUBLISH;
81 using ::NAN_RESPONSE_PUBLISH_CANCEL;
82 using ::NAN_RESPONSE_STATS;
83 using ::NAN_RESPONSE_SUBSCRIBE;
84 using ::NAN_RESPONSE_SUBSCRIBE_CANCEL;
85 using ::NAN_RESPONSE_TCA;
86 using ::NAN_RESPONSE_TRANSMIT_FOLLOWUP;
87 using ::NAN_SECURITY_KEY_INPUT_PASSPHRASE;
88 using ::NAN_SECURITY_KEY_INPUT_PMK;
89 using ::NAN_SERVICE_ACCEPT_POLICY_ALL;
90 using ::NAN_SERVICE_ACCEPT_POLICY_NONE;
91 using ::NAN_SRF_ATTR_BLOOM_FILTER;
92 using ::NAN_SRF_ATTR_PARTIAL_MAC_ADDR;
93 using ::NAN_SRF_INCLUDE_DO_NOT_RESPOND;
94 using ::NAN_SRF_INCLUDE_RESPOND;
95 using ::NAN_SSI_NOT_REQUIRED_IN_MATCH_IND;
96 using ::NAN_SSI_REQUIRED_IN_MATCH_IND;
97 using ::NAN_STATUS_ALREADY_ENABLED;
98 using ::NAN_STATUS_FOLLOWUP_QUEUE_FULL;
99 using ::NAN_STATUS_INTERNAL_FAILURE;
100 using ::NAN_STATUS_INVALID_NDP_ID;
101 using ::NAN_STATUS_INVALID_PARAM;
102 using ::NAN_STATUS_INVALID_PUBLISH_SUBSCRIBE_ID;
103 using ::NAN_STATUS_INVALID_REQUESTOR_INSTANCE_ID;
104 using ::NAN_STATUS_NAN_NOT_ALLOWED;
105 using ::NAN_STATUS_NO_OTA_ACK;
106 using ::NAN_STATUS_NO_RESOURCE_AVAILABLE;
107 using ::NAN_STATUS_PROTOCOL_FAILURE;
108 using ::NAN_STATUS_SUCCESS;
109 using ::NAN_STATUS_UNSUPPORTED_CONCURRENCY_NAN_DISABLED;
110 using ::NAN_SUBSCRIBE_TYPE_ACTIVE;
111 using ::NAN_SUBSCRIBE_TYPE_PASSIVE;
112 using ::NAN_TRANSMIT_IN_DW;
113 using ::NAN_TRANSMIT_IN_FAW;
114 using ::NAN_TX_PRIORITY_HIGH;
115 using ::NAN_TX_PRIORITY_NORMAL;
116 using ::NAN_TX_TYPE_BROADCAST;
117 using ::NAN_TX_TYPE_UNICAST;
118 using ::NAN_USE_SRF;
119 using ::NanBeaconSdfPayloadInd;
120 using ::NanCapabilities;
121 using ::NanChannelInfo;
122 using ::NanConfigRequest;
123 using ::NanDataPathChannelCfg;
124 using ::NanDataPathConfirmInd;
125 using ::NanDataPathEndInd;
126 using ::NanDataPathIndicationResponse;
127 using ::NanDataPathInitiatorRequest;
128 using ::NanDataPathRequestInd;
129 using ::NanDataPathScheduleUpdateInd;
130 using ::NanDisabledInd;
131 using ::NanDiscEngEventInd;
132 using ::NanEnableRequest;
133 using ::NanFollowupInd;
134 using ::NanMatchAlg;
135 using ::NanMatchExpiredInd;
136 using ::NanMatchInd;
137 using ::NanPublishCancelRequest;
138 using ::NanPublishRequest;
139 using ::NanPublishTerminatedInd;
140 using ::NanPublishType;
141 using ::NanRangeReportInd;
142 using ::NanRangeRequestInd;
143 using ::NanResponseMsg;
144 using ::NanSRFType;
145 using ::NanStatusType;
146 using ::NanSubscribeCancelRequest;
147 using ::NanSubscribeRequest;
148 using ::NanSubscribeTerminatedInd;
149 using ::NanSubscribeType;
150 using ::NanTransmitFollowupInd;
151 using ::NanTransmitFollowupRequest;
152 using ::NanTxType;
153 using ::ROAMING_DISABLE;
154 using ::ROAMING_ENABLE;
155 using ::RTT_PEER_AP;
156 using ::RTT_PEER_NAN;
157 using ::RTT_PEER_P2P_CLIENT;
158 using ::RTT_PEER_P2P_GO;
159 using ::RTT_PEER_STA;
160 using ::rtt_peer_type;
161 using ::RTT_STATUS_ABORTED;
162 using ::RTT_STATUS_FAIL_AP_ON_DIFF_CHANNEL;
163 using ::RTT_STATUS_FAIL_BUSY_TRY_LATER;
164 using ::RTT_STATUS_FAIL_FTM_PARAM_OVERRIDE;
165 using ::RTT_STATUS_FAIL_INVALID_TS;
166 using ::RTT_STATUS_FAIL_NO_CAPABILITY;
167 using ::RTT_STATUS_FAIL_NO_RSP;
168 using ::RTT_STATUS_FAIL_NOT_SCHEDULED_YET;
169 using ::RTT_STATUS_FAIL_PROTOCOL;
170 using ::RTT_STATUS_FAIL_REJECTED;
171 using ::RTT_STATUS_FAIL_SCHEDULE;
172 using ::RTT_STATUS_FAIL_TM_TIMEOUT;
173 using ::RTT_STATUS_FAILURE;
174 using ::RTT_STATUS_INVALID_REQ;
175 using ::RTT_STATUS_NAN_RANGING_CONCURRENCY_NOT_SUPPORTED;
176 using ::RTT_STATUS_NAN_RANGING_PROTOCOL_FAILURE;
177 using ::RTT_STATUS_NO_WIFI;
178 using ::RTT_STATUS_SUCCESS;
179 using ::RTT_TYPE_1_SIDED;
180 using ::RTT_TYPE_2_SIDED;
181 using ::RX_PKT_FATE_DRV_DROP_FILTER;
182 using ::RX_PKT_FATE_DRV_DROP_INVALID;
183 using ::RX_PKT_FATE_DRV_DROP_NOBUFS;
184 using ::RX_PKT_FATE_DRV_DROP_OTHER;
185 using ::RX_PKT_FATE_DRV_QUEUED;
186 using ::RX_PKT_FATE_FW_DROP_FILTER;
187 using ::RX_PKT_FATE_FW_DROP_INVALID;
188 using ::RX_PKT_FATE_FW_DROP_NOBUFS;
189 using ::RX_PKT_FATE_FW_DROP_OTHER;
190 using ::RX_PKT_FATE_FW_QUEUED;
191 using ::RX_PKT_FATE_SUCCESS;
192 using ::ssid_t;
193 using ::transaction_id;
194 using ::TX_PKT_FATE_ACKED;
195 using ::TX_PKT_FATE_DRV_DROP_INVALID;
196 using ::TX_PKT_FATE_DRV_DROP_NOBUFS;
197 using ::TX_PKT_FATE_DRV_DROP_OTHER;
198 using ::TX_PKT_FATE_DRV_QUEUED;
199 using ::TX_PKT_FATE_FW_DROP_INVALID;
200 using ::TX_PKT_FATE_FW_DROP_NOBUFS;
201 using ::TX_PKT_FATE_FW_DROP_OTHER;
202 using ::TX_PKT_FATE_FW_QUEUED;
203 using ::TX_PKT_FATE_SENT;
204 using ::WIFI_AC_BE;
205 using ::WIFI_AC_BK;
206 using ::WIFI_AC_VI;
207 using ::WIFI_AC_VO;
208 using ::WIFI_ANTENNA_1X1;
209 using ::WIFI_ANTENNA_2X2;
210 using ::WIFI_ANTENNA_3X3;
211 using ::WIFI_ANTENNA_4X4;
212 using ::WIFI_ANTENNA_UNSPECIFIED;
213 using ::wifi_band;
214 using ::WIFI_BAND_A;
215 using ::WIFI_BAND_A_DFS;
216 using ::WIFI_BAND_A_WITH_DFS;
217 using ::WIFI_BAND_ABG;
218 using ::WIFI_BAND_ABG_WITH_DFS;
219 using ::WIFI_BAND_BG;
220 using ::WIFI_BAND_UNSPECIFIED;
221 using ::wifi_cached_scan_results;
222 using ::WIFI_CHAN_WIDTH_10;
223 using ::WIFI_CHAN_WIDTH_160;
224 using ::WIFI_CHAN_WIDTH_20;
225 using ::WIFI_CHAN_WIDTH_320;
226 using ::WIFI_CHAN_WIDTH_40;
227 using ::WIFI_CHAN_WIDTH_5;
228 using ::WIFI_CHAN_WIDTH_80;
229 using ::WIFI_CHAN_WIDTH_80P80;
230 using ::WIFI_CHAN_WIDTH_INVALID;
231 using ::wifi_channel_info;
232 using ::wifi_channel_stat;
233 using ::wifi_channel_width;
234 using ::wifi_coex_restriction;
235 using ::wifi_coex_unsafe_channel;
236 using ::WIFI_DUAL_STA_NON_TRANSIENT_UNBIASED;
237 using ::WIFI_DUAL_STA_TRANSIENT_PREFER_PRIMARY;
238 using ::wifi_error;
239 using ::WIFI_ERROR_BUSY;
240 using ::WIFI_ERROR_INVALID_ARGS;
241 using ::WIFI_ERROR_INVALID_REQUEST_ID;
242 using ::WIFI_ERROR_NONE;
243 using ::WIFI_ERROR_NOT_AVAILABLE;
244 using ::WIFI_ERROR_NOT_SUPPORTED;
245 using ::WIFI_ERROR_OUT_OF_MEMORY;
246 using ::WIFI_ERROR_TIMED_OUT;
247 using ::WIFI_ERROR_TOO_MANY_REQUESTS;
248 using ::WIFI_ERROR_UNINITIALIZED;
249 using ::WIFI_ERROR_UNKNOWN;
250 using ::wifi_gscan_capabilities;
251 using ::wifi_hal_fn;
252 using ::wifi_information_element;
253 using ::WIFI_INTERFACE_IBSS;
254 using ::WIFI_INTERFACE_MESH;
255 using ::wifi_interface_mode;
256 using ::WIFI_INTERFACE_NAN;
257 using ::WIFI_INTERFACE_P2P_CLIENT;
258 using ::WIFI_INTERFACE_P2P_GO;
259 using ::WIFI_INTERFACE_SOFTAP;
260 using ::WIFI_INTERFACE_STA;
261 using ::WIFI_INTERFACE_TDLS;
262 using ::wifi_interface_type;
263 using ::WIFI_INTERFACE_TYPE_AP;
264 using ::WIFI_INTERFACE_TYPE_NAN;
265 using ::WIFI_INTERFACE_TYPE_P2P;
266 using ::WIFI_INTERFACE_TYPE_STA;
267 using ::WIFI_INTERFACE_UNKNOWN;
268 using ::wifi_latency_mode;
269 using ::WIFI_LATENCY_MODE_LOW;
270 using ::WIFI_LATENCY_MODE_NORMAL;
271 using ::wifi_lci_information;
272 using ::wifi_lcr_information;
273 using ::WIFI_LOGGER_CONNECT_EVENT_SUPPORTED;
274 using ::WIFI_LOGGER_DRIVER_DUMP_SUPPORTED;
275 using ::WIFI_LOGGER_MEMORY_DUMP_SUPPORTED;
276 using ::WIFI_LOGGER_PACKET_FATE_SUPPORTED;
277 using ::WIFI_LOGGER_POWER_EVENT_SUPPORTED;
278 using ::WIFI_LOGGER_WAKE_LOCK_SUPPORTED;
279 using ::WIFI_MOTION_EXPECTED;
280 using ::WIFI_MOTION_NOT_EXPECTED;
281 using ::wifi_motion_pattern;
282 using ::WIFI_MOTION_UNKNOWN;
283 using ::wifi_multi_sta_use_case;
284 using ::wifi_power_scenario;
285 using ::WIFI_POWER_SCENARIO_ON_BODY_CELL_OFF;
286 using ::WIFI_POWER_SCENARIO_ON_BODY_CELL_ON;
287 using ::WIFI_POWER_SCENARIO_ON_HEAD_CELL_OFF;
288 using ::WIFI_POWER_SCENARIO_ON_HEAD_CELL_ON;
289 using ::WIFI_POWER_SCENARIO_VOICE_CALL;
290 using ::wifi_radio_combination;
291 using ::wifi_radio_combination_matrix;
292 using ::wifi_radio_configuration;
293 using ::wifi_rate;
294 using ::wifi_request_id;
295 using ::wifi_ring_buffer_status;
296 using ::wifi_roaming_capabilities;
297 using ::wifi_roaming_config;
298 using ::wifi_rtt_bw;
299 using ::WIFI_RTT_BW_10;
300 using ::WIFI_RTT_BW_160;
301 using ::WIFI_RTT_BW_20;
302 using ::WIFI_RTT_BW_320;
303 using ::WIFI_RTT_BW_40;
304 using ::WIFI_RTT_BW_5;
305 using ::WIFI_RTT_BW_80;
306 using ::wifi_rtt_capabilities;
307 using ::wifi_rtt_config;
308 using ::wifi_rtt_preamble;
309 using ::WIFI_RTT_PREAMBLE_EHT;
310 using ::WIFI_RTT_PREAMBLE_HE;
311 using ::WIFI_RTT_PREAMBLE_HT;
312 using ::WIFI_RTT_PREAMBLE_LEGACY;
313 using ::WIFI_RTT_PREAMBLE_VHT;
314 using ::wifi_rtt_responder;
315 using ::wifi_rtt_result;
316 using ::wifi_rtt_status;
317 using ::wifi_rtt_type;
318 using ::wifi_rx_packet_fate;
319 using ::wifi_rx_report;
320 using ::wifi_scan_bucket_spec;
321 using ::wifi_scan_cmd_params;
322 using ::WIFI_SCAN_FLAG_INTERRUPTED;
323 using ::wifi_scan_result;
324 using ::WIFI_SUCCESS;
325 using ::wifi_tx_packet_fate;
326 using ::wifi_tx_report;
327 using ::wifi_usable_channel;
328 using ::WIFI_USABLE_CHANNEL_FILTER_CELLULAR_COEXISTENCE;
329 using ::WIFI_USABLE_CHANNEL_FILTER_CONCURRENCY;
330 using ::WLAN_MAC_2_4_BAND;
331 using ::WLAN_MAC_5_0_BAND;
332 using ::WLAN_MAC_60_0_BAND;
333 using ::WLAN_MAC_6_0_BAND;
334 
335 // APF capabilities supported by the iface.
336 struct PacketFilterCapabilities {
337     uint32_t version;
338     uint32_t max_len;
339 };
340 
341 // WARNING: We don't care about the variable sized members of either
342 // |wifi_iface_stat|, |wifi_radio_stat| structures. So, using the pragma
343 // to escape the compiler warnings regarding this.
344 #pragma GCC diagnostic push
345 #pragma GCC diagnostic ignored "-Wgnu-variable-sized-type-not-at-end"
346 // The |wifi_radio_stat.tx_time_per_levels| stats is provided as a pointer in
347 // |wifi_radio_stat| structure in the legacy HAL API. Separate that out
348 // into a separate return element to avoid passing pointers around.
349 struct LinkLayerRadioStats {
350     wifi_radio_stat stats;
351     std::vector<uint32_t> tx_time_per_levels;
352     std::vector<wifi_channel_stat> channel_stats;
353 };
354 
355 struct WifiPeerInfo {
356     wifi_peer_info peer_info;
357     std::vector<wifi_rate_stat> rate_stats;
358 };
359 
360 struct LinkLayerStats {
361     wifi_iface_stat iface;
362     std::vector<LinkLayerRadioStats> radios;
363     std::vector<WifiPeerInfo> peers;
364 };
365 #pragma GCC diagnostic pop
366 
367 // The |WLAN_DRIVER_WAKE_REASON_CNT.cmd_event_wake_cnt| and
368 // |WLAN_DRIVER_WAKE_REASON_CNT.driver_fw_local_wake_cnt| stats is provided
369 // as a pointer in |WLAN_DRIVER_WAKE_REASON_CNT| structure in the legacy HAL
370 // API. Separate that out into a separate return elements to avoid passing
371 // pointers around.
372 struct WakeReasonStats {
373     WLAN_DRIVER_WAKE_REASON_CNT wake_reason_cnt;
374     std::vector<uint32_t> cmd_event_wake_cnt;
375     std::vector<uint32_t> driver_fw_local_wake_cnt;
376 };
377 
378 // NAN response and event callbacks struct.
379 struct NanCallbackHandlers {
380     // NotifyResponse invoked to notify the status of the Request.
381     std::function<void(transaction_id, const NanResponseMsg&)> on_notify_response;
382     // Various event callbacks.
383     std::function<void(const NanPublishTerminatedInd&)> on_event_publish_terminated;
384     std::function<void(const NanMatchInd&)> on_event_match;
385     std::function<void(const NanMatchExpiredInd&)> on_event_match_expired;
386     std::function<void(const NanSubscribeTerminatedInd&)> on_event_subscribe_terminated;
387     std::function<void(const NanFollowupInd&)> on_event_followup;
388     std::function<void(const NanDiscEngEventInd&)> on_event_disc_eng_event;
389     std::function<void(const NanDisabledInd&)> on_event_disabled;
390     std::function<void(const NanTCAInd&)> on_event_tca;
391     std::function<void(const NanBeaconSdfPayloadInd&)> on_event_beacon_sdf_payload;
392     std::function<void(const NanDataPathRequestInd&)> on_event_data_path_request;
393     std::function<void(const NanDataPathConfirmInd&)> on_event_data_path_confirm;
394     std::function<void(const NanDataPathEndInd&)> on_event_data_path_end;
395     std::function<void(const NanTransmitFollowupInd&)> on_event_transmit_follow_up;
396     std::function<void(const NanRangeRequestInd&)> on_event_range_request;
397     std::function<void(const NanRangeReportInd&)> on_event_range_report;
398     std::function<void(const NanDataPathScheduleUpdateInd&)> on_event_schedule_update;
399 };
400 
401 // Full scan results contain IE info and are hence passed by reference, to
402 // preserve the variable length array member |ie_data|. Callee must not retain
403 // the pointer.
404 using on_gscan_full_result_callback =
405         std::function<void(wifi_request_id, const wifi_scan_result*, uint32_t)>;
406 // These scan results don't contain any IE info, so no need to pass by
407 // reference.
408 using on_gscan_results_callback =
409         std::function<void(wifi_request_id, const std::vector<wifi_cached_scan_results>&)>;
410 
411 // Invoked when the rssi value breaches the thresholds set.
412 using on_rssi_threshold_breached_callback =
413         std::function<void(wifi_request_id, std::array<uint8_t, 6>, int8_t)>;
414 
415 // Callback for RTT range request results.
416 // Rtt results contain IE info and are hence passed by reference, to
417 // preserve the |LCI| and |LCR| pointers. Callee must not retain
418 // the pointer.
419 using on_rtt_results_callback =
420         std::function<void(wifi_request_id, const std::vector<const wifi_rtt_result*>&)>;
421 
422 // Callback for ring buffer data.
423 using on_ring_buffer_data_callback = std::function<void(
424         const std::string&, const std::vector<uint8_t>&, const wifi_ring_buffer_status&)>;
425 
426 // Callback for alerts.
427 using on_error_alert_callback = std::function<void(int32_t, const std::vector<uint8_t>&)>;
428 
429 // Callback for subsystem restart
430 using on_subsystem_restart_callback = std::function<void(const std::string&)>;
431 
432 // Struct for the mac info from the legacy HAL. This is a cleaner version
433 // of the |wifi_mac_info| & |wifi_iface_info|.
434 typedef struct {
435     std::string name;
436     wifi_channel channel;
437 } WifiIfaceInfo;
438 
439 typedef struct {
440     uint32_t wlan_mac_id;
441     /* BIT MASK of BIT(WLAN_MAC*) as represented by wlan_mac_band */
442     uint32_t mac_band;
443     /* Represents the connected Wi-Fi interfaces associated with each MAC */
444     std::vector<WifiIfaceInfo> iface_infos;
445 } WifiMacInfo;
446 
447 // Callback for radio mode change
448 using on_radio_mode_change_callback = std::function<void(const std::vector<WifiMacInfo>&)>;
449 
450 // TWT response and event callbacks struct.
451 struct TwtCallbackHandlers {
452     // Callback for TWT setup response
453     std::function<void(const TwtSetupResponse&)> on_setup_response;
454     // Callback for TWT teardown completion
455     std::function<void(const TwtTeardownCompletion&)> on_teardown_completion;
456     // Callback for TWT info frame received event
457     std::function<void(const TwtInfoFrameReceived&)> on_info_frame_received;
458     // Callback for TWT notification from the device
459     std::function<void(const TwtDeviceNotify&)> on_device_notify;
460 };
461 
462 // CHRE response and event callbacks struct.
463 struct ChreCallbackHandlers {
464     // Callback for CHRE NAN RTT
465     std::function<void(chre_nan_rtt_state)> on_wifi_chre_nan_rtt_state;
466 };
467 
468 /**
469  * Class that encapsulates all legacy HAL interactions.
470  * This class manages the lifetime of the event loop thread used by legacy HAL.
471  *
472  * Note: There will only be a single instance of this class created in the Wifi
473  * object and will be valid for the lifetime of the process.
474  */
475 class WifiLegacyHal {
476   public:
477     WifiLegacyHal(const std::weak_ptr<wifi_system::InterfaceTool> iface_tool, const wifi_hal_fn& fn,
478                   bool is_primary);
479     virtual ~WifiLegacyHal() = default;
480 
481     // Initialize the legacy HAL function table.
482     virtual wifi_error initialize();
483     // Start the legacy HAL and the event looper thread.
484     virtual wifi_error start();
485     // Deinitialize the legacy HAL and wait for the event loop thread to exit
486     // using a predefined timeout.
487     virtual wifi_error stop(std::unique_lock<std::recursive_mutex>* lock,
488                             const std::function<void()>& on_complete_callback);
489     virtual wifi_error waitForDriverReady();
490     // Checks if legacy HAL has successfully started
491     bool isStarted();
492     // Wrappers for all the functions in the legacy HAL function table.
493     virtual std::pair<wifi_error, std::string> getDriverVersion(const std::string& iface_name);
494     virtual std::pair<wifi_error, std::string> getFirmwareVersion(const std::string& iface_name);
495     std::pair<wifi_error, std::vector<uint8_t>> requestDriverMemoryDump(
496             const std::string& iface_name);
497     std::pair<wifi_error, std::vector<uint8_t>> requestFirmwareMemoryDump(
498             const std::string& iface_name);
499     virtual std::pair<wifi_error, uint64_t> getSupportedFeatureSet(const std::string& iface_name);
500     // APF functions.
501     std::pair<wifi_error, PacketFilterCapabilities> getPacketFilterCapabilities(
502             const std::string& iface_name);
503     wifi_error setPacketFilter(const std::string& iface_name, const std::vector<uint8_t>& program);
504     std::pair<wifi_error, std::vector<uint8_t>> readApfPacketFilterData(
505             const std::string& iface_name);
506     // Gscan functions.
507     std::pair<wifi_error, wifi_gscan_capabilities> getGscanCapabilities(
508             const std::string& iface_name);
509     // These API's provides a simplified interface over the legacy Gscan API's:
510     // a) All scan events from the legacy HAL API other than the
511     //    |WIFI_SCAN_FAILED| are treated as notification of results.
512     //    This method then retrieves the cached scan results from the legacy
513     //    HAL API and triggers the externally provided
514     //    |on_results_user_callback| on success.
515     // b) |WIFI_SCAN_FAILED| scan event or failure to retrieve cached scan
516     // results
517     //    triggers the externally provided |on_failure_user_callback|.
518     // c) Full scan result event triggers the externally provided
519     //    |on_full_result_user_callback|.
520     wifi_error startGscan(const std::string& iface_name, wifi_request_id id,
521                           const wifi_scan_cmd_params& params,
522                           const std::function<void(wifi_request_id)>& on_failure_callback,
523                           const on_gscan_results_callback& on_results_callback,
524                           const on_gscan_full_result_callback& on_full_result_callback);
525     wifi_error stopGscan(const std::string& iface_name, wifi_request_id id);
526     std::pair<wifi_error, std::vector<uint32_t>> getValidFrequenciesForBand(
527             const std::string& iface_name, wifi_band band);
528     virtual wifi_error setDfsFlag(const std::string& iface_name, bool dfs_on);
529     // Link layer stats functions.
530     wifi_error enableLinkLayerStats(const std::string& iface_name, bool debug);
531     wifi_error disableLinkLayerStats(const std::string& iface_name);
532     std::pair<wifi_error, LinkLayerStats> getLinkLayerStats(const std::string& iface_name);
533     // RSSI monitor functions.
534     wifi_error startRssiMonitoring(
535             const std::string& iface_name, wifi_request_id id, int8_t max_rssi, int8_t min_rssi,
536             const on_rssi_threshold_breached_callback& on_threshold_breached_callback);
537     wifi_error stopRssiMonitoring(const std::string& iface_name, wifi_request_id id);
538     std::pair<wifi_error, wifi_roaming_capabilities> getRoamingCapabilities(
539             const std::string& iface_name);
540     wifi_error configureRoaming(const std::string& iface_name, const wifi_roaming_config& config);
541     wifi_error enableFirmwareRoaming(const std::string& iface_name, fw_roaming_state_t state);
542     wifi_error configureNdOffload(const std::string& iface_name, bool enable);
543     wifi_error startSendingOffloadedPacket(const std::string& iface_name, uint32_t cmd_id,
544                                            uint16_t ether_type,
545                                            const std::vector<uint8_t>& ip_packet_data,
546                                            const std::array<uint8_t, 6>& src_address,
547                                            const std::array<uint8_t, 6>& dst_address,
548                                            uint32_t period_in_ms);
549     wifi_error stopSendingOffloadedPacket(const std::string& iface_name, uint32_t cmd_id);
550     virtual wifi_error selectTxPowerScenario(const std::string& iface_name,
551                                              wifi_power_scenario scenario);
552     virtual wifi_error resetTxPowerScenario(const std::string& iface_name);
553     wifi_error setLatencyMode(const std::string& iface_name, wifi_latency_mode mode);
554     wifi_error setThermalMitigationMode(wifi_thermal_mode mode, uint32_t completion_window);
555     wifi_error setDscpToAccessCategoryMapping(uint32_t start, uint32_t end,
556                                               uint32_t access_category);
557     wifi_error resetDscpToAccessCategoryMapping();
558     // Logger/debug functions.
559     std::pair<wifi_error, uint32_t> getLoggerSupportedFeatureSet(const std::string& iface_name);
560     wifi_error startPktFateMonitoring(const std::string& iface_name);
561     std::pair<wifi_error, std::vector<wifi_tx_report>> getTxPktFates(const std::string& iface_name);
562     std::pair<wifi_error, std::vector<wifi_rx_report>> getRxPktFates(const std::string& iface_name);
563     std::pair<wifi_error, WakeReasonStats> getWakeReasonStats(const std::string& iface_name);
564     wifi_error registerRingBufferCallbackHandler(
565             const std::string& iface_name, const on_ring_buffer_data_callback& on_data_callback);
566     wifi_error deregisterRingBufferCallbackHandler(const std::string& iface_name);
567     virtual wifi_error registerSubsystemRestartCallbackHandler(
568             const on_subsystem_restart_callback& on_restart_callback);
569     std::pair<wifi_error, std::vector<wifi_ring_buffer_status>> getRingBuffersStatus(
570             const std::string& iface_name);
571     wifi_error startRingBufferLogging(const std::string& iface_name, const std::string& ring_name,
572                                       uint32_t verbose_level, uint32_t max_interval_sec,
573                                       uint32_t min_data_size);
574     wifi_error getRingBufferData(const std::string& iface_name, const std::string& ring_name);
575     wifi_error registerErrorAlertCallbackHandler(const std::string& iface_name,
576                                                  const on_error_alert_callback& on_alert_callback);
577     wifi_error deregisterErrorAlertCallbackHandler(const std::string& iface_name);
578     // Radio mode functions.
579     virtual wifi_error registerRadioModeChangeCallbackHandler(
580             const std::string& iface_name,
581             const on_radio_mode_change_callback& on_user_change_callback);
582     // RTT functions.
583     wifi_error startRttRangeRequest(const std::string& iface_name, wifi_request_id id,
584                                     const std::vector<wifi_rtt_config>& rtt_configs,
585                                     const on_rtt_results_callback& on_results_callback);
586     wifi_error cancelRttRangeRequest(const std::string& iface_name, wifi_request_id id,
587                                      const std::vector<std::array<uint8_t, 6>>& mac_addrs);
588     std::pair<wifi_error, wifi_rtt_capabilities> getRttCapabilities(const std::string& iface_name);
589     std::pair<wifi_error, wifi_rtt_responder> getRttResponderInfo(const std::string& iface_name);
590     wifi_error enableRttResponder(const std::string& iface_name, wifi_request_id id,
591                                   const wifi_channel_info& channel_hint, uint32_t max_duration_secs,
592                                   const wifi_rtt_responder& info);
593     wifi_error disableRttResponder(const std::string& iface_name, wifi_request_id id);
594     wifi_error setRttLci(const std::string& iface_name, wifi_request_id id,
595                          const wifi_lci_information& info);
596     wifi_error setRttLcr(const std::string& iface_name, wifi_request_id id,
597                          const wifi_lcr_information& info);
598     // NAN functions.
599     virtual wifi_error nanRegisterCallbackHandlers(const std::string& iface_name,
600                                                    const NanCallbackHandlers& callbacks);
601     wifi_error nanEnableRequest(const std::string& iface_name, transaction_id id,
602                                 const NanEnableRequest& msg);
603     virtual wifi_error nanDisableRequest(const std::string& iface_name, transaction_id id);
604     wifi_error nanPublishRequest(const std::string& iface_name, transaction_id id,
605                                  const NanPublishRequest& msg);
606     wifi_error nanPublishCancelRequest(const std::string& iface_name, transaction_id id,
607                                        const NanPublishCancelRequest& msg);
608     wifi_error nanSubscribeRequest(const std::string& iface_name, transaction_id id,
609                                    const NanSubscribeRequest& msg);
610     wifi_error nanSubscribeCancelRequest(const std::string& iface_name, transaction_id id,
611                                          const NanSubscribeCancelRequest& msg);
612     wifi_error nanTransmitFollowupRequest(const std::string& iface_name, transaction_id id,
613                                           const NanTransmitFollowupRequest& msg);
614     wifi_error nanStatsRequest(const std::string& iface_name, transaction_id id,
615                                const NanStatsRequest& msg);
616     wifi_error nanConfigRequest(const std::string& iface_name, transaction_id id,
617                                 const NanConfigRequest& msg);
618     wifi_error nanTcaRequest(const std::string& iface_name, transaction_id id,
619                              const NanTCARequest& msg);
620     wifi_error nanBeaconSdfPayloadRequest(const std::string& iface_name, transaction_id id,
621                                           const NanBeaconSdfPayloadRequest& msg);
622     std::pair<wifi_error, NanVersion> nanGetVersion();
623     wifi_error nanGetCapabilities(const std::string& iface_name, transaction_id id);
624     wifi_error nanDataInterfaceCreate(const std::string& iface_name, transaction_id id,
625                                       const std::string& data_iface_name);
626     virtual wifi_error nanDataInterfaceDelete(const std::string& iface_name, transaction_id id,
627                                               const std::string& data_iface_name);
628     wifi_error nanDataRequestInitiator(const std::string& iface_name, transaction_id id,
629                                        const NanDataPathInitiatorRequest& msg);
630     wifi_error nanDataIndicationResponse(const std::string& iface_name, transaction_id id,
631                                          const NanDataPathIndicationResponse& msg);
632     wifi_error nanDataEnd(const std::string& iface_name, transaction_id id, uint32_t ndpInstanceId);
633     // AP functions.
634     wifi_error setCountryCode(const std::string& iface_name, std::array<int8_t, 2> code);
635 
636     // interface functions.
637     virtual wifi_error createVirtualInterface(const std::string& ifname,
638                                               wifi_interface_type iftype);
639     virtual wifi_error deleteVirtualInterface(const std::string& ifname);
640     virtual wifi_error getSupportedIfaceName(uint32_t iface_type, std::string& ifname);
641 
642     // STA + STA functions
643     virtual wifi_error multiStaSetPrimaryConnection(const std::string& ifname);
644     virtual wifi_error multiStaSetUseCase(wifi_multi_sta_use_case use_case);
645 
646     // Coex functions.
647     virtual wifi_error setCoexUnsafeChannels(std::vector<wifi_coex_unsafe_channel> unsafe_channels,
648                                              uint32_t restrictions);
649 
650     wifi_error setVoipMode(const std::string& iface_name, wifi_voip_mode mode);
651 
652     wifi_error twtRegisterHandler(const std::string& iface_name,
653                                   const TwtCallbackHandlers& handler);
654 
655     std::pair<wifi_error, TwtCapabilitySet> twtGetCapability(const std::string& iface_name);
656 
657     wifi_error twtSetupRequest(const std::string& iface_name, const TwtSetupRequest& msg);
658 
659     wifi_error twtTearDownRequest(const std::string& iface_name, const TwtTeardownRequest& msg);
660 
661     wifi_error twtInfoFrameRequest(const std::string& iface_name, const TwtInfoFrameRequest& msg);
662 
663     std::pair<wifi_error, TwtStats> twtGetStats(const std::string& iface_name, uint8_t configId);
664 
665     wifi_error twtClearStats(const std::string& iface_name, uint8_t configId);
666 
667     wifi_error setDtimConfig(const std::string& iface_name, uint32_t multiplier);
668 
669     // Retrieve the list of usable channels in the requested bands
670     // for the requested modes
671     std::pair<wifi_error, std::vector<wifi_usable_channel>> getUsableChannels(
672             uint32_t band_mask, uint32_t iface_mode_mask, uint32_t filter_mask);
673 
674     wifi_error triggerSubsystemRestart();
675 
676     wifi_error setIndoorState(bool isIndoor);
677 
678     std::pair<wifi_error, wifi_radio_combination_matrix*> getSupportedRadioCombinationsMatrix();
679 
680     // CHRE NAN RTT function
681     wifi_error chreNanRttRequest(const std::string& iface_name, bool enable);
682 
683     wifi_error chreRegisterHandler(const std::string& iface_name,
684                                    const ChreCallbackHandlers& handler);
685 
686     wifi_error enableWifiTxPowerLimits(const std::string& iface_name, bool enable);
687 
688   private:
689     // Retrieve interface handles for all the available interfaces.
690     wifi_error retrieveIfaceHandles();
691     wifi_interface_handle getIfaceHandle(const std::string& iface_name);
692     // Run the legacy HAL event loop thread.
693     void runEventLoop();
694     // Retrieve the cached gscan results to pass the results back to the
695     // external callbacks.
696     std::pair<wifi_error, std::vector<wifi_cached_scan_results>> getGscanCachedResults(
697             const std::string& iface_name);
698     void invalidate();
699     // Handles wifi (error) status of Virtual interface create/delete
700     wifi_error handleVirtualInterfaceCreateOrDeleteStatus(const std::string& ifname,
701                                                           wifi_error status);
702 
703     // Global function table of legacy HAL.
704     wifi_hal_fn global_func_table_;
705     // Opaque handle to be used for all global operations.
706     wifi_handle global_handle_;
707     // Map of interface name to handle that is to be used for all interface
708     // specific operations.
709     std::map<std::string, wifi_interface_handle> iface_name_to_handle_;
710     // Flag to indicate if we have initiated the cleanup of legacy HAL.
711     std::atomic<bool> awaiting_event_loop_termination_;
712     std::condition_variable_any stop_wait_cv_;
713     // Flag to indicate if the legacy HAL has been started.
714     bool is_started_;
715     std::weak_ptr<wifi_system::InterfaceTool> iface_tool_;
716     // flag to indicate if this HAL is for the primary chip. This is used
717     // in order to avoid some hard-coded behavior used with older HALs,
718     // such as bring wlan0 interface up/down on start/stop HAL.
719     // it may be removed once vendor HALs are updated.
720     bool is_primary_;
721 };
722 
723 }  // namespace legacy_hal
724 }  // namespace implementation
725 }  // namespace V1_6
726 }  // namespace wifi
727 }  // namespace hardware
728 }  // namespace android
729 
730 #endif  // WIFI_LEGACY_HAL_H_
731