Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/flimflam_proxy.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include <base/string_util.h> |
| 10 | #include <dbus/dbus-glib.h> |
| 11 | #include <glib.h> |
| 12 | |
| 13 | #include "update_engine/utils.h" |
| 14 | |
| 15 | using std::string; |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Gets the DbusGProxy for FlimFlam. Must be free'd with ProxyUnref() |
| 22 | bool GetFlimFlamProxy(DbusGlibInterface* dbus_iface, |
| 23 | const char* path, |
| 24 | const char* interface, |
| 25 | DBusGProxy** out_proxy) { |
| 26 | DBusGConnection* bus; |
| 27 | DBusGProxy* proxy; |
| 28 | GError* error = NULL; |
| 29 | |
| 30 | bus = dbus_iface->BusGet(DBUS_BUS_SYSTEM, &error); |
| 31 | if (!bus) { |
| 32 | LOG(ERROR) << "Failed to get system bus"; |
| 33 | return false; |
| 34 | } |
| 35 | proxy = dbus_iface->ProxyNewForNameOwner(bus, |
| 36 | kFlimFlamDbusService, |
| 37 | path, |
| 38 | interface, |
| 39 | &error); |
| 40 | if (!proxy) { |
| 41 | LOG(ERROR) << "Error getting FlimFlam proxy: " |
Darin Petkov | a0b9e77 | 2011-10-06 05:05:56 -0700 | [diff] [blame] | 42 | << utils::GetAndFreeGError(&error); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | *out_proxy = proxy; |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | // On success, caller owns the GHashTable at out_hash_table. |
| 50 | // Returns true on success. |
| 51 | bool GetProperties(DbusGlibInterface* dbus_iface, |
| 52 | const char* path, |
| 53 | const char* interface, |
| 54 | GHashTable** out_hash_table) { |
| 55 | DBusGProxy* proxy; |
| 56 | GError* error = NULL; |
| 57 | |
| 58 | TEST_AND_RETURN_FALSE(GetFlimFlamProxy(dbus_iface, |
| 59 | path, |
| 60 | interface, |
| 61 | &proxy)); |
| 62 | |
| 63 | gboolean rc = dbus_iface->ProxyCall(proxy, |
| 64 | "GetProperties", |
| 65 | &error, |
| 66 | G_TYPE_INVALID, |
| 67 | dbus_g_type_get_map("GHashTable", |
| 68 | G_TYPE_STRING, |
| 69 | G_TYPE_VALUE), |
| 70 | out_hash_table, |
| 71 | G_TYPE_INVALID); |
| 72 | dbus_iface->ProxyUnref(proxy); |
| 73 | if (rc == FALSE) { |
| 74 | LOG(ERROR) << "dbus_g_proxy_call failed"; |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | // Returns (via out_path) the default network path, or empty string if |
| 82 | // there's no network up. |
| 83 | // Returns true on success. |
| 84 | bool GetDefaultServicePath(DbusGlibInterface* dbus_iface, string* out_path) { |
| 85 | GHashTable* hash_table = NULL; |
| 86 | |
| 87 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
| 88 | kFlimFlamDbusManagerPath, |
| 89 | kFlimFlamDbusManagerInterface, |
| 90 | &hash_table)); |
| 91 | |
| 92 | GValue* value = reinterpret_cast<GValue*>(g_hash_table_lookup(hash_table, |
| 93 | "Services")); |
| 94 | GArray* array = NULL; |
| 95 | bool success = false; |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 96 | if (G_VALUE_HOLDS(value, DBUS_TYPE_G_OBJECT_PATH_ARRAY) && |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 97 | (array = reinterpret_cast<GArray*>(g_value_get_boxed(value))) && |
| 98 | (array->len > 0)) { |
| 99 | *out_path = g_array_index(array, const char*, 0); |
| 100 | success = true; |
| 101 | } |
mukesh agrawal | 88226ff | 2012-03-19 17:50:06 -0700 | [diff] [blame] | 102 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 103 | g_hash_table_unref(hash_table); |
| 104 | return success; |
| 105 | } |
| 106 | |
| 107 | NetworkConnectionType ParseConnectionType(const char* type_str) { |
| 108 | if (!strcmp(type_str, kFlimFlamNetTypeEthernet)) { |
| 109 | return kNetEthernet; |
| 110 | } else if (!strcmp(type_str, kFlimFlamNetTypeWifi)) { |
| 111 | return kNetWifi; |
| 112 | } else if (!strcmp(type_str, kFlimFlamNetTypeWimax)) { |
| 113 | return kNetWimax; |
| 114 | } else if (!strcmp(type_str, kFlimFlamNetTypeBluetooth)) { |
| 115 | return kNetBluetooth; |
| 116 | } else if (!strcmp(type_str, kFlimFlamNetTypeCellular)) { |
| 117 | return kNetCellular; |
| 118 | } |
| 119 | return kNetUnknown; |
| 120 | } |
| 121 | |
| 122 | bool GetServicePathType(DbusGlibInterface* dbus_iface, |
| 123 | const string& path, |
| 124 | NetworkConnectionType* out_type) { |
| 125 | GHashTable* hash_table = NULL; |
| 126 | |
| 127 | TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, |
| 128 | path.c_str(), |
| 129 | kFlimFlamDbusServiceInterface, |
| 130 | &hash_table)); |
| 131 | |
| 132 | GValue* value = (GValue*)g_hash_table_lookup(hash_table, "Type"); |
| 133 | const char* type_str = NULL; |
| 134 | bool success = false; |
| 135 | if (value != NULL && (type_str = g_value_get_string(value)) != NULL) { |
| 136 | *out_type = ParseConnectionType(type_str); |
| 137 | success = true; |
| 138 | } |
| 139 | g_hash_table_unref(hash_table); |
| 140 | return success; |
| 141 | } |
| 142 | |
| 143 | } // namespace {} |
| 144 | |
| 145 | const char* FlimFlamProxy::StringForConnectionType(NetworkConnectionType type) { |
| 146 | static const char* const kValues[] = {kFlimFlamNetTypeEthernet, |
| 147 | kFlimFlamNetTypeWifi, |
| 148 | kFlimFlamNetTypeWimax, |
| 149 | kFlimFlamNetTypeBluetooth, |
| 150 | kFlimFlamNetTypeCellular}; |
| 151 | if (type < 0 || type >= static_cast<int>(arraysize(kValues))) { |
| 152 | return "Unknown"; |
| 153 | } |
| 154 | return kValues[type]; |
| 155 | } |
| 156 | |
| 157 | bool FlimFlamProxy::GetConnectionType(DbusGlibInterface* dbus_iface, |
| 158 | NetworkConnectionType* out_type) { |
| 159 | string default_service_path; |
| 160 | TEST_AND_RETURN_FALSE(GetDefaultServicePath(dbus_iface, |
| 161 | &default_service_path)); |
| 162 | TEST_AND_RETURN_FALSE(GetServicePathType(dbus_iface, |
| 163 | default_service_path, |
| 164 | out_type)); |
| 165 | return true; |
| 166 | } |
| 167 | |
| 168 | const char* kFlimFlamDbusService = "org.chromium.flimflam"; |
| 169 | const char* kFlimFlamDbusManagerInterface = "org.chromium.flimflam.Manager"; |
| 170 | const char* kFlimFlamDbusManagerPath = "/"; |
| 171 | const char* kFlimFlamDbusServiceInterface = "org.chromium.flimflam.Service"; |
| 172 | |
| 173 | const char* kFlimFlamNetTypeEthernet = "ethernet"; |
| 174 | const char* kFlimFlamNetTypeWifi = "wifi"; |
| 175 | const char* kFlimFlamNetTypeWimax = "wimax"; |
| 176 | const char* kFlimFlamNetTypeBluetooth = "bluetooth"; |
| 177 | const char* kFlimFlamNetTypeCellular = "cellular"; |
| 178 | |
| 179 | } // namespace chromeos_update_engine |