blob: 13f0fe46489c81b68deb6387e4ee4821f7e5fbfe [file] [log] [blame]
Gilad Arnold55f39b72014-01-28 12:51:45 -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
Gilad Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_
Gilad Arnold55f39b72014-01-28 12:51:45 -08007
Gilad Arnold5ef9c482014-03-03 13:51:02 -08008// TODO(garnold) Much of the functionality in this module was adapted from the
9// update engine's connection_manager. We need to make sure to deprecate use of
10// connection manager when the time comes.
11
12#include <string>
13
Alex Vakulenko75039d72014-03-25 12:36:28 -070014#include <base/time/time.h>
Gilad Arnold5ef9c482014-03-03 13:51:02 -080015
16#include "update_engine/clock_interface.h"
17#include "update_engine/dbus_wrapper_interface.h"
Gilad Arnold55f39b72014-01-28 12:51:45 -080018#include "update_engine/policy_manager/shill_provider.h"
19
Gilad Arnold5ef9c482014-03-03 13:51:02 -080020using chromeos_update_engine::ClockInterface;
21using chromeos_update_engine::DBusWrapperInterface;
Gilad Arnold55f39b72014-01-28 12:51:45 -080022
23namespace chromeos_policy_manager {
24
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070025// A DBus connector for making all shill related calls.
Gilad Arnold5ef9c482014-03-03 13:51:02 -080026class ShillConnector {
27 public:
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070028 // Expected type for the PropertyChanged signal handler.
29 typedef void (*PropertyChangedHandler)(DBusGProxy*, const char*, GValue*,
30 void*);
Gilad Arnold5ef9c482014-03-03 13:51:02 -080031
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070032 ShillConnector(DBusWrapperInterface* dbus,
33 PropertyChangedHandler signal_handler, void* signal_data)
34 : dbus_(dbus), signal_handler_(signal_handler),
35 signal_data_(signal_data) {}
36
37 ~ShillConnector();
Gilad Arnold5ef9c482014-03-03 13:51:02 -080038
39 // Initializes the DBus connector. Returns |true| on success.
40 bool Init();
41
Gilad Arnold5ef9c482014-03-03 13:51:02 -080042 // Obtains the type of a network connection described by |service_path|,
43 // storing it to |*conn_type_p|. Returns |true| on success; |false| on
44 // failure, in which case no value is written.
45 bool GetConnectionType(const std::string& service_path,
Gilad Arnoldaf309d52014-03-13 11:21:55 -070046 ConnectionType* conn_type_p);
Gilad Arnold5ef9c482014-03-03 13:51:02 -080047
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070048 // Issues a GetProperties call to shill's manager interface, storing the
49 // result to |*result_p|. Returns |true| on success.
50 bool GetManagerProperties(GHashTable** result_p) {
51 return GetProperties(manager_proxy_, result_p);
52 }
53
Alex Deymoc83baf62014-04-02 17:43:35 -070054 // Converts a shill connection type string into a symbolic value.
55 static ConnectionType ParseConnectionType(const char* str);
56
Gilad Arnold5ef9c482014-03-03 13:51:02 -080057 private:
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070058 // Issues a GetProperties call through a given |proxy|, storing the result to
59 // |*result_p|. Returns |true| on success.
60 bool GetProperties(DBusGProxy* proxy, GHashTable** result_p);
61
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070062 struct ConnStrToType {
Gilad Arnold5ef9c482014-03-03 13:51:02 -080063 const char *str;
Gilad Arnoldaf309d52014-03-13 11:21:55 -070064 ConnectionType type;
Gilad Arnoldae47a9a2014-03-26 12:16:47 -070065 };
Gilad Arnold5ef9c482014-03-03 13:51:02 -080066
67 // A mapping from shill connection type strings to enum values.
68 static const ConnStrToType shill_conn_str_to_type[];
69
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070070 // An initialization flag.
71 bool is_init_ = false;
72
Gilad Arnold5ef9c482014-03-03 13:51:02 -080073 // The DBus interface and connection, and a shill manager proxy.
74 DBusWrapperInterface* dbus_;
75 DBusGConnection* connection_ = NULL;
76 DBusGProxy* manager_proxy_ = NULL;
77
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070078 // The shill manager signal handler credentials.
79 PropertyChangedHandler signal_handler_ = NULL;
80 void* signal_data_ = NULL;
81
Gilad Arnold5ef9c482014-03-03 13:51:02 -080082 // Return a DBus proxy for a given |path| and |interface| within shill.
83 DBusGProxy* GetProxy(const char* path, const char* interface);
84
Gilad Arnold5ef9c482014-03-03 13:51:02 -080085 DISALLOW_COPY_AND_ASSIGN(ShillConnector);
86};
87
Gilad Arnold55f39b72014-01-28 12:51:45 -080088// ShillProvider concrete implementation.
Gilad Arnold55f39b72014-01-28 12:51:45 -080089class RealShillProvider : public ShillProvider {
90 public:
Gilad Arnold5ef9c482014-03-03 13:51:02 -080091 RealShillProvider(DBusWrapperInterface* dbus, ClockInterface* clock)
Gilad Arnoldbeb39e92014-03-11 11:34:50 -070092 : dbus_(dbus), clock_(clock) {}
Gilad Arnold55f39b72014-01-28 12:51:45 -080093
Gilad Arnolddf3dd242014-04-09 07:15:51 -070094 virtual inline Variable<bool>* var_is_connected() override {
95 return var_is_connected_.get();
96 }
97
98 virtual inline Variable<ConnectionType>* var_conn_type() override {
99 return var_conn_type_.get();
100 }
101
102 virtual inline Variable<base::Time>* var_conn_last_changed() override {
103 return var_conn_last_changed_.get();
104 }
105
Gilad Arnold55f39b72014-01-28 12:51:45 -0800106 protected:
Gilad Arnolddf3dd242014-04-09 07:15:51 -0700107 virtual bool DoInit() override;
Gilad Arnold55f39b72014-01-28 12:51:45 -0800108
109 private:
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700110 // Process a default connection value, update last change time as needed.
111 bool ProcessDefaultService(GValue* value);
112
113 // A handler for manager PropertyChanged signal, and a static version.
114 void HandlePropertyChanged(DBusGProxy* proxy, const char *name,
115 GValue* value);
116 static void HandlePropertyChangedStatic(DBusGProxy* proxy, const char* name,
117 GValue* value, void* data);
118
119
Gilad Arnolddf3dd242014-04-09 07:15:51 -0700120 // Whether the connection status has been properly initialized.
121 bool is_conn_status_init_ = false;
122
Gilad Arnold55f39b72014-01-28 12:51:45 -0800123 // The time when the connection type last changed.
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800124 base::Time conn_last_changed_;
125
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700126 // The current connection status.
127 bool is_connected_;
128
129 // The current default service path, if connected.
130 std::string default_service_path_;
131
132 // The last known type of the default connection.
Gilad Arnoldaf309d52014-03-13 11:21:55 -0700133 ConnectionType conn_type_ = ConnectionType::kUnknown;
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700134
135 // Whether the last known connection type is valid.
136 bool is_conn_type_valid_ = false;
137
Gilad Arnold5ef9c482014-03-03 13:51:02 -0800138 // A shill DBus connector.
139 scoped_ptr<ShillConnector> connector_;
140
141 // The DBus interface object (mockable).
142 DBusWrapperInterface* const dbus_;
143
144 // A clock abstraction (mockable).
145 ClockInterface* const clock_;
146
Gilad Arnolddf3dd242014-04-09 07:15:51 -0700147 // Pointers to all variable objects.
148 scoped_ptr<Variable<bool>> var_is_connected_;
149 scoped_ptr<Variable<ConnectionType>> var_conn_type_;
150 scoped_ptr<Variable<base::Time>> var_conn_last_changed_;
151
Gilad Arnoldbeb39e92014-03-11 11:34:50 -0700152 friend class ConnTypeVariable;
Gilad Arnold55f39b72014-01-28 12:51:45 -0800153 DISALLOW_COPY_AND_ASSIGN(RealShillProvider);
154};
155
156} // namespace chromeos_policy_manager
157
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800158#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_REAL_SHILL_PROVIDER_H_