blob: 684b72d9be3b146fd96bc36bac11d313dd57dea9 [file] [log] [blame]
Darin Petkovb72b62e2012-05-15 16:55:36 +02001// 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 Petkove4b27022012-05-16 13:28:50 +02007#include <algorithm>
Darin Petkovfc00fd42012-05-30 11:30:06 +02008#include <set>
Darin Petkove4b27022012-05-16 13:28:50 +02009
Darin Petkovb72b62e2012-05-15 16:55:36 +020010#include <base/bind.h>
Darin Petkove4b27022012-05-16 13:28:50 +020011#include <base/string_util.h>
Ben Chanb879d142012-05-15 11:10:32 -070012#include <chromeos/dbus/service_constants.h>
Darin Petkovb72b62e2012-05-15 16:55:36 +020013
Darin Petkovb72b62e2012-05-15 16:55:36 +020014#include "shill/error.h"
Darin Petkovd1cd7972012-05-22 15:26:15 +020015#include "shill/key_value_store.h"
Darin Petkove4b27022012-05-16 13:28:50 +020016#include "shill/manager.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020017#include "shill/profile.h"
Darin Petkovb72b62e2012-05-15 16:55:36 +020018#include "shill/proxy_factory.h"
19#include "shill/scope_logger.h"
Darin Petkovc63dcf02012-05-24 11:51:43 +020020#include "shill/store_interface.h"
Darin Petkove4b27022012-05-16 13:28:50 +020021#include "shill/wimax.h"
Darin Petkovb72b62e2012-05-15 16:55:36 +020022#include "shill/wimax_manager_proxy_interface.h"
Darin Petkovd1cd7972012-05-22 15:26:15 +020023#include "shill/wimax_service.h"
Darin Petkovb72b62e2012-05-15 16:55:36 +020024
25using base::Bind;
26using base::Unretained;
Darin Petkove4b27022012-05-16 13:28:50 +020027using std::find;
28using std::map;
Darin Petkovc63dcf02012-05-24 11:51:43 +020029using std::set;
Darin Petkovb72b62e2012-05-15 16:55:36 +020030using std::string;
Darin Petkovb72b62e2012-05-15 16:55:36 +020031
32namespace shill {
33
Darin Petkovb72b62e2012-05-15 16:55:36 +020034WiMaxProvider::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 Petkovc63dcf02012-05-24 11:51:43 +020044WiMaxProvider::~WiMaxProvider() {}
Darin Petkovb72b62e2012-05-15 16:55:36 +020045
46void WiMaxProvider::Start() {
47 SLOG(WiMax, 2) << __func__;
48 if (manager_proxy_.get()) {
49 return;
50 }
Darin Petkovb72b62e2012-05-15 16:55:36 +020051 manager_proxy_.reset(proxy_factory_->CreateWiMaxManagerProxy());
Darin Petkov9893d9c2012-05-17 15:27:31 -070052 manager_proxy_->set_devices_changed_callback(
53 Bind(&WiMaxProvider::OnDevicesChanged, Unretained(this)));
Darin Petkovb72b62e2012-05-15 16:55:36 +020054 Error error;
Darin Petkov9893d9c2012-05-17 15:27:31 -070055 OnDevicesChanged(manager_proxy_->Devices(&error));
Darin Petkovb72b62e2012-05-15 16:55:36 +020056}
57
58void WiMaxProvider::Stop() {
59 SLOG(WiMax, 2) << __func__;
Darin Petkovb72b62e2012-05-15 16:55:36 +020060 manager_proxy_.reset();
Darin Petkove4b27022012-05-16 13:28:50 +020061 DestroyDeadDevices(RpcIdentifiers());
Darin Petkovc63dcf02012-05-24 11:51:43 +020062 networks_.clear();
63 DestroyAllServices();
Darin Petkovb72b62e2012-05-15 16:55:36 +020064}
65
Darin Petkove4b27022012-05-16 13:28:50 +020066void WiMaxProvider::OnDeviceInfoAvailable(const string &link_name) {
67 SLOG(WiMax, 2) << __func__ << "(" << link_name << ")";
Darin Petkovfc00fd42012-05-30 11:30:06 +020068 map<string, RpcIdentifier>::const_iterator find_it =
Darin Petkovc1e52732012-05-25 15:23:45 +020069 pending_devices_.find(link_name);
Darin Petkove4b27022012-05-16 13:28:50 +020070 if (find_it != pending_devices_.end()) {
71 RpcIdentifier path = find_it->second;
72 CreateDevice(link_name, path);
73 }
74}
75
Darin Petkovc63dcf02012-05-24 11:51:43 +020076void WiMaxProvider::OnNetworksChanged() {
77 SLOG(WiMax, 2) << __func__;
Darin Petkovfc00fd42012-05-30 11:30:06 +020078 // Collects a set of live networks from all devices.
79 set<RpcIdentifier> live_networks;
80 for (map<string, WiMaxRefPtr>::const_iterator it = devices_.begin();
Darin Petkovc63dcf02012-05-24 11:51:43 +020081 it != devices_.end(); ++it) {
82 const set<RpcIdentifier> &networks = it->second->networks();
Darin Petkovfc00fd42012-05-30 11:30:06 +020083 live_networks.insert(networks.begin(), networks.end());
Darin Petkovc63dcf02012-05-24 11:51:43 +020084 }
Darin Petkovfc00fd42012-05-30 11:30:06 +020085 // Removes dead networks from |networks_|.
86 for (map<RpcIdentifier, NetworkInfo>::iterator it = networks_.begin();
87 it != networks_.end(); ) {
88 if (ContainsKey(live_networks, it->first)) {
89 ++it;
90 } else {
91 networks_.erase(it++);
92 }
93 }
94 // Retrieves network info into |networks_| for the live networks.
95 for (set<RpcIdentifier>::const_iterator it = live_networks.begin();
96 it != live_networks.end(); ++it) {
97 RetrieveNetworkInfo(*it);
98 }
99 // Stops dead and starts live services based on the current |networks_|.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200100 StopDeadServices();
101 StartLiveServices();
102}
103
Darin Petkovc1e52732012-05-25 15:23:45 +0200104bool WiMaxProvider::OnServiceUnloaded(const WiMaxServiceRefPtr &service) {
105 SLOG(WiMax, 2) << __func__ << "(" << service->GetStorageIdentifier() << ")";
106 if (service->is_default()) {
107 return false;
108 }
109 // Removes the service from the managed service set. The service will be
110 // deregistered from Manager when we release ownership by returning true.
111 services_.erase(service->GetStorageIdentifier());
112 return true;
113}
114
Darin Petkovd1cd7972012-05-22 15:26:15 +0200115WiMaxServiceRefPtr WiMaxProvider::GetService(const KeyValueStore &args,
116 Error *error) {
117 SLOG(WiMax, 2) << __func__;
118 CHECK_EQ(args.GetString(flimflam::kTypeProperty), flimflam::kTypeWimax);
Darin Petkovd1cd7972012-05-22 15:26:15 +0200119 WiMaxNetworkId id = args.LookupString(WiMaxService::kNetworkIdProperty, "");
120 if (id.empty()) {
121 Error::PopulateAndLog(
122 error, Error::kInvalidArguments, "Missing WiMAX network id.");
123 return NULL;
124 }
125 string name = args.LookupString(flimflam::kNameProperty, "");
126 if (name.empty()) {
127 Error::PopulateAndLog(
128 error, Error::kInvalidArguments, "Missing WiMAX service name.");
129 return NULL;
130 }
Darin Petkovc63dcf02012-05-24 11:51:43 +0200131 WiMaxServiceRefPtr service = GetUniqueService(id, name);
Darin Petkovd1cd7972012-05-22 15:26:15 +0200132 CHECK(service);
Darin Petkovfc00fd42012-05-30 11:30:06 +0200133 // Configures the service using the rest of the passed-in arguments.
Darin Petkovd1cd7972012-05-22 15:26:15 +0200134 service->Configure(args, error);
Darin Petkovfc00fd42012-05-30 11:30:06 +0200135 // Starts the service if there's a matching live network.
Darin Petkovc63dcf02012-05-24 11:51:43 +0200136 StartLiveServices();
Darin Petkovd1cd7972012-05-22 15:26:15 +0200137 return service;
138}
139
Darin Petkovc63dcf02012-05-24 11:51:43 +0200140void WiMaxProvider::CreateServicesFromProfile(const ProfileRefPtr &profile) {
141 SLOG(WiMax, 2) << __func__;
142 bool created = false;
143 const StoreInterface *storage = profile->GetConstStorage();
144 set<string> groups = storage->GetGroupsWithKey(Service::kStorageType);
Darin Petkovfc00fd42012-05-30 11:30:06 +0200145 for (set<string>::const_iterator it = groups.begin();
146 it != groups.end(); ++it) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200147 const string &storage_id = *it;
148 string type;
149 if (!storage->GetString(storage_id, Service::kStorageType, &type) ||
150 type != Technology::NameFromIdentifier(Technology::kWiMax)) {
151 continue;
152 }
153 if (FindService(storage_id)) {
154 continue;
155 }
156 WiMaxNetworkId id;
157 if (!storage->GetString(storage_id, WiMaxService::kStorageNetworkId, &id) ||
158 id.empty()) {
159 LOG(ERROR) << "Unable to load network id: " << storage_id;
160 continue;
161 }
162 string name;
163 if (!storage->GetString(storage_id, Service::kStorageName, &name) ||
164 name.empty()) {
165 LOG(ERROR) << "Unable to load service name: " << storage_id;
166 continue;
167 }
168 WiMaxServiceRefPtr service = GetUniqueService(id, name);
169 CHECK(service);
170 if (!profile->ConfigureService(service)) {
171 LOG(ERROR) << "Could not configure service: " << storage_id;
172 }
173 created = true;
174 }
175 if (created) {
176 StartLiveServices();
177 }
178}
179
180WiMaxRefPtr WiMaxProvider::SelectCarrier(const WiMaxServiceRefPtr &service) {
181 SLOG(WiMax, 2) << __func__ << "(" << service->GetStorageIdentifier() << ")";
182 if (devices_.empty()) {
183 LOG(ERROR) << "No WiMAX devices available.";
184 return NULL;
185 }
186 // TODO(petkov): For now, just return the first available device. We need to
187 // be smarter here and select a device that sees |service|'s network.
188 return devices_.begin()->second;
189}
190
191void WiMaxProvider::OnDevicesChanged(const RpcIdentifiers &devices) {
192 SLOG(WiMax, 2) << __func__;
193 DestroyDeadDevices(devices);
194 for (RpcIdentifiers::const_iterator it = devices.begin();
195 it != devices.end(); ++it) {
196 const RpcIdentifier &path = *it;
197 string link_name = GetLinkName(path);
198 if (!link_name.empty()) {
199 CreateDevice(link_name, path);
200 }
201 }
202}
203
Darin Petkove4b27022012-05-16 13:28:50 +0200204void WiMaxProvider::CreateDevice(const string &link_name,
205 const RpcIdentifier &path) {
206 SLOG(WiMax, 2) << __func__ << "(" << link_name << ", " << path << ")";
Darin Petkov0fcd3462012-05-17 11:25:11 +0200207 if (ContainsKey(devices_, link_name)) {
208 SLOG(WiMax, 2) << "Device already exists.";
209 CHECK_EQ(path, devices_[link_name]->path());
210 return;
211 }
Darin Petkove4b27022012-05-16 13:28:50 +0200212 pending_devices_.erase(link_name);
213 DeviceInfo *device_info = manager_->device_info();
214 if (device_info->IsDeviceBlackListed(link_name)) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200215 LOG(INFO) << "WiMAX device not created, interface blacklisted: "
Darin Petkove4b27022012-05-16 13:28:50 +0200216 << link_name;
217 return;
218 }
219 int index = device_info->GetIndex(link_name);
220 if (index < 0) {
221 SLOG(WiMax, 2) << link_name << " pending device info.";
222 // Adds the link to the pending device map, waiting for a notification from
223 // DeviceInfo that it's received information about the device from RTNL.
224 pending_devices_[link_name] = path;
225 return;
226 }
227 ByteString address_bytes;
228 if (!device_info->GetMACAddress(index, &address_bytes)) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200229 LOG(ERROR) << "Unable to create a WiMAX device with no MAC address: "
Darin Petkove4b27022012-05-16 13:28:50 +0200230 << link_name;
231 return;
232 }
233 string address = address_bytes.HexEncode();
234 WiMaxRefPtr device(new WiMax(control_, dispatcher_, metrics_, manager_,
235 link_name, address, index, path));
Darin Petkov0fcd3462012-05-17 11:25:11 +0200236 devices_[link_name] = device;
Darin Petkove4b27022012-05-16 13:28:50 +0200237 device_info->RegisterDevice(device);
Darin Petkovc63dcf02012-05-24 11:51:43 +0200238 LOG(INFO) << "Created WiMAX device: " << link_name << " @ " << path;
Darin Petkove4b27022012-05-16 13:28:50 +0200239}
240
241void WiMaxProvider::DestroyDeadDevices(const RpcIdentifiers &live_devices) {
242 SLOG(WiMax, 2) << __func__ << "(" << live_devices.size() << ")";
Darin Petkovc1e52732012-05-25 15:23:45 +0200243 for (map<string, RpcIdentifier>::iterator it = pending_devices_.begin();
Darin Petkove4b27022012-05-16 13:28:50 +0200244 it != pending_devices_.end(); ) {
245 if (find(live_devices.begin(), live_devices.end(), it->second) ==
246 live_devices.end()) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200247 LOG(INFO) << "Forgetting pending device: " << it->second;
Darin Petkove4b27022012-05-16 13:28:50 +0200248 pending_devices_.erase(it++);
249 } else {
250 ++it;
251 }
252 }
Darin Petkov0fcd3462012-05-17 11:25:11 +0200253 for (map<string, WiMaxRefPtr>::iterator it = devices_.begin();
Darin Petkove4b27022012-05-16 13:28:50 +0200254 it != devices_.end(); ) {
Darin Petkov0fcd3462012-05-17 11:25:11 +0200255 if (find(live_devices.begin(), live_devices.end(), it->second->path()) ==
Darin Petkove4b27022012-05-16 13:28:50 +0200256 live_devices.end()) {
Darin Petkovc63dcf02012-05-24 11:51:43 +0200257 LOG(INFO) << "Destroying device: " << it->first;
Darin Petkov0fcd3462012-05-17 11:25:11 +0200258 manager_->device_info()->DeregisterDevice(it->second);
259 devices_.erase(it++);
Darin Petkove4b27022012-05-16 13:28:50 +0200260 } else {
261 ++it;
262 }
263 }
264}
265
266string WiMaxProvider::GetLinkName(const RpcIdentifier &path) {
Darin Petkov9893d9c2012-05-17 15:27:31 -0700267 if (StartsWithASCII(path, wimax_manager::kDeviceObjectPathPrefix, true)) {
268 return path.substr(strlen(wimax_manager::kDeviceObjectPathPrefix));
Darin Petkove4b27022012-05-16 13:28:50 +0200269 }
270 LOG(ERROR) << "Unable to determine link name from RPC path: " << path;
271 return string();
Darin Petkovb72b62e2012-05-15 16:55:36 +0200272}
273
Darin Petkovfc00fd42012-05-30 11:30:06 +0200274void WiMaxProvider::RetrieveNetworkInfo(const RpcIdentifier &path) {
275 if (ContainsKey(networks_, path)) {
276 // Nothing to do, the network info is already available.
277 return;
278 }
279 scoped_ptr<WiMaxNetworkProxyInterface> proxy(
280 proxy_factory_->CreateWiMaxNetworkProxy(path));
281 Error error;
282 NetworkInfo info;
283 info.name = proxy->Name(&error);
284 if (error.IsFailure()) {
285 return;
286 }
287 uint32 identifier = proxy->Identifier(&error);
288 if (error.IsFailure()) {
289 return;
290 }
291 info.id = WiMaxService::ConvertIdentifierToNetworkId(identifier);
292 networks_[path] = info;
293}
294
Darin Petkovc63dcf02012-05-24 11:51:43 +0200295WiMaxServiceRefPtr WiMaxProvider::FindService(const string &storage_id) {
296 SLOG(WiMax, 2) << __func__ << "(" << storage_id << ")";
Darin Petkovc1e52732012-05-25 15:23:45 +0200297 map<string, WiMaxServiceRefPtr>::const_iterator find_it =
298 services_.find(storage_id);
299 if (find_it == services_.end()) {
300 return NULL;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200301 }
Darin Petkovc1e52732012-05-25 15:23:45 +0200302 const WiMaxServiceRefPtr &service = find_it->second;
303 LOG_IF(ERROR, storage_id != service->GetStorageIdentifier());
304 return service;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200305}
306
307WiMaxServiceRefPtr WiMaxProvider::GetUniqueService(const WiMaxNetworkId &id,
308 const string &name) {
309 SLOG(WiMax, 2) << __func__ << "(" << id << ", " << name << ")";
310 string storage_id = WiMaxService::CreateStorageIdentifier(id, name);
311 WiMaxServiceRefPtr service = FindService(storage_id);
312 if (service) {
313 SLOG(WiMax, 2) << "Service already exists.";
314 return service;
315 }
316 service = new WiMaxService(control_, dispatcher_, metrics_, manager_);
317 service->set_network_id(id);
318 service->set_friendly_name(name);
319 service->InitStorageIdentifier();
Darin Petkovc1e52732012-05-25 15:23:45 +0200320 services_[service->GetStorageIdentifier()] = service;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200321 manager_->RegisterService(service);
322 LOG(INFO) << "Registered WiMAX service: " << service->GetStorageIdentifier();
323 return service;
324}
325
Darin Petkovc63dcf02012-05-24 11:51:43 +0200326void WiMaxProvider::StartLiveServices() {
Darin Petkovfc00fd42012-05-30 11:30:06 +0200327 SLOG(WiMax, 2) << __func__ << "(" << networks_.size() << ")";
328 for (map<RpcIdentifier, NetworkInfo>::const_iterator nit = networks_.begin();
329 nit != networks_.end(); ++nit) {
330 const RpcIdentifier &path = nit->first;
331 const NetworkInfo &info = nit->second;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200332
Darin Petkovfc00fd42012-05-30 11:30:06 +0200333 // Creates the default service for the network, if not already created.
334 GetUniqueService(info.id, info.name)->set_is_default(true);
335
336 // Starts services for this live network.
337 for (map<string, WiMaxServiceRefPtr>::const_iterator it = services_.begin();
338 it != services_.end(); ++it) {
339 const WiMaxServiceRefPtr &service = it->second;
340 if (service->network_id() != info.id || service->IsStarted()) {
341 continue;
342 }
343 if (!service->Start(proxy_factory_->CreateWiMaxNetworkProxy(path))) {
344 LOG(ERROR) << "Unable to start service: "
345 << service->GetStorageIdentifier();
346 }
Darin Petkovc63dcf02012-05-24 11:51:43 +0200347 }
348 }
349}
350
351void WiMaxProvider::StopDeadServices() {
352 SLOG(WiMax, 2) << __func__ << "(" << networks_.size() << ")";
Darin Petkovc1e52732012-05-25 15:23:45 +0200353 for (map<string, WiMaxServiceRefPtr>::iterator it = services_.begin();
354 it != services_.end(); ) {
355 const WiMaxServiceRefPtr &service = it->second;
356 if (service->IsStarted() &&
357 !ContainsKey(networks_, service->GetNetworkObjectPath())) {
358 service->Stop();
359 // Default services are created and registered when a network becomes
360 // live. They need to be deregistered and destroyed when the network
361 // disappears.
362 if (service->is_default()) {
363 manager_->DeregisterService(service);
364 services_.erase(it++);
365 continue;
366 }
Darin Petkovc63dcf02012-05-24 11:51:43 +0200367 }
Darin Petkovc1e52732012-05-25 15:23:45 +0200368 ++it;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200369 }
370}
371
372void WiMaxProvider::DestroyAllServices() {
373 SLOG(WiMax, 2) << __func__;
Darin Petkovc1e52732012-05-25 15:23:45 +0200374 for (map<string, WiMaxServiceRefPtr>::iterator it = services_.begin();
375 it != services_.end(); services_.erase(it++)) {
376 const WiMaxServiceRefPtr &service = it->second;
Darin Petkovc63dcf02012-05-24 11:51:43 +0200377 // Stop the service so that it can notify its carrier device, if any.
378 service->Stop();
379 manager_->DeregisterService(service);
380 LOG(INFO) << "Deregistered WiMAX service: "
381 << service->GetStorageIdentifier();
Darin Petkovc63dcf02012-05-24 11:51:43 +0200382 }
383}
384
Darin Petkovb72b62e2012-05-15 16:55:36 +0200385} // namespace shill