OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Provides wifi scan API binding for suitable for typical linux distributions. | 5 // Provides wifi scan API binding for suitable for typical linux distributions. |
6 // Currently, only the NetworkManager API is used, accessed via D-Bus (in turn | 6 // Currently, only the NetworkManager API is used, accessed via D-Bus (in turn |
7 // accessed via the GLib wrapper). | 7 // accessed via the GLib wrapper). |
8 | 8 |
9 #include "content/browser/geolocation/wifi_data_provider_linux.h" | 9 #include "content/browser/geolocation/wifi_data_provider_linux.h" |
10 | 10 |
11 #include <dbus/dbus-glib.h> | |
12 #include <dbus/dbus-glib-lowlevel.h> | |
13 #include <dbus/dbus.h> | |
14 #include <dlfcn.h> | |
15 #include <glib.h> | |
16 | |
17 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
18 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
19 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "dbus/bus.h" |
| 15 #include "dbus/message.h" |
| 16 #include "dbus/object_proxy.h" |
20 | 17 |
21 namespace { | 18 namespace { |
22 // The time periods between successive polls of the wifi data. | 19 // The time periods between successive polls of the wifi data. |
23 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s | 20 const int kDefaultPollingIntervalMilliseconds = 10 * 1000; // 10s |
24 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins | 21 const int kNoChangePollingIntervalMilliseconds = 2 * 60 * 1000; // 2 mins |
25 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins | 22 const int kTwoNoChangePollingIntervalMilliseconds = 10 * 60 * 1000; // 10 mins |
26 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s | 23 const int kNoWifiPollingIntervalMilliseconds = 20 * 1000; // 20s |
27 | 24 |
28 const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager"; | 25 const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager"; |
29 const char kNetworkManagerPath[] = "/org/freedesktop/NetworkManager"; | 26 const char kNetworkManagerPath[] = "/org/freedesktop/NetworkManager"; |
30 const char kNetworkManagerInterface[] = "org.freedesktop.NetworkManager"; | 27 const char kNetworkManagerInterface[] = "org.freedesktop.NetworkManager"; |
31 | 28 |
32 // From http://projects.gnome.org/NetworkManager/developers/spec.html | 29 // From http://projects.gnome.org/NetworkManager/developers/spec.html |
33 enum { NM_DEVICE_TYPE_WIFI = 2 }; | 30 enum { NM_DEVICE_TYPE_WIFI = 2 }; |
34 | 31 |
35 // Function type matching dbus_g_bus_get_private so that we can | |
36 // dynamically determine the presence of this symbol (see | |
37 // NetworkManagerWlanApi::Init) | |
38 typedef DBusGConnection* DBusGBusGetPrivateFunc( | |
39 DBusBusType type, GMainContext* context, GError** error); | |
40 | |
41 // Utility wrappers to make various GLib & DBus structs into scoped objects. | |
42 class ScopedGPtrArrayFree { | |
43 public: | |
44 void operator()(GPtrArray* x) const { | |
45 if (x) | |
46 g_ptr_array_free(x, TRUE); | |
47 } | |
48 }; | |
49 // Use ScopedGPtrArrayPtr as if it were scoped_ptr<GPtrArray> | |
50 typedef scoped_ptr_malloc<GPtrArray, ScopedGPtrArrayFree> ScopedGPtrArrayPtr; | |
51 | |
52 class ScopedGObjectFree { | |
53 public: | |
54 void operator()(void* x) const { | |
55 if (x) | |
56 g_object_unref(x); | |
57 } | |
58 }; | |
59 // Use ScopedDBusGProxyPtr as if it were scoped_ptr<DBusGProxy> | |
60 typedef scoped_ptr_malloc<DBusGProxy, ScopedGObjectFree> ScopedDBusGProxyPtr; | |
61 | |
62 // Use ScopedGValue::v as an instance of GValue with automatic cleanup. | |
63 class ScopedGValue { | |
64 public: | |
65 ScopedGValue() | |
66 : v(empty_gvalue()) { | |
67 } | |
68 ~ScopedGValue() { | |
69 g_value_unset(&v); | |
70 } | |
71 static GValue empty_gvalue() { | |
72 GValue value = {0}; | |
73 return value; | |
74 } | |
75 | |
76 GValue v; | |
77 }; | |
78 | |
79 // Use ScopedDLHandle to automatically clean up after dlopen. | |
80 class ScopedDLHandle { | |
81 public: | |
82 ScopedDLHandle(void* handle) | |
83 : handle_(handle) { | |
84 } | |
85 ~ScopedDLHandle() { | |
86 if (handle_) | |
87 dlclose(handle_); | |
88 } | |
89 void* get() { | |
90 return handle_; | |
91 } | |
92 private: | |
93 void *handle_; | |
94 }; | |
95 | |
96 // Wifi API binding to NetworkManager, to allow reuse of the polling behavior | 32 // Wifi API binding to NetworkManager, to allow reuse of the polling behavior |
97 // defined in WifiDataProviderCommon. | 33 // defined in WifiDataProviderCommon. |
98 // TODO(joth): NetworkManager also allows for notification based handling, | 34 // TODO(joth): NetworkManager also allows for notification based handling, |
99 // however this will require reworking of the threading code to run a GLib | 35 // however this will require reworking of the threading code to run a GLib |
100 // event loop (GMainLoop). | 36 // event loop (GMainLoop). |
101 class NetworkManagerWlanApi : public WifiDataProviderCommon::WlanApiInterface { | 37 class NetworkManagerWlanApi : public WifiDataProviderCommon::WlanApiInterface { |
102 public: | 38 public: |
103 NetworkManagerWlanApi(); | 39 NetworkManagerWlanApi(); |
104 ~NetworkManagerWlanApi(); | 40 ~NetworkManagerWlanApi(); |
105 | 41 |
106 // Must be called before any other interface method. Will return false if the | 42 // Must be called before any other interface method. Will return false if the |
107 // NetworkManager session cannot be created (e.g. not present on this distro), | 43 // NetworkManager session cannot be created (e.g. not present on this distro), |
108 // in which case no other method may be called. | 44 // in which case no other method may be called. |
109 bool Init(); | 45 bool Init(); |
110 | 46 |
| 47 // Similar to Init() but can inject the bus object. Used for testing. |
| 48 bool InitWithBus(dbus::Bus* bus); |
| 49 |
111 // WifiDataProviderCommon::WlanApiInterface | 50 // WifiDataProviderCommon::WlanApiInterface |
112 bool GetAccessPointData(WifiData::AccessPointDataSet* data); | 51 // |
| 52 // This function makes blocking D-Bus calls, but it's totally fine as |
| 53 // the code runs in "Geolocation" thread, not the browser's UI thread. |
| 54 virtual bool GetAccessPointData(WifiData::AccessPointDataSet* data); |
113 | 55 |
114 private: | 56 private: |
115 // Checks if the last dbus call returned an error. If it did, logs the error | |
116 // message, frees it and returns true. | |
117 // This must be called after every dbus call that accepts |&error_| | |
118 bool CheckError(); | |
119 | |
120 // Enumerates the list of available network adapter devices known to | 57 // Enumerates the list of available network adapter devices known to |
121 // NetworkManager. Ownership of the array (and contained objects) is returned | 58 // NetworkManager. Return true on success. |
122 // to the caller. | 59 bool GetAdapterDeviceList(std::vector<std::string>* device_paths); |
123 GPtrArray* GetAdapterDeviceList(); | |
124 | 60 |
125 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan | 61 // Given the NetworkManager path to a wireless adapater, dumps the wifi scan |
126 // results and appends them to |data|. Returns false if a fatal error is | 62 // results and appends them to |data|. Returns false if a fatal error is |
127 // encountered such that the data set could not be populated. | 63 // encountered such that the data set could not be populated. |
128 bool GetAccessPointsForAdapter(const gchar* adapter_path, | 64 bool GetAccessPointsForAdapter(const std::string& adapter_path, |
129 WifiData::AccessPointDataSet* data); | 65 WifiData::AccessPointDataSet* data); |
130 | 66 |
131 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access | 67 // Internal method used by |GetAccessPointsForAdapter|, given a wifi access |
132 // point proxy retrieves the named property into |value_out|. Returns false if | 68 // point proxy retrieves the named property and returns it. Returns NULL if |
133 // the property could not be read, or is not of type |expected_gvalue_type|. | 69 // the property could not be read. |
134 bool GetAccessPointProperty(DBusGProxy* proxy, const char* property_name, | 70 dbus::Response* GetAccessPointProperty(dbus::ObjectProxy* proxy, |
135 int expected_gvalue_type, GValue* value_out); | 71 const std::string& property_name); |
136 | 72 |
137 // Error from the last dbus call. NULL when there's no error. Freed and | 73 scoped_refptr<dbus::Bus> system_bus_; |
138 // cleared by CheckError(). | 74 dbus::ObjectProxy* network_manager_proxy_; |
139 GError* error_; | |
140 // Connection to the dbus system bus. | |
141 DBusGConnection* connection_; | |
142 // Main context | |
143 GMainContext* context_; | |
144 // Proxy to the network manager dbus service. | |
145 ScopedDBusGProxyPtr proxy_; | |
146 | 75 |
147 DISALLOW_COPY_AND_ASSIGN(NetworkManagerWlanApi); | 76 DISALLOW_COPY_AND_ASSIGN(NetworkManagerWlanApi); |
148 }; | 77 }; |
149 | 78 |
150 // Convert a wifi frequency to the corresponding channel. Adapted from | 79 // Convert a wifi frequency to the corresponding channel. Adapted from |
151 // geolocaiton/wifilib.cc in googleclient (internal to google). | 80 // geolocaiton/wifilib.cc in googleclient (internal to google). |
152 int frquency_in_khz_to_channel(int frequency_khz) { | 81 int frquency_in_khz_to_channel(int frequency_khz) { |
153 if (frequency_khz >= 2412000 && frequency_khz <= 2472000) // Channels 1-13. | 82 if (frequency_khz >= 2412000 && frequency_khz <= 2472000) // Channels 1-13. |
154 return (frequency_khz - 2407000) / 5000; | 83 return (frequency_khz - 2407000) / 5000; |
155 if (frequency_khz == 2484000) | 84 if (frequency_khz == 2484000) |
156 return 14; | 85 return 14; |
157 if (frequency_khz > 5000000 && frequency_khz < 6000000) // .11a bands. | 86 if (frequency_khz > 5000000 && frequency_khz < 6000000) // .11a bands. |
158 return (frequency_khz - 5000000) / 5000; | 87 return (frequency_khz - 5000000) / 5000; |
159 // Ignore everything else. | 88 // Ignore everything else. |
160 return AccessPointData().channel; // invalid channel | 89 return AccessPointData().channel; // invalid channel |
161 } | 90 } |
162 | 91 |
163 NetworkManagerWlanApi::NetworkManagerWlanApi() | 92 NetworkManagerWlanApi::NetworkManagerWlanApi() |
164 : error_(NULL), | 93 : network_manager_proxy_(NULL) { |
165 connection_(NULL), | |
166 context_(NULL) | |
167 { | |
168 } | 94 } |
169 | 95 |
170 NetworkManagerWlanApi::~NetworkManagerWlanApi() { | 96 NetworkManagerWlanApi::~NetworkManagerWlanApi() { |
171 proxy_.reset(); | 97 // Close the connection. |
172 if (connection_) { | 98 system_bus_->ShutdownAndBlock(); |
173 dbus_connection_close(dbus_g_connection_get_connection(connection_)); | |
174 dbus_g_connection_unref(connection_); | |
175 } | |
176 if (context_) | |
177 g_main_context_unref(context_); | |
178 | |
179 DCHECK(!error_) << "Missing a call to CheckError() to clear |error_|"; | |
180 } | 99 } |
181 | 100 |
182 bool NetworkManagerWlanApi::Init() { | 101 bool NetworkManagerWlanApi::Init() { |
183 // Chrome DLL init code handles initializing the thread system, so rather than | 102 dbus::Bus::Options options; |
184 // get caught up with that nonsense here, lets just assert our requirement. | 103 options.bus_type = dbus::Bus::SYSTEM; |
185 CHECK(g_thread_supported()); | 104 options.connection_type = dbus::Bus::PRIVATE; |
| 105 return InitWithBus(new dbus::Bus(options)); |
| 106 } |
186 | 107 |
187 // We require a private bus to ensure that we don't interfere with the | 108 bool NetworkManagerWlanApi::InitWithBus(dbus::Bus* bus) { |
188 // default loop on the main thread. Unforunately this functionality is only | 109 system_bus_ = bus; |
189 // available in dbus-glib-0.84 and later. We do a dynamic symbol lookup | 110 // system_bus_ will own all object proxies created from the bus. |
190 // to determine if dbus_g_bus_get_private is available. See bug | 111 network_manager_proxy_ = |
191 // http://code.google.com/p/chromium/issues/detail?id=59913 for more | 112 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
192 // information. | 113 kNetworkManagerPath); |
193 ScopedDLHandle handle(dlopen(NULL, RTLD_LAZY)); | |
194 if (!handle.get()) | |
195 return false; | |
196 | |
197 DBusGBusGetPrivateFunc *my_dbus_g_bus_get_private = | |
198 (DBusGBusGetPrivateFunc *) dlsym(handle.get(), "dbus_g_bus_get_private"); | |
199 | |
200 if (!my_dbus_g_bus_get_private) { | |
201 LOG(ERROR) << "We need dbus-glib >= 0.84 for wifi geolocation."; | |
202 return false; | |
203 } | |
204 // Get a private connection to the session bus. | |
205 context_ = g_main_context_new(); | |
206 DCHECK(context_); | |
207 connection_ = my_dbus_g_bus_get_private(DBUS_BUS_SYSTEM, context_, &error_); | |
208 | |
209 if (CheckError()) | |
210 return false; | |
211 DCHECK(connection_); | |
212 | |
213 // Disable timers on the connection since we don't need them and | |
214 // we're not going to run them anyway as the connection is associated | |
215 // with a private context. See bug http://crbug.com/40803 | |
216 dbus_bool_t ok = dbus_connection_set_timeout_functions( | |
217 dbus_g_connection_get_connection(connection_), | |
218 NULL, NULL, NULL, NULL, NULL); | |
219 DCHECK(ok); | |
220 | |
221 proxy_.reset(dbus_g_proxy_new_for_name(connection_, | |
222 kNetworkManagerServiceName, | |
223 kNetworkManagerPath, | |
224 kNetworkManagerInterface)); | |
225 DCHECK(proxy_.get()); | |
226 | |
227 // Validate the proxy object by checking we can enumerate devices. | 114 // Validate the proxy object by checking we can enumerate devices. |
228 ScopedGPtrArrayPtr device_list(GetAdapterDeviceList()); | 115 std::vector<std::string> adapter_paths; |
229 return !!device_list.get(); | 116 const bool success = GetAdapterDeviceList(&adapter_paths); |
| 117 VLOG(1) << "Init() result: " << success; |
| 118 return success; |
230 } | 119 } |
231 | 120 |
232 bool NetworkManagerWlanApi::GetAccessPointData( | 121 bool NetworkManagerWlanApi::GetAccessPointData( |
233 WifiData::AccessPointDataSet* data) { | 122 WifiData::AccessPointDataSet* data) { |
234 ScopedGPtrArrayPtr device_list(GetAdapterDeviceList()); | 123 std::vector<std::string> device_paths; |
235 if (device_list == NULL) { | 124 if (!GetAdapterDeviceList(&device_paths)) { |
236 DLOG(WARNING) << "Could not enumerate access points"; | 125 LOG(WARNING) << "Could not enumerate access points"; |
237 return false; | 126 return false; |
238 } | 127 } |
239 int success_count = 0; | 128 int success_count = 0; |
240 int fail_count = 0; | 129 int fail_count = 0; |
241 | 130 |
242 // Iterate the devices, getting APs for each wireless adapter found | 131 // Iterate the devices, getting APs for each wireless adapter found |
243 for (guint i = 0; i < device_list->len; i++) { | 132 for (size_t i = 0; i < device_paths.size(); ++i) { |
244 const gchar* device_path = | 133 const std::string& device_path = device_paths[i]; |
245 reinterpret_cast<const gchar*>(g_ptr_array_index(device_list, i)); | 134 VLOG(1) << "Checking device: " << device_path; |
246 | 135 |
247 ScopedDBusGProxyPtr device_properties_proxy(dbus_g_proxy_new_from_proxy( | 136 dbus::ObjectProxy* device_proxy = |
248 proxy_.get(), DBUS_INTERFACE_PROPERTIES, device_path)); | 137 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
249 ScopedGValue device_type_g_value; | 138 device_path); |
250 dbus_g_proxy_call(device_properties_proxy.get(), "Get", &error_, | |
251 G_TYPE_STRING, "org.freedesktop.NetworkManager.Device", | |
252 G_TYPE_STRING, "DeviceType", | |
253 G_TYPE_INVALID, | |
254 G_TYPE_VALUE, &device_type_g_value.v, | |
255 G_TYPE_INVALID); | |
256 if (CheckError()) | |
257 continue; | |
258 | 139 |
259 const guint device_type = g_value_get_uint(&device_type_g_value.v); | 140 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); |
| 141 dbus::MessageWriter builder(&method_call); |
| 142 builder.AppendString("org.freedesktop.NetworkManager.Device"); |
| 143 builder.AppendString("DeviceType"); |
| 144 scoped_ptr<dbus::Response> response( |
| 145 device_proxy->CallMethodAndBlock( |
| 146 &method_call, |
| 147 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
| 148 if (!response.get()) { |
| 149 LOG(WARNING) << "Failed to get the device type for " << device_path; |
| 150 continue; // Check the next device. |
| 151 } |
| 152 dbus::MessageReader reader(response.get()); |
| 153 uint32 device_type = 0; |
| 154 if (!reader.PopVariantOfUint32(&device_type)) { |
| 155 LOG(WARNING) << "Unexpected response for " << device_type << ": " |
| 156 << response->ToString(); |
| 157 continue; // Check the next device. |
| 158 } |
| 159 VLOG(1) << "Device type: " << device_type; |
260 | 160 |
261 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter | 161 if (device_type == NM_DEVICE_TYPE_WIFI) { // Found a wlan adapter |
262 if (GetAccessPointsForAdapter(device_path, data)) | 162 if (GetAccessPointsForAdapter(device_path, data)) |
263 ++success_count; | 163 ++success_count; |
264 else | 164 else |
265 ++fail_count; | 165 ++fail_count; |
266 } | 166 } |
267 } | 167 } |
268 // At least one successfull scan overrides any other adapter reporting error. | 168 // At least one successfull scan overrides any other adapter reporting error. |
269 return success_count || fail_count == 0; | 169 return success_count || fail_count == 0; |
270 } | 170 } |
271 | 171 |
272 bool NetworkManagerWlanApi::CheckError() { | 172 bool NetworkManagerWlanApi::GetAdapterDeviceList( |
273 if (error_) { | 173 std::vector<std::string>* device_paths) { |
274 LOG(ERROR) << "Failed to complete NetworkManager call: " << error_->message; | 174 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); |
275 g_error_free(error_); | 175 scoped_ptr<dbus::Response> response( |
276 error_ = NULL; | 176 network_manager_proxy_->CallMethodAndBlock( |
277 return true; | 177 &method_call, |
| 178 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
| 179 if (!response.get()) { |
| 180 LOG(WARNING) << "Failed to get the device list"; |
| 181 return false; |
278 } | 182 } |
279 return false; | 183 |
| 184 dbus::MessageReader reader(response.get()); |
| 185 if (!reader.PopArrayOfObjectPaths(device_paths)) { |
| 186 LOG(WARNING) << "Unexpected response: " << response->ToString(); |
| 187 return false; |
| 188 } |
| 189 return true; |
280 } | 190 } |
281 | 191 |
282 GPtrArray* NetworkManagerWlanApi::GetAdapterDeviceList() { | |
283 GPtrArray* device_list = NULL; | |
284 dbus_g_proxy_call(proxy_.get(), "GetDevices", &error_, | |
285 G_TYPE_INVALID, | |
286 dbus_g_type_get_collection("GPtrArray", | |
287 DBUS_TYPE_G_OBJECT_PATH), | |
288 &device_list, | |
289 G_TYPE_INVALID); | |
290 if (CheckError()) | |
291 return NULL; | |
292 return device_list; | |
293 } | |
294 | 192 |
295 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( | 193 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( |
296 const gchar* adapter_path, WifiData::AccessPointDataSet* data) { | 194 const std::string& adapter_path, WifiData::AccessPointDataSet* data) { |
297 DCHECK(proxy_.get()); | |
298 | |
299 // Create a proxy object for this wifi adapter, and ask it to do a scan | 195 // Create a proxy object for this wifi adapter, and ask it to do a scan |
300 // (or at least, dump its scan results). | 196 // (or at least, dump its scan results). |
301 ScopedDBusGProxyPtr wifi_adapter_proxy(dbus_g_proxy_new_from_proxy( | 197 dbus::ObjectProxy* device_proxy = |
302 proxy_.get(), "org.freedesktop.NetworkManager.Device.Wireless", | 198 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
303 adapter_path)); | 199 adapter_path); |
| 200 dbus::MethodCall method_call( |
| 201 "org.freedesktop.NetworkManager.Device.Wireless", |
| 202 "GetAccessPoints"); |
| 203 scoped_ptr<dbus::Response> response( |
| 204 device_proxy->CallMethodAndBlock( |
| 205 &method_call, |
| 206 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
| 207 if (!response.get()) { |
| 208 LOG(WARNING) << "Failed to get access points data for " << adapter_path; |
| 209 return false; |
| 210 } |
| 211 dbus::MessageReader reader(response.get()); |
| 212 std::vector<std::string> access_point_paths; |
| 213 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { |
| 214 LOG(WARNING) << "Unexpected response for " << adapter_path << ": " |
| 215 << response->ToString(); |
| 216 return false; |
| 217 } |
304 | 218 |
305 GPtrArray* ap_list_raw = NULL; | 219 VLOG(1) << "Wireless adapter " << adapter_path << " found " |
306 // Enumerate the access points for this adapter. | 220 << access_point_paths.size() << " access points."; |
307 dbus_g_proxy_call(wifi_adapter_proxy.get(), "GetAccessPoints", &error_, | |
308 G_TYPE_INVALID, | |
309 dbus_g_type_get_collection("GPtrArray", | |
310 DBUS_TYPE_G_OBJECT_PATH), | |
311 &ap_list_raw, | |
312 G_TYPE_INVALID); | |
313 ScopedGPtrArrayPtr ap_list(ap_list_raw); // Takes ownership. | |
314 ap_list_raw = NULL; | |
315 | 221 |
316 if (CheckError()) | 222 for (size_t i = 0; i < access_point_paths.size(); ++i) { |
317 return false; | 223 const std::string& access_point_path = access_point_paths[i]; |
| 224 VLOG(1) << "Checking access point: " << access_point_path; |
318 | 225 |
319 DVLOG(1) << "Wireless adapter " << adapter_path << " found " << ap_list->len | 226 dbus::ObjectProxy* access_point_proxy = |
320 << " access points."; | 227 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
321 | 228 access_point_path); |
322 for (guint i = 0; i < ap_list->len; i++) { | |
323 const gchar* ap_path = | |
324 reinterpret_cast<const gchar*>(g_ptr_array_index(ap_list, i)); | |
325 ScopedDBusGProxyPtr access_point_proxy(dbus_g_proxy_new_from_proxy( | |
326 proxy_.get(), DBUS_INTERFACE_PROPERTIES, ap_path)); | |
327 | 229 |
328 AccessPointData access_point_data; | 230 AccessPointData access_point_data; |
329 { // Read SSID. | 231 { |
330 ScopedGValue ssid_g_value; | 232 scoped_ptr<dbus::Response> response( |
331 if (!GetAccessPointProperty(access_point_proxy.get(), "Ssid", | 233 GetAccessPointProperty(access_point_proxy, "Ssid")); |
332 G_TYPE_BOXED, &ssid_g_value.v)) | 234 if (!response.get()) |
333 continue; | 235 continue; |
334 const GArray* ssid = | 236 // The response should contain a variant that contains an array of bytes. |
335 reinterpret_cast<const GArray*>(g_value_get_boxed(&ssid_g_value.v)); | 237 dbus::MessageReader reader(response.get()); |
336 UTF8ToUTF16(ssid->data, ssid->len, &access_point_data.ssid); | 238 dbus::MessageReader variant_reader(response.get()); |
| 239 if (!reader.PopVariant(&variant_reader)) { |
| 240 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " |
| 241 << response->ToString(); |
| 242 continue; |
| 243 } |
| 244 uint8* ssid_bytes = NULL; |
| 245 size_t ssid_length = 0; |
| 246 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) { |
| 247 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " |
| 248 << response->ToString(); |
| 249 continue; |
| 250 } |
| 251 std::string ssid(ssid_bytes, ssid_bytes + ssid_length); |
| 252 access_point_data.ssid = UTF8ToUTF16(ssid); |
337 } | 253 } |
338 | 254 |
339 { // Read the mac address | 255 { // Read the mac address |
340 ScopedGValue mac_g_value; | 256 scoped_ptr<dbus::Response> response( |
341 if (!GetAccessPointProperty(access_point_proxy.get(), "HwAddress", | 257 GetAccessPointProperty(access_point_proxy, "HwAddress")); |
342 G_TYPE_STRING, &mac_g_value.v)) | 258 if (!response.get()) |
343 continue; | 259 continue; |
344 std::string mac = g_value_get_string(&mac_g_value.v); | 260 dbus::MessageReader reader(response.get()); |
| 261 std::string mac; |
| 262 if (!reader.PopVariantOfString(&mac)) { |
| 263 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " |
| 264 << response->ToString(); |
| 265 continue; |
| 266 } |
| 267 |
345 ReplaceSubstringsAfterOffset(&mac, 0U, ":", ""); | 268 ReplaceSubstringsAfterOffset(&mac, 0U, ":", ""); |
346 std::vector<uint8> mac_bytes; | 269 std::vector<uint8> mac_bytes; |
347 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) { | 270 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) { |
348 DLOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size() | 271 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size() |
349 << " bytes) so using raw string: " << mac; | 272 << " bytes) so using raw string: " << mac; |
350 access_point_data.mac_address = UTF8ToUTF16(mac); | 273 access_point_data.mac_address = UTF8ToUTF16(mac); |
351 } else { | 274 } else { |
352 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]); | 275 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]); |
353 } | 276 } |
354 } | 277 } |
355 | 278 |
356 { // Read signal strength. | 279 { // Read signal strength. |
357 ScopedGValue signal_g_value; | 280 scoped_ptr<dbus::Response> response( |
358 if (!GetAccessPointProperty(access_point_proxy.get(), "Strength", | 281 GetAccessPointProperty(access_point_proxy, "Strength")); |
359 G_TYPE_UCHAR, &signal_g_value.v)) | 282 if (!response.get()) |
360 continue; | 283 continue; |
| 284 dbus::MessageReader reader(response.get()); |
| 285 uint8 strength = 0; |
| 286 if (!reader.PopVariantOfByte(&strength)) { |
| 287 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " |
| 288 << response->ToString(); |
| 289 continue; |
| 290 } |
361 // Convert strength as a percentage into dBs. | 291 // Convert strength as a percentage into dBs. |
362 access_point_data.radio_signal_strength = | 292 access_point_data.radio_signal_strength = -100 + strength / 2; |
363 -100 + g_value_get_uchar(&signal_g_value.v) / 2; | |
364 } | 293 } |
365 | 294 |
366 { // Read the channel | 295 { // Read the channel |
367 ScopedGValue freq_g_value; | 296 scoped_ptr<dbus::Response> response( |
368 if (!GetAccessPointProperty(access_point_proxy.get(), "Frequency", | 297 GetAccessPointProperty(access_point_proxy, "Frequency")); |
369 G_TYPE_UINT, &freq_g_value.v)) | 298 if (!response.get()) |
370 continue; | 299 continue; |
| 300 dbus::MessageReader reader(response.get()); |
| 301 uint32 frequency = 0; |
| 302 if (!reader.PopVariantOfUint32(&frequency)) { |
| 303 LOG(WARNING) << "Unexpected response for " << access_point_path << ": " |
| 304 << response->ToString(); |
| 305 continue; |
| 306 } |
| 307 |
371 // NetworkManager returns frequency in MHz. | 308 // NetworkManager returns frequency in MHz. |
372 access_point_data.channel = | 309 access_point_data.channel = |
373 frquency_in_khz_to_channel(g_value_get_uint(&freq_g_value.v) * 1000); | 310 frquency_in_khz_to_channel(frequency * 1000); |
374 } | 311 } |
| 312 VLOG(1) << "Access point data of " << access_point_path << ": " |
| 313 << "SSID: " << access_point_data.ssid << ", " |
| 314 << "MAC: " << access_point_data.mac_address << ", " |
| 315 << "Strength: " << access_point_data.radio_signal_strength << ", " |
| 316 << "Channel: " << access_point_data.channel; |
| 317 |
375 data->insert(access_point_data); | 318 data->insert(access_point_data); |
376 } | 319 } |
377 return true; | 320 return true; |
378 } | 321 } |
379 | 322 |
380 bool NetworkManagerWlanApi::GetAccessPointProperty(DBusGProxy* proxy, | 323 dbus::Response* NetworkManagerWlanApi::GetAccessPointProperty( |
381 const char* property_name, | 324 dbus::ObjectProxy* access_point_proxy, |
382 int expected_gvalue_type, | 325 const std::string& property_name) { |
383 GValue* value_out) { | 326 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); |
384 dbus_g_proxy_call(proxy, "Get", &error_, | 327 dbus::MessageWriter builder(&method_call); |
385 G_TYPE_STRING, "org.freedesktop.NetworkManager.AccessPoint", | 328 builder.AppendString("org.freedesktop.NetworkManager.AccessPoint"); |
386 G_TYPE_STRING, property_name, | 329 builder.AppendString(property_name); |
387 G_TYPE_INVALID, | 330 dbus::Response* response = access_point_proxy->CallMethodAndBlock( |
388 G_TYPE_VALUE, value_out, | 331 &method_call, |
389 G_TYPE_INVALID); | 332 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); |
390 if (CheckError()) | 333 if (!response) { |
391 return false; | 334 LOG(WARNING) << "Failed to get property for " << property_name; |
392 if (!G_VALUE_HOLDS(value_out, expected_gvalue_type)) { | |
393 DLOG(WARNING) << "Property " << property_name << " unexptected type " | |
394 << G_VALUE_TYPE(value_out); | |
395 return false; | |
396 } | 335 } |
397 return true; | 336 return response; |
398 } | 337 } |
399 | 338 |
400 } // namespace | 339 } // namespace |
401 | 340 |
402 // static | 341 // static |
403 template<> | 342 template<> |
404 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { | 343 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { |
405 return new WifiDataProviderLinux(); | 344 return new WifiDataProviderLinux(); |
406 } | 345 } |
407 | 346 |
(...skipping 10 matching lines...) Expand all Loading... |
418 return wlan_api.release(); | 357 return wlan_api.release(); |
419 return NULL; | 358 return NULL; |
420 } | 359 } |
421 | 360 |
422 PollingPolicyInterface* WifiDataProviderLinux::NewPollingPolicy() { | 361 PollingPolicyInterface* WifiDataProviderLinux::NewPollingPolicy() { |
423 return new GenericPollingPolicy<kDefaultPollingIntervalMilliseconds, | 362 return new GenericPollingPolicy<kDefaultPollingIntervalMilliseconds, |
424 kNoChangePollingIntervalMilliseconds, | 363 kNoChangePollingIntervalMilliseconds, |
425 kTwoNoChangePollingIntervalMilliseconds, | 364 kTwoNoChangePollingIntervalMilliseconds, |
426 kNoWifiPollingIntervalMilliseconds>; | 365 kNoWifiPollingIntervalMilliseconds>; |
427 } | 366 } |
| 367 |
| 368 WifiDataProviderCommon::WlanApiInterface* |
| 369 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { |
| 370 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); |
| 371 if (wlan_api->InitWithBus(bus)) |
| 372 return wlan_api.release(); |
| 373 return NULL; |
| 374 } |
OLD | NEW |