OSDN Git Service

Merge "Get interface index from kernel for wificond"
[android-x86/system-connectivity-wificond.git] / ap_interface_impl.cpp
1 /*
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
17 #include "wificond/ap_interface_impl.h"
18
19 #include <android-base/logging.h>
20
21 #include "wificond/ap_interface_binder.h"
22
23 using android::net::wifi::IApInterface;
24 using android::wifi_system::HostapdManager;
25 using std::string;
26 using std::unique_ptr;
27 using std::vector;
28
29 using EncryptionType = android::wifi_system::HostapdManager::EncryptionType;
30
31 namespace android {
32 namespace wificond {
33
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_;
44 }
45
46 ApInterfaceImpl::~ApInterfaceImpl() {
47   binder_->NotifyImplDead();
48 }
49
50 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
51   return binder_;
52 }
53
54 bool ApInterfaceImpl::StartHostapd() {
55   return hostapd_manager_->StartHostapd();
56 }
57
58 bool ApInterfaceImpl::StopHostapd() {
59   pid_t hostapd_pid;
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);
65     }
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()) {
70         break;
71       }
72       usleep(10 * 1000);  // Don't busy wait.
73     }
74   }
75
76   // Always drop a SIGKILL on hostapd on the way out, just in case.
77   return hostapd_manager_->StopHostapd();
78 }
79
80 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
81                                          bool is_hidden,
82                                          int32_t channel,
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);
87
88   if (config.empty()) {
89     return false;
90   }
91
92   return hostapd_manager_->WriteHostapdConfig(config);
93 }
94
95 }  // namespace wificond
96 }  // namespace android