Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "shill/wimax_provider.h" |
| 6 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 7 | #include <algorithm> |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 8 | #include <set> |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 9 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 10 | #include <base/bind.h> |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 11 | #include <base/string_util.h> |
Ben Chan | b879d14 | 2012-05-15 11:10:32 -0700 | [diff] [blame] | 12 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 13 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 14 | #include "shill/error.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 15 | #include "shill/key_value_store.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 16 | #include "shill/logging.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 17 | #include "shill/manager.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 18 | #include "shill/profile.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 19 | #include "shill/proxy_factory.h" |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 20 | #include "shill/store_interface.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 21 | #include "shill/wimax.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 22 | #include "shill/wimax_manager_proxy_interface.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 23 | #include "shill/wimax_service.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 24 | |
| 25 | using base::Bind; |
| 26 | using base::Unretained; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 27 | using std::find; |
| 28 | using std::map; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 29 | using std::set; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 30 | using std::string; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 31 | |
| 32 | namespace shill { |
| 33 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 34 | WiMaxProvider::WiMaxProvider(ControlInterface *control, |
| 35 | EventDispatcher *dispatcher, |
| 36 | Metrics *metrics, |
| 37 | Manager *manager) |
| 38 | : control_(control), |
| 39 | dispatcher_(dispatcher), |
| 40 | metrics_(metrics), |
| 41 | manager_(manager), |
| 42 | proxy_factory_(ProxyFactory::GetInstance()) {} |
| 43 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 44 | WiMaxProvider::~WiMaxProvider() {} |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 45 | |
| 46 | void WiMaxProvider::Start() { |
| 47 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 48 | if (!on_wimax_manager_appear_.IsCancelled()) { |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 49 | return; |
| 50 | } |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 51 | // Registers a watcher for the WiMaxManager service. This provider will |
| 52 | // connect to it if/when the OnWiMaxManagerAppear callback is invoked. |
| 53 | on_wimax_manager_appear_.Reset( |
| 54 | Bind(&WiMaxProvider::OnWiMaxManagerAppear, Unretained(this))); |
| 55 | on_wimax_manager_vanish_.Reset( |
| 56 | Bind(&WiMaxProvider::DisconnectFromWiMaxManager, Unretained(this))); |
| 57 | manager_->dbus_manager()->WatchName( |
| 58 | wimax_manager::kWiMaxManagerServiceName, |
| 59 | on_wimax_manager_appear_.callback(), |
| 60 | on_wimax_manager_vanish_.callback()); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void WiMaxProvider::Stop() { |
| 64 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 65 | // TODO(petkov): Deregister the watcher from DBusManager to avoid potential |
Paul Stewart | ee6b3d7 | 2013-07-12 16:07:51 -0700 | [diff] [blame] | 66 | // memory leaks (crbug.com/214476). |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 67 | on_wimax_manager_appear_.Cancel(); |
| 68 | on_wimax_manager_vanish_.Cancel(); |
| 69 | DisconnectFromWiMaxManager(); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 70 | DestroyAllServices(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Darin Petkov | b501ad2 | 2012-07-03 12:50:52 +0200 | [diff] [blame] | 73 | void WiMaxProvider::ConnectToWiMaxManager() { |
| 74 | DCHECK(!wimax_manager_proxy_.get()); |
| 75 | LOG(INFO) << "Connecting to WiMaxManager."; |
| 76 | wimax_manager_proxy_.reset(proxy_factory_->CreateWiMaxManagerProxy()); |
| 77 | wimax_manager_proxy_->set_devices_changed_callback( |
| 78 | Bind(&WiMaxProvider::OnDevicesChanged, Unretained(this))); |
| 79 | Error error; |
| 80 | OnDevicesChanged(wimax_manager_proxy_->Devices(&error)); |
| 81 | } |
| 82 | |
| 83 | void WiMaxProvider::DisconnectFromWiMaxManager() { |
| 84 | SLOG(WiMax, 2) << __func__; |
| 85 | if (!wimax_manager_proxy_.get()) { |
| 86 | return; |
| 87 | } |
| 88 | LOG(INFO) << "Disconnecting from WiMaxManager."; |
| 89 | wimax_manager_proxy_.reset(); |
| 90 | OnDevicesChanged(RpcIdentifiers()); |
| 91 | } |
| 92 | |
| 93 | void WiMaxProvider::OnWiMaxManagerAppear(const string &owner) { |
| 94 | SLOG(WiMax, 2) << __func__ << "(" << owner << ")"; |
| 95 | DisconnectFromWiMaxManager(); |
| 96 | ConnectToWiMaxManager(); |
| 97 | } |
| 98 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 99 | void WiMaxProvider::OnDeviceInfoAvailable(const string &link_name) { |
| 100 | SLOG(WiMax, 2) << __func__ << "(" << link_name << ")"; |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 101 | map<string, RpcIdentifier>::const_iterator find_it = |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 102 | pending_devices_.find(link_name); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 103 | if (find_it != pending_devices_.end()) { |
| 104 | RpcIdentifier path = find_it->second; |
| 105 | CreateDevice(link_name, path); |
| 106 | } |
| 107 | } |
| 108 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 109 | void WiMaxProvider::OnNetworksChanged() { |
| 110 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 111 | // Collects a set of live networks from all devices. |
| 112 | set<RpcIdentifier> live_networks; |
| 113 | for (map<string, WiMaxRefPtr>::const_iterator it = devices_.begin(); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 114 | it != devices_.end(); ++it) { |
| 115 | const set<RpcIdentifier> &networks = it->second->networks(); |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 116 | live_networks.insert(networks.begin(), networks.end()); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 117 | } |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 118 | // Removes dead networks from |networks_|. |
| 119 | for (map<RpcIdentifier, NetworkInfo>::iterator it = networks_.begin(); |
| 120 | it != networks_.end(); ) { |
Darin Petkov | b96a451 | 2012-06-04 11:02:49 +0200 | [diff] [blame] | 121 | const RpcIdentifier &path = it->first; |
| 122 | if (ContainsKey(live_networks, path)) { |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 123 | ++it; |
| 124 | } else { |
Darin Petkov | b96a451 | 2012-06-04 11:02:49 +0200 | [diff] [blame] | 125 | LOG(INFO) << "WiMAX network disappeared: " << path; |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 126 | networks_.erase(it++); |
| 127 | } |
| 128 | } |
| 129 | // Retrieves network info into |networks_| for the live networks. |
| 130 | for (set<RpcIdentifier>::const_iterator it = live_networks.begin(); |
| 131 | it != live_networks.end(); ++it) { |
| 132 | RetrieveNetworkInfo(*it); |
| 133 | } |
| 134 | // Stops dead and starts live services based on the current |networks_|. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 135 | StopDeadServices(); |
| 136 | StartLiveServices(); |
| 137 | } |
| 138 | |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 139 | bool WiMaxProvider::OnServiceUnloaded(const WiMaxServiceRefPtr &service) { |
| 140 | SLOG(WiMax, 2) << __func__ << "(" << service->GetStorageIdentifier() << ")"; |
| 141 | if (service->is_default()) { |
| 142 | return false; |
| 143 | } |
| 144 | // Removes the service from the managed service set. The service will be |
| 145 | // deregistered from Manager when we release ownership by returning true. |
| 146 | services_.erase(service->GetStorageIdentifier()); |
| 147 | return true; |
| 148 | } |
| 149 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 150 | // static |
| 151 | bool WiMaxProvider::GetServiceParametersFromArgs(const KeyValueStore &args, |
| 152 | WiMaxNetworkId *id_ptr, |
| 153 | string *name_ptr, |
| 154 | Error *error) { |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 155 | WiMaxNetworkId id = args.LookupString(WiMaxService::kNetworkIdProperty, ""); |
| 156 | if (id.empty()) { |
| 157 | Error::PopulateAndLog( |
| 158 | error, Error::kInvalidArguments, "Missing WiMAX network id."); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 159 | return false; |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 160 | } |
| 161 | string name = args.LookupString(flimflam::kNameProperty, ""); |
| 162 | if (name.empty()) { |
| 163 | Error::PopulateAndLog( |
| 164 | error, Error::kInvalidArguments, "Missing WiMAX service name."); |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 165 | return false; |
| 166 | } |
| 167 | |
| 168 | *id_ptr = id; |
| 169 | *name_ptr = name; |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | ServiceRefPtr WiMaxProvider::GetService(const KeyValueStore &args, |
| 175 | Error *error) { |
| 176 | SLOG(WiMax, 2) << __func__; |
| 177 | CHECK_EQ(flimflam::kTypeWimax, args.GetString(flimflam::kTypeProperty)); |
| 178 | WiMaxNetworkId id; |
| 179 | string name; |
| 180 | if (!GetServiceParametersFromArgs(args, &id, &name, error)) { |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 183 | WiMaxServiceRefPtr service = GetUniqueService(id, name); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 184 | CHECK(service); |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 185 | // Starts the service if there's a matching live network. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 186 | StartLiveServices(); |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame] | 187 | return service; |
| 188 | } |
| 189 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 190 | ServiceRefPtr WiMaxProvider::FindSimilarService(const KeyValueStore &args, |
| 191 | Error *error) const { |
| 192 | SLOG(WiMax, 2) << __func__; |
| 193 | CHECK_EQ(flimflam::kTypeWimax, args.GetString(flimflam::kTypeProperty)); |
| 194 | WiMaxNetworkId id; |
| 195 | string name; |
| 196 | if (!GetServiceParametersFromArgs(args, &id, &name, error)) { |
| 197 | return NULL; |
| 198 | } |
| 199 | string storage_id = WiMaxService::CreateStorageIdentifier(id, name); |
| 200 | WiMaxServiceRefPtr service = FindService(storage_id); |
| 201 | if (!service) { |
| 202 | error->Populate(Error::kNotFound, "Matching service was not found"); |
| 203 | } |
| 204 | return service; |
| 205 | } |
| 206 | |
| 207 | ServiceRefPtr WiMaxProvider::CreateTemporaryService(const KeyValueStore &args, |
| 208 | Error *error) { |
| 209 | SLOG(WiMax, 2) << __func__; |
| 210 | CHECK_EQ(flimflam::kTypeWimax, args.GetString(flimflam::kTypeProperty)); |
| 211 | WiMaxNetworkId id; |
| 212 | string name; |
| 213 | if (!GetServiceParametersFromArgs(args, &id, &name, error)) { |
| 214 | return NULL; |
| 215 | } |
| 216 | return CreateService(id, name); |
| 217 | } |
| 218 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 219 | void WiMaxProvider::CreateServicesFromProfile(const ProfileRefPtr &profile) { |
| 220 | SLOG(WiMax, 2) << __func__; |
| 221 | bool created = false; |
| 222 | const StoreInterface *storage = profile->GetConstStorage(); |
| 223 | set<string> groups = storage->GetGroupsWithKey(Service::kStorageType); |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 224 | for (set<string>::const_iterator it = groups.begin(); |
| 225 | it != groups.end(); ++it) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 226 | const string &storage_id = *it; |
| 227 | string type; |
| 228 | if (!storage->GetString(storage_id, Service::kStorageType, &type) || |
| 229 | type != Technology::NameFromIdentifier(Technology::kWiMax)) { |
| 230 | continue; |
| 231 | } |
| 232 | if (FindService(storage_id)) { |
| 233 | continue; |
| 234 | } |
| 235 | WiMaxNetworkId id; |
| 236 | if (!storage->GetString(storage_id, WiMaxService::kStorageNetworkId, &id) || |
| 237 | id.empty()) { |
| 238 | LOG(ERROR) << "Unable to load network id: " << storage_id; |
| 239 | continue; |
| 240 | } |
| 241 | string name; |
| 242 | if (!storage->GetString(storage_id, Service::kStorageName, &name) || |
| 243 | name.empty()) { |
| 244 | LOG(ERROR) << "Unable to load service name: " << storage_id; |
| 245 | continue; |
| 246 | } |
| 247 | WiMaxServiceRefPtr service = GetUniqueService(id, name); |
| 248 | CHECK(service); |
| 249 | if (!profile->ConfigureService(service)) { |
| 250 | LOG(ERROR) << "Could not configure service: " << storage_id; |
| 251 | } |
| 252 | created = true; |
| 253 | } |
| 254 | if (created) { |
| 255 | StartLiveServices(); |
| 256 | } |
| 257 | } |
| 258 | |
Darin Petkov | 6b9b2e1 | 2012-07-10 15:51:42 +0200 | [diff] [blame] | 259 | WiMaxRefPtr WiMaxProvider::SelectCarrier( |
| 260 | const WiMaxServiceConstRefPtr &service) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 261 | SLOG(WiMax, 2) << __func__ << "(" << service->GetStorageIdentifier() << ")"; |
| 262 | if (devices_.empty()) { |
| 263 | LOG(ERROR) << "No WiMAX devices available."; |
| 264 | return NULL; |
| 265 | } |
| 266 | // TODO(petkov): For now, just return the first available device. We need to |
| 267 | // be smarter here and select a device that sees |service|'s network. |
| 268 | return devices_.begin()->second; |
| 269 | } |
| 270 | |
| 271 | void WiMaxProvider::OnDevicesChanged(const RpcIdentifiers &devices) { |
| 272 | SLOG(WiMax, 2) << __func__; |
| 273 | DestroyDeadDevices(devices); |
| 274 | for (RpcIdentifiers::const_iterator it = devices.begin(); |
| 275 | it != devices.end(); ++it) { |
| 276 | const RpcIdentifier &path = *it; |
| 277 | string link_name = GetLinkName(path); |
| 278 | if (!link_name.empty()) { |
| 279 | CreateDevice(link_name, path); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 284 | void WiMaxProvider::CreateDevice(const string &link_name, |
| 285 | const RpcIdentifier &path) { |
| 286 | SLOG(WiMax, 2) << __func__ << "(" << link_name << ", " << path << ")"; |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 287 | if (ContainsKey(devices_, link_name)) { |
| 288 | SLOG(WiMax, 2) << "Device already exists."; |
| 289 | CHECK_EQ(path, devices_[link_name]->path()); |
| 290 | return; |
| 291 | } |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 292 | pending_devices_.erase(link_name); |
| 293 | DeviceInfo *device_info = manager_->device_info(); |
| 294 | if (device_info->IsDeviceBlackListed(link_name)) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 295 | LOG(INFO) << "WiMAX device not created, interface blacklisted: " |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 296 | << link_name; |
| 297 | return; |
| 298 | } |
| 299 | int index = device_info->GetIndex(link_name); |
| 300 | if (index < 0) { |
| 301 | SLOG(WiMax, 2) << link_name << " pending device info."; |
| 302 | // Adds the link to the pending device map, waiting for a notification from |
| 303 | // DeviceInfo that it's received information about the device from RTNL. |
| 304 | pending_devices_[link_name] = path; |
| 305 | return; |
| 306 | } |
| 307 | ByteString address_bytes; |
| 308 | if (!device_info->GetMACAddress(index, &address_bytes)) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 309 | LOG(ERROR) << "Unable to create a WiMAX device with no MAC address: " |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 310 | << link_name; |
| 311 | return; |
| 312 | } |
| 313 | string address = address_bytes.HexEncode(); |
| 314 | WiMaxRefPtr device(new WiMax(control_, dispatcher_, metrics_, manager_, |
| 315 | link_name, address, index, path)); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 316 | devices_[link_name] = device; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 317 | device_info->RegisterDevice(device); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 318 | LOG(INFO) << "Created WiMAX device: " << link_name << " @ " << path; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | void WiMaxProvider::DestroyDeadDevices(const RpcIdentifiers &live_devices) { |
| 322 | SLOG(WiMax, 2) << __func__ << "(" << live_devices.size() << ")"; |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 323 | for (map<string, RpcIdentifier>::iterator it = pending_devices_.begin(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 324 | it != pending_devices_.end(); ) { |
| 325 | if (find(live_devices.begin(), live_devices.end(), it->second) == |
| 326 | live_devices.end()) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 327 | LOG(INFO) << "Forgetting pending device: " << it->second; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 328 | pending_devices_.erase(it++); |
| 329 | } else { |
| 330 | ++it; |
| 331 | } |
| 332 | } |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 333 | for (map<string, WiMaxRefPtr>::iterator it = devices_.begin(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 334 | it != devices_.end(); ) { |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 335 | if (find(live_devices.begin(), live_devices.end(), it->second->path()) == |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 336 | live_devices.end()) { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 337 | LOG(INFO) << "Destroying device: " << it->first; |
Darin Petkov | b96a451 | 2012-06-04 11:02:49 +0200 | [diff] [blame] | 338 | const WiMaxRefPtr &device = it->second; |
| 339 | device->OnDeviceVanished(); |
| 340 | manager_->device_info()->DeregisterDevice(device); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 341 | devices_.erase(it++); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 342 | } else { |
| 343 | ++it; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | string WiMaxProvider::GetLinkName(const RpcIdentifier &path) { |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 349 | if (StartsWithASCII(path, wimax_manager::kDeviceObjectPathPrefix, true)) { |
| 350 | return path.substr(strlen(wimax_manager::kDeviceObjectPathPrefix)); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 351 | } |
| 352 | LOG(ERROR) << "Unable to determine link name from RPC path: " << path; |
| 353 | return string(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 356 | void WiMaxProvider::RetrieveNetworkInfo(const RpcIdentifier &path) { |
| 357 | if (ContainsKey(networks_, path)) { |
| 358 | // Nothing to do, the network info is already available. |
| 359 | return; |
| 360 | } |
Darin Petkov | b96a451 | 2012-06-04 11:02:49 +0200 | [diff] [blame] | 361 | LOG(INFO) << "WiMAX network appeared: " << path; |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 362 | scoped_ptr<WiMaxNetworkProxyInterface> proxy( |
| 363 | proxy_factory_->CreateWiMaxNetworkProxy(path)); |
| 364 | Error error; |
| 365 | NetworkInfo info; |
| 366 | info.name = proxy->Name(&error); |
| 367 | if (error.IsFailure()) { |
| 368 | return; |
| 369 | } |
| 370 | uint32 identifier = proxy->Identifier(&error); |
| 371 | if (error.IsFailure()) { |
| 372 | return; |
| 373 | } |
| 374 | info.id = WiMaxService::ConvertIdentifierToNetworkId(identifier); |
| 375 | networks_[path] = info; |
| 376 | } |
| 377 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 378 | WiMaxServiceRefPtr WiMaxProvider::FindService(const string &storage_id) const { |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 379 | SLOG(WiMax, 2) << __func__ << "(" << storage_id << ")"; |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 380 | map<string, WiMaxServiceRefPtr>::const_iterator find_it = |
| 381 | services_.find(storage_id); |
| 382 | if (find_it == services_.end()) { |
| 383 | return NULL; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 384 | } |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 385 | const WiMaxServiceRefPtr &service = find_it->second; |
| 386 | LOG_IF(ERROR, storage_id != service->GetStorageIdentifier()); |
| 387 | return service; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | WiMaxServiceRefPtr WiMaxProvider::GetUniqueService(const WiMaxNetworkId &id, |
| 391 | const string &name) { |
| 392 | SLOG(WiMax, 2) << __func__ << "(" << id << ", " << name << ")"; |
| 393 | string storage_id = WiMaxService::CreateStorageIdentifier(id, name); |
| 394 | WiMaxServiceRefPtr service = FindService(storage_id); |
| 395 | if (service) { |
| 396 | SLOG(WiMax, 2) << "Service already exists."; |
| 397 | return service; |
| 398 | } |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 399 | service = CreateService(id, name); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 400 | services_[service->GetStorageIdentifier()] = service; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 401 | manager_->RegisterService(service); |
| 402 | LOG(INFO) << "Registered WiMAX service: " << service->GetStorageIdentifier(); |
| 403 | return service; |
| 404 | } |
| 405 | |
Paul Stewart | bc14fb7 | 2013-07-30 08:21:58 -0700 | [diff] [blame] | 406 | WiMaxServiceRefPtr WiMaxProvider::CreateService(const WiMaxNetworkId &id, |
| 407 | const string &name) { |
| 408 | WiMaxServiceRefPtr service = |
| 409 | new WiMaxService(control_, dispatcher_, metrics_, manager_); |
| 410 | service->set_network_id(id); |
| 411 | service->set_friendly_name(name); |
| 412 | service->InitStorageIdentifier(); |
| 413 | return service; |
| 414 | } |
| 415 | |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 416 | void WiMaxProvider::StartLiveServices() { |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 417 | SLOG(WiMax, 2) << __func__ << "(" << networks_.size() << ")"; |
| 418 | for (map<RpcIdentifier, NetworkInfo>::const_iterator nit = networks_.begin(); |
| 419 | nit != networks_.end(); ++nit) { |
| 420 | const RpcIdentifier &path = nit->first; |
| 421 | const NetworkInfo &info = nit->second; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 422 | |
Darin Petkov | fc00fd4 | 2012-05-30 11:30:06 +0200 | [diff] [blame] | 423 | // Creates the default service for the network, if not already created. |
| 424 | GetUniqueService(info.id, info.name)->set_is_default(true); |
| 425 | |
| 426 | // Starts services for this live network. |
| 427 | for (map<string, WiMaxServiceRefPtr>::const_iterator it = services_.begin(); |
| 428 | it != services_.end(); ++it) { |
| 429 | const WiMaxServiceRefPtr &service = it->second; |
| 430 | if (service->network_id() != info.id || service->IsStarted()) { |
| 431 | continue; |
| 432 | } |
| 433 | if (!service->Start(proxy_factory_->CreateWiMaxNetworkProxy(path))) { |
| 434 | LOG(ERROR) << "Unable to start service: " |
| 435 | << service->GetStorageIdentifier(); |
| 436 | } |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | void WiMaxProvider::StopDeadServices() { |
| 442 | SLOG(WiMax, 2) << __func__ << "(" << networks_.size() << ")"; |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 443 | for (map<string, WiMaxServiceRefPtr>::iterator it = services_.begin(); |
| 444 | it != services_.end(); ) { |
Darin Petkov | c33eba8 | 2012-06-28 10:51:16 +0200 | [diff] [blame] | 445 | // Keeps a local reference until we're done with this service. |
| 446 | WiMaxServiceRefPtr service = it->second; |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 447 | if (service->IsStarted() && |
| 448 | !ContainsKey(networks_, service->GetNetworkObjectPath())) { |
| 449 | service->Stop(); |
| 450 | // Default services are created and registered when a network becomes |
| 451 | // live. They need to be deregistered and destroyed when the network |
| 452 | // disappears. |
| 453 | if (service->is_default()) { |
Darin Petkov | c33eba8 | 2012-06-28 10:51:16 +0200 | [diff] [blame] | 454 | // Removes |service| from the managed service set before deregistering |
| 455 | // it from Manager to ensure correct iterator increment (consider |
| 456 | // Manager::DeregisterService -> WiMaxService::Unload -> |
| 457 | // WiMaxProvider::OnServiceUnloaded -> services_.erase). |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 458 | services_.erase(it++); |
Darin Petkov | c33eba8 | 2012-06-28 10:51:16 +0200 | [diff] [blame] | 459 | manager_->DeregisterService(service); |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 460 | continue; |
| 461 | } |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 462 | } |
Darin Petkov | c1e5273 | 2012-05-25 15:23:45 +0200 | [diff] [blame] | 463 | ++it; |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 464 | } |
| 465 | } |
| 466 | |
| 467 | void WiMaxProvider::DestroyAllServices() { |
| 468 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | c33eba8 | 2012-06-28 10:51:16 +0200 | [diff] [blame] | 469 | while (!services_.empty()) { |
| 470 | // Keeps a local reference until we're done with this service. |
| 471 | WiMaxServiceRefPtr service = services_.begin()->second; |
| 472 | services_.erase(services_.begin()); |
| 473 | // Stops the service so that it can notify its carrier device, if any. |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 474 | service->Stop(); |
| 475 | manager_->DeregisterService(service); |
| 476 | LOG(INFO) << "Deregistered WiMAX service: " |
| 477 | << service->GetStorageIdentifier(); |
Darin Petkov | c63dcf0 | 2012-05-24 11:51:43 +0200 | [diff] [blame] | 478 | } |
| 479 | } |
| 480 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 481 | } // namespace shill |