blob: 7eb301cd71ce039f8308d3c100743703bcff1161 [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
9#include "shill/cellular.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070010#include "shill/logging.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070011#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
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015using base::Bind;
Jason Glasgow9c09e362012-04-18 15:16:29 -040016using base::Unretained;
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";
25const char Modem::kPropertyType[] = "Type";
Darin Petkove0a312e2011-07-20 13:45:28 -070026
David Rochbergfa1d31d2012-03-20 10:38:07 -040027Modem::Modem(const string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040028 const string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -040029 const string &path,
Darin Petkov5c97ac52011-07-19 16:30:49 -070030 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,
Ben Chan62028b22012-11-05 11:20:02 -080034 CellularOperatorInfo *cellular_operator_info,
Darin Petkov137884a2011-10-26 18:52:47 +020035 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),
Ben Chan62028b22012-11-05 11:20:02 -080043 cellular_operator_info_(cellular_operator_info),
Darin Petkov41c0e0a2012-01-09 16:38:53 +010044 provider_db_(provider_db),
David Rochbergfa1d31d2012-03-20 10:38:07 -040045 type_(Cellular::kTypeInvalid),
46 pending_device_info_(false),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040047 rtnl_handler_(RTNLHandler::GetInstance()),
48 proxy_factory_(ProxyFactory::GetInstance()) {
Darin Petkove0a312e2011-07-20 13:45:28 -070049 LOG(INFO) << "Modem created: " << owner << " at " << path;
50}
Darin Petkov5c97ac52011-07-19 16:30:49 -070051
Jason Glasgowe9089492012-02-23 17:57:37 -050052Modem::~Modem() {
Darin Petkova0a0efe2012-06-27 12:50:01 +020053 LOG(INFO) << "Modem destructed: " << owner_ << " at " << path_;
54 if (device_) {
55 device_->DestroyService();
Jason Glasgowe9089492012-02-23 17:57:37 -050056 manager_->device_info()->DeregisterDevice(device_);
57 }
58}
Darin Petkov5c97ac52011-07-19 16:30:49 -070059
Eric Shienbrood11567d02012-04-10 18:08:49 -040060void Modem::Init() {
61 dbus_properties_proxy_.reset(
Jason Glasgow9c09e362012-04-18 15:16:29 -040062 proxy_factory_->CreateDBusPropertiesProxy(path(), owner()));
63 dbus_properties_proxy_->set_modem_manager_properties_changed_callback(
64 Bind(&Modem::OnModemManagerPropertiesChanged, Unretained(this)));
65 dbus_properties_proxy_->set_properties_changed_callback(
66 Bind(&Modem::OnDBusPropertiesChanged, Unretained(this)));
Eric Shienbrood11567d02012-04-10 18:08:49 -040067}
68
Darin Petkov41c0e0a2012-01-09 16:38:53 +010069void Modem::OnDeviceInfoAvailable(const string &link_name) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070070 SLOG(Modem, 2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010071 if (pending_device_info_ && link_name_ == link_name) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040072 // pending_device_info_ is only set if we've already been through
73 // CreateDeviceFromModemProperties() and saved our initial
74 // properties already
Darin Petkov41c0e0a2012-01-09 16:38:53 +010075 pending_device_info_ = false;
Ben Chan876efd32012-09-28 15:25:13 -070076 CreateDeviceFromModemProperties(initial_properties_);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010077 }
78}
79
David Rochbergfa1d31d2012-03-20 10:38:07 -040080Cellular *Modem::ConstructCellular(const string &link_name,
81 const string &address,
82 int interface_index) {
83 LOG(INFO) << "Creating a cellular device on link " << link_name_
84 << " interface index " << interface_index << ".";
85 return new Cellular(control_interface_,
86 dispatcher_,
87 metrics_,
88 manager_,
89 link_name,
90 address,
91 interface_index,
92 type_,
93 owner_,
Jason Glasgowa585fc32012-06-06 11:04:09 -040094 service_,
David Rochbergfa1d31d2012-03-20 10:38:07 -040095 path_,
Ben Chan62028b22012-11-05 11:20:02 -080096 cellular_operator_info_,
Ben Chan3ecdf822012-08-06 12:29:23 -070097 provider_db_,
98 ProxyFactory::GetInstance());
Darin Petkove0a312e2011-07-20 13:45:28 -070099}
100
David Rochbergfa1d31d2012-03-20 10:38:07 -0400101void Modem::CreateDeviceFromModemProperties(
Ben Chan876efd32012-09-28 15:25:13 -0700102 const DBusInterfaceToProperties &properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -0700103 SLOG(Modem, 2) << __func__;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400104
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100105 if (device_.get()) {
106 return;
107 }
Ben Chan876efd32012-09-28 15:25:13 -0700108
109 DBusInterfaceToProperties::const_iterator properties_it =
110 properties.find(GetModemInterface());
111 if (properties_it == properties.end()) {
112 LOG(ERROR) << "Unable to find modem interface properties.";
113 return;
114 }
115 if (!GetLinkName(properties_it->second, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -0700116 LOG(ERROR) << "Unable to create cellular device without a link name.";
117 return;
118 }
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400119 if (manager_->device_info()->IsDeviceBlackListed(link_name_)) {
120 LOG(INFO) << "Do not create cellular device for blacklisted interface "
121 << link_name_;
122 return;
123 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100124 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
125 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700126 int interface_index =
David Rochbergfa1d31d2012-03-20 10:38:07 -0400127 rtnl_handler_->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700128 if (interface_index < 0) {
129 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
130 return;
131 }
132
Chris Masone626719f2011-08-18 16:58:48 -0700133 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700134 if (!manager_->device_info()->GetMACAddress(interface_index,
135 &address_bytes)) {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400136 // Save our properties, wait for OnDeviceInfoAvailable to be called.
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100137 LOG(WARNING) << "No hardware address, device creation pending device info.";
Ben Chan876efd32012-09-28 15:25:13 -0700138 initial_properties_ = properties;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100139 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700140 return;
141 }
142
David Rochbergfa1d31d2012-03-20 10:38:07 -0400143 string address = address_bytes.HexEncode();
144 device_ = ConstructCellular(link_name_, address, interface_index);
Darin Petkovbac96002011-08-09 13:22:00 -0700145
Darin Petkov721ac932011-11-16 15:43:09 +0100146 // Give the device a chance to extract any capability-specific properties.
Ben Chan876efd32012-09-28 15:25:13 -0700147 for (properties_it = properties.begin(); properties_it != properties.end();
148 ++properties_it) {
149 device_->OnDBusPropertiesChanged(
150 properties_it->first, properties_it->second, vector<string>());
151 }
Darin Petkovbac96002011-08-09 13:22:00 -0700152
153 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700154}
Eric Shienbrood11567d02012-04-10 18:08:49 -0400155
156void Modem::OnDBusPropertiesChanged(
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400157 const string &interface,
158 const DBusPropertiesMap &changed_properties,
159 const vector<string> &invalidated_properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400160 if (device_.get()) {
161 device_->OnDBusPropertiesChanged(interface,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400162 changed_properties,
163 invalidated_properties);
164 }
Eric Shienbrood11567d02012-04-10 18:08:49 -0400165}
166
167void Modem::OnModemManagerPropertiesChanged(
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400168 const string &interface,
Eric Shienbrood11567d02012-04-10 18:08:49 -0400169 const DBusPropertiesMap &properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400170 vector<string> invalidated_properties;
171 OnDBusPropertiesChanged(interface, properties, invalidated_properties);
Eric Shienbrood11567d02012-04-10 18:08:49 -0400172}
173
Darin Petkov5c97ac52011-07-19 16:30:49 -0700174} // namespace shill