blob: 1450971cd618262c35bb2ea956fa6eaebc4fa40a [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>
Darin Petkove0a312e2011-07-20 13:45:28 -07009
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"
Ben Chanfad4a0b2012-04-18 15:49:59 -070014#include "shill/scope_logger.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070015
Eric Shienbrood3e20a232012-02-16 11:35:56 -050016using base::Bind;
Jason Glasgow9c09e362012-04-18 15:16:29 -040017using base::Unretained;
Darin Petkove0a312e2011-07-20 13:45:28 -070018using std::string;
Darin Petkovc5f56562011-08-06 16:40:05 -070019using std::vector;
Darin Petkov5c97ac52011-07-19 16:30:49 -070020
21namespace shill {
22
Darin Petkove0a312e2011-07-20 13:45:28 -070023// TODO(petkov): Consider generating these in mm/mm-modem.h.
24const char Modem::kPropertyLinkName[] = "Device";
25const char Modem::kPropertyIPMethod[] = "IpMethod";
26const char Modem::kPropertyType[] = "Type";
Darin Petkove0a312e2011-07-20 13:45:28 -070027
David Rochbergfa1d31d2012-03-20 10:38:07 -040028Modem::Modem(const string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040029 const string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -040030 const string &path,
Darin Petkov5c97ac52011-07-19 16:30:49 -070031 ControlInterface *control_interface,
32 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080033 Metrics *metrics,
Darin Petkov137884a2011-10-26 18:52:47 +020034 Manager *manager,
35 mobile_provider_db *provider_db)
David Rochbergfa1d31d2012-03-20 10:38:07 -040036 : owner_(owner),
Jason Glasgowa585fc32012-06-06 11:04:09 -040037 service_(service),
Darin Petkove0a312e2011-07-20 13:45:28 -070038 path_(path),
Darin Petkov5c97ac52011-07-19 16:30:49 -070039 control_interface_(control_interface),
40 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080041 metrics_(metrics),
Darin Petkov137884a2011-10-26 18:52:47 +020042 manager_(manager),
Darin Petkov41c0e0a2012-01-09 16:38:53 +010043 provider_db_(provider_db),
David Rochbergfa1d31d2012-03-20 10:38:07 -040044 type_(Cellular::kTypeInvalid),
45 pending_device_info_(false),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040046 rtnl_handler_(RTNLHandler::GetInstance()),
47 proxy_factory_(ProxyFactory::GetInstance()) {
Darin Petkove0a312e2011-07-20 13:45:28 -070048 LOG(INFO) << "Modem created: " << owner << " at " << path;
49}
Darin Petkov5c97ac52011-07-19 16:30:49 -070050
Jason Glasgowe9089492012-02-23 17:57:37 -050051Modem::~Modem() {
Darin Petkova0a0efe2012-06-27 12:50:01 +020052 LOG(INFO) << "Modem destructed: " << owner_ << " at " << path_;
53 if (device_) {
54 device_->DestroyService();
Jason Glasgowe9089492012-02-23 17:57:37 -050055 manager_->device_info()->DeregisterDevice(device_);
56 }
57}
Darin Petkov5c97ac52011-07-19 16:30:49 -070058
Eric Shienbrood11567d02012-04-10 18:08:49 -040059void Modem::Init() {
60 dbus_properties_proxy_.reset(
Jason Glasgow9c09e362012-04-18 15:16:29 -040061 proxy_factory_->CreateDBusPropertiesProxy(path(), owner()));
62 dbus_properties_proxy_->set_modem_manager_properties_changed_callback(
63 Bind(&Modem::OnModemManagerPropertiesChanged, Unretained(this)));
64 dbus_properties_proxy_->set_properties_changed_callback(
65 Bind(&Modem::OnDBusPropertiesChanged, Unretained(this)));
Eric Shienbrood11567d02012-04-10 18:08:49 -040066}
67
Darin Petkov41c0e0a2012-01-09 16:38:53 +010068void Modem::OnDeviceInfoAvailable(const string &link_name) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070069 SLOG(Modem, 2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010070 if (pending_device_info_ && link_name_ == link_name) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040071 // pending_device_info_ is only set if we've already been through
72 // CreateDeviceFromModemProperties() and saved our initial
73 // properties already
Darin Petkov41c0e0a2012-01-09 16:38:53 +010074 pending_device_info_ = false;
David Rochbergfa1d31d2012-03-20 10:38:07 -040075 CreateDeviceFromModemProperties(initial_modem_properties_);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010076 }
77}
78
David Rochbergfa1d31d2012-03-20 10:38:07 -040079Cellular *Modem::ConstructCellular(const string &link_name,
80 const string &address,
81 int interface_index) {
82 LOG(INFO) << "Creating a cellular device on link " << link_name_
83 << " interface index " << interface_index << ".";
84 return new Cellular(control_interface_,
85 dispatcher_,
86 metrics_,
87 manager_,
88 link_name,
89 address,
90 interface_index,
91 type_,
92 owner_,
Jason Glasgowa585fc32012-06-06 11:04:09 -040093 service_,
David Rochbergfa1d31d2012-03-20 10:38:07 -040094 path_,
Ben Chan3ecdf822012-08-06 12:29:23 -070095 provider_db_,
96 ProxyFactory::GetInstance());
Darin Petkove0a312e2011-07-20 13:45:28 -070097}
98
David Rochbergfa1d31d2012-03-20 10:38:07 -040099void Modem::CreateDeviceFromModemProperties(
100 const DBusPropertiesMap &modem_properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700101 SLOG(Modem, 2) << __func__;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400102
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100103 if (device_.get()) {
104 return;
105 }
David Rochbergfa1d31d2012-03-20 10:38:07 -0400106 if (!GetLinkName(modem_properties, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -0700107 LOG(ERROR) << "Unable to create cellular device without a link name.";
108 return;
109 }
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400110 if (manager_->device_info()->IsDeviceBlackListed(link_name_)) {
111 LOG(INFO) << "Do not create cellular device for blacklisted interface "
112 << link_name_;
113 return;
114 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100115 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
116 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700117 int interface_index =
David Rochbergfa1d31d2012-03-20 10:38:07 -0400118 rtnl_handler_->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700119 if (interface_index < 0) {
120 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
121 return;
122 }
123
Chris Masone626719f2011-08-18 16:58:48 -0700124 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700125 if (!manager_->device_info()->GetMACAddress(interface_index,
126 &address_bytes)) {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400127 // Save our properties, wait for OnDeviceInfoAvailable to be called.
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100128 LOG(WARNING) << "No hardware address, device creation pending device info.";
David Rochbergfa1d31d2012-03-20 10:38:07 -0400129 initial_modem_properties_ = modem_properties;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100130 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700131 return;
132 }
133
David Rochbergfa1d31d2012-03-20 10:38:07 -0400134 string address = address_bytes.HexEncode();
135 device_ = ConstructCellular(link_name_, address, interface_index);
Darin Petkovbac96002011-08-09 13:22:00 -0700136
Darin Petkov721ac932011-11-16 15:43:09 +0100137 // Give the device a chance to extract any capability-specific properties.
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400138 device_->OnDBusPropertiesChanged(GetModemInterface(), modem_properties,
139 vector<string>());
Darin Petkovbac96002011-08-09 13:22:00 -0700140
141 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700142}
Eric Shienbrood11567d02012-04-10 18:08:49 -0400143
144void Modem::OnDBusPropertiesChanged(
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400145 const string &interface,
146 const DBusPropertiesMap &changed_properties,
147 const vector<string> &invalidated_properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400148 if (device_.get()) {
149 device_->OnDBusPropertiesChanged(interface,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400150 changed_properties,
151 invalidated_properties);
152 }
Eric Shienbrood11567d02012-04-10 18:08:49 -0400153}
154
155void Modem::OnModemManagerPropertiesChanged(
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400156 const string &interface,
Eric Shienbrood11567d02012-04-10 18:08:49 -0400157 const DBusPropertiesMap &properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400158 vector<string> invalidated_properties;
159 OnDBusPropertiesChanged(interface, properties, invalidated_properties);
Eric Shienbrood11567d02012-04-10 18:08:49 -0400160}
161
Darin Petkov5c97ac52011-07-19 16:30:49 -0700162} // namespace shill