blob: 7b5b08cad3658b959ab6b0fe3ab18f5dd0a4cb13 [file] [log] [blame]
Paul Stewart75897df2011-04-27 09:05:53 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07006#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07007
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <string>
9
Chris Masoneee929b72011-05-10 10:02:18 -070010#include <base/logging.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070011#include <base/memory/ref_counted.h>
Chris Masoneee929b72011-05-10 10:02:18 -070012
Chris Masoned0ceb8c2011-06-02 10:05:39 -070013#include "shill/adaptor_interfaces.h"
Paul Stewart75897df2011-04-27 09:05:53 -070014#include "shill/control_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070015#include "shill/dbus_adaptor.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070016#include "shill/device.h"
17#include "shill/device_info.h"
Paul Stewart75897df2011-04-27 09:05:53 -070018#include "shill/manager.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070019#include "shill/shill_event.h"
20#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070021
22using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070023using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070024
25namespace shill {
26Manager::Manager(ControlInterface *control_interface,
Paul Stewart0af98bf2011-05-10 17:38:08 -070027 EventDispatcher *dispatcher)
Chris Masone413a3192011-05-09 17:10:05 -070028 : adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070029 device_info_(control_interface, dispatcher, this),
Paul Stewart75897df2011-04-27 09:05:53 -070030 running_(false) {
Chris Masoneee929b72011-05-10 10:02:18 -070031 // Initialize Interface monitor, so we can detect new interfaces
Chris Masoneb07006b2011-05-14 16:10:04 -070032 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070033}
34
Paul Stewart0af98bf2011-05-10 17:38:08 -070035Manager::~Manager() {}
Paul Stewart75897df2011-04-27 09:05:53 -070036
37void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070038 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070039 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -070040 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -070041 device_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -070042}
43
44void Manager::Stop() {
45 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070046 adaptor_->UpdateRunning();
Paul Stewart75897df2011-04-27 09:05:53 -070047}
48
Chris Masonec1e50412011-06-07 13:04:53 -070049void Manager::RegisterDevice(DeviceRefPtr to_manage) {
50 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070051 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070052 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -070053 return;
54 }
Chris Masonec1e50412011-06-07 13:04:53 -070055 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070056
57 // TODO(pstew): Should check configuration
58 if (running_)
59 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -070060}
61
Chris Masonec1e50412011-06-07 13:04:53 -070062void Manager::DeregisterDevice(DeviceConstRefPtr to_forget) {
63 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070064 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070065 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -070066 devices_.erase(it);
67 return;
68 }
69 }
70}
71
Chris Masonec1e50412011-06-07 13:04:53 -070072void Manager::RegisterService(ServiceRefPtr to_manage) {
73 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070074 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070075 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -070076 return;
77 }
Chris Masonec1e50412011-06-07 13:04:53 -070078 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -070079}
80
Chris Masonec1e50412011-06-07 13:04:53 -070081void Manager::DeregisterService(ServiceConstRefPtr to_forget) {
82 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070083 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070084 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -070085 services_.erase(it);
86 return;
87 }
88 }
89}
90
91void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -070092 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -070093 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -070094 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070095 for (it = devices_.begin(); it != devices_.end(); ++it) {
96 if ((*it)->TechnologyIs(tech))
97 found->push_back(*it);
98 }
99}
100
Chris Masonee0dea762011-06-09 09:06:03 -0700101ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700102 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700103 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonee0dea762011-06-09 09:06:03 -0700104 if (name == (*it)->UniqueName()) {
105 return *it;
106 }
Chris Masone9be4a9d2011-05-16 15:44:09 -0700107 }
Chris Masonee0dea762011-06-09 09:06:03 -0700108 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700109}
110
Paul Stewart75897df2011-04-27 09:05:53 -0700111} // namespace shill