Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_descriptor_win.cc

Issue 1606013002: BLE GATT service implementation in Chrome for Windows 8 and later (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update Build.gn Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "device/bluetooth/bluetooth_remote_gatt_descriptor_win.h"
6
7 #include "base/bind.h"
8
9 namespace device {
10
11 BluetoothRemoteGattDescriptorWin::BluetoothRemoteGattDescriptorWin(
12 BluetoothAdapterWin* adapter,
13 base::FilePath service_path,
14 BluetoothRemoteGattCharacteristicWin* parent_characteristic,
15 BTH_LE_GATT_DESCRIPTOR* descriptor_info,
16 scoped_refptr<base::SequencedTaskRunner>& ui_task_runner)
17 : adapter_(adapter),
18 service_path_(service_path),
19 parent_characteristic_(parent_characteristic),
20 descriptor_info_(descriptor_info),
21 ui_task_runner_(ui_task_runner),
22 descriptor_initialized_(false),
23 weak_ptr_factory_(this) {
24 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
25 descriptor_value_.clear();
26 read_remote_descriptor_value_callbacks_.clear();
27 write_remote_descriptor_value_callbacks_.clear();
28
29 task_manager_ = adapter_->GetWinBluetoothTaskManager();
30 DCHECK(parent_characteristic_);
31 DCHECK(descriptor_info_.get());
32 DCHECK(adapter_);
33 DCHECK(task_manager_);
34
35 descriptor_uuid_ = task_manager_->BluetoothLowEnergyUuidToBluetoothUuid(
36 descriptor_info_.get()->DescriptorUuid);
37 // TODO(gogerald): Could not find permission information from
38 // BTH_LE_GATT_DESCRIPTOR. Give read and write permission by default. Update
39 // it according to future read/write feedback. Use system information when it
40 // is available.
41 permissions_ = BluetoothGattCharacteristic::PERMISSION_READ |
42 BluetoothGattCharacteristic::PERMISSION_WRITE;
43 Update();
44 }
45
46 BluetoothRemoteGattDescriptorWin::~BluetoothRemoteGattDescriptorWin() {
47 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
48 adapter_->NotifyGattDescriptorRemoved(this);
49 }
50
51 std::string BluetoothRemoteGattDescriptorWin::GetIdentifier() const {
52 return parent_characteristic_->GetIdentifier() + "_" +
53 descriptor_uuid_.value();
54 }
55
56 BluetoothUUID BluetoothRemoteGattDescriptorWin::GetUUID() const {
57 return descriptor_uuid_;
58 }
59
60 bool BluetoothRemoteGattDescriptorWin::IsLocal() const {
61 return false;
62 }
63
64 std::vector<uint8_t>& BluetoothRemoteGattDescriptorWin::GetValue() const {
65 return const_cast<std::vector<uint8_t>&>(descriptor_value_);
66 }
67
68 BluetoothGattCharacteristic*
69 BluetoothRemoteGattDescriptorWin::GetCharacteristic() const {
70 return parent_characteristic_;
71 }
72
73 BluetoothGattCharacteristic::Permissions
74 BluetoothRemoteGattDescriptorWin::GetPermissions() const {
75 return permissions_;
76 }
77
78 BluetoothGattService::GattErrorCode
79 BluetoothRemoteGattDescriptorWin::SystemErrorToGattErrorCode(HRESULT hr) {
80 if (hr == E_BLUETOOTH_ATT_READ_NOT_PERMITTED ||
81 hr == E_BLUETOOTH_ATT_WRITE_NOT_PERMITTED) {
82 return BluetoothGattService::GATT_ERROR_NOT_PERMITTED;
83 }
84 return BluetoothGattService::GATT_ERROR_FAILED;
85 }
86
87 void BluetoothRemoteGattDescriptorWin::ReadRemoteDescriptorValueCallback(
88 BTH_LE_GATT_DESCRIPTOR_VALUE* value,
89 HRESULT hr) {
90 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
91 if (FAILED(hr)) {
92 LOG(ERROR) << "Failed at reading descriptor value";
93 BluetoothGattService::GattErrorCode error = SystemErrorToGattErrorCode(hr);
94 if (error == BluetoothGattService::GATT_ERROR_NOT_PERMITTED) {
95 permissions_ =
96 permissions_ & ~BluetoothGattCharacteristic::PERMISSION_READ;
97 }
98 for (const auto callback : read_remote_descriptor_value_callbacks_)
99 callback.second.Run(error);
100 } else {
101 descriptor_value_.clear();
102 for (ULONG i = 0; i < value->DataSize; i++)
103 descriptor_value_.push_back(value->Data[i]);
104 for (const auto callback : read_remote_descriptor_value_callbacks_)
105 callback.first.Run(descriptor_value_);
106 }
107
108 if (!descriptor_initialized_) {
109 parent_characteristic_->NotifyGattDescriptorAdded(this);
110 descriptor_initialized_ = true;
111 }
112 read_remote_descriptor_value_callbacks_.clear();
113 }
114
115 void BluetoothRemoteGattDescriptorWin::ReadRemoteDescriptor(
116 const ValueCallback& callback,
117 const ErrorCallback& error_callback) {
118 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
119 read_remote_descriptor_value_callbacks_.push_back(
120 std::make_pair(callback, error_callback));
121 task_manager_->PostReadDescriptorValue(
122 service_path_, descriptor_info_.get(),
123 base::Bind(
124 &BluetoothRemoteGattDescriptorWin::ReadRemoteDescriptorValueCallback,
125 weak_ptr_factory_.GetWeakPtr()));
126 }
127
128 void BluetoothRemoteGattDescriptorWin::WriteRemoteDescriptorValueCallback(
129 HRESULT hr) {
130 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
131 if (FAILED(hr)) {
132 BluetoothGattService::GattErrorCode error = SystemErrorToGattErrorCode(hr);
133 if (error == BluetoothGattService::GATT_ERROR_NOT_PERMITTED) {
134 permissions_ =
135 permissions_ & ~BluetoothGattCharacteristic::PERMISSION_WRITE;
136 }
137 for (const auto callback : write_remote_descriptor_value_callbacks_)
138 callback.second.Run(error);
139 } else {
140 for (auto callback : write_remote_descriptor_value_callbacks_)
141 callback.first.Run();
142 }
143
144 write_remote_descriptor_value_callbacks_.clear();
145 }
146
147 void BluetoothRemoteGattDescriptorWin::WriteRemoteDescriptor(
148 const std::vector<uint8_t>& new_value,
149 const base::Closure& callback,
150 const ErrorCallback& error_callback) {
151 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
152 write_remote_descriptor_value_callbacks_.push_back(
153 std::make_pair(callback, error_callback));
154 task_manager_->PostWriteDescriptorValue(
155 service_path_, descriptor_info_.get(), new_value,
156 base::Bind(
157 &BluetoothRemoteGattDescriptorWin::WriteRemoteDescriptorValueCallback,
158 weak_ptr_factory_.GetWeakPtr()));
159 }
160
161 void BluetoothRemoteGattDescriptorWin::Update() {
162 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
163 task_manager_->PostReadDescriptorValue(
164 service_path_, descriptor_info_.get(),
165 base::Bind(
166 &BluetoothRemoteGattDescriptorWin::ReadRemoteDescriptorValueCallback,
167 weak_ptr_factory_.GetWeakPtr()));
168 }
169
170 } // namespace device.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698