blob: ce7a2a44a5de0441ee2e33baacb60c0221f9e213 [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
93void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -070094 LOG(INFO) << "Manager started.";
Paul Stewart75897df2011-04-27 09:05:53 -070095 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -070096 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -070097 device_info_.Start();
Darin Petkov887f2982011-07-14 16:10:17 -070098 modem_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -070099}
100
101void Manager::Stop() {
102 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700103 adaptor_->UpdateRunning();
Darin Petkov887f2982011-07-14 16:10:17 -0700104 modem_info_.Stop();
105 device_info_.Stop();
Paul Stewart75897df2011-04-27 09:05:53 -0700106}
107
Chris Masone7aa5f902011-07-11 11:13:35 -0700108const ProfileRefPtr &Manager::ActiveProfile() {
109 return profiles_.back();
110}
111
Chris Masone6791a432011-07-12 13:23:19 -0700112bool Manager::MoveToActiveProfile(const ProfileRefPtr &from,
113 const ServiceRefPtr &to_move) {
114 return ActiveProfile()->AdoptService(to_move) &&
115 from->AbandonService(to_move->UniqueName());
116}
117
Chris Masone2b105542011-06-22 10:58:09 -0700118void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700119 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700120 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700121 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700122 return;
123 }
Chris Masonec1e50412011-06-07 13:04:53 -0700124 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700125
126 // TODO(pstew): Should check configuration
127 if (running_)
128 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700129}
130
Chris Masone2b105542011-06-22 10:58:09 -0700131void Manager::DeregisterDevice(const DeviceConstRefPtr &to_forget) {
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) {
Chris Masonec1e50412011-06-07 13:04:53 -0700134 if (to_forget.get() == it->get()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700135 devices_.erase(it);
136 return;
137 }
138 }
139}
140
Chris Masone2b105542011-06-22 10:58:09 -0700141void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masone6791a432011-07-12 13:23:19 -0700142 // This should look for |to_manage| in the real profiles and, if found,
143 // do...something...to merge the meaningful state, I guess.
144
145 // If not found, add it to the ephemeral profile
146 ephemeral_profile_->AdoptService(to_manage);
147
148 // Now add to OUR list.
149 // TODO(cmasone): Keep this list sorted.
Chris Masonec1e50412011-06-07 13:04:53 -0700150 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700151 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700152 if (to_manage->UniqueName() == (*it)->UniqueName())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700153 return;
154 }
Chris Masonec1e50412011-06-07 13:04:53 -0700155 services_.push_back(to_manage);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700156}
157
Chris Masone2b105542011-06-22 10:58:09 -0700158void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masone6791a432011-07-12 13:23:19 -0700159 // If the service is in the ephemeral profile, destroy it.
160 if (!ephemeral_profile_->AbandonService(to_forget->UniqueName())) {
161 // if it's in one of the real profiles...um...I guess mark it unconnectable?
162 }
Chris Masonec1e50412011-06-07 13:04:53 -0700163 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700164 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700165 if (to_forget->UniqueName() == (*it)->UniqueName()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700166 services_.erase(it);
167 return;
168 }
169 }
170}
171
172void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700173 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700174 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700175 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700176 for (it = devices_.begin(); it != devices_.end(); ++it) {
177 if ((*it)->TechnologyIs(tech))
178 found->push_back(*it);
179 }
180}
181
Chris Masonee0dea762011-06-09 09:06:03 -0700182ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700183 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700184 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700185 if (name == (*it)->UniqueName())
Chris Masonee0dea762011-06-09 09:06:03 -0700186 return *it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700187 }
Chris Masonee0dea762011-06-09 09:06:03 -0700188 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700189}
190
Chris Masone27c4aa52011-07-02 13:10:14 -0700191void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700192 string(Manager::*get)(void),
193 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700194 store_.RegisterDerivedString(
195 name,
196 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700197}
198
Chris Masone27c4aa52011-07-02 13:10:14 -0700199void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700200 Strings(Manager::*get)(void),
201 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700202 store_.RegisterDerivedStrings(
203 name,
204 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700205}
206
207string Manager::CalculateState() {
208 return flimflam::kStateOffline;
209}
210
211vector<string> Manager::AvailableTechnologies() {
212 return vector<string>();
213}
214
215vector<string> Manager::ConnectedTechnologies() {
216 return vector<string>();
217}
218
219string Manager::DefaultTechnology() {
220 return "";
221}
222
223vector<string> Manager::EnabledTechnologies() {
224 return vector<string>();
225}
226
Chris Masone3c3f6a12011-07-01 10:01:41 -0700227vector<string> Manager::EnumerateDevices() {
228 vector<string> device_rpc_ids;
229 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
230 it != devices_.end();
231 ++it) {
232 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
233 }
234 return device_rpc_ids;
235}
236
237vector<string> Manager::EnumerateAvailableServices() {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700238 vector<string> service_rpc_ids;
239 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
240 it != services_.end();
241 ++it) {
242 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
243 }
244 return service_rpc_ids;
245}
246
247vector<string> Manager::EnumerateWatchedServices() {
Chris Masone6791a432011-07-12 13:23:19 -0700248 // TODO(cmasone): Filter this list for services in appropriate states.
Chris Masone3c3f6a12011-07-01 10:01:41 -0700249 return EnumerateAvailableServices();
250}
251
Chris Masone7aa5f902011-07-11 11:13:35 -0700252string Manager::GetActiveProfileName() {
253 return ActiveProfile()->name();
254}
255
Paul Stewart75897df2011-04-27 09:05:53 -0700256} // namespace shill