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

Unified 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: Fixing last comments, and last unit test Created 4 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm
diff --git a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm
index 6ff692f8e70016d79f578a42625342087425b995..a4bce15b5916ad2bfd2c7f885b59602ccd16982c 100644
--- a/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm
+++ b/device/bluetooth/bluetooth_remote_gatt_characteristic_mac.mm
@@ -5,9 +5,11 @@
#include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h"
#include "base/bind.h"
+#include "base/memory/ptr_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "device/bluetooth/bluetooth_adapter_mac.h"
#include "device/bluetooth/bluetooth_device_mac.h"
+#include "device/bluetooth/bluetooth_gatt_notify_session_mac.h"
#include "device/bluetooth/bluetooth_remote_gatt_service_mac.h"
namespace device {
@@ -67,7 +69,8 @@ BluetoothRemoteGattCharacteristicMac::BluetoothRemoteGattCharacteristicMac(
CBCharacteristic* cb_characteristic)
: gatt_service_(gatt_service),
cb_characteristic_(cb_characteristic, base::scoped_policy::RETAIN),
- characteristic_value_read_or_write_in_progress_(false) {
+ characteristic_value_read_or_write_in_progress_(false),
+ weak_ptr_factory_(this) {
uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID(
[cb_characteristic_.get() UUID]);
identifier_ =
@@ -109,8 +112,7 @@ BluetoothRemoteGattService* BluetoothRemoteGattCharacteristicMac::GetService()
}
bool BluetoothRemoteGattCharacteristicMac::IsNotifying() const {
- NOTIMPLEMENTED();
- return false;
+ return cb_characteristic_.get().isNotifying == YES;
}
std::vector<BluetoothRemoteGattDescriptor*>
@@ -129,7 +131,28 @@ BluetoothRemoteGattCharacteristicMac::GetDescriptor(
void BluetoothRemoteGattCharacteristicMac::StartNotifySession(
const NotifySessionCallback& callback,
const ErrorCallback& error_callback) {
- NOTIMPLEMENTED();
+ if (IsNotifying()) {
+ std::unique_ptr<BluetoothGattNotifySessionMac> notify_session(
+ new BluetoothGattNotifySessionMac(weak_ptr_factory_.GetWeakPtr()));
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE,
+ base::Bind(callback, base::Passed(std::move(notify_session))));
+ return;
+ }
+ if (!SupportsNotificationsOrIndications()) {
+ base::ThreadTaskRunnerHandle::Get()->PostTask(
+ FROM_HERE,
+ base::Bind(error_callback,
+ BluetoothRemoteGattService::GATT_ERROR_NOT_SUPPORTED));
+ return;
+ }
+ start_notify_session_callbacks_.push_back(
+ std::make_pair(callback, error_callback));
+ if (start_notifications_in_progress_)
+ return;
+ [gatt_service_->GetCBPeripheral() setNotifyValue:YES
+ forCharacteristic:cb_characteristic_.get()];
+ start_notifications_in_progress_ = true;
}
void BluetoothRemoteGattCharacteristicMac::ReadRemoteCharacteristic(
@@ -192,34 +215,45 @@ void BluetoothRemoteGattCharacteristicMac::WriteRemoteCharacteristic(
}
void BluetoothRemoteGattCharacteristicMac::DidUpdateValue(NSError* error) {
- if (!characteristic_value_read_or_write_in_progress_) {
+ // This method is called when the characteristic is read and when a
+ // notification received.
msarda 2016/06/29 07:57:39 s/received/is received
jlebel 2016/06/29 08:05:57 Done.
+ if (characteristic_value_read_or_write_in_progress_) {
+ std::pair<ValueCallback, ErrorCallback> callbacks;
+ callbacks.swap(read_characteristic_value_callbacks_);
+ characteristic_value_read_or_write_in_progress_ = false;
+ if (error) {
+ VLOG(1) << "Bluetooth error while reading for characteristic, domain: "
+ << error.domain.UTF8String << ", error code: " << error.code;
msarda 2016/06/29 07:57:39 Concert NSString to std::string objects using base
jlebel 2016/06/29 08:05:57 Done.
+ BluetoothGattService::GattErrorCode error_code =
+ BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
+ callbacks.second.Run(error_code);
+ return;
+ }
+ UpdateValueAndNotify();
+ callbacks.first.Run(value_);
+ } else if (IsNotifying()) {
+ UpdateValueAndNotify();
+ } else {
// In case of buggy device, nothing should be done if receiving extra
// read confirmation.
- return;
- }
- std::pair<ValueCallback, ErrorCallback> callbacks;
- callbacks.swap(read_characteristic_value_callbacks_);
- characteristic_value_read_or_write_in_progress_ = false;
- if (error) {
- VLOG(1) << "Bluetooth error while reading for characteristic, domain: "
- << error.domain.UTF8String << ", error code: " << error.code;
- BluetoothGattService::GattErrorCode error_code =
- BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
- callbacks.second.Run(error_code);
- return;
+ VLOG(1) << "Characteristic value updated while having no pending read nor "
+ "notification.";
}
+}
+
+void BluetoothRemoteGattCharacteristicMac::UpdateValueAndNotify() {
NSData* nsdata_value = cb_characteristic_.get().value;
const uint8_t* buffer = static_cast<const uint8_t*>(nsdata_value.bytes);
value_.assign(buffer, buffer + nsdata_value.length);
gatt_service_->GetMacAdapter()->NotifyGattCharacteristicValueChanged(this,
value_);
- callbacks.first.Run(value_);
}
void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) {
if (!characteristic_value_read_or_write_in_progress_) {
// In case of buggy device, nothing should be done if receiving extra
// write confirmation.
+ VLOG(1) << "Write notification while no write operation pending.";
return;
}
std::pair<base::Closure, ErrorCallback> callbacks;
@@ -241,6 +275,31 @@ void BluetoothRemoteGattCharacteristicMac::DidWriteValue(NSError* error) {
callbacks.first.Run();
}
+void BluetoothRemoteGattCharacteristicMac::DidUpdateNotificationState(
+ NSError* error) {
+ std::vector<std::pair<NotifySessionCallback, ErrorCallback>>
+ reentrant_safe_callbacks;
+ reentrant_safe_callbacks.swap(start_notify_session_callbacks_);
+ start_notifications_in_progress_ = false;
+ if (error) {
+ VLOG(1) << "Bluetooth error while modifying notification state for "
+ "characteristic, domain: "
+ << error.domain.UTF8String << ", error code: " << error.code
+ << ", localized description: "
+ << error.localizedDescription.UTF8String;
+ BluetoothGattService::GattErrorCode error_code =
+ BluetoothDeviceMac::GetGattErrorCodeFromNSError(error);
+ for (const auto& callback : reentrant_safe_callbacks) {
+ callback.second.Run(error_code);
+ }
+ return;
+ }
+ for (const auto& callback : reentrant_safe_callbacks) {
+ callback.first.Run(base::MakeUnique<BluetoothGattNotifySessionMac>(
+ weak_ptr_factory_.GetWeakPtr()));
+ }
+}
+
bool BluetoothRemoteGattCharacteristicMac::IsReadable() const {
return GetProperties() & BluetoothGattCharacteristic::PROPERTY_READ;
}
@@ -251,6 +310,12 @@ bool BluetoothRemoteGattCharacteristicMac::IsWritable() const {
(properties & PROPERTY_WRITE_WITHOUT_RESPONSE);
}
+bool BluetoothRemoteGattCharacteristicMac::SupportsNotificationsOrIndications()
+ const {
+ BluetoothGattCharacteristic::Properties properties = GetProperties();
+ return (properties & PROPERTY_NOTIFY) || (properties & PROPERTY_INDICATE);
+}
+
CBCharacteristicWriteType BluetoothRemoteGattCharacteristicMac::GetCBWriteType()
const {
return (GetProperties() & BluetoothGattCharacteristic::PROPERTY_WRITE)

Powered by Google App Engine
This is Rietveld 408576698