Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Ben Chan | c54afe5 | 2014-11-05 10:28:08 -0800 | [diff] [blame] | 5 | #ifndef SHILL_CELLULAR_CELLULAR_BEARER_H_ |
| 6 | #define SHILL_CELLULAR_CELLULAR_BEARER_H_ |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 7 | |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 8 | #include <memory> |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
Ben Chan | cc67c52 | 2014-09-03 07:19:18 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Ben Chan | 539ab02 | 2014-02-03 16:34:57 -0800 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 14 | |
| 15 | #include "shill/dbus_properties.h" |
| 16 | #include "shill/ipconfig.h" |
| 17 | |
| 18 | namespace shill { |
| 19 | |
| 20 | class DBusPropertiesProxyInterface; |
| 21 | class ProxyFactory; |
| 22 | |
| 23 | // A class for observing property changes of a bearer object exposed by |
| 24 | // ModemManager. |
| 25 | class CellularBearer { |
| 26 | public: |
| 27 | // Constructs a cellular bearer for observing property changes of a |
| 28 | // corresponding bearer object, at the DBus path |dbus_path| of DBus service |
| 29 | // |dbus_service|, exposed by ModemManager. The ownership of |proxy_factory| |
| 30 | // is not transferred, and should outlive this object. |
| 31 | // |
| 32 | // TODO(benchan): Use a context object approach to pass objects like |
| 33 | // ProxyFactory through constructor. |
| 34 | CellularBearer(ProxyFactory *proxy_factory, |
| 35 | const std::string &dbus_path, |
| 36 | const std::string &dbus_service); |
| 37 | ~CellularBearer(); |
| 38 | |
| 39 | // Initializes this object by creating a DBus properties proxy to observe |
| 40 | // property changes of the corresponding bearer object exposed by ModemManager |
| 41 | // and also fetching the current properties of the bearer. Returns true on |
| 42 | // success or false if it fails to the DBus properties proxy. |
| 43 | bool Init(); |
| 44 | |
| 45 | // Callback upon DBus property changes of the bearer. |
| 46 | void OnDBusPropertiesChanged( |
| 47 | const std::string &interface, |
| 48 | const DBusPropertiesMap &changed_properties, |
| 49 | const std::vector<std::string> &invalidated_properties); |
| 50 | |
| 51 | const std::string &dbus_path() const { return dbus_path_; } |
| 52 | const std::string &dbus_service() const { return dbus_service_; } |
| 53 | |
| 54 | bool connected() const { return connected_; } |
| 55 | const std::string &data_interface() const { return data_interface_; } |
| 56 | IPConfig::Method ipv4_config_method() const { return ipv4_config_method_; } |
| 57 | const IPConfig::Properties *ipv4_config_properties() const { |
| 58 | return ipv4_config_properties_.get(); |
| 59 | } |
| 60 | IPConfig::Method ipv6_config_method() const { return ipv6_config_method_; } |
| 61 | const IPConfig::Properties *ipv6_config_properties() const { |
| 62 | return ipv6_config_properties_.get(); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | friend class CellularBearerTest; |
Ben Chan | 539ab02 | 2014-02-03 16:34:57 -0800 | [diff] [blame] | 67 | FRIEND_TEST(CellularTest, EstablishLinkDHCP); |
| 68 | FRIEND_TEST(CellularTest, EstablishLinkPPP); |
| 69 | FRIEND_TEST(CellularTest, EstablishLinkStatic); |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 70 | |
| 71 | // Gets the IP configuration method and properties from |properties|. |
| 72 | // |address_family| specifies the IP address family of the configuration. |
| 73 | // |ipconfig_method| and |ipconfig_properties| are used to return the IP |
| 74 | // configuration method and properties and should be non-NULL. |
| 75 | void GetIPConfigMethodAndProperties( |
| 76 | const DBusPropertiesMap &properties, |
| 77 | IPAddress::Family address_family, |
| 78 | IPConfig::Method *ipconfig_method, |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 79 | std::unique_ptr<IPConfig::Properties> *ipconfig_properties) const; |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 80 | |
| 81 | // Resets bearer properties. |
| 82 | void ResetProperties(); |
| 83 | |
| 84 | // Updates bearer properties by fetching the current properties of the |
| 85 | // corresponding bearer object exposed by ModemManager over DBus. |
| 86 | void UpdateProperties(); |
| 87 | |
| 88 | // Setters for unit tests. |
| 89 | void set_connected(bool connected) { connected_ = connected; } |
| 90 | void set_data_interface(const std::string &data_interface) { |
| 91 | data_interface_ = data_interface; |
| 92 | } |
| 93 | void set_ipv4_config_method(IPConfig::Method ipv4_config_method) { |
| 94 | ipv4_config_method_ = ipv4_config_method; |
| 95 | } |
| 96 | void set_ipv4_config_properties( |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 97 | std::unique_ptr<IPConfig::Properties> ipv4_config_properties) { |
| 98 | ipv4_config_properties_ = std::move(ipv4_config_properties); |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 99 | } |
| 100 | void set_ipv6_config_method(IPConfig::Method ipv6_config_method) { |
| 101 | ipv6_config_method_ = ipv6_config_method; |
| 102 | } |
| 103 | void set_ipv6_config_properties( |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 104 | std::unique_ptr<IPConfig::Properties> ipv6_config_properties) { |
| 105 | ipv6_config_properties_ = std::move(ipv6_config_properties); |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | ProxyFactory *proxy_factory_; |
| 109 | std::string dbus_path_; |
| 110 | std::string dbus_service_; |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 111 | std::unique_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_; |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 112 | bool connected_; |
| 113 | std::string data_interface_; |
| 114 | |
| 115 | // If |ipv4_config_method_| is set to |IPConfig::kMethodStatic|, |
| 116 | // |ipv4_config_properties_| is guaranteed to contain valid IP configuration |
Ben Chan | ea18c6c | 2014-09-30 13:08:26 -0700 | [diff] [blame] | 117 | // properties. Otherwise, |ipv4_config_properties_| is set to nullptr. |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 118 | // |ipv6_config_properties_| is handled similarly. |
| 119 | IPConfig::Method ipv4_config_method_; |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 120 | std::unique_ptr<IPConfig::Properties> ipv4_config_properties_; |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 121 | IPConfig::Method ipv6_config_method_; |
Ben Chan | c20ed13 | 2014-10-16 12:25:03 -0700 | [diff] [blame] | 122 | std::unique_ptr<IPConfig::Properties> ipv6_config_properties_; |
Ben Chan | 26692bd | 2014-02-03 16:34:55 -0800 | [diff] [blame] | 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(CellularBearer); |
| 125 | }; |
| 126 | |
| 127 | } // namespace shill |
| 128 | |
Ben Chan | c54afe5 | 2014-11-05 10:28:08 -0800 | [diff] [blame] | 129 | #endif // SHILL_CELLULAR_CELLULAR_BEARER_H_ |