Fixed crash in Bluetooth Adapter

PiperOrigin-RevId: 566452194
This commit is contained in:
Guogang Li
2023-09-18 16:57:16 -07:00
committed by Copybara-Service
parent 746a8b6231
commit 9774b5f53c
2 changed files with 20 additions and 12 deletions
@@ -1,4 +1,4 @@
// Copyright 2020 Google LLC
// Copyright 2020-2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -32,7 +32,9 @@
#include <stdio.h>
#include <usbiodef.h>
#include <cstring>
#include <exception>
#include <optional>
#include <string>
#include "absl/strings/str_format.h"
@@ -342,14 +344,15 @@ std::string BluetoothAdapter::GetName() const {
return *device_name_;
}
char *_instance_id = GetGenericBluetoothAdapterInstanceID();
if (_instance_id == nullptr) {
std::optional<std::string> adapter_instance_id =
GetGenericBluetoothAdapterInstanceID();
if (!adapter_instance_id.has_value()) {
NEARBY_LOGS(ERROR)
<< __func__ << ": Failed to get Generic Bluetooth Adapter InstanceID";
return std::string();
}
std::string instance_id(_instance_id);
std::string instance_id = *adapter_instance_id;
// Change radio module local name in registry
HKEY hKey;
@@ -435,13 +438,16 @@ bool BluetoothAdapter::SetName(absl::string_view name, bool persist) {
return true;
}
std::string instance_id(GetGenericBluetoothAdapterInstanceID());
std::optional<std::string> adapter_instance_id =
GetGenericBluetoothAdapterInstanceID();
if (instance_id.empty()) {
if (!adapter_instance_id.has_value()) {
NEARBY_LOGS(ERROR)
<< __func__ << ": Failed to get Generic Bluetooth Adapter InstanceID";
return false;
}
std::string instance_id = *adapter_instance_id;
// defined in usbiodef.h
const GUID guid = GUID_DEVINTERFACE_USB_DEVICE;
@@ -697,7 +703,8 @@ void BluetoothAdapter::find_and_replace(char *source, const char *strFind,
memcpy(source, s.c_str(), s.size());
}
char *BluetoothAdapter::GetGenericBluetoothAdapterInstanceID(void) const {
std::optional<std::string>
BluetoothAdapter::GetGenericBluetoothAdapterInstanceID() const {
unsigned i;
CONFIGRET r;
HDEVINFO hDevInfo;
@@ -715,7 +722,7 @@ char *BluetoothAdapter::GetGenericBluetoothAdapterInstanceID(void) const {
if (hDevInfo == INVALID_HANDLE_VALUE) {
NEARBY_LOGS(ERROR) << __func__
<< ": Could not find BluetoothDevice on this machine";
return NULL;
return std::nullopt;
}
// Get first Generic Bluetooth Adapter InstanceID
@@ -741,14 +748,15 @@ char *BluetoothAdapter::GetGenericBluetoothAdapterInstanceID(void) const {
// 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;
SetupDiDestroyDeviceInfoList(hDevInfo);
return std::string(deviceInstanceID);
}
}
NEARBY_LOGS(ERROR) << __func__
<< ": Failed to get the generic bluetooth adapter id";
return NULL;
SetupDiDestroyDeviceInfoList(hDevInfo);
return std::nullopt;
}
// Returns BT MAC address assigned to this adapter.
@@ -109,7 +109,7 @@ class BluetoothAdapter : public api::BluetoothAdapter {
std::string registry_bluetooth_adapter_name_;
IRadio windows_bluetooth_radio_;
char *GetGenericBluetoothAdapterInstanceID() const;
std::optional<std::string> GetGenericBluetoothAdapterInstanceID() const;
void find_and_replace(char *source, const char *strFind,
const char *strReplace) const;
ScanMode scan_mode_ = ScanMode::kNone;