Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 1 | // 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_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, |
| 24 | Manager *manager, |
| 25 | GLib *glib) |
| 26 | : service_(service), |
| 27 | path_(path), |
| 28 | watcher_id_(0), |
| 29 | control_interface_(control_interface), |
| 30 | dispatcher_(dispatcher), |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 31 | manager_(manager), |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 32 | glib_(glib) {} |
| 33 | |
| 34 | ModemManager::~ModemManager() { |
| 35 | Stop(); |
| 36 | } |
| 37 | |
| 38 | void ModemManager::Start() { |
| 39 | LOG(INFO) << "Start watching modem manager service: " << service_; |
| 40 | CHECK_EQ(0, watcher_id_); |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 41 | // TODO(petkov): Implement DBus name watching through dbus-c++. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 42 | watcher_id_ = glib_->BusWatchName(G_BUS_TYPE_SYSTEM, |
| 43 | service_.c_str(), |
| 44 | G_BUS_NAME_WATCHER_FLAGS_NONE, |
| 45 | OnAppear, |
| 46 | OnVanish, |
| 47 | this, |
| 48 | NULL); |
| 49 | } |
| 50 | |
| 51 | void ModemManager::Stop() { |
| 52 | LOG(INFO) << "Stop watching modem manager service: " << service_; |
| 53 | if (watcher_id_) { |
| 54 | glib_->BusUnwatchName(watcher_id_); |
| 55 | watcher_id_ = 0; |
| 56 | } |
| 57 | Disconnect(); |
| 58 | } |
| 59 | |
| 60 | void ModemManager::Connect(const string &owner) { |
| 61 | owner_ = owner; |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 62 | proxy_.reset( |
| 63 | ProxyFactory::factory()->CreateModemManagerProxy(this, path_, owner_)); |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 64 | |
| 65 | // TODO(petkov): Switch to asynchronous calls (crosbug.com/17583). |
| 66 | vector<DBus::Path> devices = proxy_->EnumerateDevices(); |
| 67 | for (vector<DBus::Path>::const_iterator it = devices.begin(); |
| 68 | it != devices.end(); ++it) { |
| 69 | AddModem(*it); |
| 70 | } |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void ModemManager::Disconnect() { |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 74 | modems_.clear(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 75 | owner_.clear(); |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 76 | proxy_.reset(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 77 | } |
| 78 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 79 | void ModemManager::OnAppear(GDBusConnection */*connection*/, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 80 | const gchar *name, |
| 81 | const gchar *name_owner, |
| 82 | gpointer user_data) { |
| 83 | LOG(INFO) << "Modem manager " << name << " appeared. Owner: " << name_owner; |
| 84 | ModemManager *manager = reinterpret_cast<ModemManager *>(user_data); |
| 85 | manager->Connect(name_owner); |
| 86 | } |
| 87 | |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 88 | void ModemManager::OnVanish(GDBusConnection */*connection*/, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 89 | const gchar *name, |
| 90 | gpointer user_data) { |
| 91 | LOG(INFO) << "Modem manager " << name << " vanished."; |
| 92 | ModemManager *manager = reinterpret_cast<ModemManager *>(user_data); |
| 93 | manager->Disconnect(); |
| 94 | } |
| 95 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 96 | void ModemManager::AddModem(const std::string &path) { |
| 97 | LOG(INFO) << "Add modem: " << path; |
| 98 | CHECK(!owner_.empty()); |
| 99 | if (ContainsKey(modems_, path)) { |
| 100 | LOG(INFO) << "Modem already exists; ignored."; |
| 101 | return; |
| 102 | } |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 103 | shared_ptr<Modem> modem( |
| 104 | new Modem(owner_, path, control_interface_, dispatcher_, manager_)); |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 105 | modems_[path] = modem; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 106 | modem->Init(); |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | void ModemManager::RemoveModem(const std::string &path) { |
| 110 | LOG(INFO) << "Remove modem: " << path; |
| 111 | CHECK(!owner_.empty()); |
| 112 | modems_.erase(path); |
| 113 | } |
| 114 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 115 | } // namespace shill |