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 debug = console.log; // requir('debug')('ble_mgmt');
39 var events = require('events');
40 var util = require('util');
42 var BluetoothHciSocket = require('ble_hci_socket');
44 var LTK_INFO_SIZE = 36;
46 var MGMT_OP_LOAD_LONG_TERM_KEYS = 0x0013;
49 this._socket = new BluetoothHciSocket();
52 this._socket.on('data', this.onSocketData.bind(this));
53 this._socket.on('error', this.onSocketError.bind(this));
55 this._socket.bindControl();
59 Mgmt.prototype.onSocketData = function(data) {
60 debug('on data ->' + data.toString('hex'));
63 Mgmt.prototype.onSocketError = function(error) {
64 debug('on error ->' + error.message);
67 Mgmt.prototype.addLongTermKey = function(address, addressType, authenticated, master, ediv, rand, key) {
68 var ltkInfo = new Buffer(LTK_INFO_SIZE);
70 address.copy(ltkInfo, 0);
71 ltkInfo.writeUInt8(addressType.readUInt8(0) + 1, 6); // BDADDR_LE_PUBLIC = 0x01, BDADDR_LE_RANDOM 0x02, so add one
73 ltkInfo.writeUInt8(authenticated, 7);
74 ltkInfo.writeUInt8(master, 8);
75 ltkInfo.writeUInt8(key.length, 9);
77 ediv.copy(ltkInfo, 10);
78 rand.copy(ltkInfo, 12);
79 key.copy(ltkInfo, 20);
81 this._ltkInfos.push(ltkInfo);
83 this.loadLongTermKeys();
86 Mgmt.prototype.clearLongTermKeys = function() {
89 this.loadLongTermKeys();
92 Mgmt.prototype.loadLongTermKeys = function() {
93 var numLongTermKeys = this._ltkInfos.length;
94 var op = new Buffer(2 + numLongTermKeys * LTK_INFO_SIZE);
96 op.writeUInt16LE(numLongTermKeys, 0);
98 for (var i = 0; i < numLongTermKeys; i++) {
99 this._ltkInfos[i].copy(op, 2 + i * LTK_INFO_SIZE);
102 this.write(MGMT_OP_LOAD_LONG_TERM_KEYS, 0, op);
105 Mgmt.prototype.write = function(opcode, index, data) {
109 length = data.length;
112 var pkt = new Buffer(6 + length);
114 pkt.writeUInt16LE(opcode, 0);
115 pkt.writeUInt16LE(index, 2);
116 pkt.writeUInt16LE(length, 4);
122 debug('writing -> ' + pkt.toString('hex'));
123 this._socket.write(pkt);
126 module.exports = new Mgmt();