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" |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 21 | #include "shill/modem_info.h" |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 22 | |
Darin Petkov | 137884a | 2011-10-26 18:52:47 +0200 | [diff] [blame] | 23 | struct mobile_provider_db; |
| 24 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 25 | namespace shill { |
| 26 | |
Ben Chan | 66174a1 | 2014-01-08 21:27:00 -0800 | [diff] [blame^] | 27 | class DBusNameWatcher; |
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; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 30 | class Modem1; |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 31 | class Modem; |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 32 | class ModemClassic; |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 33 | class ModemManagerProxyInterface; |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 34 | class ProxyFactory; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 35 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 36 | // Handles a modem manager service and creates and destroys modem instances. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 37 | class ModemManager { |
| 38 | public: |
| 39 | ModemManager(const std::string &service, |
| 40 | const std::string &path, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 41 | ModemInfo *modem_info); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 42 | virtual ~ModemManager(); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 43 | |
| 44 | // Starts watching for and handling the DBus modem manager service. |
| 45 | void Start(); |
| 46 | |
| 47 | // Stops watching for the DBus modem manager service and destroys any |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 48 | // associated modems. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 49 | void Stop(); |
| 50 | |
Ben Chan | 66174a1 | 2014-01-08 21:27:00 -0800 | [diff] [blame^] | 51 | // DBusNameWatcher callbacks. |
| 52 | void OnAppear(const std::string &name, const std::string &owner); |
| 53 | void OnVanish(const std::string &name); |
| 54 | |
Darin Petkov | 41c0e0a | 2012-01-09 16:38:53 +0100 | [diff] [blame] | 55 | void OnDeviceInfoAvailable(const std::string &link_name); |
| 56 | |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 57 | protected: |
| 58 | typedef std::map<std::string, std::tr1::shared_ptr<Modem> > Modems; |
| 59 | |
| 60 | const std::string &owner() const { return owner_; } |
Jason Glasgow | a585fc3 | 2012-06-06 11:04:09 -0400 | [diff] [blame] | 61 | const std::string &service() const { return service_; } |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 62 | const std::string &path() const { return path_; } |
| 63 | ProxyFactory *proxy_factory() const { return proxy_factory_; } |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 64 | ModemInfo *modem_info() const { return modem_info_; } |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 65 | |
| 66 | // Connect/Disconnect to a modem manager service. |
| 67 | // Inheriting classes must call this superclass method. |
| 68 | virtual void Connect(const std::string &owner); |
| 69 | // Inheriting classes must call this superclass method. |
| 70 | virtual void Disconnect(); |
| 71 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 72 | bool ModemExists(const std::string &path) const; |
| 73 | // Put the modem into our modem map |
| 74 | void RecordAddedModem(std::tr1::shared_ptr<Modem> modem); |
| 75 | |
| 76 | // Removes a modem on |path|. |
| 77 | void RemoveModem(const std::string &path); |
| 78 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 79 | private: |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 80 | friend class ModemManagerCoreTest; |
| 81 | friend class ModemManagerClassicTest; |
| 82 | friend class ModemManager1Test; |
| 83 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 84 | FRIEND_TEST(ModemInfoTest, RegisterModemManager); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 85 | FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces); |
| 86 | FRIEND_TEST(ModemManager1Test, Connect); |
| 87 | FRIEND_TEST(ModemManagerClassicTest, Connect); |
| 88 | FRIEND_TEST(ModemManagerCoreTest, AddRemoveModem); |
Ben Chan | 66174a1 | 2014-01-08 21:27:00 -0800 | [diff] [blame^] | 89 | FRIEND_TEST(ModemManagerCoreTest, ConnectDisconnect); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 90 | FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish); |
Ben Chan | 66174a1 | 2014-01-08 21:27:00 -0800 | [diff] [blame^] | 91 | FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServiceAbsent); |
| 92 | FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServicePresent); |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 93 | |
Darin Petkov | ab565bb | 2011-10-06 02:55:51 -0700 | [diff] [blame] | 94 | // Store cached copies of singletons for speed/ease of testing. |
| 95 | ProxyFactory *proxy_factory_; |
| 96 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 97 | const std::string service_; |
| 98 | const std::string path_; |
Ben Chan | 66174a1 | 2014-01-08 21:27:00 -0800 | [diff] [blame^] | 99 | scoped_ptr<DBusNameWatcher> name_watcher_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 100 | |
Darin Petkov | c90fe52 | 2011-07-15 13:59:47 -0700 | [diff] [blame] | 101 | std::string owner_; // DBus service owner. |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 102 | |
Darin Petkov | 5c97ac5 | 2011-07-19 16:30:49 -0700 | [diff] [blame] | 103 | Modems modems_; // Maps a modem |path| to a modem instance. |
| 104 | |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 105 | ModemInfo *modem_info_; |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 106 | |
| 107 | DISALLOW_COPY_AND_ASSIGN(ModemManager); |
| 108 | }; |
| 109 | |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 110 | class ModemManagerClassic : public ModemManager { |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 111 | public: |
| 112 | ModemManagerClassic(const std::string &service, |
| 113 | const std::string &path, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 114 | ModemInfo *modem_info); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 115 | |
| 116 | virtual ~ModemManagerClassic(); |
| 117 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 118 | // Called by our dbus proxy |
| 119 | void OnDeviceAdded(const std::string &path); |
| 120 | void OnDeviceRemoved(const std::string &path); |
| 121 | |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 122 | protected: |
| 123 | virtual void Connect(const std::string &owner); |
| 124 | virtual void Disconnect(); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 125 | |
| 126 | virtual void AddModemClassic(const std::string &path); |
| 127 | virtual void InitModemClassic(std::tr1::shared_ptr<ModemClassic> modem); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | scoped_ptr<ModemManagerProxyInterface> proxy_; // DBus service proxy |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 131 | scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_; |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 132 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 133 | FRIEND_TEST(ModemManagerClassicTest, Connect); |
David Rochberg | 81030ea | 2012-03-02 15:44:25 -0500 | [diff] [blame] | 134 | |
| 135 | DISALLOW_COPY_AND_ASSIGN(ModemManagerClassic); |
| 136 | }; |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 137 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 138 | class ModemManager1 : public ModemManager { |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 139 | public: |
| 140 | ModemManager1(const std::string &service, |
| 141 | const std::string &path, |
Prathmesh Prabhu | 27526f1 | 2013-03-25 19:42:18 -0700 | [diff] [blame] | 142 | ModemInfo *modem_info); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 143 | |
| 144 | virtual ~ModemManager1(); |
| 145 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 146 | protected: |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 147 | void AddModem1(const std::string &path, |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 148 | const DBusInterfaceToProperties &properties); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 149 | virtual void InitModem1(std::tr1::shared_ptr<Modem1> modem, |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 150 | const DBusInterfaceToProperties &properties); |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 151 | |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 152 | // ModemManager methods |
| 153 | virtual void Connect(const std::string &owner); |
| 154 | virtual void Disconnect(); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 155 | |
| 156 | // DBusObjectManagerProxyDelegate signal methods |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 157 | virtual void OnInterfacesAddedSignal( |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 158 | const ::DBus::Path &object_path, |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 159 | const DBusInterfaceToProperties &properties); |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 160 | virtual void OnInterfacesRemovedSignal( |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 161 | const ::DBus::Path &object_path, |
| 162 | const std::vector<std::string> &interfaces); |
| 163 | |
| 164 | // DBusObjectManagerProxyDelegate method callbacks |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 165 | virtual void OnGetManagedObjectsReply( |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 166 | const DBusObjectsWithProperties &objects_with_properties, |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 167 | const Error &error); |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 168 | |
| 169 | private: |
| 170 | FRIEND_TEST(ModemManager1Test, Connect); |
| 171 | FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces); |
| 172 | |
| 173 | scoped_ptr<DBusObjectManagerProxyInterface> proxy_; |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 174 | base::WeakPtrFactory<ModemManager1> weak_ptr_factory_; |
David Rochberg | 7cb06f6 | 2012-03-05 11:23:44 -0500 | [diff] [blame] | 175 | |
| 176 | DISALLOW_COPY_AND_ASSIGN(ModemManager1); |
| 177 | }; |
Ben Chan | 876efd3 | 2012-09-28 15:25:13 -0700 | [diff] [blame] | 178 | |
Darin Petkov | 887f298 | 2011-07-14 16:10:17 -0700 | [diff] [blame] | 179 | } // namespace shill |
| 180 | |
David Rochberg | fa1d31d | 2012-03-20 10:38:07 -0400 | [diff] [blame] | 181 | #endif // SHILL_MODEM_MANAGER_H_ |