Upload packaging folder
[platform/upstream/iotjs.git] / tools / src / js / ble_hci_socket_mgmt.js
1 /* Copyright 2016-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 debug = console.log; // requir('debug')('ble_mgmt');
38
39 var events = require('events');
40 var util = require('util');
41
42 var BluetoothHciSocket = require('ble_hci_socket');
43
44 var LTK_INFO_SIZE = 36;
45
46 var MGMT_OP_LOAD_LONG_TERM_KEYS = 0x0013;
47
48 function Mgmt() {
49   this._socket = new BluetoothHciSocket();
50   this._ltkInfos = [];
51
52   this._socket.on('data', this.onSocketData.bind(this));
53   this._socket.on('error', this.onSocketError.bind(this));
54
55   this._socket.bindControl();
56   this._socket.start();
57 }
58
59 Mgmt.prototype.onSocketData = function(data) {
60   debug('on data ->' + data.toString('hex'));
61 };
62
63 Mgmt.prototype.onSocketError = function(error) {
64   debug('on error ->' + error.message);
65 };
66
67 Mgmt.prototype.addLongTermKey = function(address, addressType, authenticated, master, ediv, rand, key) {
68   var ltkInfo = new Buffer(LTK_INFO_SIZE);
69
70   address.copy(ltkInfo, 0);
71   ltkInfo.writeUInt8(addressType.readUInt8(0) + 1, 6); // BDADDR_LE_PUBLIC = 0x01, BDADDR_LE_RANDOM 0x02, so add one
72
73   ltkInfo.writeUInt8(authenticated, 7);
74   ltkInfo.writeUInt8(master, 8);
75   ltkInfo.writeUInt8(key.length, 9);
76
77   ediv.copy(ltkInfo, 10);
78   rand.copy(ltkInfo, 12);
79   key.copy(ltkInfo, 20);
80
81   this._ltkInfos.push(ltkInfo);
82
83   this.loadLongTermKeys();
84 };
85
86 Mgmt.prototype.clearLongTermKeys = function() {
87   this._ltkInfos = [];
88
89   this.loadLongTermKeys();
90 };
91
92 Mgmt.prototype.loadLongTermKeys = function() {
93   var numLongTermKeys = this._ltkInfos.length;
94   var op = new Buffer(2 + numLongTermKeys * LTK_INFO_SIZE);
95
96   op.writeUInt16LE(numLongTermKeys, 0);
97
98   for (var i = 0; i < numLongTermKeys; i++) {
99     this._ltkInfos[i].copy(op, 2 + i * LTK_INFO_SIZE);
100   }
101
102   this.write(MGMT_OP_LOAD_LONG_TERM_KEYS, 0, op);
103 };
104
105 Mgmt.prototype.write = function(opcode, index, data) {
106   var length = 0;
107
108   if (data) {
109     length = data.length;
110   }
111
112   var pkt = new Buffer(6 + length);
113
114   pkt.writeUInt16LE(opcode, 0);
115   pkt.writeUInt16LE(index, 2);
116   pkt.writeUInt16LE(length, 4);
117
118   if (length) {
119     data.copy(pkt, 6);
120   }
121
122   debug('writing -> ' + pkt.toString('hex'));
123   this._socket.write(pkt);
124 };
125
126 module.exports = new Mgmt();