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> |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 8 | #include <base/stringprintf.h> |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 9 | |
| 10 | #include "shill/cellular.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 11 | #include "shill/logging.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 12 | #include "shill/manager.h" |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 13 | #include "shill/proxy_factory.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 14 | #include "shill/rtnl_handler.h" |
| 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 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 28 | // statics |
| 29 | const char Modem::kFakeDevNameFormat[] = "no_netdev_%zu"; |
Ben Chan | 8b6fe28 | 2013-09-04 01:14:52 -0700 | [diff] [blame] | 30 | const char Modem::kFakeDevAddress[] = "000000000000"; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 31 | const int Modem::kFakeDevInterfaceIndex = -1; |
| 32 | size_t Modem::fake_dev_serial_ = 0; |
| 33 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 34 | Modem::Modem(const string &owner, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 35 | const string &service, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 36 | const string &path, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 37 | ModemInfo * modem_info) |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 38 | : owner_(owner), |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 39 | service_(service), |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 40 | path_(path), |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 41 | modem_info_(modem_info), |
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() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 50 | LOG(INFO) << "Modem destructed: " << owner_ << " at " << path_; |
| 51 | if (device_) { |
| 52 | device_->DestroyService(); |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 53 | modem_info_->manager()->device_info()->DeregisterDevice(device_); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 54 | } |
| 55 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 56 | |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 57 | void Modem::Init() { |
| 58 | dbus_properties_proxy_.reset( |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 59 | proxy_factory_->CreateDBusPropertiesProxy(path(), owner())); |
| 60 | dbus_properties_proxy_->set_modem_manager_properties_changed_callback( |
| 61 | Bind(&Modem::OnModemManagerPropertiesChanged, Unretained(this))); |
| 62 | dbus_properties_proxy_->set_properties_changed_callback( |
| 63 | Bind(&Modem::OnDBusPropertiesChanged, Unretained(this))); |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 66 | void Modem::OnDeviceInfoAvailable(const string &link_name) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 67 | SLOG(Modem, 2) << __func__; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 68 | if (pending_device_info_ && link_name_ == link_name) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 69 | // pending_device_info_ is only set if we've already been through |
| 70 | // CreateDeviceFromModemProperties() and saved our initial |
| 71 | // properties already |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 72 | pending_device_info_ = false; |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 73 | CreateDeviceFromModemProperties(initial_properties_); |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 77 | Cellular *Modem::ConstructCellular(const string &link_name, |
| 78 | const string &address, |
| 79 | int interface_index) { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 80 | LOG(INFO) << "Creating a cellular device on link " << link_name |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 81 | << " interface index " << interface_index << "."; |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 82 | return new Cellular(modem_info_, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 83 | link_name, |
| 84 | address, |
| 85 | interface_index, |
| 86 | type_, |
| 87 | owner_, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 88 | service_, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 89 | path_, |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 90 | ProxyFactory::GetInstance()); |
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( |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 94 | const DBusInterfaceToProperties &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 | } |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 100 | |
| 101 | DBusInterfaceToProperties::const_iterator properties_it = |
| 102 | properties.find(GetModemInterface()); |
| 103 | if (properties_it == properties.end()) { |
| 104 | LOG(ERROR) << "Unable to find modem interface properties."; |
| 105 | return; |
| 106 | } |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 107 | |
| 108 | string mac_address; |
| 109 | int interface_index = -1; |
| 110 | if (GetLinkName(properties_it->second, &link_name_)) { |
| 111 | GetDeviceParams(&mac_address, &interface_index); |
| 112 | if (interface_index < 0) { |
| 113 | LOG(ERROR) << "Unable to create cellular device -- no interface index."; |
| 114 | return; |
| 115 | } |
| 116 | if (mac_address.empty()) { |
| 117 | // Save our properties, wait for OnDeviceInfoAvailable to be called. |
| 118 | LOG(WARNING) |
| 119 | << "No hardware address, device creation pending device info."; |
| 120 | initial_properties_ = properties; |
| 121 | pending_device_info_ = true; |
| 122 | return; |
| 123 | } |
| 124 | // Got the interface index and MAC address. Fall-through to actually |
| 125 | // creating the Cellular object. |
| 126 | } else { |
| 127 | // Probably a PPP dongle. |
| 128 | LOG(INFO) << "Cellular device without link name; assuming PPP dongle."; |
| 129 | link_name_ = base::StringPrintf(kFakeDevNameFormat, fake_dev_serial_++); |
| 130 | mac_address = kFakeDevAddress; |
| 131 | interface_index = kFakeDevInterfaceIndex; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 132 | } |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 133 | |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 134 | if (modem_info_->manager()->device_info()->IsDeviceBlackListed(link_name_)) { |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 135 | LOG(INFO) << "Not creating cellular device for blacklisted interface " |
| 136 | << link_name_ << "."; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 140 | device_ = ConstructCellular(link_name_, mac_address, interface_index); |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 141 | // Give the device a chance to extract any capability-specific properties. |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 142 | for (properties_it = properties.begin(); properties_it != properties.end(); |
| 143 | ++properties_it) { |
| 144 | device_->OnDBusPropertiesChanged( |
| 145 | properties_it->first, properties_it->second, vector<string>()); |
| 146 | } |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 147 | |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 148 | modem_info_->manager()->device_info()->RegisterDevice(device_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 149 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 150 | |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 151 | bool Modem::GetDeviceParams(string *mac_address, int *interface_index) { |
| 152 | // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC |
| 153 | // address below. |
| 154 | *interface_index = rtnl_handler_->GetInterfaceIndex(link_name_); |
| 155 | if (*interface_index < 0) { |
| 156 | return false; |
| 157 | } |
| 158 | |
| 159 | ByteString address_bytes; |
| 160 | if (!modem_info_->manager()->device_info()->GetMACAddress(*interface_index, |
| 161 | &address_bytes)) { |
| 162 | return false; |
| 163 | } |
| 164 | |
| 165 | *mac_address = address_bytes.HexEncode(); |
| 166 | return true; |
| 167 | } |
| 168 | |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 169 | void Modem::OnDBusPropertiesChanged( |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 170 | const string &interface, |
| 171 | const DBusPropertiesMap &changed_properties, |
| 172 | const vector<string> &invalidated_properties) { |
Arman Uguray | f6366ac | 2013-06-12 16:02:28 -0700 | [diff] [blame] | 173 | SLOG(Modem, 2) << __func__; |
| 174 | SLOG(Modem, 3) << "PropertiesChanged signal received."; |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 175 | if (device_.get()) { |
| 176 | device_->OnDBusPropertiesChanged(interface, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 177 | changed_properties, |
| 178 | invalidated_properties); |
| 179 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void Modem::OnModemManagerPropertiesChanged( |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 183 | const string &interface, |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 184 | const DBusPropertiesMap &properties) { |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 185 | vector<string> invalidated_properties; |
| 186 | OnDBusPropertiesChanged(interface, properties, invalidated_properties); |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 189 | } // namespace shill |