Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // 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 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> |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 18 | #include <base/string_split.h> |
| 19 | #include <base/string_util.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 20 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 21 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 22 | #include "shill/adaptor_interfaces.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 23 | #include "shill/control_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 24 | #include "shill/dbus_adaptor.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 25 | #include "shill/default_profile.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 26 | #include "shill/device.h" |
| 27 | #include "shill/device_info.h" |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 28 | #include "shill/ephemeral_profile.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | #include "shill/error.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 30 | #include "shill/event_dispatcher.h" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 31 | #include "shill/key_file_store.h" |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 32 | #include "shill/service_sorter.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 33 | #include "shill/profile.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 34 | #include "shill/property_accessor.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 35 | #include "shill/resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 36 | #include "shill/service.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 37 | #include "shill/wifi.h" |
| 38 | #include "shill/wifi_service.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 39 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 40 | using std::map; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 41 | using std::string; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 42 | using std::vector; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 43 | |
| 44 | namespace shill { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 45 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 46 | // static |
| 47 | const char Manager::kManagerErrorNoDevice[] = "no wifi devices available"; |
| 48 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 49 | Manager::Manager(ControlInterface *control_interface, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 50 | EventDispatcher *dispatcher, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 51 | 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 Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 58 | adaptor_(control_interface->CreateManagerAdaptor(this)), |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 59 | device_info_(control_interface, dispatcher, this), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 60 | modem_info_(control_interface, dispatcher, this, glib), |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 61 | running_(false), |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 62 | connect_profiles_to_rpc_(true), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 63 | ephemeral_profile_(new EphemeralProfile(control_interface, this)), |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 64 | control_interface_(control_interface), |
| 65 | glib_(glib) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 66 | HelpRegisterDerivedString(flimflam::kActiveProfileProperty, |
| 67 | &Manager::GetActiveProfileName, |
| 68 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 69 | HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty, |
| 70 | &Manager::AvailableTechnologies, |
| 71 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 72 | store_.RegisterString(flimflam::kCheckPortalListProperty, |
| 73 | &props_.check_portal_list); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 74 | HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty, |
| 75 | &Manager::ConnectedTechnologies, |
| 76 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 77 | store_.RegisterString(flimflam::kCountryProperty, &props_.country); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 78 | HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty, |
| 79 | &Manager::DefaultTechnology, |
| 80 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 81 | HelpRegisterDerivedStrings(flimflam::kDevicesProperty, |
| 82 | &Manager::EnumerateDevices, |
| 83 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 84 | HelpRegisterDerivedStrings(flimflam::kEnabledTechnologiesProperty, |
| 85 | &Manager::EnabledTechnologies, |
| 86 | NULL); |
| 87 | store_.RegisterBool(flimflam::kOfflineModeProperty, &props_.offline_mode); |
| 88 | store_.RegisterString(flimflam::kPortalURLProperty, &props_.portal_url); |
| 89 | HelpRegisterDerivedString(flimflam::kStateProperty, |
| 90 | &Manager::CalculateState, |
| 91 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 92 | HelpRegisterDerivedStrings(flimflam::kServicesProperty, |
| 93 | &Manager::EnumerateAvailableServices, |
| 94 | NULL); |
| 95 | HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty, |
| 96 | &Manager::EnumerateWatchedServices, |
| 97 | NULL); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 98 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 99 | // TODO(cmasone): Wire these up once we actually put in profile support. |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 100 | // known_properties_.push_back(flimflam::kProfilesProperty); |
Chris Masone | b07006b | 2011-05-14 16:10:04 -0700 | [diff] [blame] | 101 | VLOG(2) << "Manager initialized."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 104 | Manager::~Manager() { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 105 | profiles_.clear(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 106 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 107 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 108 | void Manager::AddDeviceToBlackList(const string &device_name) { |
| 109 | device_info_.AddDeviceToBlackList(device_name); |
| 110 | } |
| 111 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 112 | void Manager::Start() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 113 | LOG(INFO) << "Manager started."; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 114 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 115 | CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 116 | Resolver::GetInstance()->set_path(run_path_.Append("resolv.conf")); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 117 | |
| 118 | CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value(); |
| 119 | |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 120 | profiles_.push_back(new DefaultProfile(control_interface_, |
| 121 | this, |
| 122 | storage_path_, |
| 123 | props_)); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 124 | CHECK(profiles_[0]->InitStorage(glib_, Profile::kCreateOrOpenExisting, NULL)); |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 125 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 126 | running_ = true; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 127 | adaptor_->UpdateRunning(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 128 | device_info_.Start(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 129 | modem_info_.Start(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void Manager::Stop() { |
| 133 | running_ = false; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 134 | // Persist profile, device, service information to disk. |
| 135 | vector<ProfileRefPtr>::iterator it; |
| 136 | for (it = profiles_.begin(); it != profiles_.end(); ++it) { |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 137 | (*it)->Save(); |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 138 | } |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 139 | |
Thieu Le | 1271d68 | 2011-11-02 22:48:19 +0000 | [diff] [blame] | 140 | vector<ServiceRefPtr>::iterator services_it; |
| 141 | for (services_it = services_.begin(); services_it != services_.end(); |
| 142 | ++services_it) { |
| 143 | (*services_it)->Disconnect(); |
| 144 | } |
| 145 | |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 146 | adaptor_->UpdateRunning(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 147 | modem_info_.Stop(); |
| 148 | device_info_.Stop(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 151 | void Manager::CreateProfile(const std::string &name, Error *error) { |
| 152 | Profile::Identifier ident; |
| 153 | if (!Profile::ParseIdentifier(name, &ident)) { |
| 154 | const string kMessage = "Invalid profile name " + name; |
| 155 | LOG(ERROR) << kMessage; |
| 156 | CHECK(error); |
| 157 | error->Populate(Error::kInvalidArguments, kMessage); |
| 158 | return; |
| 159 | } |
| 160 | ProfileRefPtr profile(new Profile(control_interface_, |
| 161 | this, |
| 162 | ident, |
| 163 | user_storage_format_, |
| 164 | false)); |
| 165 | if (!profile->InitStorage(glib_, Profile::kCreateNew, error)) { |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | // Save profile data out, and then let the scoped pointer fall out of scope. |
| 170 | if (!profile->Save()) { |
| 171 | const string kMessage = "Profile name " + name + " could not be saved"; |
| 172 | LOG(ERROR) << kMessage; |
| 173 | CHECK(error); |
| 174 | error->Populate(Error::kInternalError, kMessage); |
| 175 | return; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | void Manager::PushProfile(const string &name, Error *error) { |
| 180 | |
| 181 | Profile::Identifier ident; |
| 182 | if (!Profile::ParseIdentifier(name, &ident)) { |
| 183 | const string kMessage = "Invalid profile name " + name; |
| 184 | LOG(ERROR) << kMessage; |
| 185 | CHECK(error); |
| 186 | error->Populate(Error::kInvalidArguments, kMessage); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | for (vector<ProfileRefPtr>::const_iterator it = profiles_.begin(); |
| 191 | it != profiles_.end(); |
| 192 | ++it) { |
| 193 | if ((*it)->MatchesIdentifier(ident)) { |
| 194 | const string kMessage = "Profile name " + name + " is already on stack"; |
| 195 | LOG(ERROR) << kMessage; |
| 196 | CHECK(error); |
| 197 | error->Populate(Error::kAlreadyExists, kMessage); |
| 198 | return; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | if (ident.user.empty()) { |
| 203 | // The manager will have only one machine-wide profile, and this is the |
| 204 | // DefaultProfile. This means no other profiles can be loaded that do |
| 205 | // not have a user component. |
| 206 | // TODO(pstew): This is all well and good, but WiFi autotests try to |
| 207 | // creating a default profile (by a name other than "default") in order |
| 208 | // to avoid leaving permanent side effects to devices under test. This |
| 209 | // whole thing will need to be reworked in order to allow that to happen, |
| 210 | // or the autotests (or their expectations) will need to change. |
| 211 | const string kMessage = "Cannot load non-default global profile " + name; |
| 212 | LOG(ERROR) << kMessage; |
| 213 | CHECK(error); |
| 214 | error->Populate(Error::kInvalidArguments, kMessage); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | ProfileRefPtr profile(new Profile(control_interface_, |
| 219 | this, |
| 220 | ident, |
| 221 | user_storage_format_, |
| 222 | connect_profiles_to_rpc_)); |
| 223 | if (!profile->InitStorage(glib_, Profile::kOpenExisting, error)) { |
| 224 | return; |
| 225 | } |
| 226 | |
Paul Stewart | a849a3d | 2011-11-03 05:54:09 -0700 | [diff] [blame^] | 227 | profiles_.push_back(profile); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 228 | |
| 229 | // Offer each registered Service the opportunity to join this new Profile. |
| 230 | vector<ServiceRefPtr>::iterator it; |
| 231 | for (it = services_.begin(); it != services_.end(); ++it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 232 | profile->ConfigureService(*it); |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | // TODO(pstew): Now shop the Profile contents around to Devices which |
| 236 | // can create non-visible services. |
| 237 | } |
| 238 | |
| 239 | void Manager::PopProfileInternal() { |
| 240 | CHECK(!profiles_.empty()); |
| 241 | ProfileRefPtr active_profile = profiles_.back(); |
| 242 | profiles_.pop_back(); |
| 243 | vector<ServiceRefPtr>::iterator s_it; |
| 244 | for (s_it = services_.begin(); s_it != services_.end(); ++s_it) { |
| 245 | if ((*s_it)->profile().get() == active_profile.get()) { |
| 246 | vector<ProfileRefPtr>::reverse_iterator p_it; |
| 247 | for (p_it = profiles_.rbegin(); p_it != profiles_.rend(); ++p_it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 248 | if ((*p_it)->ConfigureService(*s_it)) { |
Paul Stewart | 5dc40aa | 2011-10-28 19:43:43 -0700 | [diff] [blame] | 249 | break; |
| 250 | } |
| 251 | } |
| 252 | if (p_it == profiles_.rend()) { |
| 253 | ephemeral_profile_->AdoptService(*s_it); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void Manager::PopProfile(const std::string &name, Error *error) { |
| 260 | Profile::Identifier ident; |
| 261 | if (profiles_.empty()) { |
| 262 | const string kMessage = "Profile stack is empty"; |
| 263 | LOG(ERROR) << kMessage; |
| 264 | CHECK(error); |
| 265 | error->Populate(Error::kNotFound, kMessage); |
| 266 | return; |
| 267 | } |
| 268 | ProfileRefPtr active_profile = profiles_.back(); |
| 269 | if (!Profile::ParseIdentifier(name, &ident)) { |
| 270 | const string kMessage = "Invalid profile name " + name; |
| 271 | LOG(ERROR) << kMessage; |
| 272 | CHECK(error); |
| 273 | error->Populate(Error::kInvalidArguments, kMessage); |
| 274 | return; |
| 275 | } |
| 276 | if (!active_profile->MatchesIdentifier(ident)) { |
| 277 | const string kMessage = name + " is not the active profile"; |
| 278 | LOG(ERROR) << kMessage; |
| 279 | CHECK(error); |
| 280 | error->Populate(Error::kNotSupported, kMessage); |
| 281 | return; |
| 282 | } |
| 283 | PopProfileInternal(); |
| 284 | } |
| 285 | |
| 286 | void Manager::PopAnyProfile(Error *error) { |
| 287 | Profile::Identifier ident; |
| 288 | if (profiles_.empty()) { |
| 289 | const string kMessage = "Profile stack is empty"; |
| 290 | LOG(ERROR) << kMessage; |
| 291 | CHECK(error); |
| 292 | error->Populate(Error::kNotFound, kMessage); |
| 293 | return; |
| 294 | } |
| 295 | PopProfileInternal(); |
| 296 | } |
| 297 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 298 | const ProfileRefPtr &Manager::ActiveProfile() { |
Chris Masone | b9c0059 | 2011-10-06 13:10:39 -0700 | [diff] [blame] | 299 | DCHECK_NE(profiles_.size(), 0); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 300 | return profiles_.back(); |
| 301 | } |
| 302 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 303 | bool Manager::MoveServiceToProfile(const ServiceRefPtr &to_move, |
| 304 | const ProfileRefPtr &destination) { |
| 305 | const ProfileRefPtr from = to_move->profile(); |
| 306 | return destination->AdoptService(to_move) && |
| 307 | from->AbandonService(to_move); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 310 | void Manager::RegisterDevice(const DeviceRefPtr &to_manage) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 311 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 312 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 313 | if (to_manage.get() == it->get()) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 314 | return; |
| 315 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 316 | devices_.push_back(to_manage); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 317 | |
| 318 | // TODO(pstew): Should check configuration |
| 319 | if (running_) |
| 320 | to_manage->Start(); |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 321 | |
| 322 | // NB: Should we keep a ptr to default profile and only persist that here? |
| 323 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 324 | it != profiles_.end(); |
| 325 | ++it) { |
| 326 | (*it)->Save(); |
| 327 | } |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 328 | } |
| 329 | |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 330 | void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 331 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 332 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 333 | if (to_forget.get() == it->get()) { |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 334 | VLOG(2) << "Deregistered device: " << to_forget->UniqueName(); |
| 335 | to_forget->Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 336 | devices_.erase(it); |
| 337 | return; |
| 338 | } |
| 339 | } |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 340 | VLOG(2) << __func__ << " unknown device: " << to_forget->UniqueName(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 343 | void Manager::RegisterService(const ServiceRefPtr &to_manage) { |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 344 | VLOG(2) << __func__ << to_manage->UniqueName(); |
| 345 | |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 346 | bool configured = false; |
Chris Masone | 157aa0c | 2011-10-03 09:24:31 -0700 | [diff] [blame] | 347 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 348 | !configured && it != profiles_.end(); |
Chris Masone | 157aa0c | 2011-10-03 09:24:31 -0700 | [diff] [blame] | 349 | ++it) { |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 350 | configured = (*it)->ConfigureService(to_manage); |
Chris Masone | 157aa0c | 2011-10-03 09:24:31 -0700 | [diff] [blame] | 351 | } |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 352 | |
| 353 | // If not found, add it to the ephemeral profile |
Paul Stewart | bba6a5b | 2011-11-02 18:45:59 -0700 | [diff] [blame] | 354 | if (!configured) |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 355 | ephemeral_profile_->AdoptService(to_manage); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 356 | |
| 357 | // Now add to OUR list. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 358 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 359 | for (it = services_.begin(); it != services_.end(); ++it) { |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 360 | CHECK(to_manage->UniqueName() != (*it)->UniqueName()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 361 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 362 | services_.push_back(to_manage); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 363 | SortServices(); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 364 | |
| 365 | vector<string> service_paths; |
| 366 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 367 | service_paths.push_back((*it)->GetRpcIdentifier()); |
| 368 | } |
| 369 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, |
| 370 | service_paths); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Chris Masone | 6515aab | 2011-10-12 16:19:09 -0700 | [diff] [blame] | 373 | void Manager::DeregisterService(const ServiceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 374 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 375 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 376 | if (to_forget->UniqueName() == (*it)->UniqueName()) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 377 | services_.erase(it); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 378 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 379 | return; |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 384 | void Manager::UpdateService(const ServiceConstRefPtr &to_update) { |
| 385 | LOG(INFO) << "Service " << to_update->UniqueName() << " updated;" |
| 386 | << " state: " << to_update->state() << " failure: " |
| 387 | << to_update->failure(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 388 | SortServices(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 391 | void Manager::FilterByTechnology(Technology::Identifier tech, |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 392 | vector<DeviceRefPtr> *found) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 393 | CHECK(found); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 394 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 395 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 396 | if ((*it)->TechnologyIs(tech)) |
| 397 | found->push_back(*it); |
| 398 | } |
| 399 | } |
| 400 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 401 | ServiceRefPtr Manager::FindService(const string& name) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 402 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 403 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 404 | if (name == (*it)->UniqueName()) |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 405 | return *it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 406 | } |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 407 | return NULL; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 408 | } |
| 409 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 410 | void Manager::HelpRegisterDerivedString( |
| 411 | const string &name, |
| 412 | string(Manager::*get)(void), |
| 413 | void(Manager::*set)(const string&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 414 | store_.RegisterDerivedString( |
| 415 | name, |
| 416 | StringAccessor(new CustomAccessor<Manager, string>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 417 | } |
| 418 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 419 | void Manager::HelpRegisterDerivedStrings( |
| 420 | const string &name, |
| 421 | Strings(Manager::*get)(void), |
| 422 | void(Manager::*set)(const Strings &, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 423 | store_.RegisterDerivedStrings( |
| 424 | name, |
| 425 | StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 428 | void Manager::SortServices() { |
| 429 | sort(services_.begin(), services_.end(), ServiceSorter(technology_order_)); |
| 430 | } |
| 431 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 432 | string Manager::CalculateState() { |
| 433 | return flimflam::kStateOffline; |
| 434 | } |
| 435 | |
| 436 | vector<string> Manager::AvailableTechnologies() { |
| 437 | return vector<string>(); |
| 438 | } |
| 439 | |
| 440 | vector<string> Manager::ConnectedTechnologies() { |
| 441 | return vector<string>(); |
| 442 | } |
| 443 | |
| 444 | string Manager::DefaultTechnology() { |
| 445 | return ""; |
| 446 | } |
| 447 | |
| 448 | vector<string> Manager::EnabledTechnologies() { |
| 449 | return vector<string>(); |
| 450 | } |
| 451 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 452 | vector<string> Manager::EnumerateDevices() { |
| 453 | vector<string> device_rpc_ids; |
| 454 | for (vector<DeviceRefPtr>::const_iterator it = devices_.begin(); |
| 455 | it != devices_.end(); |
| 456 | ++it) { |
| 457 | device_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 458 | } |
| 459 | return device_rpc_ids; |
| 460 | } |
| 461 | |
| 462 | vector<string> Manager::EnumerateAvailableServices() { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 463 | vector<string> service_rpc_ids; |
| 464 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 465 | it != services_.end(); |
| 466 | ++it) { |
| 467 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 468 | } |
| 469 | return service_rpc_ids; |
| 470 | } |
| 471 | |
| 472 | vector<string> Manager::EnumerateWatchedServices() { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 473 | // TODO(cmasone): Filter this list for services in appropriate states. |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 474 | return EnumerateAvailableServices(); |
| 475 | } |
| 476 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 477 | string Manager::GetActiveProfileName() { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 478 | return ActiveProfile()->GetFriendlyName(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 479 | } |
| 480 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 481 | // called via RPC (e.g., from ManagerDBusAdaptor) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 482 | WiFiServiceRefPtr Manager::GetWifiService(const KeyValueStore &args, |
| 483 | Error *error) { |
| 484 | std::vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 485 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 486 | if (wifi_devices.empty()) { |
| 487 | error->Populate(Error::kInvalidArguments, kManagerErrorNoDevice); |
| 488 | return NULL; |
| 489 | } else { |
| 490 | WiFi *wifi = dynamic_cast<WiFi *>(wifi_devices.front().get()); |
| 491 | CHECK(wifi); |
| 492 | return wifi->GetService(args, error); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 497 | void Manager::RequestScan(const string &technology, Error *error) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 498 | if (technology == flimflam::kTypeWifi || technology == "") { |
| 499 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 500 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 501 | |
| 502 | for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin(); |
| 503 | it != wifi_devices.end(); |
| 504 | ++it) { |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 505 | (*it)->Scan(error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 506 | } |
| 507 | } else { |
| 508 | // TODO(quiche): support scanning for other technologies? |
| 509 | const string kMessage = "Unrecognized technology " + technology; |
| 510 | LOG(ERROR) << kMessage; |
| 511 | CHECK(error); |
| 512 | error->Populate(Error::kInvalidArguments, kMessage); |
| 513 | } |
| 514 | } |
| 515 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 516 | string Manager::GetTechnologyOrder() { |
| 517 | vector<string> technology_names; |
| 518 | for (vector<Technology::Identifier>::iterator it = technology_order_.begin(); |
| 519 | it != technology_order_.end(); |
| 520 | ++it) { |
| 521 | technology_names.push_back(Technology::NameFromIdentifier(*it)); |
| 522 | } |
| 523 | |
| 524 | return JoinString(technology_names, ','); |
| 525 | } |
| 526 | |
| 527 | void Manager::SetTechnologyOrder(const string &order, Error *error) { |
| 528 | vector<Technology::Identifier> new_order; |
| 529 | map<Technology::Identifier, bool> seen; |
| 530 | |
| 531 | vector<string> order_parts; |
| 532 | base::SplitString(order, ',', &order_parts); |
| 533 | |
| 534 | for (vector<string>::iterator it = order_parts.begin(); |
| 535 | it != order_parts.end(); |
| 536 | ++it) { |
| 537 | Technology::Identifier identifier = Technology::IdentifierFromName(*it); |
| 538 | |
| 539 | if (identifier == Technology::kUnknown) { |
| 540 | error->Populate(Error::kInvalidArguments, *it + |
| 541 | " is an unknown technology name"); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | if (ContainsKey(seen, identifier)) { |
| 546 | error->Populate(Error::kInvalidArguments, *it + |
| 547 | " is duplicated in the list"); |
| 548 | return; |
| 549 | } |
| 550 | seen[identifier] = true; |
| 551 | new_order.push_back(identifier); |
| 552 | } |
| 553 | |
| 554 | technology_order_ = new_order; |
| 555 | SortServices(); |
| 556 | } |
| 557 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 558 | } // namespace shill |