OSDN Git Service

wificond: return association frequency upon signal poll
[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
31 using android::net::wifi::IClientInterface;
32 using com::android::server::wifi::wificond::NativeScanResult;
33 using android::sp;
34 using android::wifi_system::InterfaceTool;
35 using android::wifi_system::SupplicantManager;
36
37 using namespace std::placeholders;
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     const std::string& interface_name,
75     uint32_t interface_index,
76     const std::vector<uint8_t>& interface_mac_addr,
77     InterfaceTool* if_tool,
78     SupplicantManager* supplicant_manager,
79     NetlinkUtils* netlink_utils,
80     ScanUtils* scan_utils)
81     : interface_name_(interface_name),
82       interface_index_(interface_index),
83       interface_mac_addr_(interface_mac_addr),
84       if_tool_(if_tool),
85       supplicant_manager_(supplicant_manager),
86       netlink_utils_(netlink_utils),
87       scan_utils_(scan_utils),
88       mlme_event_handler_(new MlmeEventHandlerImpl(this)),
89       binder_(new ClientInterfaceBinder(this)) {
90   scan_utils_->SubscribeScanResultNotification(
91       interface_index_,
92       std::bind(&ClientInterfaceImpl::OnScanResultsReady,
93                 this,
94                 _1, _2, _3, _4));
95   netlink_utils_->SubscribeMlmeEvent(
96       interface_index_,
97       mlme_event_handler_.get());
98 }
99
100 ClientInterfaceImpl::~ClientInterfaceImpl() {
101   binder_->NotifyImplDead();
102   DisableSupplicant();
103   scan_utils_->UnsubscribeScanResultNotification(interface_index_);
104   netlink_utils_->UnsubscribeMlmeEvent(interface_index_);
105   if_tool_->SetUpState(interface_name_.c_str(), false);
106 }
107
108 sp<android::net::wifi::IClientInterface> ClientInterfaceImpl::GetBinder() const {
109   return binder_;
110 }
111
112 bool ClientInterfaceImpl::EnableSupplicant() {
113   return supplicant_manager_->StartSupplicant();
114 }
115
116 bool ClientInterfaceImpl::DisableSupplicant() {
117   return supplicant_manager_->StopSupplicant();
118 }
119
120 bool ClientInterfaceImpl::GetPacketCounters(vector<int32_t>* out_packet_counters) {
121   StationInfo station_info;
122   if (!netlink_utils_->GetStationInfo(interface_index_,
123                                       interface_mac_addr_,
124                                       &station_info)) {
125     return false;
126   }
127   out_packet_counters->push_back(station_info.station_tx_packets);
128   out_packet_counters->push_back(station_info.station_tx_failed);
129
130   return true;
131 }
132
133 bool ClientInterfaceImpl::SignalPoll(vector<int32_t>* out_signal_poll_results) {
134   StationInfo station_info;
135   if (!netlink_utils_->GetStationInfo(interface_index_,
136                                       bssid_,
137                                       &station_info)) {
138     return false;
139   }
140   out_signal_poll_results->push_back(
141       static_cast<int32_t>(station_info.current_rssi));
142   // Convert from 100kbit/s to Mbps.
143   out_signal_poll_results->push_back(
144       static_cast<int32_t>(station_info.station_tx_bitrate/10));
145   // Association frequency.
146   out_signal_poll_results->push_back(
147       static_cast<int32_t>(associate_freq_));
148
149   return true;
150 }
151
152 const vector<uint8_t>& ClientInterfaceImpl::GetMacAddress() {
153   return interface_mac_addr_;
154 }
155
156 void ClientInterfaceImpl::OnScanResultsReady(
157                          uint32_t interface_index,
158                          bool aborted,
159                          std::vector<std::vector<uint8_t>>& ssids,
160                          std::vector<uint32_t>& frequencies) {
161   if (aborted) {
162     LOG(ERROR) << "Scan aborted";
163     return;
164   }
165   vector<NativeScanResult> scan_results;
166   // TODO(nywang): Find a way to differentiate scan results for
167   // internel/external scan request. This is useful when location is
168   // scanning using regular NL80211 commands.
169   scan_utils_->GetScanResult(interface_index, &scan_results);
170   // TODO(nywang): Send these scan results back to java framework.
171 }
172
173 void ClientInterfaceImpl::OnSchedScanResultsReady(
174                          uint32_t interface_index) {
175   vector<NativeScanResult> scan_results;
176   scan_utils_->GetScanResult(interface_index, &scan_results);
177   // TODO(nywang): Send these scan results back to java framework.
178 }
179
180 bool ClientInterfaceImpl::requestANQP(
181       const ::std::vector<uint8_t>& bssid,
182       const ::android::sp<::android::net::wifi::IANQPDoneCallback>& callback) {
183   // TODO(nywang): query ANQP information from wpa_supplicant.
184   return true;
185 }
186
187 bool ClientInterfaceImpl::RefreshAssociateFreq() {
188   // wpa_supplicant fetches associate frequency using the latest scan result.
189   // We should follow the same method here before we find a better solution.
190   std::vector<NativeScanResult> scan_results;
191   if (!scan_utils_->GetScanResult(interface_index_, &scan_results)) {
192     return false;
193   }
194   for (auto& scan_result : scan_results) {
195     if (scan_result.associated) {
196       associate_freq_ = scan_result.frequency;
197     }
198   }
199   return false;
200 }
201
202 }  // namespace wificond
203 }  // namespace android