OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2010 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 | |
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: " | |
42 << utils::GetGErrorMessage(error); | |
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; | |
96 if (value && | |
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 } | |
102 g_hash_table_unref(hash_table); | |
103 return success; | |
104 } | |
105 | |
106 NetworkConnectionType ParseConnectionType(const char* type_str) { | |
107 if (!strcmp(type_str, kFlimFlamNetTypeEthernet)) { | |
108 return kNetEthernet; | |
109 } else if (!strcmp(type_str, kFlimFlamNetTypeWifi)) { | |
110 return kNetWifi; | |
111 } else if (!strcmp(type_str, kFlimFlamNetTypeWimax)) { | |
112 return kNetWimax; | |
113 } else if (!strcmp(type_str, kFlimFlamNetTypeBluetooth)) { | |
114 return kNetBluetooth; | |
115 } else if (!strcmp(type_str, kFlimFlamNetTypeCellular)) { | |
116 return kNetCellular; | |
117 } | |
118 return kNetUnknown; | |
119 } | |
120 | |
121 bool GetServicePathType(DbusGlibInterface* dbus_iface, | |
122 const string& path, | |
123 NetworkConnectionType* out_type) { | |
124 GHashTable* hash_table = NULL; | |
125 | |
126 TEST_AND_RETURN_FALSE(GetProperties(dbus_iface, | |
127 path.c_str(), | |
128 kFlimFlamDbusServiceInterface, | |
129 &hash_table)); | |
130 | |
131 GValue* value = (GValue*)g_hash_table_lookup(hash_table, "Type"); | |
132 const char* type_str = NULL; | |
133 bool success = false; | |
134 if (value && (type_str = g_value_get_string(value))) { | |
petkov
2010/10/21 05:15:19
I'm surprised you don't get a gcc warning here. Ma
adlr
2010/10/21 19:41:14
Done.
| |
135 *out_type = ParseConnectionType(type_str); | |
136 success = true; | |
137 } | |
138 g_hash_table_unref(hash_table); | |
139 return success; | |
140 } | |
141 | |
142 } // namespace {} | |
143 | |
144 const char* FlimFlamProxy::StringForConnectionType(NetworkConnectionType type) { | |
145 static const char* const kValues[] = {kFlimFlamNetTypeEthernet, | |
146 kFlimFlamNetTypeWifi, | |
147 kFlimFlamNetTypeWimax, | |
148 kFlimFlamNetTypeBluetooth, | |
149 kFlimFlamNetTypeCellular}; | |
150 if (type < 0 || type >= static_cast<int>(arraysize(kValues))) { | |
151 return "Unknown"; | |
152 } | |
153 return kValues[type]; | |
154 } | |
155 | |
156 bool FlimFlamProxy::GetConnectionType(DbusGlibInterface* dbus_iface, | |
157 NetworkConnectionType* out_type) { | |
158 string default_service_path; | |
159 TEST_AND_RETURN_FALSE(GetDefaultServicePath(dbus_iface, | |
160 &default_service_path)); | |
161 TEST_AND_RETURN_FALSE(GetServicePathType(dbus_iface, | |
162 default_service_path, | |
163 out_type)); | |
164 return true; | |
165 } | |
166 | |
167 const char* kFlimFlamDbusService = "org.chromium.flimflam"; | |
168 const char* kFlimFlamDbusManagerInterface = "org.chromium.flimflam.Manager"; | |
169 const char* kFlimFlamDbusManagerPath = "/"; | |
170 const char* kFlimFlamDbusServiceInterface = "org.chromium.flimflam.Service"; | |
171 | |
172 const char* kFlimFlamNetTypeEthernet = "ethernet"; | |
173 const char* kFlimFlamNetTypeWifi = "wifi"; | |
174 const char* kFlimFlamNetTypeWimax = "wimax"; | |
175 const char* kFlimFlamNetTypeBluetooth = "bluetooth"; | |
176 const char* kFlimFlamNetTypeCellular = "cellular"; | |
177 | |
178 } // namespace chromeos_update_engine | |
OLD | NEW |