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_system::HostapdManager;
35 using android::wifi_system::InterfaceTool;
36 using android::wifi_system::SupplicantManager;
38 using std::placeholders::_1;
40 using std::stringstream;
41 using std::unique_ptr;
47 Server::Server(unique_ptr<InterfaceTool> if_tool,
48 unique_ptr<SupplicantManager> supplicant_manager,
49 unique_ptr<HostapdManager> hostapd_manager,
50 NetlinkUtils* netlink_utils,
51 ScanUtils* scan_utils)
52 : if_tool_(std::move(if_tool)),
53 supplicant_manager_(std::move(supplicant_manager)),
54 hostapd_manager_(std::move(hostapd_manager)),
55 netlink_utils_(netlink_utils),
56 scan_utils_(scan_utils) {
59 Status Server::RegisterCallback(const sp<IInterfaceEventCallback>& callback) {
60 for (auto& it : interface_event_callbacks_) {
61 if (IInterface::asBinder(callback) == IInterface::asBinder(it)) {
62 LOG(WARNING) << "Ignore duplicate interface event callback registration";
66 LOG(INFO) << "New interface event callback registered";
67 interface_event_callbacks_.push_back(callback);
71 Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
72 for (auto it = interface_event_callbacks_.begin();
73 it != interface_event_callbacks_.end();
75 if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
76 interface_event_callbacks_.erase(it);
77 LOG(INFO) << "Unregister interface event callback";
81 LOG(WARNING) << "Failed to find registered interface event callback"
86 Status Server::registerRttClient(const sp<IRttClient>& rtt_client,
87 sp<IRttController>* out_rtt_controller) {
88 if (rtt_controller_ == nullptr) {
89 rtt_controller_.reset(new RttControllerImpl());
91 rtt_controller_->RegisterRttClient(rtt_client);
93 *out_rtt_controller = rtt_controller_->GetBinder();
97 Status Server::unregisterRttClient(const sp<IRttClient>& rttClient) {
98 rtt_controller_->UnregisterRttClient(rttClient);
99 if (rtt_controller_->GetClientCount() == 0) {
100 rtt_controller_.reset();
105 Status Server::createApInterface(sp<IApInterface>* created_interface) {
106 string interface_name;
107 uint32_t interface_index;
108 vector<uint8_t> interface_mac_addr;
109 if (!SetupInterface(&interface_name,
111 &interface_mac_addr)) {
112 return Status::ok(); // Logging was done internally
115 unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
120 hostapd_manager_.get()));
121 *created_interface = ap_interface->GetBinder();
122 ap_interfaces_.push_back(std::move(ap_interface));
123 BroadcastApInterfaceReady(ap_interfaces_.back()->GetBinder());
128 Status Server::createClientInterface(sp<IClientInterface>* created_interface) {
129 string interface_name;
130 uint32_t interface_index;
131 vector<uint8_t> interface_mac_addr;
132 if (!SetupInterface(&interface_name,
134 &interface_mac_addr)) {
135 return Status::ok(); // Logging was done internally
138 unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
144 supplicant_manager_.get(),
147 *created_interface = client_interface->GetBinder();
148 client_interfaces_.push_back(std::move(client_interface));
149 BroadcastClientInterfaceReady(client_interfaces_.back()->GetBinder());
154 Status Server::tearDownInterfaces() {
155 for (auto& it : client_interfaces_) {
156 BroadcastClientInterfaceTornDown(it->GetBinder());
158 client_interfaces_.clear();
160 for (auto& it : ap_interfaces_) {
161 BroadcastApInterfaceTornDown(it->GetBinder());
163 ap_interfaces_.clear();
165 netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
170 Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
171 vector<sp<android::IBinder>> client_interfaces_binder;
172 for (auto& it : client_interfaces_) {
173 out_client_interfaces->push_back(asBinder(it->GetBinder()));
175 return binder::Status::ok();
178 Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
179 vector<sp<IBinder>> ap_interfaces_binder;
180 for (auto& it : ap_interfaces_) {
181 out_ap_interfaces->push_back(asBinder(it->GetBinder()));
183 return binder::Status::ok();
186 void Server::CleanUpSystemState() {
187 supplicant_manager_->StopSupplicant();
188 hostapd_manager_->StopHostapd();
190 uint32_t phy_index = 0;
191 uint32_t if_index = 0;
194 if (netlink_utils_->GetWiphyIndex(&phy_index) &&
195 netlink_utils_->GetInterfaceInfo(phy_index,
199 // If the kernel knows about a network interface, mark it as down.
200 // This prevents us from beaconing as an AP, or remaining associated
202 if_tool_->SetUpState(if_name.c_str(), false);
206 bool Server::SetupInterface(string* interface_name,
207 uint32_t* interface_index,
208 vector<uint8_t>* interface_mac_addr) {
209 if (!ap_interfaces_.empty() || !client_interfaces_.empty()) {
210 // In the future we may support multiple interfaces at once. However,
211 // today, we support just one.
212 LOG(ERROR) << "Cannot create AP interface when other interfaces exist";
216 if (!RefreshWiphyIndex()) {
220 netlink_utils_->SubscribeRegDomainChange(
222 std::bind(&Server::OnRegDomainChanged,
226 if (!netlink_utils_->GetInterfaceInfo(wiphy_index_,
229 interface_mac_addr)) {
230 LOG(ERROR) << "Failed to get interface info from kernel";
237 bool Server::RefreshWiphyIndex() {
238 if (!netlink_utils_->GetWiphyIndex(&wiphy_index_)) {
239 LOG(ERROR) << "Failed to get wiphy index";
245 void Server::OnRegDomainChanged(std::string& country_code) {
246 if (country_code.empty()) {
247 LOG(INFO) << "Regulatory domain changed";
249 LOG(INFO) << "Regulatory domain changed to country: " << country_code;
254 void Server::LogSupportedBands() {
256 ScanCapabilities scan_capabilities;
257 WiphyFeatures wiphy_features;
258 netlink_utils_->GetWiphyInfo(wiphy_index_,
264 for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
265 ss << " " << band_info.band_2g[i];
267 LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
270 for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
271 ss << " " << band_info.band_5g[i];
273 LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
276 for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
277 ss << " " << band_info.band_dfs[i];
279 LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
282 void Server::BroadcastClientInterfaceReady(
283 sp<IClientInterface> network_interface) {
284 for (auto& it : interface_event_callbacks_) {
285 it->OnClientInterfaceReady(network_interface);
289 void Server::BroadcastApInterfaceReady(
290 sp<IApInterface> network_interface) {
291 for (auto& it : interface_event_callbacks_) {
292 it->OnApInterfaceReady(network_interface);
296 void Server::BroadcastClientInterfaceTornDown(
297 sp<IClientInterface> network_interface) {
298 for (auto& it : interface_event_callbacks_) {
299 it->OnClientTorndownEvent(network_interface);
303 void Server::BroadcastApInterfaceTornDown(
304 sp<IApInterface> network_interface) {
305 for (auto& it : interface_event_callbacks_) {
306 it->OnApTorndownEvent(network_interface);
310 } // namespace wificond
311 } // namespace android