Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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. |
Darin Petkov | 48a511a | 2011-09-15 10:33:37 -0700 | [diff] [blame] | 21 | const char Modem::kPropertyAccessTechnology[] = "AccessTechnology"; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 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"; |
| 26 | const char Modem::kPropertyUnlockRequired[] = "UnlockRequired"; |
| 27 | const char Modem::kPropertyUnlockRetries[] = "UnlockRetries"; |
| 28 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 29 | Modem::Modem(const std::string &owner, |
| 30 | const std::string &path, |
| 31 | ControlInterface *control_interface, |
| 32 | EventDispatcher *dispatcher, |
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 | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 38 | task_factory_(this), |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 39 | control_interface_(control_interface), |
| 40 | dispatcher_(dispatcher), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame^] | 41 | manager_(manager), |
| 42 | provider_db_(provider_db) { |
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 | |
| 46 | Modem::~Modem() {} |
| 47 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 48 | void Modem::Init() { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 49 | VLOG(2) << __func__; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 50 | CHECK(!device_.get()); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 51 | |
| 52 | // Defer device creation because dbus-c++ doesn't allow registration of new |
| 53 | // D-Bus objects in the context of a D-Bus signal handler. |
| 54 | dispatcher_->PostTask(task_factory_.NewRunnableMethod(&Modem::InitTask)); |
| 55 | } |
| 56 | |
| 57 | void Modem::InitTask() { |
| 58 | VLOG(2) << __func__; |
| 59 | CHECK(!device_.get()); |
| 60 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 61 | dbus_properties_proxy_.reset( |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 62 | proxy_factory_->CreateDBusPropertiesProxy(this, path_, owner_)); |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 63 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 64 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 65 | DBusPropertiesMap properties = |
| 66 | dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE); |
Darin Petkov | a7b8949 | 2011-07-27 12:48:17 -0700 | [diff] [blame] | 67 | CreateCellularDevice(properties); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void Modem::CreateCellularDevice(const DBusPropertiesMap &properties) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 71 | VLOG(2) << __func__; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 72 | uint32 ip_method = kuint32max; |
| 73 | if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) || |
| 74 | ip_method != MM_MODEM_IP_METHOD_DHCP) { |
| 75 | LOG(ERROR) << "Unsupported IP method: " << ip_method; |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | string link_name; |
| 80 | if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name)) { |
| 81 | LOG(ERROR) << "Unable to create cellular device without a link name."; |
| 82 | return; |
| 83 | } |
| 84 | int interface_index = |
| 85 | RTNLHandler::GetInstance()->GetInterfaceIndex(link_name); |
| 86 | if (interface_index < 0) { |
| 87 | LOG(ERROR) << "Unable to create cellular device -- no interface index."; |
| 88 | return; |
| 89 | } |
| 90 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 91 | ByteString address_bytes; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 92 | if (!manager_->device_info()->GetMACAddress(interface_index, |
| 93 | &address_bytes)) { |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 94 | // TODO(petkov): ensure that DeviceInfo has heard about this device before |
| 95 | // we go ahead and try to add it. |
| 96 | LOG(ERROR) << "Unable to create cellular device without a hardware addr."; |
| 97 | return; |
| 98 | } |
| 99 | |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 100 | uint32 mm_type = kuint32max; |
| 101 | Cellular::Type type; |
| 102 | DBusProperties::GetUint32(properties, kPropertyType, &mm_type); |
| 103 | switch (mm_type) { |
| 104 | case MM_MODEM_TYPE_CDMA: |
| 105 | type = Cellular::kTypeCDMA; |
| 106 | break; |
| 107 | case MM_MODEM_TYPE_GSM: |
| 108 | type = Cellular::kTypeGSM; |
| 109 | break; |
| 110 | default: |
| 111 | LOG(ERROR) << "Unsupported cellular modem type: " << mm_type; |
| 112 | return; |
| 113 | } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 114 | |
| 115 | LOG(INFO) << "Creating a cellular device on link " << link_name |
| 116 | << " interface index " << interface_index << "."; |
| 117 | device_ = new Cellular(control_interface_, |
| 118 | dispatcher_, |
| 119 | manager_, |
| 120 | link_name, |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 121 | address_bytes.HexEncode(), |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 122 | interface_index, |
| 123 | type, |
| 124 | owner_, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame^] | 125 | path_, |
| 126 | provider_db_); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 127 | |
| 128 | uint32 modem_state = Cellular::kModemStateUnknown; |
| 129 | DBusProperties::GetUint32(properties, kPropertyState, &modem_state); |
| 130 | device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 131 | |
| 132 | string unlock_required; |
| 133 | if (DBusProperties::GetString( |
| 134 | properties, kPropertyUnlockRequired, &unlock_required)) { |
| 135 | uint32 unlock_retries = 0; |
| 136 | DBusProperties::GetUint32(properties, |
| 137 | kPropertyUnlockRetries, |
| 138 | &unlock_retries); |
Darin Petkov | 48a511a | 2011-09-15 10:33:37 -0700 | [diff] [blame] | 139 | device_->set_sim_lock_status( |
| 140 | Cellular::SimLockStatus(unlock_required, unlock_retries)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 141 | } |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 142 | |
| 143 | manager_->device_info()->RegisterDevice(device_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 146 | void Modem::OnDBusPropertiesChanged( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 147 | const string &/*interface*/, |
| 148 | const DBusPropertiesMap &/*changed_properties*/, |
| 149 | const vector<string> &/*invalidated_properties*/) { |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 150 | // Ignored. |
| 151 | } |
| 152 | |
| 153 | void Modem::OnModemManagerPropertiesChanged( |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 154 | const string &/*interface*/, |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 155 | const DBusPropertiesMap &properties) { |
Darin Petkov | 48a511a | 2011-09-15 10:33:37 -0700 | [diff] [blame] | 156 | if (!device_.get()) { |
| 157 | return; |
| 158 | } |
| 159 | Cellular::SimLockStatus lock_status = device_->sim_lock_status(); |
| 160 | if (DBusProperties::GetString( |
| 161 | properties, kPropertyUnlockRequired, &lock_status.lock_type) || |
| 162 | DBusProperties::GetUint32(properties, |
| 163 | kPropertyUnlockRetries, |
| 164 | &lock_status.retries_left)) { |
| 165 | device_->set_sim_lock_status(lock_status); |
| 166 | } |
| 167 | uint32 access_technology = MM_MODEM_GSM_ACCESS_TECH_UNKNOWN; |
| 168 | if (DBusProperties::GetUint32(properties, |
| 169 | kPropertyAccessTechnology, |
| 170 | &access_technology)) { |
| 171 | device_->SetGSMAccessTechnology(access_technology); |
| 172 | } |
Darin Petkov | c5f5656 | 2011-08-06 16:40:05 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 175 | } // namespace shill |