blob: a9f2e554cb14145dff3b55936d30e470561476e8 [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"
14
Eric Shienbrood3e20a232012-02-16 11:35:56 -050015using base::Bind;
Darin Petkove0a312e2011-07-20 13:45:28 -070016using std::string;
Darin Petkovc5f56562011-08-06 16:40:05 -070017using std::vector;
Darin Petkov5c97ac52011-07-19 16:30:49 -070018
19namespace shill {
20
Darin Petkove0a312e2011-07-20 13:45:28 -070021// TODO(petkov): Consider generating these in mm/mm-modem.h.
22const char Modem::kPropertyLinkName[] = "Device";
23const char Modem::kPropertyIPMethod[] = "IpMethod";
Darin Petkovbac96002011-08-09 13:22:00 -070024const char Modem::kPropertyState[] = "State";
Darin Petkove0a312e2011-07-20 13:45:28 -070025const char Modem::kPropertyType[] = "Type";
Darin Petkove0a312e2011-07-20 13:45:28 -070026
David Rochbergfa1d31d2012-03-20 10:38:07 -040027Modem::Modem(const string &owner,
28 const string &path,
Darin Petkov5c97ac52011-07-19 16:30:49 -070029 ControlInterface *control_interface,
30 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080031 Metrics *metrics,
Darin Petkov137884a2011-10-26 18:52:47 +020032 Manager *manager,
33 mobile_provider_db *provider_db)
David Rochbergfa1d31d2012-03-20 10:38:07 -040034 : owner_(owner),
Darin Petkove0a312e2011-07-20 13:45:28 -070035 path_(path),
Darin Petkov5c97ac52011-07-19 16:30:49 -070036 control_interface_(control_interface),
37 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080038 metrics_(metrics),
Darin Petkov137884a2011-10-26 18:52:47 +020039 manager_(manager),
Darin Petkov41c0e0a2012-01-09 16:38:53 +010040 provider_db_(provider_db),
David Rochbergfa1d31d2012-03-20 10:38:07 -040041 type_(Cellular::kTypeInvalid),
42 pending_device_info_(false),
Jason Glasgow82f9ab32012-04-04 14:27:19 -040043 rtnl_handler_(RTNLHandler::GetInstance()),
44 proxy_factory_(ProxyFactory::GetInstance()) {
Darin Petkove0a312e2011-07-20 13:45:28 -070045 LOG(INFO) << "Modem created: " << owner << " at " << path;
46}
Darin Petkov5c97ac52011-07-19 16:30:49 -070047
Jason Glasgowe9089492012-02-23 17:57:37 -050048Modem::~Modem() {
49 if (device_.get()) {
50 manager_->device_info()->DeregisterDevice(device_);
51 }
52}
Darin Petkov5c97ac52011-07-19 16:30:49 -070053
Eric Shienbrood11567d02012-04-10 18:08:49 -040054void Modem::Init() {
55 dbus_properties_proxy_.reset(
Jason Glasgow82f9ab32012-04-04 14:27:19 -040056 proxy_factory_->CreateDBusPropertiesProxy(this, path(), owner()));
Eric Shienbrood11567d02012-04-10 18:08:49 -040057}
58
Darin Petkov41c0e0a2012-01-09 16:38:53 +010059void Modem::OnDeviceInfoAvailable(const string &link_name) {
60 VLOG(2) << __func__;
61 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;
David Rochbergfa1d31d2012-03-20 10:38:07 -040066 CreateDeviceFromModemProperties(initial_modem_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 << ".";
75 return new Cellular(control_interface_,
76 dispatcher_,
77 metrics_,
78 manager_,
79 link_name,
80 address,
81 interface_index,
82 type_,
83 owner_,
84 path_,
85 provider_db_);
Darin Petkove0a312e2011-07-20 13:45:28 -070086}
87
David Rochbergfa1d31d2012-03-20 10:38:07 -040088void Modem::CreateDeviceFromModemProperties(
89 const DBusPropertiesMap &modem_properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070090 VLOG(2) << __func__;
David Rochbergfa1d31d2012-03-20 10:38:07 -040091
Darin Petkov41c0e0a2012-01-09 16:38:53 +010092 if (device_.get()) {
93 return;
94 }
David Rochbergfa1d31d2012-03-20 10:38:07 -040095 if (!GetLinkName(modem_properties, &link_name_)) {
Darin Petkove0a312e2011-07-20 13:45:28 -070096 LOG(ERROR) << "Unable to create cellular device without a link name.";
97 return;
98 }
Eric Shienbrood5e628a52012-03-21 16:56:59 -040099 if (manager_->device_info()->IsDeviceBlackListed(link_name_)) {
100 LOG(INFO) << "Do not create cellular device for blacklisted interface "
101 << link_name_;
102 return;
103 }
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100104 // TODO(petkov): Get the interface index from DeviceInfo, similar to the MAC
105 // address below.
Darin Petkove0a312e2011-07-20 13:45:28 -0700106 int interface_index =
David Rochbergfa1d31d2012-03-20 10:38:07 -0400107 rtnl_handler_->GetInterfaceIndex(link_name_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700108 if (interface_index < 0) {
109 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
110 return;
111 }
112
Chris Masone626719f2011-08-18 16:58:48 -0700113 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -0700114 if (!manager_->device_info()->GetMACAddress(interface_index,
115 &address_bytes)) {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400116 // Save our properties, wait for OnDeviceInfoAvailable to be called.
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100117 LOG(WARNING) << "No hardware address, device creation pending device info.";
David Rochbergfa1d31d2012-03-20 10:38:07 -0400118 initial_modem_properties_ = modem_properties;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100119 pending_device_info_ = true;
Chris Masone626719f2011-08-18 16:58:48 -0700120 return;
121 }
122
David Rochbergfa1d31d2012-03-20 10:38:07 -0400123 string address = address_bytes.HexEncode();
124 device_ = ConstructCellular(link_name_, address, interface_index);
Darin Petkovbac96002011-08-09 13:22:00 -0700125
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400126 // Note: 0 happens to map to Cellular::kModemStateUnknown for all
127 // types of modems
128 uint32 modem_state = 0;
129 // TODO(jglasgow): refactor to avoid explicit if on modem type. The
130 // ModemManager1 interface type is an "i" not a "u". Handle that
131 // during refactoring.
132 if (type_ != Cellular::kTypeUniversal)
133 DBusProperties::GetUint32(modem_properties, kPropertyState, &modem_state);
134
David Rochbergfa1d31d2012-03-20 10:38:07 -0400135 device_->set_modem_state(ConvertMmToCellularModemState(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700136
Darin Petkov721ac932011-11-16 15:43:09 +0100137 // Give the device a chance to extract any capability-specific properties.
David Rochbergfa1d31d2012-03-20 10:38:07 -0400138 device_->OnModemManagerPropertiesChanged(modem_properties);
Darin Petkovbac96002011-08-09 13:22:00 -0700139
140 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700141}
Eric Shienbrood11567d02012-04-10 18:08:49 -0400142
143void Modem::OnDBusPropertiesChanged(
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400144 const string &interface,
145 const DBusPropertiesMap &changed_properties,
146 const vector<string> &invalidated_properties) {
147 if (device().get()) {
148 device()->OnDBusPropertiesChanged(interface,
149 changed_properties,
150 invalidated_properties);
151 }
Eric Shienbrood11567d02012-04-10 18:08:49 -0400152}
153
154void Modem::OnModemManagerPropertiesChanged(
155 const string &/*interface*/,
156 const DBusPropertiesMap &properties) {
157 if (device().get()) {
158 device()->OnModemManagerPropertiesChanged(properties);
159 }
160}
161
Darin Petkov5c97ac52011-07-19 16:30:49 -0700162} // namespace shill