blob: 82a799b0cc4048b08cda6cdcf6b05ca2370c812c [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>
Chris Masone52cd19b2011-06-29 17:23:04 -070011#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070012
Chris Masoneee929b72011-05-10 10:02:18 -070013#include <base/logging.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070014#include <base/memory/ref_counted.h>
Chris Masoneb925cc82011-06-22 15:39:57 -070015#include <base/stl_util-inl.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070017
Chris Masoned0ceb8c2011-06-02 10:05:39 -070018#include "shill/adaptor_interfaces.h"
Paul Stewart75897df2011-04-27 09:05:53 -070019#include "shill/control_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070020#include "shill/dbus_adaptor.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070021#include "shill/device.h"
22#include "shill/device_info.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023#include "shill/error.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070024#include "shill/property_accessor.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070025#include "shill/shill_event.h"
26#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070027
28using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070029using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070030
31namespace shill {
32Manager::Manager(ControlInterface *control_interface,
Paul Stewart0af98bf2011-05-10 17:38:08 -070033 EventDispatcher *dispatcher)
Chris Masone413a3192011-05-09 17:10:05 -070034 : adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070035 device_info_(control_interface, dispatcher, this),
Chris Masone3bd3c8c2011-06-13 08:20:26 -070036 running_(false),
37 offline_mode_(false),
38 state_(flimflam::kStateOffline) {
Chris Masoneee929b72011-05-10 10:02:18 -070039 // Initialize Interface monitor, so we can detect new interfaces
Chris Masoneb925cc82011-06-22 15:39:57 -070040 RegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
41 &Manager::AvailableTechnologies,
42 NULL);
43 RegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
44 &Manager::ConnectedTechnologies,
45 NULL);
46 RegisterDerivedString(flimflam::kDefaultTechnologyProperty,
47 &Manager::DefaultTechnology,
48 NULL);
49 RegisterString(flimflam::kCheckPortalListProperty, &check_portal_list_);
50 RegisterString(flimflam::kCountryProperty, &country_);
51 RegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
52 &Manager::EnabledTechnologies,
53 NULL);
54 RegisterBool(flimflam::kOfflineModeProperty, &offline_mode_);
55 RegisterString(flimflam::kPortalURLProperty, &portal_url_);
56 RegisterDerivedString(flimflam::kStateProperty,
57 &Manager::CalculateState,
58 NULL);
59
60 // TODO(cmasone): Add support for R/O properties that return DBus object paths
61 // known_properties_.push_back(flimflam::kActiveProfileProperty);
62 // known_properties_.push_back(flimflam::kProfilesProperty);
63 // known_properties_.push_back(flimflam::kDevicesProperty);
64 // known_properties_.push_back(flimflam::kServicesProperty);
65 // known_properties_.push_back(flimflam::kServiceWatchListProperty);
66
Chris Masoneb07006b2011-05-14 16:10:04 -070067 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070068}
69
Paul Stewart0af98bf2011-05-10 17:38:08 -070070Manager::~Manager() {}
Paul Stewart75897df2011-04-27 09:05:53 -070071
72void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070073 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070074 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -070075 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -070076 device_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -070077}
78
79void Manager::Stop() {
80 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070081 adaptor_->UpdateRunning();
Paul Stewart75897df2011-04-27 09:05:53 -070082}
83
Chris Masone2b105542011-06-22 10:58:09 -070084void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -070085 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070086 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070087 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -070088 return;
89 }
Chris Masonec1e50412011-06-07 13:04:53 -070090 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070091
92 // TODO(pstew): Should check configuration
93 if (running_)
94 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -070095}
96
Chris Masone2b105542011-06-22 10:58:09 -070097void Manager::DeregisterDevice(const DeviceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -070098 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070099 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700100 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700101 devices_.erase(it);
102 return;
103 }
104 }
105}
106
Chris Masone2b105542011-06-22 10:58:09 -0700107void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700108 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700109 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700110 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700111 return;
112 }
Chris Masonec1e50412011-06-07 13:04:53 -0700113 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700114}
115
Chris Masone2b105542011-06-22 10:58:09 -0700116void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700117 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700118 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700119 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700120 services_.erase(it);
121 return;
122 }
123 }
124}
125
126void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700127 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700128 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700129 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700130 for (it = devices_.begin(); it != devices_.end(); ++it) {
131 if ((*it)->TechnologyIs(tech))
132 found->push_back(*it);
133 }
134}
135
Chris Masonee0dea762011-06-09 09:06:03 -0700136ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700137 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700138 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonee0dea762011-06-09 09:06:03 -0700139 if (name == (*it)->UniqueName()) {
140 return *it;
141 }
Chris Masone9be4a9d2011-05-16 15:44:09 -0700142 }
Chris Masonee0dea762011-06-09 09:06:03 -0700143 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700144}
145
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700146bool Manager::SetBoolProperty(const string& name, bool value, Error *error) {
147 VLOG(2) << "Setting " << name << " as a bool.";
Chris Masoneb925cc82011-06-22 15:39:57 -0700148 bool set = (ContainsKey(bool_properties_, name) &&
149 bool_properties_[name]->Set(value));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700150 if (!set && error)
151 error->Populate(Error::kInvalidArguments, name + " is not a R/W bool.");
152 return set;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700153}
154
155bool Manager::SetStringProperty(const string& name,
156 const string& value,
157 Error *error) {
158 VLOG(2) << "Setting " << name << " as a string.";
Chris Masoneb925cc82011-06-22 15:39:57 -0700159 bool set = (ContainsKey(string_properties_, name) &&
160 string_properties_[name]->Set(value));
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700161 if (!set && error)
162 error->Populate(Error::kInvalidArguments, name + " is not a R/W string.");
163 return set;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700164}
165
Chris Masoneb925cc82011-06-22 15:39:57 -0700166void Manager::RegisterDerivedString(const string &name,
167 string(Manager::*get)(void),
168 bool(Manager::*set)(const string&)) {
169 string_properties_[name] =
170 StringAccessor(new CustomAccessor<Manager, string>(this, get, set));
171}
172
Chris Masonea8a2c252011-06-27 22:16:30 -0700173void Manager::RegisterDerivedStrings(const string &name,
174 Strings(Manager::*get)(void),
175 bool(Manager::*set)(const Strings&)) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700176 strings_properties_[name] =
Chris Masonea8a2c252011-06-27 22:16:30 -0700177 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set));
Chris Masoneb925cc82011-06-22 15:39:57 -0700178}
179
180string Manager::CalculateState() {
181 return flimflam::kStateOffline;
182}
183
184vector<string> Manager::AvailableTechnologies() {
185 return vector<string>();
186}
187
188vector<string> Manager::ConnectedTechnologies() {
189 return vector<string>();
190}
191
192string Manager::DefaultTechnology() {
193 return "";
194}
195
196vector<string> Manager::EnabledTechnologies() {
197 return vector<string>();
198}
199
Paul Stewart75897df2011-04-27 09:05:53 -0700200} // namespace shill