blob: 74bed030a63937ef2bb914daa3d83b1532da87f2 [file] [log] [blame]
Darin Petkov41c0e0a2012-01-09 16:38:53 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov887f2982011-07-14 16:10:17 -07002// 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 Petkov5c97ac52011-07-19 16:30:49 -07008#include <base/stl_util-inl.h>
Darin Petkov887f2982011-07-14 16:10:17 -07009
Darin Petkov5c97ac52011-07-19 16:30:49 -070010#include "shill/modem.h"
Darin Petkovc90fe522011-07-15 13:59:47 -070011#include "shill/modem_manager_proxy.h"
12#include "shill/proxy_factory.h"
13
Darin Petkov887f2982011-07-14 16:10:17 -070014using std::string;
Darin Petkov5c97ac52011-07-19 16:30:49 -070015using std::tr1::shared_ptr;
16using std::vector;
Darin Petkov887f2982011-07-14 16:10:17 -070017
18namespace shill {
19
20ModemManager::ModemManager(const string &service,
21 const string &path,
22 ControlInterface *control_interface,
23 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080024 Metrics *metrics,
Darin Petkov887f2982011-07-14 16:10:17 -070025 Manager *manager,
Darin Petkov137884a2011-10-26 18:52:47 +020026 GLib *glib,
27 mobile_provider_db *provider_db)
Darin Petkovab565bb2011-10-06 02:55:51 -070028 : proxy_factory_(ProxyFactory::GetInstance()),
29 service_(service),
Darin Petkov887f2982011-07-14 16:10:17 -070030 path_(path),
31 watcher_id_(0),
32 control_interface_(control_interface),
33 dispatcher_(dispatcher),
Thieu Le3426c8f2012-01-11 17:35:11 -080034 metrics_(metrics),
Darin Petkov67d8ecf2011-07-26 16:03:30 -070035 manager_(manager),
Darin Petkov137884a2011-10-26 18:52:47 +020036 glib_(glib),
37 provider_db_(provider_db) {}
Darin Petkov887f2982011-07-14 16:10:17 -070038
39ModemManager::~ModemManager() {
40 Stop();
41}
42
43void ModemManager::Start() {
44 LOG(INFO) << "Start watching modem manager service: " << service_;
45 CHECK_EQ(0, watcher_id_);
Darin Petkovc90fe522011-07-15 13:59:47 -070046 // TODO(petkov): Implement DBus name watching through dbus-c++.
Darin Petkov887f2982011-07-14 16:10:17 -070047 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
56void 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
65void ModemManager::Connect(const string &owner) {
66 owner_ = owner;
Darin Petkovab565bb2011-10-06 02:55:51 -070067 proxy_.reset(proxy_factory_->CreateModemManagerProxy(this, path_, owner_));
Darin Petkov5c97ac52011-07-19 16:30:49 -070068
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 Petkov887f2982011-07-14 16:10:17 -070075}
76
77void ModemManager::Disconnect() {
Darin Petkov5c97ac52011-07-19 16:30:49 -070078 modems_.clear();
Darin Petkov887f2982011-07-14 16:10:17 -070079 owner_.clear();
Darin Petkovc90fe522011-07-15 13:59:47 -070080 proxy_.reset();
Darin Petkov887f2982011-07-14 16:10:17 -070081}
82
mukesh agrawal1830fa12011-09-26 14:31:40 -070083void ModemManager::OnAppear(GDBusConnection */*connection*/,
Darin Petkov887f2982011-07-14 16:10:17 -070084 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 agrawal1830fa12011-09-26 14:31:40 -070092void ModemManager::OnVanish(GDBusConnection */*connection*/,
Darin Petkov887f2982011-07-14 16:10:17 -070093 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 Petkov41c0e0a2012-01-09 16:38:53 +0100100void ModemManager::AddModem(const string &path) {
Darin Petkov5c97ac52011-07-19 16:30:49 -0700101 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 Petkov137884a2011-10-26 18:52:47 +0200107 shared_ptr<Modem> modem(new Modem(owner_,
108 path,
109 control_interface_,
110 dispatcher_,
Thieu Le3426c8f2012-01-11 17:35:11 -0800111 metrics_,
Darin Petkov137884a2011-10-26 18:52:47 +0200112 manager_,
113 provider_db_));
Darin Petkov5c97ac52011-07-19 16:30:49 -0700114 modems_[path] = modem;
Darin Petkove0a312e2011-07-20 13:45:28 -0700115 modem->Init();
Darin Petkov5c97ac52011-07-19 16:30:49 -0700116}
117
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100118void ModemManager::RemoveModem(const string &path) {
Darin Petkov5c97ac52011-07-19 16:30:49 -0700119 LOG(INFO) << "Remove modem: " << path;
120 CHECK(!owner_.empty());
121 modems_.erase(path);
122}
123
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100124void 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 Petkov887f2982011-07-14 16:10:17 -0700130} // namespace shill