2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "wificond/ap_interface_impl.h"
19 #include <android-base/logging.h>
21 #include "wificond/net/netlink_utils.h"
23 #include "wificond/ap_interface_binder.h"
24 #include "wificond/logging_utils.h"
26 using android::net::wifi::IApInterface;
27 using android::wifi_system::HostapdManager;
28 using android::wifi_system::InterfaceTool;
31 using std::unique_ptr;
34 using EncryptionType = android::wifi_system::HostapdManager::EncryptionType;
36 using namespace std::placeholders;
41 ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
42 uint32_t interface_index,
43 NetlinkUtils* netlink_utils,
44 InterfaceTool* if_tool,
45 HostapdManager* hostapd_manager)
46 : interface_name_(interface_name),
47 interface_index_(interface_index),
48 netlink_utils_(netlink_utils),
50 hostapd_manager_(hostapd_manager),
51 binder_(new ApInterfaceBinder(this)),
52 number_of_associated_stations_(0) {
53 // This log keeps compiler happy.
54 LOG(DEBUG) << "Created ap interface " << interface_name_
55 << " with index " << interface_index_;
57 netlink_utils_->SubscribeStationEvent(
59 std::bind(&ApInterfaceImpl::OnStationEvent,
64 ApInterfaceImpl::~ApInterfaceImpl() {
65 binder_->NotifyImplDead();
66 if_tool_->SetUpState(interface_name_.c_str(), false);
67 netlink_utils_->UnsubscribeStationEvent(interface_index_);
70 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
74 void ApInterfaceImpl::Dump(std::stringstream* ss) const {
75 *ss << "------- Dump of AP interface with index: "
76 << interface_index_ << " and name: " << interface_name_
78 *ss << "Number of associated stations: "
79 << number_of_associated_stations_ << endl;
80 *ss << "------- Dump End -------" << endl;
83 bool ApInterfaceImpl::StartHostapd() {
84 return hostapd_manager_->StartHostapd();
87 bool ApInterfaceImpl::StopHostapd() {
88 // Drop SIGKILL on hostapd.
89 if (!hostapd_manager_->StopHostapd()) {
90 // Logging was done internally.
94 // Take down the interface.
95 if (!if_tool_->SetUpState(interface_name_.c_str(), false)) {
96 // Logging was done internally.
100 // Since wificond SIGKILLs hostapd, hostapd has no chance to handle
102 // Besides taking down the interface, we also need to set the interface mode
103 // back to station mode for the cleanup.
104 if (!netlink_utils_->SetInterfaceMode(interface_index_,
105 NetlinkUtils::STATION_MODE)) {
106 LOG(ERROR) << "Failed to set interface back to station mode";
113 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
116 EncryptionType encryption_type,
117 const vector<uint8_t>& passphrase) {
118 string config = hostapd_manager_->CreateHostapdConfig(
119 interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
121 if (config.empty()) {
125 return hostapd_manager_->WriteHostapdConfig(config);
128 void ApInterfaceImpl::OnStationEvent(StationEvent event,
129 const vector<uint8_t>& mac_address) {
130 if (event == NEW_STATION) {
131 LOG(INFO) << "New station "
132 << LoggingUtils::GetMacString(mac_address)
133 << " associated with hotspot";
134 number_of_associated_stations_++;
135 } else if (event == DEL_STATION) {
136 LOG(INFO) << "Station "
137 << LoggingUtils::GetMacString(mac_address)
138 << " disassociated from hotspot";
139 if (number_of_associated_stations_ <= 0) {
140 LOG(ERROR) << "Received DEL_STATION event when station counter is: "
141 << number_of_associated_stations_;
143 number_of_associated_stations_--;
148 int ApInterfaceImpl::GetNumberOfAssociatedStations() const {
149 return number_of_associated_stations_;
152 } // namespace wificond
153 } // namespace android