blob: ffd507f801370ae9921d9580388031ee4ddf258a [file] [log] [blame]
Gabriel Birenf3262f92022-07-15 23:25:39 +00001/*
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>
Gabriel Birenaa9bd832023-11-10 00:43:08 +000022#include <aidl/android/hardware/wifi/common/OuiKeyedData.h>
Gabriel Birenf3262f92022-07-15 23:25:39 +000023#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
40namespace aidl {
41namespace android {
42namespace hardware {
43namespace 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 */
50class 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,
Gabriel Biren989c78a2023-06-14 22:42:07 +000057 const std::function<void(const std::string&)>& subsystemCallbackHandler,
58 bool using_dynamic_iface_combination);
Gabriel Birenf3262f92022-07-15 23:25:39 +000059
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,
Gabriel Biren989c78a2023-06-14 22:42:07 +000067 const std::function<void(const std::string&)>& subsystemCallbackHandler,
68 bool using_dynamic_iface_combination);
Gabriel Birenf3262f92022-07-15 23:25:39 +000069
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;
Gabriel Biren2f862492023-03-09 19:13:07 +000091 ndk::ScopedAStatus getFeatureSet(int32_t* _aidl_return) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +000092 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;
Gabriel Birenaa9bd832023-11-10 00:43:08 +0000100 ndk::ScopedAStatus createApOrBridgedApIface(
101 IfaceConcurrencyType in_ifaceType,
102 const std::vector<common::OuiKeyedData>& in_vendorData,
103 std::shared_ptr<IWifiApIface>* _aidl_return) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000104 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,
Gabriel Biren3b86a782023-02-04 00:42:53 +0000146 int32_t in_restrictions) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000147 ndk::ScopedAStatus setCountryCode(const std::array<uint8_t, 2>& in_code) override;
Gabriel Biren3b86a782023-02-04 00:42:53 +0000148 ndk::ScopedAStatus getUsableChannels(WifiBand in_band, int32_t in_ifaceModeMask,
149 int32_t in_filterMask,
Gabriel Birenf3262f92022-07-15 23:25:39 +0000150 std::vector<WifiUsableChannel>* _aidl_return) override;
Oscar Shuab8313c2022-12-13 00:55:11 +0000151 ndk::ScopedAStatus setAfcChannelAllowance(
Oscar Shu4275c872023-03-08 22:48:09 +0000152 const AfcChannelAllowance& afcChannelAllowance) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000153 ndk::ScopedAStatus triggerSubsystemRestart() override;
Gabriel Biren263db452023-02-24 21:07:38 +0000154 ndk::ScopedAStatus getSupportedRadioCombinations(
155 std::vector<WifiRadioCombination>* _aidl_return) override;
Mahesh KKVc84d3772022-12-02 16:53:28 -0800156 ndk::ScopedAStatus getWifiChipCapabilities(WifiChipCapabilities* _aidl_return) override;
Shuibing Daie5fbcab2022-12-19 15:37:19 -0800157 ndk::ScopedAStatus enableStaChannelForPeerNetwork(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000158 int32_t in_channelCategoryEnableFlag) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000159 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
maheshkkva8aba172023-02-13 12:33:26 -0800160 ndk::ScopedAStatus setMloMode(const ChipMloMode in_mode) override;
Les Lee8efe3752023-11-10 02:51:56 +0000161 ndk::ScopedAStatus setVoipMode(const VoipMode in_mode) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000162
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);
Gabriel Biren2f862492023-03-09 19:13:07 +0000173 std::pair<int32_t, ndk::ScopedAStatus> getFeatureSetInternal();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000174 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();
Gabriel Birenaa9bd832023-11-10 00:43:08 +0000185 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApOrBridgedApIfaceInternal(
186 IfaceConcurrencyType ifaceType, const std::vector<common::OuiKeyedData>& vendorData);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000187 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(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000227 std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, int32_t restrictions);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000228 ndk::ScopedAStatus setCountryCodeInternal(const std::array<uint8_t, 2>& in_code);
229 std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> getUsableChannelsInternal(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000230 WifiBand band, int32_t ifaceModeMask, int32_t filterMask);
231 ndk::ScopedAStatus enableStaChannelForPeerNetworkInternal(int32_t channelCategoryEnableFlag);
Oscar Shuab8313c2022-12-13 00:55:11 +0000232 ndk::ScopedAStatus setAfcChannelAllowanceInternal(
Oscar Shu4275c872023-03-08 22:48:09 +0000233 const AfcChannelAllowance& afcChannelAllowance);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000234 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();
Sunil Ravi780bef02023-06-01 21:43:04 +0000266 void deleteApIface(const std::string& if_name);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000267 bool findUsingNameFromBridgedApInstances(const std::string& name);
268 ndk::ScopedAStatus triggerSubsystemRestartInternal();
Gabriel Biren263db452023-02-24 21:07:38 +0000269 std::pair<std::vector<WifiRadioCombination>, ndk::ScopedAStatus>
270 getSupportedRadioCombinationsInternal();
Mahesh KKVc84d3772022-12-02 16:53:28 -0800271 std::pair<WifiChipCapabilities, ndk::ScopedAStatus> getWifiChipCapabilitiesInternal();
maheshkkva8aba172023-02-13 12:33:26 -0800272 ndk::ScopedAStatus setMloModeInternal(const ChipMloMode in_mode);
Les Lee8efe3752023-11-10 02:51:56 +0000273 ndk::ScopedAStatus setVoipModeInternal(const VoipMode in_mode);
Sunil Ravi2be1f262023-02-15 20:56:56 +0000274 void retrieveDynamicIfaceCombination();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000275 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_;
Sunil Ravi2be1f262023-02-15 20:56:56 +0000296 bool using_dynamic_iface_combination_;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000297 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_