blob: 0d4711b542806276fb2fb8d9db1e31b48c498202 [file] [log] [blame]
Darin Petkov41c0e0a2012-01-09 16:38:53 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov5c97ac52011-07-19 16:30:49 -07002// 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 Petkove0a312e2011-07-20 13:45:28 -07007#include <base/logging.h>
8#include <mm/mm-modem.h>
9
10#include "shill/cellular.h"
11#include "shill/manager.h"
Darin Petkov5c97ac52011-07-19 16:30:49 -070012#include "shill/proxy_factory.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070013#include "shill/rtnl_handler.h"
14
15using std::string;
Darin Petkovc5f56562011-08-06 16:40:05 -070016using std::vector;
Darin Petkov5c97ac52011-07-19 16:30:49 -070017
18namespace shill {
19
Darin Petkove0a312e2011-07-20 13:45:28 -070020// TODO(petkov): Consider generating these in mm/mm-modem.h.
21const char Modem::kPropertyLinkName[] = "Device";
22const char Modem::kPropertyIPMethod[] = "IpMethod";
Darin Petkovbac96002011-08-09 13:22:00 -070023const char Modem::kPropertyState[] = "State";
Darin Petkove0a312e2011-07-20 13:45:28 -070024const char Modem::kPropertyType[] = "Type";
Darin Petkove0a312e2011-07-20 13:45:28 -070025
Darin Petkov5c97ac52011-07-19 16:30:49 -070026Modem::Modem(const std::string &owner,
27 const std::string &path,
28 ControlInterface *control_interface,
29 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080030 Metrics *metrics,
Darin Petkov137884a2011-10-26 18:52:47 +020031 Manager *manager,
32 mobile_provider_db *provider_db)
Darin Petkovab565bb2011-10-06 02:55:51 -070033 : proxy_factory_(ProxyFactory::GetInstance()),
34 owner_(owner),
Darin Petkove0a312e2011-07-20 13:45:28 -070035 path_(path),
Darin Petkov67d8ecf2011-07-26 16:03:30 -070036 task_factory_(this),
Darin Petkov5c97ac52011-07-19 16:30:49 -070037 control_interface_(control_interface),
38 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080039 metrics_(metrics),
Darin Petkov137884a2011-10-26 18:52:47 +020040 manager_(manager),
Darin Petkov41c0e0a2012-01-09 16:38:53 +010041 provider_db_(provider_db),
42 pending_device_info_(false) {
Darin Petkove0a312e2011-07-20 13:45:28 -070043 LOG(INFO) << "Modem created: " << owner << " at " << path;
44}
Darin Petkov5c97ac52011-07-19 16:30:49 -070045
Jason Glasgowe9089492012-02-23 17:57:37 -050046Modem::~Modem() {
47 if (device_.get()) {
48 manager_->device_info()->DeregisterDevice(device_);
49 }
50}
Darin Petkov5c97ac52011-07-19 16:30:49 -070051
Darin Petkove0a312e2011-07-20 13:45:28 -070052void Modem::Init() {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070053 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070054 CHECK(!device_.get());
Darin Petkova7b89492011-07-27 12:48:17 -070055
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
61void Modem::InitTask() {
62 VLOG(2) << __func__;
63 CHECK(!device_.get());
64
Darin Petkove0a312e2011-07-20 13:45:28 -070065 dbus_properties_proxy_.reset(
Darin Petkovab565bb2011-10-06 02:55:51 -070066 proxy_factory_->CreateDBusPropertiesProxy(this, path_, owner_));
Darin Petkov41c0e0a2012-01-09 16:38:53 +010067 CreateDevice();
68}
Darin Petkov67d8ecf2011-07-26 16:03:30 -070069
Darin Petkov41c0e0a2012-01-09 16:38:53 +010070void 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
78void Modem::CreateDevice() {
79 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070080 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
81 DBusPropertiesMap properties =
82 dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010083 CreateDeviceFromProperties(properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070084}
85
Darin Petkov41c0e0a2012-01-09 16:38:53 +010086void Modem::CreateDeviceFromProperties(const DBusPropertiesMap &properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070087 VLOG(2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010088 if (device_.get()) {
89 return;
90 }
91
Darin Petkove0a312e2011-07-20 13:45:28 -070092 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 Petkov41c0e0a2012-01-09 16:38:53 +010099 if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -0700100 LOG(ERROR) << "Unable to create cellular device without a link name.";
101 return;
102 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100103 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
104 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700105 int interface_index =
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100106 RTNLHandler::GetInstance()->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700107 if (interface_index < 0) {
108 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
109 return;
110 }
111
Chris Masone626719f2011-08-18 16:58:48 -0700112 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700113 if (!manager_->device_info()->GetMACAddress(interface_index,
114 &address_bytes)) {
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100115 LOG(WARNING) << "No hardware address, device creation pending device info.";
116 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700117 return;
118 }
119
Darin Petkove9d12e02011-07-27 15:09:37 -0700120 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 Petkove0a312e2011-07-20 13:45:28 -0700134
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100135 LOG(INFO) << "Creating a cellular device on link " << link_name_
Darin Petkove0a312e2011-07-20 13:45:28 -0700136 << " interface index " << interface_index << ".";
137 device_ = new Cellular(control_interface_,
138 dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800139 metrics_,
Darin Petkove0a312e2011-07-20 13:45:28 -0700140 manager_,
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100141 link_name_,
Chris Masone626719f2011-08-18 16:58:48 -0700142 address_bytes.HexEncode(),
Darin Petkove9d12e02011-07-27 15:09:37 -0700143 interface_index,
144 type,
145 owner_,
Darin Petkov137884a2011-10-26 18:52:47 +0200146 path_,
147 provider_db_);
Darin Petkovbac96002011-08-09 13:22:00 -0700148
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 Petkove0a312e2011-07-20 13:45:28 -0700152
Darin Petkov721ac932011-11-16 15:43:09 +0100153 // Give the device a chance to extract any capability-specific properties.
154 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkovbac96002011-08-09 13:22:00 -0700155
156 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700157}
158
Darin Petkovc5f56562011-08-06 16:40:05 -0700159void Modem::OnDBusPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700160 const string &/*interface*/,
161 const DBusPropertiesMap &/*changed_properties*/,
162 const vector<string> &/*invalidated_properties*/) {
Darin Petkovc5f56562011-08-06 16:40:05 -0700163 // Ignored.
164}
165
166void Modem::OnModemManagerPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700167 const string &/*interface*/,
Darin Petkovc5f56562011-08-06 16:40:05 -0700168 const DBusPropertiesMap &properties) {
Darin Petkov721ac932011-11-16 15:43:09 +0100169 if (device_.get()) {
170 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkov48a511a2011-09-15 10:33:37 -0700171 }
Darin Petkovc5f56562011-08-06 16:40:05 -0700172}
173
Darin Petkov5c97ac52011-07-19 16:30:49 -0700174} // namespace shill