• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <aidl/android/hardware/wifi/BnWifiChip.h>
21 #include <aidl/android/hardware/wifi/IWifiRttController.h>
22 #include <aidl/android/hardware/wifi/common/OuiKeyedData.h>
23 #include <android-base/macros.h>
24 
25 #include <list>
26 #include <map>
27 #include <mutex>
28 
29 #include "aidl_callback_util.h"
30 #include "ringbuffer.h"
31 #include "wifi_ap_iface.h"
32 #include "wifi_feature_flags.h"
33 #include "wifi_legacy_hal.h"
34 #include "wifi_mode_controller.h"
35 #include "wifi_nan_iface.h"
36 #include "wifi_p2p_iface.h"
37 #include "wifi_rtt_controller.h"
38 #include "wifi_sta_iface.h"
39 
40 namespace aidl {
41 namespace android {
42 namespace hardware {
43 namespace wifi {
44 
45 /**
46  * AIDL interface object used to control a Wifi HAL chip instance.
47  * Since there is only a single chip instance used today, there is no
48  * identifying handle information stored here.
49  */
50 class WifiChip : public BnWifiChip {
51   public:
52     WifiChip(int32_t chip_id, bool is_primary,
53              const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
54              const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
55              const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util,
56              const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
57              const std::function<void(const std::string&)>& subsystemCallbackHandler,
58              bool using_dynamic_iface_combination);
59 
60     // Factory method - use instead of default constructor.
61     static std::shared_ptr<WifiChip> create(
62             int32_t chip_id, bool is_primary,
63             const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
64             const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
65             const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util,
66             const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
67             const std::function<void(const std::string&)>& subsystemCallbackHandler,
68             bool using_dynamic_iface_combination);
69 
70     // AIDL does not provide a built-in mechanism to let the server invalidate
71     // an AIDL interface object after creation. If any client process holds onto
72     // a reference to the object in their context, any method calls on that
73     // reference will continue to be directed to the server.
74     //
75     // However Wifi HAL needs to control the lifetime of these objects. So, add
76     // a public |invalidate| method to |WifiChip| and its child objects. This
77     // will be used to mark an object invalid when either:
78     // a) Wifi HAL is stopped, or
79     // b) Wifi Chip is reconfigured.
80     //
81     // All AIDL method implementations should check if the object is still
82     // marked valid before processing them.
83     void invalidate();
84     bool isValid();
85     std::set<std::shared_ptr<IWifiChipEventCallback>> getEventCallbacks();
86 
87     // AIDL methods exposed.
88     ndk::ScopedAStatus getId(int32_t* _aidl_return) override;
89     ndk::ScopedAStatus registerEventCallback(
90             const std::shared_ptr<IWifiChipEventCallback>& in_callback) override;
91     ndk::ScopedAStatus getFeatureSet(int32_t* _aidl_return) override;
92     ndk::ScopedAStatus getAvailableModes(std::vector<IWifiChip::ChipMode>* _aidl_return) override;
93     ndk::ScopedAStatus configureChip(int32_t in_modeId) override;
94     ndk::ScopedAStatus getMode(int32_t* _aidl_return) override;
95     ndk::ScopedAStatus requestChipDebugInfo(IWifiChip::ChipDebugInfo* _aidl_return) override;
96     ndk::ScopedAStatus requestDriverDebugDump(std::vector<uint8_t>* _aidl_return) override;
97     ndk::ScopedAStatus requestFirmwareDebugDump(std::vector<uint8_t>* _aidl_return) override;
98     ndk::ScopedAStatus createApIface(std::shared_ptr<IWifiApIface>* _aidl_return) override;
99     ndk::ScopedAStatus createBridgedApIface(std::shared_ptr<IWifiApIface>* _aidl_return) override;
100     ndk::ScopedAStatus createApOrBridgedApIface(
101             IfaceConcurrencyType in_ifaceType,
102             const std::vector<common::OuiKeyedData>& in_vendorData,
103             std::shared_ptr<IWifiApIface>* _aidl_return) override;
104     ndk::ScopedAStatus getApIfaceNames(std::vector<std::string>* _aidl_return) override;
105     ndk::ScopedAStatus getApIface(const std::string& in_ifname,
106                                   std::shared_ptr<IWifiApIface>* _aidl_return) override;
107     ndk::ScopedAStatus removeApIface(const std::string& in_ifname) override;
108     ndk::ScopedAStatus removeIfaceInstanceFromBridgedApIface(
109             const std::string& in_brIfaceName, const std::string& in_ifaceInstanceName) override;
110     ndk::ScopedAStatus createNanIface(std::shared_ptr<IWifiNanIface>* _aidl_return) override;
111     ndk::ScopedAStatus getNanIfaceNames(std::vector<std::string>* _aidl_return) override;
112     ndk::ScopedAStatus getNanIface(const std::string& in_ifname,
113                                    std::shared_ptr<IWifiNanIface>* _aidl_return) override;
114     ndk::ScopedAStatus removeNanIface(const std::string& in_ifname) override;
115     ndk::ScopedAStatus createP2pIface(std::shared_ptr<IWifiP2pIface>* _aidl_return) override;
116     ndk::ScopedAStatus getP2pIfaceNames(std::vector<std::string>* _aidl_return) override;
117     ndk::ScopedAStatus getP2pIface(const std::string& in_ifname,
118                                    std::shared_ptr<IWifiP2pIface>* _aidl_return) override;
119     ndk::ScopedAStatus removeP2pIface(const std::string& in_ifname) override;
120     ndk::ScopedAStatus createStaIface(std::shared_ptr<IWifiStaIface>* _aidl_return) override;
121     ndk::ScopedAStatus getStaIfaceNames(std::vector<std::string>* _aidl_return) override;
122     ndk::ScopedAStatus getStaIface(const std::string& in_ifname,
123                                    std::shared_ptr<IWifiStaIface>* _aidl_return) override;
124     ndk::ScopedAStatus removeStaIface(const std::string& in_ifname) override;
125     ndk::ScopedAStatus createRttController(
126             const std::shared_ptr<IWifiStaIface>& in_boundIface,
127             std::shared_ptr<IWifiRttController>* _aidl_return) override;
128     ndk::ScopedAStatus getDebugRingBuffersStatus(
129             std::vector<WifiDebugRingBufferStatus>* _aidl_return) override;
130     ndk::ScopedAStatus startLoggingToDebugRingBuffer(
131             const std::string& in_ringName, WifiDebugRingBufferVerboseLevel in_verboseLevel,
132             int32_t in_maxIntervalInSec, int32_t in_minDataSizeInBytes) override;
133     ndk::ScopedAStatus forceDumpToDebugRingBuffer(const std::string& in_ringName) override;
134     ndk::ScopedAStatus flushRingBufferToFile() override;
135     ndk::ScopedAStatus stopLoggingToDebugRingBuffer() override;
136     ndk::ScopedAStatus getDebugHostWakeReasonStats(
137             WifiDebugHostWakeReasonStats* _aidl_return) override;
138     ndk::ScopedAStatus enableDebugErrorAlerts(bool in_enable) override;
139     ndk::ScopedAStatus selectTxPowerScenario(IWifiChip::TxPowerScenario in_scenario) override;
140     ndk::ScopedAStatus resetTxPowerScenario() override;
141     ndk::ScopedAStatus setLatencyMode(IWifiChip::LatencyMode in_mode) override;
142     ndk::ScopedAStatus setMultiStaPrimaryConnection(const std::string& in_ifName) override;
143     ndk::ScopedAStatus setMultiStaUseCase(IWifiChip::MultiStaUseCase in_useCase) override;
144     ndk::ScopedAStatus setCoexUnsafeChannels(
145             const std::vector<IWifiChip::CoexUnsafeChannel>& in_unsafeChannels,
146             int32_t in_restrictions) override;
147     ndk::ScopedAStatus setCountryCode(const std::array<uint8_t, 2>& in_code) override;
148     ndk::ScopedAStatus getUsableChannels(WifiBand in_band, int32_t in_ifaceModeMask,
149                                          int32_t in_filterMask,
150                                          std::vector<WifiUsableChannel>* _aidl_return) override;
151     ndk::ScopedAStatus setAfcChannelAllowance(
152             const AfcChannelAllowance& afcChannelAllowance) override;
153     ndk::ScopedAStatus triggerSubsystemRestart() override;
154     ndk::ScopedAStatus getSupportedRadioCombinations(
155             std::vector<WifiRadioCombination>* _aidl_return) override;
156     ndk::ScopedAStatus getWifiChipCapabilities(WifiChipCapabilities* _aidl_return) override;
157     ndk::ScopedAStatus enableStaChannelForPeerNetwork(
158             int32_t in_channelCategoryEnableFlag) override;
159     binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
160     ndk::ScopedAStatus setMloMode(const ChipMloMode in_mode) override;
161     ndk::ScopedAStatus setVoipMode(const VoipMode in_mode) override;
162 
163   private:
164     void invalidateAndRemoveAllIfaces();
165     // When a STA iface is removed any dependent NAN-ifaces/RTT-controllers are
166     // invalidated & removed.
167     void invalidateAndRemoveDependencies(const std::string& removed_iface_name);
168 
169     // Corresponding worker functions for the AIDL methods.
170     std::pair<int32_t, ndk::ScopedAStatus> getIdInternal();
171     ndk::ScopedAStatus registerEventCallbackInternal(
172             const std::shared_ptr<IWifiChipEventCallback>& event_callback);
173     std::pair<int32_t, ndk::ScopedAStatus> getFeatureSetInternal();
174     std::pair<std::vector<IWifiChip::ChipMode>, ndk::ScopedAStatus> getAvailableModesInternal();
175     ndk::ScopedAStatus configureChipInternal(std::unique_lock<std::recursive_mutex>* lock,
176                                              int32_t mode_id);
177     std::pair<int32_t, ndk::ScopedAStatus> getModeInternal();
178     std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> requestChipDebugInfoInternal();
179     std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestDriverDebugDumpInternal();
180     std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestFirmwareDebugDumpInternal();
181     std::shared_ptr<WifiApIface> newWifiApIface(std::string& ifname);
182     ndk::ScopedAStatus createVirtualApInterface(const std::string& apVirtIf);
183     std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApIfaceInternal();
184     std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createBridgedApIfaceInternal();
185     std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApOrBridgedApIfaceInternal(
186             IfaceConcurrencyType ifaceType, const std::vector<common::OuiKeyedData>& vendorData);
187     std::pair<std::vector<std::string>, ndk::ScopedAStatus> getApIfaceNamesInternal();
188     std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> getApIfaceInternal(
189             const std::string& ifname);
190     ndk::ScopedAStatus removeApIfaceInternal(const std::string& ifname);
191     ndk::ScopedAStatus removeIfaceInstanceFromBridgedApIfaceInternal(
192             const std::string& brIfaceName, const std::string& ifInstanceName);
193     std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> createNanIfaceInternal();
194     std::pair<std::vector<std::string>, ndk::ScopedAStatus> getNanIfaceNamesInternal();
195     std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> getNanIfaceInternal(
196             const std::string& ifname);
197     ndk::ScopedAStatus removeNanIfaceInternal(const std::string& ifname);
198     std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> createP2pIfaceInternal();
199     std::pair<std::vector<std::string>, ndk::ScopedAStatus> getP2pIfaceNamesInternal();
200     std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> getP2pIfaceInternal(
201             const std::string& ifname);
202     ndk::ScopedAStatus removeP2pIfaceInternal(const std::string& ifname);
203     std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> createStaIfaceInternal();
204     std::pair<std::vector<std::string>, ndk::ScopedAStatus> getStaIfaceNamesInternal();
205     std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> getStaIfaceInternal(
206             const std::string& ifname);
207     ndk::ScopedAStatus removeStaIfaceInternal(const std::string& ifname);
208     std::pair<std::shared_ptr<IWifiRttController>, ndk::ScopedAStatus> createRttControllerInternal(
209             const std::shared_ptr<IWifiStaIface>& bound_iface);
210     std::pair<std::vector<WifiDebugRingBufferStatus>, ndk::ScopedAStatus>
211     getDebugRingBuffersStatusInternal();
212     ndk::ScopedAStatus startLoggingToDebugRingBufferInternal(
213             const std::string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
214             uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes);
215     ndk::ScopedAStatus forceDumpToDebugRingBufferInternal(const std::string& ring_name);
216     ndk::ScopedAStatus flushRingBufferToFileInternal();
217     ndk::ScopedAStatus stopLoggingToDebugRingBufferInternal();
218     std::pair<WifiDebugHostWakeReasonStats, ndk::ScopedAStatus>
219     getDebugHostWakeReasonStatsInternal();
220     ndk::ScopedAStatus enableDebugErrorAlertsInternal(bool enable);
221     ndk::ScopedAStatus selectTxPowerScenarioInternal(IWifiChip::TxPowerScenario scenario);
222     ndk::ScopedAStatus resetTxPowerScenarioInternal();
223     ndk::ScopedAStatus setLatencyModeInternal(IWifiChip::LatencyMode mode);
224     ndk::ScopedAStatus setMultiStaPrimaryConnectionInternal(const std::string& ifname);
225     ndk::ScopedAStatus setMultiStaUseCaseInternal(IWifiChip::MultiStaUseCase use_case);
226     ndk::ScopedAStatus setCoexUnsafeChannelsInternal(
227             std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, int32_t restrictions);
228     ndk::ScopedAStatus setCountryCodeInternal(const std::array<uint8_t, 2>& in_code);
229     std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> getUsableChannelsInternal(
230             WifiBand band, int32_t ifaceModeMask, int32_t filterMask);
231     ndk::ScopedAStatus enableStaChannelForPeerNetworkInternal(int32_t channelCategoryEnableFlag);
232     ndk::ScopedAStatus setAfcChannelAllowanceInternal(
233             const AfcChannelAllowance& afcChannelAllowance);
234     ndk::ScopedAStatus handleChipConfiguration(std::unique_lock<std::recursive_mutex>* lock,
235                                                int32_t mode_id);
236     ndk::ScopedAStatus registerDebugRingBufferCallback();
237     ndk::ScopedAStatus registerRadioModeChangeCallback();
238 
239     std::vector<ChipConcurrencyCombination> getCurrentModeConcurrencyCombinations();
240     std::map<IfaceConcurrencyType, size_t> getCurrentConcurrencyCombination();
241     std::vector<std::map<IfaceConcurrencyType, size_t>> expandConcurrencyCombinations(
242             const ChipConcurrencyCombination& combination);
243     bool canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(
244             const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
245             IfaceConcurrencyType requested_type);
246     bool canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType requested_type);
247     bool canExpandedConcurrencyComboSupportConcurrencyCombo(
248             const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
249             const std::map<IfaceConcurrencyType, size_t>& req_combo);
250     bool canCurrentModeSupportConcurrencyCombo(
251             const std::map<IfaceConcurrencyType, size_t>& req_combo);
252     bool canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type);
253 
254     bool isValidModeId(int32_t mode_id);
255     bool isStaApConcurrencyAllowedInCurrentMode();
256     bool isDualStaConcurrencyAllowedInCurrentMode();
257     uint32_t startIdxOfApIface();
258     std::string getFirstActiveWlanIfaceName();
259     std::string allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx);
260     std::string allocateApIfaceName();
261     std::vector<std::string> allocateBridgedApInstanceNames();
262     std::string allocateStaIfaceName();
263     bool writeRingbufferFilesInternal();
264     std::string getWlanIfaceNameWithType(IfaceType type, unsigned idx);
265     void invalidateAndClearBridgedApAll();
266     void deleteApIface(const std::string& if_name);
267     bool findUsingNameFromBridgedApInstances(const std::string& name);
268     ndk::ScopedAStatus triggerSubsystemRestartInternal();
269     std::pair<std::vector<WifiRadioCombination>, ndk::ScopedAStatus>
270     getSupportedRadioCombinationsInternal();
271     std::pair<WifiChipCapabilities, ndk::ScopedAStatus> getWifiChipCapabilitiesInternal();
272     ndk::ScopedAStatus setMloModeInternal(const ChipMloMode in_mode);
273     ndk::ScopedAStatus setVoipModeInternal(const VoipMode in_mode);
274     void retrieveDynamicIfaceCombination();
275     void setWeakPtr(std::weak_ptr<WifiChip> ptr);
276 
277     int32_t chip_id_;
278     std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
279     std::weak_ptr<mode_controller::WifiModeController> mode_controller_;
280     std::shared_ptr<iface_util::WifiIfaceUtil> iface_util_;
281     std::vector<std::shared_ptr<WifiApIface>> ap_ifaces_;
282     std::vector<std::shared_ptr<WifiNanIface>> nan_ifaces_;
283     std::vector<std::shared_ptr<WifiP2pIface>> p2p_ifaces_;
284     std::vector<std::shared_ptr<WifiStaIface>> sta_ifaces_;
285     std::vector<std::shared_ptr<WifiRttController>> rtt_controllers_;
286     std::map<std::string, Ringbuffer> ringbuffer_map_;
287     bool is_valid_;
288     // Members pertaining to chip configuration.
289     int32_t current_mode_id_;
290     std::mutex lock_t;
291     std::vector<IWifiChip::ChipMode> modes_;
292     // The legacy ring buffer callback API has only a global callback
293     // registration mechanism. Use this to check if we have already
294     // registered a callback.
295     bool debug_ring_buffer_cb_registered_;
296     bool using_dynamic_iface_combination_;
297     aidl_callback_util::AidlCallbackHandler<IWifiChipEventCallback> event_cb_handler_;
298     std::weak_ptr<WifiChip> weak_ptr_this_;
299 
300     const std::function<void(const std::string&)> subsystemCallbackHandler_;
301     std::map<std::string, std::vector<std::string>> br_ifaces_ap_instances_;
302     DISALLOW_COPY_AND_ASSIGN(WifiChip);
303 };
304 
305 }  // namespace wifi
306 }  // namespace hardware
307 }  // namespace android
308 }  // namespace aidl
309 
310 #endif  // WIFI_CHIP_H_
311