blob: b36b5c68143c0061ac5a52a207acab1cf658b1bc [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
Eric Shienbrood3e20a232012-02-16 11:35:56 -05007#include <base/bind.h>
Darin Petkove0a312e2011-07-20 13:45:28 -07008#include <base/logging.h>
9#include <mm/mm-modem.h>
10
11#include "shill/cellular.h"
12#include "shill/manager.h"
Darin Petkov5c97ac52011-07-19 16:30:49 -070013#include "shill/proxy_factory.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070014#include "shill/rtnl_handler.h"
15
Eric Shienbrood3e20a232012-02-16 11:35:56 -050016using base::Bind;
Darin Petkove0a312e2011-07-20 13:45:28 -070017using std::string;
Darin Petkovc5f56562011-08-06 16:40:05 -070018using std::vector;
Darin Petkov5c97ac52011-07-19 16:30:49 -070019
20namespace shill {
21
Darin Petkove0a312e2011-07-20 13:45:28 -070022// TODO(petkov): Consider generating these in mm/mm-modem.h.
23const char Modem::kPropertyLinkName[] = "Device";
24const char Modem::kPropertyIPMethod[] = "IpMethod";
Darin Petkovbac96002011-08-09 13:22:00 -070025const char Modem::kPropertyState[] = "State";
Darin Petkove0a312e2011-07-20 13:45:28 -070026const char Modem::kPropertyType[] = "Type";
Darin Petkove0a312e2011-07-20 13:45:28 -070027
Darin Petkov5c97ac52011-07-19 16:30:49 -070028Modem::Modem(const std::string &owner,
29 const std::string &path,
30 ControlInterface *control_interface,
31 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080032 Metrics *metrics,
Darin Petkov137884a2011-10-26 18:52:47 +020033 Manager *manager,
34 mobile_provider_db *provider_db)
Darin Petkovab565bb2011-10-06 02:55:51 -070035 : proxy_factory_(ProxyFactory::GetInstance()),
36 owner_(owner),
Darin Petkove0a312e2011-07-20 13:45:28 -070037 path_(path),
Darin Petkov5c97ac52011-07-19 16:30:49 -070038 control_interface_(control_interface),
39 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080040 metrics_(metrics),
Darin Petkov137884a2011-10-26 18:52:47 +020041 manager_(manager),
Darin Petkov41c0e0a2012-01-09 16:38:53 +010042 provider_db_(provider_db),
43 pending_device_info_(false) {
Darin Petkove0a312e2011-07-20 13:45:28 -070044 LOG(INFO) << "Modem created: " << owner << " at " << path;
45}
Darin Petkov5c97ac52011-07-19 16:30:49 -070046
Jason Glasgowe9089492012-02-23 17:57:37 -050047Modem::~Modem() {
48 if (device_.get()) {
49 manager_->device_info()->DeregisterDevice(device_);
50 }
51}
Darin Petkov5c97ac52011-07-19 16:30:49 -070052
Darin Petkove0a312e2011-07-20 13:45:28 -070053void Modem::Init() {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070054 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070055 CHECK(!device_.get());
Darin Petkova7b89492011-07-27 12:48:17 -070056
57 // Defer device creation because dbus-c++ doesn't allow registration of new
58 // D-Bus objects in the context of a D-Bus signal handler.
Eric Shienbrood3e20a232012-02-16 11:35:56 -050059 dispatcher_->PostTask(Bind(&Modem::InitTask, AsWeakPtr()));
Darin Petkova7b89492011-07-27 12:48:17 -070060}
61
62void Modem::InitTask() {
63 VLOG(2) << __func__;
64 CHECK(!device_.get());
65
Darin Petkove0a312e2011-07-20 13:45:28 -070066 dbus_properties_proxy_.reset(
Darin Petkovab565bb2011-10-06 02:55:51 -070067 proxy_factory_->CreateDBusPropertiesProxy(this, path_, owner_));
Darin Petkov41c0e0a2012-01-09 16:38:53 +010068 CreateDevice();
69}
Darin Petkov67d8ecf2011-07-26 16:03:30 -070070
Darin Petkov41c0e0a2012-01-09 16:38:53 +010071void Modem::OnDeviceInfoAvailable(const string &link_name) {
72 VLOG(2) << __func__;
73 if (pending_device_info_ && link_name_ == link_name) {
74 pending_device_info_ = false;
75 CreateDevice();
76 }
77}
78
79void Modem::CreateDevice() {
80 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070081 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
82 DBusPropertiesMap properties =
83 dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010084 CreateDeviceFromProperties(properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070085}
86
Darin Petkov41c0e0a2012-01-09 16:38:53 +010087void Modem::CreateDeviceFromProperties(const DBusPropertiesMap &properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070088 VLOG(2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010089 if (device_.get()) {
90 return;
91 }
92
Darin Petkove0a312e2011-07-20 13:45:28 -070093 uint32 ip_method = kuint32max;
94 if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) ||
95 ip_method != MM_MODEM_IP_METHOD_DHCP) {
96 LOG(ERROR) << "Unsupported IP method: " << ip_method;
97 return;
98 }
99
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100100 if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -0700101 LOG(ERROR) << "Unable to create cellular device without a link name.";
102 return;
103 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100104 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
105 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700106 int interface_index =
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100107 RTNLHandler::GetInstance()->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700108 if (interface_index < 0) {
109 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
110 return;
111 }
112
Chris Masone626719f2011-08-18 16:58:48 -0700113 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700114 if (!manager_->device_info()->GetMACAddress(interface_index,
115 &address_bytes)) {
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100116 LOG(WARNING) << "No hardware address, device creation pending device info.";
117 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700118 return;
119 }
120
Darin Petkove9d12e02011-07-27 15:09:37 -0700121 uint32 mm_type = kuint32max;
122 Cellular::Type type;
123 DBusProperties::GetUint32(properties, kPropertyType, &mm_type);
124 switch (mm_type) {
125 case MM_MODEM_TYPE_CDMA:
126 type = Cellular::kTypeCDMA;
127 break;
128 case MM_MODEM_TYPE_GSM:
129 type = Cellular::kTypeGSM;
130 break;
131 default:
132 LOG(ERROR) << "Unsupported cellular modem type: " << mm_type;
133 return;
134 }
Darin Petkove0a312e2011-07-20 13:45:28 -0700135
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100136 LOG(INFO) << "Creating a cellular device on link " << link_name_
Darin Petkove0a312e2011-07-20 13:45:28 -0700137 << " interface index " << interface_index << ".";
138 device_ = new Cellular(control_interface_,
139 dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800140 metrics_,
Darin Petkove0a312e2011-07-20 13:45:28 -0700141 manager_,
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100142 link_name_,
Chris Masone626719f2011-08-18 16:58:48 -0700143 address_bytes.HexEncode(),
Darin Petkove9d12e02011-07-27 15:09:37 -0700144 interface_index,
145 type,
146 owner_,
Darin Petkov137884a2011-10-26 18:52:47 +0200147 path_,
148 provider_db_);
Darin Petkovbac96002011-08-09 13:22:00 -0700149
150 uint32 modem_state = Cellular::kModemStateUnknown;
151 DBusProperties::GetUint32(properties, kPropertyState, &modem_state);
152 device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700153
Darin Petkov721ac932011-11-16 15:43:09 +0100154 // Give the device a chance to extract any capability-specific properties.
155 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkovbac96002011-08-09 13:22:00 -0700156
157 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700158}
159
Darin Petkovc5f56562011-08-06 16:40:05 -0700160void Modem::OnDBusPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700161 const string &/*interface*/,
162 const DBusPropertiesMap &/*changed_properties*/,
163 const vector<string> &/*invalidated_properties*/) {
Darin Petkovc5f56562011-08-06 16:40:05 -0700164 // Ignored.
165}
166
167void Modem::OnModemManagerPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700168 const string &/*interface*/,
Darin Petkovc5f56562011-08-06 16:40:05 -0700169 const DBusPropertiesMap &properties) {
Darin Petkov721ac932011-11-16 15:43:09 +0100170 if (device_.get()) {
171 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkov48a511a2011-09-15 10:33:37 -0700172 }
Darin Petkovc5f56562011-08-06 16:40:05 -0700173}
174
Darin Petkov5c97ac52011-07-19 16:30:49 -0700175} // namespace shill