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 ndk::ScopedAStatus createApOrBridgedApIfaceWithParams( 163 const ApIfaceParams& in_params, std::shared_ptr<IWifiApIface>* _aidl_return) override; 164 165 private: 166 void invalidateAndRemoveAllIfaces(); 167 // When a STA iface is removed any dependent NAN-ifaces/RTT-controllers are 168 // invalidated & removed. 169 void invalidateAndRemoveDependencies(const std::string& removed_iface_name); 170 171 // Corresponding worker functions for the AIDL methods. 172 std::pair<int32_t, ndk::ScopedAStatus> getIdInternal(); 173 ndk::ScopedAStatus registerEventCallbackInternal( 174 const std::shared_ptr<IWifiChipEventCallback>& event_callback); 175 std::pair<int32_t, ndk::ScopedAStatus> getFeatureSetInternal(); 176 std::pair<std::vector<IWifiChip::ChipMode>, ndk::ScopedAStatus> getAvailableModesInternal(); 177 ndk::ScopedAStatus configureChipInternal(std::unique_lock<std::recursive_mutex>* lock, 178 int32_t mode_id); 179 std::pair<int32_t, ndk::ScopedAStatus> getModeInternal(); 180 std::pair<IWifiChip::ChipDebugInfo, ndk::ScopedAStatus> requestChipDebugInfoInternal(); 181 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestDriverDebugDumpInternal(); 182 std::pair<std::vector<uint8_t>, ndk::ScopedAStatus> requestFirmwareDebugDumpInternal(); 183 std::shared_ptr<WifiApIface> newWifiApIface(std::string& ifname, bool usesMlo); 184 ndk::ScopedAStatus createVirtualApInterface(const std::string& apVirtIf); 185 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApIfaceInternal(); 186 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createBridgedApIfaceInternal( 187 bool usesMlo); 188 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> createApOrBridgedApIfaceInternal( 189 IfaceConcurrencyType ifaceType, const std::vector<common::OuiKeyedData>& vendorData); 190 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> 191 createApOrBridgedApIfaceWithParamsInternal(const ApIfaceParams& params); 192 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getApIfaceNamesInternal(); 193 std::pair<std::shared_ptr<IWifiApIface>, ndk::ScopedAStatus> getApIfaceInternal( 194 const std::string& ifname); 195 ndk::ScopedAStatus removeApIfaceInternal(const std::string& ifname); 196 ndk::ScopedAStatus removeIfaceInstanceFromBridgedApIfaceInternal( 197 const std::string& brIfaceName, const std::string& ifInstanceName); 198 std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> createNanIfaceInternal(); 199 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getNanIfaceNamesInternal(); 200 std::pair<std::shared_ptr<IWifiNanIface>, ndk::ScopedAStatus> getNanIfaceInternal( 201 const std::string& ifname); 202 ndk::ScopedAStatus removeNanIfaceInternal(const std::string& ifname); 203 std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> createP2pIfaceInternal(); 204 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getP2pIfaceNamesInternal(); 205 std::pair<std::shared_ptr<IWifiP2pIface>, ndk::ScopedAStatus> getP2pIfaceInternal( 206 const std::string& ifname); 207 ndk::ScopedAStatus removeP2pIfaceInternal(const std::string& ifname); 208 std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> createStaIfaceInternal(); 209 std::pair<std::vector<std::string>, ndk::ScopedAStatus> getStaIfaceNamesInternal(); 210 std::pair<std::shared_ptr<IWifiStaIface>, ndk::ScopedAStatus> getStaIfaceInternal( 211 const std::string& ifname); 212 ndk::ScopedAStatus removeStaIfaceInternal(const std::string& ifname); 213 std::pair<std::shared_ptr<IWifiRttController>, ndk::ScopedAStatus> createRttControllerInternal( 214 const std::shared_ptr<IWifiStaIface>& bound_iface); 215 std::pair<std::vector<WifiDebugRingBufferStatus>, ndk::ScopedAStatus> 216 getDebugRingBuffersStatusInternal(); 217 ndk::ScopedAStatus startLoggingToDebugRingBufferInternal( 218 const std::string& ring_name, WifiDebugRingBufferVerboseLevel verbose_level, 219 uint32_t max_interval_in_sec, uint32_t min_data_size_in_bytes); 220 ndk::ScopedAStatus forceDumpToDebugRingBufferInternal(const std::string& ring_name); 221 ndk::ScopedAStatus flushRingBufferToFileInternal(); 222 ndk::ScopedAStatus stopLoggingToDebugRingBufferInternal(); 223 std::pair<WifiDebugHostWakeReasonStats, ndk::ScopedAStatus> 224 getDebugHostWakeReasonStatsInternal(); 225 ndk::ScopedAStatus enableDebugErrorAlertsInternal(bool enable); 226 ndk::ScopedAStatus selectTxPowerScenarioInternal(IWifiChip::TxPowerScenario scenario); 227 ndk::ScopedAStatus resetTxPowerScenarioInternal(); 228 ndk::ScopedAStatus setLatencyModeInternal(IWifiChip::LatencyMode mode); 229 ndk::ScopedAStatus setMultiStaPrimaryConnectionInternal(const std::string& ifname); 230 ndk::ScopedAStatus setMultiStaUseCaseInternal(IWifiChip::MultiStaUseCase use_case); 231 ndk::ScopedAStatus setCoexUnsafeChannelsInternal( 232 std::vector<IWifiChip::CoexUnsafeChannel> unsafe_channels, int32_t restrictions); 233 ndk::ScopedAStatus setCountryCodeInternal(const std::array<uint8_t, 2>& in_code); 234 std::pair<std::vector<WifiUsableChannel>, ndk::ScopedAStatus> getUsableChannelsInternal( 235 WifiBand band, int32_t ifaceModeMask, int32_t filterMask); 236 ndk::ScopedAStatus enableStaChannelForPeerNetworkInternal(int32_t channelCategoryEnableFlag); 237 ndk::ScopedAStatus setAfcChannelAllowanceInternal( 238 const AfcChannelAllowance& afcChannelAllowance); 239 ndk::ScopedAStatus handleChipConfiguration(std::unique_lock<std::recursive_mutex>* lock, 240 int32_t mode_id); 241 ndk::ScopedAStatus registerDebugRingBufferCallback(); 242 ndk::ScopedAStatus registerRadioModeChangeCallback(); 243 244 std::vector<ChipConcurrencyCombination> getCurrentModeConcurrencyCombinations(); 245 std::map<IfaceConcurrencyType, size_t> getCurrentConcurrencyCombination(); 246 std::vector<std::map<IfaceConcurrencyType, size_t>> expandConcurrencyCombinations( 247 const ChipConcurrencyCombination& combination); 248 bool canExpandedConcurrencyComboSupportConcurrencyTypeWithCurrentTypes( 249 const std::map<IfaceConcurrencyType, size_t>& expanded_combo, 250 IfaceConcurrencyType requested_type); 251 bool canCurrentModeSupportConcurrencyTypeWithCurrentTypes(IfaceConcurrencyType requested_type); 252 bool canExpandedConcurrencyComboSupportConcurrencyCombo( 253 const std::map<IfaceConcurrencyType, size_t>& expanded_combo, 254 const std::map<IfaceConcurrencyType, size_t>& req_combo); 255 bool canCurrentModeSupportConcurrencyCombo( 256 const std::map<IfaceConcurrencyType, size_t>& req_combo); 257 bool canCurrentModeSupportConcurrencyType(IfaceConcurrencyType requested_type); 258 259 bool isValidModeId(int32_t mode_id); 260 bool isStaApConcurrencyAllowedInCurrentMode(); 261 bool isDualStaConcurrencyAllowedInCurrentMode(); 262 uint32_t startIdxOfApIface(); 263 std::string getFirstActiveWlanIfaceName(); 264 std::string allocateApOrStaIfaceName(IfaceType type, uint32_t start_idx); 265 std::string allocateApIfaceName(); 266 std::vector<std::string> allocateBridgedApInstanceNames(bool usesMlo); 267 std::string allocateStaIfaceName(); 268 bool writeRingbufferFilesInternal(); 269 std::string getWlanIfaceNameWithType(IfaceType type, unsigned idx); 270 void invalidateAndClearBridgedApAll(); 271 void deleteApIface(const std::string& if_name); 272 bool findUsingNameFromBridgedApInstances(const std::string& name); 273 ndk::ScopedAStatus triggerSubsystemRestartInternal(); 274 std::pair<std::vector<WifiRadioCombination>, ndk::ScopedAStatus> 275 getSupportedRadioCombinationsInternal(); 276 std::pair<WifiChipCapabilities, ndk::ScopedAStatus> getWifiChipCapabilitiesInternal(); 277 ndk::ScopedAStatus setMloModeInternal(const ChipMloMode in_mode); 278 ndk::ScopedAStatus setVoipModeInternal(const VoipMode in_mode); 279 void retrieveDynamicIfaceCombination(); 280 void setWeakPtr(std::weak_ptr<WifiChip> ptr); 281 282 int32_t chip_id_; 283 std::weak_ptr<legacy_hal::WifiLegacyHal> legacy_hal_; 284 std::weak_ptr<mode_controller::WifiModeController> mode_controller_; 285 std::shared_ptr<iface_util::WifiIfaceUtil> iface_util_; 286 std::vector<std::shared_ptr<WifiApIface>> ap_ifaces_; 287 std::vector<std::shared_ptr<WifiNanIface>> nan_ifaces_; 288 std::vector<std::shared_ptr<WifiP2pIface>> p2p_ifaces_; 289 std::vector<std::shared_ptr<WifiStaIface>> sta_ifaces_; 290 std::vector<std::shared_ptr<WifiRttController>> rtt_controllers_; 291 std::map<std::string, Ringbuffer> ringbuffer_map_; 292 bool is_valid_; 293 // Members pertaining to chip configuration. 294 int32_t current_mode_id_; 295 std::mutex lock_t; 296 std::vector<IWifiChip::ChipMode> modes_; 297 // The legacy ring buffer callback API has only a global callback 298 // registration mechanism. Use this to check if we have already 299 // registered a callback. 300 bool debug_ring_buffer_cb_registered_; 301 bool using_dynamic_iface_combination_; 302 aidl_callback_util::AidlCallbackHandler<IWifiChipEventCallback> event_cb_handler_; 303 std::weak_ptr<WifiChip> weak_ptr_this_; 304 305 const std::function<void(const std::string&)> subsystemCallbackHandler_; 306 std::map<std::string, std::vector<std::string>> br_ifaces_ap_instances_; 307 DISALLOW_COPY_AND_ASSIGN(WifiChip); 308 }; 309 310 } // namespace wifi 311 } // namespace hardware 312 } // namespace android 313 } // namespace aidl 314 315 #endif // WIFI_CHIP_H_ 316