OSDN Git Service

Merge "Add AIDL method to retrieve network interface name" am: ced07e8491
[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 }
52
53 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
54   return binder_;
55 }
56
57 bool ApInterfaceImpl::StartHostapd() {
58   return hostapd_manager_->StartHostapd();
59 }
60
61 bool ApInterfaceImpl::StopHostapd() {
62   // Drop SIGKILL on hostapd.
63   bool success = hostapd_manager_->StopHostapd();
64
65   // Take down the interface.  This has the pleasant side effect of
66   // letting the driver know that we don't want any lingering AP logic
67   // running in the driver.
68   success = if_tool_->SetUpState(interface_name_.c_str(), false) && success;
69
70   return success;
71 }
72
73 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
74                                          bool is_hidden,
75                                          int32_t channel,
76                                          EncryptionType encryption_type,
77                                          const vector<uint8_t>& passphrase) {
78   string config = hostapd_manager_->CreateHostapdConfig(
79       interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
80
81   if (config.empty()) {
82     return false;
83   }
84
85   return hostapd_manager_->WriteHostapdConfig(config);
86 }
87
88 }  // namespace wificond
89 }  // namespace android