blob: 1df0dc3f3e459605a310dab52fef40c0fa1dece0 [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 Stewart22aa71b2011-09-16 12:15:11 -070010#include <algorithm>
mukesh agrawal7a4e4002011-09-06 11:26:05 -070011#include <map>
Paul Stewart75897df2011-04-27 09:05:53 -070012#include <string>
Chris Masone52cd19b2011-06-29 17:23:04 -070013#include <vector>
Paul Stewart75897df2011-04-27 09:05:53 -070014
Paul Stewarte6132022011-08-16 09:11:02 -070015#include <base/file_util.h>
Chris Masoneee929b72011-05-10 10:02:18 -070016#include <base/logging.h>
Chris Masone9be4a9d2011-05-16 15:44:09 -070017#include <base/memory/ref_counted.h>
Paul Stewart22aa71b2011-09-16 12:15:11 -070018#include <base/string_split.h>
19#include <base/string_util.h>
Chris Masone3bd3c8c2011-06-13 08:20:26 -070020#include <chromeos/dbus/service_constants.h>
Chris Masoneee929b72011-05-10 10:02:18 -070021
Chris Masoned0ceb8c2011-06-02 10:05:39 -070022#include "shill/adaptor_interfaces.h"
Paul Stewart75897df2011-04-27 09:05:53 -070023#include "shill/control_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070024#include "shill/dbus_adaptor.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070025#include "shill/default_profile.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070026#include "shill/device.h"
27#include "shill/device_info.h"
Chris Masone6791a432011-07-12 13:23:19 -070028#include "shill/ephemeral_profile.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070029#include "shill/error.h"
Chris Masone9d779932011-08-25 16:33:41 -070030#include "shill/key_file_store.h"
Paul Stewart22aa71b2011-09-16 12:15:11 -070031#include "shill/service_sorter.h"
Chris Masone7aa5f902011-07-11 11:13:35 -070032#include "shill/profile.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070033#include "shill/property_accessor.h"
Paul Stewarte6132022011-08-16 09:11:02 -070034#include "shill/resolver.h"
Chris Masone9be4a9d2011-05-16 15:44:09 -070035#include "shill/shill_event.h"
36#include "shill/service.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070037#include "shill/wifi.h"
38#include "shill/wifi_service.h"
Paul Stewart75897df2011-04-27 09:05:53 -070039
Paul Stewart22aa71b2011-09-16 12:15:11 -070040using std::map;
Paul Stewart75897df2011-04-27 09:05:53 -070041using std::string;
Chris Masone9be4a9d2011-05-16 15:44:09 -070042using std::vector;
Paul Stewart75897df2011-04-27 09:05:53 -070043
44namespace shill {
Paul Stewarte6132022011-08-16 09:11:02 -070045
mukesh agrawal7a4e4002011-09-06 11:26:05 -070046// static
47const char Manager::kManagerErrorNoDevice[] = "no wifi devices available";
48
Paul Stewart75897df2011-04-27 09:05:53 -070049Manager::Manager(ControlInterface *control_interface,
Darin Petkov887f2982011-07-14 16:10:17 -070050 EventDispatcher *dispatcher,
Chris Masone2ae797d2011-08-23 20:41:00 -070051 GLib *glib,
52 const string &run_directory,
53 const string &storage_directory,
54 const string &user_storage_format)
55 : run_path_(FilePath(run_directory)),
56 storage_path_(FilePath(storage_directory)),
57 user_storage_format_(user_storage_format),
Paul Stewarte6132022011-08-16 09:11:02 -070058 adaptor_(control_interface->CreateManagerAdaptor(this)),
Paul Stewartb50f0b92011-05-16 16:31:42 -070059 device_info_(control_interface, dispatcher, this),
Darin Petkov887f2982011-07-14 16:10:17 -070060 modem_info_(control_interface, dispatcher, this, glib),
Chris Masone6791a432011-07-12 13:23:19 -070061 running_(false),
Chris Masone9d779932011-08-25 16:33:41 -070062 ephemeral_profile_(new EphemeralProfile(control_interface, this)),
Chris Masone2ae797d2011-08-23 20:41:00 -070063 control_interface_(control_interface),
64 glib_(glib) {
Chris Masone7aa5f902011-07-11 11:13:35 -070065 HelpRegisterDerivedString(flimflam::kActiveProfileProperty,
66 &Manager::GetActiveProfileName,
67 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070068 HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty,
69 &Manager::AvailableTechnologies,
70 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070071 store_.RegisterString(flimflam::kCheckPortalListProperty,
72 &props_.check_portal_list);
Chris Masone27c4aa52011-07-02 13:10:14 -070073 HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty,
74 &Manager::ConnectedTechnologies,
75 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070076 store_.RegisterString(flimflam::kCountryProperty, &props_.country);
Chris Masone27c4aa52011-07-02 13:10:14 -070077 HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty,
78 &Manager::DefaultTechnology,
79 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070080 HelpRegisterDerivedStrings(flimflam::kDevicesProperty,
81 &Manager::EnumerateDevices,
82 NULL);
Chris Masone88cbd5f2011-07-03 14:30:04 -070083 HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty,
84 &Manager::EnabledTechnologies,
85 NULL);
86 store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode);
87 store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url);
88 HelpRegisterDerivedString(flimflam::kStateProperty,
89 &Manager::CalculateState,
90 NULL);
Chris Masone27c4aa52011-07-02 13:10:14 -070091 HelpRegisterDerivedStrings(flimflam::kServicesProperty,
92 &Manager::EnumerateAvailableServices,
93 NULL);
94 HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty,
95 &Manager::EnumerateWatchedServices,
96 NULL);
Chris Masone3c3f6a12011-07-01 10:01:41 -070097
Chris Masone4d42df82011-07-02 17:09:39 -070098 // TODO(cmasone): Wire these up once we actually put in profile support.
Chris Masoneb925cc82011-06-22 15:39:57 -070099 // known_properties_.push_back(flimflam::kProfilesProperty);
Chris Masone2ae797d2011-08-23 20:41:00 -0700100 profiles_.push_back(new DefaultProfile(control_interface_,
Chris Masone6791a432011-07-12 13:23:19 -0700101 this,
Chris Masone2ae797d2011-08-23 20:41:00 -0700102 storage_path_,
Chris Masone6791a432011-07-12 13:23:19 -0700103 props_));
Chris Masoneb07006b2011-05-14 16:10:04 -0700104 VLOG(2) << "Manager initialized.";
Paul Stewart75897df2011-04-27 09:05:53 -0700105}
106
Chris Masone6791a432011-07-12 13:23:19 -0700107Manager::~Manager() {
Chris Masone9d779932011-08-25 16:33:41 -0700108 profiles_.clear();
Chris Masone6791a432011-07-12 13:23:19 -0700109}
Paul Stewart75897df2011-04-27 09:05:53 -0700110
mukesh agrawal8f317b62011-07-15 11:53:23 -0700111void Manager::AddDeviceToBlackList(const string &device_name) {
112 device_info_.AddDeviceToBlackList(device_name);
113}
114
Paul Stewart75897df2011-04-27 09:05:53 -0700115void Manager::Start() {
Paul Stewart0af98bf2011-05-10 17:38:08 -0700116 LOG(INFO) << "Manager started.";
Paul Stewarte6132022011-08-16 09:11:02 -0700117
Chris Masone2ae797d2011-08-23 20:41:00 -0700118 CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value();
Paul Stewarte6132022011-08-16 09:11:02 -0700119 Resolver::GetInstance()->set_path(run_path_.Append("resolv.conf"));
Chris Masone2ae797d2011-08-23 20:41:00 -0700120
121 CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value();
122
Paul Stewart75897df2011-04-27 09:05:53 -0700123 running_ = true;
Chris Masone413a3192011-05-09 17:10:05 -0700124 adaptor_->UpdateRunning();
Paul Stewart0af98bf2011-05-10 17:38:08 -0700125 device_info_.Start();
Darin Petkov887f2982011-07-14 16:10:17 -0700126 modem_info_.Start();
Paul Stewart75897df2011-04-27 09:05:53 -0700127}
128
129void Manager::Stop() {
130 running_ = false;
Chris Masone9d779932011-08-25 16:33:41 -0700131 // Persist profile, device, service information to disk.
132 vector<ProfileRefPtr>::iterator it;
133 for (it = profiles_.begin(); it != profiles_.end(); ++it) {
134 KeyFileStore storage(glib_);
135 FilePath profile_path;
136 CHECK((*it)->GetStoragePath(&profile_path));
137 storage.set_path(profile_path);
138 if (storage.Open()) {
139 (*it)->Finalize(&storage);
140 storage.Close();
141 } else {
142 LOG(ERROR) << "Could not open storage at " << profile_path.value();
143 }
144 }
145 ephemeral_profile_->Finalize(NULL);
146
Chris Masone413a3192011-05-09 17:10:05 -0700147 adaptor_->UpdateRunning();
Darin Petkov887f2982011-07-14 16:10:17 -0700148 modem_info_.Stop();
149 device_info_.Stop();
Paul Stewart75897df2011-04-27 09:05:53 -0700150}
151
Chris Masone7aa5f902011-07-11 11:13:35 -0700152const ProfileRefPtr &Manager::ActiveProfile() {
153 return profiles_.back();
154}
155
Chris Masone6791a432011-07-12 13:23:19 -0700156bool Manager::MoveToActiveProfile(const ProfileRefPtr &from,
157 const ServiceRefPtr &to_move) {
158 return ActiveProfile()->AdoptService(to_move) &&
159 from->AbandonService(to_move->UniqueName());
160}
161
Chris Masone2b105542011-06-22 10:58:09 -0700162void Manager::RegisterDevice(const DeviceRefPtr &to_manage) {
Chris Masonec1e50412011-06-07 13:04:53 -0700163 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700164 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700165 if (to_manage.get() == it->get())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700166 return;
167 }
Chris Masonec1e50412011-06-07 13:04:53 -0700168 devices_.push_back(to_manage);
Paul Stewartf1ce5d22011-05-19 13:10:20 -0700169
170 // TODO(pstew): Should check configuration
171 if (running_)
172 to_manage->Start();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700173}
174
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700175void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) {
Chris Masonec1e50412011-06-07 13:04:53 -0700176 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700177 for (it = devices_.begin(); it != devices_.end(); ++it) {
Chris Masonec1e50412011-06-07 13:04:53 -0700178 if (to_forget.get() == it->get()) {
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700179 VLOG(2) << "Deregistered device: " << to_forget->UniqueName();
180 to_forget->Stop();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700181 devices_.erase(it);
182 return;
183 }
184 }
mukesh agrawal5029c6c2011-08-25 11:12:40 -0700185 VLOG(2) << __func__ << " unknown device: " << to_forget->UniqueName();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700186}
187
Chris Masone2b105542011-06-22 10:58:09 -0700188void Manager::RegisterService(const ServiceRefPtr &to_manage) {
Chris Masone6791a432011-07-12 13:23:19 -0700189 // This should look for |to_manage| in the real profiles and, if found,
190 // do...something...to merge the meaningful state, I guess.
191
192 // If not found, add it to the ephemeral profile
193 ephemeral_profile_->AdoptService(to_manage);
194
195 // Now add to OUR list.
196 // TODO(cmasone): Keep this list sorted.
Chris Masonec1e50412011-06-07 13:04:53 -0700197 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700198 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700199 if (to_manage->UniqueName() == (*it)->UniqueName())
Chris Masone9be4a9d2011-05-16 15:44:09 -0700200 return;
201 }
Chris Masonec1e50412011-06-07 13:04:53 -0700202 services_.push_back(to_manage);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700203 SortServices();
mukesh agrawal32399322011-09-01 10:53:43 -0700204
205 vector<string> service_paths;
206 for (it = services_.begin(); it != services_.end(); ++it) {
207 service_paths.push_back((*it)->GetRpcIdentifier());
208 }
209 adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty,
210 service_paths);
Chris Masone9be4a9d2011-05-16 15:44:09 -0700211}
212
Chris Masone2b105542011-06-22 10:58:09 -0700213void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) {
Chris Masone6791a432011-07-12 13:23:19 -0700214 // If the service is in the ephemeral profile, destroy it.
215 if (!ephemeral_profile_->AbandonService(to_forget->UniqueName())) {
216 // if it's in one of the real profiles...um...I guess mark it unconnectable?
217 }
Chris Masonec1e50412011-06-07 13:04:53 -0700218 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700219 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700220 if (to_forget->UniqueName() == (*it)->UniqueName()) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700221 services_.erase(it);
Paul Stewart22aa71b2011-09-16 12:15:11 -0700222 SortServices();
Chris Masone9be4a9d2011-05-16 15:44:09 -0700223 return;
224 }
225 }
226}
227
Paul Stewart03dba0b2011-08-22 16:32:45 -0700228void Manager::UpdateService(const ServiceConstRefPtr &to_update) {
229 LOG(INFO) << "Service " << to_update->UniqueName() << " updated;"
230 << " state: " << to_update->state() << " failure: "
231 << to_update->failure();
Paul Stewart22aa71b2011-09-16 12:15:11 -0700232 SortServices();
Paul Stewart03dba0b2011-08-22 16:32:45 -0700233}
234
Paul Stewartfdd16072011-09-16 12:41:35 -0700235void Manager::FilterByTechnology(Technology::Identifier tech,
Chris Masonec1e50412011-06-07 13:04:53 -0700236 vector<DeviceRefPtr> *found) {
Chris Masone9be4a9d2011-05-16 15:44:09 -0700237 CHECK(found);
Chris Masonec1e50412011-06-07 13:04:53 -0700238 vector<DeviceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700239 for (it = devices_.begin(); it != devices_.end(); ++it) {
240 if ((*it)->TechnologyIs(tech))
241 found->push_back(*it);
242 }
243}
244
Paul Stewart22aa71b2011-09-16 12:15:11 -0700245ServiceRefPtr Manager::FindService(const string& name) {
Chris Masonec1e50412011-06-07 13:04:53 -0700246 vector<ServiceRefPtr>::iterator it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700247 for (it = services_.begin(); it != services_.end(); ++it) {
Chris Masone6791a432011-07-12 13:23:19 -0700248 if (name == (*it)->UniqueName())
Chris Masonee0dea762011-06-09 09:06:03 -0700249 return *it;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700250 }
Chris Masonee0dea762011-06-09 09:06:03 -0700251 return NULL;
Chris Masone9be4a9d2011-05-16 15:44:09 -0700252}
253
Chris Masone27c4aa52011-07-02 13:10:14 -0700254void Manager::HelpRegisterDerivedString(const string &name,
Chris Masoneb925cc82011-06-22 15:39:57 -0700255 string(Manager::*get)(void),
256 bool(Manager::*set)(const string&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700257 store_.RegisterDerivedString(
258 name,
259 StringAccessor(new CustomAccessor<Manager, string>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700260}
261
Chris Masone27c4aa52011-07-02 13:10:14 -0700262void Manager::HelpRegisterDerivedStrings(const string &name,
Chris Masonea8a2c252011-06-27 22:16:30 -0700263 Strings(Manager::*get)(void),
264 bool(Manager::*set)(const Strings&)) {
Chris Masone27c4aa52011-07-02 13:10:14 -0700265 store_.RegisterDerivedStrings(
266 name,
267 StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set)));
Chris Masoneb925cc82011-06-22 15:39:57 -0700268}
269
Paul Stewart22aa71b2011-09-16 12:15:11 -0700270void Manager::SortServices() {
271 sort(services_.begin(), services_.end(), ServiceSorter(technology_order_));
272}
273
Chris Masoneb925cc82011-06-22 15:39:57 -0700274string Manager::CalculateState() {
275 return flimflam::kStateOffline;
276}
277
278vector<string> Manager::AvailableTechnologies() {
279 return vector<string>();
280}
281
282vector<string> Manager::ConnectedTechnologies() {
283 return vector<string>();
284}
285
286string Manager::DefaultTechnology() {
287 return "";
288}
289
290vector<string> Manager::EnabledTechnologies() {
291 return vector<string>();
292}
293
Chris Masone3c3f6a12011-07-01 10:01:41 -0700294vector<string> Manager::EnumerateDevices() {
295 vector<string> device_rpc_ids;
296 for (vector<DeviceRefPtr>::const_iterator it = devices_.begin();
297 it != devices_.end();
298 ++it) {
299 device_rpc_ids.push_back((*it)->GetRpcIdentifier());
300 }
301 return device_rpc_ids;
302}
303
304vector<string> Manager::EnumerateAvailableServices() {
Chris Masone3c3f6a12011-07-01 10:01:41 -0700305 vector<string> service_rpc_ids;
306 for (vector<ServiceRefPtr>::const_iterator it = services_.begin();
307 it != services_.end();
308 ++it) {
309 service_rpc_ids.push_back((*it)->GetRpcIdentifier());
310 }
311 return service_rpc_ids;
312}
313
314vector<string> Manager::EnumerateWatchedServices() {
Chris Masone6791a432011-07-12 13:23:19 -0700315 // TODO(cmasone): Filter this list for services in appropriate states.
Chris Masone3c3f6a12011-07-01 10:01:41 -0700316 return EnumerateAvailableServices();
317}
318
Chris Masone7aa5f902011-07-11 11:13:35 -0700319string Manager::GetActiveProfileName() {
Chris Masone7df0c672011-07-15 10:24:54 -0700320 return ActiveProfile()->GetFriendlyName();
Chris Masone7aa5f902011-07-11 11:13:35 -0700321}
322
mukesh agrawal32399322011-09-01 10:53:43 -0700323// called via RPC (e.g., from ManagerDBusAdaptor)
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700324WiFiServiceRefPtr Manager::GetWifiService(const KeyValueStore &args,
325 Error *error) {
326 std::vector<DeviceRefPtr> wifi_devices;
Paul Stewart22aa71b2011-09-16 12:15:11 -0700327 FilterByTechnology(Technology::kWifi, &wifi_devices);
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700328 if (wifi_devices.empty()) {
329 error->Populate(Error::kInvalidArguments, kManagerErrorNoDevice);
330 return NULL;
331 } else {
332 WiFi *wifi = dynamic_cast<WiFi *>(wifi_devices.front().get());
333 CHECK(wifi);
334 return wifi->GetService(args, error);
335 }
336}
337
338// called via RPC (e.g., from ManagerDBusAdaptor)
Paul Stewart22aa71b2011-09-16 12:15:11 -0700339void Manager::RequestScan(const string &technology, Error *error) {
mukesh agrawal32399322011-09-01 10:53:43 -0700340 if (technology == flimflam::kTypeWifi || technology == "") {
341 vector<DeviceRefPtr> wifi_devices;
Paul Stewartfdd16072011-09-16 12:41:35 -0700342 FilterByTechnology(Technology::kWifi, &wifi_devices);
mukesh agrawal32399322011-09-01 10:53:43 -0700343
344 for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin();
345 it != wifi_devices.end();
346 ++it) {
Darin Petkovc0865312011-09-16 15:31:20 -0700347 (*it)->Scan(error);
mukesh agrawal32399322011-09-01 10:53:43 -0700348 }
349 } else {
350 // TODO(quiche): support scanning for other technologies?
351 const string kMessage = "Unrecognized technology " + technology;
352 LOG(ERROR) << kMessage;
353 CHECK(error);
354 error->Populate(Error::kInvalidArguments, kMessage);
355 }
356}
357
Paul Stewart22aa71b2011-09-16 12:15:11 -0700358string Manager::GetTechnologyOrder() {
359 vector<string> technology_names;
360 for (vector<Technology::Identifier>::iterator it = technology_order_.begin();
361 it != technology_order_.end();
362 ++it) {
363 technology_names.push_back(Technology::NameFromIdentifier(*it));
364 }
365
366 return JoinString(technology_names, ',');
367}
368
369void Manager::SetTechnologyOrder(const string &order, Error *error) {
370 vector<Technology::Identifier> new_order;
371 map<Technology::Identifier, bool> seen;
372
373 vector<string> order_parts;
374 base::SplitString(order, ',', &order_parts);
375
376 for (vector<string>::iterator it = order_parts.begin();
377 it != order_parts.end();
378 ++it) {
379 Technology::Identifier identifier = Technology::IdentifierFromName(*it);
380
381 if (identifier == Technology::kUnknown) {
382 error->Populate(Error::kInvalidArguments, *it +
383 " is an unknown technology name");
384 return;
385 }
386
387 if (ContainsKey(seen, identifier)) {
388 error->Populate(Error::kInvalidArguments, *it +
389 " is duplicated in the list");
390 return;
391 }
392 seen[identifier] = true;
393 new_order.push_back(identifier);
394 }
395
396 technology_order_ = new_order;
397 SortServices();
398}
399
Paul Stewart75897df2011-04-27 09:05:53 -0700400} // namespace shill