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