Skip to content

Commit

Permalink
- only allow characteristics with value to be read only (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeepmistry committed May 2, 2016
1 parent 920d72a commit 3a1ffa9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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
],
Expand Down
4 changes: 4 additions & 0 deletions lib/characteristic.js
Expand Up @@ -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;
}
Expand Down
10 changes: 10 additions & 0 deletions test/test-characteristic.js
Expand Up @@ -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
Expand Down

0 comments on commit 3a1ffa9

Please sign in to comment.