blob: 91bede77a19cbb1f0a369a8bcb69cd48559d65b5 [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 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 Masone88cbd5f2011-07-03 14:30:04 -070035 running_(false) {
Chris Masone27c4aa52011-07-02 13:10:14 -070036 HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
37 &Manager::AvailableTechnologies,
38 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070039 store_.RegisterString(flimflam::kCheckPortalListProperty,
40 &props_.check_portal_list);
Chris Masone27c4aa52011-07-02 13:10:14 -070041 HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
42 &Manager::ConnectedTechnologies,
43 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070044 store_.RegisterString(flimflam::kCountryProperty, &props_.country);
Chris Masone27c4aa52011-07-02 13:10:14 -070045 HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty,
46 &Manager::DefaultTechnology,
47 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070048 HelpRegisterDerivedStrings(flimflam::kDevicesProperty,
49 &Manager::EnumerateDevices,
50 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070051 HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
52 &Manager::EnabledTechnologies,
53 NULL);
54 store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode);
55 store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url);
56 HelpRegisterDerivedString(flimflam::kStateProperty,
57 &Manager::CalculateState,
58 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070059 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
60 &Manager::EnumerateAvailableServices,
61 NULL);
62 HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty,
63 &Manager::EnumerateWatchedServices,
64 NULL);
Chris Masone3c3f6a12011-07-01 10:01:41 -070065
Chris Masone4d42df82011-07-02 17:09:39 -070066 // TODO(cmasone): Wire these up once we actually put in profile support.
Chris Masoneb925cc82011-06-22 15:39:57 -070067 // known_properties_.push_back(flimflam::kActiveProfileProperty);
68 // known_properties_.push_back(flimflam::kProfilesProperty);
Chris Masoneb925cc82011-06-22 15:39:57 -070069
Chris Masoneb07006b2011-05-14 16:10:04 -070070 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070071}
72
Paul Stewart0af98bf2011-05-10 17:38:08 -070073Manager::~Manager() {}
Paul Stewart75897df2011-04-27 09:05:53 -070074
75void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070076 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070077 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -070078 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -070079 device_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -070080}
81
82void Manager::Stop() {
83 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070084 adaptor_->UpdateRunning();
Paul Stewart75897df2011-04-27 09:05:53 -070085}
86
Chris Masone2b105542011-06-22 10:58:09 -070087void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -070088 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070089 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070090 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -070091 return;
92 }
Chris Masonec1e50412011-06-07 13:04:53 -070093 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070094
95 // TODO(pstew): Should check configuration
96 if (running_)
97 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -070098}
99
Chris Masone2b105542011-06-22 10:58:09 -0700100void Manager::DeregisterDevice(const DeviceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700101 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700102 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700103 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700104 devices_.erase(it);
105 return;
106 }
107 }
108}
109
Chris Masone2b105542011-06-22 10:58:09 -0700110void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700111 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700112 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700113 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700114 return;
115 }
Chris Masonec1e50412011-06-07 13:04:53 -0700116 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700117}
118
Chris Masone2b105542011-06-22 10:58:09 -0700119void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700120 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700121 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700122 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700123 services_.erase(it);
124 return;
125 }
126 }
127}
128
129void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700130 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700131 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700132 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700133 for (it = devices_.begin(); it != devices_.end(); ++it) {
134 if ((*it)->TechnologyIs(tech))
135 found->push_back(*it);
136 }
137}
138
Chris Masonee0dea762011-06-09 09:06:03 -0700139ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700140 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700141 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonee0dea762011-06-09 09:06:03 -0700142 if (name == (*it)->UniqueName()) {
143 return *it;
144 }
Chris Masone9be4a9d2011-05-16 15:44:09 -0700145 }
Chris Masonee0dea762011-06-09 09:06:03 -0700146 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700147}
148
Chris Masone27c4aa52011-07-02 13:10:14 -0700149void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700150 string(Manager::*get)(void),
151 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700152 store_.RegisterDerivedString(
153 name,
154 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700155}
156
Chris Masone27c4aa52011-07-02 13:10:14 -0700157void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700158 Strings(Manager::*get)(void),
159 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700160 store_.RegisterDerivedStrings(
161 name,
162 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700163}
164
165string Manager::CalculateState() {
166 return flimflam::kStateOffline;
167}
168
169vector<string> Manager::AvailableTechnologies() {
170 return vector<string>();
171}
172
173vector<string> Manager::ConnectedTechnologies() {
174 return vector<string>();
175}
176
177string Manager::DefaultTechnology() {
178 return "";
179}
180
181vector<string> Manager::EnabledTechnologies() {
182 return vector<string>();
183}
184
Chris Masone3c3f6a12011-07-01 10:01:41 -0700185vector<string> Manager::EnumerateDevices() {
186 vector<string> device_rpc_ids;
187 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
188 it != devices_.end();
189 ++it) {
190 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
191 }
192 return device_rpc_ids;
193}
194
195vector<string> Manager::EnumerateAvailableServices() {
196 // TODO(cmasone): This should, instead, be returned by calling into the
197 // currently active profile.
198 vector<string> service_rpc_ids;
199 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
200 it != services_.end();
201 ++it) {
202 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
203 }
204 return service_rpc_ids;
205}
206
207vector<string> Manager::EnumerateWatchedServices() {
208 // TODO(cmasone): Implement this for real by querying the active profile.
209 return EnumerateAvailableServices();
210}
211
Paul Stewart75897df2011-04-27 09:05:53 -0700212} // namespace shill