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"
19 #include <android-base/logging.h>
21 #include "wificond/net/netlink_utils.h"
22 #include "wificond/scanning/scan_utils.h"
24 using android::binder::Status;
26 using android::IBinder;
29 using std::unique_ptr;
30 using android::net::wifi::IApInterface;
31 using android::net::wifi::IClientInterface;
32 using android::net::wifi::IInterfaceEventCallback;
33 using android::wifi_hal::DriverTool;
34 using android::wifi_system::HalTool;
35 using android::wifi_system::HostapdManager;
36 using android::wifi_system::InterfaceTool;
37 using android::wifi_system::SupplicantManager;
42 Server::Server(unique_ptr<HalTool> hal_tool,
43 unique_ptr<InterfaceTool> if_tool,
44 unique_ptr<DriverTool> driver_tool,
45 unique_ptr<SupplicantManager> supplicant_manager,
46 unique_ptr<HostapdManager> hostapd_manager,
47 NetlinkUtils* netlink_utils,
48 ScanUtils* scan_utils)
49 : hal_tool_(std::move(hal_tool)),
50 if_tool_(std::move(if_tool)),
51 driver_tool_(std::move(driver_tool)),
52 supplicant_manager_(std::move(supplicant_manager)),
53 hostapd_manager_(std::move(hostapd_manager)),
54 netlink_utils_(netlink_utils),
55 scan_utils_(scan_utils) {
58 Status Server::RegisterCallback(const sp<IInterfaceEventCallback>& callback) {
59 for (auto& it : interface_event_callbacks_) {
60 if (IInterface::asBinder(callback) == IInterface::asBinder(it)) {
61 LOG(WARNING) << "Ignore duplicate interface event callback registration";
65 LOG(INFO) << "New interface event callback registered";
66 interface_event_callbacks_.push_back(callback);
70 Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
71 for (auto it = interface_event_callbacks_.begin();
72 it != interface_event_callbacks_.end();
74 if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
75 interface_event_callbacks_.erase(it);
76 LOG(INFO) << "Unregister interface event callback";
80 LOG(WARNING) << "Failed to find registered interface event callback"
86 Status Server::createApInterface(sp<IApInterface>* created_interface) {
87 string interface_name;
88 uint32_t interface_index;
89 vector<uint8_t> interface_mac_addr;
90 if (!SetupInterfaceForMode(DriverTool::kFirmwareModeAp,
93 &interface_mac_addr)) {
94 return Status::ok(); // Logging was done internally
97 unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
101 hostapd_manager_.get()));
102 *created_interface = ap_interface->GetBinder();
103 ap_interfaces_.push_back(std::move(ap_interface));
104 BroadcastApInterfaceReady(ap_interfaces_.back()->GetBinder());
109 Status Server::createClientInterface(sp<IClientInterface>* created_interface) {
110 string interface_name;
111 uint32_t interface_index;
112 vector<uint8_t> interface_mac_addr;
113 if (!SetupInterfaceForMode(DriverTool::kFirmwareModeSta,
116 &interface_mac_addr)) {
117 return Status::ok(); // Logging was done internally
120 unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
125 supplicant_manager_.get(),
128 *created_interface = client_interface->GetBinder();
129 client_interfaces_.push_back(std::move(client_interface));
130 BroadcastClientInterfaceReady(client_interfaces_.back()->GetBinder());
135 Status Server::tearDownInterfaces() {
136 for (auto& it : client_interfaces_) {
137 BroadcastClientInterfaceTornDown(it->GetBinder());
139 client_interfaces_.clear();
141 for (auto& it : ap_interfaces_) {
142 BroadcastApInterfaceTornDown(it->GetBinder());
144 ap_interfaces_.clear();
146 if (!driver_tool_->UnloadDriver()) {
147 LOG(ERROR) << "Failed to unload WiFi driver!";
152 Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
153 vector<sp<android::IBinder>> client_interfaces_binder;
154 for (auto& it : client_interfaces_) {
155 out_client_interfaces->push_back(asBinder(it->GetBinder()));
157 return binder::Status::ok();
160 Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
161 vector<sp<IBinder>> ap_interfaces_binder;
162 for (auto& it : ap_interfaces_) {
163 out_ap_interfaces->push_back(asBinder(it->GetBinder()));
165 return binder::Status::ok();
168 void Server::CleanUpSystemState() {
169 supplicant_manager_->StopSupplicant();
170 hostapd_manager_->StopHostapd();
172 uint32_t phy_index = 0;
173 uint32_t if_index = 0;
176 if (netlink_utils_->GetWiphyIndex(&phy_index) &&
177 netlink_utils_->GetInterfaceInfo(phy_index,
181 // If the kernel knows about a network interface, mark it as down.
182 // This prevents us from beaconing as an AP, or remaining associated
184 if_tool_->SetUpState(if_name.c_str(), false);
186 // "unloading the driver" is frequently a no-op in systems that
187 // don't have kernel modules, but just in case.
188 driver_tool_->UnloadDriver();
191 bool Server::SetupInterfaceForMode(int mode,
192 string* interface_name,
193 uint32_t* interface_index,
194 vector<uint8_t>* interface_mac_addr) {
195 if (!ap_interfaces_.empty() || !client_interfaces_.empty()) {
196 // In the future we may support multiple interfaces at once. However,
197 // today, we support just one.
198 LOG(ERROR) << "Cannot create AP interface when other interfaces exist";
203 if (!driver_tool_->LoadDriver()) {
204 LOG(ERROR) << "Failed to load WiFi driver!";
207 if (!driver_tool_->ChangeFirmwareMode(mode)) {
208 LOG(ERROR) << "Failed to change WiFi firmware mode!";
212 if (!RefreshWiphyIndex()) {
216 if (!netlink_utils_->GetInterfaceInfo(wiphy_index_,
219 interface_mac_addr)) {
220 LOG(ERROR) << "Failed to get interface info from kernel";
227 bool Server::RefreshWiphyIndex() {
228 if (!netlink_utils_->GetWiphyIndex(&wiphy_index_)) {
229 LOG(ERROR) << "Failed to get wiphy index";
235 void Server::BroadcastClientInterfaceReady(
236 sp<IClientInterface> network_interface) {
237 for (auto& it : interface_event_callbacks_) {
238 it->OnClientInterfaceReady(network_interface);
242 void Server::BroadcastApInterfaceReady(
243 sp<IApInterface> network_interface) {
244 for (auto& it : interface_event_callbacks_) {
245 it->OnApInterfaceReady(network_interface);
249 void Server::BroadcastClientInterfaceTornDown(
250 sp<IClientInterface> network_interface) {
251 for (auto& it : interface_event_callbacks_) {
252 it->OnClientTorndownEvent(network_interface);
256 void Server::BroadcastApInterfaceTornDown(
257 sp<IApInterface> network_interface) {
258 for (auto& it : interface_event_callbacks_) {
259 it->OnApTorndownEvent(network_interface);
263 } // namespace wificond
264 } // namespace android