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/ap_interface_binder.h"
23 using android::net::wifi::IApInterface;
24 using android::wifi_system::HostapdManager;
26 using std::unique_ptr;
29 using EncryptionType = android::wifi_system::HostapdManager::EncryptionType;
34 ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
35 unique_ptr<HostapdManager> hostapd_manager)
36 : interface_name_(interface_name),
37 hostapd_manager_(std::move(hostapd_manager)) {
38 binder_ = new ApInterfaceBinder(this);
41 ApInterfaceImpl::~ApInterfaceImpl() {
42 binder_->NotifyImplDead();
45 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
49 bool ApInterfaceImpl::StartHostapd() {
50 return hostapd_manager_->StartHostapd();
53 bool ApInterfaceImpl::StopHostapd() {
55 if (hostapd_manager_->GetHostapdPid(&hostapd_pid)) {
56 // We do this because it was found that hostapd would leave the driver in a
57 // bad state if init killed it harshly with a SIGKILL. b/30311493
58 if (kill(hostapd_pid, SIGTERM) == -1) {
59 LOG(ERROR) << "Error delivering signal to hostapd: " << strerror(errno);
61 // This should really be asynchronous: b/30465379
62 for (int tries = 0; tries < 10; tries++) {
63 // Try to give hostapd some time to go down.
64 if (!hostapd_manager_->IsHostapdRunning()) {
67 usleep(10 * 1000); // Don't busy wait.
71 // Always drop a SIGKILL on hostapd on the way out, just in case.
72 return hostapd_manager_->StopHostapd();
75 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
78 EncryptionType encryption_type,
79 const vector<uint8_t>& passphrase) {
80 string config = hostapd_manager_->CreateHostapdConfig(
81 interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
87 return hostapd_manager_->WriteHostapdConfig(config);
90 } // namespace wificond
91 } // namespace android