blob: 2ab9200d07f75509b68d788d8e0449b1bc38d280 [file] [log] [blame]
Ben Chan26692bd2014-02-03 16:34:55 -08001// 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
5#ifndef SHILL_CELLULAR_BEARER_H_
6#define SHILL_CELLULAR_BEARER_H_
7
8#include <string>
9#include <vector>
10
11#include <base/basictypes.h>
12#include <base/memory/scoped_ptr.h>
Ben Chan539ab022014-02-03 16:34:57 -080013#include <gtest/gtest_prod.h>
Ben Chan26692bd2014-02-03 16:34:55 -080014
15#include "shill/dbus_properties.h"
16#include "shill/ipconfig.h"
17
18namespace shill {
19
20class DBusPropertiesProxyInterface;
21class ProxyFactory;
22
23// A class for observing property changes of a bearer object exposed by
24// ModemManager.
25class 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 Chan539ab022014-02-03 16:34:57 -080067 FRIEND_TEST(CellularTest, EstablishLinkDHCP);
68 FRIEND_TEST(CellularTest, EstablishLinkPPP);
69 FRIEND_TEST(CellularTest, EstablishLinkStatic);
Ben Chan26692bd2014-02-03 16:34:55 -080070
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,
79 scoped_ptr<IPConfig::Properties> *ipconfig_properties) const;
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(
97 scoped_ptr<IPConfig::Properties> ipv4_config_properties) {
98 ipv4_config_properties_ = ipv4_config_properties.Pass();
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(
104 scoped_ptr<IPConfig::Properties> ipv6_config_properties) {
105 ipv6_config_properties_ = ipv6_config_properties.Pass();
106 }
107
108 ProxyFactory *proxy_factory_;
109 std::string dbus_path_;
110 std::string dbus_service_;
111 scoped_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
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
117 // properties. Otherwise, |ipv4_config_properties_| is set to NULL.
118 // |ipv6_config_properties_| is handled similarly.
119 IPConfig::Method ipv4_config_method_;
120 scoped_ptr<IPConfig::Properties> ipv4_config_properties_;
121 IPConfig::Method ipv6_config_method_;
122 scoped_ptr<IPConfig::Properties> ipv6_config_properties_;
123
124 DISALLOW_COPY_AND_ASSIGN(CellularBearer);
125};
126
127} // namespace shill
128
129#endif // SHILL_CELLULAR_BEARER_H_