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> |
| 8 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 9 | #include <base/bind.h> |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 10 | #include <base/string_util.h> |
Ben Chan | b879d14 | 2012-05-15 11:10:32 -0700 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 12 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 13 | #include "shill/error.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame^] | 14 | #include "shill/key_value_store.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 15 | #include "shill/manager.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 16 | #include "shill/proxy_factory.h" |
| 17 | #include "shill/scope_logger.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 18 | #include "shill/wimax.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 19 | #include "shill/wimax_manager_proxy_interface.h" |
Darin Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame^] | 20 | #include "shill/wimax_service.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 21 | |
| 22 | using base::Bind; |
| 23 | using base::Unretained; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 24 | using std::find; |
| 25 | using std::map; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 26 | using std::string; |
| 27 | using std::vector; |
| 28 | |
| 29 | namespace shill { |
| 30 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 31 | WiMaxProvider::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 | |
| 41 | WiMaxProvider::~WiMaxProvider() { |
| 42 | Stop(); |
| 43 | } |
| 44 | |
| 45 | void WiMaxProvider::Start() { |
| 46 | SLOG(WiMax, 2) << __func__; |
| 47 | if (manager_proxy_.get()) { |
| 48 | return; |
| 49 | } |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 50 | manager_proxy_.reset(proxy_factory_->CreateWiMaxManagerProxy()); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 51 | manager_proxy_->set_devices_changed_callback( |
| 52 | Bind(&WiMaxProvider::OnDevicesChanged, Unretained(this))); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 53 | Error error; |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 54 | OnDevicesChanged(manager_proxy_->Devices(&error)); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | void WiMaxProvider::Stop() { |
| 58 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 59 | manager_proxy_.reset(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 60 | DestroyDeadDevices(RpcIdentifiers()); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 61 | } |
| 62 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 63 | void WiMaxProvider::OnDevicesChanged(const RpcIdentifiers &devices) { |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 64 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 65 | 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 Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 75 | void 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 Petkov | d1cd797 | 2012-05-22 15:26:15 +0200 | [diff] [blame^] | 84 | WiMaxServiceRefPtr 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 Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 119 | void WiMaxProvider::CreateDevice(const string &link_name, |
| 120 | const RpcIdentifier &path) { |
| 121 | SLOG(WiMax, 2) << __func__ << "(" << link_name << ", " << path << ")"; |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 122 | if (ContainsKey(devices_, link_name)) { |
| 123 | SLOG(WiMax, 2) << "Device already exists."; |
| 124 | CHECK_EQ(path, devices_[link_name]->path()); |
| 125 | return; |
| 126 | } |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 127 | 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 Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 144 | LOG(ERROR) << "Unable to create a WiMax device with no MAC address: " |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 145 | << 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 Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 151 | devices_[link_name] = device; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 152 | device_info->RegisterDevice(device); |
| 153 | } |
| 154 | |
| 155 | void 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 Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 167 | for (map<string, WiMaxRefPtr>::iterator it = devices_.begin(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 168 | it != devices_.end(); ) { |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 169 | if (find(live_devices.begin(), live_devices.end(), it->second->path()) == |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 170 | live_devices.end()) { |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 171 | SLOG(WiMax, 2) << "Destroying device: " << it->first; |
| 172 | manager_->device_info()->DeregisterDevice(it->second); |
| 173 | devices_.erase(it++); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 174 | } else { |
| 175 | ++it; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | string WiMaxProvider::GetLinkName(const RpcIdentifier &path) { |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 181 | if (StartsWithASCII(path, wimax_manager::kDeviceObjectPathPrefix, true)) { |
| 182 | return path.substr(strlen(wimax_manager::kDeviceObjectPathPrefix)); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 183 | } |
| 184 | LOG(ERROR) << "Unable to determine link name from RPC path: " << path; |
| 185 | return string(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | } // namespace shill |