1//
2// Copyright (C) 2014 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8//      http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#ifndef SHILL_CELLULAR_CELLULAR_BEARER_H_
18#define SHILL_CELLULAR_CELLULAR_BEARER_H_
19
20#include <memory>
21#include <string>
22#include <vector>
23
24#include <base/macros.h>
25#include <gtest/gtest_prod.h>
26
27#include "shill/ipconfig.h"
28
29namespace shill {
30
31class DBusPropertiesProxyInterface;
32class ControlInterface;
33
34// A class for observing property changes of a bearer object exposed by
35// ModemManager.
36class CellularBearer {
37 public:
38  // Constructs a cellular bearer for observing property changes of a
39  // corresponding bearer object, at the DBus path |dbus_path| of DBus service
40  // |dbus_service|,  exposed by ModemManager. The ownership of |proxy_factory|
41  // is not transferred, and should outlive this object.
42  //
43  // TODO(benchan): Use a context object approach to pass objects like
44  // ControlInterface through constructor.
45  CellularBearer(ControlInterface* control_interface,
46                 const std::string& dbus_path,
47                 const std::string& dbus_service);
48  ~CellularBearer();
49
50  // Initializes this object by creating a DBus properties proxy to observe
51  // property changes of the corresponding bearer object exposed by ModemManager
52  // and also fetching the current properties of the bearer.  Returns true on
53  // success or false if it fails to the DBus properties proxy.
54  bool Init();
55
56  // Callback upon property changes of the bearer.
57  void OnPropertiesChanged(
58      const std::string& interface,
59      const KeyValueStore& changed_properties,
60      const std::vector<std::string>& invalidated_properties);
61
62  const std::string& dbus_path() const { return dbus_path_; }
63  const std::string& dbus_service() const { return dbus_service_; }
64
65  bool connected() const { return connected_; }
66  const std::string& data_interface() const { return data_interface_; }
67  IPConfig::Method ipv4_config_method() const { return ipv4_config_method_; }
68  const IPConfig::Properties* ipv4_config_properties() const {
69    return ipv4_config_properties_.get();
70  }
71  IPConfig::Method ipv6_config_method() const { return ipv6_config_method_; }
72  const IPConfig::Properties* ipv6_config_properties() const {
73    return ipv6_config_properties_.get();
74  }
75
76 private:
77  friend class CellularBearerTest;
78  FRIEND_TEST(CellularTest, EstablishLinkDHCP);
79  FRIEND_TEST(CellularTest, EstablishLinkPPP);
80  FRIEND_TEST(CellularTest, EstablishLinkStatic);
81
82  // Gets the IP configuration method and properties from |properties|.
83  // |address_family| specifies the IP address family of the configuration.
84  // |ipconfig_method| and |ipconfig_properties| are used to return the IP
85  // configuration method and properties and should be non-NULL.
86  void GetIPConfigMethodAndProperties(
87      const KeyValueStore& properties,
88      IPAddress::Family address_family,
89      IPConfig::Method* ipconfig_method,
90      std::unique_ptr<IPConfig::Properties>* ipconfig_properties) const;
91
92  // Resets bearer properties.
93  void ResetProperties();
94
95  // Updates bearer properties by fetching the current properties of the
96  // corresponding bearer object exposed by ModemManager over DBus.
97  void UpdateProperties();
98
99  // Setters for unit tests.
100  void set_connected(bool connected) { connected_ = connected; }
101  void set_data_interface(const std::string& data_interface) {
102    data_interface_ = data_interface;
103  }
104  void set_ipv4_config_method(IPConfig::Method ipv4_config_method) {
105    ipv4_config_method_ = ipv4_config_method;
106  }
107  void set_ipv4_config_properties(
108      std::unique_ptr<IPConfig::Properties> ipv4_config_properties) {
109    ipv4_config_properties_ = std::move(ipv4_config_properties);
110  }
111  void set_ipv6_config_method(IPConfig::Method ipv6_config_method) {
112    ipv6_config_method_ = ipv6_config_method;
113  }
114  void set_ipv6_config_properties(
115      std::unique_ptr<IPConfig::Properties> ipv6_config_properties) {
116    ipv6_config_properties_ = std::move(ipv6_config_properties);
117  }
118
119  ControlInterface* control_interface_;
120  std::string dbus_path_;
121  std::string dbus_service_;
122  std::unique_ptr<DBusPropertiesProxyInterface> dbus_properties_proxy_;
123  bool connected_;
124  std::string data_interface_;
125
126  // If |ipv4_config_method_| is set to |IPConfig::kMethodStatic|,
127  // |ipv4_config_properties_| is guaranteed to contain valid IP configuration
128  // properties. Otherwise, |ipv4_config_properties_| is set to nullptr.
129  // |ipv6_config_properties_| is handled similarly.
130  IPConfig::Method ipv4_config_method_;
131  std::unique_ptr<IPConfig::Properties> ipv4_config_properties_;
132  IPConfig::Method ipv6_config_method_;
133  std::unique_ptr<IPConfig::Properties> ipv6_config_properties_;
134
135  DISALLOW_COPY_AND_ASSIGN(CellularBearer);
136};
137
138}  // namespace shill
139
140#endif  // SHILL_CELLULAR_CELLULAR_BEARER_H_
141