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