From: Piotr Kosko
Date: Fri, 8 May 2015 12:51:46 +0000 (+0200)
Subject: [BluetoothLE] Added implementation of readData
X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~60
X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=f6327485d8a4993abbc2f2e59b149fa774b7d93a;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git
[BluetoothLE] Added implementation of readData
[Feature] Added c++ implementation of method readData for objects
BluetoothGATTDescriptor and BluetoothGATTCharacteristic.
[Verification] Code compiles without errors.
Change-Id: I0d6ea6cc6f10e5890644540bb8dd13da99a38a80
Signed-off-by: Piotr Kosko
---
diff --git a/src/bluetooth/bluetooth_api.js b/src/bluetooth/bluetooth_api.js
index 7b1beb41..4ebaf8e8 100644
--- a/src/bluetooth/bluetooth_api.js
+++ b/src/bluetooth/bluetooth_api.js
@@ -1478,6 +1478,7 @@ var BluetoothGATTService = function(data) {
//class BluetoothGATTCharacteristic ////////////////////////////////////////////////////
var BluetoothGATTCharacteristic = function(data) {
+ var handle_ = data.handle;
var descriptors_ = [];
var isBroadcast_ = false;
var hasExtendedProperties_ = false;
@@ -1576,80 +1577,79 @@ var BluetoothGATTCharacteristic = function(data) {
}
}
});
-};
-
-BluetoothGATTCharacteristic.prototype.readValue = function() {
- console.log('Entered BluetoothGATTCharacteristic.readValue()');
-
- xwalk.utils.checkPrivilegeAccess(Privilege.BLUETOOTH);
-
- var args = AV.validateMethod(arguments, [{
- name: 'successCallback',
- type: AV.Types.FUNCTION
- }, {
- name: 'errorCallback',
- type: AV.Types.FUNCTION,
- optional: true,
- nullable: true
- }]);
-
- var callback = function(result) {
- if (native.isFailure(result)) {
- native.callIfPossible(args.errorCallback, native.getErrorObject(result));
- } else {
- var d = [];
- native.getResultObject(result).forEach(function(b) {
- d.push(Converter.toByte(b));
- });
- args.successCallback(d);
- }
- };
-
- var callArgs = {}; // TODO: add more arguments
-
- var result = native.call('BluetoothGATT_readValue', callArgs, callback);
+ BluetoothGATTCharacteristic.prototype.readValue = function() {
+ console.log('Entered BluetoothGATTCharacteristic.readValue()');
+
+ xwalk.utils.checkPrivilegeAccess(Privilege.BLUETOOTH);
+
+ var args = AV.validateMethod(arguments, [{
+ name: 'successCallback',
+ type: AV.Types.FUNCTION
+ }, {
+ name: 'errorCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ }]);
+
+ var callback = function(result) {
+ if (native.isFailure(result)) {
+ native.callIfPossible(args.errorCallback, native.getErrorObject(result));
+ } else {
+ var d = [];
+ native.getResultObject(result).forEach(function(b) {
+ d.push(Converter.toByte(b));
+ });
+ args.successCallback(d);
+ }
+ };
- if (native.isFailure(result)) {
- throw native.getErrorObject(result);
- }
-};
+ var callArgs = {handle : handle_};
-BluetoothGATTCharacteristic.prototype.writeValue = function() {
- console.log('Entered BluetoothGATTCharacteristic.writeValue()');
+ var result = native.call('BluetoothGATT_readValue', callArgs, callback);
- xwalk.utils.checkPrivilegeAccess(Privilege.BLUETOOTH);
-
- var args = AV.validateMethod(arguments, [{
- name: 'value',
- type: AV.Types.ARRAY,
- values: AV.Types.BYTE
- }, {
- name: 'successCallback',
- type: AV.Types.FUNCTION,
- optional: true,
- nullable: true
- }, {
- name: 'errorCallback',
- type: AV.Types.FUNCTION,
- optional: true,
- nullable: true
- }]);
+ if (native.isFailure(result)) {
+ throw native.getErrorObject(result);
+ }
+ };
- var callback = function(result) {
- if (native.isFailure(result)) {
- native.callIfPossible(args.errorCallback, native.getErrorObject(result));
- } else {
- native.callIfPossible(args.successCallback);
- }
- };
+ BluetoothGATTCharacteristic.prototype.writeValue = function() {
+ console.log('Entered BluetoothGATTCharacteristic.writeValue()');
+
+ xwalk.utils.checkPrivilegeAccess(Privilege.BLUETOOTH);
+
+ var args = AV.validateMethod(arguments, [{
+ name: 'value',
+ type: AV.Types.ARRAY,
+ values: AV.Types.BYTE
+ }, {
+ name: 'successCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ }, {
+ name: 'errorCallback',
+ type: AV.Types.FUNCTION,
+ optional: true,
+ nullable: true
+ }]);
+
+ var callback = function(result) {
+ if (native.isFailure(result)) {
+ native.callIfPossible(args.errorCallback, native.getErrorObject(result));
+ } else {
+ native.callIfPossible(args.successCallback);
+ }
+ };
- var callArgs = { value: args.value }; // TODO: add more arguments
+ var callArgs = { handle : handle_, value: args.value };
- var result = native.call('BluetoothGATT_writeValue', callArgs, callback);
+ var result = native.call('BluetoothGATT_writeValue', callArgs, callback);
- if (native.isFailure(result)) {
- throw native.getErrorObject(result);
- }
+ if (native.isFailure(result)) {
+ throw native.getErrorObject(result);
+ }
+ };
};
/**
diff --git a/src/bluetooth/bluetooth_gatt_service.cc b/src/bluetooth/bluetooth_gatt_service.cc
index 87f4393d..4777f293 100644
--- a/src/bluetooth/bluetooth_gatt_service.cc
+++ b/src/bluetooth/bluetooth_gatt_service.cc
@@ -21,6 +21,7 @@
#include "common/logger.h"
#include "common/platform_result.h"
#include "common/extension.h"
+#include "common/task-queue.h"
#include "bluetooth/bluetooth_instance.h"
#include "bluetooth/bluetooth_util.h"
@@ -30,6 +31,7 @@ namespace bluetooth {
using common::PlatformResult;
using common::ErrorCode;
+using common::TaskQueue;
using namespace common::tools;
namespace {
@@ -37,7 +39,7 @@ const std::string kUuid = "uuid";
const std::string kHandle = "handle";
const std::string kDescriptors = "descriptors";
-const std::string kBroadcast = "broadcast";
+const std::string kBroadcast = "isBroadcast";
const std::string kExtendedProperties = "hasExtendedProperties";
const std::string kNotify = "isNotify";
const std::string kIndication = "isIndication";
@@ -250,5 +252,69 @@ PlatformResult BluetoothGATTService::GetCharacteristicsHelper(bt_gatt_h handle,
return PlatformResult(ErrorCode::NO_ERROR);
}
+void BluetoothGATTService::ReadValue(const picojson::value& args,
+ picojson::object& out) {
+ LoggerD("Entered");
+ const double callback_handle = util::GetAsyncCallbackHandle(args);
+ struct Data {
+ double callback_handle;
+ BluetoothGATTService* service;
+ };
+
+ Data* user_data = new Data{callback_handle, this};
+ bt_gatt_h handle = (bt_gatt_h) static_cast(args.get("handle").get());
+
+ //TODO check if client device is still connected and throw InvalidStateError
+
+ auto read_value = [](int result, bt_gatt_h handle, void *user_data) -> void {
+ Data* data = (Data*) user_data;
+ double callback_handle = data->callback_handle;
+ BluetoothGATTService* service = data->service;
+ delete data;
+
+ PlatformResult ret = PlatformResult(ErrorCode::NO_ERROR);
+
+ picojson::value byte_array = picojson::value(picojson::array());
+ picojson::array& byte_array_obj = byte_array.get();
+
+ if (BT_ERROR_NONE != result) {
+ //TODO handle error
+ } else {
+ char *value = nullptr;
+ int length = 0;
+ int ret = bt_gatt_get_value(handle, &value, &length);
+ if (BT_ERROR_NONE != result) {
+ //TODO handle error
+ }
+
+ for (size_t i = 0 ; i < length; i++) {
+ byte_array_obj.push_back(picojson::value(std::to_string(value[i])));
+ }
+ if (value) {
+ free(value);
+ value = nullptr;
+ }
+ }
+
+ std::shared_ptr response =
+ std::shared_ptr(new picojson::value(picojson::object()));
+ if (ret.IsSuccess()) {
+ ReportSuccess(byte_array, response->get());
+ } else {
+ ReportError(ret, &response->get());
+ }
+ TaskQueue::GetInstance().Async(
+ [service, callback_handle](const std::shared_ptr& response) {
+ service->instance_.SyncResponse(callback_handle, response);
+ }, response);
+ };
+ int ret = bt_gatt_client_read_value(handle, read_value, (void*)user_data);
+ if (BT_ERROR_NONE != ret) {
+ LOGE("Couldn't register callback for read value");
+ //TODO handle error ??
+ }
+ ReportSuccess(out);
+}
+
} // namespace bluetooth
} // namespace extension
diff --git a/src/bluetooth/bluetooth_gatt_service.h b/src/bluetooth/bluetooth_gatt_service.h
index 18be27bf..77c35c83 100644
--- a/src/bluetooth/bluetooth_gatt_service.h
+++ b/src/bluetooth/bluetooth_gatt_service.h
@@ -40,6 +40,7 @@ class BluetoothGATTService {
void GetServices(const picojson::value& data, picojson::object& out);
void GetCharacteristics(const picojson::value& data, picojson::object& out);
+ void ReadValue(const picojson::value& args, picojson::object& out);
private:
static common::PlatformResult GetServicesHelper(bt_gatt_h handle, const std::string& uuid,
diff --git a/src/bluetooth/bluetooth_instance.cc b/src/bluetooth/bluetooth_instance.cc
index d665fc06..29f82008 100644
--- a/src/bluetooth/bluetooth_instance.cc
+++ b/src/bluetooth/bluetooth_instance.cc
@@ -136,6 +136,8 @@ BluetoothInstance::BluetoothInstance() :
std::bind(&BluetoothGATTService::GetServices, &bluetooth_gatt_service_, _1, _2));
REGISTER_SYNC("BluetoothGATTService_getCharacteristics",
std::bind(&BluetoothGATTService::GetCharacteristics, &bluetooth_gatt_service_, _1, _2));
+ REGISTER_SYNC("BluetoothGATT_readValue",
+ std::bind(&BluetoothGATTService::ReadValue, &bluetooth_gatt_service_, _1, _2));
#undef REGISTER_ASYNC
#undef REGISTER_SYNC