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/ap_interface_impl.h"
18
19 #include <android-base/logging.h>
20
21 #include "wificond/net/netlink_utils.h"
22
23 #include "wificond/ap_interface_binder.h"
24 #include "wificond/logging_utils.h"
25
26 using android::net::wifi::IApInterface;
27 using android::wifi_system::InterfaceTool;
28 using com::android::server::wifi::wificond::NativeWifiClient;
29 using std::array;
30 using std::endl;
31 using std::string;
32 using std::unique_ptr;
33 using std::vector;
34
35 using namespace std::placeholders;
36
37 namespace android {
38 namespace wificond {
39
ApInterfaceImpl(const string & interface_name,uint32_t interface_index,NetlinkUtils * netlink_utils,InterfaceTool * if_tool)40 ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
41 uint32_t interface_index,
42 NetlinkUtils* netlink_utils,
43 InterfaceTool* if_tool)
44 : interface_name_(interface_name),
45 interface_index_(interface_index),
46 netlink_utils_(netlink_utils),
47 if_tool_(if_tool),
48 binder_(new ApInterfaceBinder(this)) {
49 // This log keeps compiler happy.
50 LOG(DEBUG) << "Created ap interface " << interface_name_
51 << " with index " << interface_index_;
52
53 netlink_utils_->SubscribeStationEvent(
54 interface_index_,
55 std::bind(&ApInterfaceImpl::OnStationEvent,
56 this,
57 _1, _2));
58 netlink_utils_->SubscribeChannelSwitchEvent(
59 interface_index_,
60 std::bind(&ApInterfaceImpl::OnChannelSwitchEvent, this, _1, _2));
61
62 }
63
~ApInterfaceImpl()64 ApInterfaceImpl::~ApInterfaceImpl() {
65 binder_->NotifyImplDead();
66 if_tool_->SetUpState(interface_name_.c_str(), false);
67 netlink_utils_->UnsubscribeStationEvent(interface_index_);
68 netlink_utils_->UnsubscribeChannelSwitchEvent(interface_index_);
69 }
70
GetBinder() const71 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
72 return binder_;
73 }
74
Dump(std::stringstream * ss) const75 void ApInterfaceImpl::Dump(std::stringstream* ss) const {
76 *ss << "------- Dump of AP interface with index: "
77 << interface_index_ << " and name: " << interface_name_
78 << "-------" << endl;
79 *ss << "Number of connected access point clients: "
80 << connected_clients_.size() << endl;
81 *ss << "------- Dump End -------" << endl;
82 }
83
OnStationEvent(StationEvent event,const array<uint8_t,ETH_ALEN> & mac_address)84 void ApInterfaceImpl::OnStationEvent(
85 StationEvent event,
86 const array<uint8_t, ETH_ALEN>& mac_address) {
87 if (event == NEW_STATION) {
88 LOG(INFO) << "New station "
89 << LoggingUtils::GetMacString(mac_address)
90 << " connected to hotspot"
91 << " using interface "
92 << interface_name_;
93
94 vector<uint8_t> mac_return_address = vector<uint8_t>(mac_address.begin(), mac_address.end());
95 const auto iterator = std::find_if(connected_clients_.begin(), connected_clients_.end(),
96 [&] (NativeWifiClient const& p) {
97 return p.macAddress == mac_return_address;
98 });
99
100 if (iterator == connected_clients_.end()) {
101 NativeWifiClient station;
102 station.macAddress = mac_return_address;
103 connected_clients_.push_back(station);
104
105 LOG(INFO) << "Sending notifications for station event";
106 binder_->NotifyConnectedClientsChanged(connected_clients_);
107 }
108 } else if (event == DEL_STATION) {
109 LOG(INFO) << "Station "
110 << LoggingUtils::GetMacString(mac_address)
111 << " disassociated from hotspot";
112 if (connected_clients_.size() <= 0) {
113 LOG(ERROR) << "Received DEL_STATION event when station counter is: "
114 << connected_clients_.size();
115 return;
116 } else {
117 vector<uint8_t> mac_return_address = vector<uint8_t>(mac_address.begin(), mac_address.end());
118 const auto iterator = std::find_if(connected_clients_.begin(), connected_clients_.end(),
119 [&] (NativeWifiClient const& p) {
120 return p.macAddress == mac_return_address;
121 });
122
123 if (iterator != connected_clients_.end()) {
124 connected_clients_.erase(iterator);
125
126 LOG(DEBUG) << "Sending notifications for station event";
127 binder_->NotifyConnectedClientsChanged(connected_clients_);
128 }
129 }
130 }
131 }
132
133
OnChannelSwitchEvent(uint32_t frequency,ChannelBandwidth bandwidth)134 void ApInterfaceImpl::OnChannelSwitchEvent(uint32_t frequency,
135 ChannelBandwidth bandwidth) {
136 LOG(INFO) << "New channel on frequency: " << frequency
137 << " with bandwidth: " << LoggingUtils::GetBandwidthString(bandwidth);
138 binder_->NotifySoftApChannelSwitched(frequency, bandwidth);
139 }
140
GetConnectedClients() const141 std::vector<NativeWifiClient> ApInterfaceImpl::GetConnectedClients() const {
142 return connected_clients_;
143 }
144
145 } // namespace wificond
146 } // namespace android
147