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> |
| 9 | #include <mm/mm-modem.h> |
| 10 | |
| 11 | #include "shill/cellular.h" |
| 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; |
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"; |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 25 | const char Modem::kPropertyState[] = "State"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 26 | const char Modem::kPropertyType[] = "Type"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 27 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 28 | Modem::Modem(const std::string &owner, |
| 29 | const std::string &path, |
| 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) |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 35 | : proxy_factory_(ProxyFactory::GetInstance()), |
| 36 | owner_(owner), |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 37 | path_(path), |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 38 | control_interface_(control_interface), |
| 39 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 40 | metrics_(metrics), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 41 | manager_(manager), |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 42 | provider_db_(provider_db), |
| 43 | pending_device_info_(false) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 44 | LOG(INFO) << "Modem created: " << owner << " at " << path; |
| 45 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 46 | |
Jason Glasgow | e908949 | 2012-02-23 17:57:37 -0500 | [diff] [blame] | 47 | Modem::~Modem() { |
| 48 | if (device_.get()) { |
| 49 | manager_->device_info()->DeregisterDevice(device_); |
| 50 | } |
| 51 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 52 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 53 | void Modem::Init() { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 54 | VLOG(2) << __func__; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 55 | CHECK(!device_.get()); |
| 56 | dbus_properties_proxy_.reset( |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 57 | proxy_factory_->CreateDBusPropertiesProxy(this, path_, owner_)); |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 58 | CreateDevice(); |
| 59 | } |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 60 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 61 | void Modem::OnDeviceInfoAvailable(const string &link_name) { |
| 62 | VLOG(2) << __func__; |
| 63 | if (pending_device_info_ && link_name_ == link_name) { |
| 64 | pending_device_info_ = false; |
| 65 | CreateDevice(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void Modem::CreateDevice() { |
| 70 | VLOG(2) << __func__; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 71 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 72 | DBusPropertiesMap properties = |
| 73 | dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE); |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 74 | CreateDeviceFromProperties(properties); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 77 | void Modem::CreateDeviceFromProperties(const DBusPropertiesMap &properties) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 78 | VLOG(2) << __func__; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 79 | if (device_.get()) { |
| 80 | return; |
| 81 | } |
| 82 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 83 | uint32 ip_method = kuint32max; |
| 84 | if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) || |
| 85 | ip_method != MM_MODEM_IP_METHOD_DHCP) { |
| 86 | LOG(ERROR) << "Unsupported IP method: " << ip_method; |
| 87 | return; |
| 88 | } |
| 89 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 90 | if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name_)) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 91 | LOG(ERROR) << "Unable to create cellular device without a link name."; |
| 92 | return; |
| 93 | } |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 94 | // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC |
| 95 | // address below. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 96 | int interface_index = |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 97 | RTNLHandler::GetInstance()->GetInterfaceIndex(link_name_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 98 | if (interface_index < 0) { |
| 99 | LOG(ERROR) << "Unable to create cellular device -- no interface index."; |
| 100 | return; |
| 101 | } |
| 102 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 103 | ByteString address_bytes; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 104 | if (!manager_->device_info()->GetMACAddress(interface_index, |
| 105 | &address_bytes)) { |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 106 | LOG(WARNING) << "No hardware address, device creation pending device info."; |
| 107 | pending_device_info_ = true; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 108 | return; |
| 109 | } |
| 110 | |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 111 | uint32 mm_type = kuint32max; |
| 112 | Cellular::Type type; |
| 113 | DBusProperties::GetUint32(properties, kPropertyType, &mm_type); |
| 114 | switch (mm_type) { |
| 115 | case MM_MODEM_TYPE_CDMA: |
| 116 | type = Cellular::kTypeCDMA; |
| 117 | break; |
| 118 | case MM_MODEM_TYPE_GSM: |
| 119 | type = Cellular::kTypeGSM; |
| 120 | break; |
| 121 | default: |
| 122 | LOG(ERROR) << "Unsupported cellular modem type: " << mm_type; |
| 123 | return; |
| 124 | } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 125 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 126 | LOG(INFO) << "Creating a cellular device on link " << link_name_ |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 127 | << " interface index " << interface_index << "."; |
| 128 | device_ = new Cellular(control_interface_, |
| 129 | dispatcher_, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 130 | metrics_, |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 131 | manager_, |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 132 | link_name_, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 133 | address_bytes.HexEncode(), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 134 | interface_index, |
| 135 | type, |
| 136 | owner_, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 137 | path_, |
| 138 | provider_db_); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 139 | |
| 140 | uint32 modem_state = Cellular::kModemStateUnknown; |
| 141 | DBusProperties::GetUint32(properties, kPropertyState, &modem_state); |
| 142 | device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 143 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 144 | // Give the device a chance to extract any capability-specific properties. |
| 145 | device_->OnModemManagerPropertiesChanged(properties); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 146 | |
| 147 | manager_->device_info()->RegisterDevice(device_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 150 | void Modem::OnDBusPropertiesChanged( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 151 | const string &/*interface*/, |
| 152 | const DBusPropertiesMap &/*changed_properties*/, |
| 153 | const vector<string> &/*invalidated_properties*/) { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 154 | // Ignored. |
| 155 | } |
| 156 | |
| 157 | void Modem::OnModemManagerPropertiesChanged( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 158 | const string &/*interface*/, |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 159 | const DBusPropertiesMap &properties) { |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 160 | if (device_.get()) { |
| 161 | device_->OnModemManagerPropertiesChanged(properties); |
Darin Petkov | 48a511a | 2011-09-15 10:33:37 -0700 | [diff] [blame] | 162 | } |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 165 | } // namespace shill |