blob: 820dba7d04e603ff24d186a20af0c09ea55a1cbf [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 Masone7aa5f902011-07-11 11:13:35 -070020#include "shill/default_profile.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 Masone7aa5f902011-07-11 11:13:35 -070024#include "shill/profile.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070025#include "shill/property_accessor.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070026#include "shill/shill_event.h"
27#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070028
29using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070030using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070031
32namespace shill {
33Manager::Manager(ControlInterface *control_interface,
Darin Petkov887f2982011-07-14 16:10:17 -070034 EventDispatcher *dispatcher,
35 GLib *glib)
Chris Masone413a3192011-05-09 17:10:05 -070036 : adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070037 device_info_(control_interface, dispatcher, this),
Darin Petkov887f2982011-07-14 16:10:17 -070038 modem_info_(control_interface, dispatcher, this, glib),
Chris Masone88cbd5f2011-07-03 14:30:04 -070039 running_(false) {
Chris Masone7aa5f902011-07-11 11:13:35 -070040 HelpRegisterDerivedString(flimflam::kActiveProfileProperty,
41 &Manager::GetActiveProfileName,
42 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070043 HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
44 &Manager::AvailableTechnologies,
45 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070046 store_.RegisterString(flimflam::kCheckPortalListProperty,
47 &props_.check_portal_list);
Chris Masone27c4aa52011-07-02 13:10:14 -070048 HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
49 &Manager::ConnectedTechnologies,
50 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070051 store_.RegisterString(flimflam::kCountryProperty, &props_.country);
Chris Masone27c4aa52011-07-02 13:10:14 -070052 HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty,
53 &Manager::DefaultTechnology,
54 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070055 HelpRegisterDerivedStrings(flimflam::kDevicesProperty,
56 &Manager::EnumerateDevices,
57 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070058 HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
59 &Manager::EnabledTechnologies,
60 NULL);
61 store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode);
62 store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url);
63 HelpRegisterDerivedString(flimflam::kStateProperty,
64 &Manager::CalculateState,
65 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070066 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
67 &Manager::EnumerateAvailableServices,
68 NULL);
69 HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty,
70 &Manager::EnumerateWatchedServices,
71 NULL);
Chris Masone3c3f6a12011-07-01 10:01:41 -070072
Chris Masone4d42df82011-07-02 17:09:39 -070073 // TODO(cmasone): Wire these up once we actually put in profile support.
Chris Masoneb925cc82011-06-22 15:39:57 -070074 // known_properties_.push_back(flimflam::kProfilesProperty);
Chris Masoneb925cc82011-06-22 15:39:57 -070075
Darin Petkov887f2982011-07-14 16:10:17 -070076 profiles_.push_back(new DefaultProfile(control_interface, glib, props_));
Chris Masone7aa5f902011-07-11 11:13:35 -070077
Chris Masoneb07006b2011-05-14 16:10:04 -070078 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070079}
80
Paul Stewart0af98bf2011-05-10 17:38:08 -070081Manager::~Manager() {}
Paul Stewart75897df2011-04-27 09:05:53 -070082
83void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070084 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070085 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -070086 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -070087 device_info_.Start();
Darin Petkov887f2982011-07-14 16:10:17 -070088 modem_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -070089}
90
91void Manager::Stop() {
92 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -070093 adaptor_->UpdateRunning();
Darin Petkov887f2982011-07-14 16:10:17 -070094 modem_info_.Stop();
95 device_info_.Stop();
Paul Stewart75897df2011-04-27 09:05:53 -070096}
97
Chris Masone7aa5f902011-07-11 11:13:35 -070098const ProfileRefPtr &Manager::ActiveProfile() {
99 return profiles_.back();
100}
101
Chris Masone2b105542011-06-22 10:58:09 -0700102void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700103 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700104 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700105 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700106 return;
107 }
Chris Masonec1e50412011-06-07 13:04:53 -0700108 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700109
110 // TODO(pstew): Should check configuration
111 if (running_)
112 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700113}
114
Chris Masone2b105542011-06-22 10:58:09 -0700115void Manager::DeregisterDevice(const DeviceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700116 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700117 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700118 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700119 devices_.erase(it);
120 return;
121 }
122 }
123}
124
Chris Masone2b105542011-06-22 10:58:09 -0700125void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700126 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700127 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700128 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700129 return;
130 }
Chris Masonec1e50412011-06-07 13:04:53 -0700131 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700132}
133
Chris Masone2b105542011-06-22 10:58:09 -0700134void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700135 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700136 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700137 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700138 services_.erase(it);
139 return;
140 }
141 }
142}
143
144void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700145 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700146 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700147 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700148 for (it = devices_.begin(); it != devices_.end(); ++it) {
149 if ((*it)->TechnologyIs(tech))
150 found->push_back(*it);
151 }
152}
153
Chris Masonee0dea762011-06-09 09:06:03 -0700154ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700155 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700156 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masonee0dea762011-06-09 09:06:03 -0700157 if (name == (*it)->UniqueName()) {
158 return *it;
159 }
Chris Masone9be4a9d2011-05-16 15:44:09 -0700160 }
Chris Masonee0dea762011-06-09 09:06:03 -0700161 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700162}
163
Chris Masone27c4aa52011-07-02 13:10:14 -0700164void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700165 string(Manager::*get)(void),
166 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700167 store_.RegisterDerivedString(
168 name,
169 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700170}
171
Chris Masone27c4aa52011-07-02 13:10:14 -0700172void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700173 Strings(Manager::*get)(void),
174 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700175 store_.RegisterDerivedStrings(
176 name,
177 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
Chris Masone3c3f6a12011-07-01 10:01:41 -0700200vector<string> Manager::EnumerateDevices() {
201 vector<string> device_rpc_ids;
202 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
203 it != devices_.end();
204 ++it) {
205 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
206 }
207 return device_rpc_ids;
208}
209
210vector<string> Manager::EnumerateAvailableServices() {
211 // TODO(cmasone): This should, instead, be returned by calling into the
212 // currently active profile.
213 vector<string> service_rpc_ids;
214 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
215 it != services_.end();
216 ++it) {
217 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
218 }
219 return service_rpc_ids;
220}
221
222vector<string> Manager::EnumerateWatchedServices() {
223 // TODO(cmasone): Implement this for real by querying the active profile.
224 return EnumerateAvailableServices();
225}
226
Chris Masone7aa5f902011-07-11 11:13:35 -0700227string Manager::GetActiveProfileName() {
228 return ActiveProfile()->name();
229}
230
Paul Stewart75897df2011-04-27 09:05:53 -0700231} // namespace shill