Bug 1181482 - Patch2: Revise read/write characteristic/descriptor value to cover both GATT client and GATT server role. r=btian, r=bz

This commit is contained in:
Jocelyn Liu 2015-09-23 14:16:05 +08:00
parent df333a9a50
commit 51f4f018c4
4 changed files with 42 additions and 2 deletions

View File

@ -350,6 +350,11 @@ BluetoothGattCharacteristic::ReadValue(ErrorResult& aRv)
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
if (mAttRole == ATT_SERVER_ROLE) {
promise->MaybeResolve(mValue);
return promise.forget();
}
BT_ENSURE_TRUE_REJECT(mProperties & GATT_CHAR_PROP_BIT_READ,
promise,
NS_ERROR_NOT_AVAILABLE);
@ -377,6 +382,16 @@ BluetoothGattCharacteristic::WriteValue(const ArrayBuffer& aValue,
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
aValue.ComputeLengthAndData();
if (mAttRole == ATT_SERVER_ROLE) {
mValue.Clear();
mValue.AppendElements(aValue.Data(), aValue.Length());
promise->MaybeResolve(JS::UndefinedHandleValue);
return promise.forget();
}
BT_ENSURE_TRUE_REJECT(mProperties &
(GATT_CHAR_PROP_BIT_WRITE_NO_RESPONSE |
GATT_CHAR_PROP_BIT_WRITE |
@ -384,8 +399,6 @@ BluetoothGattCharacteristic::WriteValue(const ArrayBuffer& aValue,
promise,
NS_ERROR_NOT_AVAILABLE);
aValue.ComputeLengthAndData();
nsTArray<uint8_t> value;
value.AppendElements(aValue.Data(), aValue.Length());

View File

@ -232,6 +232,11 @@ BluetoothGattDescriptor::ReadValue(ErrorResult& aRv)
nsRefPtr<Promise> promise = Promise::Create(global, aRv);
NS_ENSURE_TRUE(!aRv.Failed(), nullptr);
if (mAttRole == ATT_SERVER_ROLE) {
promise->MaybeResolve(mValue);
return promise.forget();
}
BluetoothService* bs = BluetoothService::Get();
BT_ENSURE_TRUE_REJECT(bs, promise, NS_ERROR_NOT_AVAILABLE);
@ -260,6 +265,14 @@ BluetoothGattDescriptor::WriteValue(
aValue.ComputeLengthAndData();
if (mAttRole == ATT_SERVER_ROLE) {
mValue.Clear();
mValue.AppendElements(aValue.Data(), aValue.Length());
promise->MaybeResolve(JS::UndefinedHandleValue);
return promise.forget();
}
nsTArray<uint8_t> value;
value.AppendElements(aValue.Data(), aValue.Length());

View File

@ -48,6 +48,13 @@ interface BluetoothGattCharacteristic
[Cached, Constant]
readonly attribute GattCharacteristicProperties properties;
/**
* Read or write the value of this characteristic.
*
* If this charactersitic is in the client role, the value will be
* read from or written to the remote GATT server. Otherwise, the local value
* will be read/written.
*/
[NewObject]
Promise<ArrayBuffer> readValue();
[NewObject]

View File

@ -18,6 +18,13 @@ interface BluetoothGattDescriptor
[Cached, Constant]
readonly attribute GattPermissions permissions;
/**
* Read or write the value of this descriptor.
*
* If this descriptor is in the client role, the value will be read from or
* written to the remote GATT server. Otherwise, the local value will be
* read/written.
*/
[NewObject]
Promise<ArrayBuffer> readValue();
[NewObject]