mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 5 | #include "shill/manager.h" |
| 6 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 7 | #include <time.h> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 9 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 10 | #include <algorithm> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 11 | #include <map> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | #include <string> |
Chris Masone | 52cd19b | 2011-06-29 17:23:04 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 15 | #include <base/file_util.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 16 | #include <base/logging.h> |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 17 | #include <base/memory/ref_counted.h> |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 18 | #include <base/stl_util-inl.h> |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 19 | #include <base/string_split.h> |
| 20 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 21 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 22 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 23 | #include "shill/adaptor_interfaces.h" |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 24 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 25 | #include "shill/control_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 26 | #include "shill/dbus_adaptor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 27 | #include "shill/default_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 28 | #include "shill/device.h" |
| 29 | #include "shill/device_info.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 30 | #include "shill/ephemeral_profile.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 31 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 32 | #include "shill/event_dispatcher.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 33 | #include "shill/key_file_store.h" |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 34 | #include "shill/service_sorter.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 35 | #include "shill/profile.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 36 | #include "shill/property_accessor.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 37 | #include "shill/resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 38 | #include "shill/service.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 39 | #include "shill/wifi.h" |
| 40 | #include "shill/wifi_service.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 41 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 42 | using std::map; |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 43 | using std::set; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 44 | using std::string; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 45 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 46 | |
| 47 | namespace shill { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 48 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 49 | // static |
| 50 | const char Manager::kManagerErrorNoDevice[] = "no wifi devices available"; |
| 51 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 52 | Manager::Manager(ControlInterface *control_interface, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 53 | EventDispatcher *dispatcher, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 54 | GLib *glib, |
| 55 | const string &run_directory, |
| 56 | const string &storage_directory, |
| 57 | const string &user_storage_format) |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 58 | : dispatcher_(dispatcher), |
| 59 | task_factory_(this), |
| 60 | run_path_(FilePath(run_directory)), |
| 61 | storage_path_(FilePath(storage_directory)), |
| 62 | user_storage_format_(user_storage_format), |
| 63 | adaptor_(control_interface->CreateManagerAdaptor(this)), |
| 64 | device_info_(control_interface, dispatcher, this), |
| 65 | modem_info_(control_interface, dispatcher, this, glib), |
| 66 | running_(false), |
| 67 | connect_profiles_to_rpc_(true), |
| 68 | ephemeral_profile_(new EphemeralProfile(control_interface, this)), |
| 69 | control_interface_(control_interface), |
| 70 | glib_(glib) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 71 | HelpRegisterDerivedString(flimflam::kActiveProfileProperty, |
| 72 | &Manager::GetActiveProfileName, |
| 73 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 74 | HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty, |
| 75 | &Manager::AvailableTechnologies, |
| 76 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 77 | store_.RegisterString(flimflam::kCheckPortalListProperty, |
| 78 | &props_.check_portal_list); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 79 | HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty, |
| 80 | &Manager::ConnectedTechnologies, |
| 81 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 82 | store_.RegisterString(flimflam::kCountryProperty, &props_.country); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 83 | HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty, |
| 84 | &Manager::DefaultTechnology, |
| 85 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 86 | HelpRegisterDerivedStrings(flimflam::kDevicesProperty, |
| 87 | &Manager::EnumerateDevices, |
| 88 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 89 | HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty, |
| 90 | &Manager::EnabledTechnologies, |
| 91 | NULL); |
| 92 | store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode); |
| 93 | store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url); |
| 94 | HelpRegisterDerivedString(flimflam::kStateProperty, |
| 95 | &Manager::CalculateState, |
| 96 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 97 | HelpRegisterDerivedStrings(flimflam::kServicesProperty, |
| 98 | &Manager::EnumerateAvailableServices, |
| 99 | NULL); |
| 100 | HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty, |
| 101 | &Manager::EnumerateWatchedServices, |
| 102 | NULL); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 103 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 104 | // TODO(cmasone): Wire these up once we actually put in profile support. |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 105 | // known_properties_.push_back(flimflam::kProfilesProperty); |
Chris Masone | b07006b | 2011-05-14 16:10:04 -0700 | [diff] [blame] | 106 | VLOG(2) << "Manager initialized."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 109 | Manager::~Manager() { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 110 | profiles_.clear(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 111 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 112 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 113 | void Manager::AddDeviceToBlackList(const string &device_name) { |
| 114 | device_info_.AddDeviceToBlackList(device_name); |
| 115 | } |
| 116 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 117 | void Manager::Start() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 118 | LOG(INFO) << "Manager started."; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 119 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 120 | CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 121 | Resolver::GetInstance()->set_path(run_path_.Append("resolv.conf")); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 122 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 123 | InitializeProfiles(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 124 | running_ = true; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 125 | adaptor_->UpdateRunning(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 126 | device_info_.Start(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 127 | modem_info_.Start(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void Manager::Stop() { |
| 131 | running_ = false; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 132 | // Persist profile, device, service information to disk. |
| 133 | vector<ProfileRefPtr>::iterator it; |
| 134 | for (it = profiles_.begin(); it != profiles_.end(); ++it) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 135 | (*it)->Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 136 | } |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 137 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 138 | vector<ServiceRefPtr>::iterator services_it; |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 139 | Error e; |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 140 | for (services_it = services_.begin(); services_it != services_.end(); |
| 141 | ++services_it) { |
mukesh agrawal | 0ed0f2e | 2011-12-05 20:36:17 +0000 | [diff] [blame] | 142 | (*services_it)->Disconnect(&e); |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 145 | adaptor_->UpdateRunning(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 146 | modem_info_.Stop(); |
| 147 | device_info_.Stop(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 150 | void Manager::InitializeProfiles() { |
| 151 | DCHECK(profiles_.empty()); |
| 152 | // The default profile must go first on the stack. |
| 153 | CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value(); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 154 | scoped_refptr<DefaultProfile> |
| 155 | default_profile(new DefaultProfile(control_interface_, |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 156 | this, |
| 157 | storage_path_, |
| 158 | props_)); |
Paul Stewart | 870523b | 2012-01-11 17:00:42 -0800 | [diff] [blame] | 159 | CHECK(default_profile->InitStorage(glib_, Profile::kCreateOrOpenExisting, |
| 160 | NULL)); |
| 161 | CHECK(default_profile->LoadManagerProperties(&props_)); |
| 162 | profiles_.push_back(default_profile.release()); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 163 | Error error; |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 164 | string path; |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 165 | for (vector<string>::iterator it = startup_profiles_.begin(); |
| 166 | it != startup_profiles_.end(); ++it) |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 167 | PushProfile(*it, &path, &error); |
Gaurav Shah | 7135476 | 2011-11-28 19:22:49 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 170 | void Manager::CreateProfile(const string &name, string *path, Error *error) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 171 | Profile::Identifier ident; |
| 172 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 173 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 174 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | ProfileRefPtr profile(new Profile(control_interface_, |
| 178 | this, |
| 179 | ident, |
| 180 | user_storage_format_, |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 181 | connect_profiles_to_rpc_)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 182 | if (!profile->InitStorage(glib_, Profile::kCreateNew, error)) { |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 183 | // |error| will have been populated by InitStorage(). |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | // Save profile data out, and then let the scoped pointer fall out of scope. |
| 188 | if (!profile->Save()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 189 | Error::PopulateAndLog(error, Error::kInternalError, |
| 190 | "Profile name " + name + " could not be saved"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 191 | return; |
| 192 | } |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 193 | |
| 194 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 197 | void Manager::PushProfile(const string &name, string *path, Error *error) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 198 | Profile::Identifier ident; |
| 199 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 200 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 201 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 206 | it != profiles_.end(); |
| 207 | ++it) { |
| 208 | if ((*it)->MatchesIdentifier(ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 209 | Error::PopulateAndLog(error, Error::kAlreadyExists, |
| 210 | "Profile name " + name + " is already on stack"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | if (ident.user.empty()) { |
| 216 | // The manager will have only one machine-wide profile, and this is the |
| 217 | // DefaultProfile. This means no other profiles can be loaded that do |
| 218 | // not have a user component. |
| 219 | // TODO(pstew): This is all well and good, but WiFi autotests try to |
| 220 | // creating a default profile (by a name other than "default") in order |
| 221 | // to avoid leaving permanent side effects to devices under test. This |
| 222 | // whole thing will need to be reworked in order to allow that to happen, |
| 223 | // or the autotests (or their expectations) will need to change. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 224 | // crosbug.com/24461 |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 225 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 226 | "Cannot load non-default global profile " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 227 | return; |
| 228 | } |
| 229 | |
| 230 | ProfileRefPtr profile(new Profile(control_interface_, |
| 231 | this, |
| 232 | ident, |
| 233 | user_storage_format_, |
| 234 | connect_profiles_to_rpc_)); |
| 235 | if (!profile->InitStorage(glib_, Profile::kOpenExisting, error)) { |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 236 | // |error| will have been populated by InitStorage(). |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 237 | return; |
| 238 | } |
| 239 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame] | 240 | profiles_.push_back(profile); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 241 | |
| 242 | // Offer each registered Service the opportunity to join this new Profile. |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 243 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 244 | it != services_.end(); ++it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 245 | profile->ConfigureService(*it); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 248 | // Shop the Profile contents around to Devices which can create |
| 249 | // non-visible services. |
| 250 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 251 | it != devices_.end(); ++it) { |
| 252 | profile->ConfigureDevice(*it); |
| 253 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 254 | |
Paul Stewart | 19c871d | 2011-12-15 16:10:13 -0800 | [diff] [blame] | 255 | *path = profile->GetRpcIdentifier(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 256 | SortServices(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void Manager::PopProfileInternal() { |
| 260 | CHECK(!profiles_.empty()); |
| 261 | ProfileRefPtr active_profile = profiles_.back(); |
| 262 | profiles_.pop_back(); |
| 263 | vector<ServiceRefPtr>::iterator s_it; |
| 264 | for (s_it = services_.begin(); s_it != services_.end(); ++s_it) { |
| 265 | if ((*s_it)->profile().get() == active_profile.get()) { |
| 266 | vector<ProfileRefPtr>::reverse_iterator p_it; |
| 267 | for (p_it = profiles_.rbegin(); p_it != profiles_.rend(); ++p_it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 268 | if ((*p_it)->ConfigureService(*s_it)) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 269 | break; |
| 270 | } |
| 271 | } |
| 272 | if (p_it == profiles_.rend()) { |
| 273 | ephemeral_profile_->AdoptService(*s_it); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 274 | (*s_it)->Unload(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 278 | SortServices(); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 281 | void Manager::PopProfile(const string &name, Error *error) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 282 | Profile::Identifier ident; |
| 283 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 284 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 285 | return; |
| 286 | } |
| 287 | ProfileRefPtr active_profile = profiles_.back(); |
| 288 | if (!Profile::ParseIdentifier(name, &ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 289 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 290 | "Invalid profile name " + name); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 291 | return; |
| 292 | } |
| 293 | if (!active_profile->MatchesIdentifier(ident)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 294 | Error::PopulateAndLog(error, Error::kNotSupported, |
| 295 | name + " is not the active profile"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 296 | return; |
| 297 | } |
| 298 | PopProfileInternal(); |
| 299 | } |
| 300 | |
| 301 | void Manager::PopAnyProfile(Error *error) { |
| 302 | Profile::Identifier ident; |
| 303 | if (profiles_.empty()) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 304 | Error::PopulateAndLog(error, Error::kNotFound, "Profile stack is empty"); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | PopProfileInternal(); |
| 308 | } |
| 309 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 310 | const ProfileRefPtr &Manager::ActiveProfile() { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 311 | DCHECK_NE(profiles_.size(), 0); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 312 | return profiles_.back(); |
| 313 | } |
| 314 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 315 | bool Manager::MoveServiceToProfile(const ServiceRefPtr &to_move, |
| 316 | const ProfileRefPtr &destination) { |
| 317 | const ProfileRefPtr from = to_move->profile(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 318 | VLOG(2) << "Moving service " |
| 319 | << to_move->UniqueName() |
| 320 | << " to profile " |
| 321 | << destination->GetFriendlyName() |
| 322 | << " from " |
| 323 | << from->GetFriendlyName(); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 324 | return destination->AdoptService(to_move) && |
| 325 | from->AbandonService(to_move); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Paul Stewart | 1b1a7f2 | 2012-01-06 16:24:06 -0800 | [diff] [blame] | 328 | void Manager::SetProfileForService(const ServiceRefPtr &to_set, |
| 329 | const string &profile_rpcid, |
| 330 | Error *error) { |
| 331 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 332 | it != profiles_.end(); |
| 333 | ++it) { |
| 334 | if (profile_rpcid == (*it)->GetRpcIdentifier()) { |
| 335 | if (to_set->profile().get() == it->get()) { |
| 336 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 337 | "Service is already connected to this profile"); |
| 338 | } else if (!MoveServiceToProfile(to_set, *it)) { |
| 339 | Error::PopulateAndLog(error, Error::kInternalError, |
| 340 | "Unable to move service to profile"); |
| 341 | } |
| 342 | return; |
| 343 | } |
| 344 | } |
| 345 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 346 | "Unknown Profile requested for Service"); |
| 347 | } |
| 348 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 349 | void Manager::RegisterDevice(const DeviceRefPtr &to_manage) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 350 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 351 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 352 | if (to_manage.get() == it->get()) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 353 | return; |
| 354 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 355 | devices_.push_back(to_manage); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 356 | |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 357 | // We are applying device properties from the DefaultProfile, and adding |
| 358 | // the union of hidden services in all loaded profiles to the device. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 359 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 360 | it != profiles_.end(); |
| 361 | ++it) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 362 | // Load device configuration, if any exists, as well as hidden services. |
| 363 | (*it)->ConfigureDevice(to_manage); |
| 364 | |
| 365 | // Currently the only profile for which "Save" is implemented is the |
| 366 | // DefaultProfile. It iterates over all Devices and stores their state. |
| 367 | // We perform the Save now in case the device we have just registered |
| 368 | // is new and needs to be added to the stored DefaultProfile. |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 369 | (*it)->Save(); |
| 370 | } |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 371 | |
| 372 | // In normal usage, running_ will always be true when we are here, however |
| 373 | // unit tests sometimes do things in otherwise invalid states. |
| 374 | if (running_ && to_manage->powered()) |
| 375 | to_manage->Start(); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 376 | |
| 377 | Error error; |
| 378 | adaptor_->EmitStringsChanged(flimflam::kAvailableTechnologiesProperty, |
| 379 | AvailableTechnologies(&error)); |
| 380 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 381 | EnabledTechnologies(&error)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 382 | } |
| 383 | |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 384 | void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 385 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 386 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 387 | if (to_forget.get() == it->get()) { |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 388 | VLOG(2) << "Deregistered device: " << to_forget->UniqueName(); |
| 389 | to_forget->Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 390 | devices_.erase(it); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 391 | Error error; |
| 392 | adaptor_->EmitStringsChanged(flimflam::kAvailableTechnologiesProperty, |
| 393 | AvailableTechnologies(&error)); |
| 394 | adaptor_->EmitStringsChanged(flimflam::kEnabledTechnologiesProperty, |
| 395 | EnabledTechnologies(&error)); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 396 | return; |
| 397 | } |
| 398 | } |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 399 | VLOG(2) << __func__ << " unknown device: " << to_forget->UniqueName(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 400 | } |
| 401 | |
mukesh agrawal | 4eb4d78 | 2011-12-05 17:34:37 +0000 | [diff] [blame] | 402 | bool Manager::HasService(const ServiceRefPtr &service) { |
| 403 | vector<ServiceRefPtr>::iterator it; |
| 404 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 405 | if ((*it)->UniqueName() == service->UniqueName()) |
| 406 | return true; |
| 407 | } |
| 408 | return false; |
| 409 | } |
| 410 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 411 | void Manager::RegisterService(const ServiceRefPtr &to_manage) { |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 412 | VLOG(2) << "In " << __func__ << "(): Registering service " |
| 413 | << to_manage->UniqueName(); |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 414 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 415 | bool configured = false; |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 416 | for (vector<ProfileRefPtr>::reverse_iterator it = profiles_.rbegin(); |
| 417 | !configured && it != profiles_.rend(); |
Chris Masone | 157aa0c | 2011-10-03 09:24:31 -0700 | [diff] [blame] | 418 | ++it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 419 | configured = (*it)->ConfigureService(to_manage); |
Chris Masone | 157aa0c | 2011-10-03 09:24:31 -0700 | [diff] [blame] | 420 | } |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 421 | |
| 422 | // If not found, add it to the ephemeral profile |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 423 | if (!configured) |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 424 | ephemeral_profile_->AdoptService(to_manage); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 425 | |
| 426 | // Now add to OUR list. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 427 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 428 | for (it = services_.begin(); it != services_.end(); ++it) { |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 429 | CHECK(to_manage->UniqueName() != (*it)->UniqueName()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 430 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 431 | services_.push_back(to_manage); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 432 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 435 | void Manager::DeregisterService(const ServiceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 436 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 437 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 438 | if (to_forget->UniqueName() == (*it)->UniqueName()) { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 439 | DCHECK(!(*it)->connection()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 440 | services_.erase(it); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 441 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 442 | return; |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 447 | void Manager::UpdateService(const ServiceRefPtr &to_update) { |
| 448 | CHECK(to_update); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 449 | LOG(INFO) << "Service " << to_update->UniqueName() << " updated;" |
Gaurav Shah | c6d6c72 | 2011-11-17 18:59:39 -0800 | [diff] [blame] | 450 | << " state: " << Service::ConnectStateToString(to_update->state()) |
| 451 | << " failure: " |
| 452 | << Service::ConnectFailureToString(to_update->failure()); |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 453 | VLOG(2) << "IsConnected(): " << to_update->IsConnected(); |
| 454 | VLOG(2) << "IsConnecting(): " << to_update->IsConnecting(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 455 | if (to_update->IsConnected()) { |
mukesh agrawal | 00917ce | 2011-11-22 23:56:55 +0000 | [diff] [blame] | 456 | to_update->MakeFavorite(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 457 | if (to_update->profile().get() == ephemeral_profile_.get()) { |
| 458 | if (profiles_.empty()) { |
| 459 | LOG(ERROR) << "Cannot assign profile to service: no profiles exist!"; |
| 460 | } else { |
| 461 | MoveServiceToProfile(to_update, profiles_.back()); |
| 462 | } |
| 463 | } |
| 464 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 465 | SortServices(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 468 | void Manager::FilterByTechnology(Technology::Identifier tech, |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 469 | vector<DeviceRefPtr> *found) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 470 | CHECK(found); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 471 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 472 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 473 | if ((*it)->TechnologyIs(tech)) |
| 474 | found->push_back(*it); |
| 475 | } |
| 476 | } |
| 477 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 478 | ServiceRefPtr Manager::FindService(const string& name) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 479 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 480 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 481 | if (name == (*it)->UniqueName()) |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 482 | return *it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 483 | } |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 484 | return NULL; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 485 | } |
| 486 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 487 | void Manager::HelpRegisterDerivedString( |
| 488 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 489 | string(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 490 | void(Manager::*set)(const string&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 491 | store_.RegisterDerivedString( |
| 492 | name, |
| 493 | StringAccessor(new CustomAccessor<Manager, string>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 494 | } |
| 495 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 496 | void Manager::HelpRegisterDerivedStrings( |
| 497 | const string &name, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 498 | Strings(Manager::*get)(Error *), |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 499 | void(Manager::*set)(const Strings &, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 500 | store_.RegisterDerivedStrings( |
| 501 | name, |
| 502 | StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 505 | void Manager::SortServices() { |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 506 | VLOG(4) << "In " << __func__; |
| 507 | ConnectionRefPtr default_connection; |
| 508 | if (!services_.empty()) { |
| 509 | // Keep track of the connection that was last considered default. |
| 510 | default_connection = services_[0]->connection(); |
| 511 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 512 | sort(services_.begin(), services_.end(), ServiceSorter(technology_order_)); |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 513 | |
| 514 | vector<string> service_paths; |
| 515 | vector<ServiceRefPtr>::iterator it; |
| 516 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 517 | if ((*it)->IsVisible()) { |
| 518 | service_paths.push_back((*it)->GetRpcIdentifier()); |
| 519 | } |
| 520 | } |
| 521 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, |
| 522 | service_paths); |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 523 | |
| 524 | Error error; |
| 525 | adaptor_->EmitStringsChanged(flimflam::kConnectedTechnologiesProperty, |
| 526 | ConnectedTechnologies(&error)); |
| 527 | adaptor_->EmitStringChanged(flimflam::kDefaultTechnologyProperty, |
| 528 | DefaultTechnology(&error)); |
Paul Stewart | c1dec4d | 2011-12-08 15:25:28 -0800 | [diff] [blame] | 529 | |
| 530 | if (!services_.empty()) { |
| 531 | if (default_connection.get() && |
| 532 | (services_[0]->connection().get() != default_connection.get())) { |
| 533 | default_connection->SetIsDefault(false); |
| 534 | } |
| 535 | if (services_[0]->connection().get()) { |
| 536 | services_[0]->connection()->SetIsDefault(true); |
| 537 | } |
| 538 | } |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 539 | |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 540 | AutoConnect(); |
| 541 | } |
| 542 | |
| 543 | void Manager::AutoConnect() { |
| 544 | // We might be called in the middle of another request (e.g., as a |
| 545 | // consequence of Service::SetState calling UpdateService). To avoid |
| 546 | // re-entrancy issues in dbus-c++, defer to the event loop. |
| 547 | dispatcher_->PostTask( |
| 548 | task_factory_.NewRunnableMethod(&Manager::AutoConnectTask)); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | void Manager::AutoConnectTask() { |
| 553 | if (services_.empty()) { |
| 554 | LOG(INFO) << "No services."; |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | if (VLOG_IS_ON(4)) { |
| 559 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 560 | it != services_.end(); ++it) { |
| 561 | VLOG(4) << "Sorted service list: "; |
| 562 | VLOG(4) << "Service " << (*it)->friendly_name() |
| 563 | << " IsConnected: " << (*it)->IsConnected() |
| 564 | << " IsConnecting: " << (*it)->IsConnecting() |
| 565 | << " IsFailed: " << (*it)->IsFailed() |
| 566 | << " connectable: " << (*it)->connectable() |
| 567 | << " auto_connect: " << (*it)->auto_connect() |
| 568 | << " favorite: " << (*it)->favorite() |
| 569 | << " priority: " << (*it)->priority() |
| 570 | << " security_level: " << (*it)->security_level() |
| 571 | << " strength: " << (*it)->strength() |
| 572 | << " UniqueName: " << (*it)->UniqueName(); |
| 573 | } |
| 574 | } |
| 575 | |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 576 | // Perform auto-connect. |
mukesh agrawal | 8a3188d | 2011-12-01 20:56:44 +0000 | [diff] [blame] | 577 | for (vector<ServiceRefPtr>::iterator it = services_.begin(); |
| 578 | it != services_.end(); ++it) { |
| 579 | if ((*it)->auto_connect()) { |
| 580 | LOG(INFO) << "Initiating connect to " << (*it)->friendly_name() << "."; |
| 581 | (*it)->AutoConnect(); |
Paul Stewart | 3d9bcf5 | 2011-12-12 15:02:22 -0800 | [diff] [blame] | 582 | } |
| 583 | } |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 586 | string Manager::CalculateState(Error */*error*/) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 587 | return flimflam::kStateOffline; |
| 588 | } |
| 589 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 590 | vector<string> Manager::AvailableTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 591 | set<string> unique_technologies; |
| 592 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 593 | it != devices_.end(); ++it) { |
| 594 | unique_technologies.insert( |
| 595 | Technology::NameFromIdentifier((*it)->technology())); |
| 596 | } |
| 597 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 598 | } |
| 599 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 600 | vector<string> Manager::ConnectedTechnologies(Error */*error*/) { |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 601 | set<string> unique_technologies; |
| 602 | for (vector<DeviceRefPtr>::iterator it = devices_.begin(); |
| 603 | it != devices_.end(); ++it) { |
| 604 | if ((*it)->IsConnected()) |
| 605 | unique_technologies.insert( |
| 606 | Technology::NameFromIdentifier((*it)->technology())); |
| 607 | } |
| 608 | return vector<string>(unique_technologies.begin(), unique_technologies.end()); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 611 | string Manager::DefaultTechnology(Error *error) { |
| 612 | return (!services_.empty() && services_[0]->IsConnected()) ? |
| 613 | services_[0]->GetTechnologyString(error) : ""; |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Gaurav Shah | 435de2c | 2011-11-17 19:01:07 -0800 | [diff] [blame] | 616 | vector<string> Manager::EnabledTechnologies(Error *error) { |
| 617 | // TODO(gauravsh): This must be wired up to the RPC interface to handle |
| 618 | // enabled/disabled devices as set by the user. crosbug.com/23319 |
| 619 | return AvailableTechnologies(error); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 620 | } |
| 621 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 622 | vector<string> Manager::EnumerateDevices(Error */*error*/) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 623 | vector<string> device_rpc_ids; |
| 624 | for (vector<DeviceRefPtr>::const_iterator it = devices_.begin(); |
| 625 | it != devices_.end(); |
| 626 | ++it) { |
| 627 | device_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 628 | } |
| 629 | return device_rpc_ids; |
| 630 | } |
| 631 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 632 | vector<string> Manager::EnumerateAvailableServices(Error */*error*/) { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 633 | vector<string> service_rpc_ids; |
| 634 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 635 | it != services_.end(); |
| 636 | ++it) { |
| 637 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 638 | } |
| 639 | return service_rpc_ids; |
| 640 | } |
| 641 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 642 | vector<string> Manager::EnumerateWatchedServices(Error *error) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 643 | // TODO(cmasone): Filter this list for services in appropriate states. |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 644 | return EnumerateAvailableServices(error); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 647 | string Manager::GetActiveProfileName(Error */*error*/) { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 648 | return ActiveProfile()->GetFriendlyName(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 649 | } |
| 650 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 651 | // called via RPC (e.g., from ManagerDBusAdaptor) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 652 | WiFiServiceRefPtr Manager::GetWifiService(const KeyValueStore &args, |
| 653 | Error *error) { |
Paul Stewart | a41e38d | 2011-11-11 07:47:29 -0800 | [diff] [blame] | 654 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 655 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 656 | if (wifi_devices.empty()) { |
| 657 | error->Populate(Error::kInvalidArguments, kManagerErrorNoDevice); |
| 658 | return NULL; |
| 659 | } else { |
| 660 | WiFi *wifi = dynamic_cast<WiFi *>(wifi_devices.front().get()); |
| 661 | CHECK(wifi); |
| 662 | return wifi->GetService(args, error); |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 667 | void Manager::RequestScan(const string &technology, Error *error) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 668 | if (technology == flimflam::kTypeWifi || technology == "") { |
| 669 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 670 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 671 | |
| 672 | for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin(); |
| 673 | it != wifi_devices.end(); |
| 674 | ++it) { |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 675 | (*it)->Scan(error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 676 | } |
| 677 | } else { |
| 678 | // TODO(quiche): support scanning for other technologies? |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 679 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 680 | "Unrecognized technology " + technology); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 684 | string Manager::GetTechnologyOrder() { |
| 685 | vector<string> technology_names; |
| 686 | for (vector<Technology::Identifier>::iterator it = technology_order_.begin(); |
| 687 | it != technology_order_.end(); |
| 688 | ++it) { |
| 689 | technology_names.push_back(Technology::NameFromIdentifier(*it)); |
| 690 | } |
| 691 | |
| 692 | return JoinString(technology_names, ','); |
| 693 | } |
| 694 | |
| 695 | void Manager::SetTechnologyOrder(const string &order, Error *error) { |
| 696 | vector<Technology::Identifier> new_order; |
| 697 | map<Technology::Identifier, bool> seen; |
| 698 | |
| 699 | vector<string> order_parts; |
| 700 | base::SplitString(order, ',', &order_parts); |
| 701 | |
| 702 | for (vector<string>::iterator it = order_parts.begin(); |
| 703 | it != order_parts.end(); |
| 704 | ++it) { |
| 705 | Technology::Identifier identifier = Technology::IdentifierFromName(*it); |
| 706 | |
| 707 | if (identifier == Technology::kUnknown) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 708 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 709 | *it + " is an unknown technology name"); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 710 | return; |
| 711 | } |
| 712 | |
| 713 | if (ContainsKey(seen, identifier)) { |
Paul Stewart | be00517 | 2011-11-02 18:10:29 -0700 | [diff] [blame] | 714 | Error::PopulateAndLog(error, Error::kInvalidArguments, |
| 715 | *it + " is duplicated in the list"); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 716 | return; |
| 717 | } |
| 718 | seen[identifier] = true; |
| 719 | new_order.push_back(identifier); |
| 720 | } |
| 721 | |
| 722 | technology_order_ = new_order; |
| 723 | SortServices(); |
| 724 | } |
| 725 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 726 | } // namespace shill |