Home | History | Annotate | Download | only in apmanager
      1 //
      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 //
     16 
     17 #include "apmanager/manager.h"
     18 
     19 #include <base/bind.h>
     20 
     21 #include "apmanager/control_interface.h"
     22 #include "apmanager/manager.h"
     23 
     24 using std::string;
     25 
     26 namespace apmanager {
     27 
     28 Manager::Manager(ControlInterface* control_interface)
     29     : control_interface_(control_interface),
     30       service_identifier_(0),
     31       device_info_(this),
     32       adaptor_(control_interface->CreateManagerAdaptor(this)) {}
     33 
     34 Manager::~Manager() {}
     35 
     36 void Manager::RegisterAsync(
     37     const base::Callback<void(bool)>& completion_callback) {
     38   adaptor_->RegisterAsync(completion_callback);
     39 }
     40 
     41 void Manager::Start() {
     42   shill_manager_.Init(control_interface_);
     43   firewall_manager_.Init(control_interface_);
     44   device_info_.Start();
     45 }
     46 
     47 void Manager::Stop() {
     48   device_info_.Stop();
     49 }
     50 
     51 scoped_refptr<Service> Manager::CreateService() {
     52   LOG(INFO) << "Manager::CreateService";
     53   scoped_refptr<Service> service = new Service(this, service_identifier_++);
     54   services_.push_back(service);
     55   return service;
     56 }
     57 
     58 bool Manager::RemoveService(const scoped_refptr<Service>& service,
     59                             Error* error) {
     60   for (auto it = services_.begin(); it != services_.end(); ++it) {
     61     if (*it == service) {
     62       services_.erase(it);
     63       return true;
     64     }
     65   }
     66 
     67   Error::PopulateAndLog(error,
     68                         Error::kInvalidArguments,
     69                         "Service does not exist",
     70                         FROM_HERE);
     71   return false;
     72 }
     73 
     74 scoped_refptr<Device> Manager::GetAvailableDevice() {
     75   for (const auto& device : devices_) {
     76     // Look for an unused device with AP interface mode support.
     77     if (!device->GetInUse() && !device->GetPreferredApInterface().empty()) {
     78       return device;
     79     }
     80   }
     81   return nullptr;
     82 }
     83 
     84 scoped_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 
     94 void Manager::RegisterDevice(const scoped_refptr<Device>& device) {
     95   LOG(INFO) << "Manager::RegisterDevice: registering device "
     96             << device->GetDeviceName();
     97   devices_.push_back(device);
     98   // TODO(zqiu): Property update for available devices.
     99 }
    100 
    101 void Manager::ClaimInterface(const string& interface_name) {
    102   shill_manager_.ClaimInterface(interface_name);
    103 }
    104 
    105 void Manager::ReleaseInterface(const string& interface_name) {
    106   shill_manager_.ReleaseInterface(interface_name);
    107 }
    108 
    109 #if defined(__BRILLO__)
    110 bool Manager::SetupApModeInterface(string* interface_name) {
    111   return shill_manager_.SetupApModeInterface(interface_name);
    112 }
    113 
    114 bool Manager::SetupStationModeInterface(string* interface_name) {
    115   return shill_manager_.SetupStationModeInterface(interface_name);
    116 }
    117 #endif  // __BRILLO__
    118 
    119 void Manager::RequestDHCPPortAccess(const string& interface) {
    120   firewall_manager_.RequestDHCPPortAccess(interface);
    121 }
    122 
    123 void Manager::ReleaseDHCPPortAccess(const string& interface) {
    124   firewall_manager_.ReleaseDHCPPortAccess(interface);
    125 }
    126 
    127 }  // namespace apmanager
    128