| Index: chrome/utility/wifi/wifi_service_win.cc
|
| diff --git a/chrome/utility/wifi/wifi_service_win.cc b/chrome/utility/wifi/wifi_service_win.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..ec78122487446d8b72f76ae4240da2bad6c0942b
|
| --- /dev/null
|
| +++ b/chrome/utility/wifi/wifi_service_win.cc
|
| @@ -0,0 +1,805 @@
|
| +// Copyright 2013 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/utility/wifi/wifi_service.h"
|
| +
|
| +#include <iphlpapi.h>
|
| +#include <objbase.h>
|
| +#include <wlanapi.h>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/message_loop/message_loop.h"
|
| +#include "base/strings/string16.h"
|
| +#include "base/strings/string_util.h"
|
| +#include "base/strings/utf_string_conversions.h"
|
| +#include "components/onc/onc_constants.h"
|
| +
|
| +#pragma comment(lib, "wlanapi.lib")
|
| +
|
| +namespace wifi {
|
| +
|
| +// Implementation of WiFiService for Windows.
|
| +class WiFiServiceImpl : public WiFiService, base::NonThreadSafe {
|
| + public:
|
| + WiFiServiceImpl();
|
| + virtual ~WiFiServiceImpl();
|
| +
|
| + // WiFiService interface implementation.
|
| +
|
| + virtual void GetProperties(const std::string& network_guid,
|
| + const NetworkPropertiesCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE;
|
| +
|
| + virtual void GetState(const std::string& network_guid,
|
| + const NetworkPropertiesCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE;
|
| +
|
| + virtual void GetManagedProperties(
|
| + const std::string& network_guid,
|
| + const DictionaryResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE;
|
| +
|
| + virtual void SetProperties(const std::string& network_guid,
|
| + const base::DictionaryValue& properties,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE;
|
| +
|
| + virtual void GetVisibleNetworks(const NetworkListCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE;
|
| +
|
| + virtual void RequestNetworkScan() OVERRIDE;
|
| +
|
| + virtual void StartConnect(const std::string& network_guid,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE;
|
| +
|
| + virtual void StartDisconnect(const std::string& network_guid,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) OVERRIDE;
|
| +
|
| + virtual void SetNetworksChangedObserver(
|
| + const NetworkGuidListCallback& observer) OVERRIDE;
|
| +
|
| + virtual void SetNetworkListChangedObserver(
|
| + const NetworkGuidListCallback& observer) OVERRIDE;
|
| +
|
| + private:
|
| + // Static callback for Windows WLAN_NOTIFICATION. Calls OnWlanNotification
|
| + // on WiFiServiceImpl passed back as |context|.
|
| + static void __stdcall OnWlanNotificationCallback(
|
| + PWLAN_NOTIFICATION_DATA wlan_notification_data,
|
| + PVOID context);
|
| +
|
| + // Callback for Windows WLAN_NOTIFICATION. Called on random thread from
|
| + // OnWlanNotificationCallback. Handles network connectivity and scan complete
|
| + // notification and posts tasks to main thread.
|
| + void OnWlanNotification(PWLAN_NOTIFICATION_DATA wlan_notification_data);
|
| +
|
| + // Handles NetworkScanComplete notification on main thread. Sends
|
| + // |NetworkListChanged| event with new list of visible networks.
|
| + void OnNetworkScanCompleteOnMainThread();
|
| +
|
| + // Wait up to |kMaxAttempts| with |kAttemptDelayMs| delay for connection
|
| + // to network with |network_guid|. Reset DHCP and Notify that |NetworkChanged|
|
| + // upon success.
|
| + void WaitForNetworkConnect(const std::string& network_guid, int attempt);
|
| +
|
| + // Check |error_code| and if is not |ERROR_SUCCESS|, then run |error_callback|
|
| + // with |error_name|.
|
| + bool CheckError(const ErrorCallback& error_callback,
|
| + const std::string& error_name,
|
| + DWORD error_code) const;
|
| +
|
| + // Return |iterator| to network identified by |network_guid| in |networks|
|
| + // list.
|
| + NetworkList::const_iterator FindNetwork(
|
| + const NetworkList& networks,
|
| + const std::string& network_guid) const;
|
| +
|
| + // Save currently connected network profile and return its
|
| + // |connected_network_guid|, so it can be re-connected later.
|
| + DWORD SaveCurrentConnectedNetwork(std::string* connected_network_guid);
|
| +
|
| + // Sort networks, so connected/connecting is up front, then by type:
|
| + // Ethernet, WiFi, Cellular, VPN
|
| + static void SortNetworks(NetworkList* networks);
|
| +
|
| + // Open a WLAN client handle, register for WLAN notifications.
|
| + DWORD OpenClientHandle();
|
| +
|
| + // Reset DHCP on wireless adapter to speed up reconnect after chromecast
|
| + // device reset
|
| + DWORD ResetDHCP();
|
| +
|
| + // Find |adapter_index_map| by |interface_guid| for DHCP reset.
|
| + DWORD FindAdapterIndexMapByGuid(const GUID& interface_guid,
|
| + IP_ADAPTER_INDEX_MAP* adapter_index_map);
|
| +
|
| + // Ensure that |client_| handle is initialized.
|
| + DWORD EnsureInitialized();
|
| +
|
| + // Close |client_| handle if it is open.
|
| + DWORD CloseClientHandle();
|
| +
|
| + // Get |profile_name| from unique |network_guid|.
|
| + base::string16 ProfileNameFromGuid(const std::string& network_guid) const {
|
| + return base::UTF8ToUTF16(network_guid);
|
| + }
|
| +
|
| + // Get |dot11Ssid| from unique |network_guid|.
|
| + DOT11_SSID SsidFromGuid(const std::string& network_guid) const;
|
| +
|
| + // Get unique |network_guid| string based on |dot11Ssid|.
|
| + std::string GuidFromSsid(const DOT11_SSID& dot11Ssid) const {
|
| + return std::string(reinterpret_cast<const char*>(dot11Ssid.ucSSID),
|
| + dot11Ssid.uSSIDLength);
|
| + }
|
| +
|
| + // Get network |ssid| string based on |wlan|.
|
| + std::string SsidFromWlan(const WLAN_AVAILABLE_NETWORK& wlan) const {
|
| + return GuidFromSsid(wlan.dot11Ssid);
|
| + }
|
| +
|
| + // Get unique |network_guid| string based on |wlan|.
|
| + std::string GuidFromWlan(const WLAN_AVAILABLE_NETWORK& wlan) const {
|
| + return SsidFromWlan(wlan);
|
| + }
|
| +
|
| + // Deduce |onc::wifi| security from |alg|.
|
| + std::string SecurityFromDot11AuthAlg(DOT11_AUTH_ALGORITHM alg) const;
|
| +
|
| + // Populate |properties| based on |wlan| and its corresponding bss info from
|
| + // |wlan_bss_list|.
|
| + void NetworkPropertiesFromAvailableNetwork(const WLAN_AVAILABLE_NETWORK& wlan,
|
| + const WLAN_BSS_LIST& wlan_bss_list,
|
| + NetworkProperties* properties);
|
| + // Get the list of visible wireless networks.
|
| + DWORD GetVisibleNetworkList(NetworkList* network_list);
|
| +
|
| + // Find currently connected network if any. Populate |connected_network_guid|
|
| + // on success.
|
| + DWORD FindConnectedNetwork(std::string* connected_network_guid);
|
| +
|
| + // Connect to network |network_guid| using previosly stored profile if exists,
|
| + // or just network sid.
|
| + DWORD Connect(const std::string& network_guid);
|
| +
|
| + // Disconnect from currently connected network if any.
|
| + DWORD Disconnect();
|
| +
|
| + // Save temporary wireless profile for |network_guid|.
|
| + DWORD SaveTempProfile(const std::string& network_guid);
|
| +
|
| + // Get previously stored |profile_xml| for |network_guid|.
|
| + DWORD GetProfile(const std::string& network_guid, std::string* profile_xml);
|
| +
|
| + // Return true if there is previously stored profile xml for |network_guid|.
|
| + bool HaveProfile(const std::string& network_guid);
|
| +
|
| + // Notify |network_list_changed_observer_| that list of visible networks has
|
| + // changed to |networks|.
|
| + void NotifyNetworkListChanged(const NetworkList& networks);
|
| +
|
| + // Notify |networks_changed_observer_| that network |network_guid| status has
|
| + // changed.
|
| + void NotifyNetworkChanged(const std::string& network_guid);
|
| +
|
| + // Wlan Service Handle.
|
| + HANDLE client_;
|
| + // Wlan Interface Guid.
|
| + GUID interface_guid_;
|
| + // Preserved Wlan Profile Xml.
|
| + std::map<std::string, std::string> saved_profiles_xml_;
|
| + // Observer to get notified when network(s) have changed (e.g. connect).
|
| + NetworkGuidListCallback networks_changed_observer_;
|
| + // Observer to get notified when network list has changed (scan complete).
|
| + NetworkGuidListCallback network_list_changed_observer_;
|
| + // Task runner to post tasks on main thread.
|
| + scoped_refptr<base::TaskRunner> task_runner_;
|
| + // If |false|, then |networks_changed_observer_| is not notified.
|
| + bool enable_notify_network_changed_;
|
| + // Number of attempts to check that network has connected successfully.
|
| + static const int kMaxAttempts = 100;
|
| + // Delay between attempts to check that network has connected successfully.
|
| + static const int kAttemptDelayMs = 100;
|
| + // Delay after DHCP Renew to allow IP address to be acquired.
|
| + static const int kDhcpRenewDelayMs = 5000;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(WiFiServiceImpl);
|
| +};
|
| +
|
| +WiFiServiceImpl::WiFiServiceImpl()
|
| + : client_(NULL),
|
| + enable_notify_network_changed_(true) {}
|
| +
|
| +WiFiServiceImpl::~WiFiServiceImpl() { CloseClientHandle(); }
|
| +
|
| +void WiFiServiceImpl::GetProperties(const std::string& network_guid,
|
| + const NetworkPropertiesCallback& callback,
|
| + const ErrorCallback& error_callback) {
|
| + DWORD error = EnsureInitialized();
|
| + if (error == ERROR_SUCCESS) {
|
| + NetworkList network_list;
|
| + error = GetVisibleNetworkList(&network_list);
|
| + if (error == ERROR_SUCCESS && !network_list.empty()) {
|
| + NetworkList::const_iterator it = FindNetwork(network_list, network_guid);
|
| + if (it != network_list.end()) {
|
| + DLOG(INFO) << "Get Properties: "
|
| + << network_guid << ":" << it->connection_state;
|
| + callback.Run(network_guid, *it);
|
| + } else {
|
| + error = ERROR_NOT_FOUND;
|
| + }
|
| + }
|
| + }
|
| +
|
| + CheckError(error_callback, "Error.DBusFailed", error);
|
| +}
|
| +
|
| +void WiFiServiceImpl::GetState(const std::string& network_guid,
|
| + const NetworkPropertiesCallback& callback,
|
| + const ErrorCallback& error_callback) {}
|
| +
|
| +void WiFiServiceImpl::GetManagedProperties(
|
| + const std::string& network_guid,
|
| + const DictionaryResultCallback& callback,
|
| + const ErrorCallback& error_callback) {}
|
| +
|
| +void WiFiServiceImpl::SetProperties(const std::string& network_guid,
|
| + const base::DictionaryValue& properties,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) {}
|
| +
|
| +void WiFiServiceImpl::GetVisibleNetworks(const NetworkListCallback& callback,
|
| + const ErrorCallback& error_callback) {
|
| + DWORD error = EnsureInitialized();
|
| +
|
| + if (error == ERROR_SUCCESS) {
|
| + NetworkList network_list;
|
| + error = GetVisibleNetworkList(&network_list);
|
| + if (error == ERROR_SUCCESS && !network_list.empty()) {
|
| + SortNetworks(&network_list);
|
| + callback.Run(network_list);
|
| + }
|
| + }
|
| +
|
| + CheckError(error_callback, "Error.DBusFailed", error);
|
| +}
|
| +
|
| +void WiFiServiceImpl::RequestNetworkScan() {
|
| + DWORD error = EnsureInitialized();
|
| + if (error == ERROR_SUCCESS) {
|
| + WlanScan(client_, &interface_guid_, NULL, NULL, NULL);
|
| + }
|
| +}
|
| +
|
| +void WiFiServiceImpl::StartConnect(const std::string& network_guid,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) {
|
| + DLOG(INFO) << "Start Connect: " << network_guid;
|
| + DWORD error = EnsureInitialized();
|
| + if (error == ERROR_SUCCESS) {
|
| + std::string connected_network_guid;
|
| + error = SaveCurrentConnectedNetwork(&connected_network_guid);
|
| + if (error == ERROR_SUCCESS) {
|
| + // Connect only if network |network_guid| is not connected already.
|
| + if (network_guid != connected_network_guid)
|
| + error = Connect(network_guid);
|
| + if (error == ERROR_SUCCESS) {
|
| + callback.Run(network_guid);
|
| + // Notify that previously connected network has changed.
|
| + NotifyNetworkChanged(connected_network_guid);
|
| + // Start waiting for network connection state change.
|
| + if (!networks_changed_observer_.is_null()) {
|
| + // Disable automatic network change notifications.
|
| + enable_notify_network_changed_ = false;
|
| + WaitForNetworkConnect(network_guid, 0);
|
| + }
|
| + }
|
| + }
|
| + }
|
| + CheckError(error_callback, "Error.DBusFailed", error);
|
| +}
|
| +
|
| +void WiFiServiceImpl::StartDisconnect(const std::string& network_guid,
|
| + const StringResultCallback& callback,
|
| + const ErrorCallback& error_callback) {
|
| + DLOG(INFO) << "Start Disconnect: " << network_guid;
|
| + DWORD error = EnsureInitialized();
|
| + if (error == ERROR_SUCCESS) {
|
| + std::string connected_network_guid;
|
| + error = SaveCurrentConnectedNetwork(&connected_network_guid);
|
| + if (error == ERROR_SUCCESS && network_guid == connected_network_guid) {
|
| + error = Disconnect();
|
| + if (error == ERROR_SUCCESS) {
|
| + NotifyNetworkChanged(network_guid);
|
| + callback.Run(network_guid);
|
| + }
|
| + }
|
| + }
|
| + CheckError(error_callback, "Error.DBusFailed", error);
|
| +}
|
| +
|
| +void WiFiServiceImpl::SetNetworksChangedObserver(
|
| + const NetworkGuidListCallback& observer) {
|
| + networks_changed_observer_ = observer;
|
| +}
|
| +
|
| +void WiFiServiceImpl::SetNetworkListChangedObserver(
|
| + const NetworkGuidListCallback& observer) {
|
| + network_list_changed_observer_ = observer;
|
| +}
|
| +
|
| +void WiFiServiceImpl::OnWlanNotificationCallback(
|
| + PWLAN_NOTIFICATION_DATA wlan_notification_data,
|
| + PVOID context) {
|
| + WiFiServiceImpl* service = reinterpret_cast<WiFiServiceImpl*>(context);
|
| + service->OnWlanNotification(wlan_notification_data);
|
| +}
|
| +
|
| +void WiFiServiceImpl::OnWlanNotification(
|
| + PWLAN_NOTIFICATION_DATA wlan_notification_data) {
|
| + PWLAN_CONNECTION_NOTIFICATION_DATA wlan_connection_data =
|
| + reinterpret_cast<PWLAN_CONNECTION_NOTIFICATION_DATA>(
|
| + wlan_notification_data->pData);
|
| +
|
| + switch (wlan_notification_data->NotificationCode) {
|
| + case wlan_notification_acm_disconnected:
|
| + case wlan_notification_acm_connection_complete:
|
| + case wlan_notification_acm_connection_attempt_fail:
|
| + task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&WiFiServiceImpl::NotifyNetworkChanged,
|
| + base::Unretained(this),
|
| + GuidFromSsid(wlan_connection_data->dot11Ssid)));
|
| + break;
|
| + case wlan_notification_acm_scan_complete:
|
| + task_runner_->PostTask(
|
| + FROM_HERE,
|
| + base::Bind(&WiFiServiceImpl::OnNetworkScanCompleteOnMainThread,
|
| + base::Unretained(this)));
|
| + break;
|
| + }
|
| +}
|
| +
|
| +void WiFiServiceImpl::OnNetworkScanCompleteOnMainThread() {
|
| + NetworkList networks;
|
| + DWORD error = GetVisibleNetworkList(&networks);
|
| + if (error == ERROR_SUCCESS)
|
| + NotifyNetworkListChanged(networks);
|
| +}
|
| +
|
| +void WiFiServiceImpl::WaitForNetworkConnect(const std::string& network_guid,
|
| + int attempt) {
|
| + if (attempt > kMaxAttempts) {
|
| + enable_notify_network_changed_ = true;
|
| + return;
|
| + }
|
| + std::string connected_network_guid;
|
| + DWORD error = FindConnectedNetwork(&connected_network_guid);
|
| + if (network_guid == connected_network_guid) {
|
| + DLOG(INFO) << "WiFi Connected, Reset DHCP: " << network_guid;
|
| + enable_notify_network_changed_ = true;
|
| + // Reset DHCP to speed up the connection after Chromekey factory reset.
|
| + error = ResetDHCP();
|
| + if (error == ERROR_SUCCESS) {
|
| + base::MessageLoop::current()->PostDelayedTask(
|
| + FROM_HERE,
|
| + base::Bind(&WiFiServiceImpl::NotifyNetworkChanged,
|
| + base::Unretained(this),
|
| + network_guid),
|
| + base::TimeDelta::FromMilliseconds(kDhcpRenewDelayMs));
|
| + } else {
|
| + NotifyNetworkChanged(network_guid);
|
| + }
|
| + } else {
|
| + // Continue waiting for network connection state change.
|
| + base::MessageLoop::current()->PostDelayedTask(
|
| + FROM_HERE,
|
| + base::Bind(&WiFiServiceImpl::WaitForNetworkConnect,
|
| + base::Unretained(this),
|
| + network_guid,
|
| + ++attempt),
|
| + base::TimeDelta::FromMilliseconds(kAttemptDelayMs));
|
| + }
|
| +}
|
| +
|
| +bool WiFiServiceImpl::CheckError(const ErrorCallback& error_callback,
|
| + const std::string& error_name,
|
| + DWORD error_code) const {
|
| + if (error_code != ERROR_SUCCESS) {
|
| + DLOG(ERROR) << "WiFiService Error " << error_code << ": " << error_name;
|
| + scoped_ptr<base::DictionaryValue> error_data(new base::DictionaryValue);
|
| + error_data->SetInteger("Win32ErrorCode", error_code);
|
| + error_callback.Run(error_name, error_data.Pass());
|
| + return true;
|
| + }
|
| + return false;
|
| +}
|
| +
|
| +WiFiService::NetworkList::const_iterator WiFiServiceImpl::FindNetwork(
|
| + const WiFiService::NetworkList& networks,
|
| + const std::string& network_guid) const {
|
| + for (NetworkList::const_iterator it = networks.begin(); it != networks.end();
|
| + ++it) {
|
| + if (it->guid == network_guid)
|
| + return it;
|
| + }
|
| + return networks.end();
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::SaveCurrentConnectedNetwork(
|
| + std::string* connected_network_guid) {
|
| + // Find currently connected network.
|
| + DWORD error = FindConnectedNetwork(connected_network_guid);
|
| + if (error == ERROR_SUCCESS && !connected_network_guid->empty()) {
|
| + if (error == ERROR_SUCCESS) {
|
| + SaveTempProfile(*connected_network_guid);
|
| + std::string profile_xml;
|
| + error = GetProfile(*connected_network_guid, &profile_xml);
|
| + if (error == ERROR_SUCCESS) {
|
| + saved_profiles_xml_[*connected_network_guid] = profile_xml;
|
| + }
|
| + }
|
| + }
|
| + return error;
|
| +}
|
| +
|
| +void WiFiServiceImpl::SortNetworks(NetworkList* networks) {
|
| + networks->sort(WiFiService::NetworkProperties::OrderByType);
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::OpenClientHandle() {
|
| + CloseClientHandle();
|
| +
|
| + DWORD error = ERROR_SUCCESS;
|
| + DWORD service_version = 0;
|
| +
|
| + // Open a handle to the service.
|
| + error = WlanOpenHandle(WLAN_API_VERSION, NULL, &service_version, &client_);
|
| +
|
| + PWLAN_INTERFACE_INFO_LIST pIntfList = NULL;
|
| + if (error == ERROR_SUCCESS) {
|
| + // Enumerate wireless interfaces.
|
| + error = WlanEnumInterfaces(client_, NULL, &pIntfList);
|
| + if (error == ERROR_SUCCESS) {
|
| + if (pIntfList != NULL && pIntfList->dwNumberOfItems != 0) {
|
| + // Remember first interface.
|
| + interface_guid_ = pIntfList->InterfaceInfo[0].InterfaceGuid;
|
| + // Try to find connected interface.
|
| + for (DWORD itf = 0; itf < pIntfList->dwNumberOfItems; ++itf) {
|
| + if (pIntfList->InterfaceInfo[itf].isState ==
|
| + wlan_interface_state_connected) {
|
| + // Found connected interface, remember it!
|
| + interface_guid_ = pIntfList->InterfaceInfo[itf].InterfaceGuid;
|
| + break;
|
| + }
|
| + }
|
| + WlanRegisterNotification(client_,
|
| + WLAN_NOTIFICATION_SOURCE_ALL,
|
| + FALSE,
|
| + OnWlanNotificationCallback,
|
| + this,
|
| + NULL,
|
| + NULL);
|
| + } else {
|
| + error = ERROR_NOINTERFACE;
|
| + }
|
| + }
|
| + // Clean up.
|
| + if (pIntfList != NULL)
|
| + WlanFreeMemory(pIntfList);
|
| + }
|
| + return error;
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::ResetDHCP() {
|
| + IP_ADAPTER_INDEX_MAP adapter_index_map = {0};
|
| + DWORD error = FindAdapterIndexMapByGuid(interface_guid_, &adapter_index_map);
|
| + if (error == ERROR_SUCCESS) {
|
| + error = IpReleaseAddress(&adapter_index_map);
|
| + if (error == ERROR_SUCCESS) {
|
| + error = IpRenewAddress(&adapter_index_map);
|
| + }
|
| + }
|
| + return error;
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::FindAdapterIndexMapByGuid(
|
| + const GUID& interface_guid,
|
| + IP_ADAPTER_INDEX_MAP* adapter_index_map) {
|
| + string16 guid_string;
|
| + const int kGUIDSize = 39;
|
| + ::StringFromGUID2(
|
| + interface_guid, WriteInto(&guid_string, kGUIDSize), kGUIDSize);
|
| +
|
| + ULONG buffer_length = 0;
|
| + DWORD error = GetInterfaceInfo(NULL, &buffer_length);
|
| + if (error == ERROR_INSUFFICIENT_BUFFER) {
|
| + scoped_ptr<unsigned char[]> buffer(new unsigned char[buffer_length]);
|
| + IP_INTERFACE_INFO* interface_info =
|
| + reinterpret_cast<IP_INTERFACE_INFO*>(buffer.get());
|
| + error = GetInterfaceInfo(interface_info, &buffer_length);
|
| + if (error == ERROR_SUCCESS) {
|
| + for (int adapter = 0; adapter < interface_info->NumAdapters; ++adapter) {
|
| + if (EndsWith(
|
| + interface_info->Adapter[adapter].Name, guid_string, false)) {
|
| + *adapter_index_map = interface_info->Adapter[adapter];
|
| + break;
|
| + }
|
| + }
|
| + }
|
| + }
|
| + return error;
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::EnsureInitialized() {
|
| + DCHECK(CalledOnValidThread());
|
| + if (task_runner_.get() == NULL)
|
| + task_runner_ = base::MessageLoopProxy::current();
|
| +
|
| + if (client_ != NULL)
|
| + return ERROR_SUCCESS;
|
| + return OpenClientHandle();
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::CloseClientHandle() {
|
| + DWORD error = ERROR_SUCCESS;
|
| + if (client_ != NULL) {
|
| + WlanCloseHandle(client_, NULL);
|
| + client_ = NULL;
|
| + }
|
| + return error;
|
| +}
|
| +
|
| +DOT11_SSID WiFiServiceImpl::SsidFromGuid(
|
| + const std::string& network_guid) const {
|
| + DOT11_SSID ssid = {0};
|
| + if (network_guid.length() <= DOT11_SSID_MAX_LENGTH) {
|
| + ssid.uSSIDLength = network_guid.length();
|
| + strncpy(reinterpret_cast<char*>(ssid.ucSSID),
|
| + network_guid.c_str(),
|
| + ssid.uSSIDLength);
|
| + }
|
| + return ssid;
|
| +}
|
| +
|
| +std::string WiFiServiceImpl::SecurityFromDot11AuthAlg(
|
| + DOT11_AUTH_ALGORITHM alg) const {
|
| + // TODO(mef): Figure out correct mapping.
|
| + switch (alg) {
|
| + case DOT11_AUTH_ALGO_RSNA:
|
| + return onc::wifi::kWPA_EAP;
|
| + case DOT11_AUTH_ALGO_RSNA_PSK:
|
| + return onc::wifi::kWPA_PSK;
|
| + case DOT11_AUTH_ALGO_80211_SHARED_KEY:
|
| + return onc::wifi::kWEP_PSK;
|
| + case DOT11_AUTH_ALGO_80211_OPEN:
|
| + return onc::wifi::kNone;
|
| + default:
|
| + return onc::wifi::kWPA_EAP;
|
| + }
|
| + return onc::wifi::kWPA_EAP;
|
| +}
|
| +
|
| +void WiFiServiceImpl::NetworkPropertiesFromAvailableNetwork(
|
| + const WLAN_AVAILABLE_NETWORK& wlan,
|
| + const WLAN_BSS_LIST& wlan_bss_list,
|
| + NetworkProperties* properties) {
|
| + if (wlan.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) {
|
| + properties->connection_state = onc::connection_state::kConnected;
|
| + } else {
|
| + properties->connection_state = onc::connection_state::kNotConnected;
|
| + }
|
| +
|
| + properties->ssid = SsidFromWlan(wlan);
|
| + properties->name = properties->ssid;
|
| + properties->guid = GuidFromWlan(wlan);
|
| + properties->type = onc::network_type::kWiFi;
|
| +
|
| + for (size_t bss = 0; bss < wlan_bss_list.dwNumberOfItems; ++bss) {
|
| + const WLAN_BSS_ENTRY& bss_entry(wlan_bss_list.wlanBssEntries[bss]);
|
| + if (bss_entry.dot11Ssid.uSSIDLength == wlan.dot11Ssid.uSSIDLength &&
|
| + 0 == memcmp(bss_entry.dot11Ssid.ucSSID,
|
| + wlan.dot11Ssid.ucSSID,
|
| + bss_entry.dot11Ssid.uSSIDLength)) {
|
| + if (bss_entry.ulChCenterFrequency < 3000000)
|
| + properties->frequency = kFrequency2400;
|
| + else
|
| + properties->frequency = kFrequency5000;
|
| + properties->frequency_list.push_back(properties->frequency);
|
| + properties->bssid = WiFiService::NetworkProperties::MacAddressAsString(
|
| + bss_entry.dot11Bssid);
|
| + }
|
| + }
|
| + properties->frequency_list.sort();
|
| + properties->frequency_list.unique();
|
| + properties->security =
|
| + SecurityFromDot11AuthAlg(wlan.dot11DefaultAuthAlgorithm);
|
| + properties->signal_strength = wlan.wlanSignalQuality;
|
| +}
|
| +
|
| +// Get the list of visible wireless networks
|
| +DWORD WiFiServiceImpl::GetVisibleNetworkList(NetworkList* network_list) {
|
| + DWORD error = ERROR_SUCCESS;
|
| +
|
| + if (client_ == NULL) {
|
| + return ERROR_NOINTERFACE;
|
| + }
|
| +
|
| + PWLAN_AVAILABLE_NETWORK_LIST pVList = NULL;
|
| + PWLAN_BSS_LIST pWlanBssList = NULL;
|
| +
|
| + error =
|
| + WlanGetAvailableNetworkList(client_, &interface_guid_, 0, NULL, &pVList);
|
| +
|
| + if (error == ERROR_SUCCESS && NULL != pVList) {
|
| + error = WlanGetNetworkBssList(client_,
|
| + &interface_guid_,
|
| + NULL,
|
| + dot11_BSS_type_any,
|
| + FALSE,
|
| + NULL,
|
| + &pWlanBssList);
|
| + if (error == ERROR_SUCCESS && NULL != pWlanBssList) {
|
| + for (DWORD i = 0; i < pVList->dwNumberOfItems; ++i) {
|
| + network_list->push_back(NetworkProperties());
|
| + NetworkPropertiesFromAvailableNetwork(
|
| + pVList->Network[i], *pWlanBssList, &network_list->back());
|
| + }
|
| + }
|
| + }
|
| +
|
| + // clean up
|
| + if (pVList != NULL) {
|
| + WlanFreeMemory(pVList);
|
| + }
|
| + if (pWlanBssList != NULL) {
|
| + WlanFreeMemory(pWlanBssList);
|
| + }
|
| + return error;
|
| +}
|
| +
|
| +// Find currently connected network.
|
| +DWORD WiFiServiceImpl::FindConnectedNetwork(
|
| + std::string* connected_network_guid) {
|
| + DWORD error = ERROR_SUCCESS;
|
| +
|
| + if (client_ == NULL) {
|
| + return ERROR_NOINTERFACE;
|
| + }
|
| +
|
| + PWLAN_AVAILABLE_NETWORK_LIST pVList = NULL;
|
| +
|
| + error =
|
| + WlanGetAvailableNetworkList(client_, &interface_guid_, 0, NULL, &pVList);
|
| +
|
| + if (error == ERROR_SUCCESS && NULL != pVList) {
|
| + for (DWORD i = 0; i < pVList->dwNumberOfItems; ++i) {
|
| + const WLAN_AVAILABLE_NETWORK& wlan = pVList->Network[i];
|
| + if (wlan.dwFlags & WLAN_AVAILABLE_NETWORK_CONNECTED) {
|
| + *connected_network_guid = GuidFromWlan(wlan);
|
| + break;
|
| + }
|
| + }
|
| + }
|
| +
|
| + // clean up
|
| + if (pVList != NULL) {
|
| + WlanFreeMemory(pVList);
|
| + }
|
| +
|
| + return error;
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::Connect(const std::string& network_guid) {
|
| + DWORD error = ERROR_SUCCESS;
|
| +
|
| + if (client_ == NULL) {
|
| + return ERROR_NOINTERFACE;
|
| + }
|
| +
|
| + base::string16 profile_name = ProfileNameFromGuid(network_guid);
|
| +
|
| + if (HaveProfile(network_guid)) {
|
| + WLAN_CONNECTION_PARAMETERS wlan_params = {
|
| + wlan_connection_mode_profile, profile_name.c_str(), NULL,
|
| + NULL, dot11_BSS_type_any, 0};
|
| + error = ::WlanConnect(client_, &interface_guid_, &wlan_params, NULL);
|
| + } else {
|
| + DOT11_SSID ssid = SsidFromGuid(network_guid);
|
| + WLAN_CONNECTION_PARAMETERS wlan_params = {
|
| + wlan_connection_mode_discovery_unsecure, NULL, &ssid, NULL,
|
| + dot11_BSS_type_infrastructure, 0};
|
| + error = ::WlanConnect(client_, &interface_guid_, &wlan_params, NULL);
|
| + }
|
| +
|
| + return error;
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::Disconnect() {
|
| + DWORD error = ERROR_SUCCESS;
|
| +
|
| + if (client_ == NULL) {
|
| + return ERROR_NOINTERFACE;
|
| + }
|
| +
|
| + error = ::WlanDisconnect(client_, &interface_guid_, NULL);
|
| + return error;
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::SaveTempProfile(const std::string& network_guid) {
|
| + DWORD error = ERROR_SUCCESS;
|
| +
|
| + if (client_ == NULL) {
|
| + return ERROR_NOINTERFACE;
|
| + }
|
| +
|
| + base::string16 profile_name = ProfileNameFromGuid(network_guid);
|
| +
|
| + error = ::WlanSaveTemporaryProfile(
|
| + client_, &interface_guid_, profile_name.c_str(), NULL, 0, true, NULL);
|
| + return error;
|
| +}
|
| +
|
| +DWORD WiFiServiceImpl::GetProfile(const std::string& network_guid,
|
| + std::string* profile_xml) {
|
| + DWORD error = ERROR_SUCCESS;
|
| +
|
| + if (client_ == NULL) {
|
| + return ERROR_NOINTERFACE;
|
| + }
|
| +
|
| + base::string16 profile_name = ProfileNameFromGuid(network_guid);
|
| + LPWSTR str_profile_xml = NULL;
|
| + error = ::WlanGetProfile(client_,
|
| + &interface_guid_,
|
| + profile_name.c_str(),
|
| + NULL,
|
| + &str_profile_xml,
|
| + NULL,
|
| + NULL);
|
| +
|
| + if (error == ERROR_SUCCESS && str_profile_xml != NULL) {
|
| + *profile_xml = base::UTF16ToUTF8(str_profile_xml);
|
| + }
|
| + // clean up
|
| + if (str_profile_xml != NULL) {
|
| + WlanFreeMemory(str_profile_xml);
|
| + }
|
| +
|
| + return error;
|
| +}
|
| +
|
| +bool WiFiServiceImpl::HaveProfile(const std::string& network_guid) {
|
| + DWORD error = ERROR_SUCCESS;
|
| + std::string profile_xml;
|
| + return GetProfile(network_guid, &profile_xml) == ERROR_SUCCESS;
|
| +}
|
| +
|
| +void WiFiServiceImpl::NotifyNetworkListChanged(const NetworkList& networks) {
|
| + if (network_list_changed_observer_.is_null())
|
| + return;
|
| +
|
| + WiFiService::NetworkGuidList current_networks;
|
| + for (WiFiService::NetworkList::const_iterator it = networks.begin();
|
| + it != networks.end();
|
| + ++it) {
|
| + current_networks.push_back(it->guid);
|
| + }
|
| + network_list_changed_observer_.Run(current_networks);
|
| +}
|
| +
|
| +void WiFiServiceImpl::NotifyNetworkChanged(const std::string& network_guid) {
|
| + if (enable_notify_network_changed_ && !networks_changed_observer_.is_null()) {
|
| + DLOG(INFO) << "NotifyNetworkChanged: " << network_guid;
|
| + WiFiService::NetworkGuidList changed_networks(1, network_guid);
|
| + networks_changed_observer_.Run(changed_networks);
|
| + }
|
| +}
|
| +
|
| +WiFiService* WiFiService::CreateService() { return new WiFiServiceImpl(); }
|
| +
|
| +} // namespace wifi
|
|
|