Christopher Wiley | f229bbe | 2016-06-22 17:20:04 -0700 | [diff] [blame] | 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 | |
Ningyuan Wang | 808c670 | 2016-07-15 16:47:54 -0700 | [diff] [blame] | 17 | #include "wificond/ap_interface_impl.h" |
Christopher Wiley | f229bbe | 2016-06-22 17:20:04 -0700 | [diff] [blame] | 18 | |
Christopher Wiley | c06a8b8 | 2016-07-28 15:07:47 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | |
Ningyuan Wang | 9136ae3 | 2017-02-28 15:49:08 -0800 | [diff] [blame] | 21 | #include "wificond/net/netlink_utils.h" |
| 22 | |
Ningyuan Wang | 808c670 | 2016-07-15 16:47:54 -0700 | [diff] [blame] | 23 | #include "wificond/ap_interface_binder.h" |
Ningyuan Wang | cf51f86 | 2017-03-08 08:44:39 -0800 | [diff] [blame] | 24 | #include "wificond/logging_utils.h" |
Christopher Wiley | f229bbe | 2016-06-22 17:20:04 -0700 | [diff] [blame] | 25 | |
Etan Cohen | 067e119 | 2020-02-15 17:36:36 -0800 | [diff] [blame] | 26 | using android::net::wifi::nl80211::IApInterface; |
Christopher Wiley | d42ca5a | 2016-08-22 17:42:58 -0700 | [diff] [blame] | 27 | using android::wifi_system::InterfaceTool; |
Etan Cohen | 067e119 | 2020-02-15 17:36:36 -0800 | [diff] [blame] | 28 | using android::net::wifi::nl80211::NativeWifiClient; |
David Su | 61ea307 | 2018-10-02 15:14:29 -0700 | [diff] [blame] | 29 | using std::array; |
Ningyuan Wang | 8ec7696 | 2017-04-07 16:03:33 -0700 | [diff] [blame] | 30 | using std::endl; |
Christopher Wiley | 1f8dd45 | 2016-07-18 15:30:58 -0700 | [diff] [blame] | 31 | using std::string; |
| 32 | using std::unique_ptr; |
James Mattis | 3901204 | 2019-10-04 18:37:43 -0700 | [diff] [blame] | 33 | using std::vector; |
Christopher Wiley | 1f8dd45 | 2016-07-18 15:30:58 -0700 | [diff] [blame] | 34 | |
Ningyuan Wang | cf51f86 | 2017-03-08 08:44:39 -0800 | [diff] [blame] | 35 | using namespace std::placeholders; |
| 36 | |
Christopher Wiley | f229bbe | 2016-06-22 17:20:04 -0700 | [diff] [blame] | 37 | namespace android { |
| 38 | namespace wificond { |
| 39 | |
Christopher Wiley | 1f8dd45 | 2016-07-18 15:30:58 -0700 | [diff] [blame] | 40 | ApInterfaceImpl::ApInterfaceImpl(const string& interface_name, |
Ningyuan Wang | 9bc59a0 | 2016-08-11 09:59:22 -0700 | [diff] [blame] | 41 | uint32_t interface_index, |
Ningyuan Wang | 9136ae3 | 2017-02-28 15:49:08 -0800 | [diff] [blame] | 42 | NetlinkUtils* netlink_utils, |
Roshan Pius | 1731b15 | 2018-02-11 18:29:22 -0800 | [diff] [blame] | 43 | InterfaceTool* if_tool) |
Christopher Wiley | 1f8dd45 | 2016-07-18 15:30:58 -0700 | [diff] [blame] | 44 | : interface_name_(interface_name), |
Ningyuan Wang | 9bc59a0 | 2016-08-11 09:59:22 -0700 | [diff] [blame] | 45 | interface_index_(interface_index), |
Ningyuan Wang | 9136ae3 | 2017-02-28 15:49:08 -0800 | [diff] [blame] | 46 | netlink_utils_(netlink_utils), |
Christopher Wiley | d42ca5a | 2016-08-22 17:42:58 -0700 | [diff] [blame] | 47 | if_tool_(if_tool), |
James Mattis | 3901204 | 2019-10-04 18:37:43 -0700 | [diff] [blame] | 48 | binder_(new ApInterfaceBinder(this)) { |
Ningyuan Wang | 9bc59a0 | 2016-08-11 09:59:22 -0700 | [diff] [blame] | 49 | // This log keeps compiler happy. |
| 50 | LOG(DEBUG) << "Created ap interface " << interface_name_ |
| 51 | << " with index " << interface_index_; |
Ningyuan Wang | cf51f86 | 2017-03-08 08:44:39 -0800 | [diff] [blame] | 52 | |
| 53 | netlink_utils_->SubscribeStationEvent( |
| 54 | interface_index_, |
| 55 | std::bind(&ApInterfaceImpl::OnStationEvent, |
| 56 | this, |
| 57 | _1, _2)); |
Ningyuan Wang | d5cfc11 | 2018-01-29 15:50:49 -0800 | [diff] [blame] | 58 | netlink_utils_->SubscribeChannelSwitchEvent( |
| 59 | interface_index_, |
Ningyuan Wang | 791d553 | 2018-02-01 14:04:18 -0800 | [diff] [blame] | 60 | std::bind(&ApInterfaceImpl::OnChannelSwitchEvent, this, _1, _2)); |
Ningyuan Wang | d5cfc11 | 2018-01-29 15:50:49 -0800 | [diff] [blame] | 61 | |
Christopher Wiley | f229bbe | 2016-06-22 17:20:04 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | ApInterfaceImpl::~ApInterfaceImpl() { |
| 65 | binder_->NotifyImplDead(); |
Christopher Wiley | 4fc3792 | 2016-09-22 08:57:40 -0700 | [diff] [blame] | 66 | if_tool_->SetUpState(interface_name_.c_str(), false); |
Ningyuan Wang | cf51f86 | 2017-03-08 08:44:39 -0800 | [diff] [blame] | 67 | netlink_utils_->UnsubscribeStationEvent(interface_index_); |
Ningyuan Wang | d5cfc11 | 2018-01-29 15:50:49 -0800 | [diff] [blame] | 68 | netlink_utils_->UnsubscribeChannelSwitchEvent(interface_index_); |
Christopher Wiley | f229bbe | 2016-06-22 17:20:04 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | sp<IApInterface> ApInterfaceImpl::GetBinder() const { |
| 72 | return binder_; |
| 73 | } |
| 74 | |
Ningyuan Wang | 8ec7696 | 2017-04-07 16:03:33 -0700 | [diff] [blame] | 75 | void ApInterfaceImpl::Dump(std::stringstream* ss) const { |
| 76 | *ss << "------- Dump of AP interface with index: " |
| 77 | << interface_index_ << " and name: " << interface_name_ |
| 78 | << "-------" << endl; |
Ningyuan Wang | 8ec7696 | 2017-04-07 16:03:33 -0700 | [diff] [blame] | 79 | *ss << "------- Dump End -------" << endl; |
| 80 | } |
| 81 | |
David Su | 61ea307 | 2018-10-02 15:14:29 -0700 | [diff] [blame] | 82 | void ApInterfaceImpl::OnStationEvent( |
| 83 | StationEvent event, |
| 84 | const array<uint8_t, ETH_ALEN>& mac_address) { |
lesl | 414ba9c | 2019-12-04 15:33:20 +0800 | [diff] [blame] | 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 | |
Ningyuan Wang | cf51f86 | 2017-03-08 08:44:39 -0800 | [diff] [blame] | 90 | if (event == NEW_STATION) { |
| 91 | LOG(INFO) << "New station " |
| 92 | << LoggingUtils::GetMacString(mac_address) |
James Mattis | 3901204 | 2019-10-04 18:37:43 -0700 | [diff] [blame] | 93 | << " connected to hotspot" |
| 94 | << " using interface " |
| 95 | << interface_name_; |
lesl | 414ba9c | 2019-12-04 15:33:20 +0800 | [diff] [blame] | 96 | LOG(INFO) << "Sending notifications for station add event"; |
| 97 | binder_->NotifyConnectedClientsChanged(station, true); |
Ningyuan Wang | cf51f86 | 2017-03-08 08:44:39 -0800 | [diff] [blame] | 98 | } else if (event == DEL_STATION) { |
| 99 | LOG(INFO) << "Station " |
| 100 | << LoggingUtils::GetMacString(mac_address) |
| 101 | << " disassociated from hotspot"; |
lesl | 414ba9c | 2019-12-04 15:33:20 +0800 | [diff] [blame] | 102 | LOG(DEBUG) << "Sending notifications for station leave event"; |
| 103 | binder_->NotifyConnectedClientsChanged(station, false); |
Etan Cohen | fc5cbea | 2019-11-26 16:53:14 -0800 | [diff] [blame] | 104 | } |
Ningyuan Wang | cf51f86 | 2017-03-08 08:44:39 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Ningyuan Wang | d5cfc11 | 2018-01-29 15:50:49 -0800 | [diff] [blame] | 107 | |
Ningyuan Wang | 791d553 | 2018-02-01 14:04:18 -0800 | [diff] [blame] | 108 | void ApInterfaceImpl::OnChannelSwitchEvent(uint32_t frequency, |
| 109 | ChannelBandwidth bandwidth) { |
| 110 | LOG(INFO) << "New channel on frequency: " << frequency |
| 111 | << " with bandwidth: " << LoggingUtils::GetBandwidthString(bandwidth); |
Mehdi Alizadeh | 4332ce6 | 2018-03-14 17:12:34 -0700 | [diff] [blame] | 112 | binder_->NotifySoftApChannelSwitched(frequency, bandwidth); |
Ningyuan Wang | d5cfc11 | 2018-01-29 15:50:49 -0800 | [diff] [blame] | 113 | } |
| 114 | |
Christopher Wiley | f229bbe | 2016-06-22 17:20:04 -0700 | [diff] [blame] | 115 | } // namespace wificond |
| 116 | } // namespace android |