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" |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 30 | #include "shill/key_file_store.h" |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 31 | #include "shill/service_sorter.h" |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 32 | #include "shill/profile.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 33 | #include "shill/property_accessor.h" |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 34 | #include "shill/resolver.h" |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 35 | #include "shill/shill_event.h" |
| 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), |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 62 | ephemeral_profile_(new EphemeralProfile(control_interface, this)), |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 63 | control_interface_(control_interface), |
| 64 | glib_(glib) { |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 65 | HelpRegisterDerivedString(flimflam::kActiveProfileProperty, |
| 66 | &Manager::GetActiveProfileName, |
| 67 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 68 | HelpRegisterDerivedStrings(flimflam::kAvailableTechnologiesProperty, |
| 69 | &Manager::AvailableTechnologies, |
| 70 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 71 | store_.RegisterString(flimflam::kCheckPortalListProperty, |
| 72 | &props_.check_portal_list); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 73 | HelpRegisterDerivedStrings(flimflam::kConnectedTechnologiesProperty, |
| 74 | &Manager::ConnectedTechnologies, |
| 75 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 76 | store_.RegisterString(flimflam::kCountryProperty, &props_.country); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 77 | HelpRegisterDerivedString(flimflam::kDefaultTechnologyProperty, |
| 78 | &Manager::DefaultTechnology, |
| 79 | NULL); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 80 | HelpRegisterDerivedStrings(flimflam::kDevicesProperty, |
| 81 | &Manager::EnumerateDevices, |
| 82 | NULL); |
Chris Masone | 88cbd5f | 2011-07-03 14:30:04 -0700 | [diff] [blame] | 83 | 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 Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 91 | HelpRegisterDerivedStrings(flimflam::kServicesProperty, |
| 92 | &Manager::EnumerateAvailableServices, |
| 93 | NULL); |
| 94 | HelpRegisterDerivedStrings(flimflam::kServiceWatchListProperty, |
| 95 | &Manager::EnumerateWatchedServices, |
| 96 | NULL); |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 97 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 98 | // TODO(cmasone): Wire these up once we actually put in profile support. |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 99 | // known_properties_.push_back(flimflam::kProfilesProperty); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 100 | profiles_.push_back(new DefaultProfile(control_interface_, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 101 | this, |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 102 | storage_path_, |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 103 | props_)); |
Chris Masone | b07006b | 2011-05-14 16:10:04 -0700 | [diff] [blame] | 104 | VLOG(2) << "Manager initialized."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 107 | Manager::~Manager() { |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 108 | profiles_.clear(); |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 109 | } |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 110 | |
mukesh agrawal | 8f317b6 | 2011-07-15 11:53:23 -0700 | [diff] [blame] | 111 | void Manager::AddDeviceToBlackList(const string &device_name) { |
| 112 | device_info_.AddDeviceToBlackList(device_name); |
| 113 | } |
| 114 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 115 | void Manager::Start() { |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 116 | LOG(INFO) << "Manager started."; |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 117 | |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 118 | CHECK(file_util::CreateDirectory(run_path_)) << run_path_.value(); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 119 | Resolver::GetInstance()->set_path(run_path_.Append("resolv.conf")); |
Chris Masone | 2ae797d | 2011-08-23 20:41:00 -0700 | [diff] [blame] | 120 | |
| 121 | CHECK(file_util::CreateDirectory(storage_path_)) << storage_path_.value(); |
| 122 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 123 | running_ = true; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 124 | adaptor_->UpdateRunning(); |
Paul Stewart | 0af98bf | 2011-05-10 17:38:08 -0700 | [diff] [blame] | 125 | device_info_.Start(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 126 | modem_info_.Start(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void Manager::Stop() { |
| 130 | running_ = false; |
Chris Masone | 9d77993 | 2011-08-25 16:33:41 -0700 | [diff] [blame] | 131 | // 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 Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 147 | adaptor_->UpdateRunning(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 148 | modem_info_.Stop(); |
| 149 | device_info_.Stop(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 152 | const ProfileRefPtr &Manager::ActiveProfile() { |
| 153 | return profiles_.back(); |
| 154 | } |
| 155 | |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 156 | bool 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 Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 162 | void Manager::RegisterDevice(const DeviceRefPtr &to_manage) { |
Chris Masone | 157aa0c | 2011-10-03 09:24:31 -0700 | [diff] [blame] | 163 | // TODO(pstew): Should DefaultProfile have a list of devices, analogous to |
| 164 | // the list of services that it manages? If so, we should do a similar merge |
| 165 | // thing here. |
| 166 | |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 167 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 168 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 169 | if (to_manage.get() == it->get()) |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 170 | return; |
| 171 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 172 | devices_.push_back(to_manage); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 173 | |
| 174 | // TODO(pstew): Should check configuration |
| 175 | if (running_) |
| 176 | to_manage->Start(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 177 | } |
| 178 | |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 179 | void Manager::DeregisterDevice(const DeviceRefPtr &to_forget) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 180 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 181 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 182 | if (to_forget.get() == it->get()) { |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 183 | VLOG(2) << "Deregistered device: " << to_forget->UniqueName(); |
| 184 | to_forget->Stop(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 185 | devices_.erase(it); |
| 186 | return; |
| 187 | } |
| 188 | } |
mukesh agrawal | 5029c6c | 2011-08-25 11:12:40 -0700 | [diff] [blame] | 189 | VLOG(2) << __func__ << " unknown device: " << to_forget->UniqueName(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 192 | void Manager::RegisterService(const ServiceRefPtr &to_manage) { |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 193 | VLOG(2) << __func__ << to_manage->UniqueName(); |
| 194 | |
Chris Masone | 157aa0c | 2011-10-03 09:24:31 -0700 | [diff] [blame] | 195 | for (vector<ProfileRefPtr>::iterator it = profiles_.begin(); |
| 196 | it != profiles_.end(); |
| 197 | ++it) { |
| 198 | if ((*it)->MergeService(to_manage)) // this will merge, if possible. |
| 199 | break; |
| 200 | } |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 201 | |
| 202 | // If not found, add it to the ephemeral profile |
| 203 | ephemeral_profile_->AdoptService(to_manage); |
| 204 | |
| 205 | // Now add to OUR list. |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 206 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 207 | for (it = services_.begin(); it != services_.end(); ++it) { |
mukesh agrawal | d835b20 | 2011-10-07 15:26:47 -0700 | [diff] [blame] | 208 | CHECK(to_manage->UniqueName() != (*it)->UniqueName()); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 209 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 210 | services_.push_back(to_manage); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 211 | SortServices(); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 212 | |
| 213 | vector<string> service_paths; |
| 214 | for (it = services_.begin(); it != services_.end(); ++it) { |
| 215 | service_paths.push_back((*it)->GetRpcIdentifier()); |
| 216 | } |
| 217 | adaptor_->EmitRpcIdentifierArrayChanged(flimflam::kServicesProperty, |
| 218 | service_paths); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 221 | void Manager::DeregisterService(const ServiceConstRefPtr &to_forget) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 222 | // If the service is in the ephemeral profile, destroy it. |
| 223 | if (!ephemeral_profile_->AbandonService(to_forget->UniqueName())) { |
| 224 | // if it's in one of the real profiles...um...I guess mark it unconnectable? |
| 225 | } |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 226 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 227 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 228 | if (to_forget->UniqueName() == (*it)->UniqueName()) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 229 | services_.erase(it); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 230 | SortServices(); |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 236 | void Manager::UpdateService(const ServiceConstRefPtr &to_update) { |
| 237 | LOG(INFO) << "Service " << to_update->UniqueName() << " updated;" |
| 238 | << " state: " << to_update->state() << " failure: " |
| 239 | << to_update->failure(); |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 240 | SortServices(); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 243 | void Manager::FilterByTechnology(Technology::Identifier tech, |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 244 | vector<DeviceRefPtr> *found) { |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 245 | CHECK(found); |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 246 | vector<DeviceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 247 | for (it = devices_.begin(); it != devices_.end(); ++it) { |
| 248 | if ((*it)->TechnologyIs(tech)) |
| 249 | found->push_back(*it); |
| 250 | } |
| 251 | } |
| 252 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 253 | ServiceRefPtr Manager::FindService(const string& name) { |
Chris Masone | c1e5041 | 2011-06-07 13:04:53 -0700 | [diff] [blame] | 254 | vector<ServiceRefPtr>::iterator it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 255 | for (it = services_.begin(); it != services_.end(); ++it) { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 256 | if (name == (*it)->UniqueName()) |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 257 | return *it; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 258 | } |
Chris Masone | e0dea76 | 2011-06-09 09:06:03 -0700 | [diff] [blame] | 259 | return NULL; |
Chris Masone | 9be4a9d | 2011-05-16 15:44:09 -0700 | [diff] [blame] | 260 | } |
| 261 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 262 | void Manager::HelpRegisterDerivedString( |
| 263 | const string &name, |
| 264 | string(Manager::*get)(void), |
| 265 | void(Manager::*set)(const string&, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 266 | store_.RegisterDerivedString( |
| 267 | name, |
| 268 | StringAccessor(new CustomAccessor<Manager, string>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 269 | } |
| 270 | |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 271 | void Manager::HelpRegisterDerivedStrings( |
| 272 | const string &name, |
| 273 | Strings(Manager::*get)(void), |
| 274 | void(Manager::*set)(const Strings &, Error *)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 275 | store_.RegisterDerivedStrings( |
| 276 | name, |
| 277 | StringsAccessor(new CustomAccessor<Manager, Strings>(this, get, set))); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 280 | void Manager::SortServices() { |
| 281 | sort(services_.begin(), services_.end(), ServiceSorter(technology_order_)); |
| 282 | } |
| 283 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 284 | string Manager::CalculateState() { |
| 285 | return flimflam::kStateOffline; |
| 286 | } |
| 287 | |
| 288 | vector<string> Manager::AvailableTechnologies() { |
| 289 | return vector<string>(); |
| 290 | } |
| 291 | |
| 292 | vector<string> Manager::ConnectedTechnologies() { |
| 293 | return vector<string>(); |
| 294 | } |
| 295 | |
| 296 | string Manager::DefaultTechnology() { |
| 297 | return ""; |
| 298 | } |
| 299 | |
| 300 | vector<string> Manager::EnabledTechnologies() { |
| 301 | return vector<string>(); |
| 302 | } |
| 303 | |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 304 | vector<string> Manager::EnumerateDevices() { |
| 305 | vector<string> device_rpc_ids; |
| 306 | for (vector<DeviceRefPtr>::const_iterator it = devices_.begin(); |
| 307 | it != devices_.end(); |
| 308 | ++it) { |
| 309 | device_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 310 | } |
| 311 | return device_rpc_ids; |
| 312 | } |
| 313 | |
| 314 | vector<string> Manager::EnumerateAvailableServices() { |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 315 | vector<string> service_rpc_ids; |
| 316 | for (vector<ServiceRefPtr>::const_iterator it = services_.begin(); |
| 317 | it != services_.end(); |
| 318 | ++it) { |
| 319 | service_rpc_ids.push_back((*it)->GetRpcIdentifier()); |
| 320 | } |
| 321 | return service_rpc_ids; |
| 322 | } |
| 323 | |
| 324 | vector<string> Manager::EnumerateWatchedServices() { |
Chris Masone | 6791a43 | 2011-07-12 13:23:19 -0700 | [diff] [blame] | 325 | // TODO(cmasone): Filter this list for services in appropriate states. |
Chris Masone | 3c3f6a1 | 2011-07-01 10:01:41 -0700 | [diff] [blame] | 326 | return EnumerateAvailableServices(); |
| 327 | } |
| 328 | |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 329 | string Manager::GetActiveProfileName() { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 330 | return ActiveProfile()->GetFriendlyName(); |
Chris Masone | 7aa5f90 | 2011-07-11 11:13:35 -0700 | [diff] [blame] | 331 | } |
| 332 | |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 333 | // called via RPC (e.g., from ManagerDBusAdaptor) |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 334 | WiFiServiceRefPtr Manager::GetWifiService(const KeyValueStore &args, |
| 335 | Error *error) { |
| 336 | std::vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 337 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 338 | if (wifi_devices.empty()) { |
| 339 | error->Populate(Error::kInvalidArguments, kManagerErrorNoDevice); |
| 340 | return NULL; |
| 341 | } else { |
| 342 | WiFi *wifi = dynamic_cast<WiFi *>(wifi_devices.front().get()); |
| 343 | CHECK(wifi); |
| 344 | return wifi->GetService(args, error); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // called via RPC (e.g., from ManagerDBusAdaptor) |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 349 | void Manager::RequestScan(const string &technology, Error *error) { |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 350 | if (technology == flimflam::kTypeWifi || technology == "") { |
| 351 | vector<DeviceRefPtr> wifi_devices; |
Paul Stewart | fdd1607 | 2011-09-16 12:41:35 -0700 | [diff] [blame] | 352 | FilterByTechnology(Technology::kWifi, &wifi_devices); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 353 | |
| 354 | for (vector<DeviceRefPtr>::iterator it = wifi_devices.begin(); |
| 355 | it != wifi_devices.end(); |
| 356 | ++it) { |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 357 | (*it)->Scan(error); |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 358 | } |
| 359 | } else { |
| 360 | // TODO(quiche): support scanning for other technologies? |
| 361 | const string kMessage = "Unrecognized technology " + technology; |
| 362 | LOG(ERROR) << kMessage; |
| 363 | CHECK(error); |
| 364 | error->Populate(Error::kInvalidArguments, kMessage); |
| 365 | } |
| 366 | } |
| 367 | |
Paul Stewart | 22aa71b | 2011-09-16 12:15:11 -0700 | [diff] [blame] | 368 | string Manager::GetTechnologyOrder() { |
| 369 | vector<string> technology_names; |
| 370 | for (vector<Technology::Identifier>::iterator it = technology_order_.begin(); |
| 371 | it != technology_order_.end(); |
| 372 | ++it) { |
| 373 | technology_names.push_back(Technology::NameFromIdentifier(*it)); |
| 374 | } |
| 375 | |
| 376 | return JoinString(technology_names, ','); |
| 377 | } |
| 378 | |
| 379 | void Manager::SetTechnologyOrder(const string &order, Error *error) { |
| 380 | vector<Technology::Identifier> new_order; |
| 381 | map<Technology::Identifier, bool> seen; |
| 382 | |
| 383 | vector<string> order_parts; |
| 384 | base::SplitString(order, ',', &order_parts); |
| 385 | |
| 386 | for (vector<string>::iterator it = order_parts.begin(); |
| 387 | it != order_parts.end(); |
| 388 | ++it) { |
| 389 | Technology::Identifier identifier = Technology::IdentifierFromName(*it); |
| 390 | |
| 391 | if (identifier == Technology::kUnknown) { |
| 392 | error->Populate(Error::kInvalidArguments, *it + |
| 393 | " is an unknown technology name"); |
| 394 | return; |
| 395 | } |
| 396 | |
| 397 | if (ContainsKey(seen, identifier)) { |
| 398 | error->Populate(Error::kInvalidArguments, *it + |
| 399 | " is duplicated in the list"); |
| 400 | return; |
| 401 | } |
| 402 | seen[identifier] = true; |
| 403 | new_order.push_back(identifier); |
| 404 | } |
| 405 | |
| 406 | technology_order_ = new_order; |
| 407 | SortServices(); |
| 408 | } |
| 409 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 410 | } // namespace shill |