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