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