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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
// Copyright 2016 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.
#ifndef DEVICE_BLUETOOTH_DEVICE_H_
#define DEVICE_BLUETOOTH_DEVICE_H_
#include <memory>
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/bluetooth_gatt_connection.h"
#include "device/bluetooth/bluetooth_remote_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_remote_gatt_descriptor.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h"
#include "device/bluetooth/public/interfaces/device.mojom.h"
#include "mojo/public/cpp/bindings/strong_binding.h"
namespace bluetooth {
// Implementation of Mojo Device located in
// device/bluetooth/public/interfaces/device.mojom.
// It handles requests to interact with Bluetooth Device.
// Uses the platform abstraction of device/bluetooth.
// An instance of this class is constructed by Adapter and strongly bound
// to its MessagePipe. In the case where the BluetoothGattConnection dies, the
// instance closes the binding which causes the instance to be deleted.
class Device : public mojom::Device, public device::BluetoothAdapter::Observer {
public:
~Device() override;
static void Create(
scoped_refptr<device::BluetoothAdapter> adapter,
std::unique_ptr<device::BluetoothGattConnection> connection,
mojom::DeviceRequest request);
// Creates a mojom::DeviceInfo using info from the given |device|.
static mojom::DeviceInfoPtr ConstructDeviceInfoStruct(
const device::BluetoothDevice* device);
// BluetoothAdapter::Observer overrides:
void DeviceChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) override;
void GattServicesDiscovered(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) override;
// mojom::Device overrides:
void Disconnect() override;
void GetInfo(const GetInfoCallback& callback) override;
void GetServices(const GetServicesCallback& callback) override;
void GetCharacteristics(const std::string& service_id,
const GetCharacteristicsCallback& callback) override;
void ReadValueForCharacteristic(
const std::string& service_id,
const std::string& characteristic_id,
const ReadValueForCharacteristicCallback& callback) override;
void WriteValueForCharacteristic(
const std::string& service_id,
const std::string& characteristic_id,
const std::vector<uint8_t>& value,
const WriteValueForCharacteristicCallback& callback) override;
void GetDescriptors(const std::string& service_id,
const std::string& characteristic_id,
const GetDescriptorsCallback& callback) override;
void ReadValueForDescriptor(
const std::string& service_id,
const std::string& characteristic_id,
const std::string& descriptor_id,
const ReadValueForDescriptorCallback& callback) override;
void WriteValueForDescriptor(
const std::string& service_id,
const std::string& characteristic_id,
const std::string& descriptor_id,
const std::vector<uint8_t>& value,
const WriteValueForDescriptorCallback& callback) override;
private:
Device(scoped_refptr<device::BluetoothAdapter> adapter,
std::unique_ptr<device::BluetoothGattConnection> connection);
void GetServicesImpl(const GetServicesCallback& callback);
mojom::ServiceInfoPtr ConstructServiceInfoStruct(
const device::BluetoothRemoteGattService& service);
void OnReadRemoteCharacteristic(
const ReadValueForCharacteristicCallback& callback,
const std::vector<uint8_t>& value);
void OnReadRemoteCharacteristicError(
const ReadValueForCharacteristicCallback& callback,
device::BluetoothGattService::GattErrorCode error_code);
void OnWriteRemoteCharacteristic(
const WriteValueForCharacteristicCallback& callback);
void OnWriteRemoteCharacteristicError(
const WriteValueForCharacteristicCallback& callback,
device::BluetoothGattService::GattErrorCode error_code);
void OnReadRemoteDescriptor(const ReadValueForDescriptorCallback& callback,
const std::vector<uint8_t>& value);
void OnReadRemoteDescriptorError(
const ReadValueForDescriptorCallback& callback,
device::BluetoothGattService::GattErrorCode error_code);
void OnWriteRemoteDescriptor(const WriteValueForDescriptorCallback& callback);
void OnWriteRemoteDescriptorError(
const WriteValueForDescriptorCallback& callback,
device::BluetoothGattService::GattErrorCode error_code);
const std::string& GetAddress();
// The current BluetoothAdapter.
scoped_refptr<device::BluetoothAdapter> adapter_;
// The GATT connection to this device.
std::unique_ptr<device::BluetoothGattConnection> connection_;
mojo::StrongBindingPtr<mojom::Device> binding_;
// The services request queue which holds callbacks that are waiting for
// services to be discovered for this device.
std::vector<base::Closure> pending_services_requests_;
base::WeakPtrFactory<Device> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(Device);
};
} // namespace bluetooth
#endif // DEVICE_BLUETOOTH_DEVICE_H_
|