blob: 57c446cd6d79ebadcb88f20c37892ebb91239c72 [file] [log] [blame]
Darin Petkov41c0e0a2012-01-09 16:38:53 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkov5c97ac52011-07-19 16:30:49 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Chan876efd32012-09-28 15:25:13 -07005#ifndef SHILL_MODEM_H_
6#define SHILL_MODEM_H_
Darin Petkov5c97ac52011-07-19 16:30:49 -07007
8#include <string>
Hristo Stefanoved2c28c2011-11-29 15:37:30 -08009#include <vector>
Darin Petkov5c97ac52011-07-19 16:30:49 -070010
Ben Chancc67c522014-09-03 07:19:18 -070011#include <base/macros.h>
Ben Chan11c213f2014-09-05 08:21:06 -070012#include <base/files/file_util.h>
Darin Petkov5c97ac52011-07-19 16:30:49 -070013#include <base/memory/scoped_ptr.h>
Darin Petkove0a312e2011-07-20 13:45:28 -070014#include <gtest/gtest_prod.h> // for FRIEND_TEST
15
David Rochbergfa1d31d2012-03-20 10:38:07 -040016#include "shill/cellular.h"
17#include "shill/dbus_objectmanager_proxy_interface.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070018#include "shill/dbus_properties_proxy_interface.h"
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070019#include "shill/modem_info.h"
Darin Petkove0a312e2011-07-20 13:45:28 -070020#include "shill/refptr_types.h"
Darin Petkov5c97ac52011-07-19 16:30:49 -070021
22namespace shill {
23
Darin Petkovab565bb2011-10-06 02:55:51 -070024class ProxyFactory;
Darin Petkov5c97ac52011-07-19 16:30:49 -070025
Darin Petkove0a312e2011-07-20 13:45:28 -070026// Handles an instance of ModemManager.Modem and an instance of a Cellular
27// device.
Jason Glasgow9c09e362012-04-18 15:16:29 -040028class Modem {
Darin Petkov5c97ac52011-07-19 16:30:49 -070029 public:
Darin Petkove0a312e2011-07-20 13:45:28 -070030 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
31 // the ModemManager.Modem DBus object path (e.g.,
32 // "/org/chromium/ModemManager/Gobi/0").
Darin Petkov5c97ac52011-07-19 16:30:49 -070033 Modem(const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040034 const std::string &service,
Darin Petkov5c97ac52011-07-19 16:30:49 -070035 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070036 ModemInfo *modem_info);
Ben Chan62028b22012-11-05 11:20:02 -080037 virtual ~Modem();
Darin Petkov5c97ac52011-07-19 16:30:49 -070038
Ben Chan876efd32012-09-28 15:25:13 -070039 // Asynchronously initializes support for the modem.
40 // If the |properties| are valid and the MAC address is present,
David Rochbergfa1d31d2012-03-20 10:38:07 -040041 // constructs and registers a Cellular device in |device_| based on
Ben Chan876efd32012-09-28 15:25:13 -070042 // |properties|.
David Rochbergfa1d31d2012-03-20 10:38:07 -040043 virtual void CreateDeviceFromModemProperties(
Ben Chan876efd32012-09-28 15:25:13 -070044 const DBusInterfaceToProperties &properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070045
Darin Petkov41c0e0a2012-01-09 16:38:53 +010046 void OnDeviceInfoAvailable(const std::string &link_name);
47
David Rochbergfa1d31d2012-03-20 10:38:07 -040048 const std::string &owner() const { return owner_; }
Jason Glasgowa585fc32012-06-06 11:04:09 -040049 const std::string &service() const { return service_; }
David Rochbergfa1d31d2012-03-20 10:38:07 -040050 const std::string &path() const { return path_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070051
David Rochbergfa1d31d2012-03-20 10:38:07 -040052 void set_type(Cellular::Type type) { type_ = type; }
53
54 protected:
Darin Petkove0a312e2011-07-20 13:45:28 -070055 static const char kPropertyLinkName[];
56 static const char kPropertyIPMethod[];
57 static const char kPropertyType[];
Darin Petkove0a312e2011-07-20 13:45:28 -070058
Eric Shienbrood11567d02012-04-10 18:08:49 -040059 virtual void Init();
60
David Rochbergfa1d31d2012-03-20 10:38:07 -040061 CellularRefPtr device() const { return device_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070062
David Rochbergfa1d31d2012-03-20 10:38:07 -040063 virtual Cellular *ConstructCellular(const std::string &link_name,
64 const std::string &device_name,
65 int interface_index);
David Rochbergfa1d31d2012-03-20 10:38:07 -040066 virtual bool GetLinkName(const DBusPropertiesMap &properties,
67 std::string *name) const = 0;
Jason Glasgow4c0724a2012-04-17 15:47:40 -040068 // Returns the name of the DBUS Modem interface.
69 virtual std::string GetModemInterface(void) const = 0;
Darin Petkovc5f56562011-08-06 16:40:05 -070070
David Rochbergfa1d31d2012-03-20 10:38:07 -040071 private:
72 friend class ModemTest;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040073 friend class Modem1Test;
74 FRIEND_TEST(Modem1Test, Init);
75 FRIEND_TEST(Modem1Test, CreateDeviceMM1);
David Rochbergfa1d31d2012-03-20 10:38:07 -040076 FRIEND_TEST(ModemManager1Test, Connect);
Jason Glasgow82f9ab32012-04-04 14:27:19 -040077 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
David Rochbergfa1d31d2012-03-20 10:38:07 -040078 FRIEND_TEST(ModemManagerClassicTest, Connect);
79 FRIEND_TEST(ModemManagerCoreTest, ShouldAddModem);
80 FRIEND_TEST(ModemTest, CreateDeviceEarlyFailures);
mukesh agrawal9da07772013-05-15 14:15:17 -070081 FRIEND_TEST(ModemTest, CreateDevicePPP);
David Rochbergfa1d31d2012-03-20 10:38:07 -040082 FRIEND_TEST(ModemTest, EarlyDeviceProperties);
mukesh agrawal12533ad2013-08-01 15:43:23 -070083 FRIEND_TEST(ModemTest, GetDeviceParams);
David Rochbergfa1d31d2012-03-20 10:38:07 -040084 FRIEND_TEST(ModemTest, Init);
85 FRIEND_TEST(ModemTest, PendingDevicePropertiesAndCreate);
Darin Petkovab565bb2011-10-06 02:55:51 -070086
mukesh agrawal9da07772013-05-15 14:15:17 -070087 // Constants associated with fake network devices for PPP dongles.
88 // See |fake_dev_serial_|, below, for more info.
mukesh agrawal94b892a2014-08-12 15:55:27 -070089 static constexpr char kFakeDevNameFormat[] = "no_netdev_%zu";
mukesh agrawal9da07772013-05-15 14:15:17 -070090 static const char kFakeDevAddress[];
91 static const int kFakeDevInterfaceIndex;
92
93 // Find the |mac_address| and |interface_index| for the kernel
94 // network device with name |link_name|. Returns true iff both
mukesh agrawal12533ad2013-08-01 15:43:23 -070095 // |mac_address| and |interface_index| were found. Modifies
96 // |interface_index| even on failure.
mukesh agrawal9da07772013-05-15 14:15:17 -070097 virtual bool GetDeviceParams(std::string *mac_address, int *interface_index);
98
Eric Shienbrood11567d02012-04-10 18:08:49 -040099 virtual void OnDBusPropertiesChanged(
100 const std::string &interface,
101 const DBusPropertiesMap &changed_properties,
102 const std::vector<std::string> &invalidated_properties);
103 virtual void OnModemManagerPropertiesChanged(
104 const std::string &interface,
105 const DBusPropertiesMap &properties);
106
107 // A proxy to the org.freedesktop.DBusProperties interface used to obtain
108 // ModemManager.Modem properties and watch for property changes
109 scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
110
Ben Chan876efd32012-09-28 15:25:13 -0700111 DBusInterfaceToProperties initial_properties_;
Darin Petkov5c97ac52011-07-19 16:30:49 -0700112
Darin Petkove0a312e2011-07-20 13:45:28 -0700113 const std::string owner_;
Jason Glasgowa585fc32012-06-06 11:04:09 -0400114 const std::string service_;
Darin Petkove0a312e2011-07-20 13:45:28 -0700115 const std::string path_;
116
117 CellularRefPtr device_;
118
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700119 ModemInfo *modem_info_;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100120 std::string link_name_;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400121 Cellular::Type type_;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100122 bool pending_device_info_;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400123 RTNLHandler *rtnl_handler_;
Darin Petkov5c97ac52011-07-19 16:30:49 -0700124
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400125 // Store cached copies of singletons for speed/ease of testing.
126 ProxyFactory *proxy_factory_;
127
mukesh agrawal9da07772013-05-15 14:15:17 -0700128 // Serial number used to uniquify fake device names for Cellular
129 // devices that don't have network devices. (Names must be unique
130 // for D-Bus, and PPP dongles don't have network devices.)
131 static size_t fake_dev_serial_;
132
Darin Petkov5c97ac52011-07-19 16:30:49 -0700133 DISALLOW_COPY_AND_ASSIGN(Modem);
134};
135
Eric Shienbrood11567d02012-04-10 18:08:49 -0400136class ModemClassic : public Modem {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400137 public:
138 ModemClassic(const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400139 const std::string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -0400140 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700141 ModemInfo *modem_info);
Ben Chan5ea763b2014-08-13 11:07:54 -0700142 ~ModemClassic() override;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400143
144 // Gathers information and passes it to CreateDeviceFromModemProperties.
145 void CreateDeviceClassic(const DBusPropertiesMap &modem_properties);
146
David Rochbergfa1d31d2012-03-20 10:38:07 -0400147 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400148 virtual bool GetLinkName(const DBusPropertiesMap &modem_properties,
149 std::string *name) const;
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400150 virtual std::string GetModemInterface(void) const;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400151
152 private:
153 DISALLOW_COPY_AND_ASSIGN(ModemClassic);
154};
155
156class Modem1 : public Modem {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400157 public:
158 Modem1(const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400159 const std::string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -0400160 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700161 ModemInfo *modem_info);
Ben Chan5ea763b2014-08-13 11:07:54 -0700162 ~Modem1() override;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400163
164 // Gathers information and passes it to CreateDeviceFromModemProperties.
Ben Chan876efd32012-09-28 15:25:13 -0700165 void CreateDeviceMM1(const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400166
167 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400168 virtual bool GetLinkName(const DBusPropertiesMap &modem_properties,
169 std::string *name) const;
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400170 virtual std::string GetModemInterface(void) const;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400171
172 private:
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400173 friend class Modem1Test;
174
David Rochbergfa1d31d2012-03-20 10:38:07 -0400175 DISALLOW_COPY_AND_ASSIGN(Modem1);
176};
177
Darin Petkov5c97ac52011-07-19 16:30:49 -0700178} // namespace shill
179
Ben Chan876efd32012-09-28 15:25:13 -0700180#endif // SHILL_MODEM_H_