1 /* 2 * Copyright (C) 2016 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 WIFICOND_SERVER_H_ 18 #define WIFICOND_SERVER_H_ 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include <android-base/macros.h> 25 #include <wifi_system/interface_tool.h> 26 27 #include "android/net/wifi/nl80211/BnWificond.h" 28 #include "android/net/wifi/nl80211/IApInterface.h" 29 #include "android/net/wifi/nl80211/IClientInterface.h" 30 #include "android/net/wifi/nl80211/IInterfaceEventCallback.h" 31 #include "android/net/wifi/nl80211/IWificondEventCallback.h" 32 33 #include "wificond/ap_interface_impl.h" 34 #include "wificond/client_interface_impl.h" 35 36 namespace android { 37 namespace wificond { 38 39 class NL80211Packet; 40 class NetlinkUtils; 41 class ScanUtils; 42 43 struct InterfaceInfo; 44 45 class Server : public android::net::wifi::nl80211::BnWificond { 46 public: 47 Server(std::unique_ptr<wifi_system::InterfaceTool> if_tool, 48 NetlinkUtils* netlink_utils, 49 ScanUtils* scan_utils); 50 ~Server() override = default; 51 52 android::binder::Status registerWificondEventCallback( 53 const android::sp<android::net::wifi::nl80211::IWificondEventCallback>& 54 callback) override; 55 android::binder::Status unregisterWificondEventCallback( 56 const android::sp<android::net::wifi::nl80211::IWificondEventCallback>& 57 callback) override; 58 59 android::binder::Status RegisterCallback( 60 const android::sp<android::net::wifi::nl80211::IInterfaceEventCallback>& 61 callback) override; 62 android::binder::Status UnregisterCallback( 63 const android::sp<android::net::wifi::nl80211::IInterfaceEventCallback>& 64 callback) override; 65 // Returns a vector of available frequencies for 2.4GHz channels. 66 android::binder::Status getAvailable2gChannels( 67 ::std::optional<::std::vector<int32_t>>* out_frequencies) override; 68 // Returns a vector of available frequencies for 5GHz non-DFS channels. 69 android::binder::Status getAvailable5gNonDFSChannels( 70 ::std::optional<::std::vector<int32_t>>* out_frequencies) override; 71 // Returns a vector of available frequencies for DFS channels. 72 android::binder::Status getAvailableDFSChannels( 73 ::std::optional<::std::vector<int32_t>>* out_frequencies) override; 74 // Returns a vector of available frequencies for 6GHz channels. 75 android::binder::Status getAvailable6gChannels( 76 ::std::optional<::std::vector<int32_t>>* out_frequencies) override; 77 // Returns a vector of available frequencies for 60GHz channels. 78 android::binder::Status getAvailable60gChannels( 79 ::std::optional<::std::vector<int32_t>>* out_frequencies) override; 80 81 android::binder::Status createApInterface( 82 const std::string& iface_name, 83 android::sp<android::net::wifi::nl80211::IApInterface>* 84 created_interface) override; 85 86 android::binder::Status createClientInterface( 87 const std::string& iface_name, 88 android::sp<android::net::wifi::nl80211::IClientInterface>* 89 created_interface) override; 90 91 android::binder::Status tearDownApInterface( 92 const std::string& iface_name, 93 bool* out_success) override; 94 95 android::binder::Status tearDownClientInterface( 96 const std::string& iface_name, 97 bool* out_success) override; 98 99 android::binder::Status tearDownInterfaces() override; 100 101 android::binder::Status GetClientInterfaces( 102 std::vector<android::sp<android::IBinder>>* out_client_ifs) override; 103 android::binder::Status GetApInterfaces( 104 std::vector<android::sp<android::IBinder>>* out_ap_ifs) override; 105 status_t dump(int fd, const Vector<String16>& args) override; 106 107 // Returns device wiphy capabilities for an interface 108 android::binder::Status getDeviceWiphyCapabilities( 109 const std::string& iface_name, 110 ::std::optional<net::wifi::nl80211::DeviceWiphyCapabilities>* capabilities) override; 111 112 android::binder::Status notifyCountryCodeChanged() override; 113 114 private: 115 // Request interface information from kernel and setup local interface object. 116 // This assumes that interface should be in STATION mode. Even if we setup 117 // interface on behalf of createApInterace(), it is Hostapd that configure 118 // the interface to Ap mode later. 119 // Returns true on success, false otherwise. 120 bool SetupInterface(const std::string& iface_name, InterfaceInfo* interface, 121 uint32_t *wiphy_index); 122 void LogSupportedBands(uint32_t wiphy_index); 123 void OnRegDomainChanged(uint32_t wiphy_index, std::string& country_code); 124 void handleCountryCodeChanged(); 125 void BroadcastClientInterfaceReady( 126 android::sp<android::net::wifi::nl80211::IClientInterface> network_interface); 127 void BroadcastApInterfaceReady( 128 android::sp<android::net::wifi::nl80211::IApInterface> network_interface); 129 void BroadcastClientInterfaceTornDown( 130 android::sp<android::net::wifi::nl80211::IClientInterface> network_interface); 131 void BroadcastApInterfaceTornDown( 132 android::sp<android::net::wifi::nl80211::IApInterface> network_interface); 133 void BroadcastRegDomainChanged(); 134 void MarkDownAllInterfaces(); 135 int FindWiphyIndex(const std::string& iface_name); 136 bool GetBandInfo(int wiphy_index, BandInfo* band_info); 137 int GetWiphyIndexFromBand(int band); 138 void UpdateBandWiphyIndexMap(int wiphy_index); 139 void EraseBandWiphyIndexMap(int wiphy_index); 140 bool hasNoIfaceForWiphyIndex(int wiphy_index); 141 142 const std::unique_ptr<wifi_system::InterfaceTool> if_tool_; 143 NetlinkUtils* const netlink_utils_; 144 ScanUtils* const scan_utils_; 145 146 // Chips serves mutually exclusive bands. 147 std::map<uint32_t, uint32_t> band_to_wiphy_index_map_; 148 std::map<std::string, uint32_t> iface_to_wiphy_index_map_; 149 std::map<std::string, std::unique_ptr<ApInterfaceImpl>> ap_interfaces_; 150 std::map<std::string, std::unique_ptr<ClientInterfaceImpl>> client_interfaces_; 151 std::vector<android::sp<android::net::wifi::nl80211::IInterfaceEventCallback>> 152 interface_event_callbacks_; 153 std::vector<android::sp<android::net::wifi::nl80211::IWificondEventCallback>> 154 wificond_event_callbacks_; 155 156 // Cached interface list from kernel for dumping. 157 std::vector<InterfaceInfo> debug_interfaces_; 158 159 std::string current_country_code_; 160 161 DISALLOW_COPY_AND_ASSIGN(Server); 162 }; 163 164 } // namespace wificond 165 } // namespace android 166 167 #endif // WIFICOND_SERVER_H_ 168