Implement BluetoothGATTServer::unregisterAllServices() 74/244474/4
authorDawid Juszczak <d.juszczak@samsung.com>
Fri, 18 Sep 2020 15:09:15 +0000 (17:09 +0200)
committerDawid Juszczak <d.juszczak@samsung.com>
Mon, 21 Sep 2020 10:00:47 +0000 (10:00 +0000)
[ACR]
https://code.sec.samsung.net/jira/browse/TWDAPI-263

[Verification]
Tested manually on chrome console

Change-Id: Ice5bfbd3fa4a8da75119db6b8e47849ca4259c26
Signed-off-by: Dawid Juszczak <d.juszczak@samsung.com>
src/bluetooth/bluetooth_api.js

index 1e0b5f5..6f506e8 100755 (executable)
@@ -2490,34 +2490,31 @@ var BluetoothGATTServerCharacteristic = function(data) {
         notificationCB,
         errorCB
     ) {
-        var args = AV.validateArgs(
-            Array.prototype.slice.call(arguments, 1),
-            [
-                {
-                    name: 'clientAddress',
-                    type: AV.Types.STRING,
-                    optional: true,
-                    nullable: true
-                },
-                {
-                    name: 'notificationCB',
-                    type: AV.Types.LISTENER,
-                    values: [
-                        'onnotificationsuccess',
-                        'onnotificationfail',
-                        'onnotificationfinish'
-                    ],
-                    optional: true,
-                    nullable: true
-                },
-                {
-                    name: 'errorCB',
-                    type: AV.Types.FUNCTION,
-                    optional: true,
-                    nullable: true
-                }
-            ]
-        );
+        var args = AV.validateArgs(Array.prototype.slice.call(arguments, 1), [
+            {
+                name: 'clientAddress',
+                type: AV.Types.STRING,
+                optional: true,
+                nullable: true
+            },
+            {
+                name: 'notificationCB',
+                type: AV.Types.LISTENER,
+                values: [
+                    'onnotificationsuccess',
+                    'onnotificationfail',
+                    'onnotificationfinish'
+                ],
+                optional: true,
+                nullable: true
+            },
+            {
+                name: 'errorCB',
+                type: AV.Types.FUNCTION,
+                optional: true,
+                nullable: true
+            }
+        ]);
 
         var callArgs = {
             value: BluetoothManager_toByteArray(value),
@@ -3983,6 +3980,140 @@ BluetoothGATTServer.prototype.registerService = function() {
     }
 };
 
+var BluetoothGATTServer_valid_unregisterAllServices_errors = [
+    'InvalidStateError',
+    'AbortError'
+];
+
+var BluetoothGATTServer_valid_unregisterAllServices_exceptions = [
+    'TypeMismatchError',
+    'SecurityError'
+];
+
+BluetoothGATTServer.prototype.unregisterAllServices = function() {
+    privUtils_.log('Entered BluetoothGATTServer.unregisterAllServices()');
+    var args = AV.validateArgs(arguments, [
+        {
+            name: 'successCallback',
+            type: AV.Types.FUNCTION,
+            optional: true,
+            nullable: true
+        },
+        {
+            name: 'errorCallback',
+            type: AV.Types.FUNCTION,
+            optional: true,
+            nullable: true
+        }
+    ]);
+
+    var servicesToUnregister = [];
+    for (var i = 0; i < _BluetoothGATTServerServices.length; ++i) {
+        if (!T.isNullOrUndefined(_BluetoothGATTServerServices[i]._id)) {
+            servicesToUnregister.push(_BluetoothGATTServerServices[i]);
+        }
+    }
+
+    if (servicesToUnregister.length) {
+        privUtils_.log(
+            'Number of services to unregister: ' + servicesToUnregister.length
+        );
+        var unregisterServiceCallbacksAggregator = new ResultCallbacksAggregator(
+            servicesToUnregister.length,
+            function onAllSucceeded() {
+                _BluetoothGATTServerServices = [];
+                _BluetoothGATTServerServicesRegisteredInNativeLayer = [];
+                native.callIfPossible(args.successCallback);
+            },
+            function onFailure(error) {
+                native.callIfPossible(
+                    args.errorCallback,
+                    native.getErrorObjectAndValidate(
+                        error,
+                        BluetoothGATTServer_valid_unregisterAllServices_errors,
+                        AbortError
+                    )
+                );
+            }
+        );
+
+        for (var i = 0; i < servicesToUnregister.length; ++i) {
+            var serviceIndex = _BluetoothGATTServerServices.findIndex(function(service) {
+                return service._id === servicesToUnregister[i]._id;
+            });
+
+            var serviceId = servicesToUnregister[i]._id;
+
+            var unregisterServiceCallback = function(result) {
+                if (native.isFailure(result)) {
+                    unregisterServiceCallbacksAggregator.errorCallback(
+                        native.getErrorObjectAndValidate(
+                            result,
+                            BluetoothGATTServer_valid_unregisterAllServices_errors,
+                            AbortError
+                        )
+                    );
+                } else {
+                    delete _BluetoothGATTServerServicesRegisteredInNativeLayer[serviceId];
+                    _BluetoothGATTServerServices.splice(serviceIndex, 1);
+                    unregisterServiceCallbacksAggregator.successCallback();
+                }
+            };
+
+            var callArgs = {
+                _id: servicesToUnregister[i]._id,
+                idsToRemoveFromNativeLayer: _getIncludedServicesAndItsComponentsIdsRecursively(
+                    servicesToUnregister[i]
+                )
+            };
+
+            var result = native.call(
+                'BluetoothGATTServerUnregisterService',
+                callArgs,
+                unregisterServiceCallback
+            );
+
+            if (native.isFailure(result)) {
+                throw native.getErrorObjectAndValidate(
+                    result,
+                    BluetoothGATTServer_valid_unregisterAllServices_exceptions,
+                    AbortError
+                );
+            }
+        }
+    } else {
+        privUtils_.log(
+            'Nothing registered in native layer, calling BluetoothGATTServer.stop()'
+        );
+
+        var callback = function(result) {
+            if (native.isFailure(result)) {
+                native.callIfPossible(
+                    args.errorCallback,
+                    native.getErrorObjectAndValidate(
+                        result,
+                        BluetoothGATTServer_valid_unregisterAllServices_errors,
+                        AbortError
+                    )
+                );
+            } else {
+                _BluetoothGATTServerServices = [];
+                _BluetoothGATTServerServicesRegisteredInNativeLayer = {};
+                native.callIfPossible(args.successCallback);
+            }
+        };
+
+        var result = native.call('BluetoothGATTServerStop', {}, callback);
+        if (native.isFailure(result)) {
+            throw native.getErrorObjectAndValidate(
+                result,
+                BluetoothGATTServer_valid_unregisterAllServices_exceptions,
+                AbortError
+            );
+        }
+    }
+};
+
 /*
  * Objects of this class are used to wait for multiple callbacks results.
  *