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 | |
| 7 | #include <base/logging.h> |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 8 | #include <base/stl_util-inl.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 9 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 10 | #include "shill/modem.h" |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 11 | #include "shill/modem_manager_proxy.h" |
| 12 | #include "shill/proxy_factory.h" |
| 13 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 14 | using std::string; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 15 | using std::tr1::shared_ptr; |
| 16 | using std::vector; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | ModemManager::ModemManager(const string &service, |
| 21 | const string &path, |
| 22 | ControlInterface *control_interface, |
| 23 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 24 | Metrics *metrics, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 25 | Manager *manager, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 26 | GLib *glib, |
| 27 | mobile_provider_db *provider_db) |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 28 | : proxy_factory_(ProxyFactory::GetInstance()), |
| 29 | service_(service), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 30 | path_(path), |
| 31 | watcher_id_(0), |
| 32 | control_interface_(control_interface), |
| 33 | dispatcher_(dispatcher), |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 34 | metrics_(metrics), |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 35 | manager_(manager), |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 36 | glib_(glib), |
| 37 | provider_db_(provider_db) {} |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 38 | |
| 39 | ModemManager::~ModemManager() { |
| 40 | Stop(); |
| 41 | } |
| 42 | |
| 43 | void ModemManager::Start() { |
| 44 | LOG(INFO) << "Start watching modem manager service: " << service_; |
| 45 | CHECK_EQ(0, watcher_id_); |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 46 | // TODO(petkov): Implement DBus name watching through dbus-c++. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 47 | watcher_id_ = glib_->BusWatchName(G_BUS_TYPE_SYSTEM, |
| 48 | service_.c_str(), |
| 49 | G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 50 | OnAppear, |
| 51 | OnVanish, |
| 52 | this, |
| 53 | NULL); |
| 54 | } |
| 55 | |
| 56 | void ModemManager::Stop() { |
| 57 | LOG(INFO) << "Stop watching modem manager service: " << service_; |
| 58 | if (watcher_id_) { |
| 59 | glib_->BusUnwatchName(watcher_id_); |
| 60 | watcher_id_ = 0; |
| 61 | } |
| 62 | Disconnect(); |
| 63 | } |
| 64 | |
| 65 | void ModemManager::Connect(const string &owner) { |
| 66 | owner_ = owner; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 67 | proxy_.reset(proxy_factory_->CreateModemManagerProxy(this, path_, owner_)); |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 68 | |
| 69 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 70 | vector<DBus::Path> devices = proxy_->EnumerateDevices(); |
| 71 | for (vector<DBus::Path>::const_iterator it = devices.begin(); |
| 72 | it != devices.end(); ++it) { |
| 73 | AddModem(*it); |
| 74 | } |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void ModemManager::Disconnect() { |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 78 | modems_.clear(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 79 | owner_.clear(); |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 80 | proxy_.reset(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 81 | } |
| 82 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 83 | void ModemManager::OnAppear(GDBusConnection */*connection*/, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 84 | const gchar *name, |
| 85 | const gchar *name_owner, |
| 86 | gpointer user_data) { |
| 87 | LOG(INFO) << "Modem manager " << name << " appeared. Owner: " << name_owner; |
| 88 | ModemManager *manager = reinterpret_cast<ModemManager *>(user_data); |
| 89 | manager->Connect(name_owner); |
| 90 | } |
| 91 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 92 | void ModemManager::OnVanish(GDBusConnection */*connection*/, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 93 | const gchar *name, |
| 94 | gpointer user_data) { |
| 95 | LOG(INFO) << "Modem manager " << name << " vanished."; |
| 96 | ModemManager *manager = reinterpret_cast<ModemManager *>(user_data); |
| 97 | manager->Disconnect(); |
| 98 | } |
| 99 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 100 | void ModemManager::AddModem(const string &path) { |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 101 | LOG(INFO) << "Add modem: " << path; |
| 102 | CHECK(!owner_.empty()); |
| 103 | if (ContainsKey(modems_, path)) { |
| 104 | LOG(INFO) << "Modem already exists; ignored."; |
| 105 | return; |
| 106 | } |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 107 | shared_ptr<Modem> modem(new Modem(owner_, |
| 108 | path, |
| 109 | control_interface_, |
| 110 | dispatcher_, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 111 | metrics_, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 112 | manager_, |
| 113 | provider_db_)); |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 114 | modems_[path] = modem; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 115 | modem->Init(); |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 118 | void ModemManager::RemoveModem(const string &path) { |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 119 | LOG(INFO) << "Remove modem: " << path; |
| 120 | CHECK(!owner_.empty()); |
| 121 | modems_.erase(path); |
| 122 | } |
| 123 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 124 | void ModemManager::OnDeviceInfoAvailable(const string &link_name) { |
| 125 | for (Modems::iterator it = modems_.begin(); it != modems_.end(); ++it) { |
| 126 | it->second->OnDeviceInfoAvailable(link_name); |
| 127 | } |
| 128 | } |
| 129 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 130 | } // namespace shill |