blob: 1a360320c9ec34fe1ea633ae4d714d98c224741b [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>
22#include <android-base/macros.h>
23
24#include <list>
25#include <map>
26#include <mutex>
27
28#include "aidl_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
39namespace aidl {
40namespace android {
41namespace hardware {
42namespace wifi {
43
44/**
45 * AIDL 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 */
49class WifiChip : public BnWifiChip {
50 public:
51 WifiChip(int32_t chip_id, bool is_primary,
52 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
53 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
54 const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util,
55 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
Gabriel Biren989c78a2023-06-14 22:42:07 +000056 const std::function<void(const std::string&)>& subsystemCallbackHandler,
57 bool using_dynamic_iface_combination);
Gabriel Birenf3262f92022-07-15 23:25:39 +000058
59 // Factory method - use instead of default constructor.
60 static std::shared_ptr<WifiChip> create(
61 int32_t chip_id, bool is_primary,
62 const std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal,
63 const std::weak_ptr<mode_controller::WifiModeController> mode_controller,
64 const std::shared_ptr<iface_util::WifiIfaceUtil> iface_util,
65 const std::weak_ptr<feature_flags::WifiFeatureFlags> feature_flags,
Gabriel Biren989c78a2023-06-14 22:42:07 +000066 const std::function<void(const std::string&)>& subsystemCallbackHandler,
67 bool using_dynamic_iface_combination);
Gabriel Birenf3262f92022-07-15 23:25:39 +000068
69 // AIDL does not provide a built-in mechanism to let the server invalidate
70 // an AIDL interface object after creation. If any client process holds onto
71 // a reference to the object in their context, any method calls on that
72 // reference will continue to be directed to the server.
73 //
74 // However Wifi HAL needs to control the lifetime of these objects. So, add
75 // a public |invalidate| method to |WifiChip| and its child objects. This
76 // will be used to mark an object invalid when either:
77 // a) Wifi HAL is stopped, or
78 // b) Wifi Chip is reconfigured.
79 //
80 // All AIDL method implementations should check if the object is still
81 // marked valid before processing them.
82 void invalidate();
83 bool isValid();
84 std::set<std::shared_ptr<IWifiChipEventCallback>> getEventCallbacks();
85
86 // AIDL methods exposed.
87 ndk::ScopedAStatus getId(int32_t* _aidl_return) override;
88 ndk::ScopedAStatus registerEventCallback(
89 const std::shared_ptr<IWifiChipEventCallback>& in_callback) override;
Gabriel Biren2f862492023-03-09 19:13:07 +000090 ndk::ScopedAStatus getFeatureSet(int32_t* _aidl_return) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +000091 ndk::ScopedAStatus getAvailableModes(std::vector<IWifiChip::ChipMode>* _aidl_return) override;
92 ndk::ScopedAStatus configureChip(int32_t in_modeId) override;
93 ndk::ScopedAStatus getMode(int32_t* _aidl_return) override;
94 ndk::ScopedAStatus requestChipDebugInfo(IWifiChip::ChipDebugInfo* _aidl_return) override;
95 ndk::ScopedAStatus requestDriverDebugDump(std::vector<uint8_t>* _aidl_return) override;
96 ndk::ScopedAStatus requestFirmwareDebugDump(std::vector<uint8_t>* _aidl_return) override;
97 ndk::ScopedAStatus createApIface(std::shared_ptr<IWifiApIface>* _aidl_return) override;
98 ndk::ScopedAStatus createBridgedApIface(std::shared_ptr<IWifiApIface>* _aidl_return) override;
99 ndk::ScopedAStatus getApIfaceNames(std::vector<std::string>* _aidl_return) override;
100 ndk::ScopedAStatus getApIface(const std::string& in_ifname,
101 std::shared_ptr<IWifiApIface>* _aidl_return) override;
102 ndk::ScopedAStatus removeApIface(const std::string& in_ifname) override;
103 ndk::ScopedAStatus removeIfaceInstanceFromBridgedApIface(
104 const std::string& in_brIfaceName, const std::string& in_ifaceInstanceName) override;
105 ndk::ScopedAStatus createNanIface(std::shared_ptr<IWifiNanIface>* _aidl_return) override;
106 ndk::ScopedAStatus getNanIfaceNames(std::vector<std::string>* _aidl_return) override;
107 ndk::ScopedAStatus getNanIface(const std::string& in_ifname,
108 std::shared_ptr<IWifiNanIface>* _aidl_return) override;
109 ndk::ScopedAStatus removeNanIface(const std::string& in_ifname) override;
110 ndk::ScopedAStatus createP2pIface(std::shared_ptr<IWifiP2pIface>* _aidl_return) override;
111 ndk::ScopedAStatus getP2pIfaceNames(std::vector<std::string>* _aidl_return) override;
112 ndk::ScopedAStatus getP2pIface(const std::string& in_ifname,
113 std::shared_ptr<IWifiP2pIface>* _aidl_return) override;
114 ndk::ScopedAStatus removeP2pIface(const std::string& in_ifname) override;
115 ndk::ScopedAStatus createStaIface(std::shared_ptr<IWifiStaIface>* _aidl_return) override;
116 ndk::ScopedAStatus getStaIfaceNames(std::vector<std::string>* _aidl_return) override;
117 ndk::ScopedAStatus getStaIface(const std::string& in_ifname,
118 std::shared_ptr<IWifiStaIface>* _aidl_return) override;
119 ndk::ScopedAStatus removeStaIface(const std::string& in_ifname) override;
120 ndk::ScopedAStatus createRttController(
121 const std::shared_ptr<IWifiStaIface>& in_boundIface,
122 std::shared_ptr<IWifiRttController>* _aidl_return) override;
123 ndk::ScopedAStatus getDebugRingBuffersStatus(
124 std::vector<WifiDebugRingBufferStatus>* _aidl_return) override;
125 ndk::ScopedAStatus startLoggingToDebugRingBuffer(
126 const std::string& in_ringName, WifiDebugRingBufferVerboseLevel in_verboseLevel,
127 int32_t in_maxIntervalInSec, int32_t in_minDataSizeInBytes) override;
128 ndk::ScopedAStatus forceDumpToDebugRingBuffer(const std::string& in_ringName) override;
129 ndk::ScopedAStatus flushRingBufferToFile() override;
130 ndk::ScopedAStatus stopLoggingToDebugRingBuffer() override;
131 ndk::ScopedAStatus getDebugHostWakeReasonStats(
132 WifiDebugHostWakeReasonStats* _aidl_return) override;
133 ndk::ScopedAStatus enableDebugErrorAlerts(bool in_enable) override;
134 ndk::ScopedAStatus selectTxPowerScenario(IWifiChip::TxPowerScenario in_scenario) override;
135 ndk::ScopedAStatus resetTxPowerScenario() override;
136 ndk::ScopedAStatus setLatencyMode(IWifiChip::LatencyMode in_mode) override;
137 ndk::ScopedAStatus setMultiStaPrimaryConnection(const std::string& in_ifName) override;
138 ndk::ScopedAStatus setMultiStaUseCase(IWifiChip::MultiStaUseCase in_useCase) override;
139 ndk::ScopedAStatus setCoexUnsafeChannels(
140 const std::vector<IWifiChip::CoexUnsafeChannel>& in_unsafeChannels,
Gabriel Biren3b86a782023-02-04 00:42:53 +0000141 int32_t in_restrictions) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000142 ndk::ScopedAStatus setCountryCode(const std::array<uint8_t, 2>& in_code) override;
Gabriel Biren3b86a782023-02-04 00:42:53 +0000143 ndk::ScopedAStatus getUsableChannels(WifiBand in_band, int32_t in_ifaceModeMask,
144 int32_t in_filterMask,
Gabriel Birenf3262f92022-07-15 23:25:39 +0000145 std::vector<WifiUsableChannel>* _aidl_return) override;
Oscar Shuab8313c2022-12-13 00:55:11 +0000146 ndk::ScopedAStatus setAfcChannelAllowance(
Oscar Shu4275c872023-03-08 22:48:09 +0000147 const AfcChannelAllowance& afcChannelAllowance) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000148 ndk::ScopedAStatus triggerSubsystemRestart() override;
Gabriel Biren263db452023-02-24 21:07:38 +0000149 ndk::ScopedAStatus getSupportedRadioCombinations(
150 std::vector<WifiRadioCombination>* _aidl_return) override;
Mahesh KKVc84d3772022-12-02 16:53:28 -0800151 ndk::ScopedAStatus getWifiChipCapabilities(WifiChipCapabilities* _aidl_return) override;
Shuibing Daie5fbcab2022-12-19 15:37:19 -0800152 ndk::ScopedAStatus enableStaChannelForPeerNetwork(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000153 int32_t in_channelCategoryEnableFlag) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000154 binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
maheshkkva8aba172023-02-13 12:33:26 -0800155 ndk::ScopedAStatus setMloMode(const ChipMloMode in_mode) override;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000156
157 private:
158 void invalidateAndRemoveAllIfaces();
159 // When a STA iface is removed any dependent NAN-ifaces/RTT-controllers are
160 // invalidated & removed.
161 void invalidateAndRemoveDependencies(const std::string& removed_iface_name);
162
163 // Corresponding worker functions for the AIDL methods.
164 std::pair<int32_t, ndk::ScopedAStatus> getIdInternal();
165 ndk::ScopedAStatus registerEventCallbackInternal(
166 const std::shared_ptr<IWifiChipEventCallback>& event_callback);
Gabriel Biren2f862492023-03-09 19:13:07 +0000167 std::pair<int32_t, ndk::ScopedAStatus> getFeatureSetInternal();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000168 std::pair<std::vector<IWifiChip::ChipMode>, ndk::ScopedAStatus> getAvailableModesInternal();
169 ndk::ScopedAStatus configureChipInternal(std::unique_lock<std::recursive_mutex>* lock,
170 int32_t mode_id);
171 std::pair<int32_t, ndk::ScopedAStatus> getModeInternal();
172 std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> requestChipDebugInfoInternal();
173 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestDriverDebugDumpInternal();
174 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestFirmwareDebugDumpInternal();
175 std::shared_ptr<WifiApIface> newWifiApIface(std::string& ifname);
176 ndk::ScopedAStatus createVirtualApInterface(const std::string& apVirtIf);
177 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApIfaceInternal();
178 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createBridgedApIfaceInternal();
179 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getApIfaceNamesInternal();
180 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> getApIfaceInternal(
181 const std::string& ifname);
182 ndk::ScopedAStatus removeApIfaceInternal(const std::string& ifname);
183 ndk::ScopedAStatus removeIfaceInstanceFromBridgedApIfaceInternal(
184 const std::string& brIfaceName, const std::string& ifInstanceName);
185 std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> createNanIfaceInternal();
186 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getNanIfaceNamesInternal();
187 std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> getNanIfaceInternal(
188 const std::string& ifname);
189 ndk::ScopedAStatus removeNanIfaceInternal(const std::string& ifname);
190 std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> createP2pIfaceInternal();
191 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getP2pIfaceNamesInternal();
192 std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> getP2pIfaceInternal(
193 const std::string& ifname);
194 ndk::ScopedAStatus removeP2pIfaceInternal(const std::string& ifname);
195 std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> createStaIfaceInternal();
196 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getStaIfaceNamesInternal();
197 std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> getStaIfaceInternal(
198 const std::string& ifname);
199 ndk::ScopedAStatus removeStaIfaceInternal(const std::string& ifname);
200 std::pair<std::shared_ptr<IWifiRttController>, ndk::ScopedAStatus> createRttControllerInternal(
201 const std::shared_ptr<IWifiStaIface>& bound_iface);
202 std::pair<std::vector<WifiDebugRingBufferStatus>, ndk::ScopedAStatus>
203 getDebugRingBuffersStatusInternal();
204 ndk::ScopedAStatus startLoggingToDebugRingBufferInternal(
205 const std::string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level,
206 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes);
207 ndk::ScopedAStatus forceDumpToDebugRingBufferInternal(const std::string& ring_name);
208 ndk::ScopedAStatus flushRingBufferToFileInternal();
209 ndk::ScopedAStatus stopLoggingToDebugRingBufferInternal();
210 std::pair<WifiDebugHostWakeReasonStats, ndk::ScopedAStatus>
211 getDebugHostWakeReasonStatsInternal();
212 ndk::ScopedAStatus enableDebugErrorAlertsInternal(bool enable);
213 ndk::ScopedAStatus selectTxPowerScenarioInternal(IWifiChip::TxPowerScenario scenario);
214 ndk::ScopedAStatus resetTxPowerScenarioInternal();
215 ndk::ScopedAStatus setLatencyModeInternal(IWifiChip::LatencyMode mode);
216 ndk::ScopedAStatus setMultiStaPrimaryConnectionInternal(const std::string& ifname);
217 ndk::ScopedAStatus setMultiStaUseCaseInternal(IWifiChip::MultiStaUseCase use_case);
218 ndk::ScopedAStatus setCoexUnsafeChannelsInternal(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000219 std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, int32_t restrictions);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000220 ndk::ScopedAStatus setCountryCodeInternal(const std::array<uint8_t, 2>& in_code);
221 std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> getUsableChannelsInternal(
Gabriel Biren3b86a782023-02-04 00:42:53 +0000222 WifiBand band, int32_t ifaceModeMask, int32_t filterMask);
223 ndk::ScopedAStatus enableStaChannelForPeerNetworkInternal(int32_t channelCategoryEnableFlag);
Oscar Shuab8313c2022-12-13 00:55:11 +0000224 ndk::ScopedAStatus setAfcChannelAllowanceInternal(
Oscar Shu4275c872023-03-08 22:48:09 +0000225 const AfcChannelAllowance& afcChannelAllowance);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000226 ndk::ScopedAStatus handleChipConfiguration(std::unique_lock<std::recursive_mutex>* lock,
227 int32_t mode_id);
228 ndk::ScopedAStatus registerDebugRingBufferCallback();
229 ndk::ScopedAStatus registerRadioModeChangeCallback();
230
231 std::vector<ChipConcurrencyCombination> getCurrentModeConcurrencyCombinations();
232 std::map<IfaceConcurrencyType, size_t> getCurrentConcurrencyCombination();
233 std::vector<std::map<IfaceConcurrencyType, size_t>> expandConcurrencyCombinations(
234 const ChipConcurrencyCombination& combination);
235 bool canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes(
236 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
237 IfaceConcurrencyType requested_type);
238 bool canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType requested_type);
239 bool canExpandedConcurrencyComboSupportConcurrencyCombo(
240 const std::map<IfaceConcurrencyType, size_t>& expanded_combo,
241 const std::map<IfaceConcurrencyType, size_t>& req_combo);
242 bool canCurrentModeSupportConcurrencyCombo(
243 const std::map<IfaceConcurrencyType, size_t>& req_combo);
244 bool canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type);
245
246 bool isValidModeId(int32_t mode_id);
247 bool isStaApConcurrencyAllowedInCurrentMode();
248 bool isDualStaConcurrencyAllowedInCurrentMode();
249 uint32_t startIdxOfApIface();
250 std::string getFirstActiveWlanIfaceName();
251 std::string allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx);
252 std::string allocateApIfaceName();
253 std::vector<std::string> allocateBridgedApInstanceNames();
254 std::string allocateStaIfaceName();
255 bool writeRingbufferFilesInternal();
256 std::string getWlanIfaceNameWithType(IfaceType type, unsigned idx);
257 void invalidateAndClearBridgedApAll();
Sunil Ravi780bef02023-06-01 21:43:04 +0000258 void deleteApIface(const std::string& if_name);
Gabriel Birenf3262f92022-07-15 23:25:39 +0000259 bool findUsingNameFromBridgedApInstances(const std::string& name);
260 ndk::ScopedAStatus triggerSubsystemRestartInternal();
Gabriel Biren263db452023-02-24 21:07:38 +0000261 std::pair<std::vector<WifiRadioCombination>, ndk::ScopedAStatus>
262 getSupportedRadioCombinationsInternal();
Mahesh KKVc84d3772022-12-02 16:53:28 -0800263 std::pair<WifiChipCapabilities, ndk::ScopedAStatus> getWifiChipCapabilitiesInternal();
maheshkkva8aba172023-02-13 12:33:26 -0800264 ndk::ScopedAStatus setMloModeInternal(const ChipMloMode in_mode);
Sunil Ravi2be1f262023-02-15 20:56:56 +0000265 void retrieveDynamicIfaceCombination();
Gabriel Birenf3262f92022-07-15 23:25:39 +0000266 void setWeakPtr(std::weak_ptr<WifiChip> ptr);
267
268 int32_t chip_id_;
269 std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_;
270 std::weak_ptr<mode_controller::WifiModeController> mode_controller_;
271 std::shared_ptr<iface_util::WifiIfaceUtil> iface_util_;
272 std::vector<std::shared_ptr<WifiApIface>> ap_ifaces_;
273 std::vector<std::shared_ptr<WifiNanIface>> nan_ifaces_;
274 std::vector<std::shared_ptr<WifiP2pIface>> p2p_ifaces_;
275 std::vector<std::shared_ptr<WifiStaIface>> sta_ifaces_;
276 std::vector<std::shared_ptr<WifiRttController>> rtt_controllers_;
277 std::map<std::string, Ringbuffer> ringbuffer_map_;
278 bool is_valid_;
279 // Members pertaining to chip configuration.
280 int32_t current_mode_id_;
281 std::mutex lock_t;
282 std::vector<IWifiChip::ChipMode> modes_;
283 // The legacy ring buffer callback API has only a global callback
284 // registration mechanism. Use this to check if we have already
285 // registered a callback.
286 bool debug_ring_buffer_cb_registered_;
Sunil Ravi2be1f262023-02-15 20:56:56 +0000287 bool using_dynamic_iface_combination_;
Gabriel Birenf3262f92022-07-15 23:25:39 +0000288 aidl_callback_util::AidlCallbackHandler<IWifiChipEventCallback> event_cb_handler_;
289 std::weak_ptr<WifiChip> weak_ptr_this_;
290
291 const std::function<void(const std::string&)> subsystemCallbackHandler_;
292 std::map<std::string, std::vector<std::string>> br_ifaces_ap_instances_;
293 DISALLOW_COPY_AND_ASSIGN(WifiChip);
294};
295
296} // namespace wifi
297} // namespace hardware
298} // namespace android
299} // namespace aidl
300
301#endif // WIFI_CHIP_H_