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