OSDN Git Service

Expose wiphy capabilities to ScannerImpl
[android-x86/system-connectivity-wificond.git] / client_interface_impl.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/client_interface_impl.h"
18
19 #include <vector>
20
21 #include <android-base/logging.h>
22 #include <wifi_system/supplicant_manager.h>
23 #include <wifi_system/wifi.h>
24
25 #include "wificond/client_interface_binder.h"
26 #include "wificond/net/mlme_event.h"
27 #include "wificond/net/netlink_utils.h"
28 #include "wificond/scanning/scan_result.h"
29 #include "wificond/scanning/scan_utils.h"
30 #include "wificond/scanning/scanner_impl.h"
31
32 using android::net::wifi::IClientInterface;
33 using com::android::server::wifi::wificond::NativeScanResult;
34 using android::sp;
35 using android::wifi_system::InterfaceTool;
36 using android::wifi_system::SupplicantManager;
37
38 using std::string;
39 using std::unique_ptr;
40 using std::vector;
41
42 namespace android {
43 namespace wificond {
44
45 MlmeEventHandlerImpl::MlmeEventHandlerImpl(ClientInterfaceImpl* client_interface)
46     : client_interface_(client_interface) {
47 }
48
49 MlmeEventHandlerImpl::~MlmeEventHandlerImpl() {
50 }
51
52 void MlmeEventHandlerImpl::OnConnect(unique_ptr<MlmeConnectEvent> event) {
53   if (event->GetStatusCode() == 0) {
54     client_interface_->RefreshAssociateFreq();
55     client_interface_->bssid_ = event->GetBSSID();
56   }
57 }
58
59 void MlmeEventHandlerImpl::OnRoam(unique_ptr<MlmeRoamEvent> event) {
60   if (event->GetStatusCode() == 0) {
61     client_interface_->RefreshAssociateFreq();
62     client_interface_->bssid_ = event->GetBSSID();
63   }
64 }
65
66 void MlmeEventHandlerImpl::OnAssociate(unique_ptr<MlmeAssociateEvent> event) {
67   if (event->GetStatusCode() == 0) {
68     client_interface_->RefreshAssociateFreq();
69     client_interface_->bssid_ = event->GetBSSID();
70   }
71 }
72
73 ClientInterfaceImpl::ClientInterfaceImpl(
74     uint32_t wiphy_index,
75     const std::string& interface_name,
76     uint32_t interface_index,
77     const std::vector<uint8_t>& interface_mac_addr,
78     InterfaceTool* if_tool,
79     SupplicantManager* supplicant_manager,
80     NetlinkUtils* netlink_utils,
81     ScanUtils* scan_utils)
82     : wiphy_index_(wiphy_index),
83       interface_name_(interface_name),
84       interface_index_(interface_index),
85       interface_mac_addr_(interface_mac_addr),
86       if_tool_(if_tool),
87       supplicant_manager_(supplicant_manager),
88       netlink_utils_(netlink_utils),
89       scan_utils_(scan_utils),
90       mlme_event_handler_(new MlmeEventHandlerImpl(this)),
91       binder_(new ClientInterfaceBinder(this)) {
92   netlink_utils_->SubscribeMlmeEvent(
93       interface_index_,
94       mlme_event_handler_.get());
95   netlink_utils_->GetWiphyInfo(wiphy_index_,
96                                &band_info_,
97                                &scan_capabilities_,
98                                &wiphy_features_);
99   scanner_ = new ScannerImpl(interface_index_,
100                              band_info_,
101                              scan_capabilities_,
102                              wiphy_features_,
103                              scan_utils_);
104 }
105
106 ClientInterfaceImpl::~ClientInterfaceImpl() {
107   binder_->NotifyImplDead();
108   scanner_->Invalidate();
109   DisableSupplicant();
110   netlink_utils_->UnsubscribeMlmeEvent(interface_index_);
111   if_tool_->SetUpState(interface_name_.c_str(), false);
112 }
113
114 sp<android::net::wifi::IClientInterface> ClientInterfaceImpl::GetBinder() const {
115   return binder_;
116 }
117
118 bool ClientInterfaceImpl::EnableSupplicant() {
119   return supplicant_manager_->StartSupplicant();
120 }
121
122 bool ClientInterfaceImpl::DisableSupplicant() {
123   return supplicant_manager_->StopSupplicant();
124 }
125
126 bool ClientInterfaceImpl::GetPacketCounters(vector<int32_t>* out_packet_counters) {
127   StationInfo station_info;
128   if (!netlink_utils_->GetStationInfo(interface_index_,
129                                       interface_mac_addr_,
130                                       &station_info)) {
131     return false;
132   }
133   out_packet_counters->push_back(station_info.station_tx_packets);
134   out_packet_counters->push_back(station_info.station_tx_failed);
135
136   return true;
137 }
138
139 bool ClientInterfaceImpl::SignalPoll(vector<int32_t>* out_signal_poll_results) {
140   StationInfo station_info;
141   if (!netlink_utils_->GetStationInfo(interface_index_,
142                                       bssid_,
143                                       &station_info)) {
144     return false;
145   }
146   out_signal_poll_results->push_back(
147       static_cast<int32_t>(station_info.current_rssi));
148   // Convert from 100kbit/s to Mbps.
149   out_signal_poll_results->push_back(
150       static_cast<int32_t>(station_info.station_tx_bitrate/10));
151   // Association frequency.
152   out_signal_poll_results->push_back(
153       static_cast<int32_t>(associate_freq_));
154
155   return true;
156 }
157
158 const vector<uint8_t>& ClientInterfaceImpl::GetMacAddress() {
159   return interface_mac_addr_;
160 }
161
162 bool ClientInterfaceImpl::requestANQP(
163       const ::std::vector<uint8_t>& bssid,
164       const ::android::sp<::android::net::wifi::IANQPDoneCallback>& callback) {
165   // TODO(nywang): query ANQP information from wpa_supplicant.
166   return true;
167 }
168
169 bool ClientInterfaceImpl::RefreshAssociateFreq() {
170   // wpa_supplicant fetches associate frequency using the latest scan result.
171   // We should follow the same method here before we find a better solution.
172   std::vector<NativeScanResult> scan_results;
173   if (!scan_utils_->GetScanResult(interface_index_, &scan_results)) {
174     return false;
175   }
176   for (auto& scan_result : scan_results) {
177     if (scan_result.associated) {
178       associate_freq_ = scan_result.frequency;
179     }
180   }
181   return false;
182 }
183
184 }  // namespace wificond
185 }  // namespace android