OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 device_path); | 141 device_path); |
142 | 142 |
143 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); | 143 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); |
144 dbus::MessageWriter builder(&method_call); | 144 dbus::MessageWriter builder(&method_call); |
145 builder.AppendString("org.freedesktop.NetworkManager.Device"); | 145 builder.AppendString("org.freedesktop.NetworkManager.Device"); |
146 builder.AppendString("DeviceType"); | 146 builder.AppendString("DeviceType"); |
147 scoped_ptr<dbus::Response> response( | 147 scoped_ptr<dbus::Response> response( |
148 device_proxy->CallMethodAndBlock( | 148 device_proxy->CallMethodAndBlock( |
149 &method_call, | 149 &method_call, |
150 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 150 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
151 if (!response.get()) { | 151 if (!response) { |
152 LOG(WARNING) << "Failed to get the device type for " | 152 LOG(WARNING) << "Failed to get the device type for " |
153 << device_path.value(); | 153 << device_path.value(); |
154 continue; // Check the next device. | 154 continue; // Check the next device. |
155 } | 155 } |
156 dbus::MessageReader reader(response.get()); | 156 dbus::MessageReader reader(response.get()); |
157 uint32 device_type = 0; | 157 uint32 device_type = 0; |
158 if (!reader.PopVariantOfUint32(&device_type)) { | 158 if (!reader.PopVariantOfUint32(&device_type)) { |
159 LOG(WARNING) << "Unexpected response for " << device_type << ": " | 159 LOG(WARNING) << "Unexpected response for " << device_type << ": " |
160 << response->ToString(); | 160 << response->ToString(); |
161 continue; // Check the next device. | 161 continue; // Check the next device. |
(...skipping 11 matching lines...) Expand all Loading... |
173 return success_count || fail_count == 0; | 173 return success_count || fail_count == 0; |
174 } | 174 } |
175 | 175 |
176 bool NetworkManagerWlanApi::GetAdapterDeviceList( | 176 bool NetworkManagerWlanApi::GetAdapterDeviceList( |
177 std::vector<dbus::ObjectPath>* device_paths) { | 177 std::vector<dbus::ObjectPath>* device_paths) { |
178 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); | 178 dbus::MethodCall method_call(kNetworkManagerInterface, "GetDevices"); |
179 scoped_ptr<dbus::Response> response( | 179 scoped_ptr<dbus::Response> response( |
180 network_manager_proxy_->CallMethodAndBlock( | 180 network_manager_proxy_->CallMethodAndBlock( |
181 &method_call, | 181 &method_call, |
182 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 182 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
183 if (!response.get()) { | 183 if (!response) { |
184 LOG(WARNING) << "Failed to get the device list"; | 184 LOG(WARNING) << "Failed to get the device list"; |
185 return false; | 185 return false; |
186 } | 186 } |
187 | 187 |
188 dbus::MessageReader reader(response.get()); | 188 dbus::MessageReader reader(response.get()); |
189 if (!reader.PopArrayOfObjectPaths(device_paths)) { | 189 if (!reader.PopArrayOfObjectPaths(device_paths)) { |
190 LOG(WARNING) << "Unexpected response: " << response->ToString(); | 190 LOG(WARNING) << "Unexpected response: " << response->ToString(); |
191 return false; | 191 return false; |
192 } | 192 } |
193 return true; | 193 return true; |
194 } | 194 } |
195 | 195 |
196 | 196 |
197 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( | 197 bool NetworkManagerWlanApi::GetAccessPointsForAdapter( |
198 const dbus::ObjectPath& adapter_path, WifiData::AccessPointDataSet* data) { | 198 const dbus::ObjectPath& adapter_path, WifiData::AccessPointDataSet* data) { |
199 // Create a proxy object for this wifi adapter, and ask it to do a scan | 199 // Create a proxy object for this wifi adapter, and ask it to do a scan |
200 // (or at least, dump its scan results). | 200 // (or at least, dump its scan results). |
201 dbus::ObjectProxy* device_proxy = | 201 dbus::ObjectProxy* device_proxy = |
202 system_bus_->GetObjectProxy(kNetworkManagerServiceName, | 202 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
203 adapter_path); | 203 adapter_path); |
204 dbus::MethodCall method_call( | 204 dbus::MethodCall method_call( |
205 "org.freedesktop.NetworkManager.Device.Wireless", | 205 "org.freedesktop.NetworkManager.Device.Wireless", |
206 "GetAccessPoints"); | 206 "GetAccessPoints"); |
207 scoped_ptr<dbus::Response> response( | 207 scoped_ptr<dbus::Response> response( |
208 device_proxy->CallMethodAndBlock( | 208 device_proxy->CallMethodAndBlock( |
209 &method_call, | 209 &method_call, |
210 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); | 210 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT)); |
211 if (!response.get()) { | 211 if (!response) { |
212 LOG(WARNING) << "Failed to get access points data for " | 212 LOG(WARNING) << "Failed to get access points data for " |
213 << adapter_path.value(); | 213 << adapter_path.value(); |
214 return false; | 214 return false; |
215 } | 215 } |
216 dbus::MessageReader reader(response.get()); | 216 dbus::MessageReader reader(response.get()); |
217 std::vector<dbus::ObjectPath> access_point_paths; | 217 std::vector<dbus::ObjectPath> access_point_paths; |
218 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { | 218 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { |
219 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": " | 219 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": " |
220 << response->ToString(); | 220 << response->ToString(); |
221 return false; | 221 return false; |
222 } | 222 } |
223 | 223 |
224 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found " | 224 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found " |
225 << access_point_paths.size() << " access points."; | 225 << access_point_paths.size() << " access points."; |
226 | 226 |
227 for (size_t i = 0; i < access_point_paths.size(); ++i) { | 227 for (size_t i = 0; i < access_point_paths.size(); ++i) { |
228 const dbus::ObjectPath& access_point_path = access_point_paths[i]; | 228 const dbus::ObjectPath& access_point_path = access_point_paths[i]; |
229 VLOG(1) << "Checking access point: " << access_point_path.value(); | 229 VLOG(1) << "Checking access point: " << access_point_path.value(); |
230 | 230 |
231 dbus::ObjectProxy* access_point_proxy = | 231 dbus::ObjectProxy* access_point_proxy = |
232 system_bus_->GetObjectProxy(kNetworkManagerServiceName, | 232 system_bus_->GetObjectProxy(kNetworkManagerServiceName, |
233 access_point_path); | 233 access_point_path); |
234 | 234 |
235 AccessPointData access_point_data; | 235 AccessPointData access_point_data; |
236 { | 236 { |
237 scoped_ptr<dbus::Response> response( | 237 scoped_ptr<dbus::Response> response( |
238 GetAccessPointProperty(access_point_proxy, "Ssid")); | 238 GetAccessPointProperty(access_point_proxy, "Ssid")); |
239 if (!response.get()) | 239 if (!response) |
240 continue; | 240 continue; |
241 // The response should contain a variant that contains an array of bytes. | 241 // The response should contain a variant that contains an array of bytes. |
242 dbus::MessageReader reader(response.get()); | 242 dbus::MessageReader reader(response.get()); |
243 dbus::MessageReader variant_reader(response.get()); | 243 dbus::MessageReader variant_reader(response.get()); |
244 if (!reader.PopVariant(&variant_reader)) { | 244 if (!reader.PopVariant(&variant_reader)) { |
245 LOG(WARNING) << "Unexpected response for " << access_point_path.value() | 245 LOG(WARNING) << "Unexpected response for " << access_point_path.value() |
246 << ": " << response->ToString(); | 246 << ": " << response->ToString(); |
247 continue; | 247 continue; |
248 } | 248 } |
249 uint8* ssid_bytes = NULL; | 249 uint8* ssid_bytes = NULL; |
250 size_t ssid_length = 0; | 250 size_t ssid_length = 0; |
251 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) { | 251 if (!variant_reader.PopArrayOfBytes(&ssid_bytes, &ssid_length)) { |
252 LOG(WARNING) << "Unexpected response for " << access_point_path.value() | 252 LOG(WARNING) << "Unexpected response for " << access_point_path.value() |
253 << ": " << response->ToString(); | 253 << ": " << response->ToString(); |
254 continue; | 254 continue; |
255 } | 255 } |
256 std::string ssid(ssid_bytes, ssid_bytes + ssid_length); | 256 std::string ssid(ssid_bytes, ssid_bytes + ssid_length); |
257 access_point_data.ssid = UTF8ToUTF16(ssid); | 257 access_point_data.ssid = UTF8ToUTF16(ssid); |
258 } | 258 } |
259 | 259 |
260 { // Read the mac address | 260 { // Read the mac address |
261 scoped_ptr<dbus::Response> response( | 261 scoped_ptr<dbus::Response> response( |
262 GetAccessPointProperty(access_point_proxy, "HwAddress")); | 262 GetAccessPointProperty(access_point_proxy, "HwAddress")); |
263 if (!response.get()) | 263 if (!response) |
264 continue; | 264 continue; |
265 dbus::MessageReader reader(response.get()); | 265 dbus::MessageReader reader(response.get()); |
266 std::string mac; | 266 std::string mac; |
267 if (!reader.PopVariantOfString(&mac)) { | 267 if (!reader.PopVariantOfString(&mac)) { |
268 LOG(WARNING) << "Unexpected response for " << access_point_path.value() | 268 LOG(WARNING) << "Unexpected response for " << access_point_path.value() |
269 << ": " << response->ToString(); | 269 << ": " << response->ToString(); |
270 continue; | 270 continue; |
271 } | 271 } |
272 | 272 |
273 ReplaceSubstringsAfterOffset(&mac, 0U, ":", std::string()); | 273 ReplaceSubstringsAfterOffset(&mac, 0U, ":", std::string()); |
274 std::vector<uint8> mac_bytes; | 274 std::vector<uint8> mac_bytes; |
275 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) { | 275 if (!base::HexStringToBytes(mac, &mac_bytes) || mac_bytes.size() != 6) { |
276 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size() | 276 LOG(WARNING) << "Can't parse mac address (found " << mac_bytes.size() |
277 << " bytes) so using raw string: " << mac; | 277 << " bytes) so using raw string: " << mac; |
278 access_point_data.mac_address = UTF8ToUTF16(mac); | 278 access_point_data.mac_address = UTF8ToUTF16(mac); |
279 } else { | 279 } else { |
280 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]); | 280 access_point_data.mac_address = MacAddressAsString16(&mac_bytes[0]); |
281 } | 281 } |
282 } | 282 } |
283 | 283 |
284 { // Read signal strength. | 284 { // Read signal strength. |
285 scoped_ptr<dbus::Response> response( | 285 scoped_ptr<dbus::Response> response( |
286 GetAccessPointProperty(access_point_proxy, "Strength")); | 286 GetAccessPointProperty(access_point_proxy, "Strength")); |
287 if (!response.get()) | 287 if (!response) |
288 continue; | 288 continue; |
289 dbus::MessageReader reader(response.get()); | 289 dbus::MessageReader reader(response.get()); |
290 uint8 strength = 0; | 290 uint8 strength = 0; |
291 if (!reader.PopVariantOfByte(&strength)) { | 291 if (!reader.PopVariantOfByte(&strength)) { |
292 LOG(WARNING) << "Unexpected response for " << access_point_path.value() | 292 LOG(WARNING) << "Unexpected response for " << access_point_path.value() |
293 << ": " << response->ToString(); | 293 << ": " << response->ToString(); |
294 continue; | 294 continue; |
295 } | 295 } |
296 // Convert strength as a percentage into dBs. | 296 // Convert strength as a percentage into dBs. |
297 access_point_data.radio_signal_strength = -100 + strength / 2; | 297 access_point_data.radio_signal_strength = -100 + strength / 2; |
298 } | 298 } |
299 | 299 |
300 { // Read the channel | 300 { // Read the channel |
301 scoped_ptr<dbus::Response> response( | 301 scoped_ptr<dbus::Response> response( |
302 GetAccessPointProperty(access_point_proxy, "Frequency")); | 302 GetAccessPointProperty(access_point_proxy, "Frequency")); |
303 if (!response.get()) | 303 if (!response) |
304 continue; | 304 continue; |
305 dbus::MessageReader reader(response.get()); | 305 dbus::MessageReader reader(response.get()); |
306 uint32 frequency = 0; | 306 uint32 frequency = 0; |
307 if (!reader.PopVariantOfUint32(&frequency)) { | 307 if (!reader.PopVariantOfUint32(&frequency)) { |
308 LOG(WARNING) << "Unexpected response for " << access_point_path.value() | 308 LOG(WARNING) << "Unexpected response for " << access_point_path.value() |
309 << ": " << response->ToString(); | 309 << ": " << response->ToString(); |
310 continue; | 310 continue; |
311 } | 311 } |
312 | 312 |
313 // NetworkManager returns frequency in MHz. | 313 // NetworkManager returns frequency in MHz. |
(...skipping 14 matching lines...) Expand all Loading... |
328 scoped_ptr<dbus::Response> NetworkManagerWlanApi::GetAccessPointProperty( | 328 scoped_ptr<dbus::Response> NetworkManagerWlanApi::GetAccessPointProperty( |
329 dbus::ObjectProxy* access_point_proxy, | 329 dbus::ObjectProxy* access_point_proxy, |
330 const std::string& property_name) { | 330 const std::string& property_name) { |
331 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); | 331 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); |
332 dbus::MessageWriter builder(&method_call); | 332 dbus::MessageWriter builder(&method_call); |
333 builder.AppendString("org.freedesktop.NetworkManager.AccessPoint"); | 333 builder.AppendString("org.freedesktop.NetworkManager.AccessPoint"); |
334 builder.AppendString(property_name); | 334 builder.AppendString(property_name); |
335 scoped_ptr<dbus::Response> response = access_point_proxy->CallMethodAndBlock( | 335 scoped_ptr<dbus::Response> response = access_point_proxy->CallMethodAndBlock( |
336 &method_call, | 336 &method_call, |
337 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); | 337 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); |
338 if (!response.get()) { | 338 if (!response) { |
339 LOG(WARNING) << "Failed to get property for " << property_name; | 339 LOG(WARNING) << "Failed to get property for " << property_name; |
340 } | 340 } |
341 return response.Pass(); | 341 return response.Pass(); |
342 } | 342 } |
343 | 343 |
344 } // namespace | 344 } // namespace |
345 | 345 |
346 // static | 346 // static |
347 template<> | 347 template<> |
348 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { | 348 WifiDataProviderImplBase* WifiDataProvider::DefaultFactoryFunction() { |
(...skipping 23 matching lines...) Expand all Loading... |
372 | 372 |
373 WifiDataProviderCommon::WlanApiInterface* | 373 WifiDataProviderCommon::WlanApiInterface* |
374 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { | 374 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { |
375 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); | 375 scoped_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); |
376 if (wlan_api->InitWithBus(bus)) | 376 if (wlan_api->InitWithBus(bus)) |
377 return wlan_api.release(); | 377 return wlan_api.release(); |
378 return NULL; | 378 return NULL; |
379 } | 379 } |
380 | 380 |
381 } // namespace content | 381 } // namespace content |
OLD | NEW |