blob: b5f0ad5fd93656cab56d18bdd1d1a4feef0e7872 [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>
8
Darin Petkovb72b62e2012-05-15 16:55:36 +02009#include <base/bind.h>
Darin Petkove4b27022012-05-16 13:28:50 +020010#include <base/string_util.h>
Ben Chanb879d142012-05-15 11:10:32 -070011#include <chromeos/dbus/service_constants.h>
Darin Petkovb72b62e2012-05-15 16:55:36 +020012
Darin Petkovb72b62e2012-05-15 16:55:36 +020013#include "shill/error.h"
Darin Petkovd1cd7972012-05-22 15:26:15 +020014#include "shill/key_value_store.h"
Darin Petkove4b27022012-05-16 13:28:50 +020015#include "shill/manager.h"
Darin Petkovb72b62e2012-05-15 16:55:36 +020016#include "shill/proxy_factory.h"
17#include "shill/scope_logger.h"
Darin Petkove4b27022012-05-16 13:28:50 +020018#include "shill/wimax.h"
Darin Petkovb72b62e2012-05-15 16:55:36 +020019#include "shill/wimax_manager_proxy_interface.h"
Darin Petkovd1cd7972012-05-22 15:26:15 +020020#include "shill/wimax_service.h"
Darin Petkovb72b62e2012-05-15 16:55:36 +020021
22using base::Bind;
23using base::Unretained;
Darin Petkove4b27022012-05-16 13:28:50 +020024using std::find;
25using std::map;
Darin Petkovb72b62e2012-05-15 16:55:36 +020026using std::string;
27using std::vector;
28
29namespace shill {
30
Darin Petkovb72b62e2012-05-15 16:55:36 +020031WiMaxProvider::WiMaxProvider(ControlInterface *control,
32 EventDispatcher *dispatcher,
33 Metrics *metrics,
34 Manager *manager)
35 : control_(control),
36 dispatcher_(dispatcher),
37 metrics_(metrics),
38 manager_(manager),
39 proxy_factory_(ProxyFactory::GetInstance()) {}
40
41WiMaxProvider::~WiMaxProvider() {
42 Stop();
43}
44
45void WiMaxProvider::Start() {
46 SLOG(WiMax, 2) << __func__;
47 if (manager_proxy_.get()) {
48 return;
49 }
Darin Petkovb72b62e2012-05-15 16:55:36 +020050 manager_proxy_.reset(proxy_factory_->CreateWiMaxManagerProxy());
Darin Petkov9893d9c2012-05-17 15:27:31 -070051 manager_proxy_->set_devices_changed_callback(
52 Bind(&WiMaxProvider::OnDevicesChanged, Unretained(this)));
Darin Petkovb72b62e2012-05-15 16:55:36 +020053 Error error;
Darin Petkov9893d9c2012-05-17 15:27:31 -070054 OnDevicesChanged(manager_proxy_->Devices(&error));
Darin Petkovb72b62e2012-05-15 16:55:36 +020055}
56
57void WiMaxProvider::Stop() {
58 SLOG(WiMax, 2) << __func__;
Darin Petkovb72b62e2012-05-15 16:55:36 +020059 manager_proxy_.reset();
Darin Petkove4b27022012-05-16 13:28:50 +020060 DestroyDeadDevices(RpcIdentifiers());
Darin Petkovb72b62e2012-05-15 16:55:36 +020061}
62
Darin Petkov9893d9c2012-05-17 15:27:31 -070063void WiMaxProvider::OnDevicesChanged(const RpcIdentifiers &devices) {
Darin Petkovb72b62e2012-05-15 16:55:36 +020064 SLOG(WiMax, 2) << __func__;
Darin Petkove4b27022012-05-16 13:28:50 +020065 DestroyDeadDevices(devices);
66 for (RpcIdentifiers::const_iterator it = devices.begin();
67 it != devices.end(); ++it) {
68 string link_name = GetLinkName(*it);
69 if (!link_name.empty()) {
70 CreateDevice(link_name, *it);
71 }
72 }
Darin Petkovb72b62e2012-05-15 16:55:36 +020073}
74
Darin Petkove4b27022012-05-16 13:28:50 +020075void WiMaxProvider::OnDeviceInfoAvailable(const string &link_name) {
76 SLOG(WiMax, 2) << __func__ << "(" << link_name << ")";
77 map<string, string>::iterator find_it = pending_devices_.find(link_name);
78 if (find_it != pending_devices_.end()) {
79 RpcIdentifier path = find_it->second;
80 CreateDevice(link_name, path);
81 }
82}
83
Darin Petkovd1cd7972012-05-22 15:26:15 +020084WiMaxServiceRefPtr WiMaxProvider::GetService(const KeyValueStore &args,
85 Error *error) {
86 SLOG(WiMax, 2) << __func__;
87 CHECK_EQ(args.GetString(flimflam::kTypeProperty), flimflam::kTypeWimax);
88 if (devices_.empty()) {
89 Error::PopulateAndLog(
90 error, Error::kNotSupported, "No WiMAX device available.");
91 return NULL;
92 }
93 WiMaxNetworkId id = args.LookupString(WiMaxService::kNetworkIdProperty, "");
94 if (id.empty()) {
95 Error::PopulateAndLog(
96 error, Error::kInvalidArguments, "Missing WiMAX network id.");
97 return NULL;
98 }
99 string name = args.LookupString(flimflam::kNameProperty, "");
100 if (name.empty()) {
101 Error::PopulateAndLog(
102 error, Error::kInvalidArguments, "Missing WiMAX service name.");
103 return NULL;
104 }
105 // Use the first available WiMAX device to construct and register the
106 // service. We may need to consider alternatives if this becomes an issue. For
107 // example, we could refactor service creation and management out of WiMax
108 // into WiMaxProvider.
109 WiMaxRefPtr device = devices_.begin()->second;
110 WiMaxServiceRefPtr service = device->GetService(id, name);
111 CHECK(service);
112 // Configure the service using the the rest of the passed-in arguments.
113 service->Configure(args, error);
114 // Start the service if there's a matching live network.
115 device->StartLiveServices();
116 return service;
117}
118
Darin Petkove4b27022012-05-16 13:28:50 +0200119void WiMaxProvider::CreateDevice(const string &link_name,
120 const RpcIdentifier &path) {
121 SLOG(WiMax, 2) << __func__ << "(" << link_name << ", " << path << ")";
Darin Petkov0fcd3462012-05-17 11:25:11 +0200122 if (ContainsKey(devices_, link_name)) {
123 SLOG(WiMax, 2) << "Device already exists.";
124 CHECK_EQ(path, devices_[link_name]->path());
125 return;
126 }
Darin Petkove4b27022012-05-16 13:28:50 +0200127 pending_devices_.erase(link_name);
128 DeviceInfo *device_info = manager_->device_info();
129 if (device_info->IsDeviceBlackListed(link_name)) {
130 LOG(INFO) << "Do not create WiMax device for blacklisted interface "
131 << link_name;
132 return;
133 }
134 int index = device_info->GetIndex(link_name);
135 if (index < 0) {
136 SLOG(WiMax, 2) << link_name << " pending device info.";
137 // Adds the link to the pending device map, waiting for a notification from
138 // DeviceInfo that it's received information about the device from RTNL.
139 pending_devices_[link_name] = path;
140 return;
141 }
142 ByteString address_bytes;
143 if (!device_info->GetMACAddress(index, &address_bytes)) {
Darin Petkov0fcd3462012-05-17 11:25:11 +0200144 LOG(ERROR) << "Unable to create a WiMax device with no MAC address: "
Darin Petkove4b27022012-05-16 13:28:50 +0200145 << link_name;
146 return;
147 }
148 string address = address_bytes.HexEncode();
149 WiMaxRefPtr device(new WiMax(control_, dispatcher_, metrics_, manager_,
150 link_name, address, index, path));
Darin Petkov0fcd3462012-05-17 11:25:11 +0200151 devices_[link_name] = device;
Darin Petkove4b27022012-05-16 13:28:50 +0200152 device_info->RegisterDevice(device);
153}
154
155void WiMaxProvider::DestroyDeadDevices(const RpcIdentifiers &live_devices) {
156 SLOG(WiMax, 2) << __func__ << "(" << live_devices.size() << ")";
157 for (map<string, string>::iterator it = pending_devices_.begin();
158 it != pending_devices_.end(); ) {
159 if (find(live_devices.begin(), live_devices.end(), it->second) ==
160 live_devices.end()) {
161 SLOG(WiMax, 2) << "Forgetting pending device: " << it->second;
162 pending_devices_.erase(it++);
163 } else {
164 ++it;
165 }
166 }
Darin Petkov0fcd3462012-05-17 11:25:11 +0200167 for (map<string, WiMaxRefPtr>::iterator it = devices_.begin();
Darin Petkove4b27022012-05-16 13:28:50 +0200168 it != devices_.end(); ) {
Darin Petkov0fcd3462012-05-17 11:25:11 +0200169 if (find(live_devices.begin(), live_devices.end(), it->second->path()) ==
Darin Petkove4b27022012-05-16 13:28:50 +0200170 live_devices.end()) {
Darin Petkov0fcd3462012-05-17 11:25:11 +0200171 SLOG(WiMax, 2) << "Destroying device: " << it->first;
172 manager_->device_info()->DeregisterDevice(it->second);
173 devices_.erase(it++);
Darin Petkove4b27022012-05-16 13:28:50 +0200174 } else {
175 ++it;
176 }
177 }
178}
179
180string WiMaxProvider::GetLinkName(const RpcIdentifier &path) {
Darin Petkov9893d9c2012-05-17 15:27:31 -0700181 if (StartsWithASCII(path, wimax_manager::kDeviceObjectPathPrefix, true)) {
182 return path.substr(strlen(wimax_manager::kDeviceObjectPathPrefix));
Darin Petkove4b27022012-05-16 13:28:50 +0200183 }
184 LOG(ERROR) << "Unable to determine link name from RPC path: " << path;
185 return string();
Darin Petkovb72b62e2012-05-15 16:55:36 +0200186}
187
188} // namespace shill