blob: 5c5a77c86ac5f94f733d552d4957eea028b10d74 [file] [log] [blame]
Darin Petkov5c97ac52011-07-19 16:30:49 -07001// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2// 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
Darin Petkove0a312e2011-07-20 13:45:28 -07007#include <base/logging.h>
8#include <mm/mm-modem.h>
9
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
15using std::string;
Darin Petkovc5f56562011-08-06 16:40:05 -070016using std::vector;
Darin Petkov5c97ac52011-07-19 16:30:49 -070017
18namespace shill {
19
Darin Petkove0a312e2011-07-20 13:45:28 -070020// TODO(petkov): Consider generating these in mm/mm-modem.h.
Darin Petkov48a511a2011-09-15 10:33:37 -070021const char Modem::kPropertyAccessTechnology[] = "AccessTechnology";
Darin Petkove0a312e2011-07-20 13:45:28 -070022const 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";
26const char Modem::kPropertyUnlockRequired[] = "UnlockRequired";
27const char Modem::kPropertyUnlockRetries[] = "UnlockRetries";
28
Darin Petkov5c97ac52011-07-19 16:30:49 -070029Modem::Modem(const std::string &owner,
30 const std::string &path,
31 ControlInterface *control_interface,
32 EventDispatcher *dispatcher,
33 Manager *manager)
Darin Petkove0a312e2011-07-20 13:45:28 -070034 : owner_(owner),
35 path_(path),
Darin Petkov67d8ecf2011-07-26 16:03:30 -070036 task_factory_(this),
Darin Petkov5c97ac52011-07-19 16:30:49 -070037 control_interface_(control_interface),
38 dispatcher_(dispatcher),
Darin Petkove0a312e2011-07-20 13:45:28 -070039 manager_(manager) {
40 LOG(INFO) << "Modem created: " << owner << " at " << path;
41}
Darin Petkov5c97ac52011-07-19 16:30:49 -070042
43Modem::~Modem() {}
44
Darin Petkove0a312e2011-07-20 13:45:28 -070045void Modem::Init() {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070046 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070047 CHECK(!device_.get());
Darin Petkova7b89492011-07-27 12:48:17 -070048
49 // Defer device creation because dbus-c++ doesn't allow registration of new
50 // D-Bus objects in the context of a D-Bus signal handler.
51 dispatcher_->PostTask(task_factory_.NewRunnableMethod(&Modem::InitTask));
52}
53
54void Modem::InitTask() {
55 VLOG(2) << __func__;
56 CHECK(!device_.get());
57
Darin Petkove0a312e2011-07-20 13:45:28 -070058 dbus_properties_proxy_.reset(
59 ProxyFactory::factory()->CreateDBusPropertiesProxy(this, path_, owner_));
Darin Petkov67d8ecf2011-07-26 16:03:30 -070060
Darin Petkove0a312e2011-07-20 13:45:28 -070061 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
62 DBusPropertiesMap properties =
63 dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE);
Darin Petkova7b89492011-07-27 12:48:17 -070064 CreateCellularDevice(properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070065}
66
67void Modem::CreateCellularDevice(const DBusPropertiesMap &properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070068 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070069 uint32 ip_method = kuint32max;
70 if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) ||
71 ip_method != MM_MODEM_IP_METHOD_DHCP) {
72 LOG(ERROR) << "Unsupported IP method: " << ip_method;
73 return;
74 }
75
76 string link_name;
77 if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name)) {
78 LOG(ERROR) << "Unable to create cellular device without a link name.";
79 return;
80 }
81 int interface_index =
82 RTNLHandler::GetInstance()->GetInterfaceIndex(link_name);
83 if (interface_index < 0) {
84 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
85 return;
86 }
87
Chris Masone626719f2011-08-18 16:58:48 -070088 ByteString address_bytes;
Paul Stewart32852962011-08-30 14:06:53 -070089 if (!manager_->device_info()->GetMACAddress(interface_index,
90 &address_bytes)) {
Chris Masone626719f2011-08-18 16:58:48 -070091 // TODO(petkov): ensure that DeviceInfo has heard about this device before
92 // we go ahead and try to add it.
93 LOG(ERROR) << "Unable to create cellular device without a hardware addr.";
94 return;
95 }
96
Darin Petkove9d12e02011-07-27 15:09:37 -070097 uint32 mm_type = kuint32max;
98 Cellular::Type type;
99 DBusProperties::GetUint32(properties, kPropertyType, &mm_type);
100 switch (mm_type) {
101 case MM_MODEM_TYPE_CDMA:
102 type = Cellular::kTypeCDMA;
103 break;
104 case MM_MODEM_TYPE_GSM:
105 type = Cellular::kTypeGSM;
106 break;
107 default:
108 LOG(ERROR) << "Unsupported cellular modem type: " << mm_type;
109 return;
110 }
Darin Petkove0a312e2011-07-20 13:45:28 -0700111
112 LOG(INFO) << "Creating a cellular device on link " << link_name
113 << " interface index " << interface_index << ".";
114 device_ = new Cellular(control_interface_,
115 dispatcher_,
116 manager_,
117 link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700118 address_bytes.HexEncode(),
Darin Petkove9d12e02011-07-27 15:09:37 -0700119 interface_index,
120 type,
121 owner_,
122 path_);
Darin Petkovbac96002011-08-09 13:22:00 -0700123
124 uint32 modem_state = Cellular::kModemStateUnknown;
125 DBusProperties::GetUint32(properties, kPropertyState, &modem_state);
126 device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700127
128 string unlock_required;
129 if (DBusProperties::GetString(
130 properties, kPropertyUnlockRequired, &unlock_required)) {
131 uint32 unlock_retries = 0;
132 DBusProperties::GetUint32(properties,
133 kPropertyUnlockRetries,
134 &unlock_retries);
Darin Petkov48a511a2011-09-15 10:33:37 -0700135 device_->set_sim_lock_status(
136 Cellular::SimLockStatus(unlock_required, unlock_retries));
Darin Petkove0a312e2011-07-20 13:45:28 -0700137 }
Darin Petkovbac96002011-08-09 13:22:00 -0700138
139 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700140}
141
Darin Petkovc5f56562011-08-06 16:40:05 -0700142void Modem::OnDBusPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700143 const string &/*interface*/,
144 const DBusPropertiesMap &/*changed_properties*/,
145 const vector<string> &/*invalidated_properties*/) {
Darin Petkovc5f56562011-08-06 16:40:05 -0700146 // Ignored.
147}
148
149void Modem::OnModemManagerPropertiesChanged(
mukesh agrawal1830fa12011-09-26 14:31:40 -0700150 const string &/*interface*/,
Darin Petkovc5f56562011-08-06 16:40:05 -0700151 const DBusPropertiesMap &properties) {
Darin Petkov48a511a2011-09-15 10:33:37 -0700152 if (!device_.get()) {
153 return;
154 }
155 Cellular::SimLockStatus lock_status = device_->sim_lock_status();
156 if (DBusProperties::GetString(
157 properties, kPropertyUnlockRequired, &lock_status.lock_type) ||
158 DBusProperties::GetUint32(properties,
159 kPropertyUnlockRetries,
160 &lock_status.retries_left)) {
161 device_->set_sim_lock_status(lock_status);
162 }
163 uint32 access_technology = MM_MODEM_GSM_ACCESS_TECH_UNKNOWN;
164 if (DBusProperties::GetUint32(properties,
165 kPropertyAccessTechnology,
166 &access_technology)) {
167 device_->SetGSMAccessTechnology(access_technology);
168 }
Darin Petkovc5f56562011-08-06 16:40:05 -0700169}
170
Darin Petkov5c97ac52011-07-19 16:30:49 -0700171} // namespace shill