-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKCanUVR.cpp
361 lines (302 loc) · 7.81 KB
/
KCanUVR.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
/*
*
* Copyright (C) 2014 Jürg Müller, CH-5524
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see http://www.gnu.org/licenses/ .
Wird nicht länger unterstützt!
*/
#if defined(__UVR__)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "NTypes.h"
#include "KCanUVR.h"
#if defined(__MYSQL__)
#include "KMySql.h"
#endif
#include "KElsterTable.h"
#include "NUtils.h"
#include "KHttpClient.h"
KCanUVR::KCanUVR()
{
Trace = false;
}
bool KCanUVR::Init(const char * CanDev)
{
Trace = false;
return KCanServer::Init(CanDev);
}
int KCanUVR::sniff_index(unsigned can_id)
{
if ((can_id & 0x7f) > 62 ||
(can_id & 0x7f) == 0)
return -1;
switch (can_id & ~0x7f)
{
case 0x180:
return 0;
case 0x200:
return 1;
case 0x280:
return 2;
case 0x300:
return 3;
case 0x380:
return 4;
default:
return -1;
}
}
bool KCanUVR::AddToSniffed(const KCanFrame & Frame)
{
int idx = sniff_index(Frame.Id);
if (idx < 0)
return false;
sniffed_frame[idx][Frame.Id & 0x3f] = Frame;
return true;
}
const KCanFrame * KCanUVR::GetSniffFrame(unsigned can_id)
{
int idx = sniff_index(can_id);
if (idx < 0)
return NULL;
return &(sniffed_frame[idx][can_id & 0x3f]);
}
void KCanUVR::EmptyServer()
{
bool Ok;
KCanFrame Frame;
do
{
Ok = GetFrame(Frame);
if (Ok)
{
AddToSniffed(Frame);
if (Trace)
Frame.PrintFrame();
}
} while (Ok);
}
bool KCanUVR::GetIdAndChan(const char * can_val, unsigned & can_id, int & chan)
{
can_id = NUtils::GetHex(can_val);
bool Ok = *can_val == '.';
if (Ok)
{
TInt64 res;
can_val++;
Ok = NUtils::GetInt(can_val, res);
can_id = (unsigned) res;
}
if (Ok)
Ok = *can_val == 0;
if (Ok)
Ok = KCanUVR::sniff_index(can_id) >= 0;
if (Ok)
{
if ((can_id & 0xf80) == 0x180)
Ok = 1 <= chan && chan <= 16;
else
Ok = 1 <= chan && chan <= 16;
}
return Ok;
}
#if defined(__CAN__) && defined(__SCAN__)
#include <time.h>
#include "KXmlParser.h"
#include "KTcpCanClient.h"
int main(int argc, char* argv[])
{
printf("Volkszaehler can-bus UVR logger\n"
"copyright (c) 2014 Jürg Müller, CH-5524\n\n");
bool trace = !strcmp(argv[argc-1], "trace");
if (trace)
argc--;
if (argc != 2)
{
printf("usage:\n"
" can_vz_uvr <xml configuration> [ \"trace\" ]\n\n"
"example: can_vz_uvr VZ_config.xml\n\n");
return 0;
}
KXmlNode Root;
const KXmlNode * Node;
KStream buffer;
if (!KXmlParser::ParseFile(argv[1], &Root))
return -1;
// device Name="can0"
// template Name=...
// ip_address Name="...
// sleep="104"
// channel mehrfach
const char * device = Root.FindNodeAttr("device", "Name");
const char * template_ = Root.FindNodeAttr("template", "Name");
const char * ip_address = Root.FindNodeAttr("ip_address", "Name");
const char * secs = Root.FindNodeAttr("sleep", "Name");
int seconds = 104;
#if defined(__MYSQL__)
bool use_mysql = Root.Find("mysql") != NULL;
KMySql MySql;
#else
bool use_mysql = false;
#endif
if (!use_mysql)
{
if (!test_template(template_))
return -1;
if (!ip_address || strlen(ip_address) == 0)
{
printf("IP address is missing\n");
return -1;
}
}
#if defined(__MYSQL__)
if (use_mysql &&
!MySql.GetXmlParams(&Root))
return -1;
#endif
if (secs)
{
TInt64 sec;
if (NUtils::GetInt(secs, sec) &&
!*secs &&
60 <= sec && sec <= 3600)
seconds = (int) sec;
}
if (trace)
{
printf("device: %s\n", device);
printf("sleep: %d secs\n", seconds);
if (!use_mysql)
printf("IP address: %s\n", ip_address);
printf("\n");
}
buffer.SetSize(4000);
if (!Root.channel_check(use_mysql, template_, &buffer))
return -1;
KCanUVR Can;
KHttpClient TcpClient;
time_t timer;
time_t new_timer;
TcpClient.mTrace = trace;
TcpClient.SetBlockMode(bmBlocking);
TcpClient.SetRemoteHost(ip_address);
TcpClient.SetRemotePort(80);
if (!Can.Init(device))
{
printf("device \"%s\" not connected\n", Can.GetDev());
Can.Halt();
return -1;
}
Can.Trace = trace;
Can.Resume();
NUtils::SleepMs(10000);
while (!Can.Terminated())
{
time(&timer);
Can.EmptyServer();
#if defined(__MYSQL__)
if (use_mysql)
{
MySql.Open();
}
#endif
// der "log" ist für die Aufdeckung von Fehlerquellen gedacht
FILE * log = NULL; // fopen("uvr.log", "ab+");
Node = NULL;
while (Root.GetNextNode(Node))
{
if (!strcmp(Node->mName, "channel"))
{
const char * can_val = Node->FindAttr("can_val"); // <can_id>.<1..10> (hex.)
unsigned can_id = 0;
int chan;
bool Ok = Can.GetIdAndChan(can_val, can_id, chan);
const KCanFrame * Frame = Can.GetSniffFrame(can_id);
if (Ok)
Ok = Frame != NULL && Frame->TimeStampDay > 0;
if (Ok)
{
int Days = Frame->TimeStampDay;
int Ms = Frame->TimeStampMs;
bool IsNew = Days > Node->time_days;
if (!IsNew &&
Days == Node->time_days)
IsNew = Ms - Node->time_ms > 10;
if (!IsNew)
NUtils::Time(Days, Ms);
const_cast<KXmlNode *>(Node)->time_days = Days;
const_cast<KXmlNode *>(Node)->time_ms = Ms;
unsigned short val = 0;
if ((can_id & 0xf80) == 0x180)
{
Ok = Frame->Len >= 2;
val = Frame->Data[0] + 256*Frame->Data[1];
if (chan > 1)
val <<= chan - 1;
val = (val & 0x8000) ? 1 : 0;
} else {
Ok = Frame->Len >= 2*chan;
val = Frame->Data[2*chan-2] + 256*Frame->Data[2*chan-1];
}
const_cast<KXmlNode *>(Node)->can_value = val;
}
if (Ok)
Ok = const_cast<KXmlNode *>(Node)->set_channel_value(&buffer, template_, trace);
if (!Ok)
{
} else
#if defined(__MYSQL__)
if (use_mysql)
{
Ok = MySql.Query(buffer, trace);
if (log)
{
int nowDays, nowMs;
char str[64];
char now[64];
NUtils::Time(nowDays, nowMs);
NUtils::PrintDayTime(nowMs, now);
NUtils::PrintTime(Node->time_days, Node->time_ms, str, false);
fprintf(log, "%s - now: %s\n", str, now);
fprintf(log, "%d: %s\n", (int)Ok, (const char *) buffer.GetMemory());
}
} else
#endif
TcpClient.ServeStream(buffer);
}
}
if (log)
fclose(log);
#if defined(__MYSQL__)
if (use_mysql)
{
MySql.Close();
}
#endif
time(&new_timer);
int del = seconds - (int)(new_timer - timer);
if (del < 45)
del = 45;
while (del > 0)
{
int d = del < 10 ? del : 10;
NUtils::SleepMs(d*1000);
del = del < 10 ? 0 : del - 10;
Can.EmptyServer();
}
}
return 0;
}
#endif
#endif // __UVR__