This repository has been archived by the owner on Oct 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (127 loc) · 4.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const debug = require('debug');
const crc = require('crc');
//const Buffer = require('safe-buffer').Buffer
class ccTalkMessage {
// src, dest, command, data, crc
constructor(src, dest, command, data, crc) {
//fromBuffer() A buffer always should have a crc checksum already !
if(src instanceof Uint8Array) {
// parse command
this._buffer = src;
this._src = this._buffer[2];
this._dest = this._buffer[0];
this._command = this._buffer[3];
this._data = this._buffer.slice(4, this._buffer[1]+4);
// TODO: checksum detection and parsing
this._checksum = this._buffer[this._buffer[1]+4];
if (this._checksum == undefined) {
console.log(this._buffer);
throw new Error('NO_CHECKSUM');
} else {
// Check for CRC8
if (this.crc8verify()) {
this._crcType = 8;
debug('ccMessage:crc')('CRC8_CHECKSUM');
} else if (this.crc16verify()) {
this._crcType = 16;
debug('ccMessage:crc')('CRC16_CHECKSUM');
} else {
debug('ccMessage:crc::warning')(this._buffer);
//throw new Error('WRONG_CHECKSUM');
}
}
} else {
// create command
if (command == undefined) {
debug('ccMessage:command')(this._buffer);
throw new Error('NO_COMMAND');
} else if (data == undefined) {
debug('ccMessage:command')(this._buffer);
throw new Error('NO_DATA');
}
this._src = typeof src != undefined ? src : 1;
this._dest = typeof dest != undefined ? dest : 2;
this._crcType = typeof crc != undefined ? crc : 8;
this._command = command;
this._data = data;
}
}
toBuffer() {
if (this._buffer == undefined) {
this._buffer = new Uint8Array(5 + this._data.length);
this._buffer[0] = this._dest;
this._buffer[1] = this._data.length;
this._buffer[2] = this._src;
this._buffer[3] = this._command;
this._buffer.set(this._data, 4);
// console.log('CRC: ', this._crcType)
if (this._crcType === 8) {
return this.crc8();
} else {
return this.crc16();
}
} else {
return this._buffer;
}
}
crc8() {
var sum = 0;
for (var i=0; i < (this._buffer.length - 1); ++i)
sum += (this._buffer[i]);
// Set Checksum at end
this._buffer[this._data.length+4] = 0x100 - sum%0x100;
return this._buffer;
}
crc8verify() {
var sum = 0;
for (var i=0; i < (this._buffer.length - 1); ++i) {
sum += (this._buffer[i]);
}
if (this._buffer[this._data.length+4] != 0x100 - sum%0x100) {
return false;
} else {
return true;
}
}
crc16() {
//CRC16-CCITT-xModem signed Buffer
var UArray = new Uint8Array(3+ this._data.length);
//[this._buffer[0],this._buffer[1],this._buffer[3]];
UArray[0] = this._dest;
UArray[1] = this._data.length;
UArray[2] = this._command;
UArray.set(this._data, 3);
var CRCArray = require('crc').crc16xmodem(Buffer.from(UArray))
.toString(16)
.match(/.{1,2}/g)
.map((val)=> parseInt(val, 16))
.reverse();
// console.log(CRCArray)
// Set Checksum first Part at src
this._buffer.set([CRCArray[0]],2);
// Set Checksum Secund Part after data
this._buffer.set([CRCArray[1]], this._buffer[1]+4); // Position after data aka last
return this._buffer;
}
crc16verify() {
//var UArray = new Uint8Array([this._buffer[0],this._buffer[1],this._buffer[3]]);
var UArray = new Uint8Array(3+ this._data.length);
//[this._buffer[0],this._buffer[1],this._buffer[3]];
UArray[0] = this._dest;
UArray[1] = this._data.length;
UArray[2] = this._command;
UArray.set(this._data, 3);
var CRCArray = require('crc').crc16xmodem(Buffer.from(UArray))
.toString(16)
.match(/.{1,2}/g)
.map((val)=> parseInt(val, 16))
.reverse();
if ((this._buffer[2] == CRCArray[0]) && (this._buffer[this._buffer[1]+4] == CRCArray[1])) {
return true;
} else {
debug('ccMessage:crc')(this._buffer[2] +'=='+ CRCArray[0],this._buffer[this._buffer[1]+4]+'=='+ CRCArray[1]);
return false;
}
}
}
module.exports = exports = ccTalkMessage;