blob: f4f46cb6facc156dc7129a1bb091f51c1ca8dfeb [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,
Darin Petkov137884a2011-10-26 18:52:47 +020030 Manager *manager,
31 mobile_provider_db *provider_db)
Darin Petkovab565bb2011-10-06 02:55:51 -070032 : proxy_factory_(ProxyFactory::GetInstance()),
33 owner_(owner),
Darin Petkove0a312e2011-07-20 13:45:28 -070034 path_(path),
Darin Petkov67d8ecf2011-07-26 16:03:30 -070035 task_factory_(this),
Darin Petkov5c97ac52011-07-19 16:30:49 -070036 control_interface_(control_interface),
37 dispatcher_(dispatcher),
Darin Petkov137884a2011-10-26 18:52:47 +020038 manager_(manager),
Darin Petkov41c0e0a2012-01-09 16:38:53 +010039 provider_db_(provider_db),
40 pending_device_info_(false) {
Darin Petkove0a312e2011-07-20 13:45:28 -070041 LOG(INFO) << "Modem created: " << owner << " at " << path;
42}
Darin Petkov5c97ac52011-07-19 16:30:49 -070043
44Modem::~Modem() {}
45
Darin Petkove0a312e2011-07-20 13:45:28 -070046void Modem::Init() {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070047 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070048 CHECK(!device_.get());
Darin Petkova7b89492011-07-27 12:48:17 -070049
50 // Defer device creation because dbus-c++ doesn't allow registration of new
51 // D-Bus objects in the context of a D-Bus signal handler.
52 dispatcher_->PostTask(task_factory_.NewRunnableMethod(&Modem::InitTask));
53}
54
55void Modem::InitTask() {
56 VLOG(2) << __func__;
57 CHECK(!device_.get());
58
Darin Petkove0a312e2011-07-20 13:45:28 -070059 dbus_properties_proxy_.reset(
Darin Petkovab565bb2011-10-06 02:55:51 -070060 proxy_factory_->CreateDBusPropertiesProxy(this, path_, owner_));
Darin Petkov41c0e0a2012-01-09 16:38:53 +010061 CreateDevice();
62}
Darin Petkov67d8ecf2011-07-26 16:03:30 -070063
Darin Petkov41c0e0a2012-01-09 16:38:53 +010064void Modem::OnDeviceInfoAvailable(const string &link_name) {
65 VLOG(2) << __func__;
66 if (pending_device_info_ && link_name_ == link_name) {
67 pending_device_info_ = false;
68 CreateDevice();
69 }
70}
71
72void Modem::CreateDevice() {
73 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070074 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
75 DBusPropertiesMap properties =
76 dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010077 CreateDeviceFromProperties(properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070078}
79
Darin Petkov41c0e0a2012-01-09 16:38:53 +010080void Modem::CreateDeviceFromProperties(const DBusPropertiesMap &properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070081 VLOG(2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010082 if (device_.get()) {
83 return;
84 }
85
Darin Petkove0a312e2011-07-20 13:45:28 -070086 uint32 ip_method = kuint32max;
87 if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) ||
88 ip_method != MM_MODEM_IP_METHOD_DHCP) {
89 LOG(ERROR) << "Unsupported IP method: " << ip_method;
90 return;
91 }
92
Darin Petkov41c0e0a2012-01-09 16:38:53 +010093 if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -070094 LOG(ERROR) << "Unable to create cellular device without a link name.";
95 return;
96 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +010097 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
98 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -070099 int interface_index =
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100100 RTNLHandler::GetInstance()->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700101 if (interface_index < 0) {
102 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
103 return;
104 }
105
Chris Masone626719f2011-08-18 16:58:48 -0700106 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700107 if (!manager_->device_info()->GetMACAddress(interface_index,
108 &address_bytes)) {
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100109 LOG(WARNING) << "No hardware address, device creation pending device info.";
110 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700111 return;
112 }
113
Darin Petkove9d12e02011-07-27 15:09:37 -0700114 uint32 mm_type = kuint32max;
115 Cellular::Type type;
116 DBusProperties::GetUint32(properties, kPropertyType, &mm_type);
117 switch (mm_type) {
118 case MM_MODEM_TYPE_CDMA:
119 type = Cellular::kTypeCDMA;
120 break;
121 case MM_MODEM_TYPE_GSM:
122 type = Cellular::kTypeGSM;
123 break;
124 default:
125 LOG(ERROR) << "Unsupported cellular modem type: " << mm_type;
126 return;
127 }
Darin Petkove0a312e2011-07-20 13:45:28 -0700128
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100129 LOG(INFO) << "Creating a cellular device on link " << link_name_
Darin Petkove0a312e2011-07-20 13:45:28 -0700130 << " interface index " << interface_index << ".";
131 device_ = new Cellular(control_interface_,
132 dispatcher_,
133 manager_,
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100134 link_name_,
Chris Masone626719f2011-08-18 16:58:48 -0700135 address_bytes.HexEncode(),
Darin Petkove9d12e02011-07-27 15:09:37 -0700136 interface_index,
137 type,
138 owner_,
Darin Petkov137884a2011-10-26 18:52:47 +0200139 path_,
140 provider_db_);
Darin Petkovbac96002011-08-09 13:22:00 -0700141
142 uint32 modem_state = Cellular::kModemStateUnknown;
143 DBusProperties::GetUint32(properties, kPropertyState, &modem_state);
144 device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700145
Darin Petkov721ac932011-11-16 15:43:09 +0100146 // Give the device a chance to extract any capability-specific properties.
147 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkovbac96002011-08-09 13:22:00 -0700148
149 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700150}
151
Darin Petkovc5f56562011-08-06 16:40:05 -0700152void Modem::OnDBusPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700153 const string &/*interface*/,
154 const DBusPropertiesMap &/*changed_properties*/,
155 const vector<string> &/*invalidated_properties*/) {
Darin Petkovc5f56562011-08-06 16:40:05 -0700156 // Ignored.
157}
158
159void Modem::OnModemManagerPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700160 const string &/*interface*/,
Darin Petkovc5f56562011-08-06 16:40:05 -0700161 const DBusPropertiesMap &properties) {
Darin Petkov721ac932011-11-16 15:43:09 +0100162 if (device_.get()) {
163 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkov48a511a2011-09-15 10:33:37 -0700164 }
Darin Petkovc5f56562011-08-06 16:40:05 -0700165}
166
Darin Petkov5c97ac52011-07-19 16:30:49 -0700167} // namespace shill