blob: f1d8ee82870e3321ef096fb3310cac9a4b09ef65 [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 Masone3bd3c8c2011-06-13 08:20:26 -070035 running_(false),
36 offline_mode_(false),
37 state_(flimflam::kStateOffline) {
Chris Masone27c4aa52011-07-02 13:10:14 -070038 HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
39 &Manager::AvailableTechnologies,
40 NULL);
41 HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
42 &Manager::ConnectedTechnologies,
43 NULL);
44 HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty,
45 &Manager::DefaultTechnology,
46 NULL);
47 store_.RegisterString(flimflam::kCheckPortalListProperty,
48 &check_portal_list_);
49 store_.RegisterString(flimflam::kCountryProperty, &country_);
50 HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
51 &Manager::EnabledTechnologies,
52 NULL);
53 store_.RegisterBool(flimflam::kOfflineModeProperty, &offline_mode_);
54 store_.RegisterString(flimflam::kPortalURLProperty, &portal_url_);
55 HelpRegisterDerivedString(flimflam::kStateProperty,
56 &Manager::CalculateState,
57 NULL);
Chris Masoneb925cc82011-06-22 15:39:57 -070058
Chris Masone27c4aa52011-07-02 13:10:14 -070059 HelpRegisterDerivedStrings(flimflam::kDevicesProperty,
60 &Manager::EnumerateDevices,
61 NULL);
62 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
63 &Manager::EnumerateAvailableServices,
64 NULL);
65 HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty,
66 &Manager::EnumerateWatchedServices,
67 NULL);
Chris Masone3c3f6a12011-07-01 10:01:41 -070068
Chris Masone4d42df82011-07-02 17:09:39 -070069 // TODO(cmasone): Wire these up once we actually put in profile support.
Chris Masoneb925cc82011-06-22 15:39:57 -070070 // known_properties_.push_back(flimflam::kActiveProfileProperty);
71 // known_properties_.push_back(flimflam::kProfilesProperty);
Chris Masoneb925cc82011-06-22 15:39:57 -070072
Chris Masoneb07006b2011-05-14 16:10:04 -070073 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070074}
75
Paul Stewart0af98bf2011-05-10 17:38:08 -070076Manager::~Manager() {}
Paul Stewart75897df2011-04-27 09:05:53 -070077
78void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070079 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070080 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -070081 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -070082 device_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -070083}
84
85void Manager::Stop() {
86 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070087 adaptor_->UpdateRunning();
Paul Stewart75897df2011-04-27 09:05:53 -070088}
89
Chris Masone2b105542011-06-22 10:58:09 -070090void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -070091 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -070092 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -070093 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -070094 return;
95 }
Chris Masonec1e50412011-06-07 13:04:53 -070096 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -070097
98 // TODO(pstew): Should check configuration
99 if (running_)
100 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700101}
102
Chris Masone2b105542011-06-22 10:58:09 -0700103void Manager::DeregisterDevice(const DeviceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700104 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700105 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700106 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700107 devices_.erase(it);
108 return;
109 }
110 }
111}
112
Chris Masone2b105542011-06-22 10:58:09 -0700113void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700114 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700115 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700116 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700117 return;
118 }
Chris Masonec1e50412011-06-07 13:04:53 -0700119 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700120}
121
Chris Masone2b105542011-06-22 10:58:09 -0700122void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700123 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700124 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700125 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700126 services_.erase(it);
127 return;
128 }
129 }
130}
131
132void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700133 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700134 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700135 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700136 for (it = devices_.begin(); it != devices_.end(); ++it) {
137 if ((*it)->TechnologyIs(tech))
138 found->push_back(*it);
139 }
140}
141
Chris Masonee0dea762011-06-09 09:06:03 -0700142ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700143 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700144 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonee0dea762011-06-09 09:06:03 -0700145 if (name == (*it)->UniqueName()) {
146 return *it;
147 }
Chris Masone9be4a9d2011-05-16 15:44:09 -0700148 }
Chris Masonee0dea762011-06-09 09:06:03 -0700149 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700150}
151
Chris Masone27c4aa52011-07-02 13:10:14 -0700152void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700153 string(Manager::*get)(void),
154 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700155 store_.RegisterDerivedString(
156 name,
157 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700158}
159
Chris Masone27c4aa52011-07-02 13:10:14 -0700160void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700161 Strings(Manager::*get)(void),
162 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700163 store_.RegisterDerivedStrings(
164 name,
165 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700166}
167
168string Manager::CalculateState() {
169 return flimflam::kStateOffline;
170}
171
172vector<string> Manager::AvailableTechnologies() {
173 return vector<string>();
174}
175
176vector<string> Manager::ConnectedTechnologies() {
177 return vector<string>();
178}
179
180string Manager::DefaultTechnology() {
181 return "";
182}
183
184vector<string> Manager::EnabledTechnologies() {
185 return vector<string>();
186}
187
Chris Masone3c3f6a12011-07-01 10:01:41 -0700188vector<string> Manager::EnumerateDevices() {
189 vector<string> device_rpc_ids;
190 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
191 it != devices_.end();
192 ++it) {
193 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
194 }
195 return device_rpc_ids;
196}
197
198vector<string> Manager::EnumerateAvailableServices() {
199 // TODO(cmasone): This should, instead, be returned by calling into the
200 // currently active profile.
201 vector<string> service_rpc_ids;
202 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
203 it != services_.end();
204 ++it) {
205 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
206 }
207 return service_rpc_ids;
208}
209
210vector<string> Manager::EnumerateWatchedServices() {
211 // TODO(cmasone): Implement this for real by querying the active profile.
212 return EnumerateAvailableServices();
213}
214
Paul Stewart75897df2011-04-27 09:05:53 -0700215} // namespace shill