Ben Chan | 5c853ef | 2012-10-05 00:05:37 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2012 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/bind.h> |
| 8 | #include <base/stl_util.h> |
| 9 | #include <ModemManager/ModemManager.h> |
| 10 | |
| 11 | #include "shill/error.h" |
| 12 | #include "shill/logging.h" |
| 13 | #include "shill/modem.h" |
| 14 | #include "shill/proxy_factory.h" |
| 15 | |
| 16 | using base::Bind; |
| 17 | using std::string; |
| 18 | using std::tr1::shared_ptr; |
| 19 | using std::vector; |
| 20 | |
| 21 | namespace shill { |
| 22 | |
| 23 | ModemManager1::ModemManager1(const string &service, |
| 24 | const string &path, |
| 25 | ControlInterface *control_interface, |
| 26 | EventDispatcher *dispatcher, |
| 27 | Metrics *metrics, |
| 28 | Manager *manager, |
| 29 | GLib *glib, |
| 30 | mobile_provider_db *provider_db) |
| 31 | : ModemManager(service, |
| 32 | path, |
| 33 | control_interface, |
| 34 | dispatcher, |
| 35 | metrics, |
| 36 | manager, |
| 37 | glib, |
| 38 | provider_db), |
| 39 | weak_ptr_factory_(this) {} |
| 40 | |
| 41 | ModemManager1::~ModemManager1() {} |
| 42 | |
| 43 | void ModemManager1::Connect(const string &supplied_owner) { |
| 44 | ModemManager::Connect(supplied_owner); |
| 45 | proxy_.reset( |
| 46 | proxy_factory()->CreateDBusObjectManagerProxy(path(), owner())); |
| 47 | proxy_->set_interfaces_added_callback( |
| 48 | Bind(&ModemManager1::OnInterfacesAddedSignal, |
| 49 | weak_ptr_factory_.GetWeakPtr())); |
| 50 | proxy_->set_interfaces_removed_callback( |
| 51 | Bind(&ModemManager1::OnInterfacesRemovedSignal, |
| 52 | weak_ptr_factory_.GetWeakPtr())); |
| 53 | |
| 54 | // TODO(rochberg): Make global kDBusDefaultTimeout and use it here |
| 55 | Error error; |
| 56 | proxy_->GetManagedObjects(&error, |
| 57 | Bind(&ModemManager1::OnGetManagedObjectsReply, |
| 58 | weak_ptr_factory_.GetWeakPtr()), |
| 59 | 5000); |
| 60 | } |
| 61 | |
| 62 | void ModemManager1::Disconnect() { |
| 63 | ModemManager::Disconnect(); |
| 64 | proxy_.reset(); |
| 65 | } |
| 66 | |
| 67 | void ModemManager1::AddModem1(const string &path, |
| 68 | const DBusInterfaceToProperties &properties) { |
| 69 | if (ModemExists(path)) { |
| 70 | return; |
| 71 | } |
| 72 | shared_ptr<Modem1> modem1(new Modem1(owner(), |
| 73 | service(), |
| 74 | path, |
| 75 | control_interface(), |
| 76 | dispatcher(), |
| 77 | metrics(), |
| 78 | manager(), |
| 79 | provider_db())); |
| 80 | RecordAddedModem(modem1); |
| 81 | InitModem1(modem1, properties); |
| 82 | } |
| 83 | |
| 84 | void ModemManager1::InitModem1(shared_ptr<Modem1> modem, |
| 85 | const DBusInterfaceToProperties &properties) { |
| 86 | if (modem == NULL) { |
| 87 | return; |
| 88 | } |
| 89 | modem->CreateDeviceMM1(properties); |
| 90 | } |
| 91 | |
| 92 | // signal methods |
| 93 | // Also called by OnGetManagedObjectsReply |
| 94 | void ModemManager1::OnInterfacesAddedSignal( |
| 95 | const ::DBus::Path &object_path, |
| 96 | const DBusInterfaceToProperties &properties) { |
| 97 | if (ContainsKey(properties, MM_DBUS_INTERFACE_MODEM)) { |
| 98 | AddModem1(object_path, properties); |
| 99 | } else { |
| 100 | LOG(ERROR) << "Interfaces added, but not modem interface."; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void ModemManager1::OnInterfacesRemovedSignal( |
| 105 | const ::DBus::Path &object_path, |
| 106 | const vector<string> &interfaces) { |
| 107 | LOG(INFO) << "MM1: Removing interfaces from " << object_path; |
| 108 | if (find(interfaces.begin(), |
| 109 | interfaces.end(), |
| 110 | MM_DBUS_INTERFACE_MODEM) != interfaces.end()) { |
| 111 | RemoveModem(object_path); |
| 112 | } else { |
| 113 | // In theory, a modem could drop, say, 3GPP, but not CDMA. In |
| 114 | // practice, we don't expect this |
| 115 | LOG(ERROR) << "Interfaces removed, but not modem interface"; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // DBusObjectManagerProxy async method call |
| 120 | void ModemManager1::OnGetManagedObjectsReply( |
| 121 | const DBusObjectsWithProperties &objects, |
| 122 | const Error &error) { |
| 123 | if (error.IsSuccess()) { |
| 124 | DBusObjectsWithProperties::const_iterator m; |
| 125 | for (m = objects.begin(); m != objects.end(); ++m) { |
| 126 | OnInterfacesAddedSignal(m->first, m->second); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | } // namespace shill |