blob: efc7c96207bad13cc5426ab71edd7de59f604b11 [file] [log] [blame]
Christopher Wileyf229bbe2016-06-22 17:20:04 -07001/*
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 Wang808c6702016-07-15 16:47:54 -070017#include "wificond/ap_interface_impl.h"
Christopher Wileyf229bbe2016-06-22 17:20:04 -070018
Christopher Wileyc06a8b82016-07-28 15:07:47 -070019#include <android-base/logging.h>
20
Ningyuan Wang9136ae32017-02-28 15:49:08 -080021#include "wificond/net/netlink_utils.h"
22
Ningyuan Wang808c6702016-07-15 16:47:54 -070023#include "wificond/ap_interface_binder.h"
Ningyuan Wangcf51f862017-03-08 08:44:39 -080024#include "wificond/logging_utils.h"
Christopher Wileyf229bbe2016-06-22 17:20:04 -070025
Etan Cohen067e1192020-02-15 17:36:36 -080026using android::net::wifi::nl80211::IApInterface;
Christopher Wileyd42ca5a2016-08-22 17:42:58 -070027using android::wifi_system::InterfaceTool;
Etan Cohen067e1192020-02-15 17:36:36 -080028using android::net::wifi::nl80211::NativeWifiClient;
David Su61ea3072018-10-02 15:14:29 -070029using std::array;
Ningyuan Wang8ec76962017-04-07 16:03:33 -070030using std::endl;
Christopher Wiley1f8dd452016-07-18 15:30:58 -070031using std::string;
32using std::unique_ptr;
James Mattis39012042019-10-04 18:37:43 -070033using std::vector;
Christopher Wiley1f8dd452016-07-18 15:30:58 -070034
Ningyuan Wangcf51f862017-03-08 08:44:39 -080035using namespace std::placeholders;
36
Christopher Wileyf229bbe2016-06-22 17:20:04 -070037namespace android {
38namespace wificond {
39
Christopher Wiley1f8dd452016-07-18 15:30:58 -070040ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
Ningyuan Wang9bc59a02016-08-11 09:59:22 -070041 uint32_t interface_index,
Ningyuan Wang9136ae32017-02-28 15:49:08 -080042 NetlinkUtils* netlink_utils,
Roshan Pius1731b152018-02-11 18:29:22 -080043 InterfaceTool* if_tool)
Christopher Wiley1f8dd452016-07-18 15:30:58 -070044 : interface_name_(interface_name),
Ningyuan Wang9bc59a02016-08-11 09:59:22 -070045 interface_index_(interface_index),
Ningyuan Wang9136ae32017-02-28 15:49:08 -080046 netlink_utils_(netlink_utils),
Christopher Wileyd42ca5a2016-08-22 17:42:58 -070047 if_tool_(if_tool),
James Mattis39012042019-10-04 18:37:43 -070048 binder_(new ApInterfaceBinder(this)) {
Ningyuan Wang9bc59a02016-08-11 09:59:22 -070049 // This log keeps compiler happy.
50 LOG(DEBUG) << "Created ap interface " << interface_name_
51 << " with index " << interface_index_;
Ningyuan Wangcf51f862017-03-08 08:44:39 -080052
53 netlink_utils_->SubscribeStationEvent(
54 interface_index_,
55 std::bind(&ApInterfaceImpl::OnStationEvent,
56 this,
57 _1, _2));
Ningyuan Wangd5cfc112018-01-29 15:50:49 -080058 netlink_utils_->SubscribeChannelSwitchEvent(
59 interface_index_,
Ningyuan Wang791d5532018-02-01 14:04:18 -080060 std::bind(&ApInterfaceImpl::OnChannelSwitchEvent, this, _1, _2));
Ningyuan Wangd5cfc112018-01-29 15:50:49 -080061
Christopher Wileyf229bbe2016-06-22 17:20:04 -070062}
63
64ApInterfaceImpl::~ApInterfaceImpl() {
65 binder_->NotifyImplDead();
Christopher Wiley4fc37922016-09-22 08:57:40 -070066 if_tool_->SetUpState(interface_name_.c_str(), false);
Ningyuan Wangcf51f862017-03-08 08:44:39 -080067 netlink_utils_->UnsubscribeStationEvent(interface_index_);
Ningyuan Wangd5cfc112018-01-29 15:50:49 -080068 netlink_utils_->UnsubscribeChannelSwitchEvent(interface_index_);
Christopher Wileyf229bbe2016-06-22 17:20:04 -070069}
70
71sp<IApInterface> ApInterfaceImpl::GetBinder() const {
72 return binder_;
73}
74
Ningyuan Wang8ec76962017-04-07 16:03:33 -070075void ApInterfaceImpl::Dump(std::stringstream* ss) const {
76 *ss << "------- Dump of AP interface with index: "
77 << interface_index_ << " and name: " << interface_name_
78 << "-------" << endl;
Ningyuan Wang8ec76962017-04-07 16:03:33 -070079 *ss << "------- Dump End -------" << endl;
80}
81
David Su61ea3072018-10-02 15:14:29 -070082void ApInterfaceImpl::OnStationEvent(
83 StationEvent event,
84 const array<uint8_t, ETH_ALEN>& mac_address) {
lesl414ba9c2019-12-04 15:33:20 +080085 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 Wangcf51f862017-03-08 08:44:39 -080090 if (event == NEW_STATION) {
91 LOG(INFO) << "New station "
92 << LoggingUtils::GetMacString(mac_address)
James Mattis39012042019-10-04 18:37:43 -070093 << " connected to hotspot"
94 << " using interface "
95 << interface_name_;
lesl414ba9c2019-12-04 15:33:20 +080096 LOG(INFO) << "Sending notifications for station add event";
97 binder_->NotifyConnectedClientsChanged(station, true);
Ningyuan Wangcf51f862017-03-08 08:44:39 -080098 } else if (event == DEL_STATION) {
99 LOG(INFO) << "Station "
100 << LoggingUtils::GetMacString(mac_address)
101 << " disassociated from hotspot";
lesl414ba9c2019-12-04 15:33:20 +0800102 LOG(DEBUG) << "Sending notifications for station leave event";
103 binder_->NotifyConnectedClientsChanged(station, false);
Etan Cohenfc5cbea2019-11-26 16:53:14 -0800104 }
Ningyuan Wangcf51f862017-03-08 08:44:39 -0800105}
106
Ningyuan Wangd5cfc112018-01-29 15:50:49 -0800107
Ningyuan Wang791d5532018-02-01 14:04:18 -0800108void ApInterfaceImpl::OnChannelSwitchEvent(uint32_t frequency,
109 ChannelBandwidth bandwidth) {
110 LOG(INFO) << "New channel on frequency: " << frequency
111 << " with bandwidth: " << LoggingUtils::GetBandwidthString(bandwidth);
Mehdi Alizadeh4332ce62018-03-14 17:12:34 -0700112 binder_->NotifySoftApChannelSwitched(frequency, bandwidth);
Ningyuan Wangd5cfc112018-01-29 15:50:49 -0800113}
114
Christopher Wileyf229bbe2016-06-22 17:20:04 -0700115} // namespace wificond
116} // namespace android