Skip to content

Commit

Permalink
Bluetooth: Implement new socket API for Chrome OS
Browse files Browse the repository at this point in the history
Remove the old BluetoothProfileChromeOS implementation and instead
extend BluetoothSocketChromeOS to handle making outgoing connections
and accepting incoming connections through the D-Bus API.

BUG=372493
TEST=device_unittests --gtest_filter=BluetoothSocketChromeOSTest.*

Review URL: https://codereview.chromium.org/276573004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270245 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
keybuk@chromium.org committed May 13, 2014
1 parent a9a8e2f commit 891c394
Show file tree
Hide file tree
Showing 18 changed files with 1,324 additions and 1,062 deletions.
102 changes: 54 additions & 48 deletions chromeos/dbus/bluetooth_profile_manager_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ const char BluetoothProfileManagerClient::kNoResponseError[] =
"org.chromium.Error.NoResponse";


BluetoothProfileManagerClient::Options::Options()
: role(SYMMETRIC),
require_authentication(false),
require_authorization(false),
auto_connect(true) {
BluetoothProfileManagerClient::Options::Options() {
}

BluetoothProfileManagerClient::Options::~Options() {
Expand Down Expand Up @@ -56,22 +52,24 @@ class BluetoothProfileManagerClientImpl

dbus::MessageWriter dict_writer(NULL);

// Always send Name, even if empty string.
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kNameOption);
dict_writer.AppendVariantOfString(options.name);
array_writer.CloseContainer(&dict_writer);
// Send Name if provided.
if (options.name.get() != NULL) {
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kNameOption);
dict_writer.AppendVariantOfString(*(options.name));
array_writer.CloseContainer(&dict_writer);
}

// Don't send Service if not provided.
if (options.service.length()) {
// Send Service if provided.
if (options.service.get() != NULL) {
dbus::MessageWriter dict_writer(NULL);
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kServiceOption);
dict_writer.AppendVariantOfString(options.service);
dict_writer.AppendVariantOfString(*(options.service));
array_writer.CloseContainer(&dict_writer);
}

// Don't send the default Role since there's no value for it.
// Send Role if not the default value.
if (options.role != SYMMETRIC) {
dbus::MessageWriter dict_writer(NULL);
array_writer.OpenDictEntry(&dict_writer);
Expand All @@ -87,67 +85,75 @@ class BluetoothProfileManagerClientImpl
array_writer.CloseContainer(&dict_writer);
}

// Don't send Channel unless given.
if (options.channel) {
// Send Channel if provided.
if (options.channel.get() != NULL) {
dbus::MessageWriter dict_writer(NULL);
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kChannelOption);
dict_writer.AppendVariantOfUint16(options.channel);
dict_writer.AppendVariantOfUint16(*(options.channel));
array_writer.CloseContainer(&dict_writer);
}

// Don't send PSM unless given.
if (options.psm) {
// Send PSM if provided.
if (options.psm.get() != NULL) {
dbus::MessageWriter dict_writer(NULL);
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kPSMOption);
dict_writer.AppendVariantOfUint16(options.psm);
dict_writer.AppendVariantOfUint16(*(options.psm));
array_writer.CloseContainer(&dict_writer);
}

// Send RequireAuthentication if provided.
if (options.require_authentication.get() != NULL) {
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(
bluetooth_profile_manager::kRequireAuthenticationOption);
dict_writer.AppendVariantOfBool(*(options.require_authentication));
array_writer.CloseContainer(&dict_writer);
}

// Send RequireAuthorization if provided.
if (options.require_authorization.get() != NULL) {
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(
bluetooth_profile_manager::kRequireAuthorizationOption);
dict_writer.AppendVariantOfBool(*(options.require_authorization));
array_writer.CloseContainer(&dict_writer);
}

// Send AutoConnect if provided.
if (options.auto_connect.get() != NULL) {
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(
bluetooth_profile_manager::kAutoConnectOption);
dict_writer.AppendVariantOfBool(*(options.auto_connect));
array_writer.CloseContainer(&dict_writer);
}

// Always send RequireAuthentication, RequireAuthorization and AutoConnect.
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(
bluetooth_profile_manager::kRequireAuthenticationOption);
dict_writer.AppendVariantOfBool(options.require_authentication);
array_writer.CloseContainer(&dict_writer);

array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(
bluetooth_profile_manager::kRequireAuthorizationOption);
dict_writer.AppendVariantOfBool(options.require_authorization);
array_writer.CloseContainer(&dict_writer);

array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(
bluetooth_profile_manager::kAutoConnectOption);
dict_writer.AppendVariantOfBool(options.auto_connect);
array_writer.CloseContainer(&dict_writer);

// Don't send ServiceRecord if not provided.
if (options.service_record.length()) {
// Send ServiceRecord if provided.
if (options.service_record.get() != NULL) {
dbus::MessageWriter dict_writer(NULL);
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kServiceRecordOption);
dict_writer.AppendVariantOfString(options.service_record);
dict_writer.AppendVariantOfString(*(options.service_record));
array_writer.CloseContainer(&dict_writer);
}

// Don't send Version if not provided.
if (options.version) {
// Send Version if provided.
if (options.version.get() != NULL) {
dbus::MessageWriter dict_writer(NULL);
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kVersionOption);
dict_writer.AppendVariantOfUint16(options.version);
dict_writer.AppendVariantOfUint16(*(options.version));
array_writer.CloseContainer(&dict_writer);
}

// Don't send Features if not provided.
if (options.features) {
// Send Features if provided.
if (options.features.get() != NULL) {
dbus::MessageWriter dict_writer(NULL);
array_writer.OpenDictEntry(&dict_writer);
dict_writer.AppendString(bluetooth_profile_manager::kFeaturesOption);
dict_writer.AppendVariantOfUint16(options.features);
dict_writer.AppendVariantOfUint16(*(options.features));
array_writer.CloseContainer(&dict_writer);
}

Expand Down
21 changes: 11 additions & 10 deletions chromeos/dbus/bluetooth_profile_manager_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include "base/callback.h"
#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_client.h"
Expand All @@ -35,37 +36,37 @@ class CHROMEOS_EXPORT BluetoothProfileManagerClient : public DBusClient {
~Options();

// Human readable name for the profile.
std::string name;
scoped_ptr<std::string> name;

// Primary service class UUID (if different from the actual UUID)
std::string service;
scoped_ptr<std::string> service;

// Role.
enum ProfileRole role;

// RFCOMM channel number.
uint16 channel;
scoped_ptr<uint16> channel;

// PSM number.
uint16 psm;
scoped_ptr<uint16> psm;

// Pairing is required before connections will be established.
bool require_authentication;
scoped_ptr<bool> require_authentication;

// Request authorization before connections will be established.
bool require_authorization;
scoped_ptr<bool> require_authorization;

// Force connections when a remote device is connected.
bool auto_connect;
scoped_ptr<bool> auto_connect;

// Manual SDP record.
std::string service_record;
scoped_ptr<std::string> service_record;

// Profile version.
uint16 version;
scoped_ptr<uint16> version;

// Profile features.
uint16 features;
scoped_ptr<uint16> features;
};

virtual ~BluetoothProfileManagerClient();
Expand Down
4 changes: 2 additions & 2 deletions chromeos/dbus/fake_bluetooth_profile_manager_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ void FakeBluetoothProfileManagerClient::UnregisterProfile(
VLOG(1) << "UnregisterProfile: " << profile_path.value();

ServiceProviderMap::iterator iter = service_provider_map_.find(profile_path);
if (iter != service_provider_map_.end()) {
if (iter == service_provider_map_.end()) {
error_callback.Run(bluetooth_profile_manager::kErrorInvalidArguments,
"Profile still registered");
"Profile not registered");
} else {
for (ProfileMap::iterator piter = profile_map_.begin();
piter != profile_map_.end(); ++piter) {
Expand Down
2 changes: 0 additions & 2 deletions device/bluetooth/bluetooth.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@
'bluetooth_pairing_chromeos.h',
'bluetooth_profile.cc',
'bluetooth_profile.h',
'bluetooth_profile_chromeos.cc',
'bluetooth_profile_chromeos.h',
'bluetooth_profile_mac.h',
'bluetooth_profile_mac.mm',
'bluetooth_profile_win.cc',
Expand Down
34 changes: 30 additions & 4 deletions device/bluetooth/bluetooth_adapter_chromeos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,17 +243,43 @@ void BluetoothAdapterChromeOS::CreateRfcommService(
bool insecure,
const CreateServiceCallback& callback,
const CreateServiceErrorCallback& error_callback) {
// TODO(keybuk): implement.
NOTIMPLEMENTED();
VLOG(1) << object_path_.value() << ": Creating RFCOMM service: "
<< uuid.canonical_value();
scoped_refptr<BluetoothSocketChromeOS> socket =
BluetoothSocketChromeOS::CreateBluetoothSocket(
ui_task_runner_,
socket_thread_,
NULL,
net::NetLog::Source());
socket->Listen(this,
BluetoothSocketChromeOS::kRfcomm,
uuid,
channel,
insecure,
base::Bind(callback, socket),
error_callback);
}

void BluetoothAdapterChromeOS::CreateL2capService(
const BluetoothUUID& uuid,
int psm,
const CreateServiceCallback& callback,
const CreateServiceErrorCallback& error_callback) {
// TODO(keybuk): implement.
NOTIMPLEMENTED();
VLOG(1) << object_path_.value() << ": Creating L2CAP service: "
<< uuid.canonical_value();
scoped_refptr<BluetoothSocketChromeOS> socket =
BluetoothSocketChromeOS::CreateBluetoothSocket(
ui_task_runner_,
socket_thread_,
NULL,
net::NetLog::Source());
socket->Listen(this,
BluetoothSocketChromeOS::kL2cap,
uuid,
psm,
false,
base::Bind(callback, socket),
error_callback);
}

void BluetoothAdapterChromeOS::RemovePairingDelegateInternal(
Expand Down
24 changes: 12 additions & 12 deletions device/bluetooth/bluetooth_adapter_chromeos.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,25 @@ class BluetoothAdapterChromeOS
const CreateServiceCallback& callback,
const CreateServiceErrorCallback& error_callback) OVERRIDE;

// Locates the device object by object path (the devices map and
// BluetoothDevice methods are by address).
BluetoothDeviceChromeOS* GetDeviceWithPath(
const dbus::ObjectPath& object_path);

// Announce to observers a change in device state that is not reflected by
// its D-Bus properties.
void NotifyDeviceChanged(BluetoothDeviceChromeOS* device);

// Returns the object path of the adapter.
const dbus::ObjectPath& object_path() const { return object_path_; }

protected:
// BluetoothAdapter:
virtual void RemovePairingDelegateInternal(
device::BluetoothDevice::PairingDelegate* pairing_delegate) OVERRIDE;

private:
friend class BluetoothChromeOSTest;
friend class BluetoothDeviceChromeOS;
friend class BluetoothProfileChromeOS;
friend class BluetoothProfileChromeOSTest;

// typedef for callback parameters that are passed to AddDiscoverySession
// and RemoveDiscoverySession. This is used to queue incoming requests while
Expand Down Expand Up @@ -149,11 +158,6 @@ class BluetoothAdapterChromeOS
void OnRequestDefaultAgentError(const std::string& error_name,
const std::string& error_message);

// Internal method used to locate the device object by object path
// (the devices map and BluetoothDevice methods are by address)
BluetoothDeviceChromeOS* GetDeviceWithPath(
const dbus::ObjectPath& object_path);

// Internal method to obtain a BluetoothPairingChromeOS object for the device
// with path |object_path|. Returns the existing pairing object if the device
// already has one (usually an outgoing connection in progress) or a new
Expand All @@ -178,10 +182,6 @@ class BluetoothAdapterChromeOS
void DiscoveringChanged(bool discovering);
void PresentChanged(bool present);

// Announce to observers a change in device state that is not reflected by
// its D-Bus properties.
void NotifyDeviceChanged(BluetoothDeviceChromeOS* device);

// Called by dbus:: on completion of the discoverable property change.
void OnSetDiscoverable(const base::Closure& callback,
const ErrorCallback& error_callback,
Expand Down
Loading

0 comments on commit 891c394

Please sign in to comment.