blob: c61ba76ad67617f82c83b6a5b1cdb394bd10db7a [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
46Modem::~Modem() {}
47
Darin Petkove0a312e2011-07-20 13:45:28 -070048void Modem::Init() {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070049 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070050 CHECK(!device_.get());
Darin Petkova7b89492011-07-27 12:48:17 -070051
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
57void Modem::InitTask() {
58 VLOG(2) << __func__;
59 CHECK(!device_.get());
60
Darin Petkove0a312e2011-07-20 13:45:28 -070061 dbus_properties_proxy_.reset(
Darin Petkovab565bb2011-10-06 02:55:51 -070062 proxy_factory_->CreateDBusPropertiesProxy(this, path_, owner_));
Darin Petkov41c0e0a2012-01-09 16:38:53 +010063 CreateDevice();
64}
Darin Petkov67d8ecf2011-07-26 16:03:30 -070065
Darin Petkov41c0e0a2012-01-09 16:38:53 +010066void Modem::OnDeviceInfoAvailable(const string &link_name) {
67 VLOG(2) << __func__;
68 if (pending_device_info_ && link_name_ == link_name) {
69 pending_device_info_ = false;
70 CreateDevice();
71 }
72}
73
74void Modem::CreateDevice() {
75 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070076 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
77 DBusPropertiesMap properties =
78 dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010079 CreateDeviceFromProperties(properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070080}
81
Darin Petkov41c0e0a2012-01-09 16:38:53 +010082void Modem::CreateDeviceFromProperties(const DBusPropertiesMap &properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070083 VLOG(2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010084 if (device_.get()) {
85 return;
86 }
87
Darin Petkove0a312e2011-07-20 13:45:28 -070088 uint32 ip_method = kuint32max;
89 if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) ||
90 ip_method != MM_MODEM_IP_METHOD_DHCP) {
91 LOG(ERROR) << "Unsupported IP method: " << ip_method;
92 return;
93 }
94
Darin Petkov41c0e0a2012-01-09 16:38:53 +010095 if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -070096 LOG(ERROR) << "Unable to create cellular device without a link name.";
97 return;
98 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +010099 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
100 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700101 int interface_index =
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100102 RTNLHandler::GetInstance()->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700103 if (interface_index < 0) {
104 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
105 return;
106 }
107
Chris Masone626719f2011-08-18 16:58:48 -0700108 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700109 if (!manager_->device_info()->GetMACAddress(interface_index,
110 &address_bytes)) {
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100111 LOG(WARNING) << "No hardware address, device creation pending device info.";
112 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700113 return;
114 }
115
Darin Petkove9d12e02011-07-27 15:09:37 -0700116 uint32 mm_type = kuint32max;
117 Cellular::Type type;
118 DBusProperties::GetUint32(properties, kPropertyType, &mm_type);
119 switch (mm_type) {
120 case MM_MODEM_TYPE_CDMA:
121 type = Cellular::kTypeCDMA;
122 break;
123 case MM_MODEM_TYPE_GSM:
124 type = Cellular::kTypeGSM;
125 break;
126 default:
127 LOG(ERROR) << "Unsupported cellular modem type: " << mm_type;
128 return;
129 }
Darin Petkove0a312e2011-07-20 13:45:28 -0700130
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100131 LOG(INFO) << "Creating a cellular device on link " << link_name_
Darin Petkove0a312e2011-07-20 13:45:28 -0700132 << " interface index " << interface_index << ".";
133 device_ = new Cellular(control_interface_,
134 dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800135 metrics_,
Darin Petkove0a312e2011-07-20 13:45:28 -0700136 manager_,
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100137 link_name_,
Chris Masone626719f2011-08-18 16:58:48 -0700138 address_bytes.HexEncode(),
Darin Petkove9d12e02011-07-27 15:09:37 -0700139 interface_index,
140 type,
141 owner_,
Darin Petkov137884a2011-10-26 18:52:47 +0200142 path_,
143 provider_db_);
Darin Petkovbac96002011-08-09 13:22:00 -0700144
145 uint32 modem_state = Cellular::kModemStateUnknown;
146 DBusProperties::GetUint32(properties, kPropertyState, &modem_state);
147 device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700148
Darin Petkov721ac932011-11-16 15:43:09 +0100149 // Give the device a chance to extract any capability-specific properties.
150 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkovbac96002011-08-09 13:22:00 -0700151
152 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700153}
154
Darin Petkovc5f56562011-08-06 16:40:05 -0700155void Modem::OnDBusPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700156 const string &/*interface*/,
157 const DBusPropertiesMap &/*changed_properties*/,
158 const vector<string> &/*invalidated_properties*/) {
Darin Petkovc5f56562011-08-06 16:40:05 -0700159 // Ignored.
160}
161
162void Modem::OnModemManagerPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700163 const string &/*interface*/,
Darin Petkovc5f56562011-08-06 16:40:05 -0700164 const DBusPropertiesMap &properties) {
Darin Petkov721ac932011-11-16 15:43:09 +0100165 if (device_.get()) {
166 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkov48a511a2011-09-15 10:33:37 -0700167 }
Darin Petkovc5f56562011-08-06 16:40:05 -0700168}
169
Darin Petkov5c97ac52011-07-19 16:30:49 -0700170} // namespace shill