From 3a1ffa9c50b0e546ca2170706c0ef0c9148f87a5 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Sun, 1 May 2016 20:07:30 -0400 Subject: [PATCH] - only allow characteristics with value to be read only (#194) --- README.md | 2 +- lib/characteristic.js | 4 ++++ test/test-characteristic.js | 10 ++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f951c4e9..1a562c94 100644 --- a/README.md +++ b/README.md @@ -174,7 +174,7 @@ var characteristic = new Characteristic({ uuid: 'fffffffffffffffffffffffffffffff1', // or 'fff1' for 16-bit properties: [ ... ], // can be a combination of 'read', 'write', 'writeWithoutResponse', 'notify', 'indicate' secure: [ ... ], // enable security for properties, can be a combination of 'read', 'write', 'writeWithoutResponse', 'notify', 'indicate' - value: null, // optional static value, must be of type Buffer + value: null, // optional static value, must be of type Buffer - for read only characteristics descriptors: [ // see Descriptor for data type ], diff --git a/lib/characteristic.js b/lib/characteristic.js index cfbb680a..60d43a31 100644 --- a/lib/characteristic.js +++ b/lib/characteristic.js @@ -12,6 +12,10 @@ function Characteristic(options) { this.value = options.value || null; this.descriptors = options.descriptors || []; + if (this.value && (this.properties.length !== 1 || this.properties[0] !== 'read')) { + throw new Error('Characteristics with value can be read only!'); + } + if (options.onReadRequest) { this.onReadRequest = options.onReadRequest; } diff --git a/test/test-characteristic.js b/test/test-characteristic.js index ce641280..9f8975b4 100644 --- a/test/test-characteristic.js +++ b/test/test-characteristic.js @@ -56,12 +56,22 @@ describe('Characteristic', function() { it('should create with value option', function() { var characteristic = new Characteristic({ + properties: ['read'], value: mockValue }); characteristic.value.should.equal(mockValue); }); + it('should not create with value option and non-read properties', function() { + (function(){ + var characteristic = new Characteristic({ + properties: ['write'], + value: mockValue + }); + }).should.throw(); + }); + it('should create with descriptors option', function() { var characteristic = new Characteristic({ descriptors: mockDescriptors