blob: 2fa5110b0d247df5b2bfd8668d402d82fc2ec985 [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,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070030 ModemInfo * modem_info)
David Rochbergfa1d31d2012-03-20 10:38:07 -040031 : owner_(owner),
Jason Glasgowa585fc32012-06-06 11:04:09 -040032 service_(service),
Darin Petkove0a312e2011-07-20 13:45:28 -070033 path_(path),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070034 modem_info_(modem_info),
David Rochbergfa1d31d2012-03-20 10:38:07 -040035 type_(Cellular::kTypeInvalid),
36 pending_device_info_(false),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040037 rtnl_handler_(RTNLHandler::GetInstance()),
38 proxy_factory_(ProxyFactory::GetInstance()) {
Darin Petkove0a312e2011-07-20 13:45:28 -070039 LOG(INFO) << "Modem created: " << owner << " at " << path;
40}
Darin Petkov5c97ac52011-07-19 16:30:49 -070041
Jason Glasgowe9089492012-02-23 17:57:37 -050042Modem::~Modem() {
Darin Petkova0a0efe2012-06-27 12:50:01 +020043 LOG(INFO) << "Modem destructed: " << owner_ << " at " << path_;
44 if (device_) {
45 device_->DestroyService();
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070046 modem_info_->manager()->device_info()->DeregisterDevice(device_);
Jason Glasgowe9089492012-02-23 17:57:37 -050047 }
48}
Darin Petkov5c97ac52011-07-19 16:30:49 -070049
Eric Shienbrood11567d02012-04-10 18:08:49 -040050void Modem::Init() {
51 dbus_properties_proxy_.reset(
Jason Glasgow9c09e362012-04-18 15:16:29 -040052 proxy_factory_->CreateDBusPropertiesProxy(path(), owner()));
53 dbus_properties_proxy_->set_modem_manager_properties_changed_callback(
54 Bind(&Modem::OnModemManagerPropertiesChanged, Unretained(this)));
55 dbus_properties_proxy_->set_properties_changed_callback(
56 Bind(&Modem::OnDBusPropertiesChanged, Unretained(this)));
Eric Shienbrood11567d02012-04-10 18:08:49 -040057}
58
Darin Petkov41c0e0a2012-01-09 16:38:53 +010059void Modem::OnDeviceInfoAvailable(const string &link_name) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070060 SLOG(Modem, 2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010061 if (pending_device_info_ && link_name_ == link_name) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040062 // pending_device_info_ is only set if we've already been through
63 // CreateDeviceFromModemProperties() and saved our initial
64 // properties already
Darin Petkov41c0e0a2012-01-09 16:38:53 +010065 pending_device_info_ = false;
Ben Chan876efd32012-09-28 15:25:13 -070066 CreateDeviceFromModemProperties(initial_properties_);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010067 }
68}
69
David Rochbergfa1d31d2012-03-20 10:38:07 -040070Cellular *Modem::ConstructCellular(const string &link_name,
71 const string &address,
72 int interface_index) {
73 LOG(INFO) << "Creating a cellular device on link " << link_name_
74 << " interface index " << interface_index << ".";
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070075 return new Cellular(modem_info_,
David Rochbergfa1d31d2012-03-20 10:38:07 -040076 link_name,
77 address,
78 interface_index,
79 type_,
80 owner_,
Jason Glasgowa585fc32012-06-06 11:04:09 -040081 service_,
David Rochbergfa1d31d2012-03-20 10:38:07 -040082 path_,
Ben Chan3ecdf822012-08-06 12:29:23 -070083 ProxyFactory::GetInstance());
Darin Petkove0a312e2011-07-20 13:45:28 -070084}
85
David Rochbergfa1d31d2012-03-20 10:38:07 -040086void Modem::CreateDeviceFromModemProperties(
Ben Chan876efd32012-09-28 15:25:13 -070087 const DBusInterfaceToProperties &properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070088 SLOG(Modem, 2) << __func__;
David Rochbergfa1d31d2012-03-20 10:38:07 -040089
Darin Petkov41c0e0a2012-01-09 16:38:53 +010090 if (device_.get()) {
91 return;
92 }
Ben Chan876efd32012-09-28 15:25:13 -070093
94 DBusInterfaceToProperties::const_iterator properties_it =
95 properties.find(GetModemInterface());
96 if (properties_it == properties.end()) {
97 LOG(ERROR) << "Unable to find modem interface properties.";
98 return;
99 }
100 if (!GetLinkName(properties_it->second, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -0700101 LOG(ERROR) << "Unable to create cellular device without a link name.";
102 return;
103 }
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700104 if (modem_info_->manager()->device_info()->IsDeviceBlackListed(link_name_)) {
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400105 LOG(INFO) << "Do not create cellular device for blacklisted interface "
106 << link_name_;
107 return;
108 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100109 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
110 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700111 int interface_index =
David Rochbergfa1d31d2012-03-20 10:38:07 -0400112 rtnl_handler_->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700113 if (interface_index < 0) {
114 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
115 return;
116 }
117
Chris Masone626719f2011-08-18 16:58:48 -0700118 ByteString address_bytes;
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700119 if (!modem_info_->manager()->device_info()->GetMACAddress(interface_index,
120 &address_bytes)) {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400121 // Save our properties, wait for OnDeviceInfoAvailable to be called.
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100122 LOG(WARNING) << "No hardware address, device creation pending device info.";
Ben Chan876efd32012-09-28 15:25:13 -0700123 initial_properties_ = properties;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100124 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700125 return;
126 }
127
David Rochbergfa1d31d2012-03-20 10:38:07 -0400128 string address = address_bytes.HexEncode();
129 device_ = ConstructCellular(link_name_, address, interface_index);
Darin Petkovbac96002011-08-09 13:22:00 -0700130
Darin Petkov721ac932011-11-16 15:43:09 +0100131 // Give the device a chance to extract any capability-specific properties.
Ben Chan876efd32012-09-28 15:25:13 -0700132 for (properties_it = properties.begin(); properties_it != properties.end();
133 ++properties_it) {
134 device_->OnDBusPropertiesChanged(
135 properties_it->first, properties_it->second, vector<string>());
136 }
Darin Petkovbac96002011-08-09 13:22:00 -0700137
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700138 modem_info_->manager()->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700139}
Eric Shienbrood11567d02012-04-10 18:08:49 -0400140
141void Modem::OnDBusPropertiesChanged(
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400142 const string &interface,
143 const DBusPropertiesMap &changed_properties,
144 const vector<string> &invalidated_properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400145 if (device_.get()) {
146 device_->OnDBusPropertiesChanged(interface,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400147 changed_properties,
148 invalidated_properties);
149 }
Eric Shienbrood11567d02012-04-10 18:08:49 -0400150}
151
152void Modem::OnModemManagerPropertiesChanged(
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400153 const string &interface,
Eric Shienbrood11567d02012-04-10 18:08:49 -0400154 const DBusPropertiesMap &properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400155 vector<string> invalidated_properties;
156 OnDBusPropertiesChanged(interface, properties, invalidated_properties);
Eric Shienbrood11567d02012-04-10 18:08:49 -0400157}
158
Darin Petkov5c97ac52011-07-19 16:30:49 -0700159} // namespace shill