Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 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/modem.h" |
| 6 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/bind.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 8 | #include <base/logging.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 9 | |
| 10 | #include "shill/cellular.h" |
| 11 | #include "shill/manager.h" |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 12 | #include "shill/proxy_factory.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 13 | #include "shill/rtnl_handler.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 14 | #include "shill/scope_logger.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 15 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 16 | using base::Bind; |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame^] | 17 | using base::Unretained; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 18 | using std::string; |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 19 | using std::vector; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 20 | |
| 21 | namespace shill { |
| 22 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 23 | // TODO(petkov): Consider generating these in mm/mm-modem.h. |
| 24 | const char Modem::kPropertyLinkName[] = "Device"; |
| 25 | const char Modem::kPropertyIPMethod[] = "IpMethod"; |
| 26 | const char Modem::kPropertyType[] = "Type"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 27 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 28 | Modem::Modem(const string &owner, |
| 29 | const string &path, |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 30 | ControlInterface *control_interface, |
| 31 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 32 | Metrics *metrics, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 33 | Manager *manager, |
| 34 | mobile_provider_db *provider_db) |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 35 | : owner_(owner), |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 36 | path_(path), |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 37 | control_interface_(control_interface), |
| 38 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 39 | metrics_(metrics), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 40 | manager_(manager), |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 41 | provider_db_(provider_db), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 42 | type_(Cellular::kTypeInvalid), |
| 43 | pending_device_info_(false), |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 44 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 45 | proxy_factory_(ProxyFactory::GetInstance()) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 46 | LOG(INFO) << "Modem created: " << owner << " at " << path; |
| 47 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 48 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 49 | Modem::~Modem() { |
| 50 | if (device_.get()) { |
| 51 | manager_->device_info()->DeregisterDevice(device_); |
| 52 | } |
| 53 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 54 | |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 55 | void Modem::Init() { |
| 56 | dbus_properties_proxy_.reset( |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame^] | 57 | proxy_factory_->CreateDBusPropertiesProxy(path(), owner())); |
| 58 | dbus_properties_proxy_->set_modem_manager_properties_changed_callback( |
| 59 | Bind(&Modem::OnModemManagerPropertiesChanged, Unretained(this))); |
| 60 | dbus_properties_proxy_->set_properties_changed_callback( |
| 61 | Bind(&Modem::OnDBusPropertiesChanged, Unretained(this))); |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 64 | void Modem::OnDeviceInfoAvailable(const string &link_name) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 65 | SLOG(Modem, 2) << __func__; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 66 | if (pending_device_info_ && link_name_ == link_name) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 67 | // pending_device_info_ is only set if we've already been through |
| 68 | // CreateDeviceFromModemProperties() and saved our initial |
| 69 | // properties already |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 70 | pending_device_info_ = false; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 71 | CreateDeviceFromModemProperties(initial_modem_properties_); |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 75 | Cellular *Modem::ConstructCellular(const string &link_name, |
| 76 | const string &address, |
| 77 | int interface_index) { |
| 78 | LOG(INFO) << "Creating a cellular device on link " << link_name_ |
| 79 | << " interface index " << interface_index << "."; |
| 80 | return new Cellular(control_interface_, |
| 81 | dispatcher_, |
| 82 | metrics_, |
| 83 | manager_, |
| 84 | link_name, |
| 85 | address, |
| 86 | interface_index, |
| 87 | type_, |
| 88 | owner_, |
| 89 | path_, |
| 90 | provider_db_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 93 | void Modem::CreateDeviceFromModemProperties( |
| 94 | const DBusPropertiesMap &modem_properties) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 95 | SLOG(Modem, 2) << __func__; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 96 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 97 | if (device_.get()) { |
| 98 | return; |
| 99 | } |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 100 | if (!GetLinkName(modem_properties, &link_name_)) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 101 | LOG(ERROR) << "Unable to create cellular device without a link name."; |
| 102 | return; |
| 103 | } |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 104 | if (manager_->device_info()->IsDeviceBlackListed(link_name_)) { |
| 105 | LOG(INFO) << "Do not create cellular device for blacklisted interface " |
| 106 | << link_name_; |
| 107 | return; |
| 108 | } |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 109 | // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC |
| 110 | // address below. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 111 | int interface_index = |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 112 | rtnl_handler_->GetInterfaceIndex(link_name_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 113 | if (interface_index < 0) { |
| 114 | LOG(ERROR) << "Unable to create cellular device -- no interface index."; |
| 115 | return; |
| 116 | } |
| 117 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 118 | ByteString address_bytes; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 119 | if (!manager_->device_info()->GetMACAddress(interface_index, |
| 120 | &address_bytes)) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 121 | // Save our properties, wait for OnDeviceInfoAvailable to be called. |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 122 | LOG(WARNING) << "No hardware address, device creation pending device info."; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 123 | initial_modem_properties_ = modem_properties; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 124 | pending_device_info_ = true; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 128 | string address = address_bytes.HexEncode(); |
| 129 | device_ = ConstructCellular(link_name_, address, interface_index); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 130 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 131 | // Give the device a chance to extract any capability-specific properties. |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 132 | device_->OnDBusPropertiesChanged(GetModemInterface(), modem_properties, |
| 133 | vector<string>()); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 134 | |
| 135 | manager_->device_info()->RegisterDevice(device_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 136 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 137 | |
| 138 | void Modem::OnDBusPropertiesChanged( |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 139 | const string &interface, |
| 140 | const DBusPropertiesMap &changed_properties, |
| 141 | const vector<string> &invalidated_properties) { |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 142 | if (device_.get()) { |
| 143 | device_->OnDBusPropertiesChanged(interface, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 144 | changed_properties, |
| 145 | invalidated_properties); |
| 146 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | void Modem::OnModemManagerPropertiesChanged( |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 150 | const string &interface, |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 151 | const DBusPropertiesMap &properties) { |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 152 | vector<string> invalidated_properties; |
| 153 | OnDBusPropertiesChanged(interface, properties, invalidated_properties); |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 156 | } // namespace shill |