-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatalogparser.cpp
More file actions
213 lines (185 loc) · 4.74 KB
/
Copy pathdatalogparser.cpp
File metadata and controls
213 lines (185 loc) · 4.74 KB
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
#include "field.h"
#include "datalogparser.h"
#include <iostream>
#include <string.h>
DatalogParser::DatalogParser(int device)
{
if (device == DEVICE_ID_WARMTEPOMP)
{
config = warmtepompConfig;
fieldCount = FIELD_COUNT_WARMTEPOMP;
}
else if (device == DEVICE_ID_AUTOTEMP)
{
config = autotempConfig;
fieldCount = FIELD_COUNT_AUTOTEMP;
}
else
{
throw "Unknown device type";
}
// initialize to empty values
for (int x=0; x<fieldCount;x++)
{
sprintf(fieldValues[x], "");
sprintf(previousValues[x], "");
}
}
float DatalogParser::ParseSignedIntDec2(unsigned char *buffer)
{
int num = ((int) buffer[0] << 8) + buffer[1];
if (num >= 32768) {
num -= 65536;
}
return num / 100.0;
}
int DatalogParser::ParseByte(unsigned char *buffer)
{
return buffer[0];
}
unsigned int DatalogParser::ParseUnsignedInt(unsigned char *buffer)
{
return ((unsigned int) buffer[0] << 8) + buffer[1];
}
bool DatalogParser::IsChecksumValid(unsigned char *buffer, int length)
{
/*
c# code:
int runningsum = 0;
int index = 0;
while (index < data.Length)
{
runningsum += data[index];
index++;
}
int checksum = 256 - (runningsum % 256);
if (checksum == 256)
return 0;
return (byte) checksum;
*/
// printf("Calculating checksum for %d bytes \n", length);
int runningsum = 0x80; // the destination was not included in the header. hard coded here.
int index = 0;
while (index < (length-1))
{
runningsum += buffer[index];
// printf("runningsum+=%d\n", buffer[index]);
index++;
}
int checksum = 256 - (runningsum % 256);
if (checksum == 256)
return checksum = 0;
// printf("Checksum calculated %d, checksum in data %d\n", checksum, buffer[length-1]);
return checksum == buffer[length-1];
}
bool DatalogParser::ParseWithHeader(unsigned char *buffer, int length)
{
if (!IsChecksumValid(buffer, length))
{
printf("Checksum invalid, discarding data\n");
return false;
}
Parse(buffer+5, length-5);
return true;
}
// parses the data without the header
void DatalogParser::Parse(unsigned char *buffer, int length)
{
// TODO: Nothing is done with the length yet. Should check exceeding the length
for (int x=0; x<fieldCount;x++)
{
strcpy(previousValues[x], fieldValues[x]);
if (config[x].fieldType == SignedIntDec2)
{
float value = ParseSignedIntDec2(buffer + config[x].offset);
//std::cout << config[x].label << "=" << value << std::endl;
sprintf(fieldValues[x], "%.2f", value);
}
else if (config[x].fieldType == Byte)
{
int value = ParseByte(buffer + config[x].offset);
//std::cout << config[x].label << "=" << value << std::endl;
sprintf(fieldValues[x], "%d", value);
}
else if (config[x].fieldType == UnsignedInt)
{
unsigned int value = ParseUnsignedInt(buffer + config[x].offset);
//std::cout << config[x].label << "=" << value << std::endl;
sprintf(fieldValues[x], "%d", value);
}
}
}
char *DatalogParser::FieldValue(int index)
{
if (index>fieldCount)
{
sprintf(errorMessage, "Index too large");
return errorMessage;
}
return fieldValues[index];
}
int DatalogParser::GetFieldIndex(string label)
{
int x;
for (x=0; x<fieldCount; x++)
{
if (config[x].label == label)
break;
}
if (x==fieldCount)
{
return -1;
}
return x;
}
char *DatalogParser::FieldValue(string label)
{
int x = GetFieldIndex(label);
if (x==-1)
{
sprintf(errorMessage, "Field not found");
return errorMessage;
}
return FieldValue(x);
}
bool DatalogParser::FieldChanged(int index)
{
// printf("Comparing '%s' to '%s'\n", fieldValues[index], previousValues[index]);
return strcmp(fieldValues[index], previousValues[index])!=0;
}
bool DatalogParser::FieldChanged(string label)
{
int x = GetFieldIndex(label);
// printf("Field index %d\n", x);
if (x==-1)
return false;
return FieldChanged(x);
}
int DatalogParser::char2int(char input)
{
if(input >= '0' && input <= '9')
return input - '0';
if(input >= 'A' && input <= 'F')
return input - 'A' + 10;
if(input >= 'a' && input <= 'f')
return input - 'a' + 10;
throw std::invalid_argument("Invalid input string");
}
// This function assumes src to be a zero terminated sanitized string with
// an even number of [0-9a-f] characters, and target to be sufficiently large
void DatalogParser::hex2bin(const char* src, unsigned char* target)
{
while(*src && src[1])
{
*(target++) = char2int(*src)*16 + char2int(src[1]);
src += 2;
}
}
void DatalogParser::Parse(char *hexstring)
{
int length = strlen(hexstring) /2;
unsigned char *buffer = new unsigned char [length];
hex2bin(hexstring, buffer);
Parse(buffer + 6, length - 6);
delete[] buffer;
}