No changes to public files

PiperOrigin-RevId: 392585608
This commit is contained in:
hai007
2021-08-23 21:55:15 -07:00
committed by Copybara-Service
parent c73504e68d
commit 3e40931286
34 changed files with 2643 additions and 52 deletions
+1
View File
@@ -106,6 +106,7 @@ cc_library(
"platform.cc",
],
visibility = [
"//third_party/nearby_connections/cpp/analytics:__subpackages__",
"//core:__subpackages__",
"//platform:__subpackages__",
],
+2
View File
@@ -50,6 +50,7 @@ cc_library(
name = "comm",
hdrs = [
"ble.h",
"bluetooth_adapter.h",
"bluetooth_classic.h",
"bluetooth_classic_device.h",
"bluetooth_classic_medium.h",
@@ -87,6 +88,7 @@ cc_library(
cc_library(
name = "windows",
srcs = [
"bluetooth_adapter.cc",
"bluetooth_classic_device.cc",
"bluetooth_classic_medium.cc",
"bluetooth_classic_server_socket.cc",
@@ -0,0 +1,320 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "platform/impl/windows/bluetooth_adapter.h"
#include <windows.h>
#include <stdio.h>
#include <winioctl.h>
#include <bthdef.h>
#include <bthioctl.h>
#include <cfgmgr32.h>
#include <devguid.h>
#include <initguid.h>
#include <objbase.h>
#include <setupapi.h>
#include <usbiodef.h>
#include <string>
#include "absl/strings/str_format.h"
#include "platform/impl/windows/generated/winrt/Windows.Foundation.h"
#include "platform/impl/windows/utils.h"
#include "platform/public/logging.h"
typedef std::basic_string<TCHAR> tstring;
// IOCTL to get local radio information
#define BTH_GET_DEVICE_INFO_IOCTL 0x411008
namespace location {
namespace nearby {
namespace windows {
BluetoothAdapter::BluetoothAdapter()
: windows_bluetooth_adapter_(winrt::Windows::Devices::Bluetooth::
BluetoothAdapter::GetDefaultAsync()
.get()) {
// Gets the radio represented by this Bluetooth adapter.
// https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothadapter.getradioasync?view=winrt-20348
windows_bluetooth_radio_ = windows_bluetooth_adapter_.GetRadioAsync().get();
}
// Synchronously sets the status of the BluetoothAdapter to 'status', and
// returns true if the operation was a success.
bool BluetoothAdapter::SetStatus(Status status) {
if (status == Status::kDisabled) {
// An asynchronous operation that attempts to set the state of the radio
// represented by this object.
// https://docs.microsoft.com/en-us/uwp/api/windows.devices.radios.radio.setstateasync?view=winrt-20348
windows_bluetooth_radio_.SetStateAsync(RadioState::Off).get();
} else {
windows_bluetooth_radio_.SetStateAsync(RadioState::On).get();
}
return true;
}
// Returns true if the BluetoothAdapter's current status is
// Status::Value::kEnabled.
bool BluetoothAdapter::IsEnabled() const {
// Gets the current state of the radio represented by this object.
// https://docs.microsoft.com/en-us/uwp/api/windows.devices.radios.radio.state?view=winrt-20348
return windows_bluetooth_radio_.State() == RadioState::On;
}
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getScanMode()
//
// Returns ScanMode::kUnknown on error.
BluetoothAdapter::ScanMode BluetoothAdapter::GetScanMode() const {
return scan_mode_;
}
// Synchronously sets the scan mode of the adapter, and returns true if the
// operation was a success.
// TODO(jcarroll): Setup an event for this and hook the bluetooth medium into
// the event to allow for updates.
bool BluetoothAdapter::SetScanMode(ScanMode scan_mode) {
scan_mode_ = scan_mode;
return true;
}
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getName()
// Returns an empty string on error
// TODO(b/184975123): replace with real implementation.
std::string BluetoothAdapter::GetName() const { return "Un-implemented"; }
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#setName(java.lang.String)
bool BluetoothAdapter::SetName(absl::string_view name) {
char *instanceID = GetGenericBluetoothAdapterInstanceID();
if (instanceID == NULL) {
NEARBY_LOGS(ERROR)
<< __func__ << ": Failed to get Generic Bluetooth Adapter InstanceID";
return false;
}
// Add 1 to length to get size (including null)
char *instanceIDModified = new char[(strlen(instanceID) + 1) * sizeof(char)];
absl::SNPrintF(instanceIDModified,
size_t((strlen(instanceID) + 1) * sizeof(char)), "%s",
instanceID);
find_and_replace(instanceIDModified, "\\", "#");
HANDLE hDevice;
char fileName[1024] = {0};
// defined in usbiodef.h
const GUID guid = GUID_DEVINTERFACE_USB_DEVICE;
OLECHAR guidOleStr[64];
int oleBufferLen = 64;
char guidStr[64];
int bufferLen = 64;
BOOL defaultCharUsed;
// Converts a globally unique identifier (GUID) into a string of printable
// characters.
// https://docs.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-stringfromguid2
auto conversionResult = StringFromGUID2(guid, guidOleStr, bufferLen);
if (conversionResult == 0) {
NEARBY_LOGS(ERROR) << __func__ << ": Failed to convert guid to string";
return false;
}
// Maps a UTF-16 (wide character) string to a new character string. The new
// character string is not necessarily from a multibyte character set.
// https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte
conversionResult =
WideCharToMultiByte(CP_UTF8, 0, guidOleStr, oleBufferLen, guidStr,
bufferLen, NULL, &defaultCharUsed);
if (conversionResult != 0) {
const char *errorResult = {};
switch (conversionResult) {
case ERROR_INSUFFICIENT_BUFFER:
errorResult =
"A supplied buffer size was not large enough, or it was "
"incorrectly set to NULL.";
break;
case ERROR_INVALID_FLAGS:
errorResult = "The values supplied for flags were not valid.";
break;
case ERROR_INVALID_PARAMETER:
errorResult = "Any of the parameter values was invalid.";
break;
case ERROR_NO_UNICODE_TRANSLATION:
errorResult = "Invalid Unicode was found in a string.";
break;
}
NEARBY_LOGS(ERROR) << __func__ << ": Failed to convert guid to string "
<< errorResult;
}
absl::SNPrintF(fileName, sizeof(fileName), "\\\\.\\%s%s#%s", fileName,
instanceIDModified, guidStr);
// Creates or opens a file or I/O device.
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea
hDevice =
CreateFileA(fileName, GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
delete[] instanceIDModified;
if (hDevice == INVALID_HANDLE_VALUE) {
NEARBY_LOGS(ERROR) << __func__ << ": Failed to open device. Error code: "
<< GetLastError();
return false;
}
// Change radio module local name in registry
HKEY hKey;
char rmLocalNameKey[1024] = {0};
LSTATUS ret;
absl::SNPrintF(rmLocalNameKey, sizeof(rmLocalNameKey),
"SYSTEM\\ControlSet001\\Enum\\%s\\Device Parameters",
instanceID);
// Opens the specified registry key. Note that key names are not case
// sensitive.
// https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regopenkeyexa
ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, rmLocalNameKey, 0L, KEY_SET_VALUE,
&hKey);
if (ret != ERROR_SUCCESS) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to open registry key. Error code: " << ret;
return false;
}
// Sets the data and type of a specified value under a registry key.
// https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regsetvalueexa
ret = RegSetValueExA(hKey, "Local Name", 0, REG_BINARY,
(LPBYTE)std::string(name).c_str(),
strlen(std::string(name).c_str()));
if (ret != ERROR_SUCCESS) {
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to set registry key. Error code: " << ret;
return false;
}
// Closes a handle to the specified registry key.
// https://docs.microsoft.com/en-us/windows/win32/api/winreg/nf-winreg-regclosekey
RegCloseKey(hKey);
// tells the control function to reset or reload or similar...
int32 reload = 4;
// merely a placeholder
DWORD bytes = 0;
// Send radio module driver command to update device information
// Sends a control code directly to a specified device driver, causing the
// corresponding device to perform the corresponding operation.
// https://docs.microsoft.com/en-us/windows/win32/api/ioapiset/nf-ioapiset-deviceiocontrol
if (!DeviceIoControl(hDevice, BTH_GET_DEVICE_INFO_IOCTL, &reload, 4, NULL, 0,
&bytes, NULL)) {
NEARBY_LOGS(ERROR)
<< __func__
<< ": Failed to update radio module local name. Error code: "
<< GetLastError();
return false;
}
return true;
}
void BluetoothAdapter::find_and_replace(char *source, const char *strFind,
const char *strReplace) {
std::string s = std::string(source);
std::string f = std::string(strFind);
std::string r = std::string(strReplace);
size_t j;
for (; (j = s.find(f)) != std::string::npos;) {
s.replace(j, f.length(), r);
}
memcpy(source, s.c_str(), s.size());
}
char *BluetoothAdapter::GetGenericBluetoothAdapterInstanceID(void) {
unsigned i;
CONFIGRET r;
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
char *deviceInstanceID = new char[MAX_DEVICE_ID_LEN];
// Find all bluetooth radio modules
// The SetupDiGetClassDevs function returns a handle to a device information
// set that contains requested device information elements for a local
// computer.
// https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdigetclassdevsa
hDevInfo =
SetupDiGetClassDevsA(&GUID_DEVCLASS_BLUETOOTH, NULL, NULL, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE) {
NEARBY_LOGS(ERROR) << __func__
<< ": Could not find BluetoothDevice on this machine";
return NULL;
}
// Get first Generic Bluetooth Adapter InstanceID
for (i = 0;; i++) {
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
// The SetupDiEnumDeviceInfo function returns a SP_DEVINFO_DATA structure
// that specifies a device information element in a device information set.
// https://docs.microsoft.com/en-us/windows/win32/api/setupapi/nf-setupapi-setupdienumdeviceinfo
if (!SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData)) break;
// The CM_Get_Device_ID function retrieves the device instance ID for a
// specified device instance on the local machine.
// https://docs.microsoft.com/en-us/windows/win32/api/cfgmgr32/nf-cfgmgr32-cm_get_device_ida
r = CM_Get_Device_IDA(DeviceInfoData.DevInst, deviceInstanceID,
MAX_DEVICE_ID_LEN, 0);
if (r != CR_SUCCESS) continue;
// With Windows, a Bluetooth radio can be packaged as an external dongle or
// embedded inside a computer but it must be connected to one of the
// computer's USB ports.
// https://docs.microsoft.com/en-us/windows-hardware/drivers/bluetooth/bluetooth-host-radio-support
if (strncmp("USB", deviceInstanceID, 3) == 0) {
return deviceInstanceID;
}
}
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to get the generic bluetooth adapter id";
return NULL;
}
// Returns BT MAC address assigned to this adapter.
std::string BluetoothAdapter::GetMacAddress() const {
return uint64_to_mac_address_string(
windows_bluetooth_adapter_.BluetoothAddress());
}
} // namespace windows
} // namespace nearby
} // namespace location
+34 -14
View File
@@ -18,47 +18,67 @@
#include <string>
#include "platform/api/bluetooth_adapter.h"
#include "platform/impl/windows/generated/winrt/Windows.Devices.Bluetooth.h"
#include "platform/impl/windows/generated/winrt/Windows.Devices.Radios.h"
namespace location {
namespace nearby {
namespace windows {
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
// Represents a Bluetooth adapter.
// https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.bluetoothadapter?view=winrt-20348
using winrt::Windows::Devices::Bluetooth::IBluetoothAdapter;
// Represents a radio device on the system.
// https://docs.microsoft.com/en-us/uwp/api/windows.devices.radios.radio?view=winrt-20348
using winrt::Windows::Devices::Radios::IRadio;
// Enumeration that describes possible radio states.
// https://docs.microsoft.com/en-us/uwp/api/windows.devices.radios.radiostate?view=winrt-20348
using winrt::Windows::Devices::Radios::RadioState;
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html
class BluetoothAdapter : public api::BluetoothAdapter {
public:
BluetoothAdapter();
// TODO(b/184975123): replace with real implementation.
~BluetoothAdapter() override = default;
// Synchronously sets the status of the BluetoothAdapter to 'status', and
// returns true if the operation was a success.
// TODO(b/184975123): replace with real implementation.
bool SetStatus(Status status) override { return false; }
bool SetStatus(Status status) override;
// Returns true if the BluetoothAdapter's current status is
// Status::Value::kEnabled.
// TODO(b/184975123): replace with real implementation.
bool IsEnabled() const override { return false; }
bool IsEnabled() const override;
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getScanMode()
//
// Returns ScanMode::kUnknown on error.
// TODO(b/184975123): replace with real implementation.
ScanMode GetScanMode() const override { return ScanMode::kUnknown; }
ScanMode GetScanMode() const override;
// Synchronously sets the scan mode of the adapter, and returns true if the
// operation was a success.
// TODO(b/184975123): replace with real implementation.
bool SetScanMode(ScanMode scan_mode) override { return false; }
bool SetScanMode(ScanMode scan_mode) override;
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getName()
// Returns an empty string on error
// TODO(b/184975123): replace with real implementation.
std::string GetName() const override { return "Un-implemented"; }
std::string GetName() const override;
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#setName(java.lang.String)
// TODO(b/184975123): replace with real implementation.
bool SetName(absl::string_view name) override { return false; }
bool SetName(absl::string_view name) override;
// Returns BT MAC address assigned to this adapter.
// TODO(b/184975123): replace with real implementation.
std::string GetMacAddress() const override { return "Un-implemented"; }
std::string GetMacAddress() const override;
private:
IBluetoothAdapter
windows_bluetooth_adapter_;
IRadio windows_bluetooth_radio_;
char *GetGenericBluetoothAdapterInstanceID(void);
void find_and_replace(char *source, const char *strFind,
const char *strReplace);
ScanMode scan_mode_ = ScanMode::kNone;
};
} // namespace windows
@@ -21,6 +21,7 @@
#include "platform/base/cancellation_flag.h"
#include "platform/base/cancellation_flag_listener.h"
#include "platform/base/exception.h"
#include "platform/impl/windows/bluetooth_adapter.h"
#include "platform/impl/windows/bluetooth_classic_device.h"
#include "platform/impl/windows/bluetooth_classic_server_socket.h"
#include "platform/impl/windows/bluetooth_classic_socket.h"
@@ -36,7 +37,10 @@ namespace location {
namespace nearby {
namespace windows {
BluetoothClassicMedium::BluetoothClassicMedium() {
BluetoothClassicMedium::BluetoothClassicMedium(
const api::BluetoothAdapter& bluetoothAdapter)
: bluetooth_adapter_(
dynamic_cast<const BluetoothAdapter&>(bluetoothAdapter)) {
InitializeCriticalSection(&critical_section_);
InitializeDeviceWatcher();
@@ -253,7 +257,12 @@ BluetoothClassicMedium::ListenForService(const std::string& service_name,
auto bluetooth_server_socket =
std::make_unique<location::nearby::windows::BluetoothServerSocket>();
bluetooth_server_socket->StartListening(service_name, service_uuid);
bool radioDiscoverable =
bluetooth_adapter_.GetScanMode() ==
BluetoothAdapter::ScanMode::kConnectableDiscoverable;
bluetooth_server_socket->StartListening(service_name, service_uuid,
radioDiscoverable);
return std::move(bluetooth_server_socket);
}
@@ -22,6 +22,7 @@
#include "platform/impl/windows/bluetooth_classic_device.h"
#include "platform/impl/windows/bluetooth_classic_server_socket.h"
#include "platform/impl/windows/bluetooth_classic_socket.h"
#include "platform/impl/windows/bluetooth_adapter.h"
#include "platform/impl/windows/generated/winrt/Windows.Devices.Enumeration.h"
#include "platform/impl/windows/generated/winrt/Windows.Networking.Sockets.h"
#include "platform/impl/windows/generated/winrt/base.h"
@@ -86,7 +87,8 @@ using winrt::Windows::Storage::Streams::DataWriter;
// medium.
class BluetoothClassicMedium : public api::BluetoothClassicMedium {
public:
explicit BluetoothClassicMedium();
BluetoothClassicMedium() = default;
BluetoothClassicMedium(const api::BluetoothAdapter& bluetoothAdapter);
~BluetoothClassicMedium() override;
@@ -176,6 +178,8 @@ class BluetoothClassicMedium : public api::BluetoothClassicMedium {
// CRITICAL_SECTION is a lightweight synchronization mechanism
// https://docs.microsoft.com/en-us/windows/win32/sync/critical-section-objects
CRITICAL_SECTION critical_section_;
BluetoothAdapter bluetooth_adapter_;
};
} // namespace windows
@@ -26,7 +26,7 @@
namespace location {
namespace nearby {
namespace windows {
BluetoothServerSocket::BluetoothServerSocket() {
BluetoothServerSocket::BluetoothServerSocket() : rfcomm_provider_(nullptr) {
InitializeCriticalSection(&critical_section_);
}
@@ -61,8 +61,9 @@ std::unique_ptr<api::BluetoothSocket> BluetoothServerSocket::Accept() {
return nullptr;
}
Exception BluetoothServerSocket::StartListening(
const std::string& service_name, const std::string& service_uuid) {
Exception BluetoothServerSocket::StartListening(const std::string& service_name,
const std::string& service_uuid,
bool radioDiscoverable) {
EnterCriticalSection(&critical_section_);
winrt::guid service(service_uuid);
@@ -121,7 +122,7 @@ Exception BluetoothServerSocket::StartListening(
try {
rfcomm_provider_->StartAdvertising(
stream_socket_listener_.as<StreamSocketListener>(), true);
stream_socket_listener_.as<StreamSocketListener>(), radioDiscoverable);
} catch (std::exception exception) {
// We will log and eat the exception since the caller
// expects nullptr if it fails
@@ -88,7 +88,8 @@ class BluetoothServerSocket : public api::BluetoothServerSocket {
Exception Close() override;
Exception StartListening(const std::string& service_name,
const std::string& service_uuid);
const std::string& service_uuid,
bool radioDiscoverable);
private:
void InitializeServiceSdpAttributes(RfcommServiceProvider rfcommProvider,
@@ -30,6 +30,8 @@ cc_library(
"uuid.lib",
"winmm.lib",
"winspool.lib",
"comsuppwd.lib",
"setupapi.lib",
],
textual_hdrs = glob(["**/*.h"]),
visibility = [
+2 -1
View File
@@ -122,7 +122,8 @@ ImplementationPlatform::CreateBluetoothAdapter() {
std::unique_ptr<BluetoothClassicMedium>
ImplementationPlatform::CreateBluetoothClassicMedium(
BluetoothAdapter& adapter) {
return absl::make_unique<location::nearby::windows::BluetoothClassicMedium>();
return absl::make_unique<location::nearby::windows::BluetoothClassicMedium>(
adapter);
}
// TODO(b/184975123): replace with real implementation.