forked from alekslt/HANToMQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHanReader.cpp
429 lines (374 loc) · 12.8 KB
/
HanReader.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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include "HanReader.h"
#include "ObisElement.h"
#include <ArduinoJson.h>
#include <map>
#ifdef _WIN32
#include <math.h>
#else
extern "C" {
//#include "user_interface.h"
}
#endif
// http://www.cs.ru.nl/~marko/onderwijs/bss/SmartMeter/Excerpt_BB7.pdf
std::map<int, String> cosemTypeToString;
std::map<byte, String> cosemEnumTypeToString;
HanReader::HanReader() {
debugLevel = 2;
}
void HanReader::Init() {
cosemTypeToString[TYPE_LIST] = String("TYPE_LIST");
cosemTypeToString[TYPE_STRUCTURE] = String("TYPE_STRUCTURE");
cosemTypeToString[TYPE_UINT32] = String("TYPE_UINT32");
cosemTypeToString[TYPE_OCTET_STRING] = String("TYPE_OCTET_STRING");
cosemTypeToString[TYPE_STRING] = String("TYPE_STRING");
cosemTypeToString[TYPE_INT8] = String("TYPE_INT8");
cosemTypeToString[TYPE_INT16] = String("TYPE_INT16");
cosemTypeToString[TYPE_UINT16] = String("TYPE_UINT16");
cosemTypeToString[TYPE_FLOAT] = String("TYPE_FLOAT");
cosemTypeToString[TYPE_ENUM] = String("TYPE_ENUM");
cosemTypeToString[TYPE_DATETIME] = String("TYPE_DATETIME");
cosemEnumTypeToString[27] = String("W");
cosemEnumTypeToString[28] = String("VA");
cosemEnumTypeToString[29] = String("var");
cosemEnumTypeToString[30] = String("Wh");
cosemEnumTypeToString[31] = String("VAh");
cosemEnumTypeToString[32] = String("varh");
cosemEnumTypeToString[33] = String("A");
cosemEnumTypeToString[34] = String("C");
cosemEnumTypeToString[35] = String("V");
}
void HanReader::setup(HardwareSerial *hanPort, Stream *debugPort)
{
Init();
han = hanPort;
bytesRead = 0;
debug = debugPort;
reader.debug = debugPort;
reader.debugLevel = debugLevel;
reader.getEpochTime = getEpochTime;
reader.netLog = netLog;
reader.sendBuffer = sendBuffer;
}
void HanReader::printObjectStart(uint16_t pos) {
byte dataType = userData[pos];
if (debug) {
debug->print("Datatype@");
debug->print(pos, HEX);
debug->print(":");
debug->print(dataType, HEX);
debug->print("(");
debug->print(cosemTypeToString[dataType].c_str());
debug->print(")");
debug->print(", Elements:");
debug->println(userData[pos + 1], HEX);
}
}
bool HanReader::decodeClockAndDeviation(byte* buf, ClockDeviation& element) {
uint16_t year = buf[0] << 8 | buf[1];
uint8_t month = buf[2];
uint8_t day = buf[3];
//uint8_t dayOfWeek = buf[4];
uint8_t hour = buf[5];
uint8_t minute = buf[6];
uint8_t second = buf[7];
//uint8_t hundreths = buf[8]; // Can be 0xFF if not used
sprintf(element.dateTime, "%04u.%02u.%02uT%02u:%02u:%02u", year, month, day, hour, minute, second);
element.deviation = buf[9] << 8 | buf[10];
element.clockStatus = buf[11];
return true;
}
bool HanReader::decodeDataElement(uint16_t& nextPos, Element& element) {
if (debugLevel > 2) printObjectStart(nextPos);
element.dataType = userData[nextPos];
uint8_t valueLength = userData[nextPos + 1];
if (element.dataType == TYPE_UINT32) {
element.value.value_uint = userData[nextPos + 1] << 24 | userData[nextPos + 2] << 16 | userData[nextPos + 3] << 8 | userData[nextPos + 4];
nextPos += 5;
}
else if (element.dataType == TYPE_STRING) {
element.stringLength = valueLength;
element.value.value_string = (char*)&userData[nextPos + 2];
nextPos += 2 + valueLength;
}
else if (element.dataType == TYPE_UINT16) {
element.value.value_uint = userData[nextPos + 1] << 8 | userData[nextPos + 2];
nextPos += 3;
}
else if (element.dataType == TYPE_INT16) {
element.value.value_int = (int16_t)(userData[nextPos + 1] << 8 | userData[nextPos + 2]);
nextPos += 3;
}
else if (element.dataType == TYPE_OCTET_STRING) {
element.stringLength = valueLength;
element.value.value_string = (char*)&userData[nextPos + 2];
nextPos += 2 + valueLength;
}
else {
return false;
}
return true;
}
bool HanReader::decodeAndApplyScalerElement(uint16_t& nextPos, ObisElement* obisElement) {
int8_t scaler;
if (debugLevel > 2) printObjectStart(nextPos);
uint8_t scaleValStructType = userData[nextPos];
uint8_t scaleValStructElements = userData[nextPos + 1];
if (scaleValStructType != TYPE_STRUCTURE || scaleValStructElements != 2) {
if (debug) debug->println("ScaleVal Struct element is invalid. Expected 0x02(TYPE_STRUCTURE) and elements 0x02");
return false;
}
nextPos += 2;
if (debugLevel > 2) printObjectStart(nextPos);
uint8_t scalerType = userData[nextPos];
if (scalerType == TYPE_INT8) {
scaler = userData[nextPos + 1];
nextPos += 2;
}
else {
if (debug) debug->println("ScaleVal Struct element is invalid. Expected TYPE_INT8 and TYPE_ENUM");
return false;
}
if (debugLevel > 2) printObjectStart(nextPos);
uint8_t enumType = userData[nextPos];
if (enumType == TYPE_ENUM) {
obisElement->enumType = userData[nextPos + 1];
nextPos += 2;
}
else {
if (debug) {
debug->println("ScaleVal Struct element is invalid. Expected TYPE_INT8 and TYPE_ENUM. Got Type:");
debug->print(enumType, HEX);
debug->print("(");
debug->print(cosemTypeToString[enumType].c_str());
debug->println(")");
}
return false;
}
if (scaler < 0) {
if (obisElement->data->dataType == TYPE_UINT32 || obisElement->data->dataType == TYPE_UINT16) {
obisElement->data->value.value_float = (float)(obisElement->data->value.value_uint * pow(10, scaler));
obisElement->data->dataType = TYPE_FLOAT;
}
else if (/*obisElement->data->dataType == TYPE_INT32 || */obisElement->data->dataType == TYPE_INT16) {
obisElement->data->value.value_float = (float)(obisElement->data->value.value_int * pow(10, scaler));
obisElement->data->dataType = TYPE_FLOAT;
}
}
else {
if (obisElement->data->dataType == TYPE_UINT32 || obisElement->data->dataType == TYPE_UINT16) {
obisElement->data->value.value_uint = (uint32_t)(obisElement->data->value.value_uint * pow(10, scaler));
}
else if (/*dataElement->dataType == TYPE_INT32 || */obisElement->data->dataType == TYPE_INT16) {
obisElement->data->value.value_int = (int32_t)(obisElement->data->value.value_int * pow(10, scaler));
}
}
return true;
}
bool HanReader::decodeListElement(uint16_t& nextPos, ObisElement* obisElement)
{
Element* dataElement = new Element();
if (debugLevel > 0) printObjectStart(nextPos);
// Expect two or three elements. obis, value, [scaleval]
byte dataType = userData[nextPos];
uint8_t elements = userData[nextPos + 1];
nextPos += 2;
if (dataType != TYPE_STRUCTURE) {
if (debug) {
debug->print("Object with invalid type. Expected 0x02(TYPE_STRUCTURE) got ");
debug->print(dataType, HEX);
debug->println(cosemTypeToString[dataType].c_str());
}
return false;
}
if (debugLevel > 2) printObjectStart(nextPos);
uint8_t obisType = userData[nextPos];
uint8_t obisLength = userData[nextPos + 1];
if (obisType != TYPE_OCTET_STRING || obisLength != kObisCodeSize) {
if (debug) {
debug->print("OBIS element is invalid. Expected 0x09(OCTET_STRING) and length 0x06 got type:");
debug->print(obisType, HEX);
debug->print("(");
debug->print(cosemTypeToString[obisType].c_str());
debug->print(") and length:");
debug->println(obisLength, HEX);
}
return false;
}
obisElement->obisOctets = &(userData[nextPos + 2]);
nextPos += 8;
if (!decodeDataElement(nextPos, *dataElement)) {
return false;
}
obisElement->data = dataElement;
// Handle conversion of time sent in an octet string type
if (obisElement->data->dataType == TYPE_OCTET_STRING && obisElement->data->stringLength == 0x0c &&
memcmp(obisElement->obisOctets, kObisClockAndDeviation, kObisCodeSize) == 0) {
ClockDeviation* clockDeviation = new ClockDeviation();
if (decodeClockAndDeviation((byte*)obisElement->data->value.value_string, *clockDeviation)) {
obisElement->data->value.value_dateTime = clockDeviation;
obisElement->data->dataType = TYPE_DATETIME;
}
else {
delete clockDeviation;
}
}
if (elements == 3) {
decodeAndApplyScalerElement(nextPos, obisElement);
}
//char buf[255];
//obisElement->debugString(buf);
//debug->println(buf);
return true;
}
bool HanReader::read(byte data)
{
if (reader.ReadOld(data))
{
if (debug) debug->println("Got message");
//if (debug) { debug->print("Free heap left: ");debug->println(system_get_free_heap_size()); }
//bytesRead = reader.GetRawData(buffer, 0, 512);
bool retUserBufferSuccess = reader.GetUserDataBuffer(userData, userDataLen);
if (retUserBufferSuccess == false) {
if (netLog) netLog("Error. Could not get APDU buffer");
if (debug) debug->print("Error. Could not get APDU buffer");
return false;
}
if (debug)
{
debug->print("Got valid DLMS data (");
debug->print(userDataLen);
debug->println(" bytes):");
//debugPrint(userData, 0, userDataLen);
debug->print("First UserData Byte:");
debug->print(userData[0], HEX);
debug->print(", Last UserData Byte:");
debug->println(userData[userDataLen - 1], HEX);
}
/*
Data should start with E6 E7 00 0F
and continue with four bytes for the InvokeId
*/
if (
userData[0] != 0xE6 ||
userData[1] != 0xE7 ||
userData[2] != 0x00 ||
userData[3] != 0x0F
)
{
if (debug) debug->println("Invalid HAN data: Start should be E6 E7 00 0F");
return false;
}
if (debug) debug->println("HAN dataheader is valid");
// So this is where we need to differentiate between Aidon, Kaifa and Kamstrup meters.
// From what I've gathered Kaifa and Kamstrup shows in an untyped datetime with just
// the length is a preamble. We still start with +9 position into the PDU.
int firstStructurePos = 9;
// If the byte is 0x0C then we can assume that it is Kaifa or Kamstrup
byte firstByte = userData[firstStructurePos];
if (firstByte == 0x0C) {
// We don't support Kaifa or Kamstrup yet.
return decodeKaifaKamstrupMeter();
}
else {
return decodeAidonMeter();
}
}
return false;
}
bool HanReader::decodeAidonMeter() {
int firstStructurePos = 9;
byte dataType = userData[firstStructurePos];
uint8_t listLength = userData[firstStructurePos + 1];
if (dataType != TYPE_LIST)
{
if (debug) debug->println("Error. We expect the root element to be a list.");
return false;
}
uint16_t curPos = firstStructurePos + 2;
for (int listItemIndex = 0; listItemIndex < listLength; listItemIndex++) {
if (debug) debug->print("Decoding element: ");
if (debug) debug->println(listItemIndex);
ObisElement* obisElement = new ObisElement();
if (decodeListElement(curPos, obisElement)) {
this->cosemObjectList.push_back(obisElement);
}
else {
obisElement->Reset();
delete obisElement;
}
}
return true;
}
// This code is just a sketch to evaluate support for kaifa/kamstrup.
// Not tested.
bool HanReader::decodeKaifaKamstrupMeter() {
int firstStructurePos = 9;
int nextPos = firstStructurePos;
// First we get an untyped date time.
// byte dataType = userData[firstStructurePos];
byte dateTimeLen = userData[nextPos];
if (dateTimeLen != 0x0C)
{
if (debug) debug->println("Error. We expect the first byte to be the date time length.");
return false;
}
ObisElement* dateTime = new ObisElement();
ClockDeviation* clockDeviation = new ClockDeviation();
if (decodeClockAndDeviation((byte*)userData[nextPos+1], *clockDeviation)) {
dateTime->data = new Element();
dateTime->data->value.value_dateTime = clockDeviation;
dateTime->data->dataType = TYPE_DATETIME;
}
else {
delete clockDeviation;
}
nextPos += 0xC;
uint8_t structDataType = userData[nextPos]; // 0x02
uint8_t structElements = userData[nextPos+1]; // e.g. 0x19
if (structDataType != TYPE_STRUCTURE)
{
if (debug) debug->println("Error. We expect the root element to be a structure.");
return false;
}
/*
uint16_t curPos = firstStructurePos + 2;
for (int listItemIndex = 0; listItemIndex < listLength; listItemIndex++) {
if (debug) debug->print("Decoding element: ");
if (debug) debug->println(listItemIndex);
ObisElement* obisElement = new ObisElement();
if (decodeListElement(curPos, obisElement)) {
this->cosemObjectList.push_back(obisElement);
}
else {
obisElement->Reset();
delete obisElement;
}
}
return true;
*/
return false;
}
void HanReader::debugPrint(byte *buffer, int start, int length)
{
for (int i = start; i < start + length; i++)
{
if (buffer[i] < 0x10)
debug->print("0");
debug->print(buffer[i], HEX);
debug->print(" ");
if ((i - start + 1) % 16 == 0)
debug->println("");
else if ((i - start + 1) % 4 == 0)
debug->print(" ");
}
debug->println("");
}
bool HanReader::read()
{
if (han->available())
{
byte newByte = han->read();
return read(newByte);
}
return false;
}