• 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 <functional>
21 #include <thread>
22 #include <vector>
23 #include <condition_variable>
24 
25 #include <wifi_system/interface_tool.h>
26 
27 namespace android {
28 namespace hardware {
29 namespace wifi {
30 namespace V1_1 {
31 namespace implementation {
32 // This is in a separate namespace to prevent typename conflicts between
33 // the legacy HAL types and the HIDL interface types.
34 namespace legacy_hal {
35 // Wrap all the types defined inside the legacy HAL header files inside this
36 // namespace.
37 #include <hardware_legacy/wifi_hal.h>
38 
39 // APF capabilities supported by the iface.
40 struct PacketFilterCapabilities {
41   uint32_t version;
42   uint32_t max_len;
43 };
44 
45 // WARNING: We don't care about the variable sized members of either
46 // |wifi_iface_stat|, |wifi_radio_stat| structures. So, using the pragma
47 // to escape the compiler warnings regarding this.
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wgnu-variable-sized-type-not-at-end"
50 // The |wifi_radio_stat.tx_time_per_levels| stats is provided as a pointer in
51 // |wifi_radio_stat| structure in the legacy HAL API. Separate that out
52 // into a separate return element to avoid passing pointers around.
53 struct LinkLayerRadioStats {
54   wifi_radio_stat stats;
55   std::vector<uint32_t> tx_time_per_levels;
56 };
57 
58 struct LinkLayerStats {
59   wifi_iface_stat iface;
60   std::vector<LinkLayerRadioStats> radios;
61 };
62 #pragma GCC diagnostic pop
63 
64 // The |WLAN_DRIVER_WAKE_REASON_CNT.cmd_event_wake_cnt| and
65 // |WLAN_DRIVER_WAKE_REASON_CNT.driver_fw_local_wake_cnt| stats is provided
66 // as a pointer in |WLAN_DRIVER_WAKE_REASON_CNT| structure in the legacy HAL
67 // API. Separate that out into a separate return elements to avoid passing
68 // pointers around.
69 struct WakeReasonStats {
70   WLAN_DRIVER_WAKE_REASON_CNT wake_reason_cnt;
71   std::vector<uint32_t> cmd_event_wake_cnt;
72   std::vector<uint32_t> driver_fw_local_wake_cnt;
73 };
74 
75 // NAN response and event callbacks struct.
76 struct NanCallbackHandlers {
77   // NotifyResponse invoked to notify the status of the Request.
78   std::function<void(transaction_id, const NanResponseMsg&)> on_notify_response;
79   // Various event callbacks.
80   std::function<void(const NanPublishTerminatedInd&)>
81       on_event_publish_terminated;
82   std::function<void(const NanMatchInd&)> on_event_match;
83   std::function<void(const NanMatchExpiredInd&)> on_event_match_expired;
84   std::function<void(const NanSubscribeTerminatedInd&)>
85       on_event_subscribe_terminated;
86   std::function<void(const NanFollowupInd&)> on_event_followup;
87   std::function<void(const NanDiscEngEventInd&)> on_event_disc_eng_event;
88   std::function<void(const NanDisabledInd&)> on_event_disabled;
89   std::function<void(const NanTCAInd&)> on_event_tca;
90   std::function<void(const NanBeaconSdfPayloadInd&)>
91       on_event_beacon_sdf_payload;
92   std::function<void(const NanDataPathRequestInd&)> on_event_data_path_request;
93   std::function<void(const NanDataPathConfirmInd&)> on_event_data_path_confirm;
94   std::function<void(const NanDataPathEndInd&)> on_event_data_path_end;
95   std::function<void(const NanTransmitFollowupInd&)>
96       on_event_transmit_follow_up;
97   std::function<void(const NanRangeRequestInd&)>
98       on_event_range_request;
99   std::function<void(const NanRangeReportInd&)>
100       on_event_range_report;
101 };
102 
103 // Full scan results contain IE info and are hence passed by reference, to
104 // preserve the variable length array member |ie_data|. Callee must not retain
105 // the pointer.
106 using on_gscan_full_result_callback =
107     std::function<void(wifi_request_id, const wifi_scan_result*, uint32_t)>;
108 // These scan results don't contain any IE info, so no need to pass by
109 // reference.
110 using on_gscan_results_callback = std::function<void(
111     wifi_request_id, const std::vector<wifi_cached_scan_results>&)>;
112 
113 // Invoked when the rssi value breaches the thresholds set.
114 using on_rssi_threshold_breached_callback =
115     std::function<void(wifi_request_id, std::array<uint8_t, 6>, int8_t)>;
116 
117 // Callback for RTT range request results.
118 // Rtt results contain IE info and are hence passed by reference, to
119 // preserve the |LCI| and |LCR| pointers. Callee must not retain
120 // the pointer.
121 using on_rtt_results_callback = std::function<void(
122     wifi_request_id, const std::vector<const wifi_rtt_result*>&)>;
123 
124 // Callback for ring buffer data.
125 using on_ring_buffer_data_callback =
126     std::function<void(const std::string&,
127                        const std::vector<uint8_t>&,
128                        const wifi_ring_buffer_status&)>;
129 
130 // Callback for alerts.
131 using on_error_alert_callback =
132     std::function<void(int32_t, const std::vector<uint8_t>&)>;
133 /**
134  * Class that encapsulates all legacy HAL interactions.
135  * This class manages the lifetime of the event loop thread used by legacy HAL.
136  *
137  * Note: aThere will only be a single instance of this class created in the Wifi
138  * object and will be valid for the lifetime of the process.
139  */
140 class WifiLegacyHal {
141  public:
142   WifiLegacyHal();
143   // Names to use for the different types of iface.
144   std::string getApIfaceName();
145   std::string getNanIfaceName();
146   std::string getP2pIfaceName();
147   std::string getStaIfaceName();
148 
149   // Initialize the legacy HAL function table.
150   wifi_error initialize();
151   // Start the legacy HAL and the event looper thread.
152   wifi_error start();
153   // Deinitialize the legacy HAL and wait for the event loop thread to exit
154   // using a predefined timeout.
155   wifi_error stop(std::unique_lock<std::recursive_mutex>* lock,
156                   const std::function<void()>& on_complete_callback);
157   // Wrappers for all the functions in the legacy HAL function table.
158   std::pair<wifi_error, std::string> getDriverVersion();
159   std::pair<wifi_error, std::string> getFirmwareVersion();
160   std::pair<wifi_error, std::vector<uint8_t>> requestDriverMemoryDump();
161   std::pair<wifi_error, std::vector<uint8_t>> requestFirmwareMemoryDump();
162   std::pair<wifi_error, uint32_t> getSupportedFeatureSet();
163   // APF functions.
164   std::pair<wifi_error, PacketFilterCapabilities> getPacketFilterCapabilities();
165   wifi_error setPacketFilter(const std::vector<uint8_t>& program);
166   // Gscan functions.
167   std::pair<wifi_error, wifi_gscan_capabilities> getGscanCapabilities();
168   // These API's provides a simplified interface over the legacy Gscan API's:
169   // a) All scan events from the legacy HAL API other than the
170   //    |WIFI_SCAN_FAILED| are treated as notification of results.
171   //    This method then retrieves the cached scan results from the legacy
172   //    HAL API and triggers the externally provided |on_results_user_callback|
173   //    on success.
174   // b) |WIFI_SCAN_FAILED| scan event or failure to retrieve cached scan results
175   //    triggers the externally provided |on_failure_user_callback|.
176   // c) Full scan result event triggers the externally provided
177   //    |on_full_result_user_callback|.
178   wifi_error startGscan(
179       wifi_request_id id,
180       const wifi_scan_cmd_params& params,
181       const std::function<void(wifi_request_id)>& on_failure_callback,
182       const on_gscan_results_callback& on_results_callback,
183       const on_gscan_full_result_callback& on_full_result_callback);
184   wifi_error stopGscan(wifi_request_id id);
185   std::pair<wifi_error, std::vector<uint32_t>> getValidFrequenciesForBand(
186       wifi_band band);
187   wifi_error setDfsFlag(bool dfs_on);
188   // Link layer stats functions.
189   wifi_error enableLinkLayerStats(bool debug);
190   wifi_error disableLinkLayerStats();
191   std::pair<wifi_error, LinkLayerStats> getLinkLayerStats();
192   // RSSI monitor functions.
193   wifi_error startRssiMonitoring(wifi_request_id id,
194                                  int8_t max_rssi,
195                                  int8_t min_rssi,
196                                  const on_rssi_threshold_breached_callback&
197                                      on_threshold_breached_callback);
198   wifi_error stopRssiMonitoring(wifi_request_id id);
199   std::pair<wifi_error, wifi_roaming_capabilities> getRoamingCapabilities();
200   wifi_error configureRoaming(const wifi_roaming_config& config);
201   wifi_error enableFirmwareRoaming(fw_roaming_state_t state);
202   wifi_error configureNdOffload(bool enable);
203   wifi_error startSendingOffloadedPacket(
204       uint32_t cmd_id,
205       const std::vector<uint8_t>& ip_packet_data,
206       const std::array<uint8_t, 6>& src_address,
207       const std::array<uint8_t, 6>& dst_address,
208       uint32_t period_in_ms);
209   wifi_error stopSendingOffloadedPacket(uint32_t cmd_id);
210   wifi_error setScanningMacOui(const std::array<uint8_t, 3>& oui);
211   wifi_error selectTxPowerScenario(wifi_power_scenario scenario);
212   wifi_error resetTxPowerScenario();
213   // Logger/debug functions.
214   std::pair<wifi_error, uint32_t> getLoggerSupportedFeatureSet();
215   wifi_error startPktFateMonitoring();
216   std::pair<wifi_error, std::vector<wifi_tx_report>> getTxPktFates();
217   std::pair<wifi_error, std::vector<wifi_rx_report>> getRxPktFates();
218   std::pair<wifi_error, WakeReasonStats> getWakeReasonStats();
219   wifi_error registerRingBufferCallbackHandler(
220       const on_ring_buffer_data_callback& on_data_callback);
221   wifi_error deregisterRingBufferCallbackHandler();
222   std::pair<wifi_error, std::vector<wifi_ring_buffer_status>>
223   getRingBuffersStatus();
224   wifi_error startRingBufferLogging(const std::string& ring_name,
225                                     uint32_t verbose_level,
226                                     uint32_t max_interval_sec,
227                                     uint32_t min_data_size);
228   wifi_error getRingBufferData(const std::string& ring_name);
229   wifi_error registerErrorAlertCallbackHandler(
230       const on_error_alert_callback& on_alert_callback);
231   wifi_error deregisterErrorAlertCallbackHandler();
232   // RTT functions.
233   wifi_error startRttRangeRequest(
234       wifi_request_id id,
235       const std::vector<wifi_rtt_config>& rtt_configs,
236       const on_rtt_results_callback& on_results_callback);
237   wifi_error cancelRttRangeRequest(
238       wifi_request_id id, const std::vector<std::array<uint8_t, 6>>& mac_addrs);
239   std::pair<wifi_error, wifi_rtt_capabilities> getRttCapabilities();
240   std::pair<wifi_error, wifi_rtt_responder> getRttResponderInfo();
241   wifi_error enableRttResponder(wifi_request_id id,
242                                 const wifi_channel_info& channel_hint,
243                                 uint32_t max_duration_secs,
244                                 const wifi_rtt_responder& info);
245   wifi_error disableRttResponder(wifi_request_id id);
246   wifi_error setRttLci(wifi_request_id id, const wifi_lci_information& info);
247   wifi_error setRttLcr(wifi_request_id id, const wifi_lcr_information& info);
248   // NAN functions.
249   wifi_error nanRegisterCallbackHandlers(const NanCallbackHandlers& callbacks);
250   wifi_error nanEnableRequest(transaction_id id, const NanEnableRequest& msg);
251   wifi_error nanDisableRequest(transaction_id id);
252   wifi_error nanPublishRequest(transaction_id id, const NanPublishRequest& msg);
253   wifi_error nanPublishCancelRequest(transaction_id id,
254                                      const NanPublishCancelRequest& msg);
255   wifi_error nanSubscribeRequest(transaction_id id,
256                                  const NanSubscribeRequest& msg);
257   wifi_error nanSubscribeCancelRequest(transaction_id id,
258                                        const NanSubscribeCancelRequest& msg);
259   wifi_error nanTransmitFollowupRequest(transaction_id id,
260                                         const NanTransmitFollowupRequest& msg);
261   wifi_error nanStatsRequest(transaction_id id, const NanStatsRequest& msg);
262   wifi_error nanConfigRequest(transaction_id id, const NanConfigRequest& msg);
263   wifi_error nanTcaRequest(transaction_id id, const NanTCARequest& msg);
264   wifi_error nanBeaconSdfPayloadRequest(transaction_id id,
265                                         const NanBeaconSdfPayloadRequest& msg);
266   std::pair<wifi_error, NanVersion> nanGetVersion();
267   wifi_error nanGetCapabilities(transaction_id id);
268   wifi_error nanDataInterfaceCreate(transaction_id id,
269                                     const std::string& iface_name);
270   wifi_error nanDataInterfaceDelete(transaction_id id,
271                                     const std::string& iface_name);
272   wifi_error nanDataRequestInitiator(transaction_id id,
273                                      const NanDataPathInitiatorRequest& msg);
274   wifi_error nanDataIndicationResponse(
275       transaction_id id, const NanDataPathIndicationResponse& msg);
276   wifi_error nanDataEnd(transaction_id id, uint32_t ndpInstanceId);
277   // AP functions.
278   wifi_error setCountryCode(std::array<int8_t, 2> code);
279 
280  private:
281   // Retrieve the interface handle to be used for the "wlan" interface.
282   wifi_error retrieveWlanInterfaceHandle();
283   // Run the legacy HAL event loop thread.
284   void runEventLoop();
285   // Retrieve the cached gscan results to pass the results back to the external
286   // callbacks.
287   std::pair<wifi_error, std::vector<wifi_cached_scan_results>>
288   getGscanCachedResults();
289   void invalidate();
290 
291   // Global function table of legacy HAL.
292   wifi_hal_fn global_func_table_;
293   // Opaque handle to be used for all global operations.
294   wifi_handle global_handle_;
295   // Opaque handle to be used for all wlan0 interface specific operations.
296   wifi_interface_handle wlan_interface_handle_;
297   // Flag to indicate if we have initiated the cleanup of legacy HAL.
298   std::atomic<bool> awaiting_event_loop_termination_;
299   std::condition_variable_any stop_wait_cv_;
300   // Flag to indicate if the legacy HAL has been started.
301   bool is_started_;
302   wifi_system::InterfaceTool iface_tool_;
303 };
304 
305 }  // namespace legacy_hal
306 }  // namespace implementation
307 }  // namespace V1_1
308 }  // namespace wifi
309 }  // namespace hardware
310 }  // namespace android
311 
312 #endif  // WIFI_LEGACY_HAL_H_
313