Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 5 | #ifndef SHILL_MODEM_MANAGER_H_ |
| 6 | #define SHILL_MODEM_MANAGER_H_ |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 7 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 8 | #include <map> |
Hristo Stefanov | ed2c28c | 2011-11-29 15:37:30 -0800 | [diff] [blame] | 9 | #include <string> |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 10 | #include <tr1/memory> |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 11 | #include <vector> |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 12 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 13 | #include <base/basictypes.h> |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 14 | #include <base/memory/scoped_ptr.h> |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 15 | #include <base/memory/weak_ptr.h> |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 16 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 17 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 18 | #include "shill/dbus_objectmanager_proxy_interface.h" |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 19 | #include "shill/dbus_properties_proxy_interface.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 20 | #include "shill/glib.h" |
| 21 | |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 22 | struct mobile_provider_db; |
| 23 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 24 | namespace shill { |
| 25 | |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 26 | class CellularOperatorInfo; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 27 | class ControlInterface; |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 28 | class DBusObjectManagerProxyInterface; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 29 | class DBusPropertiesProxyInterface; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 30 | class EventDispatcher; |
| 31 | class Manager; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 32 | class Metrics; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 33 | class Modem1; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 34 | class Modem; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 35 | class ModemClassic; |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 36 | class ModemManagerProxyInterface; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 37 | class ProxyFactory; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 38 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 39 | // Handles a modem manager service and creates and destroys modem instances. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 40 | class ModemManager { |
| 41 | public: |
| 42 | ModemManager(const std::string &service, |
| 43 | const std::string &path, |
| 44 | ControlInterface *control_interface, |
| 45 | EventDispatcher *dispatcher, |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 46 | Metrics *metrics, |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 47 | Manager *manager, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 48 | GLib *glib, |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 49 | CellularOperatorInfo *cellular_operator_info_, |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 50 | mobile_provider_db *provider_db); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 51 | virtual ~ModemManager(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 52 | |
| 53 | // Starts watching for and handling the DBus modem manager service. |
| 54 | void Start(); |
| 55 | |
| 56 | // Stops watching for the DBus modem manager service and destroys any |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 57 | // associated modems. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 58 | void Stop(); |
| 59 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 60 | void OnDeviceInfoAvailable(const std::string &link_name); |
| 61 | |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 62 | protected: |
| 63 | typedef std::map<std::string, std::tr1::shared_ptr<Modem> > Modems; |
| 64 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 65 | ControlInterface *control_interface() const { return control_interface_; } |
| 66 | EventDispatcher *dispatcher() const { return dispatcher_; } |
| 67 | Manager *manager() const { return manager_; } |
| 68 | Metrics *metrics() const { return metrics_; } |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 69 | const std::string &owner() const { return owner_; } |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 70 | const std::string &service() const { return service_; } |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 71 | const std::string &path() const { return path_; } |
| 72 | ProxyFactory *proxy_factory() const { return proxy_factory_; } |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 73 | CellularOperatorInfo *cellular_operator_info() const { |
| 74 | return cellular_operator_info_; |
| 75 | } |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 76 | mobile_provider_db *provider_db() const { return provider_db_; } |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 77 | |
| 78 | // Connect/Disconnect to a modem manager service. |
| 79 | // Inheriting classes must call this superclass method. |
| 80 | virtual void Connect(const std::string &owner); |
| 81 | // Inheriting classes must call this superclass method. |
| 82 | virtual void Disconnect(); |
| 83 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 84 | bool ModemExists(const std::string &path) const; |
| 85 | // Put the modem into our modem map |
| 86 | void RecordAddedModem(std::tr1::shared_ptr<Modem> modem); |
| 87 | |
| 88 | // Removes a modem on |path|. |
| 89 | void RemoveModem(const std::string &path); |
| 90 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 91 | private: |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 92 | friend class ModemManagerCoreTest; |
| 93 | friend class ModemManagerClassicTest; |
| 94 | friend class ModemManager1Test; |
| 95 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 96 | FRIEND_TEST(ModemInfoTest, RegisterModemManager); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 97 | FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces); |
| 98 | FRIEND_TEST(ModemManager1Test, Connect); |
| 99 | FRIEND_TEST(ModemManagerClassicTest, Connect); |
| 100 | FRIEND_TEST(ModemManagerCoreTest, AddRemoveModem); |
| 101 | FRIEND_TEST(ModemManagerCoreTest, Connect); |
| 102 | FRIEND_TEST(ModemManagerCoreTest, Disconnect); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 103 | FRIEND_TEST(ModemManagerCoreTest, ModemExists); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 104 | FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish); |
| 105 | FRIEND_TEST(ModemManagerCoreTest, Start); |
| 106 | FRIEND_TEST(ModemManagerCoreTest, Stop); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 107 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 108 | // DBus service watcher callbacks. |
| 109 | static void OnAppear(GDBusConnection *connection, |
| 110 | const gchar *name, |
| 111 | const gchar *name_owner, |
| 112 | gpointer user_data); |
| 113 | static void OnVanish(GDBusConnection *connection, |
| 114 | const gchar *name, |
| 115 | gpointer user_data); |
| 116 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 117 | // Store cached copies of singletons for speed/ease of testing. |
| 118 | ProxyFactory *proxy_factory_; |
| 119 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 120 | const std::string service_; |
| 121 | const std::string path_; |
| 122 | guint watcher_id_; |
| 123 | |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 124 | std::string owner_; // DBus service owner. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 125 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 126 | Modems modems_; // Maps a modem |path| to a modem instance. |
| 127 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 128 | ControlInterface *control_interface_; |
| 129 | EventDispatcher *dispatcher_; |
Thieu Le | 3426c8f | 2012-01-11 17:35:11 -0800 | [diff] [blame] | 130 | Metrics *metrics_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 131 | Manager *manager_; |
| 132 | GLib *glib_; |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 133 | CellularOperatorInfo *cellular_operator_info_; |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 134 | mobile_provider_db *provider_db_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 135 | |
| 136 | DISALLOW_COPY_AND_ASSIGN(ModemManager); |
| 137 | }; |
| 138 | |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 139 | class ModemManagerClassic : public ModemManager { |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 140 | public: |
| 141 | ModemManagerClassic(const std::string &service, |
| 142 | const std::string &path, |
| 143 | ControlInterface *control_interface, |
| 144 | EventDispatcher *dispatcher, |
| 145 | Metrics *metrics, |
| 146 | Manager *manager, |
| 147 | GLib *glib, |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 148 | CellularOperatorInfo *cellular_operator_info, |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 149 | mobile_provider_db *provider_db); |
| 150 | |
| 151 | virtual ~ModemManagerClassic(); |
| 152 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 153 | // Called by our dbus proxy |
| 154 | void OnDeviceAdded(const std::string &path); |
| 155 | void OnDeviceRemoved(const std::string &path); |
| 156 | |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 157 | protected: |
| 158 | virtual void Connect(const std::string &owner); |
| 159 | virtual void Disconnect(); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 160 | |
| 161 | virtual void AddModemClassic(const std::string &path); |
| 162 | virtual void InitModemClassic(std::tr1::shared_ptr<ModemClassic> modem); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 163 | |
| 164 | private: |
| 165 | scoped_ptr<ModemManagerProxyInterface> proxy_; // DBus service proxy |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 166 | scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_; |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 167 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 168 | FRIEND_TEST(ModemManagerClassicTest, Connect); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 169 | |
| 170 | DISALLOW_COPY_AND_ASSIGN(ModemManagerClassic); |
| 171 | }; |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 172 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 173 | class ModemManager1 : public ModemManager { |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 174 | public: |
| 175 | ModemManager1(const std::string &service, |
| 176 | const std::string &path, |
| 177 | ControlInterface *control_interface, |
| 178 | EventDispatcher *dispatcher, |
| 179 | Metrics *metrics, |
| 180 | Manager *manager, |
| 181 | GLib *glib, |
Ben Chan | 62028b2 | 2012-11-05 11:20:02 -0800 | [diff] [blame] | 182 | CellularOperatorInfo *cellular_operator_info, |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 183 | mobile_provider_db *provider_db); |
| 184 | |
| 185 | virtual ~ModemManager1(); |
| 186 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 187 | protected: |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 188 | void AddModem1(const std::string &path, |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 189 | const DBusInterfaceToProperties &properties); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 190 | virtual void InitModem1(std::tr1::shared_ptr<Modem1> modem, |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 191 | const DBusInterfaceToProperties &properties); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 192 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 193 | // ModemManager methods |
| 194 | virtual void Connect(const std::string &owner); |
| 195 | virtual void Disconnect(); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 196 | |
| 197 | // DBusObjectManagerProxyDelegate signal methods |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 198 | virtual void OnInterfacesAddedSignal( |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 199 | const ::DBus::Path &object_path, |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 200 | const DBusInterfaceToProperties &properties); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 201 | virtual void OnInterfacesRemovedSignal( |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 202 | const ::DBus::Path &object_path, |
| 203 | const std::vector<std::string> &interfaces); |
| 204 | |
| 205 | // DBusObjectManagerProxyDelegate method callbacks |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 206 | virtual void OnGetManagedObjectsReply( |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 207 | const DBusObjectsWithProperties &objects_with_properties, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 208 | const Error &error); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 209 | |
| 210 | private: |
| 211 | FRIEND_TEST(ModemManager1Test, Connect); |
| 212 | FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces); |
| 213 | |
| 214 | scoped_ptr<DBusObjectManagerProxyInterface> proxy_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 215 | base::WeakPtrFactory<ModemManager1> weak_ptr_factory_; |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 216 | |
| 217 | DISALLOW_COPY_AND_ASSIGN(ModemManager1); |
| 218 | }; |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 219 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 220 | } // namespace shill |
| 221 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 222 | #endif // SHILL_MODEM_MANAGER_H_ |