[Bluetooth] Add validators for BluetoothGATTServer*InitData objects 08/240708/15
authorPawel Wasowski <p.wasowski2@samsung.com>
Wed, 12 Aug 2020 10:47:26 +0000 (12:47 +0200)
committerDawid Juszczak <d.juszczak@samsung.com>
Tue, 25 Aug 2020 13:01:26 +0000 (15:01 +0200)
This commit adds ValidateBluetoothGATT*Init() functions, that validate
BluetoothGATT{Service, Characteristic, Descriptor}Init objects and fill
optional fields with default values.

Verification: webapi-plugins build succeeds. Instantiating
tizen.bluetooth in Chrome Dev Tools succeeds.
The code will be further verified together with the code that relies on
it. Verification results will be reported in the next commits.

Change-Id: I1fc2017a8c970bd24a3cc3ef0a0c85ca7faf7569
Signed-off-by: Pawel Wasowski <p.wasowski2@samsung.com>
src/bluetooth/bluetooth_api.js

index fa28ef2aad25749b17968b07e3a2d79ca43b2dd7..eed468acba0755a3b212532c785788ce40b1ee88 100755 (executable)
@@ -1619,6 +1619,7 @@ var BluetoothGATTService = function(data, address) {
     var serviceUuid_ = data.serviceUuid;
     //address_ is needed to control if device is still connected
     var address_ = address || data.address;
+
     function servicesGetter() {
         var services = [];
         var result = native.callSync('BluetoothGATTClientServiceGetServices', {
@@ -1685,9 +1686,54 @@ function NextGattServerEntityID() {
     return ++CurrentGATTServerEntityId;
 }
 
+function IsUuidValid(uuid) {
+    return (
+        BluetoothManager_UUIDIsValid16Bit(uuid) ||
+        BluetoothManager_UUIDIsValid32Bit(uuid) ||
+        BluetoothManager_UUIDIsValid128Bit(uuid)
+    );
+}
+
+var ValidateBluetoothGATTServerServiceInit = function(initData) {
+    initData = AV.validateArgs(
+        [
+            initData['serviceUuid'],
+            initData['isPrimary'] || true,
+            initData['includedServices'] || [],
+            initData['characteristics'] || []
+        ],
+        [
+            {
+                name: 'serviceUuid',
+                type: AV.Types.STRING,
+                validator: IsUuidValid
+            },
+            {
+                name: 'isPrimary',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'includedServices',
+                type: AV.Types.ARRAY
+            },
+            {
+                name: 'characteristics',
+                type: AV.Types.ARRAY
+            }
+        ]
+    );
+
+    // "uuid" field is used to construct BluetoothGATTService, but it's not a part
+    // of BluetoothGATTServerServiceInit dictionary.
+    // In case of BluetoothGATTServerServices, its value is always the same as
+    // serviceUuid's.
+    initData.uuid = initData.serviceUuid;
+    return initData;
+};
+
 //class BluetoothGATTServerService ///////////////////////////
 var BluetoothGATTServerService = function(data) {
-    // TODO: validate data and set default values
+    data = ValidateBluetoothGATTServerServiceInit(data);
 
     BluetoothGATTService.call(this, data, null);
 
@@ -1964,9 +2010,111 @@ var BluetoothGATTCharacteristic = function(data, address) {
     };
 };
 
+var ValidateBluetoothGATTServerCharacteristicInit = function(initData) {
+    return AV.validateArgs(
+        [
+            initData['uuid'],
+            initData['descriptors'] || [],
+            initData['isBroadcast'] || false,
+            initData['hasExtendedProperties'] || false,
+            initData['isNotify'] || false,
+            initData['isIndication'] || false,
+            initData['isReadable'] || false,
+            initData['isSignedWrite'] || false,
+            initData['isWritable'] || false,
+            initData['isWriteNoResponse'] || false,
+            initData['readPermission'] || false,
+            initData['writePermission'] || false,
+            initData['encryptedReadPermission'] || false,
+            initData['encryptedWritePermission'] || false,
+            initData['encryptedSignedReadPermission'] || false,
+            initData['encryptedSignedWritePermission'] || false,
+            initData['readValueRequestCallback'] || null,
+            initData['writeValueRequestCallback'] || null
+        ],
+        [
+            {
+                name: 'uuid',
+                type: AV.Types.STRING,
+                validator: IsUuidValid
+            },
+            {
+                name: 'descriptors',
+                type: AV.Types.ARRAY
+            },
+            {
+                name: 'isBroadcast',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'hasExtendedProperties',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'isNotify',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'isIndication',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'isReadable',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'isSignedWrite',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'isWritable',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'isWriteNoResponse',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'readPermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'writePermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedReadPermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedWritePermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedSignedReadPermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedSignedWritePermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'readValueRequestCallback',
+                type: AV.Types.FUNCTION,
+                nullable: true
+            },
+            {
+                name: 'writeValueRequestCallback',
+                type: AV.Types.FUNCTION,
+                nullable: true
+            }
+        ]
+    );
+};
+
 //class BluetoothGATTServerCharacteristic ///////////////////////////
 var BluetoothGATTServerCharacteristic = function(data) {
-    // TODO: validate data and set default values
+    data = ValidateBluetoothGATTServerCharacteristicInit(data);
 
     BluetoothGATTCharacteristic.call(this, data, null);
 
@@ -2262,15 +2410,72 @@ var BluetoothGATTDescriptor = function(data, address) {
         uuid: {
             enumerable: true,
             get: function() {
-              return uuid_;
+                return uuid_;
             },
-            set: function() {},
+            set: function() {}
         }
     });
 };
 
+var ValidateBluetoothGATTServerDescriptorInit = function(initData) {
+    return AV.validateArgs(
+        [
+            initData['uuid'],
+            initData['readPermission'] || false,
+            initData['writePermission'] || false,
+            initData['encryptedReadPermission'] || false,
+            initData['encryptedWritePermission'] || false,
+            initData['encryptedSignedReadPermission'] || false,
+            initData['encryptedSignedWritePermission'] || false,
+            initData['readValueRequestCallback'] || null,
+            initData['writeValueRequestCallback'] || null
+        ],
+        [
+            {
+                name: 'uuid',
+                type: AV.Types.STRING,
+                validator: IsUuidValid
+            },
+            {
+                name: 'readPermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'writePermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedReadPermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedWritePermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedSignedReadPermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'encryptedSignedWritePermission',
+                type: AV.Types.BOOLEAN
+            },
+            {
+                name: 'readValueRequestCallback',
+                type: AV.Types.FUNCTION,
+                nullable: true
+            },
+            {
+                name: 'writeValueRequestCallback',
+                type: AV.Types.FUNCTION,
+                nullable: true
+            }
+        ]
+    );
+};
+
 var BluetoothGATTServerDescriptor = function(data, address) {
-    // TODO: validate data and set default values
+    data = ValidateBluetoothGATTServerDescriptorInit(data);
 
     BluetoothGATTDescriptor.call(this, data, null);