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_CHIP_H_
18 #define WIFI_CHIP_H_
19 
20 #include <map>
21 
22 #include <android-base/macros.h>
23 #include <android/hardware/wifi/1.0/IWifiChip.h>
24 
25 #include "hidl_callback_util.h"
26 #include "wifi_ap_iface.h"
27 #include "wifi_legacy_hal.h"
28 #include "wifi_mode_controller.h"
29 #include "wifi_nan_iface.h"
30 #include "wifi_p2p_iface.h"
31 #include "wifi_rtt_controller.h"
32 #include "wifi_sta_iface.h"
33 
34 namespace android {
35 namespace hardware {
36 namespace wifi {
37 namespace V1_0 {
38 namespace implementation {
39 
40 /**
41  * HIDL interface object used to control a Wifi HAL chip instance.
42  * Since there is only a single chip instance used today, there is no
43  * identifying handle information stored here.
44  */
45 class WifiChip : public IWifiChip {
46  public:
47   WifiChip(
48       ChipId chip_id,
49       const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
50       const std::weak_ptr<mode_controller::WifiModeController> mode_controller);
51   // HIDL does not provide a built-in mechanism to let the server invalidate
52   // a HIDL interface object after creation. If any client process holds onto
53   // a reference to the object in their context, any method calls on that
54   // reference will continue to be directed to the server.
55   //
56   // However Wifi HAL needs to control the lifetime of these objects. So, add
57   // a public |invalidate| method to |WifiChip| and it's child objects. This
58   // will be used to mark an object invalid when either:
59   // a) Wifi HAL is stopped, or
60   // b) Wifi Chip is reconfigured.
61   //
62   // All HIDL method implementations should check if the object is still marked
63   // valid before processing them.
64   void invalidate();
65   bool isValid();
66   std::set<sp<IWifiChipEventCallback>> getEventCallbacks();
67 
68   // HIDL methods exposed.
69   Return<void> getId(getId_cb hidl_status_cb) override;
70   Return<void> registerEventCallback(
71       const sp<IWifiChipEventCallback>& event_callback,
72       registerEventCallback_cb hidl_status_cb) override;
73   Return<void> getCapabilities(getCapabilities_cb hidl_status_cb) override;
74   Return<void> getAvailableModes(getAvailableModes_cb hidl_status_cb) override;
75   Return<void> configureChip(ChipModeId mode_id,
76                              configureChip_cb hidl_status_cb) override;
77   Return<void> getMode(getMode_cb hidl_status_cb) override;
78   Return<void> requestChipDebugInfo(
79       requestChipDebugInfo_cb hidl_status_cb) override;
80   Return<void> requestDriverDebugDump(
81       requestDriverDebugDump_cb hidl_status_cb) override;
82   Return<void> requestFirmwareDebugDump(
83       requestFirmwareDebugDump_cb hidl_status_cb) override;
84   Return<void> createApIface(createApIface_cb hidl_status_cb) override;
85   Return<void> getApIfaceNames(getApIfaceNames_cb hidl_status_cb) override;
86   Return<void> getApIface(const hidl_string& ifname,
87                           getApIface_cb hidl_status_cb) override;
88   Return<void> removeApIface(const hidl_string& ifname,
89                              removeApIface_cb hidl_status_cb) override;
90   Return<void> createNanIface(createNanIface_cb hidl_status_cb) override;
91   Return<void> getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) override;
92   Return<void> getNanIface(const hidl_string& ifname,
93                            getNanIface_cb hidl_status_cb) override;
94   Return<void> removeNanIface(const hidl_string& ifname,
95                               removeNanIface_cb hidl_status_cb) override;
96   Return<void> createP2pIface(createP2pIface_cb hidl_status_cb) override;
97   Return<void> getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) override;
98   Return<void> getP2pIface(const hidl_string& ifname,
99                            getP2pIface_cb hidl_status_cb) override;
100   Return<void> removeP2pIface(const hidl_string& ifname,
101                               removeP2pIface_cb hidl_status_cb) override;
102   Return<void> createStaIface(createStaIface_cb hidl_status_cb) override;
103   Return<void> getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) override;
104   Return<void> getStaIface(const hidl_string& ifname,
105                            getStaIface_cb hidl_status_cb) override;
106   Return<void> removeStaIface(const hidl_string& ifname,
107                               removeStaIface_cb hidl_status_cb) override;
108   Return<void> createRttController(
109       const sp<IWifiIface>& bound_iface,
110       createRttController_cb hidl_status_cb) override;
111   Return<void> getDebugRingBuffersStatus(
112       getDebugRingBuffersStatus_cb hidl_status_cb) override;
113   Return<void> startLoggingToDebugRingBuffer(
114       const hidl_string& ring_name,
115       WifiDebugRingBufferVerboseLevel verbose_level,
116       uint32_t max_interval_in_sec,
117       uint32_t min_data_size_in_bytes,
118       startLoggingToDebugRingBuffer_cb hidl_status_cb) override;
119   Return<void> forceDumpToDebugRingBuffer(
120       const hidl_string& ring_name,
121       forceDumpToDebugRingBuffer_cb hidl_status_cb) override;
122   Return<void> stopLoggingToDebugRingBuffer(
123       stopLoggingToDebugRingBuffer_cb hidl_status_cb) override;
124   Return<void> getDebugHostWakeReasonStats(
125       getDebugHostWakeReasonStats_cb hidl_status_cb) override;
126   Return<void> enableDebugErrorAlerts(
127       bool enable, enableDebugErrorAlerts_cb hidl_status_cb) override;
128 
129  private:
130   void invalidateAndRemoveAllIfaces();
131 
132   // Corresponding worker functions for the HIDL methods.
133   std::pair<WifiStatus, ChipId> getIdInternal();
134   WifiStatus registerEventCallbackInternal(
135       const sp<IWifiChipEventCallback>& event_callback);
136   std::pair<WifiStatus, uint32_t> getCapabilitiesInternal();
137   std::pair<WifiStatus, std::vector<ChipMode>> getAvailableModesInternal();
138   WifiStatus configureChipInternal(ChipModeId mode_id);
139   std::pair<WifiStatus, uint32_t> getModeInternal();
140   std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
141   requestChipDebugInfoInternal();
142   std::pair<WifiStatus, std::vector<uint8_t>> requestDriverDebugDumpInternal();
143   std::pair<WifiStatus, std::vector<uint8_t>>
144   requestFirmwareDebugDumpInternal();
145   std::pair<WifiStatus, sp<IWifiApIface>> createApIfaceInternal();
146   std::pair<WifiStatus, std::vector<hidl_string>> getApIfaceNamesInternal();
147   std::pair<WifiStatus, sp<IWifiApIface>> getApIfaceInternal(
148       const std::string& ifname);
149   WifiStatus removeApIfaceInternal(const std::string& ifname);
150   std::pair<WifiStatus, sp<IWifiNanIface>> createNanIfaceInternal();
151   std::pair<WifiStatus, std::vector<hidl_string>> getNanIfaceNamesInternal();
152   std::pair<WifiStatus, sp<IWifiNanIface>> getNanIfaceInternal(
153       const std::string& ifname);
154   WifiStatus removeNanIfaceInternal(const std::string& ifname);
155   std::pair<WifiStatus, sp<IWifiP2pIface>> createP2pIfaceInternal();
156   std::pair<WifiStatus, std::vector<hidl_string>> getP2pIfaceNamesInternal();
157   std::pair<WifiStatus, sp<IWifiP2pIface>> getP2pIfaceInternal(
158       const std::string& ifname);
159   WifiStatus removeP2pIfaceInternal(const std::string& ifname);
160   std::pair<WifiStatus, sp<IWifiStaIface>> createStaIfaceInternal();
161   std::pair<WifiStatus, std::vector<hidl_string>> getStaIfaceNamesInternal();
162   std::pair<WifiStatus, sp<IWifiStaIface>> getStaIfaceInternal(
163       const std::string& ifname);
164   WifiStatus removeStaIfaceInternal(const std::string& ifname);
165   std::pair<WifiStatus, sp<IWifiRttController>> createRttControllerInternal(
166       const sp<IWifiIface>& bound_iface);
167   std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
168   getDebugRingBuffersStatusInternal();
169   WifiStatus startLoggingToDebugRingBufferInternal(
170       const hidl_string& ring_name,
171       WifiDebugRingBufferVerboseLevel verbose_level,
172       uint32_t max_interval_in_sec,
173       uint32_t min_data_size_in_bytes);
174   WifiStatus forceDumpToDebugRingBufferInternal(const hidl_string& ring_name);
175   WifiStatus stopLoggingToDebugRingBufferInternal();
176   std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
177   getDebugHostWakeReasonStatsInternal();
178   WifiStatus enableDebugErrorAlertsInternal(bool enable);
179 
180   WifiStatus handleChipConfiguration(ChipModeId mode_id);
181   WifiStatus registerDebugRingBufferCallback();
182 
183   ChipId chip_id_;
184   std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
185   std::weak_ptr<mode_controller::WifiModeController> mode_controller_;
186   sp<WifiApIface> ap_iface_;
187   sp<WifiNanIface> nan_iface_;
188   sp<WifiP2pIface> p2p_iface_;
189   sp<WifiStaIface> sta_iface_;
190   std::vector<sp<WifiRttController>> rtt_controllers_;
191   bool is_valid_;
192   uint32_t current_mode_id_;
193   // The legacy ring buffer callback API has only a global callback
194   // registration mechanism. Use this to check if we have already
195   // registered a callback.
196   bool debug_ring_buffer_cb_registered_;
197   hidl_callback_util::HidlCallbackHandler<IWifiChipEventCallback>
198       event_cb_handler_;
199 
200   DISALLOW_COPY_AND_ASSIGN(WifiChip);
201 };
202 
203 }  // namespace implementation
204 }  // namespace V1_0
205 }  // namespace wifi
206 }  // namespace hardware
207 }  // namespace android
208 
209 #endif  // WIFI_CHIP_H_
210