OSDN Git Service

Cleanup AP logic after Hostapd is down
[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/net/netlink_utils.h"
22
23 #include "wificond/ap_interface_binder.h"
24
25 using android::net::wifi::IApInterface;
26 using android::wifi_system::HostapdManager;
27 using android::wifi_system::InterfaceTool;
28 using std::string;
29 using std::unique_ptr;
30 using std::vector;
31
32 using EncryptionType = android::wifi_system::HostapdManager::EncryptionType;
33
34 namespace android {
35 namespace wificond {
36
37 ApInterfaceImpl::ApInterfaceImpl(const string& interface_name,
38                                  uint32_t interface_index,
39                                  NetlinkUtils* netlink_utils,
40                                  InterfaceTool* if_tool,
41                                  HostapdManager* hostapd_manager)
42     : interface_name_(interface_name),
43       interface_index_(interface_index),
44       netlink_utils_(netlink_utils),
45       if_tool_(if_tool),
46       hostapd_manager_(hostapd_manager),
47       binder_(new ApInterfaceBinder(this)) {
48   // This log keeps compiler happy.
49   LOG(DEBUG) << "Created ap interface " << interface_name_
50              << " with index " << interface_index_;
51 }
52
53 ApInterfaceImpl::~ApInterfaceImpl() {
54   binder_->NotifyImplDead();
55   if_tool_->SetUpState(interface_name_.c_str(), false);
56 }
57
58 sp<IApInterface> ApInterfaceImpl::GetBinder() const {
59   return binder_;
60 }
61
62 bool ApInterfaceImpl::StartHostapd() {
63   return hostapd_manager_->StartHostapd();
64 }
65
66 bool ApInterfaceImpl::StopHostapd() {
67   // Drop SIGKILL on hostapd.
68   if (!hostapd_manager_->StopHostapd()) {
69     // Logging was done internally.
70     return false;
71   }
72
73   // Take down the interface.
74   if (!if_tool_->SetUpState(interface_name_.c_str(), false)) {
75     // Logging was done internally.
76     return false;
77   }
78
79   // Since wificond SIGKILLs hostapd, hostapd has no chance to handle
80   // the cleanup.
81   // Besides taking down the interface, we also need to set the interface mode
82   // back to station mode for the cleanup.
83   if (!netlink_utils_->SetInterfaceMode(interface_index_,
84                                         NetlinkUtils::STATION_MODE)) {
85     LOG(ERROR) << "Failed to set interface back to station mode";
86     return false;
87   }
88
89   return true;
90 }
91
92 bool ApInterfaceImpl::WriteHostapdConfig(const vector<uint8_t>& ssid,
93                                          bool is_hidden,
94                                          int32_t channel,
95                                          EncryptionType encryption_type,
96                                          const vector<uint8_t>& passphrase) {
97   string config = hostapd_manager_->CreateHostapdConfig(
98       interface_name_, ssid, is_hidden, channel, encryption_type, passphrase);
99
100   if (config.empty()) {
101     return false;
102   }
103
104   return hostapd_manager_->WriteHostapdConfig(config);
105 }
106
107 }  // namespace wificond
108 }  // namespace android