OSDN Git Service

Merge "Always set interface down in object destruction"
[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 android::wifi_system::InterfaceTool;
26 using std::string;
27 using std::unique_ptr;
28 using std::vector;
29
30 using EncryptionType = android::wifi_system::HostapdManager::EncryptionType;
31
32 namespace android {
33 namespace wificond {
34
35 ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
36                                  uint32_t interface_index,
37                                  InterfaceTool* if_tool,
38                                  HostapdManager* hostapd_manager)
39     : interface_name_(interface_name),
40       interface_index_(interface_index),
41       if_tool_(if_tool),
42       hostapd_manager_(hostapd_manager),
43       binder_(new ApInterfaceBinder(this)) {
44   // This log keeps compiler happy.
45   LOG(DEBUG) << "Created ap interface " << interface_name_
46              << " with index " << interface_index_;
47 }
48
49 ApInterfaceImpl::~ApInterfaceImpl() {
50   binder_->NotifyImplDead();
51   if_tool_->SetUpState(interface_name_.c_str(), false);
52 }
53
54 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
55   return binder_;
56 }
57
58 bool ApInterfaceImpl::StartHostapd() {
59   return hostapd_manager_->StartHostapd();
60 }
61
62 bool ApInterfaceImpl::StopHostapd() {
63   // Drop SIGKILL on hostapd.
64   bool success = hostapd_manager_->StopHostapd();
65
66   // Take down the interface.  This has the pleasant side effect of
67   // letting the driver know that we don't want any lingering AP logic
68   // running in the driver.
69   success = if_tool_->SetUpState(interface_name_.c_str(), false) && success;
70
71   return success;
72 }
73
74 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
75                                          bool is_hidden,
76                                          int32_t channel,
77                                          EncryptionType encryption_type,
78                                          const vector<uint8_t>& passphrase) {
79   string config = hostapd_manager_->CreateHostapdConfig(
80       interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
81
82   if (config.empty()) {
83     return false;
84   }
85
86   return hostapd_manager_->WriteHostapdConfig(config);
87 }
88
89 }  // namespace wificond
90 }  // namespace android