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