blob: 5c08f81ab975021a22a5b408e962ec2b5f7e9750 [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
11#include <base/basictypes.h>
Jason Glasgow82f9ab32012-04-04 14:27:19 -040012#include <base/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
Darin Petkov137884a2011-10-26 18:52:47 +020022struct mobile_provider_db;
23
Darin Petkov5c97ac52011-07-19 16:30:49 -070024namespace shill {
25
Darin Petkovab565bb2011-10-06 02:55:51 -070026class ProxyFactory;
Darin Petkov5c97ac52011-07-19 16:30:49 -070027
Darin Petkove0a312e2011-07-20 13:45:28 -070028// Handles an instance of ModemManager.Modem and an instance of a Cellular
29// device.
Jason Glasgow9c09e362012-04-18 15:16:29 -040030class Modem {
Darin Petkov5c97ac52011-07-19 16:30:49 -070031 public:
Darin Petkove0a312e2011-07-20 13:45:28 -070032 // |owner| is the ModemManager DBus service owner (e.g., ":1.17"). |path| is
33 // the ModemManager.Modem DBus object path (e.g.,
34 // "/org/chromium/ModemManager/Gobi/0").
Darin Petkov5c97ac52011-07-19 16:30:49 -070035 Modem(const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -040036 const std::string &service,
Darin Petkov5c97ac52011-07-19 16:30:49 -070037 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -070038 ModemInfo *modem_info);
Ben Chan62028b22012-11-05 11:20:02 -080039 virtual ~Modem();
Darin Petkov5c97ac52011-07-19 16:30:49 -070040
Ben Chan876efd32012-09-28 15:25:13 -070041 // Asynchronously initializes support for the modem.
42 // If the |properties| are valid and the MAC address is present,
David Rochbergfa1d31d2012-03-20 10:38:07 -040043 // constructs and registers a Cellular device in |device_| based on
Ben Chan876efd32012-09-28 15:25:13 -070044 // |properties|.
David Rochbergfa1d31d2012-03-20 10:38:07 -040045 virtual void CreateDeviceFromModemProperties(
Ben Chan876efd32012-09-28 15:25:13 -070046 const DBusInterfaceToProperties &properties);
Darin Petkove0a312e2011-07-20 13:45:28 -070047
Darin Petkov41c0e0a2012-01-09 16:38:53 +010048 void OnDeviceInfoAvailable(const std::string &link_name);
49
David Rochbergfa1d31d2012-03-20 10:38:07 -040050 const std::string &owner() const { return owner_; }
Jason Glasgowa585fc32012-06-06 11:04:09 -040051 const std::string &service() const { return service_; }
David Rochbergfa1d31d2012-03-20 10:38:07 -040052 const std::string &path() const { return path_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070053
David Rochbergfa1d31d2012-03-20 10:38:07 -040054 void set_type(Cellular::Type type) { type_ = type; }
55
56 protected:
Darin Petkove0a312e2011-07-20 13:45:28 -070057 static const char kPropertyLinkName[];
58 static const char kPropertyIPMethod[];
59 static const char kPropertyType[];
Darin Petkove0a312e2011-07-20 13:45:28 -070060
Eric Shienbrood11567d02012-04-10 18:08:49 -040061 virtual void Init();
62
David Rochbergfa1d31d2012-03-20 10:38:07 -040063 CellularRefPtr device() const { return device_; }
Darin Petkove0a312e2011-07-20 13:45:28 -070064
David Rochbergfa1d31d2012-03-20 10:38:07 -040065 virtual Cellular *ConstructCellular(const std::string &link_name,
66 const std::string &device_name,
67 int interface_index);
David Rochbergfa1d31d2012-03-20 10:38:07 -040068 virtual bool GetLinkName(const DBusPropertiesMap &properties,
69 std::string *name) const = 0;
Jason Glasgow4c0724a2012-04-17 15:47:40 -040070 // Returns the name of the DBUS Modem interface.
71 virtual std::string GetModemInterface(void) const = 0;
Darin Petkovc5f56562011-08-06 16:40:05 -070072
David Rochbergfa1d31d2012-03-20 10:38:07 -040073 private:
74 friend class ModemTest;
Jason Glasgow82f9ab32012-04-04 14:27:19 -040075 friend class Modem1Test;
76 FRIEND_TEST(Modem1Test, Init);
77 FRIEND_TEST(Modem1Test, CreateDeviceMM1);
David Rochbergfa1d31d2012-03-20 10:38:07 -040078 FRIEND_TEST(ModemManager1Test, Connect);
Jason Glasgow82f9ab32012-04-04 14:27:19 -040079 FRIEND_TEST(ModemManager1Test, AddRemoveInterfaces);
David Rochbergfa1d31d2012-03-20 10:38:07 -040080 FRIEND_TEST(ModemManagerClassicTest, Connect);
81 FRIEND_TEST(ModemManagerCoreTest, ShouldAddModem);
82 FRIEND_TEST(ModemTest, CreateDeviceEarlyFailures);
mukesh agrawal9da07772013-05-15 14:15:17 -070083 FRIEND_TEST(ModemTest, CreateDevicePPP);
David Rochbergfa1d31d2012-03-20 10:38:07 -040084 FRIEND_TEST(ModemTest, EarlyDeviceProperties);
85 FRIEND_TEST(ModemTest, Init);
86 FRIEND_TEST(ModemTest, PendingDevicePropertiesAndCreate);
Darin Petkovab565bb2011-10-06 02:55:51 -070087
mukesh agrawal9da07772013-05-15 14:15:17 -070088 // Constants associated with fake network devices for PPP dongles.
89 // See |fake_dev_serial_|, below, for more info.
90 static const char kFakeDevNameFormat[];
91 static const char kFakeDevAddress[];
92 static const int kFakeDevInterfaceIndex;
93
94 // Find the |mac_address| and |interface_index| for the kernel
95 // network device with name |link_name|. Returns true iff both
96 // |mac_address| and |interface_index| were found.
97 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);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400142 virtual ~ModemClassic();
143
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);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400162 virtual ~Modem1();
163
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
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800175 base::FilePath netfiles_path_; // Used for testing
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400176
David Rochbergfa1d31d2012-03-20 10:38:07 -0400177 DISALLOW_COPY_AND_ASSIGN(Modem1);
178};
179
Darin Petkov5c97ac52011-07-19 16:30:49 -0700180} // namespace shill
181
Ben Chan876efd32012-09-28 15:25:13 -0700182#endif // SHILL_MODEM_H_