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