1 /* Copyright 2016-present Samsung Electronics Co., Ltd. and other contributors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 /* Copyright (C) 2015 Sandeep Mistry sandeep.mistry@gmail.com
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this software and associated documentation files (the "Software"), to deal
20 * in the Software without restriction, including without limitation the rights
21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
22 * copies of the Software, and to permit persons to whom the Software is
23 * furnished to do so, subject to the following conditions:
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
33 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 var events = require('events');
38 var util = require('util');
40 var debug = console.log; //requir('debug')('ble_characteristic');
42 var UuidUtil = require('ble_uuid_util');
44 function Characteristic(options) {
45 this.uuid = UuidUtil.removeDashes(options.uuid);
46 this.properties = options.properties || [];
47 this.secure = options.secure || [];
48 this.value = options.value || null;
49 this.descriptors = options.descriptors || [];
51 if (this.value && (this.properties.length !== 1 || this.properties[0] !== 'read')) {
52 throw new Error('Characteristics with value can be read only!');
55 if (options.onReadRequest) {
56 this.onReadRequest = options.onReadRequest;
59 if (options.onWriteRequest) {
60 this.onWriteRequest = options.onWriteRequest;
63 if (options.onSubscribe) {
64 this.onSubscribe = options.onSubscribe;
67 if (options.onUnsubscribe) {
68 this.onUnsubscribe = options.onUnsubscribe;
71 if (options.onNotify) {
72 this.onNotify = options.onNotify;
75 if (options.onIndicate) {
76 this.onIndicate = options.onIndicate;
79 this.on('readRequest', this.onReadRequest.bind(this));
80 this.on('writeRequest', this.onWriteRequest.bind(this));
81 this.on('subscribe', this.onSubscribe.bind(this));
82 this.on('unsubscribe', this.onUnsubscribe.bind(this));
83 this.on('notify', this.onNotify.bind(this));
84 this.on('indicate', this.onIndicate.bind(this));
87 util.inherits(Characteristic, events.EventEmitter);
89 Characteristic.RESULT_SUCCESS = Characteristic.prototype.RESULT_SUCCESS = 0x00;
90 Characteristic.RESULT_INVALID_OFFSET = Characteristic.prototype.RESULT_INVALID_OFFSET = 0x07;
91 Characteristic.RESULT_ATTR_NOT_LONG = Characteristic.prototype.RESULT_ATTR_NOT_LONG = 0x0b;
92 Characteristic.RESULT_INVALID_ATTRIBUTE_LENGTH = Characteristic.prototype.RESULT_INVALID_ATTRIBUTE_LENGTH = 0x0d;
93 Characteristic.RESULT_UNLIKELY_ERROR = Characteristic.prototype.RESULT_UNLIKELY_ERROR = 0x0e;
95 Characteristic.prototype.toString = function() {
96 return JSON.stringify({
98 properties: this.properties,
101 descriptors: this.descriptors
105 Characteristic.prototype.onReadRequest = function(offset, callback) {
106 callback(this.RESULT_UNLIKELY_ERROR, null);
109 Characteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
110 callback(this.RESULT_UNLIKELY_ERROR);
113 Characteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) {
114 this.maxValueSize = maxValueSize;
115 this.updateValueCallback = updateValueCallback;
118 Characteristic.prototype.onUnsubscribe = function() {
119 this.maxValueSize = null;
120 this.updateValueCallback = null;
123 Characteristic.prototype.onNotify = function() {
126 Characteristic.prototype.onIndicate = function() {
129 module.exports = Characteristic;