blob: 1604a41e854b7d13b24f4be4232c5fb22a1d2092 [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>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -08009#include <string>
Darin Petkov5c97ac52011-07-19 16:30:49 -070010#include <tr1/memory>
David Rochberg7cb06f62012-03-05 11:23:44 -050011#include <vector>
Darin Petkov5c97ac52011-07-19 16:30:49 -070012
Darin Petkov887f2982011-07-14 16:10:17 -070013#include <base/basictypes.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
Darin Petkov137884a2011-10-26 18:52:47 +020023struct mobile_provider_db;
24
Darin Petkov887f2982011-07-14 16:10:17 -070025namespace shill {
26
Ben Chan66174a12014-01-08 21:27:00 -080027class DBusNameWatcher;
David Rochberg7cb06f62012-03-05 11:23:44 -050028class DBusObjectManagerProxyInterface;
David Rochbergfa1d31d2012-03-20 10:38:07 -040029class DBusPropertiesProxyInterface;
David Rochbergfa1d31d2012-03-20 10:38:07 -040030class Modem1;
Darin Petkov5c97ac52011-07-19 16:30:49 -070031class Modem;
David Rochbergfa1d31d2012-03-20 10:38:07 -040032class ModemClassic;
Darin Petkovc90fe522011-07-15 13:59:47 -070033class ModemManagerProxyInterface;
Darin Petkovab565bb2011-10-06 02:55:51 -070034class ProxyFactory;
Darin Petkov887f2982011-07-14 16:10:17 -070035
Darin Petkov5c97ac52011-07-19 16:30:49 -070036// Handles a modem manager service and creates and destroys modem instances.
Darin Petkov887f2982011-07-14 16:10:17 -070037class ModemManager {
38 public:
39 ModemManager(const std::string &service,
40 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070041 ModemInfo *modem_info);
David Rochberg7cb06f62012-03-05 11:23:44 -050042 virtual ~ModemManager();
Darin Petkov887f2982011-07-14 16:10:17 -070043
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 Petkov5c97ac52011-07-19 16:30:49 -070048 // associated modems.
Darin Petkov887f2982011-07-14 16:10:17 -070049 void Stop();
50
Ben Chan66174a12014-01-08 21:27:00 -080051 // DBusNameWatcher callbacks.
52 void OnAppear(const std::string &name, const std::string &owner);
53 void OnVanish(const std::string &name);
54
Darin Petkov41c0e0a2012-01-09 16:38:53 +010055 void OnDeviceInfoAvailable(const std::string &link_name);
56
David Rochberg81030ea2012-03-02 15:44:25 -050057 protected:
58 typedef std::map<std::string, std::tr1::shared_ptr<Modem> > Modems;
59
60 const std::string &owner() const { return owner_; }
Jason Glasgowa585fc32012-06-06 11:04:09 -040061 const std::string &service() const { return service_; }
David Rochberg81030ea2012-03-02 15:44:25 -050062 const std::string &path() const { return path_; }
63 ProxyFactory *proxy_factory() const { return proxy_factory_; }
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070064 ModemInfo *modem_info() const { return modem_info_; }
David Rochberg81030ea2012-03-02 15:44:25 -050065
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 Rochbergfa1d31d2012-03-20 10:38:07 -040072 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 Petkov887f2982011-07-14 16:10:17 -070079 private:
David Rochberg7cb06f62012-03-05 11:23:44 -050080 friend class ModemManagerCoreTest;
81 friend class ModemManagerClassicTest;
82 friend class ModemManager1Test;
83
Darin Petkov887f2982011-07-14 16:10:17 -070084 FRIEND_TEST(ModemInfoTest, RegisterModemManager);
David Rochberg7cb06f62012-03-05 11:23:44 -050085 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
86 FRIEND_TEST(ModemManager1Test, Connect);
87 FRIEND_TEST(ModemManagerClassicTest, Connect);
88 FRIEND_TEST(ModemManagerCoreTest, AddRemoveModem);
Ben Chan66174a12014-01-08 21:27:00 -080089 FRIEND_TEST(ModemManagerCoreTest, ConnectDisconnect);
David Rochberg7cb06f62012-03-05 11:23:44 -050090 FRIEND_TEST(ModemManagerCoreTest, OnAppearVanish);
Ben Chan66174a12014-01-08 21:27:00 -080091 FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServiceAbsent);
92 FRIEND_TEST(ModemManagerCoreTest, StartStopWithModemManagerServicePresent);
Darin Petkov887f2982011-07-14 16:10:17 -070093
Darin Petkovab565bb2011-10-06 02:55:51 -070094 // Store cached copies of singletons for speed/ease of testing.
95 ProxyFactory *proxy_factory_;
96
Darin Petkov887f2982011-07-14 16:10:17 -070097 const std::string service_;
98 const std::string path_;
Ben Chan66174a12014-01-08 21:27:00 -080099 scoped_ptr<DBusNameWatcher> name_watcher_;
Darin Petkov887f2982011-07-14 16:10:17 -0700100
Darin Petkovc90fe522011-07-15 13:59:47 -0700101 std::string owner_; // DBus service owner.
Darin Petkov887f2982011-07-14 16:10:17 -0700102
Darin Petkov5c97ac52011-07-19 16:30:49 -0700103 Modems modems_; // Maps a modem |path| to a modem instance.
104
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700105 ModemInfo *modem_info_;
Darin Petkov887f2982011-07-14 16:10:17 -0700106
107 DISALLOW_COPY_AND_ASSIGN(ModemManager);
108};
109
Jason Glasgow9c09e362012-04-18 15:16:29 -0400110class ModemManagerClassic : public ModemManager {
David Rochberg81030ea2012-03-02 15:44:25 -0500111 public:
112 ModemManagerClassic(const std::string &service,
113 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700114 ModemInfo *modem_info);
David Rochberg81030ea2012-03-02 15:44:25 -0500115
116 virtual ~ModemManagerClassic();
117
David Rochbergfa1d31d2012-03-20 10:38:07 -0400118 // Called by our dbus proxy
119 void OnDeviceAdded(const std::string &path);
120 void OnDeviceRemoved(const std::string &path);
121
David Rochberg81030ea2012-03-02 15:44:25 -0500122 protected:
123 virtual void Connect(const std::string &owner);
124 virtual void Disconnect();
David Rochbergfa1d31d2012-03-20 10:38:07 -0400125
126 virtual void AddModemClassic(const std::string &path);
127 virtual void InitModemClassic(std::tr1::shared_ptr<ModemClassic> modem);
David Rochberg81030ea2012-03-02 15:44:25 -0500128
129 private:
130 scoped_ptr<ModemManagerProxyInterface> proxy_; // DBus service proxy
David Rochbergfa1d31d2012-03-20 10:38:07 -0400131 scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
David Rochberg81030ea2012-03-02 15:44:25 -0500132
David Rochberg7cb06f62012-03-05 11:23:44 -0500133 FRIEND_TEST(ModemManagerClassicTest, Connect);
David Rochberg81030ea2012-03-02 15:44:25 -0500134
135 DISALLOW_COPY_AND_ASSIGN(ModemManagerClassic);
136};
David Rochberg7cb06f62012-03-05 11:23:44 -0500137
Eric Shienbrood9a245532012-03-07 14:20:39 -0500138class ModemManager1 : public ModemManager {
David Rochberg7cb06f62012-03-05 11:23:44 -0500139 public:
140 ModemManager1(const std::string &service,
141 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700142 ModemInfo *modem_info);
David Rochberg7cb06f62012-03-05 11:23:44 -0500143
144 virtual ~ModemManager1();
145
David Rochberg7cb06f62012-03-05 11:23:44 -0500146 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400147 void AddModem1(const std::string &path,
Ben Chan876efd32012-09-28 15:25:13 -0700148 const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400149 virtual void InitModem1(std::tr1::shared_ptr<Modem1> modem,
Ben Chan876efd32012-09-28 15:25:13 -0700150 const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400151
David Rochberg7cb06f62012-03-05 11:23:44 -0500152 // ModemManager methods
153 virtual void Connect(const std::string &owner);
154 virtual void Disconnect();
David Rochberg7cb06f62012-03-05 11:23:44 -0500155
156 // DBusObjectManagerProxyDelegate signal methods
Eric Shienbrood9a245532012-03-07 14:20:39 -0500157 virtual void OnInterfacesAddedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500158 const ::DBus::Path &object_path,
Ben Chan876efd32012-09-28 15:25:13 -0700159 const DBusInterfaceToProperties &properties);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500160 virtual void OnInterfacesRemovedSignal(
David Rochberg7cb06f62012-03-05 11:23:44 -0500161 const ::DBus::Path &object_path,
162 const std::vector<std::string> &interfaces);
163
164 // DBusObjectManagerProxyDelegate method callbacks
Eric Shienbrood9a245532012-03-07 14:20:39 -0500165 virtual void OnGetManagedObjectsReply(
David Rochberg7cb06f62012-03-05 11:23:44 -0500166 const DBusObjectsWithProperties &objects_with_properties,
Eric Shienbrood9a245532012-03-07 14:20:39 -0500167 const Error &error);
David Rochberg7cb06f62012-03-05 11:23:44 -0500168
169 private:
170 FRIEND_TEST(ModemManager1Test, Connect);
171 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
172
173 scoped_ptr<DBusObjectManagerProxyInterface> proxy_;
Eric Shienbrood9a245532012-03-07 14:20:39 -0500174 base::WeakPtrFactory<ModemManager1> weak_ptr_factory_;
David Rochberg7cb06f62012-03-05 11:23:44 -0500175
176 DISALLOW_COPY_AND_ASSIGN(ModemManager1);
177};
Ben Chan876efd32012-09-28 15:25:13 -0700178
Darin Petkov887f2982011-07-14 16:10:17 -0700179} // namespace shill
180
David Rochbergfa1d31d2012-03-20 10:38:07 -0400181#endif // SHILL_MODEM_MANAGER_H_