blob: 4f38518edddb80be87de10b5214f50bf3bda3924 [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.
21const char Modem::kPropertyLinkName[] = "Device";
22const char Modem::kPropertyIPMethod[] = "IpMethod";
Darin Petkovbac96002011-08-09 13:22:00 -070023const char Modem::kPropertyState[] = "State";
Darin Petkove0a312e2011-07-20 13:45:28 -070024const char Modem::kPropertyType[] = "Type";
25const char Modem::kPropertyUnlockRequired[] = "UnlockRequired";
26const char Modem::kPropertyUnlockRetries[] = "UnlockRetries";
27
Darin Petkov5c97ac52011-07-19 16:30:49 -070028Modem::Modem(const std::string &owner,
29 const std::string &path,
30 ControlInterface *control_interface,
31 EventDispatcher *dispatcher,
32 Manager *manager)
Darin Petkove0a312e2011-07-20 13:45:28 -070033 : owner_(owner),
34 path_(path),
Darin Petkov67d8ecf2011-07-26 16:03:30 -070035 task_factory_(this),
Darin Petkov5c97ac52011-07-19 16:30:49 -070036 control_interface_(control_interface),
37 dispatcher_(dispatcher),
Darin Petkove0a312e2011-07-20 13:45:28 -070038 manager_(manager) {
39 LOG(INFO) << "Modem created: " << owner << " at " << path;
40}
Darin Petkov5c97ac52011-07-19 16:30:49 -070041
42Modem::~Modem() {}
43
Darin Petkove0a312e2011-07-20 13:45:28 -070044void Modem::Init() {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070045 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070046 CHECK(!device_.get());
Darin Petkova7b89492011-07-27 12:48:17 -070047
48 // Defer device creation because dbus-c++ doesn't allow registration of new
49 // D-Bus objects in the context of a D-Bus signal handler.
50 dispatcher_->PostTask(task_factory_.NewRunnableMethod(&Modem::InitTask));
51}
52
53void Modem::InitTask() {
54 VLOG(2) << __func__;
55 CHECK(!device_.get());
56
Darin Petkove0a312e2011-07-20 13:45:28 -070057 dbus_properties_proxy_.reset(
58 ProxyFactory::factory()->CreateDBusPropertiesProxy(this, path_, owner_));
Darin Petkov67d8ecf2011-07-26 16:03:30 -070059
Darin Petkove0a312e2011-07-20 13:45:28 -070060 // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583).
61 DBusPropertiesMap properties =
62 dbus_properties_proxy_->GetAll(MM_MODEM_INTERFACE);
Darin Petkova7b89492011-07-27 12:48:17 -070063 CreateCellularDevice(properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070064}
65
66void Modem::CreateCellularDevice(const DBusPropertiesMap &properties) {
Darin Petkov67d8ecf2011-07-26 16:03:30 -070067 VLOG(2) << __func__;
Darin Petkove0a312e2011-07-20 13:45:28 -070068 uint32 ip_method = kuint32max;
69 if (!DBusProperties::GetUint32(properties, kPropertyIPMethod, &ip_method) ||
70 ip_method != MM_MODEM_IP_METHOD_DHCP) {
71 LOG(ERROR) << "Unsupported IP method: " << ip_method;
72 return;
73 }
74
75 string link_name;
76 if (!DBusProperties::GetString(properties, kPropertyLinkName, &link_name)) {
77 LOG(ERROR) << "Unable to create cellular device without a link name.";
78 return;
79 }
80 int interface_index =
81 RTNLHandler::GetInstance()->GetInterfaceIndex(link_name);
82 if (interface_index < 0) {
83 LOG(ERROR) << "Unable to create cellular device -- no interface index.";
84 return;
85 }
86
Chris Masone626719f2011-08-18 16:58:48 -070087 ByteString address_bytes;
88 if (!manager_->device_info()->GetAddress(interface_index, &address_bytes)) {
89 // TODO(petkov): ensure that DeviceInfo has heard about this device before
90 // we go ahead and try to add it.
91 LOG(ERROR) << "Unable to create cellular device without a hardware addr.";
92 return;
93 }
94
Darin Petkove9d12e02011-07-27 15:09:37 -070095 uint32 mm_type = kuint32max;
96 Cellular::Type type;
97 DBusProperties::GetUint32(properties, kPropertyType, &mm_type);
98 switch (mm_type) {
99 case MM_MODEM_TYPE_CDMA:
100 type = Cellular::kTypeCDMA;
101 break;
102 case MM_MODEM_TYPE_GSM:
103 type = Cellular::kTypeGSM;
104 break;
105 default:
106 LOG(ERROR) << "Unsupported cellular modem type: " << mm_type;
107 return;
108 }
Darin Petkove0a312e2011-07-20 13:45:28 -0700109
110 LOG(INFO) << "Creating a cellular device on link " << link_name
111 << " interface index " << interface_index << ".";
112 device_ = new Cellular(control_interface_,
113 dispatcher_,
114 manager_,
115 link_name,
Chris Masone626719f2011-08-18 16:58:48 -0700116 address_bytes.HexEncode(),
Darin Petkove9d12e02011-07-27 15:09:37 -0700117 interface_index,
118 type,
119 owner_,
120 path_);
Darin Petkovbac96002011-08-09 13:22:00 -0700121
122 uint32 modem_state = Cellular::kModemStateUnknown;
123 DBusProperties::GetUint32(properties, kPropertyState, &modem_state);
124 device_->set_modem_state(static_cast<Cellular::ModemState>(modem_state));
Darin Petkove0a312e2011-07-20 13:45:28 -0700125
126 string unlock_required;
127 if (DBusProperties::GetString(
128 properties, kPropertyUnlockRequired, &unlock_required)) {
129 uint32 unlock_retries = 0;
130 DBusProperties::GetUint32(properties,
131 kPropertyUnlockRetries,
132 &unlock_retries);
133 // TODO(petkov): Set these properties on the device instance.
134 }
Darin Petkovbac96002011-08-09 13:22:00 -0700135
136 manager_->device_info()->RegisterDevice(device_);
Darin Petkove0a312e2011-07-20 13:45:28 -0700137}
138
Darin Petkovc5f56562011-08-06 16:40:05 -0700139void Modem::OnDBusPropertiesChanged(
140 const string &interface,
141 const DBusPropertiesMap &changed_properties,
142 const vector<string> &invalidated_properties) {
143 // Ignored.
144}
145
146void Modem::OnModemManagerPropertiesChanged(
147 const string &interface,
148 const DBusPropertiesMap &properties) {
149 // TODO(petkov): Implement this.
150 NOTIMPLEMENTED();
151}
152
Darin Petkov5c97ac52011-07-19 16:30:49 -0700153} // namespace shill