From 1cc9a1b58d8505c5080c41f71c59d8d68d7fd21c Mon Sep 17 00:00:00 2001 From: Pawel Wasowski Date: Wed, 12 Aug 2020 13:19:08 +0200 Subject: [PATCH] [Bluetooth] Add BluetoothGATTServer{Service, Characteristic, Descriptor} This commit adds 3 interfaces, used to represent GATT objects exposed by the local server. [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: I8c5dd6a6da6d213518d0eb46d3bb0ca3e37b4629 Signed-off-by: Pawel Wasowski --- src/bluetooth/bluetooth_api.js | 262 ++++++++++++++++++++++++++++++++- 1 file changed, 261 insertions(+), 1 deletion(-) diff --git a/src/bluetooth/bluetooth_api.js b/src/bluetooth/bluetooth_api.js index df42559a..fa28ef2a 100755 --- a/src/bluetooth/bluetooth_api.js +++ b/src/bluetooth/bluetooth_api.js @@ -1650,18 +1650,87 @@ var BluetoothGATTService = function(data, address) { } return characteristics; } + + /* + * If this function is called as a constructor of BluetoothGATTServerService's + * base class, some properties have to be left configurable, to enable redefinition. + * Otherwise, if this function is called to create BluetoothGATTService, + * they are left non-configurable. + * + * If BluetoothGATTService will be a base for any other class in the future, + * the line below may have to be updated to take into account the new class. + */ + var isConfigurable = this instanceof BluetoothGATTServerService; + Object.defineProperties(this, { uuid: { value: uuid_, writable: false, enumerable: true }, serviceUuid: { value: serviceUuid_, writable: false, enumerable: true }, - services: { enumerable: true, set: function() {}, get: servicesGetter }, + services: { + enumerable: true, + configurable: isConfigurable, + set: function() {}, + get: servicesGetter + }, characteristics: { enumerable: true, + configurable: isConfigurable, set: function() {}, get: characteristicsGetter } }); }; +var CurrentGATTServerEntityId = 0; +function NextGattServerEntityID() { + return ++CurrentGATTServerEntityId; +} + +//class BluetoothGATTServerService /////////////////////////// +var BluetoothGATTServerService = function(data) { + // TODO: validate data and set default values + + BluetoothGATTService.call(this, data, null); + + var services_ = data.includedServices.map(function(serviceData) { + return new BluetoothGATTServerService(serviceData, null); + }); + var characteristics_ = data.characteristics.map(function(characteristicData) { + return new BluetoothGATTServerCharacteristic(characteristicData, null); + }); + + Object.defineProperties(this, { + isPrimary: { value: data.isPrimary, writable: false, enumerable: true }, + services: { + enumerable: true, + get: function() { + return services_.slice(); + }, + set: function() {} + }, + characteristics: { + enumerable: true, + get: function() { + return characteristics_.slice(); + }, + set: function() {} + }, + // This property is "private" and meant not to be used by users + _id: { + // It has to be enumerable, to be serialized with JSON.stringify() + enumerable: true, + value: NextGattServerEntityID() + } + }); +}; + +BluetoothGATTServerService.prototype = Object.create(BluetoothGATTService.prototype); + +Object.defineProperty(BluetoothGATTServerService.prototype, 'constructor', { + value: BluetoothGATTServerService, + enumerable: false, + writable: true +}); + var numberArrayToByteArray = function(array) { var d = []; @@ -1691,9 +1760,21 @@ var BluetoothGATTCharacteristic = function(data, address) { var isWriteNoResponse_ = data.isWriteNoResponse; var uuid_ = data.uuid; + /* + * If this function is called as a constructor of BluetoothGATTServerCharacteristic's + * base class, some properties have to be left configurable, to enable redefinition. + * Otherwise, if this function is called to create BluetoothGATTCharacteristic, + * they are left non-configurable. + * + * If BluetoothGATTCharacteristic will be a base for any other class in the future, + * the line below may have to be updated to take into account the new class. + */ + var isConfigurable = this instanceof BluetoothGATTServerCharacteristic; + Object.defineProperties(this, { descriptors: { enumerable: true, + configurable: isConfigurable, get: function() { return descriptors_.slice(); }, @@ -1883,6 +1964,106 @@ var BluetoothGATTCharacteristic = function(data, address) { }; }; +//class BluetoothGATTServerCharacteristic /////////////////////////// +var BluetoothGATTServerCharacteristic = function(data) { + // TODO: validate data and set default values + + BluetoothGATTCharacteristic.call(this, data, null); + + var descriptors_ = data.descriptors.map(function(descriptor_data) { + return new BluetoothGATTServerDescriptor(descriptor_data, null); + }); + + Object.defineProperties(this, { + descriptors: { + enumerable: true, + get: function() { + return descriptors_.slice(); + }, + set: function() {} + }, + readPermission: { + enumerable: true, + get: function() { + return data.readPermission; + }, + set: function() {} + }, + writePermission: { + enumerable: true, + get: function() { + return data.writePermission; + }, + set: function() {} + }, + encryptedReadPermission: { + enumerable: true, + get: function() { + return data.encryptedReadPermission; + }, + set: function() {} + }, + encryptedWritePermission: { + enumerable: true, + get: function() { + return data.encryptedWritePermission; + }, + set: function() {} + }, + encryptedSignedReadPermission: { + enumerable: true, + get: function() { + return data.encryptedSignedReadPermission; + }, + set: function() {} + }, + encryptedSignedWritePermission: { + enumerable: true, + get: function() { + return data.encryptedSignedWritePermission; + }, + set: function() {} + }, + // This property is "private" and meant not to be used by users + _id: { + // It has to be enumerable, to be serialized with JSON.stringify() + enumerable: true, + value: NextGattServerEntityID() + } + }); + + this.readValue = function() { + throw new WebAPIException( + 'NotSupportedError', + 'This method cannot be called on BluetoothGATTServerCharacteristic' + ); + }; + + this.writeValue = function() { + throw new WebAPIException( + 'NotSupportedError', + 'This method cannot be called on BluetoothGATTServerCharacteristic' + ); + }; + + this.addValueChangeListener = function() { + throw new WebAPIException( + 'NotSupportedError', + 'This method cannot be called on BluetoothGATTServerCharacteristic' + ); + }; + + this.removeValueChangeListener = function() { /* Intended no operation */ }; +}; + +BluetoothGATTServerCharacteristic.prototype = Object.create(BluetoothGATTCharacteristic.prototype); + +Object.defineProperty(BluetoothGATTServerCharacteristic.prototype, 'constructor', { + value: BluetoothGATTServerCharacteristic, + enumerable: false, + writable: true +}); + /** * Creates a manager for specified listener event. Manager handles multiple * registered listeners @@ -2088,6 +2269,85 @@ var BluetoothGATTDescriptor = function(data, address) { }); }; +var BluetoothGATTServerDescriptor = function(data, address) { + // TODO: validate data and set default values + + BluetoothGATTDescriptor.call(this, data, null); + + Object.defineProperties(this, { + readPermission: { + enumerable: true, + get: function() { + return data.readPermission; + }, + set: function() {} + }, + writePermission: { + enumerable: true, + get: function() { + return data.writePermission; + }, + set: function() {} + }, + encryptedReadPermission: { + enumerable: true, + get: function() { + return data.encryptedReadPermission; + }, + set: function() {} + }, + encryptedWritePermission: { + enumerable: true, + get: function() { + return data.encryptedWritePermission; + }, + set: function() {} + }, + encryptedSignedReadPermission: { + enumerable: true, + get: function() { + return data.encryptedSignedReadPermission; + }, + set: function() {} + }, + encryptedSignedWritePermission: { + enumerable: true, + get: function() { + return data.encryptedSignedWritePermission; + }, + set: function() {} + }, + // This property is "private" and meant not to be used by users + _id: { + // It has to be enumerable, to be serialized with JSON.stringify() + enumerable: true, + value: NextGattServerEntityID() + } + }); + + this.readValue = function() { + throw new WebAPIException( + 'NotSupportedError', + 'This method cannot be called on BluetoothGATTServerDescriptor' + ); + }; + + this.writeValue = function() { + throw new WebAPIException( + 'NotSupportedError', + 'This method cannot be called on BluetoothGATTServerDescriptor' + ); + }; +}; + +BluetoothGATTServerDescriptor.prototype = Object.create(BluetoothGATTDescriptor.prototype); + +Object.defineProperty(BluetoothGATTServerDescriptor.prototype, 'constructor', { + value: BluetoothGATTServerDescriptor, + enumerable: false, + writable: true +}); + // class BluetoothAdapter /////////////////////////// var BluetoothAdapter = function() { function nameGetter() { -- 2.34.1