blob: b19ad20272d0916384277bed7e9c2af27a0167ba [file] [log] [blame]
Ben Chan5c853ef2012-10-05 00:05:37 -07001// 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
16using base::Bind;
17using std::string;
18using std::tr1::shared_ptr;
19using std::vector;
20
21namespace shill {
22
23ModemManager1::ModemManager1(const string &service,
24 const string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070025 ModemInfo *modem_info)
Ben Chan5c853ef2012-10-05 00:05:37 -070026 : ModemManager(service,
27 path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070028 modem_info),
Ben Chan5c853ef2012-10-05 00:05:37 -070029 weak_ptr_factory_(this) {}
30
31ModemManager1::~ModemManager1() {}
32
33void 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
52void ModemManager1::Disconnect() {
53 ModemManager::Disconnect();
54 proxy_.reset();
55}
56
57void 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 Prabhu27526f12013-03-25 19:42:18 -070065 modem_info()));
Ben Chan5c853ef2012-10-05 00:05:37 -070066 RecordAddedModem(modem1);
67 InitModem1(modem1, properties);
68}
69
70void 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
80void 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
90void 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
106void 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