summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoryoungki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-25 17:46:36 +0000
committeryoungki@chromium.org <youngki@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-02-25 17:46:36 +0000
commitedcaf14a04182efb5785c99f8b25567a3f9ed667 (patch)
treec4a44f5ef4af3804b329a13d5052ff6f87a456fa
parentb6a0d51b5c2e17b78beb56c359a51a4a6bd8934f (diff)
downloadchromium_src-edcaf14a04182efb5785c99f8b25567a3f9ed667.zip
chromium_src-edcaf14a04182efb5785c99f8b25567a3f9ed667.tar.gz
chromium_src-edcaf14a04182efb5785c99f8b25567a3f9ed667.tar.bz2
Implemented BluetoothDeviceWin.
BUG=135470 Review URL: https://chromiumcodereview.appspot.com/12217157 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@184432 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_api.cc11
-rw-r--r--device/bluetooth/bluetooth_device.cc21
-rw-r--r--device/bluetooth/bluetooth_device.h12
-rw-r--r--device/bluetooth/bluetooth_device_chromeos.cc12
-rw-r--r--device/bluetooth/bluetooth_device_chromeos.h1
-rw-r--r--device/bluetooth/bluetooth_device_win.cc37
-rw-r--r--device/bluetooth/bluetooth_device_win.h3
-rw-r--r--device/bluetooth/bluetooth_device_win_unittest.cc126
-rw-r--r--device/bluetooth/bluetooth_init_win.h1
-rw-r--r--device/bluetooth/bluetooth_service_record_win_unittest.cc4
-rw-r--r--device/bluetooth/bluetooth_socket_win.cc1
-rw-r--r--device/device.gyp1
12 files changed, 191 insertions, 39 deletions
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
index 877dcc4..bbe929b 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
@@ -163,8 +163,8 @@ bool BluetoothGetDevicesFunction::DoWork(
std::string uuid;
if (options.uuid.get() != NULL) {
- uuid = device::bluetooth_utils::CanonicalUuid(*options.uuid.get());
- if (uuid.empty()) {
+ uuid = *options.uuid.get();
+ if (!BluetoothDevice::IsUUIDValid(uuid)) {
SetError(kInvalidUuid);
SendResponse(false);
return false;
@@ -284,9 +284,7 @@ bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) {
return false;
}
- std::string uuid = device::bluetooth_utils::CanonicalUuid(
- options.service_uuid);
- if (uuid.empty()) {
+ if (!BluetoothDevice::IsUUIDValid(options.service_uuid)) {
SetError(kInvalidUuid);
SendResponse(false);
return false;
@@ -299,6 +297,9 @@ bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) {
return false;
}
+ std::string uuid = device::bluetooth_utils::CanonicalUuid(
+ options.service_uuid);
+
device->ConnectToService(uuid,
base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback,
this,
diff --git a/device/bluetooth/bluetooth_device.cc b/device/bluetooth/bluetooth_device.cc
index 05a1bdc..5bad91a 100644
--- a/device/bluetooth/bluetooth_device.cc
+++ b/device/bluetooth/bluetooth_device.cc
@@ -4,12 +4,20 @@
#include "device/bluetooth/bluetooth_device.h"
+#include <string>
+
#include "base/utf_string_conversions.h"
+#include "device/bluetooth/bluetooth_utils.h"
#include "grit/device_bluetooth_strings.h"
#include "ui/base/l10n/l10n_util.h"
namespace device {
+// static
+bool BluetoothDevice::IsUUIDValid(const std::string& uuid) {
+ return !bluetooth_utils::CanonicalUuid(uuid).empty();
+}
+
BluetoothDevice::BluetoothDevice()
: bluetooth_class_(0),
visible_(false),
@@ -172,4 +180,17 @@ bool BluetoothDevice::IsConnectable() const {
return connectable_;
}
+bool BluetoothDevice::ProvidesServiceWithUUID(
+ const std::string& uuid) const {
+ std::string canonical_uuid = bluetooth_utils::CanonicalUuid(uuid);
+ const BluetoothDevice::ServiceList& services = GetServices();
+ for (BluetoothDevice::ServiceList::const_iterator iter = services.begin();
+ iter != services.end();
+ ++iter) {
+ if (bluetooth_utils::CanonicalUuid(*iter) == canonical_uuid)
+ return true;
+ }
+ return false;
+}
+
} // namespace device
diff --git a/device/bluetooth/bluetooth_device.h b/device/bluetooth/bluetooth_device.h
index 2053688..e07806c 100644
--- a/device/bluetooth/bluetooth_device.h
+++ b/device/bluetooth/bluetooth_device.h
@@ -145,6 +145,10 @@ class BluetoothDevice {
virtual void DismissDisplayOrConfirm() = 0;
};
+ // Returns true if uuid is in a a valid canonical format
+ // (see utils::CanonicalUuid).
+ static bool IsUUIDValid(const std::string& uuid);
+
virtual ~BluetoothDevice();
// Returns the Bluetooth of address the device. This should be used as
@@ -203,9 +207,8 @@ class BluetoothDevice {
virtual void GetServiceRecords(const ServiceRecordsCallback& callback,
const ErrorCallback& error_callback) = 0;
- // Indicates whether this device provides the given service. |uuid| should
- // be in canonical form (see utils::CanonicalUuid).
- virtual bool ProvidesServiceWithUUID(const std::string& uuid) const = 0;
+ // Indicates whether this device provides the given service.
+ virtual bool ProvidesServiceWithUUID(const std::string& uuid) const;
// The ProvidesServiceCallback is used by ProvidesServiceWithName to indicate
// whether or not a matching service was found.
@@ -338,6 +341,9 @@ class BluetoothDevice {
// the adapter once paired.
bool connectable_;
+ // The services (identified by UUIDs) that this device provides.
+ ServiceList service_uuids_;
+
private:
// Returns a localized string containing the device's bluetooth address and
// a device type for display when |name_| is empty.
diff --git a/device/bluetooth/bluetooth_device_chromeos.cc b/device/bluetooth/bluetooth_device_chromeos.cc
index c960820..6667d95 100644
--- a/device/bluetooth/bluetooth_device_chromeos.cc
+++ b/device/bluetooth/bluetooth_device_chromeos.cc
@@ -79,18 +79,6 @@ void BluetoothDeviceChromeOS::GetServiceRecords(
error_callback)));
}
-bool BluetoothDeviceChromeOS::ProvidesServiceWithUUID(
- const std::string& uuid) const {
- const BluetoothDevice::ServiceList& services = GetServices();
- for (BluetoothDevice::ServiceList::const_iterator iter = services.begin();
- iter != services.end();
- ++iter) {
- if (device::bluetooth_utils::CanonicalUuid(*iter) == uuid)
- return true;
- }
- return false;
-}
-
void BluetoothDeviceChromeOS::ProvidesServiceWithName(
const std::string& name,
const ProvidesServiceCallback& callback) {
diff --git a/device/bluetooth/bluetooth_device_chromeos.h b/device/bluetooth/bluetooth_device_chromeos.h
index 16da0e7..d93dfdc 100644
--- a/device/bluetooth/bluetooth_device_chromeos.h
+++ b/device/bluetooth/bluetooth_device_chromeos.h
@@ -45,7 +45,6 @@ class BluetoothDeviceChromeOS
virtual void GetServiceRecords(
const ServiceRecordsCallback& callback,
const ErrorCallback& error_callback) OVERRIDE;
- virtual bool ProvidesServiceWithUUID(const std::string& uuid) const OVERRIDE;
virtual void ProvidesServiceWithName(
const std::string& name,
const ProvidesServiceCallback& callback) OVERRIDE;
diff --git a/device/bluetooth/bluetooth_device_win.cc b/device/bluetooth/bluetooth_device_win.cc
index a083f09..c06693f 100644
--- a/device/bluetooth/bluetooth_device_win.cc
+++ b/device/bluetooth/bluetooth_device_win.cc
@@ -1,8 +1,6 @@
// Copyright (c) 2012 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.
-//
-// TODO(youngki): Implement this file.
#include "device/bluetooth/bluetooth_device_win.h"
@@ -15,6 +13,8 @@
#include "base/stringprintf.h"
#include "device/bluetooth/bluetooth_out_of_band_pairing_data.h"
#include "device/bluetooth/bluetooth_service_record_win.h"
+#include "device/bluetooth/bluetooth_socket_win.h"
+#include "device/bluetooth/bluetooth_task_manager_win.h"
namespace {
@@ -63,26 +63,27 @@ bool BluetoothDeviceWin::IsPaired() const {
}
const BluetoothDevice::ServiceList& BluetoothDeviceWin::GetServices() const {
- NOTIMPLEMENTED();
return service_uuids_;
}
void BluetoothDeviceWin::GetServiceRecords(
const ServiceRecordsCallback& callback,
const ErrorCallback& error_callback) {
- NOTIMPLEMENTED();
-}
-
-bool BluetoothDeviceWin::ProvidesServiceWithUUID(
- const std::string& uuid) const {
- NOTIMPLEMENTED();
- return false;
+ callback.Run(service_record_list_);
}
void BluetoothDeviceWin::ProvidesServiceWithName(
const std::string& name,
const ProvidesServiceCallback& callback) {
- NOTIMPLEMENTED();
+ for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
+ iter != service_record_list_.end();
+ ++iter) {
+ if ((*iter)->name() == name) {
+ callback.Run(true);
+ return;
+ }
+ }
+ callback.Run(false);
}
bool BluetoothDeviceWin::ExpectingPinCode() const {
@@ -140,7 +141,19 @@ void BluetoothDeviceWin::Forget(const ErrorCallback& error_callback) {
void BluetoothDeviceWin::ConnectToService(
const std::string& service_uuid,
const SocketCallback& callback) {
- NOTIMPLEMENTED();
+ for (ServiceRecordList::const_iterator iter = service_record_list_.begin();
+ iter != service_record_list_.end();
+ ++iter) {
+ if ((*iter)->uuid() == service_uuid) {
+ // If multiple service records are found, use the first one that works.
+ scoped_refptr<BluetoothSocket> socket(
+ BluetoothSocketWin::CreateBluetoothSocket(**iter));
+ if (socket.get() != NULL) {
+ callback.Run(socket);
+ return;
+ }
+ }
+ }
}
void BluetoothDeviceWin::SetOutOfBandPairingData(
diff --git a/device/bluetooth/bluetooth_device_win.h b/device/bluetooth/bluetooth_device_win.h
index 12c3a3f..9e56bc0 100644
--- a/device/bluetooth/bluetooth_device_win.h
+++ b/device/bluetooth/bluetooth_device_win.h
@@ -29,7 +29,6 @@ class BluetoothDeviceWin : public BluetoothDevice {
virtual void GetServiceRecords(
const ServiceRecordsCallback& callback,
const ErrorCallback& error_callback) OVERRIDE;
- virtual bool ProvidesServiceWithUUID(const std::string& uuid) const OVERRIDE;
virtual void ProvidesServiceWithName(
const std::string& name,
const ProvidesServiceCallback& callback) OVERRIDE;
@@ -74,8 +73,6 @@ class BluetoothDeviceWin : public BluetoothDevice {
// Used to compare the devices.
uint32 device_fingerprint_;
ServiceRecordList service_record_list_;
- // The services (identified by UUIDs) that this device provides.
- ServiceList service_uuids_;
DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceWin);
};
diff --git a/device/bluetooth/bluetooth_device_win_unittest.cc b/device/bluetooth/bluetooth_device_win_unittest.cc
new file mode 100644
index 0000000..892f022
--- /dev/null
+++ b/device/bluetooth/bluetooth_device_win_unittest.cc
@@ -0,0 +1,126 @@
+// Copyright (c) 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 "base/basictypes.h"
+#include "base/bind.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/memory/scoped_vector.h"
+#include "base/string_number_conversions.h"
+#include "device/bluetooth/bluetooth_device_win.h"
+#include "device/bluetooth/bluetooth_service_record.h"
+#include "device/bluetooth/bluetooth_task_manager_win.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const char kDeviceName[] = "Device";
+const char kDeviceAddress[] = "device address";
+
+const char kTestAudioSdpName[] = "Audio";
+const char kTestAudioSdpAddress[] = "01:02:03:0A:10:A0";
+const char kTestAudioSdpBytes[] =
+ "35510900000a00010001090001350319110a09000435103506190100090019350619001909"
+ "010209000535031910020900093508350619110d090102090100250c417564696f20536f75"
+ "726365090311090001";
+const char kTestAudioSdpUuid[] = "0000110a-0000-1000-8000-00805f9b34fb";
+
+const char kTestVideoSdpName[] = "Video";
+const char kTestVideoSdpAddress[] = "A0:10:0A:03:02:01";
+const char kTestVideoSdpBytes[] =
+ "354b0900000a000100030900013506191112191203090004350c3503190100350519000308"
+ "0b090005350319100209000935083506191108090100090100250d566f6963652047617465"
+ "776179";
+const char kTestVideoSdpUuid[] = "00001112-0000-1000-8000-00805f9b34fb";
+
+} // namespace
+
+namespace device {
+
+class BluetoothDeviceWinTest : public testing::Test {
+ public:
+ BluetoothDeviceWinTest()
+ : error_called_(false),
+ provides_service_with_name_(false) {
+ BluetoothTaskManagerWin::DeviceState device_state;
+ device_state.name = kDeviceName;
+ device_state.address = kDeviceAddress;
+
+ BluetoothTaskManagerWin::ServiceRecordState* audio_state =
+ new BluetoothTaskManagerWin::ServiceRecordState();
+ audio_state->name = kTestAudioSdpName;
+ audio_state->address = kTestAudioSdpAddress;
+ base::HexStringToBytes(kTestAudioSdpBytes, &audio_state->sdp_bytes);
+ device_state.service_record_states.push_back(audio_state);
+
+ BluetoothTaskManagerWin::ServiceRecordState* video_state =
+ new BluetoothTaskManagerWin::ServiceRecordState();
+ video_state->name = kTestVideoSdpName;
+ video_state->address = kTestVideoSdpAddress;
+ base::HexStringToBytes(kTestVideoSdpBytes, &video_state->sdp_bytes);
+ device_state.service_record_states.push_back(video_state);
+
+ device_.reset(new BluetoothDeviceWin(device_state));
+ }
+
+ void GetServiceRecords(
+ const BluetoothDevice::ServiceRecordList& service_record_list) {
+ service_records_ = &service_record_list;
+ }
+
+ void OnError() {
+ error_called_ = true;
+ }
+
+ void SetProvidesServiceWithName(bool provides_service_with_name) {
+ provides_service_with_name_ = provides_service_with_name;
+ }
+
+ protected:
+ scoped_ptr<BluetoothDevice> device_;
+ scoped_ptr<BluetoothDevice> empty_device_;
+ const BluetoothDevice::ServiceRecordList* service_records_;
+ bool error_called_;
+ bool provides_service_with_name_;
+};
+
+TEST_F(BluetoothDeviceWinTest, GetServices) {
+ const BluetoothDevice::ServiceList& service_list = device_->GetServices();
+
+ EXPECT_EQ(2, service_list.size());
+ EXPECT_STREQ(kTestAudioSdpUuid, service_list[0].c_str());
+ EXPECT_STREQ(kTestVideoSdpUuid, service_list[1].c_str());
+}
+
+TEST_F(BluetoothDeviceWinTest, GetServiceRecords) {
+ device_->GetServiceRecords(
+ base::Bind(&BluetoothDeviceWinTest::GetServiceRecords,
+ base::Unretained(this)),
+ BluetoothDevice::ErrorCallback());
+ ASSERT_TRUE(service_records_ != NULL);
+ EXPECT_EQ(2, service_records_->size());
+ EXPECT_STREQ(kTestAudioSdpUuid, (*service_records_)[0]->uuid().c_str());
+ EXPECT_STREQ(kTestVideoSdpUuid, (*service_records_)[1]->uuid().c_str());
+}
+
+TEST_F(BluetoothDeviceWinTest, ProvidesServiceWithName) {
+ device_->ProvidesServiceWithName(
+ kTestAudioSdpName,
+ base::Bind(&BluetoothDeviceWinTest::SetProvidesServiceWithName,
+ base::Unretained(this)));
+ EXPECT_TRUE(provides_service_with_name_);
+
+ device_->ProvidesServiceWithName(
+ kTestVideoSdpName,
+ base::Bind(&BluetoothDeviceWinTest::SetProvidesServiceWithName,
+ base::Unretained(this)));
+ EXPECT_TRUE(provides_service_with_name_);
+
+ device_->ProvidesServiceWithName(
+ "name that does not exist",
+ base::Bind(&BluetoothDeviceWinTest::SetProvidesServiceWithName,
+ base::Unretained(this)));
+ EXPECT_FALSE(provides_service_with_name_);
+}
+
+} // namespace device \ No newline at end of file
diff --git a/device/bluetooth/bluetooth_init_win.h b/device/bluetooth/bluetooth_init_win.h
index 188cb8e..00fd186 100644
--- a/device/bluetooth/bluetooth_init_win.h
+++ b/device/bluetooth/bluetooth_init_win.h
@@ -14,6 +14,7 @@
#undef FACILITY_VISUALCPP
#endif
#include <delayimp.h>
+#include <ws2def.h>
#include <ws2bth.h>
#pragma comment(lib, "Bthprops.lib")
diff --git a/device/bluetooth/bluetooth_service_record_win_unittest.cc b/device/bluetooth/bluetooth_service_record_win_unittest.cc
index 1b7751f..5f5c03e 100644
--- a/device/bluetooth/bluetooth_service_record_win_unittest.cc
+++ b/device/bluetooth/bluetooth_service_record_win_unittest.cc
@@ -43,7 +43,7 @@ TEST_F(BluetoothServiceRecordWinTest, NoRfcommSdp) {
uint8 sdp_bytes_array[kTestNoRfcommSdpBytesSize];
ConvertSdpBytes(kTestNoRfcommSdpBytes, sdp_bytes_array);
BluetoothServiceRecordWin service_record("NoRfcommSdp",
- "Service Address",
+ "01:02:03:0A:10:A0",
kTestNoRfcommSdpBytesSize,
sdp_bytes_array);
EXPECT_STREQ(kTestNoRfcommSdpUuid, service_record.uuid().c_str());
@@ -55,7 +55,7 @@ TEST_F(BluetoothServiceRecordWinTest, RfcommSdp) {
uint8 sdp_bytes_array[kTestRfcommSdpBytesSize];
ConvertSdpBytes(kTestRfcommSdpBytes, sdp_bytes_array);
BluetoothServiceRecordWin service_record("RfcommSdp",
- "Service Address",
+ "01:02:03:0A:10:A0",
kTestRfcommSdpBytesSize,
sdp_bytes_array);
EXPECT_STREQ(kTestRfcommSdpUuid, service_record.uuid().c_str());
diff --git a/device/bluetooth/bluetooth_socket_win.cc b/device/bluetooth/bluetooth_socket_win.cc
index 7752483..01b8eee 100644
--- a/device/bluetooth/bluetooth_socket_win.cc
+++ b/device/bluetooth/bluetooth_socket_win.cc
@@ -11,7 +11,6 @@
#include "base/sys_string_conversions.h"
#include "device/bluetooth/bluetooth_init_win.h"
#include "device/bluetooth/bluetooth_service_record_win.h"
-#include "device/bluetooth/bluetooth_utils.h"
#include "net/base/io_buffer.h"
#include "net/base/winsock_init.h"
diff --git a/device/device.gyp b/device/device.gyp
index cfc0b41..305337f 100644
--- a/device/device.gyp
+++ b/device/device.gyp
@@ -143,6 +143,7 @@
'bluetooth/bluetooth_adapter_chromeos_unittest.cc',
'bluetooth/bluetooth_adapter_devices_chromeos_unittest.cc',
'bluetooth/bluetooth_adapter_win_unittest.cc',
+ 'bluetooth/bluetooth_device_win_unittest.cc',
'bluetooth/bluetooth_service_record_chromeos_unittest.cc',
'bluetooth/bluetooth_service_record_win_unittest.cc',
'bluetooth/bluetooth_task_manager_win_unittest.cc',