blob: 0b1434e29c5833f3bc3b8314ff17193a3d1e74d1 [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
Ben Chanc54afe52014-11-05 10:28:08 -08005#ifndef SHILL_CELLULAR_MODEM_MANAGER_H_
6#define SHILL_CELLULAR_MODEM_MANAGER_H_
Darin Petkov887f2982011-07-14 16:10:17 -07007
Darin Petkov5c97ac52011-07-19 16:30:49 -07008#include <map>
Alex Vakulenko8a532292014-06-16 17:18:44 -07009#include <memory>
Alex Vakulenkoa41ab512014-07-23 14:24:23 -070010#include <string>
David Rochberg7cb06f62012-03-05 11:23:44 -050011#include <vector>
Darin Petkov5c97ac52011-07-19 16:30:49 -070012
Ben Chancc67c522014-09-03 07:19:18 -070013#include <base/macros.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050014#include <base/memory/weak_ptr.h>
Darin Petkov887f2982011-07-14 16:10:17 -070015#include <gtest/gtest_prod.h> // for FRIEND_TEST
16
Ben Chanc54afe52014-11-05 10:28:08 -080017#include "shill/cellular/dbus_objectmanager_proxy_interface.h"
18#include "shill/cellular/modem_info.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
22namespace shill {
23
Ben Chan66174a12014-01-08 21:27:00 -080024class DBusNameWatcher;
David Rochberg7cb06f62012-03-05 11:23:44 -050025class DBusObjectManagerProxyInterface;
David Rochbergfa1d31d2012-03-20 10:38:07 -040026class DBusPropertiesProxyInterface;
David Rochbergfa1d31d2012-03-20 10:38:07 -040027class Modem1;
Darin Petkov5c97ac52011-07-19 16:30:49 -070028class Modem;
David Rochbergfa1d31d2012-03-20 10:38:07 -040029class ModemClassic;
Darin Petkovc90fe522011-07-15 13:59:47 -070030class ModemManagerProxyInterface;
Darin Petkovab565bb2011-10-06 02:55:51 -070031class ProxyFactory;
Darin Petkov887f2982011-07-14 16:10:17 -070032
Darin Petkov5c97ac52011-07-19 16:30:49 -070033// Handles a modem manager service and creates and destroys modem instances.
Darin Petkov887f2982011-07-14 16:10:17 -070034class ModemManager {
35 public:
36 ModemManager(const std::string &service,
37 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070038 ModemInfo *modem_info);
David Rochberg7cb06f62012-03-05 11:23:44 -050039 virtual ~ModemManager();
Darin Petkov887f2982011-07-14 16:10:17 -070040
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 Petkov5c97ac52011-07-19 16:30:49 -070045 // associated modems.
Darin Petkov887f2982011-07-14 16:10:17 -070046 void Stop();
47
Ben Chan66174a12014-01-08 21:27:00 -080048 // DBusNameWatcher callbacks.
49 void OnAppear(const std::string &name, const std::string &owner);
50 void OnVanish(const std::string &name);
51
Darin Petkov41c0e0a2012-01-09 16:38:53 +010052 void OnDeviceInfoAvailable(const std::string &link_name);
53
David Rochberg81030ea2012-03-02 15:44:25 -050054 protected:
Ben Chane2ee5e02014-09-19 19:29:42 -070055 typedef std::map<std::string, std::shared_ptr<Modem>> Modems;
David Rochberg81030ea2012-03-02 15:44:25 -050056
57 const std::string &owner() const { return owner_; }
Jason Glasgowa585fc32012-06-06 11:04:09 -040058 const std::string &service() const { return service_; }
David Rochberg81030ea2012-03-02 15:44:25 -050059 const std::string &path() const { return path_; }
60 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070061 ModemInfo *modem_info() const { return modem_info_; }
David Rochberg81030ea2012-03-02 15:44:25 -050062
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 Rochbergfa1d31d2012-03-20 10:38:07 -040069 bool ModemExists(const std::string &path) const;
70 // Put the modem into our modem map
Alex Vakulenko8a532292014-06-16 17:18:44 -070071 void RecordAddedModem(std::shared_ptr<Modem> modem);
David Rochbergfa1d31d2012-03-20 10:38:07 -040072
73 // Removes a modem on |path|.
74 void RemoveModem(const std::string &path);
75
Darin Petkov887f2982011-07-14 16:10:17 -070076 private:
David Rochberg7cb06f62012-03-05 11:23:44 -050077 friend class ModemManagerCoreTest;
78 friend class ModemManagerClassicTest;
79 friend class ModemManager1Test;
80
Darin Petkov887f2982011-07-14 16:10:17 -070081 FRIEND_TEST(ModemInfoTest, RegisterModemManager);
David Rochberg7cb06f62012-03-05 11:23:44 -050082 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
83 FRIEND_TEST(ModemManager1Test, Connect);
84 FRIEND_TEST(ModemManagerClassicTest, Connect);
85 FRIEND_TEST(ModemManagerCoreTest, AddRemoveModem);
Ben Chan66174a12014-01-08 21:27:00 -080086 FRIEND_TEST(ModemManagerCoreTest, ConnectDisconnect);
David Rochberg7cb06f62012-03-05 11:23:44 -050087 FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish);
Ben Chan66174a12014-01-08 21:27:00 -080088 FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServiceAbsent);
89 FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServicePresent);
Darin Petkov887f2982011-07-14 16:10:17 -070090
Darin Petkovab565bb2011-10-06 02:55:51 -070091 // Store cached copies of singletons for speed/ease of testing.
92 ProxyFactory *proxy_factory_;
93
Darin Petkov887f2982011-07-14 16:10:17 -070094 const std::string service_;
95 const std::string path_;
Ben Chanc20ed132014-10-16 12:25:03 -070096 std::unique_ptr<DBusNameWatcher> name_watcher_;
Darin Petkov887f2982011-07-14 16:10:17 -070097
Darin Petkovc90fe522011-07-15 13:59:47 -070098 std::string owner_; // DBus service owner.
Darin Petkov887f2982011-07-14 16:10:17 -070099
Darin Petkov5c97ac52011-07-19 16:30:49 -0700100 Modems modems_; // Maps a modem |path| to a modem instance.
101
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700102 ModemInfo *modem_info_;
Darin Petkov887f2982011-07-14 16:10:17 -0700103
104 DISALLOW_COPY_AND_ASSIGN(ModemManager);
105};
106
Jason Glasgow9c09e362012-04-18 15:16:29 -0400107class ModemManagerClassic : public ModemManager {
David Rochberg81030ea2012-03-02 15:44:25 -0500108 public:
109 ModemManagerClassic(const std::string &service,
110 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700111 ModemInfo *modem_info);
David Rochberg81030ea2012-03-02 15:44:25 -0500112
Ben Chan5ea763b2014-08-13 11:07:54 -0700113 ~ModemManagerClassic() override;
David Rochberg81030ea2012-03-02 15:44:25 -0500114
David Rochbergfa1d31d2012-03-20 10:38:07 -0400115 // Called by our dbus proxy
116 void OnDeviceAdded(const std::string &path);
117 void OnDeviceRemoved(const std::string &path);
118
David Rochberg81030ea2012-03-02 15:44:25 -0500119 protected:
Yunlian Jiang6acd9662015-01-30 08:36:10 -0800120 void Connect(const std::string &owner) override;
121 void Disconnect() override;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400122
123 virtual void AddModemClassic(const std::string &path);
Alex Vakulenko8a532292014-06-16 17:18:44 -0700124 virtual void InitModemClassic(std::shared_ptr<ModemClassic> modem);
David Rochberg81030ea2012-03-02 15:44:25 -0500125
126 private:
Ben Chanc20ed132014-10-16 12:25:03 -0700127 std::unique_ptr<ModemManagerProxyInterface> proxy_; // DBus service proxy
128 std::unique_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
David Rochberg81030ea2012-03-02 15:44:25 -0500129
David Rochberg7cb06f62012-03-05 11:23:44 -0500130 FRIEND_TEST(ModemManagerClassicTest, Connect);
David Rochberg81030ea2012-03-02 15:44:25 -0500131
132 DISALLOW_COPY_AND_ASSIGN(ModemManagerClassic);
133};
David Rochberg7cb06f62012-03-05 11:23:44 -0500134
Eric Shienbrood9a245532012-03-07 14:20:39 -0500135class ModemManager1 : public ModemManager {
David Rochberg7cb06f62012-03-05 11:23:44 -0500136 public:
137 ModemManager1(const std::string &service,
138 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700139 ModemInfo *modem_info);
David Rochberg7cb06f62012-03-05 11:23:44 -0500140
Ben Chan5ea763b2014-08-13 11:07:54 -0700141 ~ModemManager1() override;
David Rochberg7cb06f62012-03-05 11:23:44 -0500142
David Rochberg7cb06f62012-03-05 11:23:44 -0500143 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400144 void AddModem1(const std::string &path,
Ben Chan876efd32012-09-28 15:25:13 -0700145 const DBusInterfaceToProperties &properties);
Alex Vakulenko8a532292014-06-16 17:18:44 -0700146 virtual void InitModem1(std::shared_ptr<Modem1> modem,
Ben Chan876efd32012-09-28 15:25:13 -0700147 const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400148
David Rochberg7cb06f62012-03-05 11:23:44 -0500149 // ModemManager methods
Yunlian Jiang6acd9662015-01-30 08:36:10 -0800150 void Connect(const std::string &owner) override;
151 void Disconnect() override;
David Rochberg7cb06f62012-03-05 11:23:44 -0500152
153 // DBusObjectManagerProxyDelegate signal methods
Eric Shienbrood9a245532012-03-07 14:20:39 -0500154 virtual void OnInterfacesAddedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500155 const ::DBus::Path &object_path,
Ben Chan876efd32012-09-28 15:25:13 -0700156 const DBusInterfaceToProperties &properties);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500157 virtual void OnInterfacesRemovedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500158 const ::DBus::Path &object_path,
159 const std::vector<std::string> &interfaces);
160
161 // DBusObjectManagerProxyDelegate method callbacks
Eric Shienbrood9a245532012-03-07 14:20:39 -0500162 virtual void OnGetManagedObjectsReply(
David Rochberg7cb06f62012-03-05 11:23:44 -0500163 const DBusObjectsWithProperties &objects_with_properties,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500164 const Error &error);
David Rochberg7cb06f62012-03-05 11:23:44 -0500165
166 private:
Ben Chan1e2ba232014-01-27 16:35:45 -0800167 friend class ModemManager1Test;
David Rochberg7cb06f62012-03-05 11:23:44 -0500168 FRIEND_TEST(ModemManager1Test, Connect);
169 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
170
Ben Chanc20ed132014-10-16 12:25:03 -0700171 std::unique_ptr<DBusObjectManagerProxyInterface> proxy_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500172 base::WeakPtrFactory<ModemManager1> weak_ptr_factory_;
David Rochberg7cb06f62012-03-05 11:23:44 -0500173
174 DISALLOW_COPY_AND_ASSIGN(ModemManager1);
175};
Ben Chan876efd32012-09-28 15:25:13 -0700176
Darin Petkov887f2982011-07-14 16:10:17 -0700177} // namespace shill
178
Ben Chanc54afe52014-11-05 10:28:08 -0800179#endif // SHILL_CELLULAR_MODEM_MANAGER_H_