blob: 461ac1c63f5d66483a8c760d5f5b2a8e595dcdcc [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
Chris Masone8fe2c7e2011-06-09 15:51:19 -07005#include "shill/manager.h"
6
Paul Stewart75897df2011-04-27 09:05:53 -07007#include <time.h>
Paul Stewart75897df2011-04-27 09:05:53 -07008#include <stdio.h>
Chris Masoneee929b72011-05-10 10:02:18 -07009
Paul Stewart75897df2011-04-27 09:05:53 -070010#include <string>
11
Chris Masoneee929b72011-05-10 10:02:18 -070012#include <base/logging.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070013#include <base/memory/ref_counted.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070014#include <base/stl_util-inl.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070015#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070016
Chris Masoned0ceb8c2011-06-02 10:05:39 -070017#include "shill/adaptor_interfaces.h"
Paul Stewart75897df2011-04-27 09:05:53 -070018#include "shill/control_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070019#include "shill/dbus_adaptor.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070020#include "shill/device.h"
21#include "shill/device_info.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070022#include "shill/error.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070023#include "shill/property_accessor.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070024#include "shill/shill_event.h"
25#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070026
27using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070028using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070029
30namespace shill {
31Manager::Manager(ControlInterface *control_interface,
Paul Stewart0af98bf2011-05-10 17:38:08 -070032 EventDispatcher *dispatcher)
Chris Masone413a3192011-05-09 17:10:05 -070033 : adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070034 device_info_(control_interface, dispatcher, this),
Chris Masone3bd3c8c2011-06-13 08:20:26 -070035 running_(false),
36 offline_mode_(false),
37 state_(flimflam::kStateOffline) {
Chris Masoneee929b72011-05-10 10:02:18 -070038 // Initialize Interface monitor, so we can detect new interfaces
Chris Masoneb925cc82011-06-22 15:39:57 -070039 RegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
40 &Manager::AvailableTechnologies,
41 NULL);
42 RegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
43 &Manager::ConnectedTechnologies,
44 NULL);
45 RegisterDerivedString(flimflam::kDefaultTechnologyProperty,
46 &Manager::DefaultTechnology,
47 NULL);
48 RegisterString(flimflam::kCheckPortalListProperty, &check_portal_list_);
49 RegisterString(flimflam::kCountryProperty, &country_);
50 RegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
51 &Manager::EnabledTechnologies,
52 NULL);
53 RegisterBool(flimflam::kOfflineModeProperty, &offline_mode_);
54 RegisterString(flimflam::kPortalURLProperty, &portal_url_);
55 RegisterDerivedString(flimflam::kStateProperty,
56 &Manager::CalculateState,
57 NULL);
58
59 // TODO(cmasone): Add support for R/O properties that return DBus object paths
60 // known_properties_.push_back(flimflam::kActiveProfileProperty);
61 // known_properties_.push_back(flimflam::kProfilesProperty);
62 // known_properties_.push_back(flimflam::kDevicesProperty);
63 // known_properties_.push_back(flimflam::kServicesProperty);
64 // known_properties_.push_back(flimflam::kServiceWatchListProperty);
65
Chris Masoneb07006b2011-05-14 16:10:04 -070066 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070067}
68
Paul Stewart0af98bf2011-05-10 17:38:08 -070069Manager::~Manager() {}
Paul Stewart75897df2011-04-27 09:05:53 -070070
71void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070072 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070073 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -070074 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -070075 device_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -070076}
77
78void Manager::Stop() {
79 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070080 adaptor_->UpdateRunning();
Paul Stewart75897df2011-04-27 09:05:53 -070081}
82
Chris Masonec1e50412011-06-07 13:04:53 -070083void Manager::RegisterDevice(DeviceRefPtr to_manage) {
84 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070085 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070086 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -070087 return;
88 }
Chris Masonec1e50412011-06-07 13:04:53 -070089 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070090
91 // TODO(pstew): Should check configuration
92 if (running_)
93 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -070094}
95
Chris Masonec1e50412011-06-07 13:04:53 -070096void Manager::DeregisterDevice(DeviceConstRefPtr to_forget) {
97 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070098 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070099 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700100 devices_.erase(it);
101 return;
102 }
103 }
104}
105
Chris Masonec1e50412011-06-07 13:04:53 -0700106void Manager::RegisterService(ServiceRefPtr to_manage) {
107 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700108 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700109 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700110 return;
111 }
Chris Masonec1e50412011-06-07 13:04:53 -0700112 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700113}
114
Chris Masonec1e50412011-06-07 13:04:53 -0700115void Manager::DeregisterService(ServiceConstRefPtr to_forget) {
116 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700117 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700118 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700119 services_.erase(it);
120 return;
121 }
122 }
123}
124
125void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700126 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700127 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700128 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700129 for (it = devices_.begin(); it != devices_.end(); ++it) {
130 if ((*it)->TechnologyIs(tech))
131 found->push_back(*it);
132 }
133}
134
Chris Masonee0dea762011-06-09 09:06:03 -0700135ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700136 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700137 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonee0dea762011-06-09 09:06:03 -0700138 if (name == (*it)->UniqueName()) {
139 return *it;
140 }
Chris Masone9be4a9d2011-05-16 15:44:09 -0700141 }
Chris Masonee0dea762011-06-09 09:06:03 -0700142 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700143}
144
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700145bool Manager::SetBoolProperty(const string& name, bool value, Error *error) {
146 VLOG(2) << "Setting " << name << " as a bool.";
Chris Masoneb925cc82011-06-22 15:39:57 -0700147 bool set = (ContainsKey(bool_properties_, name) &&
148 bool_properties_[name]->Set(value));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700149 if (!set && error)
150 error->Populate(Error::kInvalidArguments, name + " is not a R/W bool.");
151 return set;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700152}
153
154bool Manager::SetStringProperty(const string& name,
155 const string& value,
156 Error *error) {
157 VLOG(2) << "Setting " << name << " as a string.";
Chris Masoneb925cc82011-06-22 15:39:57 -0700158 bool set = (ContainsKey(string_properties_, name) &&
159 string_properties_[name]->Set(value));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700160 if (!set && error)
161 error->Populate(Error::kInvalidArguments, name + " is not a R/W string.");
162 return set;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700163}
164
Chris Masoneb925cc82011-06-22 15:39:57 -0700165void Manager::RegisterDerivedString(const string &name,
166 string(Manager::*get)(void),
167 bool(Manager::*set)(const string&)) {
168 string_properties_[name] =
169 StringAccessor(new CustomAccessor<Manager, string>(this, get, set));
170}
171
172void Manager::RegisterDerivedStrings(
173 const string &name,
174 std::vector<string>(Manager::*get)(void),
175 bool(Manager::*set)(const vector<string>&)) {
176 strings_properties_[name] =
177 StringsAccessor(new CustomAccessor<Manager, vector<string> >(this,
178 get,
179 set));
180}
181
182string Manager::CalculateState() {
183 return flimflam::kStateOffline;
184}
185
186vector<string> Manager::AvailableTechnologies() {
187 return vector<string>();
188}
189
190vector<string> Manager::ConnectedTechnologies() {
191 return vector<string>();
192}
193
194string Manager::DefaultTechnology() {
195 return "";
196}
197
198vector<string> Manager::EnabledTechnologies() {
199 return vector<string>();
200}
201
Paul Stewart75897df2011-04-27 09:05:53 -0700202} // namespace shill