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::nl80211::IApInterface;
27 using android::wifi_system::InterfaceTool;
28 using android::net::wifi::nl80211::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 << "------- Dump End -------" << endl;
80 }
81 
OnStationEvent(StationEvent event,const array<uint8_t,ETH_ALEN> & mac_address)82 void ApInterfaceImpl::OnStationEvent(
83     StationEvent event,
84     const array<uint8_t, ETH_ALEN>& mac_address) {
85   vector<uint8_t> mac_return_address = vector<uint8_t>(mac_address.begin(), mac_address.end());
86 
87   NativeWifiClient station;
88   station.mac_address_ = mac_return_address;
89 
90   if (event == NEW_STATION) {
91     LOG(INFO) << "New station "
92               << LoggingUtils::GetMacString(mac_address)
93               << " connected to hotspot"
94               << " using interface "
95               << interface_name_;
96     LOG(INFO) << "Sending notifications for station add event";
97     binder_->NotifyConnectedClientsChanged(station, true);
98   } else if (event == DEL_STATION) {
99     LOG(INFO) << "Station "
100               << LoggingUtils::GetMacString(mac_address)
101               << " disassociated from hotspot";
102     LOG(DEBUG) << "Sending notifications for station leave event";
103     binder_->NotifyConnectedClientsChanged(station, false);
104   }
105 }
106 
107 
OnChannelSwitchEvent(uint32_t frequency,ChannelBandwidth bandwidth)108 void ApInterfaceImpl::OnChannelSwitchEvent(uint32_t frequency,
109                                            ChannelBandwidth bandwidth) {
110   LOG(INFO) << "New channel on frequency: " << frequency
111             << " with bandwidth: " << LoggingUtils::GetBandwidthString(bandwidth);
112   binder_->NotifySoftApChannelSwitched(frequency, bandwidth);
113 }
114 
115 }  // namespace wificond
116 }  // namespace android
117