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

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm

Issue 2094633003: Bluetooth: Mac: implementation for start notification (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@write_read_characteristicscan_servicescan_cleanup
Patch Set: Addressing msarda's comments Created 4 years, 5 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" 5 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/ptr_util.h"
9 #include "base/strings/sys_string_conversions.h"
8 #include "base/threading/thread_task_runner_handle.h" 10 #include "base/threading/thread_task_runner_handle.h"
9 #include "device/bluetooth/bluetooth_adapter_mac.h" 11 #include "device/bluetooth/bluetooth_adapter_mac.h"
10 #include "device/bluetooth/bluetooth_device_mac.h" 12 #include "device/bluetooth/bluetooth_device_mac.h"
13 #include "device/bluetooth/bluetooth_gatt_notify_session_mac.h"
11 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" 14 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h"
12 15
13 namespace device { 16 namespace device {
14 17
15 namespace { 18 namespace {
16 19
17 static BluetoothGattCharacteristic::Properties ConvertProperties( 20 static BluetoothGattCharacteristic::Properties ConvertProperties(
18 CBCharacteristicProperties cb_property) { 21 CBCharacteristicProperties cb_property) {
19 BluetoothGattCharacteristic::Properties result = 22 BluetoothGattCharacteristic::Properties result =
20 BluetoothGattCharacteristic::PROPERTY_NONE; 23 BluetoothGattCharacteristic::PROPERTY_NONE;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 } 63 }
61 return result; 64 return result;
62 } 65 }
63 } // namespace 66 } // namespace
64 67
65 BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac( 68 BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac(
66 BluetoothRemoteGattServiceMac* gatt_service, 69 BluetoothRemoteGattServiceMac* gatt_service,
67 CBCharacteristic* cb_characteristic) 70 CBCharacteristic* cb_characteristic)
68 : gatt_service_(gatt_service), 71 : gatt_service_(gatt_service),
69 cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN), 72 cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN),
70 characteristic_value_read_or_write_in_progress_(false) { 73 characteristic_value_read_or_write_in_progress_(false),
74 weak_ptr_factory_(this) {
71 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID( 75 uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID(
72 [cb_characteristic_.get() UUID]); 76 [cb_characteristic_.get() UUID]);
73 identifier_ = 77 identifier_ = base::SysNSStringToUTF8(
74 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), 78 [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(),
75 (void*)cb_characteristic_] 79 (void*)cb_characteristic_]);
76 .UTF8String;
77 } 80 }
78 81
79 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {} 82 BluetoothRemoteGattCharacteristicMac::~BluetoothRemoteGattCharacteristicMac() {}
80 83
81 std::string BluetoothRemoteGattCharacteristicMac::GetIdentifier() const { 84 std::string BluetoothRemoteGattCharacteristicMac::GetIdentifier() const {
82 return identifier_; 85 return identifier_;
83 } 86 }
84 87
85 BluetoothUUID BluetoothRemoteGattCharacteristicMac::GetUUID() const { 88 BluetoothUUID BluetoothRemoteGattCharacteristicMac::GetUUID() const {
86 return uuid_; 89 return uuid_;
(...skipping 15 matching lines...) Expand all
102 const { 105 const {
103 return value_; 106 return value_;
104 } 107 }
105 108
106 BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicMac::GetService() 109 BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicMac::GetService()
107 const { 110 const {
108 return static_cast<BluetoothRemoteGattService*>(gatt_service_); 111 return static_cast<BluetoothRemoteGattService*>(gatt_service_);
109 } 112 }
110 113
111 bool BluetoothRemoteGattCharacteristicMac::IsNotifying() const { 114 bool BluetoothRemoteGattCharacteristicMac::IsNotifying() const {
112 NOTIMPLEMENTED(); 115 return cb_characteristic_.get().isNotifying == YES;
113 return false;
114 } 116 }
115 117
116 std::vector<BluetoothRemoteGattDescriptor*> 118 std::vector<BluetoothRemoteGattDescriptor*>
117 BluetoothRemoteGattCharacteristicMac::GetDescriptors() const { 119 BluetoothRemoteGattCharacteristicMac::GetDescriptors() const {
118 NOTIMPLEMENTED(); 120 NOTIMPLEMENTED();
119 return std::vector<BluetoothRemoteGattDescriptor*>(); 121 return std::vector<BluetoothRemoteGattDescriptor*>();
120 } 122 }
121 123
122 BluetoothRemoteGattDescriptor* 124 BluetoothRemoteGattDescriptor*
123 BluetoothRemoteGattCharacteristicMac::GetDescriptor( 125 BluetoothRemoteGattCharacteristicMac::GetDescriptor(
124 const std::string& identifier) const { 126 const std::string& identifier) const {
125 NOTIMPLEMENTED(); 127 NOTIMPLEMENTED();
126 return nullptr; 128 return nullptr;
127 } 129 }
128 130
129 void BluetoothRemoteGattCharacteristicMac::StartNotifySession( 131 void BluetoothRemoteGattCharacteristicMac::StartNotifySession(
130 const NotifySessionCallback& callback, 132 const NotifySessionCallback& callback,
131 const ErrorCallback& error_callback) { 133 const ErrorCallback& error_callback) {
132 NOTIMPLEMENTED(); 134 if (IsNotifying()) {
135 std::unique_ptr<BluetoothGattNotifySessionMac> notify_session(
136 new BluetoothGattNotifySessionMac(weak_ptr_factory_.GetWeakPtr()));
137 base::ThreadTaskRunnerHandle::Get()->PostTask(
138 FROM_HERE,
139 base::Bind(callback, base::Passed(std::move(notify_session))));
140 return;
141 }
142 if (!SupportsNotificationsOrIndications()) {
143 base::ThreadTaskRunnerHandle::Get()->PostTask(
144 FROM_HERE,
145 base::Bind(error_callback,
146 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED));
147 return;
148 }
149 start_notify_session_callbacks_.push_back(
150 std::make_pair(callback, error_callback));
151 if (start_notifications_in_progress_)
152 return;
153 [gatt_service_->GetCBPeripheral() setNotifyValue:YES
154 forCharacteristic:cb_characteristic_.get()];
155 start_notifications_in_progress_ = true;
133 } 156 }
134 157
135 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic( 158 void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic(
136 const ValueCallback& callback, 159 const ValueCallback& callback,
137 const ErrorCallback& error_callback) { 160 const ErrorCallback& error_callback) {
138 if (!IsReadable()) { 161 if (!IsReadable()) {
139 base::ThreadTaskRunnerHandle::Get()->PostTask( 162 base::ThreadTaskRunnerHandle::Get()->PostTask(
140 FROM_HERE, 163 FROM_HERE,
141 base::Bind(error_callback, 164 base::Bind(error_callback,
142 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED)); 165 BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED));
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 type:write_type]; 208 type:write_type];
186 if (write_type == CBCharacteristicWriteWithoutResponse) { 209 if (write_type == CBCharacteristicWriteWithoutResponse) {
187 base::ThreadTaskRunnerHandle::Get()->PostTask( 210 base::ThreadTaskRunnerHandle::Get()->PostTask(
188 FROM_HERE, 211 FROM_HERE,
189 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue, 212 base::Bind(&BluetoothRemoteGattCharacteristicMac::DidWriteValue,
190 base::Unretained(this), nil)); 213 base::Unretained(this), nil));
191 } 214 }
192 } 215 }
193 216
194 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) { 217 void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) {
195 if (!characteristic_value_read_or_write_in_progress_) { 218 // This method is called when the characteristic is read and when a
219 // notification is received.
220 if (characteristic_value_read_or_write_in_progress_) {
221 std::pair<ValueCallback, ErrorCallback> callbacks;
222 callbacks.swap(read_characteristic_value_callbacks_);
223 characteristic_value_read_or_write_in_progress_ = false;
224 if (error) {
225 VLOG(1) << "Bluetooth error while reading for characteristic, domain: "
226 << base::SysNSStringToUTF8(error.domain)
227 << ", error code: " << error.code;
228 BluetoothGattService::GattErrorCode error_code =
229 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
230 callbacks.second.Run(error_code);
231 return;
232 }
233 UpdateValueAndNotify();
234 callbacks.first.Run(value_);
235 } else if (IsNotifying()) {
236 UpdateValueAndNotify();
237 } else {
196 // In case of buggy device, nothing should be done if receiving extra 238 // In case of buggy device, nothing should be done if receiving extra
197 // read confirmation. 239 // read confirmation.
198 return; 240 VLOG(1) << "Characteristic value updated while having no pending read nor "
241 "notification.";
199 } 242 }
200 std::pair<ValueCallback, ErrorCallback> callbacks; 243 }
201 callbacks.swap(read_characteristic_value_callbacks_); 244
202 characteristic_value_read_or_write_in_progress_ = false; 245 void BluetoothRemoteGattCharacteristicMac::UpdateValueAndNotify() {
203 if (error) {
204 VLOG(1) << "Bluetooth error while reading for characteristic, domain: "
205 << error.domain.UTF8String << ", error code: " << error.code;
206 BluetoothGattService::GattErrorCode error_code =
207 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
208 callbacks.second.Run(error_code);
209 return;
210 }
211 NSData* nsdata_value = cb_characteristic_.get().value; 246 NSData* nsdata_value = cb_characteristic_.get().value;
212 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); 247 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes);
213 value_.assign(buffer, buffer + nsdata_value.length); 248 value_.assign(buffer, buffer + nsdata_value.length);
214 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, 249 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this,
215 value_); 250 value_);
216 callbacks.first.Run(value_);
217 } 251 }
218 252
219 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) { 253 void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) {
220 if (!characteristic_value_read_or_write_in_progress_) { 254 if (!characteristic_value_read_or_write_in_progress_) {
221 // In case of buggy device, nothing should be done if receiving extra 255 // In case of buggy device, nothing should be done if receiving extra
222 // write confirmation. 256 // write confirmation.
257 VLOG(1) << "Write notification while no write operation pending.";
223 return; 258 return;
224 } 259 }
225 std::pair<base::Closure, ErrorCallback> callbacks; 260 std::pair<base::Closure, ErrorCallback> callbacks;
226 callbacks.swap(write_characteristic_value_callbacks_); 261 callbacks.swap(write_characteristic_value_callbacks_);
227 characteristic_value_read_or_write_in_progress_ = false; 262 characteristic_value_read_or_write_in_progress_ = false;
228 if (error) { 263 if (error) {
229 VLOG(1) << "Bluetooth error while writing for characteristic, domain: " 264 VLOG(1) << "Bluetooth error while writing for characteristic, domain: "
230 << error.domain.UTF8String << ", error code: " << error.code; 265 << base::SysNSStringToUTF8(error.domain)
266 << ", error code: " << error.code;
231 BluetoothGattService::GattErrorCode error_code = 267 BluetoothGattService::GattErrorCode error_code =
232 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error); 268 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
233 callbacks.second.Run(error_code); 269 callbacks.second.Run(error_code);
234 return; 270 return;
235 } 271 }
236 NSData* nsdata_value = cb_characteristic_.get().value; 272 NSData* nsdata_value = cb_characteristic_.get().value;
237 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes); 273 const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes);
238 std::vector<uint8_t> gatt_value(buffer, buffer + nsdata_value.length); 274 std::vector<uint8_t> gatt_value(buffer, buffer + nsdata_value.length);
239 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this, 275 gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this,
240 value_); 276 value_);
241 callbacks.first.Run(); 277 callbacks.first.Run();
242 } 278 }
243 279
280 void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState(
281 NSError* error) {
282 std::vector<std::pair<NotifySessionCallback, ErrorCallback>>
283 reentrant_safe_callbacks;
284 reentrant_safe_callbacks.swap(start_notify_session_callbacks_);
285 start_notifications_in_progress_ = false;
286 if (error) {
287 VLOG(1) << "Bluetooth error while modifying notification state for "
288 "characteristic, domain: "
289 << base::SysNSStringToUTF8(error.domain)
290 << ", error code: " << error.code << ", localized description: "
291 << base::SysNSStringToUTF8(error.localizedDescription);
292 BluetoothGattService::GattErrorCode error_code =
293 BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
294 for (const auto& callback : reentrant_safe_callbacks) {
295 callback.second.Run(error_code);
296 }
297 return;
298 }
299 for (const auto& callback : reentrant_safe_callbacks) {
300 callback.first.Run(base::MakeUnique<BluetoothGattNotifySessionMac>(
301 weak_ptr_factory_.GetWeakPtr()));
302 }
303 }
304
244 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const { 305 bool BluetoothRemoteGattCharacteristicMac::IsReadable() const {
245 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ; 306 return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ;
246 } 307 }
247 308
248 bool BluetoothRemoteGattCharacteristicMac::IsWritable() const { 309 bool BluetoothRemoteGattCharacteristicMac::IsWritable() const {
249 BluetoothGattCharacteristic::Properties properties = GetProperties(); 310 BluetoothGattCharacteristic::Properties properties = GetProperties();
250 return (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) || 311 return (properties & BluetoothGattCharacteristic::PROPERTY_WRITE) ||
251 (properties & PROPERTY_WRITE_WITHOUT_RESPONSE); 312 (properties & PROPERTY_WRITE_WITHOUT_RESPONSE);
252 } 313 }
253 314
315 bool BluetoothRemoteGattCharacteristicMac::SupportsNotificationsOrIndications()
316 const {
317 BluetoothGattCharacteristic::Properties properties = GetProperties();
318 return (properties & PROPERTY_NOTIFY) || (properties & PROPERTY_INDICATE);
319 }
320
254 CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType() 321 CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType()
255 const { 322 const {
256 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE) 323 return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE)
257 ? CBCharacteristicWriteWithResponse 324 ? CBCharacteristicWriteWithResponse
258 : CBCharacteristicWriteWithoutResponse; 325 : CBCharacteristicWriteWithoutResponse;
259 } 326 }
260 327
261 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic() 328 CBCharacteristic* BluetoothRemoteGattCharacteristicMac::GetCBCharacteristic()
262 const { 329 const {
263 return cb_characteristic_.get(); 330 return cb_characteristic_.get();
264 } 331 }
265 } // namespace device. 332 } // namespace device.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698