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" |
| 14 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 15 | using base::Bind; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 16 | using std::string; |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 17 | using std::vector; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 18 | |
| 19 | namespace shill { |
| 20 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 21 | // TODO(petkov): Consider generating these in mm/mm-modem.h. |
| 22 | const char Modem::kPropertyLinkName[] = "Device"; |
| 23 | const char Modem::kPropertyIPMethod[] = "IpMethod"; |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 24 | const char Modem::kPropertyState[] = "State"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 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, |
| 28 | const string &path, |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 29 | ControlInterface *control_interface, |
| 30 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 31 | Metrics *metrics, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 32 | Manager *manager, |
| 33 | mobile_provider_db *provider_db) |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 34 | : owner_(owner), |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 35 | path_(path), |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 36 | control_interface_(control_interface), |
| 37 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 38 | metrics_(metrics), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 39 | manager_(manager), |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 40 | provider_db_(provider_db), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 41 | type_(Cellular::kTypeInvalid), |
| 42 | pending_device_info_(false), |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 43 | rtnl_handler_(RTNLHandler::GetInstance()), |
| 44 | proxy_factory_(ProxyFactory::GetInstance()) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 45 | LOG(INFO) << "Modem created: " << owner << " at " << path; |
| 46 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 47 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 48 | Modem::~Modem() { |
| 49 | if (device_.get()) { |
| 50 | manager_->device_info()->DeregisterDevice(device_); |
| 51 | } |
| 52 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 53 | |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 54 | void Modem::Init() { |
| 55 | dbus_properties_proxy_.reset( |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 56 | proxy_factory_->CreateDBusPropertiesProxy(this, path(), owner())); |
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) { |
| 60 | VLOG(2) << __func__; |
| 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; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 66 | CreateDeviceFromModemProperties(initial_modem_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 << "."; |
| 75 | return new Cellular(control_interface_, |
| 76 | dispatcher_, |
| 77 | metrics_, |
| 78 | manager_, |
| 79 | link_name, |
| 80 | address, |
| 81 | interface_index, |
| 82 | type_, |
| 83 | owner_, |
| 84 | path_, |
| 85 | provider_db_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 86 | } |
| 87 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 88 | void Modem::CreateDeviceFromModemProperties( |
| 89 | const DBusPropertiesMap &modem_properties) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 90 | VLOG(2) << __func__; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 91 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 92 | if (device_.get()) { |
| 93 | return; |
| 94 | } |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 95 | if (!GetLinkName(modem_properties, &link_name_)) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 96 | LOG(ERROR) << "Unable to create cellular device without a link name."; |
| 97 | return; |
| 98 | } |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 99 | if (manager_->device_info()->IsDeviceBlackListed(link_name_)) { |
| 100 | LOG(INFO) << "Do not create cellular device for blacklisted interface " |
| 101 | << link_name_; |
| 102 | return; |
| 103 | } |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 104 | // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC |
| 105 | // address below. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 106 | int interface_index = |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 107 | rtnl_handler_->GetInterfaceIndex(link_name_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 108 | if (interface_index < 0) { |
| 109 | LOG(ERROR) << "Unable to create cellular device -- no interface index."; |
| 110 | return; |
| 111 | } |
| 112 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 113 | ByteString address_bytes; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 114 | if (!manager_->device_info()->GetMACAddress(interface_index, |
| 115 | &address_bytes)) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 116 | // Save our properties, wait for OnDeviceInfoAvailable to be called. |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 117 | LOG(WARNING) << "No hardware address, device creation pending device info."; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 118 | initial_modem_properties_ = modem_properties; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 119 | pending_device_info_ = true; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 120 | return; |
| 121 | } |
| 122 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 123 | string address = address_bytes.HexEncode(); |
| 124 | device_ = ConstructCellular(link_name_, address, interface_index); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 125 | |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 126 | // Note: 0 happens to map to Cellular::kModemStateUnknown for all |
| 127 | // types of modems |
| 128 | uint32 modem_state = 0; |
| 129 | // TODO(jglasgow): refactor to avoid explicit if on modem type. The |
| 130 | // ModemManager1 interface type is an "i" not a "u". Handle that |
| 131 | // during refactoring. |
| 132 | if (type_ != Cellular::kTypeUniversal) |
| 133 | DBusProperties::GetUint32(modem_properties, kPropertyState, &modem_state); |
| 134 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 135 | device_->set_modem_state(ConvertMmToCellularModemState(modem_state)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 136 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 137 | // Give the device a chance to extract any capability-specific properties. |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame^] | 138 | device_->OnDBusPropertiesChanged(GetModemInterface(), modem_properties, |
| 139 | vector<string>()); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 140 | |
| 141 | manager_->device_info()->RegisterDevice(device_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 142 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 143 | |
| 144 | void Modem::OnDBusPropertiesChanged( |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 145 | const string &interface, |
| 146 | const DBusPropertiesMap &changed_properties, |
| 147 | const vector<string> &invalidated_properties) { |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame^] | 148 | if (device_.get()) { |
| 149 | device_->OnDBusPropertiesChanged(interface, |
Jason Glasgow | 82f9ab3 | 2012-04-04 14:27:19 -0400 | [diff] [blame] | 150 | changed_properties, |
| 151 | invalidated_properties); |
| 152 | } |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void Modem::OnModemManagerPropertiesChanged( |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame^] | 156 | const string &interface, |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 157 | const DBusPropertiesMap &properties) { |
Jason Glasgow | 4c0724a | 2012-04-17 15:47:40 -0400 | [diff] [blame^] | 158 | vector<string> invalidated_properties; |
| 159 | OnDBusPropertiesChanged(interface, properties, invalidated_properties); |
Eric Shienbrood | 11567d0 | 2012-04-10 18:08:49 -0400 | [diff] [blame] | 160 | } |
| 161 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 162 | } // namespace shill |