blob: 63483249791796992cbb31e6343ee17c18a99aed [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>
mukesh agrawal9da07772013-05-15 14:15:17 -07008#include <base/stringprintf.h>
Darin Petkove0a312e2011-07-20 13:45:28 -07009
10#include "shill/cellular.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070011#include "shill/logging.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070012#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;
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
mukesh agrawal9da07772013-05-15 14:15:17 -070028// statics
29const char Modem::kFakeDevNameFormat[] = "no_netdev_%zu";
30const char Modem::kFakeDevAddress[] = "00:00:00:00:00:00";
31const int Modem::kFakeDevInterfaceIndex = -1;
32size_t Modem::fake_dev_serial_ = 0;
33
David Rochbergfa1d31d2012-03-20 10:38:07 -040034Modem::Modem(const string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040035 const string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -040036 const string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070037 ModemInfo * modem_info)
David Rochbergfa1d31d2012-03-20 10:38:07 -040038 : owner_(owner),
Jason Glasgowa585fc32012-06-06 11:04:09 -040039 service_(service),
Darin Petkove0a312e2011-07-20 13:45:28 -070040 path_(path),
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070041 modem_info_(modem_info),
David Rochbergfa1d31d2012-03-20 10:38:07 -040042 type_(Cellular::kTypeInvalid),
43 pending_device_info_(false),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040044 rtnl_handler_(RTNLHandler::GetInstance()),
45 proxy_factory_(ProxyFactory::GetInstance()) {
Darin Petkove0a312e2011-07-20 13:45:28 -070046 LOG(INFO) << "Modem created: " << owner << " at " << path;
47}
Darin Petkov5c97ac52011-07-19 16:30:49 -070048
Jason Glasgowe9089492012-02-23 17:57:37 -050049Modem::~Modem() {
Darin Petkova0a0efe2012-06-27 12:50:01 +020050 LOG(INFO) << "Modem destructed: " << owner_ << " at " << path_;
51 if (device_) {
52 device_->DestroyService();
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070053 modem_info_->manager()->device_info()->DeregisterDevice(device_);
Jason Glasgowe9089492012-02-23 17:57:37 -050054 }
55}
Darin Petkov5c97ac52011-07-19 16:30:49 -070056
Eric Shienbrood11567d02012-04-10 18:08:49 -040057void Modem::Init() {
58 dbus_properties_proxy_.reset(
Jason Glasgow9c09e362012-04-18 15:16:29 -040059 proxy_factory_->CreateDBusPropertiesProxy(path(), owner()));
60 dbus_properties_proxy_->set_modem_manager_properties_changed_callback(
61 Bind(&Modem::OnModemManagerPropertiesChanged, Unretained(this)));
62 dbus_properties_proxy_->set_properties_changed_callback(
63 Bind(&Modem::OnDBusPropertiesChanged, Unretained(this)));
Eric Shienbrood11567d02012-04-10 18:08:49 -040064}
65
Darin Petkov41c0e0a2012-01-09 16:38:53 +010066void Modem::OnDeviceInfoAvailable(const string &link_name) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070067 SLOG(Modem, 2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010068 if (pending_device_info_ && link_name_ == link_name) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040069 // pending_device_info_ is only set if we've already been through
70 // CreateDeviceFromModemProperties() and saved our initial
71 // properties already
Darin Petkov41c0e0a2012-01-09 16:38:53 +010072 pending_device_info_ = false;
Ben Chan876efd32012-09-28 15:25:13 -070073 CreateDeviceFromModemProperties(initial_properties_);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010074 }
75}
76
David Rochbergfa1d31d2012-03-20 10:38:07 -040077Cellular *Modem::ConstructCellular(const string &link_name,
78 const string &address,
79 int interface_index) {
mukesh agrawal9da07772013-05-15 14:15:17 -070080 LOG(INFO) << "Creating a cellular device on link " << link_name
David Rochbergfa1d31d2012-03-20 10:38:07 -040081 << " interface index " << interface_index << ".";
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070082 return new Cellular(modem_info_,
David Rochbergfa1d31d2012-03-20 10:38:07 -040083 link_name,
84 address,
85 interface_index,
86 type_,
87 owner_,
Jason Glasgowa585fc32012-06-06 11:04:09 -040088 service_,
David Rochbergfa1d31d2012-03-20 10:38:07 -040089 path_,
Ben Chan3ecdf822012-08-06 12:29:23 -070090 ProxyFactory::GetInstance());
Darin Petkove0a312e2011-07-20 13:45:28 -070091}
92
David Rochbergfa1d31d2012-03-20 10:38:07 -040093void Modem::CreateDeviceFromModemProperties(
Ben Chan876efd32012-09-28 15:25:13 -070094 const DBusInterfaceToProperties &properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070095 SLOG(Modem, 2) << __func__;
David Rochbergfa1d31d2012-03-20 10:38:07 -040096
Darin Petkov41c0e0a2012-01-09 16:38:53 +010097 if (device_.get()) {
98 return;
99 }
Ben Chan876efd32012-09-28 15:25:13 -0700100
101 DBusInterfaceToProperties::const_iterator properties_it =
102 properties.find(GetModemInterface());
103 if (properties_it == properties.end()) {
104 LOG(ERROR) << "Unable to find modem interface properties.";
105 return;
106 }
mukesh agrawal9da07772013-05-15 14:15:17 -0700107
108 string mac_address;
109 int interface_index = -1;
110 if (GetLinkName(properties_it->second, &link_name_)) {
111 GetDeviceParams(&mac_address, &interface_index);
112 if (interface_index < 0) {
113 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
114 return;
115 }
116 if (mac_address.empty()) {
117 // Save our properties, wait for OnDeviceInfoAvailable to be called.
118 LOG(WARNING)
119 << "No hardware address, device creation pending device info.";
120 initial_properties_ = properties;
121 pending_device_info_ = true;
122 return;
123 }
124 // Got the interface index and MAC address. Fall-through to actually
125 // creating the Cellular object.
126 } else {
127 // Probably a PPP dongle.
128 LOG(INFO) << "Cellular device without link name; assuming PPP dongle.";
129 link_name_ = base::StringPrintf(kFakeDevNameFormat, fake_dev_serial_++);
130 mac_address = kFakeDevAddress;
131 interface_index = kFakeDevInterfaceIndex;
Darin Petkove0a312e2011-07-20 13:45:28 -0700132 }
mukesh agrawal9da07772013-05-15 14:15:17 -0700133
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700134 if (modem_info_->manager()->device_info()->IsDeviceBlackListed(link_name_)) {
mukesh agrawal9da07772013-05-15 14:15:17 -0700135 LOG(INFO) << "Not creating cellular device for blacklisted interface "
136 << link_name_ << ".";
Darin Petkove0a312e2011-07-20 13:45:28 -0700137 return;
138 }
139
mukesh agrawal9da07772013-05-15 14:15:17 -0700140 device_ = ConstructCellular(link_name_, mac_address, interface_index);
Darin Petkov721ac932011-11-16 15:43:09 +0100141 // Give the device a chance to extract any capability-specific properties.
Ben Chan876efd32012-09-28 15:25:13 -0700142 for (properties_it = properties.begin(); properties_it != properties.end();
143 ++properties_it) {
144 device_->OnDBusPropertiesChanged(
145 properties_it->first, properties_it->second, vector<string>());
146 }
Darin Petkovbac96002011-08-09 13:22:00 -0700147
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700148 modem_info_->manager()->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700149}
Eric Shienbrood11567d02012-04-10 18:08:49 -0400150
mukesh agrawal9da07772013-05-15 14:15:17 -0700151bool Modem::GetDeviceParams(string *mac_address, int *interface_index) {
152 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
153 // address below.
154 *interface_index = rtnl_handler_->GetInterfaceIndex(link_name_);
155 if (*interface_index < 0) {
156 return false;
157 }
158
159 ByteString address_bytes;
160 if (!modem_info_->manager()->device_info()->GetMACAddress(*interface_index,
161 &address_bytes)) {
162 return false;
163 }
164
165 *mac_address = address_bytes.HexEncode();
166 return true;
167}
168
Eric Shienbrood11567d02012-04-10 18:08:49 -0400169void Modem::OnDBusPropertiesChanged(
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400170 const string &interface,
171 const DBusPropertiesMap &changed_properties,
172 const vector<string> &invalidated_properties) {
Arman Ugurayf6366ac2013-06-12 16:02:28 -0700173 SLOG(Modem, 2) << __func__;
174 SLOG(Modem, 3) << "PropertiesChanged signal received.";
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400175 if (device_.get()) {
176 device_->OnDBusPropertiesChanged(interface,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400177 changed_properties,
178 invalidated_properties);
179 }
Eric Shienbrood11567d02012-04-10 18:08:49 -0400180}
181
182void Modem::OnModemManagerPropertiesChanged(
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400183 const string &interface,
Eric Shienbrood11567d02012-04-10 18:08:49 -0400184 const DBusPropertiesMap &properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400185 vector<string> invalidated_properties;
186 OnDBusPropertiesChanged(interface, properties, invalidated_properties);
Eric Shienbrood11567d02012-04-10 18:08:49 -0400187}
188
Darin Petkov5c97ac52011-07-19 16:30:49 -0700189} // namespace shill