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 | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 14 | #include "shill/manager.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 15 | #include "shill/proxy_factory.h" |
| 16 | #include "shill/scope_logger.h" |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 17 | #include "shill/wimax.h" |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 18 | #include "shill/wimax_manager_proxy_interface.h" |
| 19 | |
| 20 | using base::Bind; |
| 21 | using base::Unretained; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 22 | using std::find; |
| 23 | using std::map; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 24 | using std::string; |
| 25 | using std::vector; |
| 26 | |
| 27 | namespace shill { |
| 28 | |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 29 | WiMaxProvider::WiMaxProvider(ControlInterface *control, |
| 30 | EventDispatcher *dispatcher, |
| 31 | Metrics *metrics, |
| 32 | Manager *manager) |
| 33 | : control_(control), |
| 34 | dispatcher_(dispatcher), |
| 35 | metrics_(metrics), |
| 36 | manager_(manager), |
| 37 | proxy_factory_(ProxyFactory::GetInstance()) {} |
| 38 | |
| 39 | WiMaxProvider::~WiMaxProvider() { |
| 40 | Stop(); |
| 41 | } |
| 42 | |
| 43 | void WiMaxProvider::Start() { |
| 44 | SLOG(WiMax, 2) << __func__; |
| 45 | if (manager_proxy_.get()) { |
| 46 | return; |
| 47 | } |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 48 | manager_proxy_.reset(proxy_factory_->CreateWiMaxManagerProxy()); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 49 | manager_proxy_->set_devices_changed_callback( |
| 50 | Bind(&WiMaxProvider::OnDevicesChanged, Unretained(this))); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 51 | Error error; |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 52 | OnDevicesChanged(manager_proxy_->Devices(&error)); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void WiMaxProvider::Stop() { |
| 56 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 57 | manager_proxy_.reset(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 58 | DestroyDeadDevices(RpcIdentifiers()); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 61 | void WiMaxProvider::OnDevicesChanged(const RpcIdentifiers &devices) { |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 62 | SLOG(WiMax, 2) << __func__; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 63 | DestroyDeadDevices(devices); |
| 64 | for (RpcIdentifiers::const_iterator it = devices.begin(); |
| 65 | it != devices.end(); ++it) { |
| 66 | string link_name = GetLinkName(*it); |
| 67 | if (!link_name.empty()) { |
| 68 | CreateDevice(link_name, *it); |
| 69 | } |
| 70 | } |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 71 | } |
| 72 | |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 73 | void WiMaxProvider::OnDeviceInfoAvailable(const string &link_name) { |
| 74 | SLOG(WiMax, 2) << __func__ << "(" << link_name << ")"; |
| 75 | map<string, string>::iterator find_it = pending_devices_.find(link_name); |
| 76 | if (find_it != pending_devices_.end()) { |
| 77 | RpcIdentifier path = find_it->second; |
| 78 | CreateDevice(link_name, path); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | void WiMaxProvider::CreateDevice(const string &link_name, |
| 83 | const RpcIdentifier &path) { |
| 84 | SLOG(WiMax, 2) << __func__ << "(" << link_name << ", " << path << ")"; |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 85 | if (ContainsKey(devices_, link_name)) { |
| 86 | SLOG(WiMax, 2) << "Device already exists."; |
| 87 | CHECK_EQ(path, devices_[link_name]->path()); |
| 88 | return; |
| 89 | } |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 90 | pending_devices_.erase(link_name); |
| 91 | DeviceInfo *device_info = manager_->device_info(); |
| 92 | if (device_info->IsDeviceBlackListed(link_name)) { |
| 93 | LOG(INFO) << "Do not create WiMax device for blacklisted interface " |
| 94 | << link_name; |
| 95 | return; |
| 96 | } |
| 97 | int index = device_info->GetIndex(link_name); |
| 98 | if (index < 0) { |
| 99 | SLOG(WiMax, 2) << link_name << " pending device info."; |
| 100 | // Adds the link to the pending device map, waiting for a notification from |
| 101 | // DeviceInfo that it's received information about the device from RTNL. |
| 102 | pending_devices_[link_name] = path; |
| 103 | return; |
| 104 | } |
| 105 | ByteString address_bytes; |
| 106 | if (!device_info->GetMACAddress(index, &address_bytes)) { |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 107 | LOG(ERROR) << "Unable to create a WiMax device with no MAC address: " |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 108 | << link_name; |
| 109 | return; |
| 110 | } |
| 111 | string address = address_bytes.HexEncode(); |
| 112 | WiMaxRefPtr device(new WiMax(control_, dispatcher_, metrics_, manager_, |
| 113 | link_name, address, index, path)); |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 114 | devices_[link_name] = device; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 115 | device_info->RegisterDevice(device); |
| 116 | } |
| 117 | |
| 118 | void WiMaxProvider::DestroyDeadDevices(const RpcIdentifiers &live_devices) { |
| 119 | SLOG(WiMax, 2) << __func__ << "(" << live_devices.size() << ")"; |
| 120 | for (map<string, string>::iterator it = pending_devices_.begin(); |
| 121 | it != pending_devices_.end(); ) { |
| 122 | if (find(live_devices.begin(), live_devices.end(), it->second) == |
| 123 | live_devices.end()) { |
| 124 | SLOG(WiMax, 2) << "Forgetting pending device: " << it->second; |
| 125 | pending_devices_.erase(it++); |
| 126 | } else { |
| 127 | ++it; |
| 128 | } |
| 129 | } |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 130 | for (map<string, WiMaxRefPtr>::iterator it = devices_.begin(); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 131 | it != devices_.end(); ) { |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 132 | if (find(live_devices.begin(), live_devices.end(), it->second->path()) == |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 133 | live_devices.end()) { |
Darin Petkov | 0fcd346 | 2012-05-17 11:25:11 +0200 | [diff] [blame] | 134 | SLOG(WiMax, 2) << "Destroying device: " << it->first; |
| 135 | manager_->device_info()->DeregisterDevice(it->second); |
| 136 | devices_.erase(it++); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 137 | } else { |
| 138 | ++it; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | string WiMaxProvider::GetLinkName(const RpcIdentifier &path) { |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 144 | if (StartsWithASCII(path, wimax_manager::kDeviceObjectPathPrefix, true)) { |
| 145 | return path.substr(strlen(wimax_manager::kDeviceObjectPathPrefix)); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 146 | } |
| 147 | LOG(ERROR) << "Unable to determine link name from RPC path: " << path; |
| 148 | return string(); |
Darin Petkov | b72b62e | 2012-05-15 16:55:36 +0200 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | } // namespace shill |