OSDN Git Service

Merge "wificond: Remove all libwifi-hal linkage" am: 84d8ce0fb7 am: 7cc4fd6f1a
[android-x86/system-connectivity-wificond.git] / server.cpp
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 #include "wificond/server.h"
18
19 #include <sstream>
20
21 #include <android-base/logging.h>
22
23 #include "wificond/net/netlink_utils.h"
24 #include "wificond/scanning/scan_utils.h"
25
26 using android::binder::Status;
27 using android::sp;
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;
37
38 using std::placeholders::_1;
39 using std::string;
40 using std::stringstream;
41 using std::unique_ptr;
42 using std::vector;
43
44 namespace android {
45 namespace wificond {
46
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) {
57 }
58
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";
63       return Status::ok();
64     }
65   }
66   LOG(INFO) << "New interface event callback registered";
67   interface_event_callbacks_.push_back(callback);
68   return Status::ok();
69 }
70
71 Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
72   for (auto it = interface_event_callbacks_.begin();
73        it != interface_event_callbacks_.end();
74        it++) {
75     if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
76       interface_event_callbacks_.erase(it);
77       LOG(INFO) << "Unregister interface event callback";
78       return Status::ok();
79     }
80   }
81   LOG(WARNING) << "Failed to find registered interface event callback"
82                << " to unregister";
83   return Status::ok();
84 }
85
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());
90   }
91   rtt_controller_->RegisterRttClient(rtt_client);
92
93   *out_rtt_controller = rtt_controller_->GetBinder();
94   return Status::ok();
95 }
96
97 Status Server::unregisterRttClient(const sp<IRttClient>& rttClient) {
98   rtt_controller_->UnregisterRttClient(rttClient);
99   if (rtt_controller_->GetClientCount() == 0) {
100     rtt_controller_.reset();
101   }
102   return Status::ok();
103 }
104
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,
110                              &interface_index,
111                              &interface_mac_addr)) {
112     return Status::ok();  // Logging was done internally
113   }
114
115   unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
116       interface_name,
117       interface_index,
118       netlink_utils_,
119       if_tool_.get(),
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());
124
125   return Status::ok();
126 }
127
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,
133                              &interface_index,
134                              &interface_mac_addr)) {
135     return Status::ok();  // Logging was done internally
136   }
137
138   unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
139       wiphy_index_,
140       interface_name,
141       interface_index,
142       interface_mac_addr,
143       if_tool_.get(),
144       supplicant_manager_.get(),
145       netlink_utils_,
146       scan_utils_));
147   *created_interface = client_interface->GetBinder();
148   client_interfaces_.push_back(std::move(client_interface));
149   BroadcastClientInterfaceReady(client_interfaces_.back()->GetBinder());
150
151   return Status::ok();
152 }
153
154 Status Server::tearDownInterfaces() {
155   for (auto& it : client_interfaces_) {
156     BroadcastClientInterfaceTornDown(it->GetBinder());
157   }
158   client_interfaces_.clear();
159
160   for (auto& it : ap_interfaces_) {
161     BroadcastApInterfaceTornDown(it->GetBinder());
162   }
163   ap_interfaces_.clear();
164
165   netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
166
167   return Status::ok();
168 }
169
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()));
174   }
175   return binder::Status::ok();
176 }
177
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()));
182   }
183   return binder::Status::ok();
184 }
185
186 void Server::CleanUpSystemState() {
187   supplicant_manager_->StopSupplicant();
188   hostapd_manager_->StopHostapd();
189
190   uint32_t phy_index = 0;
191   uint32_t if_index = 0;
192   vector<uint8_t> mac;
193   string if_name;
194   if (netlink_utils_->GetWiphyIndex(&phy_index) &&
195       netlink_utils_->GetInterfaceInfo(phy_index,
196                                        &if_name,
197                                        &if_index,
198                                        &mac)) {
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
201     // as a client.
202     if_tool_->SetUpState(if_name.c_str(), false);
203   }
204 }
205
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";
213     return false;
214   }
215
216   if (!RefreshWiphyIndex()) {
217     return false;
218   }
219
220   netlink_utils_->SubscribeRegDomainChange(
221           wiphy_index_,
222           std::bind(&Server::OnRegDomainChanged,
223           this,
224           _1));
225
226   if (!netlink_utils_->GetInterfaceInfo(wiphy_index_,
227                                         interface_name,
228                                         interface_index,
229                                         interface_mac_addr)) {
230     LOG(ERROR) << "Failed to get interface info from kernel";
231     return false;
232   }
233
234   return true;
235 }
236
237 bool Server::RefreshWiphyIndex() {
238   if (!netlink_utils_->GetWiphyIndex(&wiphy_index_)) {
239     LOG(ERROR) << "Failed to get wiphy index";
240     return false;
241   }
242   return true;
243 }
244
245 void Server::OnRegDomainChanged(std::string& country_code) {
246   if (country_code.empty()) {
247     LOG(INFO) << "Regulatory domain changed";
248   } else {
249     LOG(INFO) << "Regulatory domain changed to country: " << country_code;
250   }
251   LogSupportedBands();
252 }
253
254 void Server::LogSupportedBands() {
255   BandInfo band_info;
256   ScanCapabilities scan_capabilities;
257   WiphyFeatures wiphy_features;
258   netlink_utils_->GetWiphyInfo(wiphy_index_,
259                                &band_info,
260                                &scan_capabilities,
261                                &wiphy_features);
262
263   stringstream ss;
264   for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
265     ss << " " << band_info.band_2g[i];
266   }
267   LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
268   ss.str("");
269
270   for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
271     ss << " " << band_info.band_5g[i];
272   }
273   LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
274   ss.str("");
275
276   for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
277     ss << " " << band_info.band_dfs[i];
278   }
279   LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
280 }
281
282 void Server::BroadcastClientInterfaceReady(
283     sp<IClientInterface> network_interface) {
284   for (auto& it : interface_event_callbacks_) {
285     it->OnClientInterfaceReady(network_interface);
286   }
287 }
288
289 void Server::BroadcastApInterfaceReady(
290     sp<IApInterface> network_interface) {
291   for (auto& it : interface_event_callbacks_) {
292     it->OnApInterfaceReady(network_interface);
293   }
294 }
295
296 void Server::BroadcastClientInterfaceTornDown(
297     sp<IClientInterface> network_interface) {
298   for (auto& it : interface_event_callbacks_) {
299     it->OnClientTorndownEvent(network_interface);
300   }
301 }
302
303 void Server::BroadcastApInterfaceTornDown(
304     sp<IApInterface> network_interface) {
305   for (auto& it : interface_event_callbacks_) {
306     it->OnApTorndownEvent(network_interface);
307   }
308 }
309
310 }  // namespace wificond
311 }  // namespace android