2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "wificond/client_interface_impl.h"
21 #include <android-base/logging.h>
22 #include <wifi_system/supplicant_manager.h>
23 #include <wifi_system/wifi.h>
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"
32 using android::net::wifi::IClientInterface;
33 using com::android::server::wifi::wificond::NativeScanResult;
35 using android::wifi_system::InterfaceTool;
36 using android::wifi_system::SupplicantManager;
39 using std::unique_ptr;
45 MlmeEventHandlerImpl::MlmeEventHandlerImpl(ClientInterfaceImpl* client_interface)
46 : client_interface_(client_interface) {
49 MlmeEventHandlerImpl::~MlmeEventHandlerImpl() {
52 void MlmeEventHandlerImpl::OnConnect(unique_ptr<MlmeConnectEvent> event) {
53 if (event->GetStatusCode() == 0) {
54 client_interface_->RefreshAssociateFreq();
55 client_interface_->bssid_ = event->GetBSSID();
59 void MlmeEventHandlerImpl::OnRoam(unique_ptr<MlmeRoamEvent> event) {
60 if (event->GetStatusCode() == 0) {
61 client_interface_->RefreshAssociateFreq();
62 client_interface_->bssid_ = event->GetBSSID();
66 void MlmeEventHandlerImpl::OnAssociate(unique_ptr<MlmeAssociateEvent> event) {
67 if (event->GetStatusCode() == 0) {
68 client_interface_->RefreshAssociateFreq();
69 client_interface_->bssid_ = event->GetBSSID();
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),
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 scanner_(new ScannerImpl(interface_index, scan_utils)) {
91 netlink_utils_->SubscribeMlmeEvent(
93 mlme_event_handler_.get());
96 ClientInterfaceImpl::~ClientInterfaceImpl() {
97 binder_->NotifyImplDead();
98 scanner_->Invalidate();
100 netlink_utils_->UnsubscribeMlmeEvent(interface_index_);
101 if_tool_->SetUpState(interface_name_.c_str(), false);
104 sp<android::net::wifi::IClientInterface> ClientInterfaceImpl::GetBinder() const {
108 bool ClientInterfaceImpl::EnableSupplicant() {
109 return supplicant_manager_->StartSupplicant();
112 bool ClientInterfaceImpl::DisableSupplicant() {
113 return supplicant_manager_->StopSupplicant();
116 bool ClientInterfaceImpl::GetPacketCounters(vector<int32_t>* out_packet_counters) {
117 StationInfo station_info;
118 if (!netlink_utils_->GetStationInfo(interface_index_,
123 out_packet_counters->push_back(station_info.station_tx_packets);
124 out_packet_counters->push_back(station_info.station_tx_failed);
129 bool ClientInterfaceImpl::SignalPoll(vector<int32_t>* out_signal_poll_results) {
130 StationInfo station_info;
131 if (!netlink_utils_->GetStationInfo(interface_index_,
136 out_signal_poll_results->push_back(
137 static_cast<int32_t>(station_info.current_rssi));
138 // Convert from 100kbit/s to Mbps.
139 out_signal_poll_results->push_back(
140 static_cast<int32_t>(station_info.station_tx_bitrate/10));
141 // Association frequency.
142 out_signal_poll_results->push_back(
143 static_cast<int32_t>(associate_freq_));
148 const vector<uint8_t>& ClientInterfaceImpl::GetMacAddress() {
149 return interface_mac_addr_;
152 bool ClientInterfaceImpl::requestANQP(
153 const ::std::vector<uint8_t>& bssid,
154 const ::android::sp<::android::net::wifi::IANQPDoneCallback>& callback) {
155 // TODO(nywang): query ANQP information from wpa_supplicant.
159 bool ClientInterfaceImpl::RefreshAssociateFreq() {
160 // wpa_supplicant fetches associate frequency using the latest scan result.
161 // We should follow the same method here before we find a better solution.
162 std::vector<NativeScanResult> scan_results;
163 if (!scan_utils_->GetScanResult(interface_index_, &scan_results)) {
166 for (auto& scan_result : scan_results) {
167 if (scan_result.associated) {
168 associate_freq_ = scan_result.frequency;
174 } // namespace wificond
175 } // namespace android