blob: 19aadbd19c95add47b9ca3b07aaad9f54b0a7c77 [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
Paul Stewarte6132022011-08-16 09:11:02 -070013#include <base/file_util.h>
Chris Masoneee929b72011-05-10 10:02:18 -070014#include <base/logging.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070015#include <base/memory/ref_counted.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070016#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070017
Chris Masoned0ceb8c2011-06-02 10:05:39 -070018#include "shill/adaptor_interfaces.h"
Paul Stewart75897df2011-04-27 09:05:53 -070019#include "shill/control_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070020#include "shill/dbus_adaptor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070021#include "shill/default_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070022#include "shill/device.h"
23#include "shill/device_info.h"
Chris Masone6791a432011-07-12 13:23:19 -070024#include "shill/ephemeral_profile.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070025#include "shill/error.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070026#include "shill/profile.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070027#include "shill/property_accessor.h"
Paul Stewarte6132022011-08-16 09:11:02 -070028#include "shill/resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070029#include "shill/shill_event.h"
30#include "shill/service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070031
32using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070033using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070034
35namespace shill {
Paul Stewarte6132022011-08-16 09:11:02 -070036
Paul Stewart75897df2011-04-27 09:05:53 -070037Manager::Manager(ControlInterface *control_interface,
Darin Petkov887f2982011-07-14 16:10:17 -070038 EventDispatcher *dispatcher,
Chris Masone2ae797d2011-08-23 20:41:00 -070039 GLib *glib,
40 const string &run_directory,
41 const string &storage_directory,
42 const string &user_storage_format)
43 : run_path_(FilePath(run_directory)),
44 storage_path_(FilePath(storage_directory)),
45 user_storage_format_(user_storage_format),
Paul Stewarte6132022011-08-16 09:11:02 -070046 adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070047 device_info_(control_interface, dispatcher, this),
Darin Petkov887f2982011-07-14 16:10:17 -070048 modem_info_(control_interface, dispatcher, this, glib),
Chris Masone6791a432011-07-12 13:23:19 -070049 running_(false),
Chris Masone2ae797d2011-08-23 20:41:00 -070050 ephemeral_profile_(new EphemeralProfile(control_interface, glib, this)),
51 control_interface_(control_interface),
52 glib_(glib) {
Chris Masone7aa5f902011-07-11 11:13:35 -070053 HelpRegisterDerivedString(flimflam::kActiveProfileProperty,
54 &Manager::GetActiveProfileName,
55 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070056 HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
57 &Manager::AvailableTechnologies,
58 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070059 store_.RegisterString(flimflam::kCheckPortalListProperty,
60 &props_.check_portal_list);
Chris Masone27c4aa52011-07-02 13:10:14 -070061 HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
62 &Manager::ConnectedTechnologies,
63 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070064 store_.RegisterString(flimflam::kCountryProperty, &props_.country);
Chris Masone27c4aa52011-07-02 13:10:14 -070065 HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty,
66 &Manager::DefaultTechnology,
67 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070068 HelpRegisterDerivedStrings(flimflam::kDevicesProperty,
69 &Manager::EnumerateDevices,
70 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070071 HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
72 &Manager::EnabledTechnologies,
73 NULL);
74 store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode);
75 store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url);
76 HelpRegisterDerivedString(flimflam::kStateProperty,
77 &Manager::CalculateState,
78 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070079 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
80 &Manager::EnumerateAvailableServices,
81 NULL);
82 HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty,
83 &Manager::EnumerateWatchedServices,
84 NULL);
Chris Masone3c3f6a12011-07-01 10:01:41 -070085
Chris Masone4d42df82011-07-02 17:09:39 -070086 // TODO(cmasone): Wire these up once we actually put in profile support.
Chris Masoneb925cc82011-06-22 15:39:57 -070087 // known_properties_.push_back(flimflam::kProfilesProperty);
Chris Masone2ae797d2011-08-23 20:41:00 -070088 profiles_.push_back(new DefaultProfile(control_interface_,
89 glib_,
Chris Masone6791a432011-07-12 13:23:19 -070090 this,
Chris Masone2ae797d2011-08-23 20:41:00 -070091 storage_path_,
Chris Masone6791a432011-07-12 13:23:19 -070092 props_));
Chris Masoneb07006b2011-05-14 16:10:04 -070093 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -070094}
95
Chris Masone6791a432011-07-12 13:23:19 -070096Manager::~Manager() {
97 vector<ProfileRefPtr>::iterator it;
98 for (it = profiles_.begin(); it != profiles_.end(); ++it) {
99 (*it)->Finalize();
100 }
101 ephemeral_profile_->Finalize();
102}
Paul Stewart75897df2011-04-27 09:05:53 -0700103
mukesh agrawal8f317b62011-07-15 11:53:23 -0700104void Manager::AddDeviceToBlackList(const string &device_name) {
105 device_info_.AddDeviceToBlackList(device_name);
106}
107
Paul Stewart75897df2011-04-27 09:05:53 -0700108void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -0700109 LOG(INFO) << "Manager started.";
Paul Stewarte6132022011-08-16 09:11:02 -0700110
Chris Masone2ae797d2011-08-23 20:41:00 -0700111 CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value();
Paul Stewarte6132022011-08-16 09:11:02 -0700112 Resolver::GetInstance()->set_path(run_path_.Append("resolv.conf"));
Chris Masone2ae797d2011-08-23 20:41:00 -0700113
114 CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value();
115
Paul Stewart75897df2011-04-27 09:05:53 -0700116 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -0700117 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700118 device_info_.Start();
Darin Petkov887f2982011-07-14 16:10:17 -0700119 modem_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -0700120}
121
122void Manager::Stop() {
123 running_ = false;
Chris Masone413a3192011-05-09 17:10:05 -0700124 adaptor_->UpdateRunning();
Darin Petkov887f2982011-07-14 16:10:17 -0700125 modem_info_.Stop();
126 device_info_.Stop();
Paul Stewart75897df2011-04-27 09:05:53 -0700127}
128
Chris Masone7aa5f902011-07-11 11:13:35 -0700129const ProfileRefPtr &Manager::ActiveProfile() {
130 return profiles_.back();
131}
132
Chris Masone6791a432011-07-12 13:23:19 -0700133bool Manager::MoveToActiveProfile(const ProfileRefPtr &from,
134 const ServiceRefPtr &to_move) {
135 return ActiveProfile()->AdoptService(to_move) &&
136 from->AbandonService(to_move->UniqueName());
137}
138
Chris Masone2b105542011-06-22 10:58:09 -0700139void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700140 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700141 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700142 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700143 return;
144 }
Chris Masonec1e50412011-06-07 13:04:53 -0700145 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700146
147 // TODO(pstew): Should check configuration
148 if (running_)
149 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700150}
151
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700152void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700153 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700154 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700155 if (to_forget.get() == it->get()) {
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700156 VLOG(2) << "Deregistered device: " << to_forget->UniqueName();
157 to_forget->Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700158 devices_.erase(it);
159 return;
160 }
161 }
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700162 VLOG(2) << __func__ << " unknown device: " << to_forget->UniqueName();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700163}
164
Chris Masone2b105542011-06-22 10:58:09 -0700165void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masone6791a432011-07-12 13:23:19 -0700166 // This should look for |to_manage| in the real profiles and, if found,
167 // do...something...to merge the meaningful state, I guess.
168
169 // If not found, add it to the ephemeral profile
170 ephemeral_profile_->AdoptService(to_manage);
171
172 // Now add to OUR list.
173 // TODO(cmasone): Keep this list sorted.
Chris Masonec1e50412011-06-07 13:04:53 -0700174 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700175 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700176 if (to_manage->UniqueName() == (*it)->UniqueName())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700177 return;
178 }
Chris Masonec1e50412011-06-07 13:04:53 -0700179 services_.push_back(to_manage);
mukesh agrawal32399322011-09-01 10:53:43 -0700180
181 vector<string> service_paths;
182 for (it = services_.begin(); it != services_.end(); ++it) {
183 service_paths.push_back((*it)->GetRpcIdentifier());
184 }
185 adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty,
186 service_paths);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700187}
188
Chris Masone2b105542011-06-22 10:58:09 -0700189void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masone6791a432011-07-12 13:23:19 -0700190 // If the service is in the ephemeral profile, destroy it.
191 if (!ephemeral_profile_->AbandonService(to_forget->UniqueName())) {
192 // if it's in one of the real profiles...um...I guess mark it unconnectable?
193 }
Chris Masonec1e50412011-06-07 13:04:53 -0700194 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700195 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700196 if (to_forget->UniqueName() == (*it)->UniqueName()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700197 services_.erase(it);
198 return;
199 }
200 }
201}
202
Paul Stewart03dba0b2011-08-22 16:32:45 -0700203void Manager::UpdateService(const ServiceConstRefPtr &to_update) {
204 LOG(INFO) << "Service " << to_update->UniqueName() << " updated;"
205 << " state: " << to_update->state() << " failure: "
206 << to_update->failure();
207 // TODO(pstew): This should trigger re-sorting of services, autoconnect, etc.
208}
209
Chris Masone9be4a9d2011-05-16 15:44:09 -0700210void Manager::FilterByTechnology(Device::Technology tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700211 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700212 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700213 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700214 for (it = devices_.begin(); it != devices_.end(); ++it) {
215 if ((*it)->TechnologyIs(tech))
216 found->push_back(*it);
217 }
218}
219
Chris Masonee0dea762011-06-09 09:06:03 -0700220ServiceRefPtr Manager::FindService(const std::string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700221 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700222 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700223 if (name == (*it)->UniqueName())
Chris Masonee0dea762011-06-09 09:06:03 -0700224 return *it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700225 }
Chris Masonee0dea762011-06-09 09:06:03 -0700226 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700227}
228
Chris Masone27c4aa52011-07-02 13:10:14 -0700229void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700230 string(Manager::*get)(void),
231 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700232 store_.RegisterDerivedString(
233 name,
234 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700235}
236
Chris Masone27c4aa52011-07-02 13:10:14 -0700237void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700238 Strings(Manager::*get)(void),
239 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700240 store_.RegisterDerivedStrings(
241 name,
242 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700243}
244
245string Manager::CalculateState() {
246 return flimflam::kStateOffline;
247}
248
249vector<string> Manager::AvailableTechnologies() {
250 return vector<string>();
251}
252
253vector<string> Manager::ConnectedTechnologies() {
254 return vector<string>();
255}
256
257string Manager::DefaultTechnology() {
258 return "";
259}
260
261vector<string> Manager::EnabledTechnologies() {
262 return vector<string>();
263}
264
Chris Masone3c3f6a12011-07-01 10:01:41 -0700265vector<string> Manager::EnumerateDevices() {
266 vector<string> device_rpc_ids;
267 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
268 it != devices_.end();
269 ++it) {
270 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
271 }
272 return device_rpc_ids;
273}
274
275vector<string> Manager::EnumerateAvailableServices() {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700276 vector<string> service_rpc_ids;
277 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
278 it != services_.end();
279 ++it) {
280 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
281 }
282 return service_rpc_ids;
283}
284
285vector<string> Manager::EnumerateWatchedServices() {
Chris Masone6791a432011-07-12 13:23:19 -0700286 // TODO(cmasone): Filter this list for services in appropriate states.
Chris Masone3c3f6a12011-07-01 10:01:41 -0700287 return EnumerateAvailableServices();
288}
289
Chris Masone7aa5f902011-07-11 11:13:35 -0700290string Manager::GetActiveProfileName() {
Chris Masone7df0c672011-07-15 10:24:54 -0700291 return ActiveProfile()->GetFriendlyName();
Chris Masone7aa5f902011-07-11 11:13:35 -0700292}
293
mukesh agrawal32399322011-09-01 10:53:43 -0700294// called via RPC (e.g., from ManagerDBusAdaptor)
295void Manager::RequestScan(const std::string &technology, Error *error) {
296 if (technology == flimflam::kTypeWifi || technology == "") {
297 vector<DeviceRefPtr> wifi_devices;
298 FilterByTechnology(Device::kWifi, &wifi_devices);
299
300 for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin();
301 it != wifi_devices.end();
302 ++it) {
303 (*it)->Scan();
304 }
305 } else {
306 // TODO(quiche): support scanning for other technologies?
307 const string kMessage = "Unrecognized technology " + technology;
308 LOG(ERROR) << kMessage;
309 CHECK(error);
310 error->Populate(Error::kInvalidArguments, kMessage);
311 }
312}
313
Paul Stewart75897df2011-04-27 09:05:53 -0700314} // namespace shill