blob: 112865c94ebf77b21dd79f0e45e2280084f893f3 [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;
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
David Rochbergfa1d31d2012-03-20 10:38:07 -040028Modem::Modem(const string &owner,
29 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,
34 mobile_provider_db *provider_db)
David Rochbergfa1d31d2012-03-20 10:38:07 -040035 : owner_(owner),
Darin Petkove0a312e2011-07-20 13:45:28 -070036 path_(path),
Darin Petkov5c97ac52011-07-19 16:30:49 -070037 control_interface_(control_interface),
38 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080039 metrics_(metrics),
Darin Petkov137884a2011-10-26 18:52:47 +020040 manager_(manager),
Darin Petkov41c0e0a2012-01-09 16:38:53 +010041 provider_db_(provider_db),
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() {
50 if (device_.get()) {
51 manager_->device_info()->DeregisterDevice(device_);
52 }
53}
Darin Petkov5c97ac52011-07-19 16:30:49 -070054
Eric Shienbrood11567d02012-04-10 18:08:49 -040055void Modem::Init() {
56 dbus_properties_proxy_.reset(
Jason Glasgow82f9ab32012-04-04 14:27:19 -040057 proxy_factory_->CreateDBusPropertiesProxy(this, path(), owner()));
Eric Shienbrood11567d02012-04-10 18:08:49 -040058}
59
Darin Petkov41c0e0a2012-01-09 16:38:53 +010060void Modem::OnDeviceInfoAvailable(const string &link_name) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070061 SLOG(Modem, 2) << __func__;
Darin Petkov41c0e0a2012-01-09 16:38:53 +010062 if (pending_device_info_ && link_name_ == link_name) {
David Rochbergfa1d31d2012-03-20 10:38:07 -040063 // pending_device_info_ is only set if we've already been through
64 // CreateDeviceFromModemProperties() and saved our initial
65 // properties already
Darin Petkov41c0e0a2012-01-09 16:38:53 +010066 pending_device_info_ = false;
David Rochbergfa1d31d2012-03-20 10:38:07 -040067 CreateDeviceFromModemProperties(initial_modem_properties_);
Darin Petkov41c0e0a2012-01-09 16:38:53 +010068 }
69}
70
David Rochbergfa1d31d2012-03-20 10:38:07 -040071Cellular *Modem::ConstructCellular(const string &link_name,
72 const string &address,
73 int interface_index) {
74 LOG(INFO) << "Creating a cellular device on link " << link_name_
75 << " interface index " << interface_index << ".";
76 return new Cellular(control_interface_,
77 dispatcher_,
78 metrics_,
79 manager_,
80 link_name,
81 address,
82 interface_index,
83 type_,
84 owner_,
85 path_,
86 provider_db_);
Darin Petkove0a312e2011-07-20 13:45:28 -070087}
88
David Rochbergfa1d31d2012-03-20 10:38:07 -040089void Modem::CreateDeviceFromModemProperties(
90 const DBusPropertiesMap &modem_properties) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070091 SLOG(Modem, 2) << __func__;
David Rochbergfa1d31d2012-03-20 10:38:07 -040092
Darin Petkov41c0e0a2012-01-09 16:38:53 +010093 if (device_.get()) {
94 return;
95 }
David Rochbergfa1d31d2012-03-20 10:38:07 -040096 if (!GetLinkName(modem_properties, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -070097 LOG(ERROR) << "Unable to create cellular device without a link name.";
98 return;
99 }
Eric Shienbrood5e628a52012-03-21 16:56:59 -0400100 if (manager_->device_info()->IsDeviceBlackListed(link_name_)) {
101 LOG(INFO) << "Do not create cellular device for blacklisted interface "
102 << link_name_;
103 return;
104 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100105 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
106 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700107 int interface_index =
David Rochbergfa1d31d2012-03-20 10:38:07 -0400108 rtnl_handler_->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700109 if (interface_index < 0) {
110 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
111 return;
112 }
113
Chris Masone626719f2011-08-18 16:58:48 -0700114 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700115 if (!manager_->device_info()->GetMACAddress(interface_index,
116 &address_bytes)) {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400117 // Save our properties, wait for OnDeviceInfoAvailable to be called.
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100118 LOG(WARNING) << "No hardware address, device creation pending device info.";
David Rochbergfa1d31d2012-03-20 10:38:07 -0400119 initial_modem_properties_ = modem_properties;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100120 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700121 return;
122 }
123
David Rochbergfa1d31d2012-03-20 10:38:07 -0400124 string address = address_bytes.HexEncode();
125 device_ = ConstructCellular(link_name_, address, interface_index);
Darin Petkovbac96002011-08-09 13:22:00 -0700126
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400127 // Note: 0 happens to map to Cellular::kModemStateUnknown for all
128 // types of modems
129 uint32 modem_state = 0;
130 // TODO(jglasgow): refactor to avoid explicit if on modem type. The
131 // ModemManager1 interface type is an "i" not a "u". Handle that
132 // during refactoring.
133 if (type_ != Cellular::kTypeUniversal)
134 DBusProperties::GetUint32(modem_properties, kPropertyState, &modem_state);
135
David Rochbergfa1d31d2012-03-20 10:38:07 -0400136 device_->set_modem_state(ConvertMmToCellularModemState(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700137
Darin Petkov721ac932011-11-16 15:43:09 +0100138 // Give the device a chance to extract any capability-specific properties.
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400139 device_->OnDBusPropertiesChanged(GetModemInterface(), modem_properties,
140 vector<string>());
Darin Petkovbac96002011-08-09 13:22:00 -0700141
142 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700143}
Eric Shienbrood11567d02012-04-10 18:08:49 -0400144
145void Modem::OnDBusPropertiesChanged(
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400146 const string &interface,
147 const DBusPropertiesMap &changed_properties,
148 const vector<string> &invalidated_properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400149 if (device_.get()) {
150 device_->OnDBusPropertiesChanged(interface,
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400151 changed_properties,
152 invalidated_properties);
153 }
Eric Shienbrood11567d02012-04-10 18:08:49 -0400154}
155
156void Modem::OnModemManagerPropertiesChanged(
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400157 const string &interface,
Eric Shienbrood11567d02012-04-10 18:08:49 -0400158 const DBusPropertiesMap &properties) {
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400159 vector<string> invalidated_properties;
160 OnDBusPropertiesChanged(interface, properties, invalidated_properties);
Eric Shienbrood11567d02012-04-10 18:08:49 -0400161}
162
Darin Petkov5c97ac52011-07-19 16:30:49 -0700163} // namespace shill