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 uint32_t interface_index,
36 unique_ptr<HostapdManager> hostapd_manager)
37 : interface_name_(interface_name),
38 interface_index_(interface_index),
39 hostapd_manager_(std::move(hostapd_manager)),
40 binder_(new ApInterfaceBinder(this)) {
41 // This log keeps compiler happy.
42 LOG(DEBUG) << "Created ap interface " << interface_name_
43 << " with index " << interface_index_;
46 ApInterfaceImpl::~ApInterfaceImpl() {
47 binder_->NotifyImplDead();
50 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
54 bool ApInterfaceImpl::StartHostapd() {
55 return hostapd_manager_->StartHostapd();
58 bool ApInterfaceImpl::StopHostapd() {
60 if (hostapd_manager_->GetHostapdPid(&hostapd_pid)) {
61 // We do this because it was found that hostapd would leave the driver in a
62 // bad state if init killed it harshly with a SIGKILL. b/30311493
63 if (kill(hostapd_pid, SIGTERM) == -1) {
64 LOG(ERROR) << "Error delivering signal to hostapd: " << strerror(errno);
66 // This should really be asynchronous: b/30465379
67 for (int tries = 0; tries < 10; tries++) {
68 // Try to give hostapd some time to go down.
69 if (!hostapd_manager_->IsHostapdRunning()) {
72 usleep(10 * 1000); // Don't busy wait.
76 // Always drop a SIGKILL on hostapd on the way out, just in case.
77 return hostapd_manager_->StopHostapd();
80 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
83 EncryptionType encryption_type,
84 const vector<uint8_t>& passphrase) {
85 string config = hostapd_manager_->CreateHostapdConfig(
86 interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
92 return hostapd_manager_->WriteHostapdConfig(config);
95 } // namespace wificond
96 } // namespace android