blob: fead592bcd4c32d171e559188d33cf2fafdc78aa [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());
56 dbus_properties_proxy_.reset(
Darin Petkovab565bb2011-10-06 02:55:51 -070057 proxy_factory_->CreateDBusPropertiesProxy(this, path_, owner_));
Darin Petkov41c0e0a2012-01-09 16:38:53 +010058 CreateDevice();
59}
Darin Petkov67d8ecf2011-07-26 16:03:30 -070060
Darin Petkov41c0e0a2012-01-09 16:38:53 +010061void Modem::OnDeviceInfoAvailable(const string &link_name) {
62 VLOG(2) << __func__;
63 if (pending_device_info_ && link_name_ == link_name) {
64 pending_device_info_ = false;
65 CreateDevice();
66 }
67}
68
69void Modem::CreateDevice() {
70 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070071 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
72 DBusPropertiesMap properties =
73 dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010074 CreateDeviceFromProperties(properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070075}
76
Darin Petkov41c0e0a2012-01-09 16:38:53 +010077void Modem::CreateDeviceFromProperties(const DBusPropertiesMap &properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070078 VLOG(2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010079 if (device_.get()) {
80 return;
81 }
82
Darin Petkove0a312e2011-07-20 13:45:28 -070083 uint32 ip_method = kuint32max;
84 if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) ||
85 ip_method != MM_MODEM_IP_METHOD_DHCP) {
86 LOG(ERROR) << "Unsupported IP method: " << ip_method;
87 return;
88 }
89
Darin Petkov41c0e0a2012-01-09 16:38:53 +010090 if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -070091 LOG(ERROR) << "Unable to create cellular device without a link name.";
92 return;
93 }
Eric Shienbrood5e628a52012-03-21 16:56:59 -040094 if (manager_->device_info()->IsDeviceBlackListed(link_name_)) {
95 LOG(INFO) << "Do not create cellular device for blacklisted interface "
96 << link_name_;
97 return;
98 }
99
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100100 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
101 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700102 int interface_index =
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100103 RTNLHandler::GetInstance()->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700104 if (interface_index < 0) {
105 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
106 return;
107 }
108
Chris Masone626719f2011-08-18 16:58:48 -0700109 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700110 if (!manager_->device_info()->GetMACAddress(interface_index,
111 &address_bytes)) {
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100112 LOG(WARNING) << "No hardware address, device creation pending device info.";
113 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700114 return;
115 }
116
Darin Petkove9d12e02011-07-27 15:09:37 -0700117 uint32 mm_type = kuint32max;
118 Cellular::Type type;
119 DBusProperties::GetUint32(properties, kPropertyType, &mm_type);
120 switch (mm_type) {
121 case MM_MODEM_TYPE_CDMA:
122 type = Cellular::kTypeCDMA;
123 break;
124 case MM_MODEM_TYPE_GSM:
125 type = Cellular::kTypeGSM;
126 break;
127 default:
128 LOG(ERROR) << "Unsupported cellular modem type: " << mm_type;
129 return;
130 }
Darin Petkove0a312e2011-07-20 13:45:28 -0700131
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100132 LOG(INFO) << "Creating a cellular device on link " << link_name_
Darin Petkove0a312e2011-07-20 13:45:28 -0700133 << " interface index " << interface_index << ".";
134 device_ = new Cellular(control_interface_,
135 dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800136 metrics_,
Darin Petkove0a312e2011-07-20 13:45:28 -0700137 manager_,
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100138 link_name_,
Chris Masone626719f2011-08-18 16:58:48 -0700139 address_bytes.HexEncode(),
Darin Petkove9d12e02011-07-27 15:09:37 -0700140 interface_index,
141 type,
142 owner_,
Darin Petkov137884a2011-10-26 18:52:47 +0200143 path_,
144 provider_db_);
Darin Petkovbac96002011-08-09 13:22:00 -0700145
146 uint32 modem_state = Cellular::kModemStateUnknown;
147 DBusProperties::GetUint32(properties, kPropertyState, &modem_state);
148 device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700149
Darin Petkov721ac932011-11-16 15:43:09 +0100150 // Give the device a chance to extract any capability-specific properties.
151 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkovbac96002011-08-09 13:22:00 -0700152
153 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700154}
155
Darin Petkovc5f56562011-08-06 16:40:05 -0700156void Modem::OnDBusPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700157 const string &/*interface*/,
158 const DBusPropertiesMap &/*changed_properties*/,
159 const vector<string> &/*invalidated_properties*/) {
Darin Petkovc5f56562011-08-06 16:40:05 -0700160 // Ignored.
161}
162
163void Modem::OnModemManagerPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700164 const string &/*interface*/,
Darin Petkovc5f56562011-08-06 16:40:05 -0700165 const DBusPropertiesMap &properties) {
Darin Petkov721ac932011-11-16 15:43:09 +0100166 if (device_.get()) {
167 device_->OnModemManagerPropertiesChanged(properties);
Darin Petkov48a511a2011-09-15 10:33:37 -0700168 }
Darin Petkovc5f56562011-08-06 16:40:05 -0700169}
170
Darin Petkov5c97ac52011-07-19 16:30:49 -0700171} // namespace shill