2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "wificond/server.h"
21 #include <android-base/logging.h>
23 #include "wificond/net/netlink_utils.h"
24 #include "wificond/scanning/scan_utils.h"
26 using android::binder::Status;
28 using android::IBinder;
29 using android::net::wifi::IApInterface;
30 using android::net::wifi::IClientInterface;
31 using android::net::wifi::IInterfaceEventCallback;
32 using android::net::wifi::IRttClient;
33 using android::net::wifi::IRttController;
34 using android::wifi_hal::DriverTool;
35 using android::wifi_system::HalTool;
36 using android::wifi_system::HostapdManager;
37 using android::wifi_system::InterfaceTool;
38 using android::wifi_system::SupplicantManager;
40 using std::placeholders::_1;
42 using std::stringstream;
43 using std::unique_ptr;
49 Server::Server(unique_ptr<HalTool> hal_tool,
50 unique_ptr<InterfaceTool> if_tool,
51 unique_ptr<DriverTool> driver_tool,
52 unique_ptr<SupplicantManager> supplicant_manager,
53 unique_ptr<HostapdManager> hostapd_manager,
54 NetlinkUtils* netlink_utils,
55 ScanUtils* scan_utils)
56 : hal_tool_(std::move(hal_tool)),
57 if_tool_(std::move(if_tool)),
58 driver_tool_(std::move(driver_tool)),
59 supplicant_manager_(std::move(supplicant_manager)),
60 hostapd_manager_(std::move(hostapd_manager)),
61 netlink_utils_(netlink_utils),
62 scan_utils_(scan_utils) {
65 Status Server::RegisterCallback(const sp<IInterfaceEventCallback>& callback) {
66 for (auto& it : interface_event_callbacks_) {
67 if (IInterface::asBinder(callback) == IInterface::asBinder(it)) {
68 LOG(WARNING) << "Ignore duplicate interface event callback registration";
72 LOG(INFO) << "New interface event callback registered";
73 interface_event_callbacks_.push_back(callback);
77 Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
78 for (auto it = interface_event_callbacks_.begin();
79 it != interface_event_callbacks_.end();
81 if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
82 interface_event_callbacks_.erase(it);
83 LOG(INFO) << "Unregister interface event callback";
87 LOG(WARNING) << "Failed to find registered interface event callback"
92 Status Server::registerRttClient(const sp<IRttClient>& rtt_client,
93 sp<IRttController>* out_rtt_controller) {
94 if (rtt_controller_ == nullptr) {
95 rtt_controller_.reset(new RttControllerImpl());
97 rtt_controller_->RegisterRttClient(rtt_client);
99 *out_rtt_controller = rtt_controller_->GetBinder();
103 Status Server::unregisterRttClient(const sp<IRttClient>& rttClient) {
104 rtt_controller_->UnregisterRttClient(rttClient);
105 if (rtt_controller_->GetClientCount() == 0) {
106 rtt_controller_.reset();
111 Status Server::createApInterface(sp<IApInterface>* created_interface) {
112 string interface_name;
113 uint32_t interface_index;
114 vector<uint8_t> interface_mac_addr;
115 if (!SetupInterfaceForMode(DriverTool::kFirmwareModeAp,
118 &interface_mac_addr)) {
119 return Status::ok(); // Logging was done internally
122 unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
126 hostapd_manager_.get()));
127 *created_interface = ap_interface->GetBinder();
128 ap_interfaces_.push_back(std::move(ap_interface));
129 BroadcastApInterfaceReady(ap_interfaces_.back()->GetBinder());
134 Status Server::createClientInterface(sp<IClientInterface>* created_interface) {
135 string interface_name;
136 uint32_t interface_index;
137 vector<uint8_t> interface_mac_addr;
138 if (!SetupInterfaceForMode(DriverTool::kFirmwareModeSta,
141 &interface_mac_addr)) {
142 return Status::ok(); // Logging was done internally
145 unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
151 supplicant_manager_.get(),
154 *created_interface = client_interface->GetBinder();
155 client_interfaces_.push_back(std::move(client_interface));
156 BroadcastClientInterfaceReady(client_interfaces_.back()->GetBinder());
161 Status Server::tearDownInterfaces() {
162 for (auto& it : client_interfaces_) {
163 BroadcastClientInterfaceTornDown(it->GetBinder());
165 client_interfaces_.clear();
167 for (auto& it : ap_interfaces_) {
168 BroadcastApInterfaceTornDown(it->GetBinder());
170 ap_interfaces_.clear();
172 netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
174 if (!driver_tool_->UnloadDriver()) {
175 LOG(ERROR) << "Failed to unload WiFi driver!";
180 Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
181 vector<sp<android::IBinder>> client_interfaces_binder;
182 for (auto& it : client_interfaces_) {
183 out_client_interfaces->push_back(asBinder(it->GetBinder()));
185 return binder::Status::ok();
188 Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
189 vector<sp<IBinder>> ap_interfaces_binder;
190 for (auto& it : ap_interfaces_) {
191 out_ap_interfaces->push_back(asBinder(it->GetBinder()));
193 return binder::Status::ok();
196 void Server::CleanUpSystemState() {
197 supplicant_manager_->StopSupplicant();
198 hostapd_manager_->StopHostapd();
200 uint32_t phy_index = 0;
201 uint32_t if_index = 0;
204 if (netlink_utils_->GetWiphyIndex(&phy_index) &&
205 netlink_utils_->GetInterfaceInfo(phy_index,
209 // If the kernel knows about a network interface, mark it as down.
210 // This prevents us from beaconing as an AP, or remaining associated
212 if_tool_->SetUpState(if_name.c_str(), false);
214 // "unloading the driver" is frequently a no-op in systems that
215 // don't have kernel modules, but just in case.
216 driver_tool_->UnloadDriver();
219 bool Server::SetupInterfaceForMode(int mode,
220 string* interface_name,
221 uint32_t* interface_index,
222 vector<uint8_t>* interface_mac_addr) {
223 if (!ap_interfaces_.empty() || !client_interfaces_.empty()) {
224 // In the future we may support multiple interfaces at once. However,
225 // today, we support just one.
226 LOG(ERROR) << "Cannot create AP interface when other interfaces exist";
231 if (!driver_tool_->LoadDriver()) {
232 LOG(ERROR) << "Failed to load WiFi driver!";
235 if (!driver_tool_->ChangeFirmwareMode(mode)) {
236 LOG(ERROR) << "Failed to change WiFi firmware mode!";
240 if (!RefreshWiphyIndex()) {
244 netlink_utils_->SubscribeRegDomainChange(
246 std::bind(&Server::OnRegDomainChanged,
250 if (!netlink_utils_->GetInterfaceInfo(wiphy_index_,
253 interface_mac_addr)) {
254 LOG(ERROR) << "Failed to get interface info from kernel";
261 bool Server::RefreshWiphyIndex() {
262 if (!netlink_utils_->GetWiphyIndex(&wiphy_index_)) {
263 LOG(ERROR) << "Failed to get wiphy index";
269 void Server::OnRegDomainChanged(std::string& country_code) {
270 if (country_code.empty()) {
271 LOG(INFO) << "Regulatory domain changed";
273 LOG(INFO) << "Regulatory domain changed to country: " << country_code;
278 void Server::LogSupportedBands() {
280 ScanCapabilities scan_capabilities;
281 WiphyFeatures wiphy_features;
282 netlink_utils_->GetWiphyInfo(wiphy_index_,
288 for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
289 ss << " " << band_info.band_2g[i];
291 LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
294 for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
295 ss << " " << band_info.band_5g[i];
297 LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
300 for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
301 ss << " " << band_info.band_dfs[i];
303 LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
306 void Server::BroadcastClientInterfaceReady(
307 sp<IClientInterface> network_interface) {
308 for (auto& it : interface_event_callbacks_) {
309 it->OnClientInterfaceReady(network_interface);
313 void Server::BroadcastApInterfaceReady(
314 sp<IApInterface> network_interface) {
315 for (auto& it : interface_event_callbacks_) {
316 it->OnApInterfaceReady(network_interface);
320 void Server::BroadcastClientInterfaceTornDown(
321 sp<IClientInterface> network_interface) {
322 for (auto& it : interface_event_callbacks_) {
323 it->OnClientTorndownEvent(network_interface);
327 void Server::BroadcastApInterfaceTornDown(
328 sp<IApInterface> network_interface) {
329 for (auto& it : interface_event_callbacks_) {
330 it->OnApTorndownEvent(network_interface);
334 } // namespace wificond
335 } // namespace android