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), |
| 43 | rtnl_handler_(RTNLHandler::GetInstance()) { |
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 | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 53 | void Modem::OnDeviceInfoAvailable(const string &link_name) { |
| 54 | VLOG(2) << __func__; |
| 55 | if (pending_device_info_ && link_name_ == link_name) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 56 | // pending_device_info_ is only set if we've already been through |
| 57 | // CreateDeviceFromModemProperties() and saved our initial |
| 58 | // properties already |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 59 | pending_device_info_ = false; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 60 | CreateDeviceFromModemProperties(initial_modem_properties_); |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 64 | Cellular *Modem::ConstructCellular(const string &link_name, |
| 65 | const string &address, |
| 66 | int interface_index) { |
| 67 | LOG(INFO) << "Creating a cellular device on link " << link_name_ |
| 68 | << " interface index " << interface_index << "."; |
| 69 | return new Cellular(control_interface_, |
| 70 | dispatcher_, |
| 71 | metrics_, |
| 72 | manager_, |
| 73 | link_name, |
| 74 | address, |
| 75 | interface_index, |
| 76 | type_, |
| 77 | owner_, |
| 78 | path_, |
| 79 | provider_db_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 80 | } |
| 81 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 82 | void Modem::CreateDeviceFromModemProperties( |
| 83 | const DBusPropertiesMap &modem_properties) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 84 | VLOG(2) << __func__; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 85 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 86 | if (device_.get()) { |
| 87 | return; |
| 88 | } |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 89 | if (!GetLinkName(modem_properties, &link_name_)) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 90 | LOG(ERROR) << "Unable to create cellular device without a link name."; |
| 91 | return; |
| 92 | } |
Eric Shienbrood | 5e628a5 | 2012-03-21 16:56:59 -0400 | [diff] [blame] | 93 | if (manager_->device_info()->IsDeviceBlackListed(link_name_)) { |
| 94 | LOG(INFO) << "Do not create cellular device for blacklisted interface " |
| 95 | << link_name_; |
| 96 | return; |
| 97 | } |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 98 | // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC |
| 99 | // address below. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 100 | int interface_index = |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 101 | rtnl_handler_->GetInterfaceIndex(link_name_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 102 | if (interface_index < 0) { |
| 103 | LOG(ERROR) << "Unable to create cellular device -- no interface index."; |
| 104 | return; |
| 105 | } |
| 106 | |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 107 | ByteString address_bytes; |
Paul Stewart | 3285296 | 2011-08-30 14:06:53 -0700 | [diff] [blame] | 108 | if (!manager_->device_info()->GetMACAddress(interface_index, |
| 109 | &address_bytes)) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 110 | // Save our properties, wait for OnDeviceInfoAvailable to be called. |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 111 | LOG(WARNING) << "No hardware address, device creation pending device info."; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 112 | initial_modem_properties_ = modem_properties; |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 113 | pending_device_info_ = true; |
Chris Masone | 626719f | 2011-08-18 16:58:48 -0700 | [diff] [blame] | 114 | return; |
| 115 | } |
| 116 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 117 | if (type_ == Cellular::kTypeUniversal) { |
| 118 | // TODO(rochberg): |
| 119 | LOG(ERROR) << "Cannot construct Cellular object for MM1"; |
| 120 | return; |
Darin Petkov | e9d12e0 | 2011-07-27 15:09:37 -0700 | [diff] [blame] | 121 | } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 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 | |
| 126 | uint32 modem_state = Cellular::kModemStateUnknown; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 127 | DBusProperties::GetUint32(modem_properties, kPropertyState, &modem_state); |
| 128 | device_->set_modem_state(ConvertMmToCellularModemState(modem_state)); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 129 | |
Darin Petkov | 721ac93 | 2011-11-16 15:43:09 +0100 | [diff] [blame] | 130 | // Give the device a chance to extract any capability-specific properties. |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame^] | 131 | device_->OnModemManagerPropertiesChanged(modem_properties); |
Darin Petkov | bac9600 | 2011-08-09 13:22:00 -0700 | [diff] [blame] | 132 | |
| 133 | manager_->device_info()->RegisterDevice(device_); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 134 | } |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 135 | } // namespace shill |