Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 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_manager.h" |
| 6 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/stl_util.h> |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 8 | #include <mm/mm-modem.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 9 | |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 10 | #include "shill/cellular_operator_info.h" |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 11 | #include "shill/error.h" |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 12 | #include "shill/logging.h" |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 13 | #include "shill/modem.h" |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 14 | #include "shill/modem_manager_proxy.h" |
| 15 | #include "shill/proxy_factory.h" |
| 16 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 17 | using std::string; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 18 | using std::tr1::shared_ptr; |
| 19 | using std::vector; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 20 | |
| 21 | namespace shill { |
Ben Chan | 5c853ef | 2012-10-05 00:05:37 -0700 | [diff] [blame] | 22 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 23 | ModemManager::ModemManager(const string &service, |
| 24 | const string &path, |
| 25 | ControlInterface *control_interface, |
| 26 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 27 | Metrics *metrics, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 28 | Manager *manager, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 29 | GLib *glib, |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 30 | CellularOperatorInfo *cellular_operator_info, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 31 | mobile_provider_db *provider_db) |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 32 | : proxy_factory_(ProxyFactory::GetInstance()), |
| 33 | service_(service), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 34 | path_(path), |
| 35 | watcher_id_(0), |
| 36 | control_interface_(control_interface), |
| 37 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 38 | metrics_(metrics), |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 39 | manager_(manager), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 40 | glib_(glib), |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 41 | cellular_operator_info_(cellular_operator_info), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 42 | provider_db_(provider_db) {} |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 43 | |
| 44 | ModemManager::~ModemManager() { |
| 45 | Stop(); |
| 46 | } |
| 47 | |
| 48 | void ModemManager::Start() { |
| 49 | LOG(INFO) << "Start watching modem manager service: " << service_; |
Eric Shienbrood | c74cf9c | 2012-03-02 15:00:35 -0500 | [diff] [blame] | 50 | CHECK_EQ(0U, watcher_id_); |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 51 | // TODO(petkov): Implement DBus name watching through dbus-c++. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 52 | watcher_id_ = glib_->BusWatchName(G_BUS_TYPE_SYSTEM, |
| 53 | service_.c_str(), |
| 54 | G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 55 | OnAppear, |
| 56 | OnVanish, |
| 57 | this, |
| 58 | NULL); |
| 59 | } |
| 60 | |
| 61 | void ModemManager::Stop() { |
| 62 | LOG(INFO) << "Stop watching modem manager service: " << service_; |
| 63 | if (watcher_id_) { |
| 64 | glib_->BusUnwatchName(watcher_id_); |
| 65 | watcher_id_ = 0; |
| 66 | } |
| 67 | Disconnect(); |
| 68 | } |
| 69 | |
| 70 | void ModemManager::Connect(const string &owner) { |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 71 | // Inheriting classes call this superclass method. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 72 | owner_ = owner; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void ModemManager::Disconnect() { |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 76 | // Inheriting classes call this superclass method. |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 77 | modems_.clear(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 78 | owner_.clear(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 79 | } |
| 80 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 81 | void ModemManager::OnAppear(GDBusConnection */*connection*/, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 82 | const gchar *name, |
| 83 | const gchar *name_owner, |
| 84 | gpointer user_data) { |
| 85 | LOG(INFO) << "Modem manager " << name << " appeared. Owner: " << name_owner; |
| 86 | ModemManager *manager = reinterpret_cast<ModemManager *>(user_data); |
| 87 | manager->Connect(name_owner); |
| 88 | } |
| 89 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 90 | void ModemManager::OnVanish(GDBusConnection */*connection*/, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 91 | const gchar *name, |
| 92 | gpointer user_data) { |
| 93 | LOG(INFO) << "Modem manager " << name << " vanished."; |
| 94 | ModemManager *manager = reinterpret_cast<ModemManager *>(user_data); |
| 95 | manager->Disconnect(); |
| 96 | } |
| 97 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 98 | bool ModemManager::ModemExists(const std::string &path) const { |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 99 | CHECK(!owner_.empty()); |
| 100 | if (ContainsKey(modems_, path)) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 101 | LOG(INFO) << "ModemExists: " << path << " already exists."; |
| 102 | return true; |
| 103 | } else { |
| 104 | return false; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 105 | } |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void ModemManager::RecordAddedModem(shared_ptr<Modem> modem) { |
| 109 | modems_[modem->path()] = modem; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 112 | void ModemManager::RemoveModem(const string &path) { |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 113 | LOG(INFO) << "Remove modem: " << path; |
| 114 | CHECK(!owner_.empty()); |
| 115 | modems_.erase(path); |
| 116 | } |
| 117 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 118 | void ModemManager::OnDeviceInfoAvailable(const string &link_name) { |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 119 | for (Modems::const_iterator it = modems_.begin(); it != modems_.end(); ++it) { |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 120 | it->second->OnDeviceInfoAvailable(link_name); |
| 121 | } |
| 122 | } |
| 123 | |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 124 | // ModemManagerClassic |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 125 | ModemManagerClassic::ModemManagerClassic( |
| 126 | const string &service, |
| 127 | const string &path, |
| 128 | ControlInterface *control_interface, |
| 129 | EventDispatcher *dispatcher, |
| 130 | Metrics *metrics, |
| 131 | Manager *manager, |
| 132 | GLib *glib, |
| 133 | CellularOperatorInfo *cellular_operator_info, |
| 134 | mobile_provider_db *provider_db) |
| 135 | : ModemManager(service, |
| 136 | path, |
| 137 | control_interface, |
| 138 | dispatcher, |
| 139 | metrics, |
| 140 | manager, |
| 141 | glib, |
| 142 | cellular_operator_info, |
| 143 | provider_db) {} |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 144 | |
| 145 | ModemManagerClassic::~ModemManagerClassic() {} |
| 146 | |
| 147 | void ModemManagerClassic::Connect(const string &supplied_owner) { |
| 148 | ModemManager::Connect(supplied_owner); |
| 149 | proxy_.reset(proxy_factory()->CreateModemManagerProxy(this, path(), owner())); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 150 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 151 | vector<DBus::Path> devices = proxy_->EnumerateDevices(); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 152 | |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 153 | for (vector<DBus::Path>::const_iterator it = devices.begin(); |
| 154 | it != devices.end(); ++it) { |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 155 | AddModemClassic(*it); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 159 | void ModemManagerClassic::AddModemClassic(const string &path) { |
| 160 | if (ModemExists(path)) { |
| 161 | return; |
| 162 | } |
| 163 | shared_ptr<ModemClassic> modem(new ModemClassic(owner(), |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 164 | service(), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 165 | path, |
| 166 | control_interface(), |
| 167 | dispatcher(), |
| 168 | metrics(), |
| 169 | manager(), |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 170 | cellular_operator_info(), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 171 | provider_db())); |
| 172 | RecordAddedModem(modem); |
| 173 | InitModemClassic(modem); |
| 174 | } |
| 175 | |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 176 | void ModemManagerClassic::Disconnect() { |
| 177 | ModemManager::Disconnect(); |
| 178 | proxy_.reset(); |
| 179 | } |
| 180 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 181 | void ModemManagerClassic::InitModemClassic(shared_ptr<ModemClassic> modem) { |
| 182 | // TODO(rochberg): Switch to asynchronous calls (crosbug.com/17583). |
| 183 | if (modem == NULL) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | scoped_ptr<DBusPropertiesProxyInterface> properties_proxy( |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 188 | proxy_factory()->CreateDBusPropertiesProxy(modem->path(), |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 189 | modem->owner())); |
| 190 | DBusPropertiesMap properties = |
| 191 | properties_proxy->GetAll(MM_MODEM_INTERFACE); |
| 192 | |
| 193 | modem->CreateDeviceClassic(properties); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 194 | } |
| 195 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 196 | void ModemManagerClassic::OnDeviceAdded(const string &path) { |
| 197 | AddModemClassic(path); |
| 198 | } |
| 199 | |
| 200 | void ModemManagerClassic::OnDeviceRemoved(const string &path) { |
| 201 | RemoveModem(path); |
| 202 | } |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 203 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 204 | } // namespace shill |