Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 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 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 5 | #include "shill/device.h" |
| 6 | |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 7 | #include <netinet/in.h> |
| 8 | #include <linux/if.h> // Needs definitions from netinet/in.h |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 9 | #include <stdio.h> |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 10 | #include <time.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 11 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 12 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 13 | #include <vector> |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 14 | |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 15 | #include <base/logging.h> |
Chris Masone | 487b8bf | 2011-05-13 16:27:57 -0700 | [diff] [blame] | 16 | #include <base/memory/ref_counted.h> |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 17 | #include <base/stringprintf.h> |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 18 | #include <chromeos/dbus/service_constants.h> |
Chris Masone | ee929b7 | 2011-05-10 10:02:18 -0700 | [diff] [blame] | 19 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 20 | #include "shill/connection.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 21 | #include "shill/control_interface.h" |
Chris Masone | d7732e4 | 2011-05-20 11:08:56 -0700 | [diff] [blame] | 22 | #include "shill/device_dbus_adaptor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 23 | #include "shill/dhcp_config.h" |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 24 | #include "shill/dhcp_provider.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 25 | #include "shill/error.h" |
| 26 | #include "shill/manager.h" |
Chris Masone | 95207da | 2011-06-29 16:50:49 -0700 | [diff] [blame] | 27 | #include "shill/property_accessor.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 28 | #include "shill/refptr_types.h" |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 29 | #include "shill/rtnl_handler.h" |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 30 | #include "shill/service.h" |
Chris Masone | 0e1d104 | 2011-05-09 18:07:03 -0700 | [diff] [blame] | 31 | #include "shill/shill_event.h" |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 32 | #include "shill/store_interface.h" |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 33 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 34 | using base::StringPrintf; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 35 | using std::string; |
| 36 | using std::vector; |
| 37 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 38 | namespace shill { |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 39 | |
| 40 | // static |
| 41 | const char Device::kStoragePowered[] = "Powered"; |
| 42 | |
| 43 | // static |
| 44 | const char Device::kStorageIPConfigs[] = "IPConfigs"; |
| 45 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 46 | Device::Device(ControlInterface *control_interface, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 47 | EventDispatcher *dispatcher, |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 48 | Manager *manager, |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 49 | const string &link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 50 | const string &address, |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 51 | int interface_index) |
Chris Masone | 853b81b | 2011-06-24 14:11:41 -0700 | [diff] [blame] | 52 | : powered_(true), |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 53 | reconnect_(true), |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 54 | hardware_address_(address), |
mukesh agrawal | f60e406 | 2011-05-27 13:13:41 -0700 | [diff] [blame] | 55 | interface_index_(interface_index), |
| 56 | running_(false), |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 57 | link_name_(link_name), |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 58 | unique_id_(link_name), |
Darin Petkov | d966195 | 2011-08-03 16:25:42 -0700 | [diff] [blame] | 59 | control_interface_(control_interface), |
| 60 | dispatcher_(dispatcher), |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 61 | manager_(manager), |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 62 | adaptor_(control_interface->CreateDeviceAdaptor(this)), |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 63 | dhcp_provider_(DHCPProvider::GetInstance()), |
| 64 | rtnl_handler_(RTNLHandler::GetInstance()) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 65 | store_.RegisterConstString(flimflam::kAddressProperty, &hardware_address_); |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 66 | |
| 67 | // flimflam::kBgscanMethodProperty: Registered in WiFi |
| 68 | // flimflam::kBgscanShortIntervalProperty: Registered in WiFi |
| 69 | // flimflam::kBgscanSignalThresholdProperty: Registered in WiFi |
| 70 | |
| 71 | // flimflam::kCellularAllowRoamingProperty: Registered in Cellular |
| 72 | // flimflam::kCarrierProperty: Registered in Cellular |
| 73 | // flimflam::kEsnProperty: Registered in Cellular |
Darin Petkov | 3335b37 | 2011-08-22 11:05:32 -0700 | [diff] [blame] | 74 | // flimflam::kHomeProviderProperty: Registered in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 75 | // flimflam::kImeiProperty: Registered in Cellular |
| 76 | // flimflam::kImsiProperty: Registered in Cellular |
| 77 | // flimflam::kManufacturerProperty: Registered in Cellular |
| 78 | // flimflam::kMdnProperty: Registered in Cellular |
| 79 | // flimflam::kMeidProperty: Registered in Cellular |
| 80 | // flimflam::kMinProperty: Registered in Cellular |
| 81 | // flimflam::kModelIDProperty: Registered in Cellular |
| 82 | // flimflam::kFirmwareRevisionProperty: Registered in Cellular |
| 83 | // flimflam::kHardwareRevisionProperty: Registered in Cellular |
| 84 | // flimflam::kPRLVersionProperty: Registered in Cellular |
| 85 | // flimflam::kSIMLockStatusProperty: Registered in Cellular |
| 86 | // flimflam::kFoundNetworksProperty: Registered in Cellular |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 87 | // flimflam::kDBusConnectionProperty: Registered in Cellular |
| 88 | // flimflam::kDBusObjectProperty: Register in Cellular |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 89 | |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 90 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 91 | // store_.RegisterConstString(flimflam::kInterfaceProperty, &link_name_); |
| 92 | HelpRegisterDerivedStrings(flimflam::kIPConfigsProperty, |
| 93 | &Device::AvailableIPConfigs, |
| 94 | NULL); |
| 95 | store_.RegisterConstString(flimflam::kNameProperty, &link_name_); |
| 96 | store_.RegisterBool(flimflam::kPoweredProperty, &powered_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 97 | // TODO(cmasone): Chrome doesn't use this...does anyone? |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 98 | // store_.RegisterConstBool(flimflam::kReconnectProperty, &reconnect_); |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 99 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 100 | // TODO(cmasone): Figure out what shill concept maps to flimflam's "Network". |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 101 | // known_properties_.push_back(flimflam::kNetworksProperty); |
| 102 | |
Chris Masone | 4d42df8 | 2011-07-02 17:09:39 -0700 | [diff] [blame] | 103 | // flimflam::kScanningProperty: Registered in WiFi, Cellular |
| 104 | // flimflam::kScanIntervalProperty: Registered in WiFi, Cellular |
| 105 | |
| 106 | // TODO(pstew): Initialize Interface monitor, so we can detect new interfaces |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 107 | VLOG(2) << "Device " << link_name_ << " index " << interface_index; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | Device::~Device() { |
Paul Stewart | b50f0b9 | 2011-05-16 16:31:42 -0700 | [diff] [blame] | 111 | VLOG(2) << "Device " << link_name_ << " destroyed."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void Device::Start() { |
| 115 | running_ = true; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 116 | VLOG(2) << "Device " << link_name_ << " starting."; |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 117 | adaptor_->UpdateEnabled(); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void Device::Stop() { |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 121 | VLOG(2) << "Device " << link_name_ << " stopping."; |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 122 | running_ = false; |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 123 | DestroyIPConfig(); // breaks a reference cycle |
| 124 | SelectService(NULL); // breaks a reference cycle |
Chris Masone | 413a319 | 2011-05-09 17:10:05 -0700 | [diff] [blame] | 125 | adaptor_->UpdateEnabled(); |
mukesh agrawal | 5c4dd0b | 2011-09-14 13:53:14 -0700 | [diff] [blame] | 126 | rtnl_handler_->SetInterfaceFlags(interface_index(), 0, IFF_UP); |
| 127 | |
| 128 | VLOG(3) << "Device " << link_name_ << " has " << services_.size() |
| 129 | << " remaining services."; |
| 130 | VLOG(3) << "Device " << link_name_ << " ipconfig_ " |
| 131 | << (ipconfig_.get() ? "is set." : "is not set."); |
| 132 | VLOG(3) << "Device " << link_name_ << " connection_ " |
| 133 | << (connection_.get() ? "is set." : "is not set."); |
| 134 | VLOG(3) << "Device " << link_name_ << " selected_service_ " |
| 135 | << (selected_service_.get() ? "is set." : "is not set."); |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 136 | } |
| 137 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 138 | bool Device::TechnologyIs(const Technology::Identifier /*type*/) const { |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 139 | return false; |
| 140 | } |
| 141 | |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 142 | void Device::LinkEvent(unsigned flags, unsigned change) { |
mukesh agrawal | 47009f8 | 2011-08-25 14:07:35 -0700 | [diff] [blame] | 143 | VLOG(2) << "Device " << link_name_ |
| 144 | << std::showbase << std::hex |
| 145 | << " flags " << flags << " changed " << change |
| 146 | << std::dec << std::noshowbase; |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 149 | void Device::Scan(Error *error) { |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 150 | VLOG(2) << "Device " << link_name_ << " scan requested."; |
Darin Petkov | c086531 | 2011-09-16 15:31:20 -0700 | [diff] [blame] | 151 | const string kMessage = "Device doesn't support scan."; |
| 152 | LOG(ERROR) << kMessage; |
| 153 | CHECK(error); |
| 154 | error->Populate(Error::kNotSupported, kMessage); |
Paul Stewart | f1ce5d2 | 2011-05-19 13:10:20 -0700 | [diff] [blame] | 155 | } |
| 156 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 157 | void Device::RegisterOnNetwork(const std::string &/*network_id*/, |
| 158 | Error *error) { |
Darin Petkov | 9ae310f | 2011-08-30 15:41:13 -0700 | [diff] [blame] | 159 | const string kMessage = "Device doesn't support network registration."; |
| 160 | LOG(ERROR) << kMessage; |
| 161 | CHECK(error); |
| 162 | error->Populate(Error::kNotSupported, kMessage); |
| 163 | } |
| 164 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 165 | void Device::RequirePIN(const string &/*pin*/, bool /*require*/, Error *error) { |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 166 | const string kMessage = "Device doesn't support RequirePIN."; |
| 167 | LOG(ERROR) << kMessage; |
| 168 | CHECK(error); |
| 169 | error->Populate(Error::kNotSupported, kMessage); |
| 170 | } |
| 171 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 172 | void Device::EnterPIN(const string &/*pin*/, Error *error) { |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 173 | const string kMessage = "Device doesn't support EnterPIN."; |
| 174 | LOG(ERROR) << kMessage; |
| 175 | CHECK(error); |
| 176 | error->Populate(Error::kNotSupported, kMessage); |
| 177 | } |
| 178 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 179 | void Device::UnblockPIN(const string &/*unblock_code*/, |
| 180 | const string &/*pin*/, |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 181 | Error *error) { |
| 182 | const string kMessage = "Device doesn't support UnblockPIN."; |
| 183 | LOG(ERROR) << kMessage; |
| 184 | CHECK(error); |
| 185 | error->Populate(Error::kNotSupported, kMessage); |
| 186 | } |
| 187 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 188 | void Device::ChangePIN(const string &/*old_pin*/, |
| 189 | const string &/*new_pin*/, |
Darin Petkov | e42e101 | 2011-08-31 12:35:04 -0700 | [diff] [blame] | 190 | Error *error) { |
| 191 | const string kMessage = "Device doesn't support ChangePIN."; |
| 192 | LOG(ERROR) << kMessage; |
| 193 | CHECK(error); |
| 194 | error->Populate(Error::kNotSupported, kMessage); |
| 195 | } |
| 196 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 197 | string Device::GetRpcIdentifier() { |
| 198 | return adaptor_->GetRpcIdentifier(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 201 | string Device::GetStorageIdentifier() { |
| 202 | string id = GetRpcIdentifier(); |
| 203 | ControlInterface::RpcIdToStorageId(&id); |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 204 | size_t needle = id.find('_'); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 205 | DLOG_IF(ERROR, needle == string::npos) << "No _ in storage id?!?!"; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 206 | id.replace(id.begin() + needle + 1, id.end(), hardware_address_); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 207 | return id; |
| 208 | } |
| 209 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 210 | const string& Device::FriendlyName() const { |
Chris Masone | 7df0c67 | 2011-07-15 10:24:54 -0700 | [diff] [blame] | 211 | return link_name_; |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Chris Masone | 19e3040 | 2011-07-19 15:48:47 -0700 | [diff] [blame] | 214 | const string& Device::UniqueName() const { |
| 215 | return unique_id_; |
| 216 | } |
| 217 | |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 218 | bool Device::Load(StoreInterface *storage) { |
| 219 | const string id = GetStorageIdentifier(); |
| 220 | if (!storage->ContainsGroup(id)) { |
| 221 | LOG(WARNING) << "Device is not available in the persistent store: " << id; |
| 222 | return false; |
| 223 | } |
| 224 | storage->GetBool(id, kStoragePowered, &powered_); |
| 225 | // TODO(cmasone): What does it mean to load an IPConfig identifier?? |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | bool Device::Save(StoreInterface *storage) { |
| 230 | const string id = GetStorageIdentifier(); |
| 231 | storage->SetBool(id, kStoragePowered, powered_); |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 232 | if (ipconfig_.get()) { |
| 233 | // The _0 is an index into the list of IPConfigs that this device might |
| 234 | // have. We only have one IPConfig right now, and I hope to never have |
| 235 | // to support more, as sleffler indicates that associating IPConfigs |
| 236 | // with devices is wrong and due to be changed in flimflam anyhow. |
| 237 | string suffix = hardware_address_ + "_0"; |
| 238 | ipconfig_->Save(storage, suffix); |
| 239 | storage->SetString(id, kStorageIPConfigs, SerializeIPConfigs(suffix)); |
| 240 | } |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 241 | return true; |
| 242 | } |
| 243 | |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 244 | void Device::DestroyIPConfig() { |
| 245 | if (ipconfig_.get()) { |
| 246 | ipconfig_->ReleaseIP(); |
| 247 | ipconfig_ = NULL; |
| 248 | } |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 249 | DestroyConnection(); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | bool Device::AcquireDHCPConfig() { |
| 253 | DestroyIPConfig(); |
Darin Petkov | 77cb681 | 2011-08-15 16:19:41 -0700 | [diff] [blame] | 254 | ipconfig_ = dhcp_provider_->CreateConfig(link_name_); |
Darin Petkov | afa6fc4 | 2011-06-21 16:21:08 -0700 | [diff] [blame] | 255 | ipconfig_->RegisterUpdateCallback( |
| 256 | NewCallback(this, &Device::IPConfigUpdatedCallback)); |
| 257 | return ipconfig_->RequestIP(); |
| 258 | } |
| 259 | |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 260 | void Device::HelpRegisterDerivedStrings(const string &name, |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 261 | Strings(Device::*get)(void), |
| 262 | bool(Device::*set)(const Strings&)) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 263 | store_.RegisterDerivedStrings( |
| 264 | name, |
| 265 | StringsAccessor(new CustomAccessor<Device, Strings>(this, get, set))); |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Chris Masone | 2b10554 | 2011-06-22 10:58:09 -0700 | [diff] [blame] | 268 | void Device::IPConfigUpdatedCallback(const IPConfigRefPtr &ipconfig, |
| 269 | bool success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 270 | VLOG(2) << __func__ << " " << " success: " << success; |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 271 | if (success) { |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 272 | CreateConnection(); |
| 273 | connection_->UpdateFromIPConfig(ipconfig); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 274 | SetServiceState(Service::kStateConnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 275 | } else { |
| 276 | // TODO(pstew): This logic gets more complex when multiple IPConfig types |
| 277 | // are run in parallel (e.g. DHCP and DHCP6) |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 278 | SetServiceState(Service::kStateDisconnected); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 279 | DestroyConnection(); |
Paul Stewart | c39f113 | 2011-06-22 12:02:28 -0700 | [diff] [blame] | 280 | } |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 283 | void Device::CreateConnection() { |
| 284 | VLOG(2) << __func__; |
| 285 | if (!connection_.get()) { |
Paul Stewart | 9a90808 | 2011-08-31 12:18:48 -0700 | [diff] [blame] | 286 | connection_ = new Connection(interface_index_, |
| 287 | link_name_, |
| 288 | manager_->device_info()); |
Paul Stewart | e613202 | 2011-08-16 09:11:02 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | void Device::DestroyConnection() { |
| 293 | VLOG(2) << __func__; |
| 294 | connection_ = NULL; |
| 295 | } |
| 296 | |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 297 | void Device::SelectService(const ServiceRefPtr &service) { |
Paul Stewart | a2b8cd1 | 2011-08-25 14:55:32 -0700 | [diff] [blame] | 298 | VLOG(2) << __func__ << ": " |
| 299 | << (service.get() ? service->UniqueName() : "*reset*"); |
Paul Stewart | 03dba0b | 2011-08-22 16:32:45 -0700 | [diff] [blame] | 300 | if (selected_service_.get() && |
| 301 | selected_service_->state() != Service::kStateFailure) { |
| 302 | selected_service_->SetState(Service::kStateIdle); |
| 303 | } |
| 304 | selected_service_ = service; |
| 305 | } |
| 306 | |
| 307 | void Device::SetServiceState(Service::ConnectState state) { |
| 308 | if (selected_service_.get()) { |
| 309 | selected_service_->SetState(state); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | void Device::SetServiceFailure(Service::ConnectFailure failure_state) { |
| 314 | if (selected_service_.get()) { |
| 315 | selected_service_->SetFailure(failure_state); |
| 316 | } |
| 317 | } |
| 318 | |
Chris Masone | 34af218 | 2011-08-22 11:59:36 -0700 | [diff] [blame] | 319 | string Device::SerializeIPConfigs(const string &suffix) { |
| 320 | return StringPrintf("%s:%s", suffix.c_str(), ipconfig_->type().c_str()); |
Chris Masone | 5dec5f4 | 2011-07-22 14:07:55 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Chris Masone | 4e85161 | 2011-07-01 10:46:53 -0700 | [diff] [blame] | 323 | vector<string> Device::AvailableIPConfigs() { |
| 324 | string id = (ipconfig_.get() ? ipconfig_->GetRpcIdentifier() : string()); |
| 325 | return vector<string>(1, id); |
| 326 | } |
| 327 | |
| 328 | string Device::GetRpcConnectionIdentifier() { |
| 329 | return adaptor_->GetRpcConnectionIdentifier(); |
| 330 | } |
| 331 | |
Paul Stewart | 75897df | 2011-04-27 09:05:53 -0700 | [diff] [blame] | 332 | } // namespace shill |