-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdecoder.cpp
415 lines (356 loc) · 11.2 KB
/
decoder.cpp
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#include "decoder.h"
#include "nodenames.h"
Decoder::Decoder()
{
state = IDLE;
raw = new QByteArray;
nodenames = new NodeNames();
//initialise statistics
numInputBytes = 0;
numCandidatePayloads = 0;
numOverLengthMessages = 0;
numLengthOdd = 0;
numManchesterInvalid = 0;
numCheckSumErrors = 0;
numValidMessages = 0;
numCollisions = 0;
numSeventyBytes = 0;
}
void Decoder::inputBytes(QByteArray in)
{
int size = in.size();
numInputBytes += size;
emit inputByteCount(numInputBytes);
for(int i=0; i<size; i++) {
decodeChar(in.at(i));
}
}
void Decoder::decodeChar(char c)
{
switch(state) {
case IDLE:
if (c=='3') {
raw->append(c);
state = HEAD1;
}
break;
case HEAD1:
if (c=='U') {
raw->append(c);
state = HEAD2;
} else {
raw->clear();
state = IDLE;
}
break;
case HEAD2:
if (c=='S') {
raw->append(c);
state = PAYLOAD;
} else {
raw->clear();
state = IDLE;
}
break;
case PAYLOAD:
//why do i receive 0x70 characters?
//these seem spurious yet messages are valid with them removed
if(c == 0x70) {
numSeventyBytes++;
emit seventyCount(numSeventyBytes);
break;
}
//if two messages collide, we find the 0x33 0x55 0x53 pattern
//in the middle of an existing payload
if(c == '3') {
raw->clear();
raw->append(c);
state = HEAD1;
numCollisions++;
emit collisionCount(numCollisions);
break;
}
raw->append(c);
if (c=='5') {
numCandidatePayloads++;
emit candidatePayloadCount(numCandidatePayloads);
lengthCheck();
raw->clear();
state = IDLE;
}
break;
default:
raw->clear();
state = IDLE;
}
}
void Decoder::lengthCheck()
{
if(raw->length() < 255) {
manchesterDecode();
} else {
numOverLengthMessages++;
emit overLengthMessageCount(numOverLengthMessages);
emit consoleMessage(QString("Message too long - %1\n") .arg(raw->length()));
}
}
char Decoder::manchesterLUT(char c)
{
char val=-1;
switch((unsigned char)c)
{
case 0xAA: val=0x00; break;
case 0xA9: val=0x01; break;
case 0xA6: val=0x02; break;
case 0xA5: val=0x03; break;
case 0x9A: val=0x04; break;
case 0x99: val=0x05; break;
case 0x96: val=0x06; break;
case 0x95: val=0x07; break;
case 0x6A: val=0x08; break;
case 0x69: val=0x09; break;
case 0x66: val=0x0A; break;
case 0x65: val=0x0B; break;
case 0x5A: val=0x0C; break;
case 0x59: val=0x0D; break;
case 0x56: val=0x0E; break;
case 0x55: val=0x0F; break;
}
return val;
}
void Decoder::manchesterDecode()
{
QByteArray mdecoded;
QString msg;
msg.append("RAW ");
for(int i=0; i<raw->size(); i++) {
msg.append(QString("%1 ") .arg(raw->at(i) & 0xFF, 2, 16, QChar('0')));
}
msg.append("\n");
emit consoleMessage(msg);
msg.clear();
if(raw->length()%2 != 0) {
numLengthOdd++;
emit lengthOddCount(numLengthOdd);
emit consoleMessage(QString("ERROR - packet length is not a multiple of 2\n"));
return;
}
for(int i=3; i<raw->size()-1; i+=2) {
char ms=manchesterLUT(raw->at(i));
if(ms==-1) {
numManchesterInvalid++;
emit manchesterInvalidCount(numManchesterInvalid);
emit consoleMessage(QString("Manchester decoding failed at %1 (ms) - 0x%2\n") .arg(i).arg(raw->at(i) & 0xFF, 2, 16));
return;
}
char ls=manchesterLUT(raw->at(i+1));
if(ls==-1) {
numManchesterInvalid++;
emit manchesterInvalidCount(numManchesterInvalid);
emit consoleMessage(QString("Manchester decoding failed at %1 (ls) - 0x%2\n") .arg(i).arg(raw->at(i+1) & 0xFF, 2, 16));
return;
}
char byte=(ms<<4) | ls;
mdecoded.append(byte);
}
msg.append("DEC ");
for(int i=0; i<mdecoded.size(); i++) {
msg.append(QString("%1 ") .arg(mdecoded.at(i) & 0xFF, 2, 16, QChar('0')));
}
msg.append("\n");
emit consoleMessage(msg);
checksum(mdecoded);
}
void Decoder::checksum(QByteArray &in)
{
int sum=0;
for(int i=0; i<in.size(); i++)
sum+=in.at(i);
if((sum & 0xFF) == 0) {
numValidMessages++;
emit validMessageCount(numValidMessages);
emit consoleMessage(QString("CHECKSUM - OK\n"));
emit gotMessage(in);
decodeHeader(in);
} else {
numCheckSumErrors++;
emit checkSumErrorCount(numCheckSumErrors);
emit consoleMessage(QString("CHECKSUM - FAIL\n"));
}
}
void Decoder::writeLog(QByteArray &in, quint32 command)
{
QString msg = QString("%1 ") .arg(command, 4, 16, QChar('0'));
for(int i=0; i < (int)in.size(); i++) {
msg.append(QString("%1 ") .arg(in.at(i) & 0xFF, 2, 16, QChar('0')));
}
emit logMessage(msg);
}
void Decoder::decodeHeader(QByteArray &in)
{
bool id1_present=0;
bool id2_present=0;
bool ts_present=0;
switch(in.at(0)) {
case 0x18: //two addresses, no timestamp
id1_present=1;
id2_present=1;
break;
case 0x16: //one address and one timestamp
id1_present=1;
ts_present=1;
break;
case 0x14: //one address, no timestamp
id1_present=1;
}
quint32 id1=0, id2=0;
quint8 ts=0;
//position we start parsing after the header byte
qint32 index=1;
if(id1_present) {
id1 = (in.at(index) & 0xFF) << 16;
id1 |= (in.at(index+1) & 0xFF) << 8;
id1 |= (in.at(index+2) & 0xFF);
index+=3;
}
if(id2_present) {
id2 = (in.at(index) & 0xFF) << 16;
id2 |= (in.at(index+1) & 0xFF) << 8;
id2 |= (in.at(index+2) & 0xFF);
index+=3;
}
if(ts_present) {
ts = in.at(index++);
}
quint32 command = (in.at(index) & 0xFF) << 8 | (in.at(index+1) & 0xFF);
index+=2;
quint32 length = in.at(index++);
writeLog(in, command);
emit gotCommand(id1, id2, command);
emit consoleMessage(QString("ID1=%1 ID2=%2 TS=%3 COMMAND=%4, LENGTH=%5\n")
.arg(id1, 6, 16, QChar('0')) .arg(id2, 6, 16, QChar('0'))
.arg(ts, 2, 16, QChar('0'))
.arg(command, 4, 16, QChar('0')) .arg(length));
switch(command) {
case 0x2309: //zone setpoint setting
decode2309(id1, id2, length, index, in);
break;
case 0x3150: //heat demand timing
decode3150(id1, id2, length, index, in);
break;
case 0x30C9: //zone temperature distribution
decode30C9(id1, id2, length, index, in);
break;
case 0x2249: //programmer now/next setpoint
decode2249(id1, length, index, in);
break;
case 0x1060: //unknown, but frequent
decode1060(id1, id2, length, index, in);
break;
case 0x1100: //unknown
decode1100(id1, length, index, in);
break;
case 0x0008: //unknown
decode0008(id1, length, index, in);
break;
case 0x0009: //unknown
decode0009(id1, length, index, in);
break;
case 0x000A: //hr80 parameters
decode000A(id1, length, index, in);
break;
case 0x1F09: //unknown
decode1F09(id1, length, index, in);
break;
case 0x3B00: //unknown
decode3B00(id1, length, index, in);
break;
default:
emit consoleMessage(QString("No decoder for command 0x%1\n") .arg(command, 4, 16));
}
emit consoleMessage(QString("\n"));
}
void Decoder::decode2309(quint32 id1, quint32 id2, quint32 length, quint32 i, QByteArray &in)
{
//zone setpoint setting
//a number of 3 byte sections
for(int n=i; n<in.size()-1; n+=3) {
quint32 zone = in.at(n) & 0xFF;
quint32 temp_i = (in.at(n+1) & 0xFF) << 8 | (in.at(n+2) & 0xFF);
float temp = (float)temp_i / 100.0;
emit zoneSetpointSetting(id1, id2, zone, temp);
emit influxData(QString("setpoint,device=%1,zone=%2 temp=%3") .arg(nodenames->idToName(id1)) .arg(zone) .arg(temp));
}
}
void Decoder::decode3150(quint32 id1, quint32 id2, quint32 length, quint32 i, QByteArray &in)
{
//heat demand
quint32 zone = (in.at(i) & 0xFF);
quint32 demand = (in.at(i+1) & 0xFF);
emit heatDemand(id1, id2, zone, demand);
emit influxData(QString("demand,device=%1,zone=%2 value=%3") .arg(nodenames->idToName(id1)) .arg(zone) .arg(demand));
}
void Decoder::decode30C9(quint32 id1, quint32 id2, quint32 length, quint32 i, QByteArray &in)
{
//zone temperature distribution
//a number of 3 byte sections
for(int n=i; n<in.size()-1; n+=3) {
quint32 zone = in.at(n) & 0xFF;
quint32 temp_i = (in.at(n+1) & 0xFF) << 8 | (in.at(n+2) & 0xFF);
float temp = (float)temp_i / 100.0;
emit zoneTempDistribution(id1, id2, zone, temp);
emit influxData(QString("temperature,device=%1,zone=%2 temp=%3") .arg(nodenames->idToName(id1)) .arg(zone) .arg(temp));
}
}
void Decoder::decode2249(quint32 id1, quint32 length, quint32 i, QByteArray &in)
{
//programmer now/next setpoint
//a number of 7 byte sections, one for each zome
for(int n=i; n<in.size()-1; n+=7) {
quint32 zone = in.at(n) & 0xFF;
quint32 tempNow_i = (in.at(n+1) & 0xFF) << 8 | (in.at(n+2) & 0xFF);
quint32 tempNext_i = (in.at(n+3) & 0xFF) << 8 | (in.at(n+4) & 0xFF);
float tempNow = (float)tempNow_i / 100.0;
float tempNext = (float)tempNext_i / 100.0;
quint32 countDown = (in.at(n+5) & 0xFF) << 8 | (in.at(n+6) & 0xFF);
emit programmerNowNext(id1, zone, tempNow, tempNext, countDown);
}
}
void Decoder::decode000A(quint32 id1, quint32 length, quint32 i, QByteArray &in)
{
//HR80 parameters
//a number of 6 byte sections, one for each zone
for(int n=i; n<in.size()-1; n+=6) {
quint32 zone = in.at(n) & 0xFF;
quint32 flags = in.at(n+1) & 0xFF;
quint32 tempMin_i = (in.at(n+2) & 0xFF) << 8 | (in.at(n+3) & 0xFF);
quint32 tempMax_i = (in.at(n+4) & 0xFF) << 8 | (in.at(n+5) & 0xFF);
float tempMin = (float)tempMin_i / 100.0;
float tempMax = (float)tempMax_i / 100.0;
emit hr80parameters(id1, zone, flags, tempMin, tempMax);
}
}
void Decoder::decode1060(quint32 id1, quint32 id2, quint32 length, quint32 i, QByteArray &in)
{
emit unknown1060(id1, id2, in);
}
void Decoder::decode1100(quint32 id1, quint32 length, quint32 i, QByteArray &in)
{
emit unknown1100(id1, in);
}
void Decoder::decode0008(quint32 id1, quint32 length, quint32 i, QByteArray &in)
{
emit unknown0008(id1, in);
}
void Decoder::decode0009(quint32 id1, quint32 length, quint32 i, QByteArray &in)
{
emit unknown0009(id1, in);
}
void Decoder::decode1F09(quint32 id1, quint32 length, quint32 i, QByteArray &in)
{
emit unknown1F09(id1, in);
}
void Decoder::decode3B00(quint32 id1, quint32 length, quint32 i, QByteArray &in)
{
emit unknown3B00(id1, in);
}