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"
25 using android::net::wifi::IApInterface;
26 using android::wifi_system::HostapdManager;
27 using android::wifi_system::InterfaceTool;
29 using std::unique_ptr;
32 using EncryptionType = android::wifi_system::HostapdManager::EncryptionType;
37 ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
38 uint32_t interface_index,
39 NetlinkUtils* netlink_utils,
40 InterfaceTool* if_tool,
41 HostapdManager* hostapd_manager)
42 : interface_name_(interface_name),
43 interface_index_(interface_index),
44 netlink_utils_(netlink_utils),
46 hostapd_manager_(hostapd_manager),
47 binder_(new ApInterfaceBinder(this)) {
48 // This log keeps compiler happy.
49 LOG(DEBUG) << "Created ap interface " << interface_name_
50 << " with index " << interface_index_;
53 ApInterfaceImpl::~ApInterfaceImpl() {
54 binder_->NotifyImplDead();
55 if_tool_->SetUpState(interface_name_.c_str(), false);
58 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
62 bool ApInterfaceImpl::StartHostapd() {
63 return hostapd_manager_->StartHostapd();
66 bool ApInterfaceImpl::StopHostapd() {
67 // Drop SIGKILL on hostapd.
68 if (!hostapd_manager_->StopHostapd()) {
69 // Logging was done internally.
73 // Take down the interface.
74 if (!if_tool_->SetUpState(interface_name_.c_str(), false)) {
75 // Logging was done internally.
79 // Since wificond SIGKILLs hostapd, hostapd has no chance to handle
81 // Besides taking down the interface, we also need to set the interface mode
82 // back to station mode for the cleanup.
83 if (!netlink_utils_->SetInterfaceMode(interface_index_,
84 NetlinkUtils::STATION_MODE)) {
85 LOG(ERROR) << "Failed to set interface back to station mode";
92 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
95 EncryptionType encryption_type,
96 const vector<uint8_t>& passphrase) {
97 string config = hostapd_manager_->CreateHostapdConfig(
98 interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
100 if (config.empty()) {
104 return hostapd_manager_->WriteHostapdConfig(config);
107 } // namespace wificond
108 } // namespace android