[BluetoothLE] Added implementation of readData
authorPiotr Kosko <p.kosko@samsung.com>
Fri, 8 May 2015 12:51:46 +0000 (14:51 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Mon, 11 May 2015 09:52:24 +0000 (18:52 +0900)
[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 <p.kosko@samsung.com>
src/bluetooth/bluetooth_api.js
src/bluetooth/bluetooth_gatt_service.cc
src/bluetooth/bluetooth_gatt_service.h
src/bluetooth/bluetooth_instance.cc

index 7b1beb418b269306c9d3b86c1edd53ccd4c0e6c0..4ebaf8e819b2b9f37cb686b0b232596ef12fbfac 100644 (file)
@@ -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);
+      }
+    };
 };
 
 /**
index 87f4393d715eb181a73ca1259439629823afee67..4777f293cf600264035b63bfd68761d840dd9056 100644 (file)
@@ -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<long>(args.get("handle").get<double>());
+
+  //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<picojson::array>();
+
+    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<picojson::value> response =
+        std::shared_ptr<picojson::value>(new picojson::value(picojson::object()));
+    if (ret.IsSuccess()) {
+      ReportSuccess(byte_array, response->get<picojson::object>());
+    } else {
+      ReportError(ret, &response->get<picojson::object>());
+    }
+    TaskQueue::GetInstance().Async<picojson::value>(
+        [service, callback_handle](const std::shared_ptr<picojson::value>& 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
index 18be27bf70b24486886a215339b64dd5d78bd217..77c35c8388a1a423a3b6df2a8fb5ca2821696ce2 100644 (file)
@@ -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,
index d665fc06ccd54b492b9eaa82f314324ef36d2ff5..29f8200848bcf41615055cdde238e7e3003538b9 100644 (file)
@@ -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