Update Iot.js
[platform/upstream/iotjs.git] / tools / test / run_pass / test_ble_setservices.js
1 /* Copyright 2017-present Samsung Electronics Co., Ltd. and other contributors
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 /* Copyright (C) 2015 Sandeep Mistry sandeep.mistry@gmail.com
17  *
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:
24  *
25  * The above copyright notice and this permission notice shall be included in
26  * all copies or substantial portions of the Software.
27  *
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
34  * SOFTWARE.
35  */
36
37 var ble = require('ble');
38
39 var BlenoPrimaryService = ble.PrimaryService;
40 var BlenoCharacteristic = ble.Characteristic;
41
42 var util = require('util');
43
44 var EchoCharacteristic = function() {
45   BlenoCharacteristic.call(this, {
46     uuid: 'ec0e',
47     properties: ['read', 'write', 'notify'],
48     value: null
49   });
50
51   this._value = new Buffer(0);
52   this._updateValueCallback = null;
53 };
54
55 util.inherits(EchoCharacteristic, BlenoCharacteristic);
56
57 EchoCharacteristic.prototype.onReadRequest = function(offset, callback) {
58   console.log('EchoCharacteristic - onReadRequest: value = ' + this._value.toString('hex'));
59
60   callback(this.RESULT_SUCCESS, this._value);
61 };
62
63 EchoCharacteristic.prototype.onWriteRequest = function(data, offset, withoutResponse, callback) {
64   this._value = data;
65
66   console.log('EchoCharacteristic - onWriteRequest: value = ' + this._value.toString('hex'));
67
68   if (this._updateValueCallback) {
69     console.log('EchoCharacteristic - onWriteRequest: notifying');
70
71     this._updateValueCallback(this._value);
72   }
73
74   callback(this.RESULT_SUCCESS);
75 };
76
77 EchoCharacteristic.prototype.onSubscribe = function(maxValueSize, updateValueCallback) {
78   console.log('EchoCharacteristic - onSubscribe');
79
80   this._updateValueCallback = updateValueCallback;
81 };
82
83 EchoCharacteristic.prototype.onUnsubscribe = function() {
84   console.log('EchoCharacteristic - onUnsubscribe');
85
86   this._updateValueCallback = null;
87 };
88
89 console.log('ble - echo');
90
91 ble.on('stateChange', function(state) {
92   console.log('on -> stateChange: ' + state);
93
94   if (state === 'poweredOn') {
95     ble.startAdvertising('iotjs_echo', ['ec00']);
96   } else {
97     ble.stopAdvertising();
98   }
99 });
100
101 ble.on('advertisingStart', function(error) {
102   console.log('on -> advertisingStart: ' + (error ? 'error ' + error : 'success'));
103
104   if (!error) {
105     ble.setServices([
106       new BlenoPrimaryService({
107         uuid: 'ec00',
108         characteristics: [
109           new EchoCharacteristic()
110         ]
111       })
112     ]);
113   }
114 });