blob: b95a8c8f9a63481902d5b5b275f800b4c28ec639 [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
Ben Chanc54afe52014-11-05 10:28:08 -08005#ifndef SHILL_CELLULAR_CELLULAR_BEARER_H_
6#define SHILL_CELLULAR_CELLULAR_BEARER_H_
Ben Chan26692bd2014-02-03 16:34:55 -08007
Ben Chanc20ed132014-10-16 12:25:03 -07008#include <memory>
Ben Chan26692bd2014-02-03 16:34:55 -08009#include <string>
10#include <vector>
11
Ben Chancc67c522014-09-03 07:19:18 -070012#include <base/macros.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,
Ben Chanc20ed132014-10-16 12:25:03 -070079 std::unique_ptr<IPConfig::Properties> *ipconfig_properties) const;
Ben Chan26692bd2014-02-03 16:34:55 -080080
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 Chanc20ed132014-10-16 12:25:03 -070097 std::unique_ptr<IPConfig::Properties> ipv4_config_properties) {
98 ipv4_config_properties_ = std::move(ipv4_config_properties);
Ben Chan26692bd2014-02-03 16:34:55 -080099 }
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 Chanc20ed132014-10-16 12:25:03 -0700104 std::unique_ptr<IPConfig::Properties> ipv6_config_properties) {
105 ipv6_config_properties_ = std::move(ipv6_config_properties);
Ben Chan26692bd2014-02-03 16:34:55 -0800106 }
107
108 ProxyFactory *proxy_factory_;
109 std::string dbus_path_;
110 std::string dbus_service_;
Ben Chanc20ed132014-10-16 12:25:03 -0700111 std::unique_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
Ben Chan26692bd2014-02-03 16:34:55 -0800112 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 Chanea18c6c2014-09-30 13:08:26 -0700117 // properties. Otherwise, |ipv4_config_properties_| is set to nullptr.
Ben Chan26692bd2014-02-03 16:34:55 -0800118 // |ipv6_config_properties_| is handled similarly.
119 IPConfig::Method ipv4_config_method_;
Ben Chanc20ed132014-10-16 12:25:03 -0700120 std::unique_ptr<IPConfig::Properties> ipv4_config_properties_;
Ben Chan26692bd2014-02-03 16:34:55 -0800121 IPConfig::Method ipv6_config_method_;
Ben Chanc20ed132014-10-16 12:25:03 -0700122 std::unique_ptr<IPConfig::Properties> ipv6_config_properties_;
Ben Chan26692bd2014-02-03 16:34:55 -0800123
124 DISALLOW_COPY_AND_ASSIGN(CellularBearer);
125};
126
127} // namespace shill
128
Ben Chanc54afe52014-11-05 10:28:08 -0800129#endif // SHILL_CELLULAR_CELLULAR_BEARER_H_