OSDN Git Service

Merge "Do not request MAC address randomization when associated" am: cd9ee43fd7
[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_->is_associated_ = true;
55     client_interface_->RefreshAssociateFreq();
56     client_interface_->bssid_ = event->GetBSSID();
57   } else {
58     client_interface_->is_associated_ = false;
59     client_interface_->bssid_.clear();
60   }
61 }
62
63 void MlmeEventHandlerImpl::OnRoam(unique_ptr<MlmeRoamEvent> event) {
64   if (event->GetStatusCode() == 0) {
65     client_interface_->is_associated_ = true;
66     client_interface_->RefreshAssociateFreq();
67     client_interface_->bssid_ = event->GetBSSID();
68   } else {
69     client_interface_->is_associated_ = false;
70     client_interface_->bssid_.clear();
71   }
72 }
73
74 void MlmeEventHandlerImpl::OnAssociate(unique_ptr<MlmeAssociateEvent> event) {
75   if (event->GetStatusCode() == 0) {
76     client_interface_->is_associated_ = true;
77     client_interface_->RefreshAssociateFreq();
78     client_interface_->bssid_ = event->GetBSSID();
79   } else {
80     client_interface_->is_associated_ = false;
81     client_interface_->bssid_.clear();
82   }
83 }
84
85 void MlmeEventHandlerImpl::OnDisconnect(unique_ptr<MlmeDisconnectEvent> event) {
86   client_interface_->is_associated_ = false;
87   client_interface_->bssid_.clear();
88 }
89
90 void MlmeEventHandlerImpl::OnDisassociate(unique_ptr<MlmeDisassociateEvent> event) {
91   client_interface_->is_associated_ = false;
92   client_interface_->bssid_.clear();
93 }
94
95
96 ClientInterfaceImpl::ClientInterfaceImpl(
97     uint32_t wiphy_index,
98     const std::string& interface_name,
99     uint32_t interface_index,
100     const std::vector<uint8_t>& interface_mac_addr,
101     InterfaceTool* if_tool,
102     SupplicantManager* supplicant_manager,
103     NetlinkUtils* netlink_utils,
104     ScanUtils* scan_utils)
105     : wiphy_index_(wiphy_index),
106       interface_name_(interface_name),
107       interface_index_(interface_index),
108       interface_mac_addr_(interface_mac_addr),
109       if_tool_(if_tool),
110       supplicant_manager_(supplicant_manager),
111       netlink_utils_(netlink_utils),
112       scan_utils_(scan_utils),
113       mlme_event_handler_(new MlmeEventHandlerImpl(this)),
114       binder_(new ClientInterfaceBinder(this)),
115       is_associated_(false) {
116   netlink_utils_->SubscribeMlmeEvent(
117       interface_index_,
118       mlme_event_handler_.get());
119   if (!netlink_utils_->GetWiphyInfo(wiphy_index_,
120                                &band_info_,
121                                &scan_capabilities_,
122                                &wiphy_features_)) {
123     LOG(ERROR) << "Failed to get wiphy info from kernel";
124   }
125   LOG(INFO) << "create scanner for interface with index: "
126             << (int)interface_index_;
127   scanner_ = new ScannerImpl(wiphy_index,
128                              interface_index_,
129                              scan_capabilities_,
130                              wiphy_features_,
131                              this,
132                              netlink_utils_,
133                              scan_utils_);
134 }
135
136 ClientInterfaceImpl::~ClientInterfaceImpl() {
137   binder_->NotifyImplDead();
138   scanner_->Invalidate();
139   DisableSupplicant();
140   netlink_utils_->UnsubscribeMlmeEvent(interface_index_);
141   if_tool_->SetUpState(interface_name_.c_str(), false);
142 }
143
144 sp<android::net::wifi::IClientInterface> ClientInterfaceImpl::GetBinder() const {
145   return binder_;
146 }
147
148 bool ClientInterfaceImpl::EnableSupplicant() {
149   return supplicant_manager_->StartSupplicant();
150 }
151
152 bool ClientInterfaceImpl::DisableSupplicant() {
153   return supplicant_manager_->StopSupplicant();
154 }
155
156 bool ClientInterfaceImpl::GetPacketCounters(vector<int32_t>* out_packet_counters) {
157   StationInfo station_info;
158   if (!netlink_utils_->GetStationInfo(interface_index_,
159                                       interface_mac_addr_,
160                                       &station_info)) {
161     return false;
162   }
163   out_packet_counters->push_back(station_info.station_tx_packets);
164   out_packet_counters->push_back(station_info.station_tx_failed);
165
166   return true;
167 }
168
169 bool ClientInterfaceImpl::SignalPoll(vector<int32_t>* out_signal_poll_results) {
170   StationInfo station_info;
171   if (!netlink_utils_->GetStationInfo(interface_index_,
172                                       bssid_,
173                                       &station_info)) {
174     return false;
175   }
176   out_signal_poll_results->push_back(
177       static_cast<int32_t>(station_info.current_rssi));
178   // Convert from 100kbit/s to Mbps.
179   out_signal_poll_results->push_back(
180       static_cast<int32_t>(station_info.station_tx_bitrate/10));
181   // Association frequency.
182   out_signal_poll_results->push_back(
183       static_cast<int32_t>(associate_freq_));
184
185   return true;
186 }
187
188 const vector<uint8_t>& ClientInterfaceImpl::GetMacAddress() {
189   return interface_mac_addr_;
190 }
191
192 bool ClientInterfaceImpl::requestANQP(
193       const ::std::vector<uint8_t>& bssid,
194       const ::android::sp<::android::net::wifi::IANQPDoneCallback>& callback) {
195   // TODO(nywang): query ANQP information from wpa_supplicant.
196   return true;
197 }
198
199 bool ClientInterfaceImpl::RefreshAssociateFreq() {
200   // wpa_supplicant fetches associate frequency using the latest scan result.
201   // We should follow the same method here before we find a better solution.
202   std::vector<NativeScanResult> scan_results;
203   if (!scan_utils_->GetScanResult(interface_index_, &scan_results)) {
204     return false;
205   }
206   for (auto& scan_result : scan_results) {
207     if (scan_result.associated) {
208       associate_freq_ = scan_result.frequency;
209     }
210   }
211   return false;
212 }
213
214 bool ClientInterfaceImpl::IsAssociated() {
215   return is_associated_;
216 }
217
218 }  // namespace wificond
219 }  // namespace android