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