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 <list>
21 #include <map>
22 
23 #include <android-base/macros.h>
24 #include <android/hardware/wifi/1.3/IWifiChip.h>
25 
26 #include "hidl_callback_util.h"
27 #include "ringbuffer.h"
28 #include "wifi_ap_iface.h"
29 #include "wifi_feature_flags.h"
30 #include "wifi_legacy_hal.h"
31 #include "wifi_mode_controller.h"
32 #include "wifi_nan_iface.h"
33 #include "wifi_p2p_iface.h"
34 #include "wifi_rtt_controller.h"
35 #include "wifi_sta_iface.h"
36 
37 namespace android {
38 namespace hardware {
39 namespace wifi {
40 namespace V1_3 {
41 namespace implementation {
42 using namespace android::hardware::wifi::V1_0;
43 
44 /**
45  * HIDL interface object used to control a Wifi HAL chip instance.
46  * Since there is only a single chip instance used today, there is no
47  * identifying handle information stored here.
48  */
49 class WifiChip : public V1_3::IWifiChip {
50    public:
51     WifiChip(
52         ChipId chip_id,
53         const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
54         const std::weak_ptr<mode_controller::WifiModeController>
55             mode_controller,
56         const std::weak_ptr<iface_util::WifiIfaceUtil> iface_util,
57         const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags);
58     // HIDL does not provide a built-in mechanism to let the server invalidate
59     // a HIDL interface object after creation. If any client process holds onto
60     // a reference to the object in their context, any method calls on that
61     // reference will continue to be directed to the server.
62     //
63     // However Wifi HAL needs to control the lifetime of these objects. So, add
64     // a public |invalidate| method to |WifiChip| and it's child objects. This
65     // will be used to mark an object invalid when either:
66     // a) Wifi HAL is stopped, or
67     // b) Wifi Chip is reconfigured.
68     //
69     // All HIDL method implementations should check if the object is still
70     // marked valid before processing them.
71     void invalidate();
72     bool isValid();
73     std::set<sp<V1_2::IWifiChipEventCallback>> getEventCallbacks();
74 
75     // HIDL methods exposed.
76     Return<void> getId(getId_cb hidl_status_cb) override;
77     // Deprecated support for this callback
78     Return<void> registerEventCallback(
79         const sp<V1_0::IWifiChipEventCallback>& event_callback,
80         registerEventCallback_cb hidl_status_cb) override;
81     Return<void> getCapabilities(getCapabilities_cb hidl_status_cb) override;
82     Return<void> getAvailableModes(
83         getAvailableModes_cb hidl_status_cb) override;
84     Return<void> configureChip(ChipModeId mode_id,
85                                configureChip_cb hidl_status_cb) override;
86     Return<void> getMode(getMode_cb hidl_status_cb) override;
87     Return<void> requestChipDebugInfo(
88         requestChipDebugInfo_cb hidl_status_cb) override;
89     Return<void> requestDriverDebugDump(
90         requestDriverDebugDump_cb hidl_status_cb) override;
91     Return<void> requestFirmwareDebugDump(
92         requestFirmwareDebugDump_cb hidl_status_cb) override;
93     Return<void> createApIface(createApIface_cb hidl_status_cb) override;
94     Return<void> getApIfaceNames(getApIfaceNames_cb hidl_status_cb) override;
95     Return<void> getApIface(const hidl_string& ifname,
96                             getApIface_cb hidl_status_cb) override;
97     Return<void> removeApIface(const hidl_string& ifname,
98                                removeApIface_cb hidl_status_cb) override;
99     Return<void> createNanIface(createNanIface_cb hidl_status_cb) override;
100     Return<void> getNanIfaceNames(getNanIfaceNames_cb hidl_status_cb) override;
101     Return<void> getNanIface(const hidl_string& ifname,
102                              getNanIface_cb hidl_status_cb) override;
103     Return<void> removeNanIface(const hidl_string& ifname,
104                                 removeNanIface_cb hidl_status_cb) override;
105     Return<void> createP2pIface(createP2pIface_cb hidl_status_cb) override;
106     Return<void> getP2pIfaceNames(getP2pIfaceNames_cb hidl_status_cb) override;
107     Return<void> getP2pIface(const hidl_string& ifname,
108                              getP2pIface_cb hidl_status_cb) override;
109     Return<void> removeP2pIface(const hidl_string& ifname,
110                                 removeP2pIface_cb hidl_status_cb) override;
111     Return<void> createStaIface(createStaIface_cb hidl_status_cb) override;
112     Return<void> getStaIfaceNames(getStaIfaceNames_cb hidl_status_cb) override;
113     Return<void> getStaIface(const hidl_string& ifname,
114                              getStaIface_cb hidl_status_cb) override;
115     Return<void> removeStaIface(const hidl_string& ifname,
116                                 removeStaIface_cb hidl_status_cb) override;
117     Return<void> createRttController(
118         const sp<IWifiIface>& bound_iface,
119         createRttController_cb hidl_status_cb) override;
120     Return<void> getDebugRingBuffersStatus(
121         getDebugRingBuffersStatus_cb hidl_status_cb) override;
122     Return<void> startLoggingToDebugRingBuffer(
123         const hidl_string& ring_name,
124         WifiDebugRingBufferVerboseLevel verbose_level,
125         uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes,
126         startLoggingToDebugRingBuffer_cb hidl_status_cb) override;
127     Return<void> forceDumpToDebugRingBuffer(
128         const hidl_string& ring_name,
129         forceDumpToDebugRingBuffer_cb hidl_status_cb) override;
130     Return<void> flushRingBufferToFile(
131         flushRingBufferToFile_cb hidl_status_cb) override;
132     Return<void> stopLoggingToDebugRingBuffer(
133         stopLoggingToDebugRingBuffer_cb hidl_status_cb) override;
134     Return<void> getDebugHostWakeReasonStats(
135         getDebugHostWakeReasonStats_cb hidl_status_cb) override;
136     Return<void> enableDebugErrorAlerts(
137         bool enable, enableDebugErrorAlerts_cb hidl_status_cb) override;
138     Return<void> selectTxPowerScenario(
139         V1_1::IWifiChip::TxPowerScenario scenario,
140         selectTxPowerScenario_cb hidl_status_cb) override;
141     Return<void> resetTxPowerScenario(
142         resetTxPowerScenario_cb hidl_status_cb) override;
143     Return<void> setLatencyMode(LatencyMode mode,
144                                 setLatencyMode_cb hidl_status_cb) override;
145     Return<void> registerEventCallback_1_2(
146         const sp<V1_2::IWifiChipEventCallback>& event_callback,
147         registerEventCallback_1_2_cb hidl_status_cb) override;
148     Return<void> selectTxPowerScenario_1_2(
149         TxPowerScenario scenario,
150         selectTxPowerScenario_cb hidl_status_cb) override;
151     Return<void> getCapabilities_1_3(
152         getCapabilities_cb hidl_status_cb) override;
153     Return<void> debug(const hidl_handle& handle,
154                        const hidl_vec<hidl_string>& options) override;
155 
156    private:
157     void invalidateAndRemoveAllIfaces();
158     // When a STA iface is removed any dependent NAN-ifaces/RTT-controllers are
159     // invalidated & removed.
160     void invalidateAndRemoveDependencies(const std::string& removed_iface_name);
161 
162     // Corresponding worker functions for the HIDL methods.
163     std::pair<WifiStatus, ChipId> getIdInternal();
164     // Deprecated support for this callback
165     WifiStatus registerEventCallbackInternal(
166         const sp<V1_0::IWifiChipEventCallback>& event_callback);
167     std::pair<WifiStatus, uint32_t> getCapabilitiesInternal();
168     std::pair<WifiStatus, std::vector<ChipMode>> getAvailableModesInternal();
169     WifiStatus configureChipInternal(
170         std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id);
171     std::pair<WifiStatus, uint32_t> getModeInternal();
172     std::pair<WifiStatus, IWifiChip::ChipDebugInfo>
173     requestChipDebugInfoInternal();
174     std::pair<WifiStatus, std::vector<uint8_t>>
175     requestDriverDebugDumpInternal();
176     std::pair<WifiStatus, std::vector<uint8_t>>
177     requestFirmwareDebugDumpInternal();
178     std::pair<WifiStatus, sp<IWifiApIface>> createApIfaceInternal();
179     std::pair<WifiStatus, std::vector<hidl_string>> getApIfaceNamesInternal();
180     std::pair<WifiStatus, sp<IWifiApIface>> getApIfaceInternal(
181         const std::string& ifname);
182     WifiStatus removeApIfaceInternal(const std::string& ifname);
183     std::pair<WifiStatus, sp<IWifiNanIface>> createNanIfaceInternal();
184     std::pair<WifiStatus, std::vector<hidl_string>> getNanIfaceNamesInternal();
185     std::pair<WifiStatus, sp<IWifiNanIface>> getNanIfaceInternal(
186         const std::string& ifname);
187     WifiStatus removeNanIfaceInternal(const std::string& ifname);
188     std::pair<WifiStatus, sp<IWifiP2pIface>> createP2pIfaceInternal();
189     std::pair<WifiStatus, std::vector<hidl_string>> getP2pIfaceNamesInternal();
190     std::pair<WifiStatus, sp<IWifiP2pIface>> getP2pIfaceInternal(
191         const std::string& ifname);
192     WifiStatus removeP2pIfaceInternal(const std::string& ifname);
193     std::pair<WifiStatus, sp<IWifiStaIface>> createStaIfaceInternal();
194     std::pair<WifiStatus, std::vector<hidl_string>> getStaIfaceNamesInternal();
195     std::pair<WifiStatus, sp<IWifiStaIface>> getStaIfaceInternal(
196         const std::string& ifname);
197     WifiStatus removeStaIfaceInternal(const std::string& ifname);
198     std::pair<WifiStatus, sp<IWifiRttController>> createRttControllerInternal(
199         const sp<IWifiIface>& bound_iface);
200     std::pair<WifiStatus, std::vector<WifiDebugRingBufferStatus>>
201     getDebugRingBuffersStatusInternal();
202     WifiStatus startLoggingToDebugRingBufferInternal(
203         const hidl_string& ring_name,
204         WifiDebugRingBufferVerboseLevel verbose_level,
205         uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes);
206     WifiStatus forceDumpToDebugRingBufferInternal(const hidl_string& ring_name);
207     WifiStatus flushRingBufferToFileInternal();
208     WifiStatus stopLoggingToDebugRingBufferInternal();
209     std::pair<WifiStatus, WifiDebugHostWakeReasonStats>
210     getDebugHostWakeReasonStatsInternal();
211     WifiStatus enableDebugErrorAlertsInternal(bool enable);
212     WifiStatus selectTxPowerScenarioInternal(
213         V1_1::IWifiChip::TxPowerScenario scenario);
214     WifiStatus resetTxPowerScenarioInternal();
215     WifiStatus setLatencyModeInternal(LatencyMode mode);
216     WifiStatus registerEventCallbackInternal_1_2(
217         const sp<V1_2::IWifiChipEventCallback>& event_callback);
218     WifiStatus selectTxPowerScenarioInternal_1_2(TxPowerScenario scenario);
219     std::pair<WifiStatus, uint32_t> getCapabilitiesInternal_1_3();
220     WifiStatus handleChipConfiguration(
221         std::unique_lock<std::recursive_mutex>* lock, ChipModeId mode_id);
222     WifiStatus registerDebugRingBufferCallback();
223     WifiStatus registerRadioModeChangeCallback();
224 
225     std::vector<IWifiChip::ChipIfaceCombination>
226     getCurrentModeIfaceCombinations();
227     std::map<IfaceType, size_t> getCurrentIfaceCombination();
228     std::vector<std::map<IfaceType, size_t>> expandIfaceCombinations(
229         const IWifiChip::ChipIfaceCombination& combination);
230     bool canExpandedIfaceComboSupportIfaceOfTypeWithCurrentIfaces(
231         const std::map<IfaceType, size_t>& expanded_combo,
232         IfaceType requested_type);
233     bool canCurrentModeSupportIfaceOfTypeWithCurrentIfaces(
234         IfaceType requested_type);
235     bool canExpandedIfaceComboSupportIfaceCombo(
236         const std::map<IfaceType, size_t>& expanded_combo,
237         const std::map<IfaceType, size_t>& req_combo);
238     bool canCurrentModeSupportIfaceCombo(
239         const std::map<IfaceType, size_t>& req_combo);
240     bool canCurrentModeSupportIfaceOfType(IfaceType requested_type);
241     bool isValidModeId(ChipModeId mode_id);
242     bool isStaApConcurrencyAllowedInCurrentMode();
243     bool isDualApAllowedInCurrentMode();
244     std::string getFirstActiveWlanIfaceName();
245     std::string allocateApOrStaIfaceName(uint32_t start_idx);
246     std::string allocateApIfaceName();
247     std::string allocateStaIfaceName();
248     bool writeRingbufferFilesInternal();
249 
250     ChipId chip_id_;
251     std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
252     std::weak_ptr<mode_controller::WifiModeController> mode_controller_;
253     std::weak_ptr<iface_util::WifiIfaceUtil> iface_util_;
254     std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags_;
255     std::vector<sp<WifiApIface>> ap_ifaces_;
256     std::vector<sp<WifiNanIface>> nan_ifaces_;
257     std::vector<sp<WifiP2pIface>> p2p_ifaces_;
258     std::vector<sp<WifiStaIface>> sta_ifaces_;
259     std::vector<sp<WifiRttController>> rtt_controllers_;
260     std::map<std::string, Ringbuffer> ringbuffer_map_;
261     bool is_valid_;
262     // Members pertaining to chip configuration.
263     uint32_t current_mode_id_;
264     std::vector<IWifiChip::ChipMode> modes_;
265     // The legacy ring buffer callback API has only a global callback
266     // registration mechanism. Use this to check if we have already
267     // registered a callback.
268     bool debug_ring_buffer_cb_registered_;
269     hidl_callback_util::HidlCallbackHandler<V1_2::IWifiChipEventCallback>
270         event_cb_handler_;
271 
272     DISALLOW_COPY_AND_ASSIGN(WifiChip);
273 };
274 
275 }  // namespace implementation
276 }  // namespace V1_3
277 }  // namespace wifi
278 }  // namespace hardware
279 }  // namespace android
280 
281 #endif  // WIFI_CHIP_H_
282