blob: 9395269724cea546f138ace49e97af1702939693 [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 Masone6791a432011-07-12 13:23:19 -070023#include "shill/ephemeral_profile.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070024#include "shill/error.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070025#include "shill/profile.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070026#include "shill/property_accessor.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070027#include "shill/shill_event.h"
28#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070029
30using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070031using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070032
33namespace shill {
34Manager::Manager(ControlInterface *control_interface,
Darin Petkov887f2982011-07-14 16:10:17 -070035 EventDispatcher *dispatcher,
36 GLib *glib)
Chris Masone413a3192011-05-09 17:10:05 -070037 : adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070038 device_info_(control_interface, dispatcher, this),
Darin Petkov887f2982011-07-14 16:10:17 -070039 modem_info_(control_interface, dispatcher, this, glib),
Chris Masone6791a432011-07-12 13:23:19 -070040 running_(false),
41 ephemeral_profile_(new EphemeralProfile(control_interface, glib, this)) {
Chris Masone7aa5f902011-07-11 11:13:35 -070042 HelpRegisterDerivedString(flimflam::kActiveProfileProperty,
43 &Manager::GetActiveProfileName,
44 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070045 HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
46 &Manager::AvailableTechnologies,
47 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070048 store_.RegisterString(flimflam::kCheckPortalListProperty,
49 &props_.check_portal_list);
Chris Masone27c4aa52011-07-02 13:10:14 -070050 HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
51 &Manager::ConnectedTechnologies,
52 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070053 store_.RegisterString(flimflam::kCountryProperty, &props_.country);
Chris Masone27c4aa52011-07-02 13:10:14 -070054 HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty,
55 &Manager::DefaultTechnology,
56 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070057 HelpRegisterDerivedStrings(flimflam::kDevicesProperty,
58 &Manager::EnumerateDevices,
59 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070060 HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
61 &Manager::EnabledTechnologies,
62 NULL);
63 store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode);
64 store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url);
65 HelpRegisterDerivedString(flimflam::kStateProperty,
66 &Manager::CalculateState,
67 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070068 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
69 &Manager::EnumerateAvailableServices,
70 NULL);
71 HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty,
72 &Manager::EnumerateWatchedServices,
73 NULL);
Chris Masone3c3f6a12011-07-01 10:01:41 -070074
Chris Masone4d42df82011-07-02 17:09:39 -070075 // TODO(cmasone): Wire these up once we actually put in profile support.
Chris Masoneb925cc82011-06-22 15:39:57 -070076 // known_properties_.push_back(flimflam::kProfilesProperty);
Chris Masoneb925cc82011-06-22 15:39:57 -070077
Chris Masone6791a432011-07-12 13:23:19 -070078 profiles_.push_back(new DefaultProfile(control_interface,
79 glib,
80 this,
81 props_));
Chris Masoneb07006b2011-05-14 16:10:04 -070082 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070083}
84
Chris Masone6791a432011-07-12 13:23:19 -070085Manager::~Manager() {
86 vector<ProfileRefPtr>::iterator it;
87 for (it = profiles_.begin(); it != profiles_.end(); ++it) {
88 (*it)->Finalize();
89 }
90 ephemeral_profile_->Finalize();
91}
Paul Stewart75897df2011-04-27 09:05:53 -070092
mukesh agrawal8f317b62011-07-15 11:53:23 -070093void Manager::AddDeviceToBlackList(const string &device_name) {
94 device_info_.AddDeviceToBlackList(device_name);
95}
96
Paul Stewart75897df2011-04-27 09:05:53 -070097void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070098 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070099 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -0700100 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700101 device_info_.Start();
Darin Petkov887f2982011-07-14 16:10:17 -0700102 modem_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -0700103}
104
105void Manager::Stop() {
106 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700107 adaptor_->UpdateRunning();
Darin Petkov887f2982011-07-14 16:10:17 -0700108 modem_info_.Stop();
109 device_info_.Stop();
Paul Stewart75897df2011-04-27 09:05:53 -0700110}
111
Chris Masone7aa5f902011-07-11 11:13:35 -0700112const ProfileRefPtr &Manager::ActiveProfile() {
113 return profiles_.back();
114}
115
Chris Masone6791a432011-07-12 13:23:19 -0700116bool Manager::MoveToActiveProfile(const ProfileRefPtr &from,
117 const ServiceRefPtr &to_move) {
118 return ActiveProfile()->AdoptService(to_move) &&
119 from->AbandonService(to_move->UniqueName());
120}
121
Chris Masone2b105542011-06-22 10:58:09 -0700122void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700123 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700124 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700125 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700126 return;
127 }
Chris Masonec1e50412011-06-07 13:04:53 -0700128 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700129
130 // TODO(pstew): Should check configuration
131 if (running_)
132 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700133}
134
Chris Masone2b105542011-06-22 10:58:09 -0700135void Manager::DeregisterDevice(const DeviceConstRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700136 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700137 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700138 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700139 devices_.erase(it);
140 return;
141 }
142 }
143}
144
Chris Masone2b105542011-06-22 10:58:09 -0700145void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masone6791a432011-07-12 13:23:19 -0700146 // This should look for |to_manage| in the real profiles and, if found,
147 // do...something...to merge the meaningful state, I guess.
148
149 // If not found, add it to the ephemeral profile
150 ephemeral_profile_->AdoptService(to_manage);
151
152 // Now add to OUR list.
153 // TODO(cmasone): Keep this list sorted.
Chris Masonec1e50412011-06-07 13:04:53 -0700154 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700155 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700156 if (to_manage->UniqueName() == (*it)->UniqueName())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700157 return;
158 }
Chris Masonec1e50412011-06-07 13:04:53 -0700159 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700160}
161
Chris Masone2b105542011-06-22 10:58:09 -0700162void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masone6791a432011-07-12 13:23:19 -0700163 // If the service is in the ephemeral profile, destroy it.
164 if (!ephemeral_profile_->AbandonService(to_forget->UniqueName())) {
165 // if it's in one of the real profiles...um...I guess mark it unconnectable?
166 }
Chris Masonec1e50412011-06-07 13:04:53 -0700167 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700168 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700169 if (to_forget->UniqueName() == (*it)->UniqueName()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700170 services_.erase(it);
171 return;
172 }
173 }
174}
175
176void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700177 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700178 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700179 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700180 for (it = devices_.begin(); it != devices_.end(); ++it) {
181 if ((*it)->TechnologyIs(tech))
182 found->push_back(*it);
183 }
184}
185
Chris Masonee0dea762011-06-09 09:06:03 -0700186ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700187 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700188 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700189 if (name == (*it)->UniqueName())
Chris Masonee0dea762011-06-09 09:06:03 -0700190 return *it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700191 }
Chris Masonee0dea762011-06-09 09:06:03 -0700192 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700193}
194
Chris Masone27c4aa52011-07-02 13:10:14 -0700195void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700196 string(Manager::*get)(void),
197 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700198 store_.RegisterDerivedString(
199 name,
200 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700201}
202
Chris Masone27c4aa52011-07-02 13:10:14 -0700203void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700204 Strings(Manager::*get)(void),
205 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700206 store_.RegisterDerivedStrings(
207 name,
208 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700209}
210
211string Manager::CalculateState() {
212 return flimflam::kStateOffline;
213}
214
215vector<string> Manager::AvailableTechnologies() {
216 return vector<string>();
217}
218
219vector<string> Manager::ConnectedTechnologies() {
220 return vector<string>();
221}
222
223string Manager::DefaultTechnology() {
224 return "";
225}
226
227vector<string> Manager::EnabledTechnologies() {
228 return vector<string>();
229}
230
Chris Masone3c3f6a12011-07-01 10:01:41 -0700231vector<string> Manager::EnumerateDevices() {
232 vector<string> device_rpc_ids;
233 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
234 it != devices_.end();
235 ++it) {
236 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
237 }
238 return device_rpc_ids;
239}
240
241vector<string> Manager::EnumerateAvailableServices() {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700242 vector<string> service_rpc_ids;
243 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
244 it != services_.end();
245 ++it) {
246 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
247 }
248 return service_rpc_ids;
249}
250
251vector<string> Manager::EnumerateWatchedServices() {
Chris Masone6791a432011-07-12 13:23:19 -0700252 // TODO(cmasone): Filter this list for services in appropriate states.
Chris Masone3c3f6a12011-07-01 10:01:41 -0700253 return EnumerateAvailableServices();
254}
255
Chris Masone7aa5f902011-07-11 11:13:35 -0700256string Manager::GetActiveProfileName() {
Chris Masone7df0c672011-07-15 10:24:54 -0700257 return ActiveProfile()->GetFriendlyName();
Chris Masone7aa5f902011-07-11 11:13:35 -0700258}
259
Paul Stewart75897df2011-04-27 09:05:53 -0700260} // namespace shill