1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/copresence_sockets/public/copresence_peer.h"
10 #include "base/bind_helpers.h"
11 #include "base/format_macros.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "components/copresence_sockets/transports/bluetooth/copresence_socket_bluetooth.h"
14 #include "device/bluetooth/bluetooth_adapter.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_device.h"
17 #include "device/bluetooth/bluetooth_socket.h"
18 #include "device/bluetooth/bluetooth_uuid.h"
22 const char kAdapterError
[] = "NOADAPTER";
23 const char kSocketServiceUuid
[] = "2491fb14-0077-4d4d-bd41-b18e9a570f56";
25 // This class will confirm pairing for a device that is expecting a pairing
27 class DefaultApprovalDelegate
28 : public device::BluetoothDevice::PairingDelegate
{
30 DefaultApprovalDelegate() {}
31 ~DefaultApprovalDelegate() override
{}
33 // device::BluetoothDevice::PairingDelegate overrides:
34 void RequestPinCode(device::BluetoothDevice
* device
) override
{}
35 void RequestPasskey(device::BluetoothDevice
* device
) override
{}
36 void DisplayPinCode(device::BluetoothDevice
* device
,
37 const std::string
& pincode
) override
{}
38 void DisplayPasskey(device::BluetoothDevice
* device
,
39 uint32 passkey
) override
{}
40 void KeysEntered(device::BluetoothDevice
* device
, uint32 entered
) override
{}
41 void ConfirmPasskey(device::BluetoothDevice
* device
,
42 uint32 passkey
) override
{}
43 void AuthorizePairing(device::BluetoothDevice
* device
) override
{
44 if (device
->ExpectingConfirmation())
45 device
->ConfirmPairing();
51 namespace copresence_sockets
{
53 CopresencePeer::CopresencePeer(const CreatePeerCallback
& create_callback
,
54 const AcceptCallback
& accept_callback
)
55 : create_callback_(create_callback
),
56 accept_callback_(accept_callback
),
58 weak_ptr_factory_(this) {
59 DCHECK(!create_callback
.is_null());
60 DCHECK(!accept_callback
.is_null());
62 if (!device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) {
63 create_callback_
.Run(std::string());
67 device::BluetoothAdapterFactory::GetAdapter(base::Bind(
68 &CopresencePeer::OnGetAdapter
, weak_ptr_factory_
.GetWeakPtr()));
71 std::string
CopresencePeer::GetLocatorData() {
74 // TODO(rkc): Fix the "1." once we have finalized the locator format with
75 // other platforms. http://crbug.com/418616
76 return "1." + adapter_
->GetAddress() + "." + kSocketServiceUuid
;
79 CopresencePeer::~CopresencePeer() {
80 server_socket_
->Disconnect(base::Bind(&base::DoNothing
));
81 server_socket_
->Close();
83 adapter_
->RemovePairingDelegate(delegate_
.get());
88 void CopresencePeer::OnGetAdapter(
89 scoped_refptr
<device::BluetoothAdapter
> adapter
) {
90 if (!adapter
.get() || !adapter
->IsPresent() || !adapter
->IsPowered()) {
91 LOG(WARNING
) << "Unable to use BT adapter";
92 create_callback_
.Run(std::string());
97 delegate_
= make_scoped_ptr(new DefaultApprovalDelegate());
98 VLOG(2) << "Got Adapter, creating service with UUID: " << kSocketServiceUuid
;
99 adapter_
->AddPairingDelegate(
101 device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
102 adapter_
->CreateRfcommService(
103 device::BluetoothUUID(kSocketServiceUuid
),
104 device::BluetoothAdapter::ServiceOptions(),
105 base::Bind(&CopresencePeer::OnCreateService
,
106 weak_ptr_factory_
.GetWeakPtr()),
107 base::Bind(&CopresencePeer::OnCreateServiceError
,
108 weak_ptr_factory_
.GetWeakPtr()));
111 void CopresencePeer::OnCreateService(
112 scoped_refptr
<device::BluetoothSocket
> socket
) {
114 LOG(WARNING
) << "Couldn't create service!";
115 create_callback_
.Run(std::string());
119 VLOG(3) << "Starting Accept Socket.";
120 server_socket_
= socket
;
121 create_callback_
.Run(GetLocatorData());
122 server_socket_
->Accept(
123 base::Bind(&CopresencePeer::OnAccept
, weak_ptr_factory_
.GetWeakPtr()),
124 base::Bind(&CopresencePeer::OnAcceptError
,
125 weak_ptr_factory_
.GetWeakPtr()));
128 void CopresencePeer::OnCreateServiceError(const std::string
& message
) {
129 LOG(WARNING
) << "Couldn't create Bluetooth service: " << message
;
130 create_callback_
.Run(std::string());
133 void CopresencePeer::OnAccept(const device::BluetoothDevice
* device
,
134 scoped_refptr
<device::BluetoothSocket
> socket
) {
137 VLOG(3) << "Accepted Socket.";
138 accept_callback_
.Run(make_scoped_ptr(new CopresenceSocketBluetooth(socket
)));
141 void CopresencePeer::OnAcceptError(const std::string
& message
) {
142 LOG(WARNING
) << "Couldn't accept Bluetooth connection: " << message
;
145 } // namespace copresence_sockets