blob: 15931c97104dbe47b4f7687c3ed58cb0574a48ba [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
David Rochbergfa1d31d2012-03-20 10:38:07 -04005#ifndef SHILL_MODEM_MANAGER_H_
6#define SHILL_MODEM_MANAGER_H_
Darin Petkov887f2982011-07-14 16:10:17 -07007
Darin Petkov5c97ac52011-07-19 16:30:49 -07008#include <map>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -08009#include <string>
Darin Petkov5c97ac52011-07-19 16:30:49 -070010#include <tr1/memory>
David Rochberg7cb06f62012-03-05 11:23:44 -050011#include <vector>
Darin Petkov5c97ac52011-07-19 16:30:49 -070012
Darin Petkov887f2982011-07-14 16:10:17 -070013#include <base/basictypes.h>
Darin Petkovc90fe522011-07-15 13:59:47 -070014#include <base/memory/scoped_ptr.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050015#include <base/memory/weak_ptr.h>
Darin Petkov887f2982011-07-14 16:10:17 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
17
David Rochberg7cb06f62012-03-05 11:23:44 -050018#include "shill/dbus_objectmanager_proxy_interface.h"
David Rochbergfa1d31d2012-03-20 10:38:07 -040019#include "shill/dbus_properties_proxy_interface.h"
Darin Petkov887f2982011-07-14 16:10:17 -070020#include "shill/glib.h"
21
Darin Petkov137884a2011-10-26 18:52:47 +020022struct mobile_provider_db;
23
Darin Petkov887f2982011-07-14 16:10:17 -070024namespace shill {
25
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -070026class ActivatingIccidStore;
Ben Chan62028b22012-11-05 11:20:02 -080027class CellularOperatorInfo;
Darin Petkov887f2982011-07-14 16:10:17 -070028class ControlInterface;
David Rochberg7cb06f62012-03-05 11:23:44 -050029class DBusObjectManagerProxyInterface;
David Rochbergfa1d31d2012-03-20 10:38:07 -040030class DBusPropertiesProxyInterface;
Darin Petkov887f2982011-07-14 16:10:17 -070031class EventDispatcher;
32class Manager;
Thieu Le3426c8f2012-01-11 17:35:11 -080033class Metrics;
David Rochbergfa1d31d2012-03-20 10:38:07 -040034class Modem1;
Darin Petkov5c97ac52011-07-19 16:30:49 -070035class Modem;
David Rochbergfa1d31d2012-03-20 10:38:07 -040036class ModemClassic;
Darin Petkovc90fe522011-07-15 13:59:47 -070037class ModemManagerProxyInterface;
Darin Petkovab565bb2011-10-06 02:55:51 -070038class ProxyFactory;
Darin Petkov887f2982011-07-14 16:10:17 -070039
Darin Petkov5c97ac52011-07-19 16:30:49 -070040// Handles a modem manager service and creates and destroys modem instances.
Darin Petkov887f2982011-07-14 16:10:17 -070041class ModemManager {
42 public:
43 ModemManager(const std::string &service,
44 const std::string &path,
45 ControlInterface *control_interface,
46 EventDispatcher *dispatcher,
Thieu Le3426c8f2012-01-11 17:35:11 -080047 Metrics *metrics,
Darin Petkov887f2982011-07-14 16:10:17 -070048 Manager *manager,
Darin Petkov137884a2011-10-26 18:52:47 +020049 GLib *glib,
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -070050 ActivatingIccidStore *activating_iccid_store,
51 CellularOperatorInfo *cellular_operator_info,
Darin Petkov137884a2011-10-26 18:52:47 +020052 mobile_provider_db *provider_db);
David Rochberg7cb06f62012-03-05 11:23:44 -050053 virtual ~ModemManager();
Darin Petkov887f2982011-07-14 16:10:17 -070054
55 // Starts watching for and handling the DBus modem manager service.
56 void Start();
57
58 // Stops watching for the DBus modem manager service and destroys any
Darin Petkov5c97ac52011-07-19 16:30:49 -070059 // associated modems.
Darin Petkov887f2982011-07-14 16:10:17 -070060 void Stop();
61
Darin Petkov41c0e0a2012-01-09 16:38:53 +010062 void OnDeviceInfoAvailable(const std::string &link_name);
63
David Rochberg81030ea2012-03-02 15:44:25 -050064 protected:
65 typedef std::map<std::string, std::tr1::shared_ptr<Modem> > Modems;
66
David Rochbergfa1d31d2012-03-20 10:38:07 -040067 ControlInterface *control_interface() const { return control_interface_; }
68 EventDispatcher *dispatcher() const { return dispatcher_; }
69 Manager *manager() const { return manager_; }
70 Metrics *metrics() const { return metrics_; }
David Rochberg81030ea2012-03-02 15:44:25 -050071 const std::string &owner() const { return owner_; }
Jason Glasgowa585fc32012-06-06 11:04:09 -040072 const std::string &service() const { return service_; }
David Rochberg81030ea2012-03-02 15:44:25 -050073 const std::string &path() const { return path_; }
74 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -070075 ActivatingIccidStore *activating_iccid_store() const {
76 return activating_iccid_store_;
77 }
Ben Chan62028b22012-11-05 11:20:02 -080078 CellularOperatorInfo *cellular_operator_info() const {
79 return cellular_operator_info_;
80 }
David Rochbergfa1d31d2012-03-20 10:38:07 -040081 mobile_provider_db *provider_db() const { return provider_db_; }
David Rochberg81030ea2012-03-02 15:44:25 -050082
83 // Connect/Disconnect to a modem manager service.
84 // Inheriting classes must call this superclass method.
85 virtual void Connect(const std::string &owner);
86 // Inheriting classes must call this superclass method.
87 virtual void Disconnect();
88
David Rochbergfa1d31d2012-03-20 10:38:07 -040089 bool ModemExists(const std::string &path) const;
90 // Put the modem into our modem map
91 void RecordAddedModem(std::tr1::shared_ptr<Modem> modem);
92
93 // Removes a modem on |path|.
94 void RemoveModem(const std::string &path);
95
Darin Petkov887f2982011-07-14 16:10:17 -070096 private:
David Rochberg7cb06f62012-03-05 11:23:44 -050097 friend class ModemManagerCoreTest;
98 friend class ModemManagerClassicTest;
99 friend class ModemManager1Test;
100
Darin Petkov887f2982011-07-14 16:10:17 -0700101 FRIEND_TEST(ModemInfoTest, RegisterModemManager);
David Rochberg7cb06f62012-03-05 11:23:44 -0500102 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
103 FRIEND_TEST(ModemManager1Test, Connect);
104 FRIEND_TEST(ModemManagerClassicTest, Connect);
105 FRIEND_TEST(ModemManagerCoreTest, AddRemoveModem);
106 FRIEND_TEST(ModemManagerCoreTest, Connect);
107 FRIEND_TEST(ModemManagerCoreTest, Disconnect);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400108 FRIEND_TEST(ModemManagerCoreTest, ModemExists);
David Rochberg7cb06f62012-03-05 11:23:44 -0500109 FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish);
110 FRIEND_TEST(ModemManagerCoreTest, Start);
111 FRIEND_TEST(ModemManagerCoreTest, Stop);
Darin Petkov887f2982011-07-14 16:10:17 -0700112
Darin Petkov887f2982011-07-14 16:10:17 -0700113 // DBus service watcher callbacks.
114 static void OnAppear(GDBusConnection *connection,
115 const gchar *name,
116 const gchar *name_owner,
117 gpointer user_data);
118 static void OnVanish(GDBusConnection *connection,
119 const gchar *name,
120 gpointer user_data);
121
Darin Petkovab565bb2011-10-06 02:55:51 -0700122 // Store cached copies of singletons for speed/ease of testing.
123 ProxyFactory *proxy_factory_;
124
Darin Petkov887f2982011-07-14 16:10:17 -0700125 const std::string service_;
126 const std::string path_;
127 guint watcher_id_;
128
Darin Petkovc90fe522011-07-15 13:59:47 -0700129 std::string owner_; // DBus service owner.
Darin Petkov887f2982011-07-14 16:10:17 -0700130
Darin Petkov5c97ac52011-07-19 16:30:49 -0700131 Modems modems_; // Maps a modem |path| to a modem instance.
132
Darin Petkov887f2982011-07-14 16:10:17 -0700133 ControlInterface *control_interface_;
134 EventDispatcher *dispatcher_;
Thieu Le3426c8f2012-01-11 17:35:11 -0800135 Metrics *metrics_;
Darin Petkov887f2982011-07-14 16:10:17 -0700136 Manager *manager_;
137 GLib *glib_;
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -0700138 ActivatingIccidStore *activating_iccid_store_;
Ben Chan62028b22012-11-05 11:20:02 -0800139 CellularOperatorInfo *cellular_operator_info_;
Darin Petkov137884a2011-10-26 18:52:47 +0200140 mobile_provider_db *provider_db_;
Darin Petkov887f2982011-07-14 16:10:17 -0700141
142 DISALLOW_COPY_AND_ASSIGN(ModemManager);
143};
144
Jason Glasgow9c09e362012-04-18 15:16:29 -0400145class ModemManagerClassic : public ModemManager {
David Rochberg81030ea2012-03-02 15:44:25 -0500146 public:
147 ModemManagerClassic(const std::string &service,
148 const std::string &path,
149 ControlInterface *control_interface,
150 EventDispatcher *dispatcher,
151 Metrics *metrics,
152 Manager *manager,
153 GLib *glib,
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -0700154 ActivatingIccidStore *activating_iccid_store,
Ben Chan62028b22012-11-05 11:20:02 -0800155 CellularOperatorInfo *cellular_operator_info,
David Rochberg81030ea2012-03-02 15:44:25 -0500156 mobile_provider_db *provider_db);
157
158 virtual ~ModemManagerClassic();
159
David Rochbergfa1d31d2012-03-20 10:38:07 -0400160 // Called by our dbus proxy
161 void OnDeviceAdded(const std::string &path);
162 void OnDeviceRemoved(const std::string &path);
163
David Rochberg81030ea2012-03-02 15:44:25 -0500164 protected:
165 virtual void Connect(const std::string &owner);
166 virtual void Disconnect();
David Rochbergfa1d31d2012-03-20 10:38:07 -0400167
168 virtual void AddModemClassic(const std::string &path);
169 virtual void InitModemClassic(std::tr1::shared_ptr<ModemClassic> modem);
David Rochberg81030ea2012-03-02 15:44:25 -0500170
171 private:
172 scoped_ptr<ModemManagerProxyInterface> proxy_; // DBus service proxy
David Rochbergfa1d31d2012-03-20 10:38:07 -0400173 scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
David Rochberg81030ea2012-03-02 15:44:25 -0500174
David Rochberg7cb06f62012-03-05 11:23:44 -0500175 FRIEND_TEST(ModemManagerClassicTest, Connect);
David Rochberg81030ea2012-03-02 15:44:25 -0500176
177 DISALLOW_COPY_AND_ASSIGN(ModemManagerClassic);
178};
David Rochberg7cb06f62012-03-05 11:23:44 -0500179
Eric Shienbrood9a245532012-03-07 14:20:39 -0500180class ModemManager1 : public ModemManager {
David Rochberg7cb06f62012-03-05 11:23:44 -0500181 public:
182 ModemManager1(const std::string &service,
183 const std::string &path,
184 ControlInterface *control_interface,
185 EventDispatcher *dispatcher,
186 Metrics *metrics,
187 Manager *manager,
188 GLib *glib,
Prathmesh Prabhu97f317c2013-03-15 16:20:34 -0700189 ActivatingIccidStore *activating_iccid_store,
Ben Chan62028b22012-11-05 11:20:02 -0800190 CellularOperatorInfo *cellular_operator_info,
David Rochberg7cb06f62012-03-05 11:23:44 -0500191 mobile_provider_db *provider_db);
192
193 virtual ~ModemManager1();
194
David Rochberg7cb06f62012-03-05 11:23:44 -0500195 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400196 void AddModem1(const std::string &path,
Ben Chan876efd32012-09-28 15:25:13 -0700197 const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400198 virtual void InitModem1(std::tr1::shared_ptr<Modem1> modem,
Ben Chan876efd32012-09-28 15:25:13 -0700199 const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400200
David Rochberg7cb06f62012-03-05 11:23:44 -0500201 // ModemManager methods
202 virtual void Connect(const std::string &owner);
203 virtual void Disconnect();
David Rochberg7cb06f62012-03-05 11:23:44 -0500204
205 // DBusObjectManagerProxyDelegate signal methods
Eric Shienbrood9a245532012-03-07 14:20:39 -0500206 virtual void OnInterfacesAddedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500207 const ::DBus::Path &object_path,
Ben Chan876efd32012-09-28 15:25:13 -0700208 const DBusInterfaceToProperties &properties);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500209 virtual void OnInterfacesRemovedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500210 const ::DBus::Path &object_path,
211 const std::vector<std::string> &interfaces);
212
213 // DBusObjectManagerProxyDelegate method callbacks
Eric Shienbrood9a245532012-03-07 14:20:39 -0500214 virtual void OnGetManagedObjectsReply(
David Rochberg7cb06f62012-03-05 11:23:44 -0500215 const DBusObjectsWithProperties &objects_with_properties,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500216 const Error &error);
David Rochberg7cb06f62012-03-05 11:23:44 -0500217
218 private:
219 FRIEND_TEST(ModemManager1Test, Connect);
220 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
221
222 scoped_ptr<DBusObjectManagerProxyInterface> proxy_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500223 base::WeakPtrFactory<ModemManager1> weak_ptr_factory_;
David Rochberg7cb06f62012-03-05 11:23:44 -0500224
225 DISALLOW_COPY_AND_ASSIGN(ModemManager1);
226};
Ben Chan876efd32012-09-28 15:25:13 -0700227
Darin Petkov887f2982011-07-14 16:10:17 -0700228} // namespace shill
229
David Rochbergfa1d31d2012-03-20 10:38:07 -0400230#endif // SHILL_MODEM_MANAGER_H_