summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_gatt_connection_bluez.cc
blob: 98b6a4699bd4b8b51b5b99d568a99decf620085e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2014 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 "device/bluetooth/bluetooth_gatt_connection_bluez.h"

#include "base/bind.h"
#include "base/logging.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"

namespace bluez {

BluetoothGattConnectionBlueZ::BluetoothGattConnectionBlueZ(
    scoped_refptr<device::BluetoothAdapter> adapter,
    const std::string& device_address,
    const dbus::ObjectPath& object_path)
    : BluetoothGattConnection(adapter.get(), device_address),
      connected_(true),
      object_path_(object_path) {
  DCHECK(adapter_.get());
  DCHECK(!device_address_.empty());
  DCHECK(object_path_.IsValid());

  bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->AddObserver(this);
}

BluetoothGattConnectionBlueZ::~BluetoothGattConnectionBlueZ() {
  bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->RemoveObserver(
      this);
  Disconnect();
}

bool BluetoothGattConnectionBlueZ::IsConnected() {
  // Lazily determine the activity state of the connection. If already
  // marked as inactive, then return false. Otherwise, explicitly mark
  // |connected_| as false if the device is removed or disconnected. We do this,
  // so that if this method is called during a call to DeviceRemoved or
  // DeviceChanged somewhere else, it returns the correct status.
  if (!connected_)
    return false;

  bluez::BluetoothDeviceClient::Properties* properties =
      bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
          object_path_);
  if (!properties || !properties->connected.value())
    connected_ = false;

  return connected_;
}

void BluetoothGattConnectionBlueZ::Disconnect() {
  if (!connected_) {
    VLOG(1) << "Connection already inactive.";
    return;
  }

  // TODO(armansito): There isn't currently a good way to manage the ownership
  // of a connection between Chrome and bluetoothd plugins/profiles. Until
  // a proper reference count is kept by bluetoothd, we might unwittingly kill
  // a connection that is managed by the daemon (e.g. HoG). For now, just return
  // success to indicate that this BluetoothGattConnection is no longer active,
  // even though the underlying connection won't actually be disconnected. This
  // technically doesn't violate the contract put forth by this API.
  connected_ = false;
}

void BluetoothGattConnectionBlueZ::DeviceRemoved(
    const dbus::ObjectPath& object_path) {
  if (object_path != object_path_)
    return;

  connected_ = false;
}

void BluetoothGattConnectionBlueZ::DevicePropertyChanged(
    const dbus::ObjectPath& object_path,
    const std::string& property_name) {
  if (object_path != object_path_)
    return;

  if (!connected_)
    return;

  bluez::BluetoothDeviceClient::Properties* properties =
      bluez::BluezDBusManager::Get()->GetBluetoothDeviceClient()->GetProperties(
          object_path_);

  if (!properties) {
    connected_ = false;
    return;
  }

  if (property_name == properties->connected.name() &&
      !properties->connected.value())
    connected_ = false;

  // The remote device's bluetooth address may change if it is paired while
  // connected.
  if (property_name == properties->address.name())
    device_address_ = properties->address.value();
}

}  // namespace bluez