Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -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 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 5 | #include "update_engine/policy_manager/real_shill_provider.h" |
| 6 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 7 | #include <string> |
| 8 | |
| 9 | #include <base/logging.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 10 | #include <base/strings/stringprintf.h> |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 11 | #include <chromeos/dbus/service_constants.h> |
| 12 | |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using std::string; |
| 16 | |
| 17 | namespace { |
| 18 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 19 | // Looks up a |key| in a GLib |hash_table| and returns the unboxed string from |
| 20 | // the corresponding GValue, if found. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 21 | const char* GetStrProperty(GHashTable* hash_table, const char* key) { |
| 22 | auto gval = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, key)); |
| 23 | return (gval ? g_value_get_string(gval) : NULL); |
| 24 | } |
| 25 | |
| 26 | }; // namespace |
| 27 | |
| 28 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 29 | namespace chromeos_policy_manager { |
| 30 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 31 | RealShillProvider::~RealShillProvider() { |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 32 | // Detach signal handler, free manager proxy. |
| 33 | dbus_->ProxyDisconnectSignal(manager_proxy_, shill::kMonitorPropertyChanged, |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 34 | G_CALLBACK(HandlePropertyChangedStatic), |
| 35 | this); |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 36 | dbus_->ProxyUnref(manager_proxy_); |
| 37 | } |
| 38 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 39 | ConnectionType RealShillProvider::ParseConnectionType(const char* type_str) { |
| 40 | if (!strcmp(type_str, shill::kTypeEthernet)) |
| 41 | return ConnectionType::kEthernet; |
| 42 | if (!strcmp(type_str, shill::kTypeWifi)) |
| 43 | return ConnectionType::kWifi; |
| 44 | if (!strcmp(type_str, shill::kTypeWimax)) |
| 45 | return ConnectionType::kWimax; |
| 46 | if (!strcmp(type_str, shill::kTypeBluetooth)) |
| 47 | return ConnectionType::kBluetooth; |
| 48 | if (!strcmp(type_str, shill::kTypeCellular)) |
| 49 | return ConnectionType::kCellular; |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 50 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 51 | return ConnectionType::kUnknown; |
| 52 | } |
| 53 | |
| 54 | ConnectionTethering RealShillProvider::ParseConnectionTethering( |
| 55 | const char* tethering_str) { |
| 56 | if (!strcmp(tethering_str, shill::kTetheringNotDetectedState)) |
| 57 | return ConnectionTethering::kNotDetected; |
| 58 | if (!strcmp(tethering_str, shill::kTetheringSuspectedState)) |
| 59 | return ConnectionTethering::kSuspected; |
| 60 | if (!strcmp(tethering_str, shill::kTetheringConfirmedState)) |
| 61 | return ConnectionTethering::kConfirmed; |
| 62 | |
| 63 | return ConnectionTethering::kUnknown; |
| 64 | } |
| 65 | |
| 66 | bool RealShillProvider::DoInit() { |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 67 | // Obtain a DBus connection. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 68 | GError* error = NULL; |
| 69 | connection_ = dbus_->BusGet(DBUS_BUS_SYSTEM, &error); |
| 70 | if (!connection_) { |
| 71 | LOG(ERROR) << "Failed to initialize DBus connection: " |
| 72 | << chromeos_update_engine::utils::GetAndFreeGError(&error); |
| 73 | return false; |
| 74 | } |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 75 | |
| 76 | // Allocate a shill manager proxy. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 77 | manager_proxy_ = GetProxy(shill::kFlimflamServicePath, |
| 78 | shill::kFlimflamManagerInterface); |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 79 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 80 | // Subscribe to the manager's PropertyChanged signal. |
| 81 | dbus_->ProxyAddSignal_2(manager_proxy_, shill::kMonitorPropertyChanged, |
| 82 | G_TYPE_STRING, G_TYPE_VALUE); |
| 83 | dbus_->ProxyConnectSignal(manager_proxy_, shill::kMonitorPropertyChanged, |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 84 | G_CALLBACK(HandlePropertyChangedStatic), |
| 85 | this, NULL); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 86 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 87 | // Attempt to read initial connection status. Even if this fails because shill |
| 88 | // is not responding (e.g. it is down) we'll be notified via "PropertyChanged" |
| 89 | // signal as soon as it comes up, so this is not a critical step. |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 90 | GHashTable* hash_table = NULL; |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 91 | if (GetProperties(manager_proxy_, &hash_table)) { |
| 92 | GValue* value = reinterpret_cast<GValue*>( |
| 93 | g_hash_table_lookup(hash_table, shill::kDefaultServiceProperty)); |
| 94 | ProcessDefaultService(value); |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 95 | g_hash_table_unref(hash_table); |
| 96 | } |
| 97 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 98 | return true; |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 101 | DBusGProxy* RealShillProvider::GetProxy(const char* path, |
| 102 | const char* interface) { |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 103 | return dbus_->ProxyNewForName(connection_, shill::kFlimflamServiceName, |
| 104 | path, interface); |
| 105 | } |
| 106 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 107 | bool RealShillProvider::GetProperties(DBusGProxy* proxy, |
| 108 | GHashTable** result_p) { |
Gilad Arnold | 5ef9c48 | 2014-03-03 13:51:02 -0800 | [diff] [blame] | 109 | GError* error = NULL; |
| 110 | if (!dbus_->ProxyCall_0_1(proxy, shill::kGetPropertiesFunction, &error, |
| 111 | result_p)) { |
| 112 | LOG(ERROR) << "Calling shill via DBus proxy failed: " |
| 113 | << chromeos_update_engine::utils::GetAndFreeGError(&error); |
| 114 | return false; |
| 115 | } |
| 116 | return true; |
| 117 | } |
| 118 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 119 | bool RealShillProvider::ProcessDefaultService(GValue* value) { |
| 120 | // Decode the string from the boxed value. |
| 121 | const char* default_service_path_str = NULL; |
| 122 | if (!(value && (default_service_path_str = g_value_get_string(value)))) |
| 123 | return false; |
| 124 | |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 125 | // Anything changed? |
| 126 | if (default_service_path_ == default_service_path_str) |
| 127 | return true; |
| 128 | |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 129 | // Update the connection status. |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 130 | default_service_path_ = default_service_path_str; |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 131 | bool is_connected = (default_service_path_ != "/"); |
| 132 | var_is_connected_.SetValue(is_connected); |
| 133 | var_conn_last_changed_.SetValue(clock_->GetWallclockTime()); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 134 | |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 135 | // Update the connection attributes. |
| 136 | if (is_connected) { |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 137 | DBusGProxy* service_proxy = GetProxy(default_service_path_.c_str(), |
| 138 | shill::kFlimflamServiceInterface); |
| 139 | GHashTable* hash_table = NULL; |
| 140 | if (GetProperties(service_proxy, &hash_table)) { |
| 141 | // Get the connection type. |
| 142 | const char* type_str = GetStrProperty(hash_table, shill::kTypeProperty); |
| 143 | if (type_str && !strcmp(type_str, shill::kTypeVPN)) { |
| 144 | type_str = GetStrProperty(hash_table, |
| 145 | shill::kPhysicalTechnologyProperty); |
| 146 | } |
| 147 | if (type_str) { |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 148 | var_conn_type_.SetValue(ParseConnectionType(type_str)); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 149 | } else { |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 150 | var_conn_type_.UnsetValue(); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 151 | LOG(ERROR) << "Could not find connection type (" |
| 152 | << default_service_path_ << ")"; |
| 153 | } |
| 154 | |
| 155 | // Get the connection tethering mode. |
| 156 | const char* tethering_str = GetStrProperty(hash_table, |
| 157 | shill::kTetheringProperty); |
| 158 | if (tethering_str) { |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 159 | var_conn_tethering_.SetValue(ParseConnectionTethering(tethering_str)); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 160 | } else { |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 161 | var_conn_tethering_.UnsetValue(); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 162 | LOG(ERROR) << "Could not find connection tethering mode (" |
| 163 | << default_service_path_ << ")"; |
| 164 | } |
| 165 | |
| 166 | g_hash_table_unref(hash_table); |
| 167 | } |
| 168 | dbus_->ProxyUnref(service_proxy); |
Gilad Arnold | d3df25f | 2014-04-22 08:39:48 -0700 | [diff] [blame^] | 169 | } else { |
| 170 | var_conn_type_.UnsetValue(); |
| 171 | var_conn_tethering_.UnsetValue(); |
Gilad Arnold | ef120fa | 2014-04-09 12:52:10 -0700 | [diff] [blame] | 172 | } |
Gilad Arnold | df3dd24 | 2014-04-09 07:15:51 -0700 | [diff] [blame] | 173 | |
Gilad Arnold | beb39e9 | 2014-03-11 11:34:50 -0700 | [diff] [blame] | 174 | return true; |
| 175 | } |
| 176 | |
| 177 | void RealShillProvider::HandlePropertyChanged(DBusGProxy* proxy, |
| 178 | const char* name, GValue* value) { |
| 179 | if (!strcmp(name, shill::kDefaultServiceProperty)) |
| 180 | ProcessDefaultService(value); |
| 181 | } |
| 182 | |
| 183 | void RealShillProvider::HandlePropertyChangedStatic(DBusGProxy* proxy, |
| 184 | const char* name, |
| 185 | GValue* value, |
| 186 | void* data) { |
| 187 | auto obj = reinterpret_cast<RealShillProvider*>(data); |
| 188 | obj->HandlePropertyChanged(proxy, name, value); |
| 189 | } |
| 190 | |
Gilad Arnold | 55f39b7 | 2014-01-28 12:51:45 -0800 | [diff] [blame] | 191 | } // namespace chromeos_policy_manager |