blob: 936a65f8e324684538372e882f317bea18bbb5a7 [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);
mukesh agrawal12533ad2013-08-01 15:43:23 -070085 FRIEND_TEST(ModemTest, GetDeviceParams);
David Rochbergfa1d31d2012-03-20 10:38:07 -040086 FRIEND_TEST(ModemTest, Init);
87 FRIEND_TEST(ModemTest, PendingDevicePropertiesAndCreate);
Darin Petkovab565bb2011-10-06 02:55:51 -070088
mukesh agrawal9da07772013-05-15 14:15:17 -070089 // Constants associated with fake network devices for PPP dongles.
90 // See |fake_dev_serial_|, below, for more info.
91 static const char kFakeDevNameFormat[];
92 static const char kFakeDevAddress[];
93 static const int kFakeDevInterfaceIndex;
94
95 // Find the |mac_address| and |interface_index| for the kernel
96 // network device with name |link_name|. Returns true iff both
mukesh agrawal12533ad2013-08-01 15:43:23 -070097 // |mac_address| and |interface_index| were found. Modifies
98 // |interface_index| even on failure.
mukesh agrawal9da07772013-05-15 14:15:17 -070099 virtual bool GetDeviceParams(std::string *mac_address, int *interface_index);
100
Eric Shienbrood11567d02012-04-10 18:08:49 -0400101 virtual void OnDBusPropertiesChanged(
102 const std::string &interface,
103 const DBusPropertiesMap &changed_properties,
104 const std::vector<std::string> &invalidated_properties);
105 virtual void OnModemManagerPropertiesChanged(
106 const std::string &interface,
107 const DBusPropertiesMap &properties);
108
109 // A proxy to the org.freedesktop.DBusProperties interface used to obtain
110 // ModemManager.Modem properties and watch for property changes
111 scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
112
Ben Chan876efd32012-09-28 15:25:13 -0700113 DBusInterfaceToProperties initial_properties_;
Darin Petkov5c97ac52011-07-19 16:30:49 -0700114
Darin Petkove0a312e2011-07-20 13:45:28 -0700115 const std::string owner_;
Jason Glasgowa585fc32012-06-06 11:04:09 -0400116 const std::string service_;
Darin Petkove0a312e2011-07-20 13:45:28 -0700117 const std::string path_;
118
119 CellularRefPtr device_;
120
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700121 ModemInfo *modem_info_;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100122 std::string link_name_;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400123 Cellular::Type type_;
Darin Petkov41c0e0a2012-01-09 16:38:53 +0100124 bool pending_device_info_;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400125 RTNLHandler *rtnl_handler_;
Darin Petkov5c97ac52011-07-19 16:30:49 -0700126
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400127 // Store cached copies of singletons for speed/ease of testing.
128 ProxyFactory *proxy_factory_;
129
mukesh agrawal9da07772013-05-15 14:15:17 -0700130 // Serial number used to uniquify fake device names for Cellular
131 // devices that don't have network devices. (Names must be unique
132 // for D-Bus, and PPP dongles don't have network devices.)
133 static size_t fake_dev_serial_;
134
Darin Petkov5c97ac52011-07-19 16:30:49 -0700135 DISALLOW_COPY_AND_ASSIGN(Modem);
136};
137
Eric Shienbrood11567d02012-04-10 18:08:49 -0400138class ModemClassic : public Modem {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400139 public:
140 ModemClassic(const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400141 const std::string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -0400142 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700143 ModemInfo *modem_info);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400144 virtual ~ModemClassic();
145
146 // Gathers information and passes it to CreateDeviceFromModemProperties.
147 void CreateDeviceClassic(const DBusPropertiesMap &modem_properties);
148
David Rochbergfa1d31d2012-03-20 10:38:07 -0400149 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400150 virtual bool GetLinkName(const DBusPropertiesMap &modem_properties,
151 std::string *name) const;
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400152 virtual std::string GetModemInterface(void) const;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400153
154 private:
155 DISALLOW_COPY_AND_ASSIGN(ModemClassic);
156};
157
158class Modem1 : public Modem {
David Rochbergfa1d31d2012-03-20 10:38:07 -0400159 public:
160 Modem1(const std::string &owner,
Jason Glasgowa585fc32012-06-06 11:04:09 -0400161 const std::string &service,
David Rochbergfa1d31d2012-03-20 10:38:07 -0400162 const std::string &path,
Prathmesh Prabhu27526f12013-03-25 19:42:18 -0700163 ModemInfo *modem_info);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400164 virtual ~Modem1();
165
166 // Gathers information and passes it to CreateDeviceFromModemProperties.
Ben Chan876efd32012-09-28 15:25:13 -0700167 void CreateDeviceMM1(const DBusInterfaceToProperties &properties);
David Rochbergfa1d31d2012-03-20 10:38:07 -0400168
169 protected:
David Rochbergfa1d31d2012-03-20 10:38:07 -0400170 virtual bool GetLinkName(const DBusPropertiesMap &modem_properties,
171 std::string *name) const;
Jason Glasgow4c0724a2012-04-17 15:47:40 -0400172 virtual std::string GetModemInterface(void) const;
David Rochbergfa1d31d2012-03-20 10:38:07 -0400173
174 private:
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400175 friend class Modem1Test;
176
Albert Chaulk0e1cdea2013-02-27 15:32:55 -0800177 base::FilePath netfiles_path_; // Used for testing
Jason Glasgow82f9ab32012-04-04 14:27:19 -0400178
David Rochbergfa1d31d2012-03-20 10:38:07 -0400179 DISALLOW_COPY_AND_ASSIGN(Modem1);
180};
181
Darin Petkov5c97ac52011-07-19 16:30:49 -0700182} // namespace shill
183
Ben Chan876efd32012-09-28 15:25:13 -0700184#endif // SHILL_MODEM_H_