blob: 88c54ef4ef0f7b6a1186e982e6e73a12fc51c450 [file] [log] [blame]
Peter Qiu326b6cf2015-09-02 11:11:42 -07001//
2// Copyright (C) 2014 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//
Peter Qiu5dd242d2014-10-14 12:23:21 -070016
17#include "apmanager/manager.h"
18
Peter Qiu376e4042014-11-13 09:40:28 -080019#include <base/bind.h>
Peter Qiu7a420d32015-09-22 11:25:15 -070020
Peter Qiu7758d8d2015-11-23 14:27:33 -080021#include "apmanager/control_interface.h"
22#include "apmanager/manager.h"
Peter Qiu5dd242d2014-10-14 12:23:21 -070023
Peter Qiufb39ba42014-11-21 09:09:59 -080024using std::string;
Peter Qiu376e4042014-11-13 09:40:28 -080025
Peter Qiu5dd242d2014-10-14 12:23:21 -070026namespace apmanager {
27
Peter Qiuf9335402015-11-16 12:09:16 -080028Manager::Manager(ControlInterface* control_interface)
Peter Qiu7758d8d2015-11-23 14:27:33 -080029 : control_interface_(control_interface),
Peter Qiufb39ba42014-11-21 09:09:59 -080030 service_identifier_(0),
Peter Qiu7758d8d2015-11-23 14:27:33 -080031 device_info_(this),
32 adaptor_(control_interface->CreateManagerAdaptor(this)) {}
Peter Qiu5dd242d2014-10-14 12:23:21 -070033
Peter Qiu7758d8d2015-11-23 14:27:33 -080034Manager::~Manager() {}
Peter Qiu5dd242d2014-10-14 12:23:21 -070035
Peter Qiu0d70fa72015-11-12 10:31:40 -080036void Manager::RegisterAsync(
Peter Qiu0d70fa72015-11-12 10:31:40 -080037 const base::Callback<void(bool)>& completion_callback) {
Peter Qiu7758d8d2015-11-23 14:27:33 -080038 adaptor_->RegisterAsync(completion_callback);
Peter Qiu5dd242d2014-10-14 12:23:21 -070039}
40
Peter Qiufb39ba42014-11-21 09:09:59 -080041void Manager::Start() {
Peter Qiu7758d8d2015-11-23 14:27:33 -080042 shill_manager_.Init(control_interface_);
43 firewall_manager_.Init(control_interface_);
Peter Qiufb39ba42014-11-21 09:09:59 -080044 device_info_.Start();
45}
46
47void Manager::Stop() {
48 device_info_.Stop();
49}
50
Peter Qiu7758d8d2015-11-23 14:27:33 -080051scoped_refptr<Service> Manager::CreateService() {
Peter Qiufb39ba42014-11-21 09:09:59 -080052 LOG(INFO) << "Manager::CreateService";
Peter Qiu7758d8d2015-11-23 14:27:33 -080053 scoped_refptr<Service> service = new Service(this, service_identifier_++);
54 services_.push_back(service);
55 return service;
Peter Qiu5dd242d2014-10-14 12:23:21 -070056}
57
Peter Qiu7758d8d2015-11-23 14:27:33 -080058bool Manager::RemoveService(const scoped_refptr<Service>& service,
59 Error* error) {
Peter Qiu376e4042014-11-13 09:40:28 -080060 for (auto it = services_.begin(); it != services_.end(); ++it) {
Peter Qiu7758d8d2015-11-23 14:27:33 -080061 if (*it == service) {
Peter Qiu376e4042014-11-13 09:40:28 -080062 services_.erase(it);
63 return true;
64 }
65 }
66
Peter Qiu7758d8d2015-11-23 14:27:33 -080067 Error::PopulateAndLog(error,
68 Error::kInvalidArguments,
69 "Service does not exist",
70 FROM_HERE);
Peter Qiuf0731732014-11-11 09:46:41 -080071 return false;
Peter Qiu5dd242d2014-10-14 12:23:21 -070072}
73
Peter Qiufb39ba42014-11-21 09:09:59 -080074scoped_refptr<Device> Manager::GetAvailableDevice() {
75 for (const auto& device : devices_) {
Peter Qiu8e785b92014-11-24 10:01:08 -080076 // Look for an unused device with AP interface mode support.
Garret Kelly0c0e9e72015-09-01 17:28:01 -040077 if (!device->GetInUse() && !device->GetPreferredApInterface().empty()) {
Peter Qiufb39ba42014-11-21 09:09:59 -080078 return device;
79 }
80 }
81 return nullptr;
82}
83
84scoped_refptr<Device> Manager::GetDeviceFromInterfaceName(
85 const string& interface_name) {
86 for (const auto& device : devices_) {
87 if (device->InterfaceExists(interface_name)) {
88 return device;
89 }
90 }
91 return nullptr;
92}
93
Peter Qiu7758d8d2015-11-23 14:27:33 -080094void Manager::RegisterDevice(const scoped_refptr<Device>& device) {
Peter Qiufb39ba42014-11-21 09:09:59 -080095 LOG(INFO) << "Manager::RegisterDevice: registering device "
96 << device->GetDeviceName();
Peter Qiuf9335402015-11-16 12:09:16 -080097 devices_.push_back(device);
98 // TODO(zqiu): Property update for available devices.
Peter Qiufb39ba42014-11-21 09:09:59 -080099}
100
Peter Qiu7e0ffcf2014-12-02 12:53:27 -0800101void Manager::ClaimInterface(const string& interface_name) {
Peter Qiu2a6fb332015-09-17 22:19:17 -0700102 shill_manager_.ClaimInterface(interface_name);
Peter Qiu7e0ffcf2014-12-02 12:53:27 -0800103}
104
105void Manager::ReleaseInterface(const string& interface_name) {
Peter Qiu2a6fb332015-09-17 22:19:17 -0700106 shill_manager_.ReleaseInterface(interface_name);
Peter Qiu7e0ffcf2014-12-02 12:53:27 -0800107}
108
Peter Qiucbbefa22015-10-27 12:05:34 -0700109#if defined(__BRILLO__)
110bool Manager::SetupApModeInterface(string* interface_name) {
111 return shill_manager_.SetupApModeInterface(interface_name);
112}
113
114bool Manager::SetupStationModeInterface(string* interface_name) {
115 return shill_manager_.SetupStationModeInterface(interface_name);
116}
117#endif // __BRILLO__
118
Peter Qiu943cf3a2015-02-24 10:59:17 -0800119void Manager::RequestDHCPPortAccess(const string& interface) {
120 firewall_manager_.RequestDHCPPortAccess(interface);
121}
122
123void Manager::ReleaseDHCPPortAccess(const string& interface) {
124 firewall_manager_.ReleaseDHCPPortAccess(interface);
125}
126
Peter Qiu5dd242d2014-10-14 12:23:21 -0700127} // namespace apmanager