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