OSDN Git Service

Always set interface down in object destruction
[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/netlink_utils.h"
27 #include "wificond/scanning/scan_result.h"
28 #include "wificond/scanning/scan_utils.h"
29
30 using android::net::wifi::IClientInterface;
31 using android::sp;
32 using android::wifi_system::InterfaceTool;
33 using android::wifi_system::SupplicantManager;
34
35 using namespace std::placeholders;
36 using std::string;
37 using std::unique_ptr;
38 using std::vector;
39
40 namespace android {
41 namespace wificond {
42
43 ClientInterfaceImpl::ClientInterfaceImpl(
44     const std::string& interface_name,
45     uint32_t interface_index,
46     const std::vector<uint8_t>& interface_mac_addr,
47     InterfaceTool* if_tool,
48     SupplicantManager* supplicant_manager,
49     NetlinkUtils* netlink_utils,
50     ScanUtils* scan_utils)
51     : interface_name_(interface_name),
52       interface_index_(interface_index),
53       interface_mac_addr_(interface_mac_addr),
54       if_tool_(if_tool),
55       supplicant_manager_(supplicant_manager),
56       netlink_utils_(netlink_utils),
57       scan_utils_(scan_utils),
58       binder_(new ClientInterfaceBinder(this)) {
59   scan_utils_->SubscribeScanResultNotification(
60       interface_index_,
61       std::bind(&ClientInterfaceImpl::OnScanResultsReady,
62                 this,
63                 _1, _2, _3, _4));
64 }
65
66 ClientInterfaceImpl::~ClientInterfaceImpl() {
67   binder_->NotifyImplDead();
68   DisableSupplicant();
69   scan_utils_->UnsubscribeScanResultNotification(interface_index_);
70   if_tool_->SetUpState(interface_name_.c_str(), false);
71 }
72
73 sp<android::net::wifi::IClientInterface> ClientInterfaceImpl::GetBinder() const {
74   return binder_;
75 }
76
77 bool ClientInterfaceImpl::EnableSupplicant() {
78   return supplicant_manager_->StartSupplicant();
79 }
80
81 bool ClientInterfaceImpl::DisableSupplicant() {
82   return supplicant_manager_->StopSupplicant();
83 }
84
85 bool ClientInterfaceImpl::GetPacketCounters(vector<int32_t>* out_packet_counters) {
86   StationInfo station_info;
87   if (!netlink_utils_->GetStationInfo(interface_index_,
88                                       interface_mac_addr_,
89                                       &station_info)) {
90     return false;
91   }
92   out_packet_counters->push_back(station_info.station_tx_packets);
93   out_packet_counters->push_back(station_info.station_tx_failed);
94
95   return true;
96 }
97
98 bool ClientInterfaceImpl::SignalPoll(vector<int32_t>* out_signal_poll_results) {
99   StationInfo station_info;
100   if (!netlink_utils_->GetStationInfo(interface_index_,
101                                       interface_mac_addr_,
102                                       &station_info)) {
103     return false;
104   }
105   out_signal_poll_results->push_back(
106       static_cast<int32_t>(station_info.current_rssi));
107   // Convert from 100kbit/s to Mbps.
108   out_signal_poll_results->push_back(
109       static_cast<int32_t>(station_info.station_tx_bitrate/10));
110
111   return true;
112 }
113
114 const vector<uint8_t>& ClientInterfaceImpl::GetMacAddress() {
115   return interface_mac_addr_;
116 }
117
118 void ClientInterfaceImpl::OnScanResultsReady(
119                          uint32_t interface_index,
120                          bool aborted,
121                          std::vector<std::vector<uint8_t>>& ssids,
122                          std::vector<uint32_t>& frequencies) {
123   if (aborted) {
124     LOG(ERROR) << "Scan aborted";
125     return;
126   }
127   vector<ScanResult> scan_results;
128   // TODO(nywang): Find a way to differentiate scan results for
129   // internel/external scan request. This is useful when location is
130   // scanning using regular NL80211 commands.
131   scan_utils_->GetScanResult(interface_index, &scan_results);
132   // TODO(nywang): Send these scan results back to java framework.
133 }
134
135 }  // namespace wificond
136 }  // namespace android