OSDN Git Service

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