blob: 12bc00bba38adc0ac4b5a2bb4dcba90a93280107 [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
David Rochbergfa1d31d2012-03-20 10:38:07 -04005#ifndef SHILL_MODEM_MANAGER_H_
6#define SHILL_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>
Darin Petkovc90fe522011-07-15 13:59:47 -070014#include <base/memory/scoped_ptr.h>
Eric Shienbrood9a245532012-03-07 14:20:39 -050015#include <base/memory/weak_ptr.h>
Darin Petkov887f2982011-07-14 16:10:17 -070016#include <gtest/gtest_prod.h> // for FRIEND_TEST
17
David Rochberg7cb06f62012-03-05 11:23:44 -050018#include "shill/dbus_objectmanager_proxy_interface.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"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070021#include "shill/modem_info.h"
Darin Petkov887f2982011-07-14 16:10:17 -070022
23namespace shill {
24
Ben Chan66174a12014-01-08 21:27:00 -080025class DBusNameWatcher;
David Rochberg7cb06f62012-03-05 11:23:44 -050026class DBusObjectManagerProxyInterface;
David Rochbergfa1d31d2012-03-20 10:38:07 -040027class DBusPropertiesProxyInterface;
David Rochbergfa1d31d2012-03-20 10:38:07 -040028class Modem1;
Darin Petkov5c97ac52011-07-19 16:30:49 -070029class Modem;
David Rochbergfa1d31d2012-03-20 10:38:07 -040030class ModemClassic;
Darin Petkovc90fe522011-07-15 13:59:47 -070031class ModemManagerProxyInterface;
Darin Petkovab565bb2011-10-06 02:55:51 -070032class ProxyFactory;
Darin Petkov887f2982011-07-14 16:10:17 -070033
Darin Petkov5c97ac52011-07-19 16:30:49 -070034// Handles a modem manager service and creates and destroys modem instances.
Darin Petkov887f2982011-07-14 16:10:17 -070035class ModemManager {
36 public:
37 ModemManager(const std::string &service,
38 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070039 ModemInfo *modem_info);
David Rochberg7cb06f62012-03-05 11:23:44 -050040 virtual ~ModemManager();
Darin Petkov887f2982011-07-14 16:10:17 -070041
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 Petkov5c97ac52011-07-19 16:30:49 -070046 // associated modems.
Darin Petkov887f2982011-07-14 16:10:17 -070047 void Stop();
48
Ben Chan66174a12014-01-08 21:27:00 -080049 // DBusNameWatcher callbacks.
50 void OnAppear(const std::string &name, const std::string &owner);
51 void OnVanish(const std::string &name);
52
Darin Petkov41c0e0a2012-01-09 16:38:53 +010053 void OnDeviceInfoAvailable(const std::string &link_name);
54
David Rochberg81030ea2012-03-02 15:44:25 -050055 protected:
Alex Vakulenko8a532292014-06-16 17:18:44 -070056 typedef std::map<std::string, std::shared_ptr<Modem> > Modems;
David Rochberg81030ea2012-03-02 15:44:25 -050057
58 const std::string &owner() const { return owner_; }
Jason Glasgowa585fc32012-06-06 11:04:09 -040059 const std::string &service() const { return service_; }
David Rochberg81030ea2012-03-02 15:44:25 -050060 const std::string &path() const { return path_; }
61 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070062 ModemInfo *modem_info() const { return modem_info_; }
David Rochberg81030ea2012-03-02 15:44:25 -050063
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 Rochbergfa1d31d2012-03-20 10:38:07 -040070 bool ModemExists(const std::string &path) const;
71 // Put the modem into our modem map
Alex Vakulenko8a532292014-06-16 17:18:44 -070072 void RecordAddedModem(std::shared_ptr<Modem> modem);
David Rochbergfa1d31d2012-03-20 10:38:07 -040073
74 // Removes a modem on |path|.
75 void RemoveModem(const std::string &path);
76
Darin Petkov887f2982011-07-14 16:10:17 -070077 private:
David Rochberg7cb06f62012-03-05 11:23:44 -050078 friend class ModemManagerCoreTest;
79 friend class ModemManagerClassicTest;
80 friend class ModemManager1Test;
81
Darin Petkov887f2982011-07-14 16:10:17 -070082 FRIEND_TEST(ModemInfoTest, RegisterModemManager);
David Rochberg7cb06f62012-03-05 11:23:44 -050083 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
84 FRIEND_TEST(ModemManager1Test, Connect);
85 FRIEND_TEST(ModemManagerClassicTest, Connect);
86 FRIEND_TEST(ModemManagerCoreTest, AddRemoveModem);
Ben Chan66174a12014-01-08 21:27:00 -080087 FRIEND_TEST(ModemManagerCoreTest, ConnectDisconnect);
David Rochberg7cb06f62012-03-05 11:23:44 -050088 FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish);
Ben Chan66174a12014-01-08 21:27:00 -080089 FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServiceAbsent);
90 FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServicePresent);
Darin Petkov887f2982011-07-14 16:10:17 -070091
Darin Petkovab565bb2011-10-06 02:55:51 -070092 // Store cached copies of singletons for speed/ease of testing.
93 ProxyFactory *proxy_factory_;
94
Darin Petkov887f2982011-07-14 16:10:17 -070095 const std::string service_;
96 const std::string path_;
Ben Chan66174a12014-01-08 21:27:00 -080097 scoped_ptr<DBusNameWatcher> name_watcher_;
Darin Petkov887f2982011-07-14 16:10:17 -070098
Darin Petkovc90fe522011-07-15 13:59:47 -070099 std::string owner_; // DBus service owner.
Darin Petkov887f2982011-07-14 16:10:17 -0700100
Darin Petkov5c97ac52011-07-19 16:30:49 -0700101 Modems modems_; // Maps a modem |path| to a modem instance.
102
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700103 ModemInfo *modem_info_;
Darin Petkov887f2982011-07-14 16:10:17 -0700104
105 DISALLOW_COPY_AND_ASSIGN(ModemManager);
106};
107
Jason Glasgow9c09e362012-04-18 15:16:29 -0400108class ModemManagerClassic : public ModemManager {
David Rochberg81030ea2012-03-02 15:44:25 -0500109 public:
110 ModemManagerClassic(const std::string &service,
111 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700112 ModemInfo *modem_info);
David Rochberg81030ea2012-03-02 15:44:25 -0500113
Ben Chan5ea763b2014-08-13 11:07:54 -0700114 ~ModemManagerClassic() override;
David Rochberg81030ea2012-03-02 15:44:25 -0500115
David Rochbergfa1d31d2012-03-20 10:38:07 -0400116 // Called by our dbus proxy
117 void OnDeviceAdded(const std::string &path);
118 void OnDeviceRemoved(const std::string &path);
119
David Rochberg81030ea2012-03-02 15:44:25 -0500120 protected:
121 virtual void Connect(const std::string &owner);
122 virtual void Disconnect();
David Rochbergfa1d31d2012-03-20 10:38:07 -0400123
124 virtual void AddModemClassic(const std::string &path);
Alex Vakulenko8a532292014-06-16 17:18:44 -0700125 virtual void InitModemClassic(std::shared_ptr<ModemClassic> modem);
David Rochberg81030ea2012-03-02 15:44:25 -0500126
127 private:
128 scoped_ptr<ModemManagerProxyInterface> proxy_; // DBus service proxy
David Rochbergfa1d31d2012-03-20 10:38:07 -0400129 scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
David Rochberg81030ea2012-03-02 15:44:25 -0500130
David Rochberg7cb06f62012-03-05 11:23:44 -0500131 FRIEND_TEST(ModemManagerClassicTest, Connect);
David Rochberg81030ea2012-03-02 15:44:25 -0500132
133 DISALLOW_COPY_AND_ASSIGN(ModemManagerClassic);
134};
David Rochberg7cb06f62012-03-05 11:23:44 -0500135
Eric Shienbrood9a245532012-03-07 14:20:39 -0500136class ModemManager1 : public ModemManager {
David Rochberg7cb06f62012-03-05 11:23:44 -0500137 public:
138 ModemManager1(const std::string &service,
139 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700140 ModemInfo *modem_info);
David Rochberg7cb06f62012-03-05 11:23:44 -0500141
Ben Chan5ea763b2014-08-13 11:07:54 -0700142 ~ModemManager1() override;
David Rochberg7cb06f62012-03-05 11:23:44 -0500143
David Rochberg7cb06f62012-03-05 11:23:44 -0500144 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400145 void AddModem1(const std::string &path,
Ben Chan876efd32012-09-28 15:25:13 -0700146 const DBusInterfaceToProperties &properties);
Alex Vakulenko8a532292014-06-16 17:18:44 -0700147 virtual void InitModem1(std::shared_ptr<Modem1> modem,
Ben Chan876efd32012-09-28 15:25:13 -0700148 const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400149
David Rochberg7cb06f62012-03-05 11:23:44 -0500150 // ModemManager methods
151 virtual void Connect(const std::string &owner);
152 virtual void Disconnect();
David Rochberg7cb06f62012-03-05 11:23:44 -0500153
154 // DBusObjectManagerProxyDelegate signal methods
Eric Shienbrood9a245532012-03-07 14:20:39 -0500155 virtual void OnInterfacesAddedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500156 const ::DBus::Path &object_path,
Ben Chan876efd32012-09-28 15:25:13 -0700157 const DBusInterfaceToProperties &properties);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500158 virtual void OnInterfacesRemovedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500159 const ::DBus::Path &object_path,
160 const std::vector<std::string> &interfaces);
161
162 // DBusObjectManagerProxyDelegate method callbacks
Eric Shienbrood9a245532012-03-07 14:20:39 -0500163 virtual void OnGetManagedObjectsReply(
David Rochberg7cb06f62012-03-05 11:23:44 -0500164 const DBusObjectsWithProperties &objects_with_properties,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500165 const Error &error);
David Rochberg7cb06f62012-03-05 11:23:44 -0500166
167 private:
Ben Chan1e2ba232014-01-27 16:35:45 -0800168 friend class ModemManager1Test;
David Rochberg7cb06f62012-03-05 11:23:44 -0500169 FRIEND_TEST(ModemManager1Test, Connect);
170 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
171
172 scoped_ptr<DBusObjectManagerProxyInterface> proxy_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500173 base::WeakPtrFactory<ModemManager1> weak_ptr_factory_;
David Rochberg7cb06f62012-03-05 11:23:44 -0500174
175 DISALLOW_COPY_AND_ASSIGN(ModemManager1);
176};
Ben Chan876efd32012-09-28 15:25:13 -0700177
Darin Petkov887f2982011-07-14 16:10:17 -0700178} // namespace shill
179
David Rochbergfa1d31d2012-03-20 10:38:07 -0400180#endif // SHILL_MODEM_MANAGER_H_