[Bluetooth] Fix writeValue method from BluetoothGATT 84/244184/1
authorRafal Walczyna <r.walczyna@samsung.com>
Tue, 15 Sep 2020 13:27:31 +0000 (15:27 +0200)
committerRafal Walczyna <r.walczyna@samsung.com>
Tue, 15 Sep 2020 14:33:25 +0000 (16:33 +0200)
There was an exception when user has not provided callbacks parameters.
successCallback and errorCallback was set to undefined, what caused
validator to fail.

Added array validate to toDOMString method of BluetoothManager

[Verification] tct-bluetooth auto: 100% pass
BluetoothGATTCombined: 100% pass

Change-Id: If0aa54fee7f19850d3bc94d8936476c0b322dc39
Signed-off-by: Rafal Walczyna <r.walczyna@samsung.com>
src/bluetooth/bluetooth_api.js

index 9805955..5608e2e 100755 (executable)
@@ -832,7 +832,7 @@ BluetoothLEDevice.prototype.getAttMtu = function() {
         throw native.getErrorObject(result);
     }
     return native.getResultObject(result);
-}
+};
 
 BluetoothLEDevice.prototype.requestAttMtuChange = function() {
     privUtils_.log('Entered BluetoothLEDevice.requestAttMtuChange()');
@@ -854,7 +854,7 @@ BluetoothLEDevice.prototype.requestAttMtuChange = function() {
         throw native.getErrorObject(result);
     }
     return native.getResultObject(result);
-}
+};
 
 BluetoothLEDevice.prototype.addAttMtuChangeListener = function() {
     privUtils_.log('Entered BluetoothLEDevice.addAttMtuChangeListener()');
@@ -2173,23 +2173,20 @@ var BluetoothGATTCharacteristic = function(data, address) {
 
     this.writeValue = function(value, successCallback, errorCallback) {
         privUtils_.log('Entered BluetoothGATTCharacteristic.writeValue()');
-        var args = AV.validateArgs(
-            [successCallback, errorCallback],
-            [
-                {
-                    name: 'successCallback',
-                    type: AV.Types.FUNCTION,
-                    optional: true,
-                    nullable: true
-                },
-                {
-                    name: 'errorCallback',
-                    type: AV.Types.FUNCTION,
-                    optional: true,
-                    nullable: true
-                }
-            ]
-        );
+        var args = AV.validateArgs(Array.prototype.slice.call(arguments, 1), [
+            {
+                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)) {
@@ -2601,7 +2598,7 @@ BluetoothGATTServerCharacteristic.prototype.setReadValueRequestCallback = functi
                 }
             );
             _BluetoothGATTServerReadWriteValueRequestCallbacks[
-                "ReadValueCallback" + characteristicId
+                'ReadValueCallback' + characteristicId
             ] = readValueRequestCallback;
             native.callIfPossible(args.successCallback, native.getErrorObject(result));
         }
@@ -2726,7 +2723,6 @@ var _bluetoothGATTCharacteristicListener = _multipleListenerBuilder(
     true
 );
 
-
 /*
  * This object is used by:
  * - BluetoothLEDevice.addConnectStateChangeListener()
@@ -2796,23 +2792,20 @@ var BluetoothGATTDescriptor = function(data, address) {
 
     this.writeValue = function(value, successCallback, errorCallback) {
         privUtils_.log('Entered BluetoothGATTDescriptor.writeValue()');
-        var args = AV.validateArgs(
-            [successCallback, errorCallback],
-            [
-                {
-                    name: 'successCallback',
-                    type: AV.Types.FUNCTION,
-                    optional: true,
-                    nullable: true
-                },
-                {
-                    name: 'errorCallback',
-                    type: AV.Types.FUNCTION,
-                    optional: true,
-                    nullable: true
-                }
-            ]
-        );
+        var args = AV.validateArgs(Array.prototype.slice.call(arguments, 1), [
+            {
+                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)) {
@@ -3770,8 +3763,10 @@ BluetoothGATTServer.prototype.start = function() {
 
     var servicesUnregisteredInNativeLayer = [];
     for (var i = 0; i < this.services.length; ++i) {
-        if(this.services[i]) {
-            if (!_BluetoothGATTServerServicesRegisteredInNativeLayer[this.services[i]._id]) {
+        if (this.services[i]) {
+            if (
+                !_BluetoothGATTServerServicesRegisteredInNativeLayer[this.services[i]._id]
+            ) {
                 servicesUnregisteredInNativeLayer.push(this.services[i]);
             }
         }
@@ -3917,8 +3912,15 @@ BluetoothGATTServer.prototype.stop = function() {
     }
 };
 
-var BluetoothGATTServer_valid_getConnectionMtu_errors = ['InvalidStateError', 'NotSupportedError', 'UnknownError'];
-var BluetoothGATTServer_valid_getConnectionMtu_exceptions = ['TypeMismatchError', 'SecurityError'];
+var BluetoothGATTServer_valid_getConnectionMtu_errors = [
+    'InvalidStateError',
+    'NotSupportedError',
+    'UnknownError'
+];
+var BluetoothGATTServer_valid_getConnectionMtu_exceptions = [
+    'TypeMismatchError',
+    'SecurityError'
+];
 
 BluetoothGATTServer.prototype.getConnectionMtu = function() {
     privUtils_.log('Entered BluetoothGATTServer.getConnectionMtu()');
@@ -3940,19 +3942,30 @@ BluetoothGATTServer.prototype.getConnectionMtu = function() {
 
     var callback = function(result) {
         if (native.isFailure(result)) {
-            native.callIfPossible(args.errorCallback,
-                                  native.getErrorObjectAndValidate(result,
-                                    BluetoothGATTServer_valid_getConnectionMtu_errors, UnknownError));
+            native.callIfPossible(
+                args.errorCallback,
+                native.getErrorObjectAndValidate(
+                    result,
+                    BluetoothGATTServer_valid_getConnectionMtu_errors,
+                    UnknownError
+                )
+            );
         } else {
             args.successCallback(native.getResultObject(result));
         }
     };
 
-    var result = native.call('BluetoothGATTServerGetConnectionMtu',
-                             {clientAddress: args.clientAddress}, callback);
+    var result = native.call(
+        'BluetoothGATTServerGetConnectionMtu',
+        { clientAddress: args.clientAddress },
+        callback
+    );
     if (native.isFailure(result)) {
-        throw native.getErrorObjectAndValidate(result, BluetoothGATTServer_valid_getConnectionMtu_exceptions,
-                                               UnknownError);
+        throw native.getErrorObjectAndValidate(
+            result,
+            BluetoothGATTServer_valid_getConnectionMtu_exceptions,
+            UnknownError
+        );
     }
 };
 
@@ -4088,9 +4101,16 @@ var BluetoothManager_toDOMString = function(data) {
             data = '0x' + data;
         }
         return data;
-    } else {
-        return BluetoothManager_byteArrayToHexString(numberArrayToByteArray(data));
     }
+    try {
+        data = numberArrayToByteArray(data);
+    } catch (err) {
+        throw new WebAPIException(
+            WebAPIException.TYPE_MISMATCH_ERR,
+            'argument is not a valid Bytes type'
+        );
+    }
+    return BluetoothManager_byteArrayToHexString(data);
 };
 
 BluetoothManager.prototype.toDOMString = function(data) {