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 | |
| 9 | #include "shill/cellular.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 10 | #include "shill/logging.h" |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 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" |
| 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | using base::Bind; |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 16 | using base::Unretained; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 17 | using std::string; |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 18 | using std::vector; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 19 | |
| 20 | namespace shill { |
| 21 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 22 | // TODO(petkov): Consider generating these in mm/mm-modem.h. |
| 23 | const char Modem::kPropertyLinkName[] = "Device"; |
| 24 | const char Modem::kPropertyIPMethod[] = "IpMethod"; |
| 25 | const char Modem::kPropertyType[] = "Type"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 26 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 27 | Modem::Modem(const string &owner, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 28 | const string &service, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 29 | const string &path, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 30 | ModemInfo * modem_info) |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 31 | : owner_(owner), |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 32 | service_(service), |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 33 | path_(path), |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 34 | modem_info_(modem_info), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 35 | type_(Cellular::kTypeInvalid), |
| 36 | pending_device_info_(false), |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 37 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 38 | proxy_factory_(ProxyFactory::GetInstance()) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 39 | LOG(INFO) << "Modem created: " << owner << " at " << path; |
| 40 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 41 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 42 | Modem::~Modem() { |
Darin Petkov | a0a0efe | 2012-06-27 12:50:01 +0200 | [diff] [blame] | 43 | LOG(INFO) << "Modem destructed: " << owner_ << " at " << path_; |
| 44 | if (device_) { |
| 45 | device_->DestroyService(); |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 46 | modem_info_->manager()->device_info()->DeregisterDevice(device_); |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 47 | } |
| 48 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 49 | |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 50 | void Modem::Init() { |
| 51 | dbus_properties_proxy_.reset( |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 52 | proxy_factory_->CreateDBusPropertiesProxy(path(), owner())); |
| 53 | dbus_properties_proxy_->set_modem_manager_properties_changed_callback( |
| 54 | Bind(&Modem::OnModemManagerPropertiesChanged, Unretained(this))); |
| 55 | dbus_properties_proxy_->set_properties_changed_callback( |
| 56 | Bind(&Modem::OnDBusPropertiesChanged, Unretained(this))); |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 59 | void Modem::OnDeviceInfoAvailable(const string &link_name) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 60 | SLOG(Modem, 2) << __func__; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 61 | if (pending_device_info_ && link_name_ == link_name) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 62 | // pending_device_info_ is only set if we've already been through |
| 63 | // CreateDeviceFromModemProperties() and saved our initial |
| 64 | // properties already |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 65 | pending_device_info_ = false; |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 66 | CreateDeviceFromModemProperties(initial_properties_); |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 70 | Cellular *Modem::ConstructCellular(const string &link_name, |
| 71 | const string &address, |
| 72 | int interface_index) { |
| 73 | LOG(INFO) << "Creating a cellular device on link " << link_name_ |
| 74 | << " interface index " << interface_index << "."; |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 75 | return new Cellular(modem_info_, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 76 | link_name, |
| 77 | address, |
| 78 | interface_index, |
| 79 | type_, |
| 80 | owner_, |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 81 | service_, |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 82 | path_, |
Ben Chan | 3ecdf82 | 2012-08-06 12:29:23 -0700 | [diff] [blame] | 83 | ProxyFactory::GetInstance()); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 86 | void Modem::CreateDeviceFromModemProperties( |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 87 | const DBusInterfaceToProperties &properties) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 88 | SLOG(Modem, 2) << __func__; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 89 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 90 | if (device_.get()) { |
| 91 | return; |
| 92 | } |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 93 | |
| 94 | DBusInterfaceToProperties::const_iterator properties_it = |
| 95 | properties.find(GetModemInterface()); |
| 96 | if (properties_it == properties.end()) { |
| 97 | LOG(ERROR) << "Unable to find modem interface properties."; |
| 98 | return; |
| 99 | } |
| 100 | if (!GetLinkName(properties_it->second, &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 | } |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 104 | if (modem_info_->manager()->device_info()->IsDeviceBlackListed(link_name_)) { |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 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; |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 119 | if (!modem_info_->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."; |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 123 | initial_properties_ = 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. |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 132 | for (properties_it = properties.begin(); properties_it != properties.end(); |
| 133 | ++properties_it) { |
| 134 | device_->OnDBusPropertiesChanged( |
| 135 | properties_it->first, properties_it->second, vector<string>()); |
| 136 | } |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 137 | |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 138 | modem_info_->manager()->device_info()->RegisterDevice(device_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 139 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 140 | |
| 141 | void Modem::OnDBusPropertiesChanged( |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 142 | const string &interface, |
| 143 | const DBusPropertiesMap &changed_properties, |
| 144 | const vector<string> &invalidated_properties) { |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 145 | if (device_.get()) { |
| 146 | device_->OnDBusPropertiesChanged(interface, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 147 | changed_properties, |
| 148 | invalidated_properties); |
| 149 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void Modem::OnModemManagerPropertiesChanged( |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 153 | const string &interface, |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 154 | const DBusPropertiesMap &properties) { |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame] | 155 | vector<string> invalidated_properties; |
| 156 | OnDBusPropertiesChanged(interface, properties, invalidated_properties); |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 157 | } |
| 158 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 159 | } // namespace shill |