| 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 "device/geolocation/wifi_data_provider_linux.h" | 9 #include "device/geolocation/wifi_data_provider_linux.h" |
| 10 | 10 |
| 11 #include <stddef.h> | 11 #include <stddef.h> |
| 12 #include <stdint.h> | 12 #include <stdint.h> |
| 13 | 13 |
| 14 #include <memory> | 14 #include <memory> |
| 15 | 15 |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ptr_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 19 #include "dbus/bus.h" | 20 #include "dbus/bus.h" |
| 20 #include "dbus/message.h" | 21 #include "dbus/message.h" |
| 21 #include "dbus/object_path.h" | 22 #include "dbus/object_path.h" |
| 22 #include "dbus/object_proxy.h" | 23 #include "dbus/object_proxy.h" |
| 23 #include "device/geolocation/wifi_data_provider_manager.h" | 24 #include "device/geolocation/wifi_data_provider_manager.h" |
| 24 | 25 |
| 25 namespace device { | 26 namespace device { |
| 26 namespace { | 27 namespace { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 dbus::ObjectProxy* proxy, | 80 dbus::ObjectProxy* proxy, |
| 80 const std::string& property_name); | 81 const std::string& property_name); |
| 81 | 82 |
| 82 scoped_refptr<dbus::Bus> system_bus_; | 83 scoped_refptr<dbus::Bus> system_bus_; |
| 83 dbus::ObjectProxy* network_manager_proxy_; | 84 dbus::ObjectProxy* network_manager_proxy_; |
| 84 | 85 |
| 85 DISALLOW_COPY_AND_ASSIGN(NetworkManagerWlanApi); | 86 DISALLOW_COPY_AND_ASSIGN(NetworkManagerWlanApi); |
| 86 }; | 87 }; |
| 87 | 88 |
| 88 // Convert a wifi frequency to the corresponding channel. Adapted from | 89 // Convert a wifi frequency to the corresponding channel. Adapted from |
| 89 // geolocaiton/wifilib.cc in googleclient (internal to google). | 90 // geolocation/wifilib.cc in googleclient (internal to google). |
| 90 int frquency_in_khz_to_channel(int frequency_khz) { | 91 int frquency_in_khz_to_channel(int frequency_khz) { |
| 91 if (frequency_khz >= 2412000 && frequency_khz <= 2472000) // Channels 1-13. | 92 if (frequency_khz >= 2412000 && frequency_khz <= 2472000) // Channels 1-13. |
| 92 return (frequency_khz - 2407000) / 5000; | 93 return (frequency_khz - 2407000) / 5000; |
| 93 if (frequency_khz == 2484000) | 94 if (frequency_khz == 2484000) |
| 94 return 14; | 95 return 14; |
| 95 if (frequency_khz > 5000000 && frequency_khz < 6000000) // .11a bands. | 96 if (frequency_khz > 5000000 && frequency_khz < 6000000) // .11a bands. |
| 96 return (frequency_khz - 5000000) / 5000; | 97 return (frequency_khz - 5000000) / 5000; |
| 97 // Ignore everything else. | 98 // Ignore everything else. |
| 98 return AccessPointData().channel; // invalid channel | 99 return AccessPointData().channel; // invalid channel |
| 99 } | 100 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 128 WifiData::AccessPointDataSet* data) { | 129 WifiData::AccessPointDataSet* data) { |
| 129 std::vector<dbus::ObjectPath> device_paths; | 130 std::vector<dbus::ObjectPath> device_paths; |
| 130 if (!GetAdapterDeviceList(&device_paths)) { | 131 if (!GetAdapterDeviceList(&device_paths)) { |
| 131 LOG(WARNING) << "Could not enumerate access points"; | 132 LOG(WARNING) << "Could not enumerate access points"; |
| 132 return false; | 133 return false; |
| 133 } | 134 } |
| 134 int success_count = 0; | 135 int success_count = 0; |
| 135 int fail_count = 0; | 136 int fail_count = 0; |
| 136 | 137 |
| 137 // Iterate the devices, getting APs for each wireless adapter found | 138 // Iterate the devices, getting APs for each wireless adapter found |
| 138 for (size_t i = 0; i < device_paths.size(); ++i) { | 139 for (const dbus::ObjectPath& device_path : device_paths) { |
| 139 const dbus::ObjectPath& device_path = device_paths[i]; | |
| 140 VLOG(1) << "Checking device: " << device_path.value(); | 140 VLOG(1) << "Checking device: " << device_path.value(); |
| 141 | 141 |
| 142 dbus::ObjectProxy* device_proxy = | 142 dbus::ObjectProxy* device_proxy = |
| 143 system_bus_->GetObjectProxy(kNetworkManagerServiceName, device_path); | 143 system_bus_->GetObjectProxy(kNetworkManagerServiceName, device_path); |
| 144 | 144 |
| 145 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); | 145 dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get"); |
| 146 dbus::MessageWriter builder(&method_call); | 146 dbus::MessageWriter builder(&method_call); |
| 147 builder.AppendString("org.freedesktop.NetworkManager.Device"); | 147 builder.AppendString("org.freedesktop.NetworkManager.Device"); |
| 148 builder.AppendString("DeviceType"); | 148 builder.AppendString("DeviceType"); |
| 149 std::unique_ptr<dbus::Response> response(device_proxy->CallMethodAndBlock( | 149 std::unique_ptr<dbus::Response> response(device_proxy->CallMethodAndBlock( |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 std::vector<dbus::ObjectPath> access_point_paths; | 212 std::vector<dbus::ObjectPath> access_point_paths; |
| 213 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { | 213 if (!reader.PopArrayOfObjectPaths(&access_point_paths)) { |
| 214 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": " | 214 LOG(WARNING) << "Unexpected response for " << adapter_path.value() << ": " |
| 215 << response->ToString(); | 215 << response->ToString(); |
| 216 return false; | 216 return false; |
| 217 } | 217 } |
| 218 | 218 |
| 219 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found " | 219 VLOG(1) << "Wireless adapter " << adapter_path.value() << " found " |
| 220 << access_point_paths.size() << " access points."; | 220 << access_point_paths.size() << " access points."; |
| 221 | 221 |
| 222 for (size_t i = 0; i < access_point_paths.size(); ++i) { | 222 for (const dbus::ObjectPath& access_point_path : access_point_paths) { |
| 223 const dbus::ObjectPath& access_point_path = access_point_paths[i]; | |
| 224 VLOG(1) << "Checking access point: " << access_point_path.value(); | 223 VLOG(1) << "Checking access point: " << access_point_path.value(); |
| 225 | 224 |
| 226 dbus::ObjectProxy* access_point_proxy = system_bus_->GetObjectProxy( | 225 dbus::ObjectProxy* access_point_proxy = system_bus_->GetObjectProxy( |
| 227 kNetworkManagerServiceName, access_point_path); | 226 kNetworkManagerServiceName, access_point_path); |
| 228 | 227 |
| 229 AccessPointData access_point_data; | 228 AccessPointData access_point_data; |
| 230 { | 229 { |
| 231 std::unique_ptr<dbus::Response> response( | 230 std::unique_ptr<dbus::Response> response( |
| 232 GetAccessPointProperty(access_point_proxy, "Ssid")); | 231 GetAccessPointProperty(access_point_proxy, "Ssid")); |
| 233 if (!response) | 232 if (!response) |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 338 | 337 |
| 339 // static | 338 // static |
| 340 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { | 339 WifiDataProvider* WifiDataProviderManager::DefaultFactoryFunction() { |
| 341 return new WifiDataProviderLinux(); | 340 return new WifiDataProviderLinux(); |
| 342 } | 341 } |
| 343 | 342 |
| 344 WifiDataProviderLinux::WifiDataProviderLinux() {} | 343 WifiDataProviderLinux::WifiDataProviderLinux() {} |
| 345 | 344 |
| 346 WifiDataProviderLinux::~WifiDataProviderLinux() {} | 345 WifiDataProviderLinux::~WifiDataProviderLinux() {} |
| 347 | 346 |
| 348 WifiDataProviderCommon::WlanApiInterface* WifiDataProviderLinux::NewWlanApi() { | 347 std::unique_ptr<WifiDataProviderCommon::WlanApiInterface> |
| 348 WifiDataProviderLinux::CreateWlanApi() { |
| 349 std::unique_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); | 349 std::unique_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); |
| 350 if (wlan_api->Init()) | 350 if (wlan_api->Init()) |
| 351 return wlan_api.release(); | 351 return std::move(wlan_api); |
| 352 return NULL; | 352 return nullptr; |
| 353 } | 353 } |
| 354 | 354 |
| 355 WifiPollingPolicy* WifiDataProviderLinux::NewPollingPolicy() { | 355 std::unique_ptr<WifiPollingPolicy> |
| 356 return new GenericWifiPollingPolicy<kDefaultPollingIntervalMilliseconds, | 356 WifiDataProviderLinux::CreatePollingPolicy() { |
| 357 kNoChangePollingIntervalMilliseconds, | 357 return base::MakeUnique<GenericWifiPollingPolicy< |
| 358 kTwoNoChangePollingIntervalMilliseconds, | 358 kDefaultPollingIntervalMilliseconds, kNoChangePollingIntervalMilliseconds, |
| 359 kNoWifiPollingIntervalMilliseconds>; | 359 kTwoNoChangePollingIntervalMilliseconds, |
| 360 kNoWifiPollingIntervalMilliseconds>>(); |
| 360 } | 361 } |
| 361 | 362 |
| 362 WifiDataProviderCommon::WlanApiInterface* | 363 std::unique_ptr<WifiDataProviderCommon::WlanApiInterface> |
| 363 WifiDataProviderLinux::NewWlanApiForTesting(dbus::Bus* bus) { | 364 WifiDataProviderLinux::CreateWlanApiForTesting(dbus::Bus* bus) { |
| 364 std::unique_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); | 365 std::unique_ptr<NetworkManagerWlanApi> wlan_api(new NetworkManagerWlanApi); |
| 365 if (wlan_api->InitWithBus(bus)) | 366 if (wlan_api->InitWithBus(bus)) |
| 366 return wlan_api.release(); | 367 return std::move(wlan_api); |
| 367 return NULL; | 368 return nullptr; |
| 369 ; |
| 368 } | 370 } |
| 369 | 371 |
| 370 } // namespace device | 372 } // namespace device |
| OLD | NEW |