@mschaeffler/node-red-bthome 0.1.3 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bthome.js +29 -20
  2. package/package.json +1 -1
package/bthome.js CHANGED
@@ -9,8 +9,8 @@ module.exports = function(RED) {
9
9
  var node = this;
10
10
  this.flowcontext = this.context().flow;
11
11
  this.devices = JSON.parse( config.devices ?? "{}" );
12
- this.statusPrefix = config.statusPrefix ? config.statusPrefix+'/' :"";
13
- this.eventPrefix = config.eventPrefix ? config.eventPrefix+'/' : "";
12
+ this.statusPrefix = config.statusPrefix ? config.statusPrefix+'/' : "";
13
+ this.eventPrefix = config.eventPrefix ? config.eventPrefix +'/' : "";
14
14
  this.contextVar = config.contextVar ?? "bthome";
15
15
  this.contextStore = config.contextStore ?? "none";
16
16
  this.data = {};
@@ -25,7 +25,7 @@ module.exports = function(RED) {
25
25
  }
26
26
  else
27
27
  {
28
- console.log( "context read", value );
28
+ //console.log( "context read", value );
29
29
  if( value !== undefined )
30
30
  {
31
31
  node.data = value;
@@ -64,9 +64,7 @@ module.exports = function(RED) {
64
64
  node.on('input', function(msg,send,done) {
65
65
  if( ! Array.isArray( msg.payload.data ) )
66
66
  {
67
- node.error( "msg.payload.data must be an Array!" );
68
- node.trace( "msg processed" );
69
- done();
67
+ done( "msg.payload.data must be an Array!" );
70
68
  return;
71
69
  }
72
70
 
@@ -74,30 +72,38 @@ module.exports = function(RED) {
74
72
  {
75
73
  if( name === undefined )
76
74
  {
77
- node.error( "unknown BT-Home " + msg.payload.addr );
75
+ throw new Error( "unknown BT-Home " + msg.payload.addr );
78
76
  }
79
77
  else if( version !== 2 )
80
78
  {
81
- node.error( "wrong BT-Home version " + version );
79
+ throw new Error( "wrong BT-Home version " + version );
82
80
  }
83
81
  else if( encrypted && !node.devices[msg.payload.addr].key )
84
82
  {
85
- node.error( name + " encryption key needed" );
83
+ throw new Error( name + " encryption key needed" );
86
84
  }
87
85
  else if( (!encrypted) && node.devices[msg.payload.addr].key )
88
86
  {
89
- node.error( name + " encrypted messages needed" );
87
+ throw new Error( name + " encrypted messages needed" );
90
88
  }
91
- else
92
- {
93
- return true;
94
- }
95
- return false;
96
89
  }
97
90
 
98
91
  function decryptMsg()
99
92
  {
100
- console.log("encrypted");
93
+ let mac = [];
94
+ for( const help of msg.payload.addr.split( ":" ) )
95
+ {
96
+ mac.push( Number.parseInt( help, 16 ) );
97
+ }
98
+ const uuid16 = [0xD2,0xFC];
99
+ const ciphertext = Buffer.from( rawdata.slice( 0, -8 ) );
100
+ const counter = rawdata.slice( -8, -4 );
101
+ const mic = Buffer.from( rawdata.slice( -4 ) );
102
+ const nonce = Buffer.from( mac.concat( uuid16, dib, counter ) );
103
+ const decipher = Crypto.createDecipheriv( "aes-128-ccm", node.devices[msg.payload.addr].key, nonce, { authTagLength: 4 } );
104
+ decipher.setAuthTag( mic );
105
+ rawdata = Array.from( decipher.update( ciphertext ) );
106
+ decipher.final();
101
107
  }
102
108
 
103
109
  function setData(name,value)
@@ -212,12 +218,12 @@ module.exports = function(RED) {
212
218
  const events = new BtEvent( node.eventPrefix );
213
219
  let item = node.data[name];
214
220
 
215
- if( checkMsg() )
221
+ try
216
222
  {
223
+ checkMsg();
217
224
  if( encrypted )
218
225
  {
219
226
  decryptMsg();
220
- done();return;
221
227
  }
222
228
  if( item == undefined )
223
229
  {
@@ -229,9 +235,12 @@ module.exports = function(RED) {
229
235
  {
230
236
  newMessage();
231
237
  }
238
+ done();
239
+ }
240
+ catch( e )
241
+ {
242
+ done( e.message );
232
243
  }
233
- node.trace( "msg processed" );
234
- done();
235
244
  });
236
245
  }
237
246
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mschaeffler/node-red-bthome",
3
- "version": "0.1.3",
3
+ "version": "0.2.0",
4
4
  "description": "A Node Red node to decrypt and decode BT-Home frames",
5
5
  "author": {
6
6
  "name": "Mathias Schäffler",