blob: c6517db6f94d5122764065bebc0cd5ae9dbe0455 [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;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000161
162 private:
163 void invalidateAndRemoveAllIfaces();
164 // When a STA iface is removed any dependent NAN-ifaces/RTT-controllers are
165 // invalidated & removed.
166 void invalidateAndRemoveDependencies(const std::string& removed_iface_name);
167
168 // Corresponding worker functions for the AIDL methods.
169 std::pair<int32_t, ndk::ScopedAStatus> getIdInternal();
170 ndk::ScopedAStatus registerEventCallbackInternal(
171 const std::shared_ptr<IWifiChipEventCallback>& event_callback);
Gabriel Biren2f862492023-03-09 19:13:07 +0000172 std::pair<int32_t, ndk::ScopedAStatus> getFeatureSetInternal();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000173 std::pair<std::vector<IWifiChip::ChipMode>, ndk::ScopedAStatus> getAvailableModesInternal();
174 ndk::ScopedAStatus configureChipInternal(std::unique_lock<std::recursive_mutex>* lock,
175 int32_t mode_id);
176 std::pair<int32_t, ndk::ScopedAStatus> getModeInternal();
177 std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> requestChipDebugInfoInternal();
178 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestDriverDebugDumpInternal();
179 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestFirmwareDebugDumpInternal();
180 std::shared_ptr<WifiApIface> newWifiApIface(std::string& ifname);
181 ndk::ScopedAStatus createVirtualApInterface(const std::string& apVirtIf);
182 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApIfaceInternal();
183 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createBridgedApIfaceInternal();
Gabriel Birenaa9bd832023-11-10 00:43:08 +0000184 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApOrBridgedApIfaceInternal(
185 IfaceConcurrencyType ifaceType, const std::vector<common::OuiKeyedData>& vendorData);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000186 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getApIfaceNamesInternal();
187 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> getApIfaceInternal(
188 const std::string& ifname);
189 ndk::ScopedAStatus removeApIfaceInternal(const std::string& ifname);
190 ndk::ScopedAStatus removeIfaceInstanceFromBridgedApIfaceInternal(
191 const std::string& brIfaceName, const std::string& ifInstanceName);
192 std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> createNanIfaceInternal();
193 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getNanIfaceNamesInternal();
194 std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> getNanIfaceInternal(
195 const std::string& ifname);
196 ndk::ScopedAStatus removeNanIfaceInternal(const std::string& ifname);
197 std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> createP2pIfaceInternal();
198 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getP2pIfaceNamesInternal();
199 std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> getP2pIfaceInternal(
200 const std::string& ifname);
201 ndk::ScopedAStatus removeP2pIfaceInternal(const std::string& ifname);
202 std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> createStaIfaceInternal();
203 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getStaIfaceNamesInternal();
204 std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> getStaIfaceInternal(
205 const std::string& ifname);
206 ndk::ScopedAStatus removeStaIfaceInternal(const std::string& ifname);
207 std::pair<std::shared_ptr<IWifiRttController>, ndk::ScopedAStatus> createRttControllerInternal(
208 const std::shared_ptr<IWifiStaIface>& bound_iface);
209 std::pair<std::vector<WifiDebugRingBufferStatus>, ndk::ScopedAStatus>
210 getDebugRingBuffersStatusInternal();
211 ndk::ScopedAStatus startLoggingToDebugRingBufferInternal(
212 const std::string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
213 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes);
214 ndk::ScopedAStatus forceDumpToDebugRingBufferInternal(const std::string& ring_name);
215 ndk::ScopedAStatus flushRingBufferToFileInternal();
216 ndk::ScopedAStatus stopLoggingToDebugRingBufferInternal();
217 std::pair<WifiDebugHostWakeReasonStats, ndk::ScopedAStatus>
218 getDebugHostWakeReasonStatsInternal();
219 ndk::ScopedAStatus enableDebugErrorAlertsInternal(bool enable);
220 ndk::ScopedAStatus selectTxPowerScenarioInternal(IWifiChip::TxPowerScenario scenario);
221 ndk::ScopedAStatus resetTxPowerScenarioInternal();
222 ndk::ScopedAStatus setLatencyModeInternal(IWifiChip::LatencyMode mode);
223 ndk::ScopedAStatus setMultiStaPrimaryConnectionInternal(const std::string& ifname);
224 ndk::ScopedAStatus setMultiStaUseCaseInternal(IWifiChip::MultiStaUseCase use_case);
225 ndk::ScopedAStatus setCoexUnsafeChannelsInternal(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000226 std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, int32_t restrictions);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000227 ndk::ScopedAStatus setCountryCodeInternal(const std::array<uint8_t, 2>& in_code);
228 std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> getUsableChannelsInternal(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000229 WifiBand band, int32_t ifaceModeMask, int32_t filterMask);
230 ndk::ScopedAStatus enableStaChannelForPeerNetworkInternal(int32_t channelCategoryEnableFlag);
Oscar Shuab8313c2022-12-13 00:55:11 +0000231 ndk::ScopedAStatus setAfcChannelAllowanceInternal(
Oscar Shu4275c872023-03-08 22:48:09 +0000232 const AfcChannelAllowance& afcChannelAllowance);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000233 ndk::ScopedAStatus handleChipConfiguration(std::unique_lock<std::recursive_mutex>* lock,
234 int32_t mode_id);
235 ndk::ScopedAStatus registerDebugRingBufferCallback();
236 ndk::ScopedAStatus registerRadioModeChangeCallback();
237
238 std::vector<ChipConcurrencyCombination> getCurrentModeConcurrencyCombinations();
239 std::map<IfaceConcurrencyType, size_t> getCurrentConcurrencyCombination();
240 std::vector<std::map<IfaceConcurrencyType, size_t>> expandConcurrencyCombinations(
241 const ChipConcurrencyCombination& combination);
242 bool canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(
243 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
244 IfaceConcurrencyType requested_type);
245 bool canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType requested_type);
246 bool canExpandedConcurrencyComboSupportConcurrencyCombo(
247 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
248 const std::map<IfaceConcurrencyType, size_t>& req_combo);
249 bool canCurrentModeSupportConcurrencyCombo(
250 const std::map<IfaceConcurrencyType, size_t>& req_combo);
251 bool canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type);
252
253 bool isValidModeId(int32_t mode_id);
254 bool isStaApConcurrencyAllowedInCurrentMode();
255 bool isDualStaConcurrencyAllowedInCurrentMode();
256 uint32_t startIdxOfApIface();
257 std::string getFirstActiveWlanIfaceName();
258 std::string allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx);
259 std::string allocateApIfaceName();
260 std::vector<std::string> allocateBridgedApInstanceNames();
261 std::string allocateStaIfaceName();
262 bool writeRingbufferFilesInternal();
263 std::string getWlanIfaceNameWithType(IfaceType type, unsigned idx);
264 void invalidateAndClearBridgedApAll();
Sunil Ravi780bef02023-06-01 21:43:04 +0000265 void deleteApIface(const std::string& if_name);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000266 bool findUsingNameFromBridgedApInstances(const std::string& name);
267 ndk::ScopedAStatus triggerSubsystemRestartInternal();
Gabriel Biren263db452023-02-24 21:07:38 +0000268 std::pair<std::vector<WifiRadioCombination>, ndk::ScopedAStatus>
269 getSupportedRadioCombinationsInternal();
Mahesh KKVc84d3772022-12-02 16:53:28 -0800270 std::pair<WifiChipCapabilities, ndk::ScopedAStatus> getWifiChipCapabilitiesInternal();
maheshkkva8aba172023-02-13 12:33:26 -0800271 ndk::ScopedAStatus setMloModeInternal(const ChipMloMode in_mode);
Sunil Ravi2be1f262023-02-15 20:56:56 +0000272 void retrieveDynamicIfaceCombination();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000273 void setWeakPtr(std::weak_ptr<WifiChip> ptr);
274
275 int32_t chip_id_;
276 std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
277 std::weak_ptr<mode_controller::WifiModeController> mode_controller_;
278 std::shared_ptr<iface_util::WifiIfaceUtil> iface_util_;
279 std::vector<std::shared_ptr<WifiApIface>> ap_ifaces_;
280 std::vector<std::shared_ptr<WifiNanIface>> nan_ifaces_;
281 std::vector<std::shared_ptr<WifiP2pIface>> p2p_ifaces_;
282 std::vector<std::shared_ptr<WifiStaIface>> sta_ifaces_;
283 std::vector<std::shared_ptr<WifiRttController>> rtt_controllers_;
284 std::map<std::string, Ringbuffer> ringbuffer_map_;
285 bool is_valid_;
286 // Members pertaining to chip configuration.
287 int32_t current_mode_id_;
288 std::mutex lock_t;
289 std::vector<IWifiChip::ChipMode> modes_;
290 // The legacy ring buffer callback API has only a global callback
291 // registration mechanism. Use this to check if we have already
292 // registered a callback.
293 bool debug_ring_buffer_cb_registered_;
Sunil Ravi2be1f262023-02-15 20:56:56 +0000294 bool using_dynamic_iface_combination_;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000295 aidl_callback_util::AidlCallbackHandler<IWifiChipEventCallback> event_cb_handler_;
296 std::weak_ptr<WifiChip> weak_ptr_this_;
297
298 const std::function<void(const std::string&)> subsystemCallbackHandler_;
299 std::map<std::string, std::vector<std::string>> br_ifaces_ap_instances_;
300 DISALLOW_COPY_AND_ASSIGN(WifiChip);
301};
302
303} // namespace wifi
304} // namespace hardware
305} // namespace android
306} // namespace aidl
307
308#endif // WIFI_CHIP_H_