From 83a20a3c8c4c698569f8bec7b88f37872fef65a1 Mon Sep 17 00:00:00 2001 From: Dawid Juszczak Date: Fri, 18 Sep 2020 17:09:15 +0200 Subject: [PATCH] Implement BluetoothGATTServer::unregisterAllServices() [ACR] https://code.sec.samsung.net/jira/browse/TWDAPI-263 [Verification] Tested manually on chrome console Change-Id: Ice5bfbd3fa4a8da75119db6b8e47849ca4259c26 Signed-off-by: Dawid Juszczak --- src/bluetooth/bluetooth_api.js | 187 +++++++++++++++++++++++++++++++++++------ 1 file changed, 159 insertions(+), 28 deletions(-) diff --git a/src/bluetooth/bluetooth_api.js b/src/bluetooth/bluetooth_api.js index 1e0b5f5..6f506e8 100755 --- a/src/bluetooth/bluetooth_api.js +++ b/src/bluetooth/bluetooth_api.js @@ -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. * -- 2.7.4