blob: 899918a24b496d56613a2a46ca1d9e9d77b1aa84 [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),
43 rtnl_handler_(RTNLHandler::GetInstance()) {
Darin Petkove0a312e2011-07-20 13:45:28 -070044 LOG(INFO) << "Modem created: " << owner << " at " << path;
45}
Darin Petkov5c97ac52011-07-19 16:30:49 -070046
Jason Glasgowe9089492012-02-23 17:57:37 -050047Modem::~Modem() {
48 if (device_.get()) {
49 manager_->device_info()->DeregisterDevice(device_);
50 }
51}
Darin Petkov5c97ac52011-07-19 16:30:49 -070052
Eric Shienbrood11567d02012-04-10 18:08:49 -040053void Modem::Init() {
54 dbus_properties_proxy_.reset(
55 ProxyFactory::GetInstance()->CreateDBusPropertiesProxy(this,
56 path(),
57 owner()));
58}
59
Darin Petkov41c0e0a2012-01-09 16:38:53 +010060void Modem::OnDeviceInfoAvailable(const string &link_name) {
61 VLOG(2) << __func__;
62 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) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070091 VLOG(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 if (type_ == Cellular::kTypeUniversal) {
125 // TODO(rochberg):
126 LOG(ERROR) << "Cannot construct Cellular object for MM1";
127 return;
Darin Petkove9d12e02011-07-27 15:09:37 -0700128 }
Darin Petkove0a312e2011-07-20 13:45:28 -0700129
David Rochbergfa1d31d2012-03-20 10:38:07 -0400130 string address = address_bytes.HexEncode();
131 device_ = ConstructCellular(link_name_, address, interface_index);
Darin Petkovbac96002011-08-09 13:22:00 -0700132
133 uint32 modem_state = Cellular::kModemStateUnknown;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400134 DBusProperties::GetUint32(modem_properties, kPropertyState, &modem_state);
135 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(
144 const string &/*interface*/,
145 const DBusPropertiesMap &/*changed_properties*/,
146 const vector<string> &/*invalidated_properties*/) {
147 // Ignored.
148}
149
150void Modem::OnModemManagerPropertiesChanged(
151 const string &/*interface*/,
152 const DBusPropertiesMap &properties) {
153 if (device().get()) {
154 device()->OnModemManagerPropertiesChanged(properties);
155 }
156}
157
Darin Petkov5c97ac52011-07-19 16:30:49 -0700158} // namespace shill