-
Notifications
You must be signed in to change notification settings - Fork 0
/
vito_p300.cpp
433 lines (378 loc) · 10.9 KB
/
vito_p300.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
430
431
432
433
/*
* vito_p300.cpp
* vConnect
*
* Created by Frank Nobis on 23.11.11.
* Copyright 2011 Radio-DO.de. All rights reserved.
*
*/
#include <iostream>
#include <cerrno>
#include <sys/ioctl.h>
#include "vito_p300.h"
#include "unit.h"
void command::calcCRC(int bufferEnd)
{
m_ucCRC=0;
for (int i=1; i< bufferEnd-1; i++) {
m_ucCRC += m_ucBuffer[i];
}
m_ucCRC &= 0xff;
DPRINT(fprintf(stderr, "m_ucCRC = %x\n", m_ucCRC);)
}
bool command::checkCRC()
{
calcCRC(m_totalBufferLen);
return m_ucCRC == m_ucBuffer[m_totalBufferLen-1];
}
int bcd2int(unsigned char b)
{
int i = 0;
i = (b >> 4) & 0x0f;
i *= 10;
i += b & 0x0f;
return i;
}
void command::sendCommand()
{
int w = 0;
unsigned char byte = 0;
clear_Buffer();
m_ucBuffer[0] = 0x41; // 0x41 is the start of frame
m_ucBuffer[1] = 1+1+2+1; // 1 command, 1 subcommand, 2 addr, 1 read
m_ucBuffer[2] = 0x00; // 0x00 to read, 0x01 to write
m_ucBuffer[3] = 0x01;
m_ucBuffer[4] = m_uiAddr >> 8; // high byte
m_ucBuffer[5] = m_uiAddr & 0xff;// low byte of commands address
m_ucBuffer[6] = m_ucLen;
m_totalBufferLen = 8;
calcCRC(m_totalBufferLen);
m_ucBuffer[7] = m_ucCRC;
DPRINT(std::cerr << "sendCommand: buffer" << std::endl;)
DPRINT(dumpBuffer(m_ucBuffer, 8);)
w = write(m_fd, m_ucBuffer, m_ucBuffer[1]+3); // payload + 3 overhead, framestart, length and crc
if (w == -1) {
perror("write failed\n");
}
if (w > 0) {
DPRINT(fprintf(stderr, "wrote %d bytes\n", w);)
}
clear_Buffer(); // clear the buffer and start reading the answer
byte=readOneByte();
DPRINT(dumpBuffer(&byte, 1, stderr);)
if (byte != 0x06) {
DPRINT(printf("stderr, got no ACK %x\n", byte);)
m_bCommandFailed = true;
return; // in this case no sense to read further
}
byte = readOneByte();
DPRINT(dumpBuffer(&byte, 1, stderr);)
DPRINT(fprintf(stderr, "got ACK\n");)
if (byte != 0x41) {
DPRINT(fprintf(stderr, "expected 0x41 got %x\n", byte);)
m_bCommandFailed = true;
return; // in this case no sense to read further
}
intoBuffer(0x41);
DPRINT(fprintf(stderr, "got framestart\n");)
byte = readOneByte();
if (byte == 0) {
m_bCommandFailed = true;
return; // in this case no sense to read further
}
intoBuffer(byte); // This is the length of message
DPRINT(fprintf(stderr, "got %d bytes to read further\n", byte);)
for (int i=0; i<byte+1; i++) { // typical one off bug here crc is NOT counted remember!
// Fill the buffer with the rest of the message
intoBuffer(readOneByte());
}
// time to do some checks
// if crc is good then check further otherwise the buffer and the return value
// is invalid. In that case set the failed flag.
// until here a complete buffer has been received.
// Frame is valid in terms of framestart byte 0x41
// now check crc and the other bytes
DPRINT(dumpBuffer(m_ucBuffer, m_totalBufferLen, stderr);)
DPRINT(fprintf(stderr, "checksumm is %s\n", checkCRC() ? "good" : "bad");)
if (checkCRC()) {
// Wenn Anfrage, dann erwarten wir b2 und b3 gleich 1
if ((m_ucBuffer[1] != 1) and (m_ucBuffer[2] != 1)) {
DPRINT(std::cerr << "(b1 and b2) != 1" << std::endl;)
m_bCommandFailed = true;
return;
}
if (m_cMyUnit != NULL) { // Anhand der Länge und des Types innerhalb der Unit dekodieren
m_cMyUnit->decodeBuffer(m_ucBuffer+7, m_ucLen);
// Ausgabe
}
}
#if 0
if (checkCRC()) {
// Wenn Anfrage, dann erwarten wir b2 und b3 gleich 1
if ((m_ucBuffer[1] != 1) and (m_ucBuffer[2] != 1)) {
DPRINT(std::cerr << "(b1 and b2) != 1" << std::endl;)
m_bCommandFailed = true;
return;
}
// Alles was hier folgt, wird noch auf unit basierte Umrechnung angepasst
// Beispiel ist unten zu sehen.
unsigned int ival = 0;
m_ucLen = m_ucBuffer[6];
if (m_ucLen == 1) {
m_val.iVal = m_ucBuffer[7];
}
if ((m_ucLen == 2) && (m_cMyUnit == NULL)) {
ival = (m_ucBuffer[8] & 0xff) << 8;
ival += m_ucBuffer[7];
if (ival >= 1<<15) {
ival ^= 0xffff;
ival++;
m_val.iVal = -ival;
} else {
m_val.iVal = ival;
}
} else {
m_cMyUnit->decodeBuffer(m_ucBuffer+7,2);
}
if (m_ucLen == 4) {
ival = (m_ucBuffer[10] & 0xff) << 24;
ival |= (m_ucBuffer[9] & 0xff) << 16;
ival |= (m_ucBuffer[8] & 0xff) << 8;
ival |= m_ucBuffer[7];
m_val.iVal = ival;
}
if (m_ucLen == 8 && m_cMyUnit == NULL) { // z.Z. ist dies die Systemzeit TODO: auf unabhängige Units umstellen
struct tm vtime;
bzero(&vtime, sizeof(vtime));
// Jetzt wird die Unixzeit zusammengebaut.
// Viessmann liefert acht Bytes mit einer BCD kodierten Zeit
vtime.tm_year = (bcd2int(m_ucBuffer[7])-19)*100 + bcd2int(m_ucBuffer[8]);
vtime.tm_mon = bcd2int(m_ucBuffer[ 9])-1;
vtime.tm_mday = bcd2int(m_ucBuffer[10]);
// in m_ucBuffer[11] ist der Wochentag kodiert. Das ist aber anhand der Unixzeit irrelevant.
vtime.tm_hour = bcd2int(m_ucBuffer[12]);
vtime.tm_min = bcd2int(m_ucBuffer[13]);
vtime.tm_sec = bcd2int(m_ucBuffer[14]);
time_t t = mktime(&vtime);
std::cerr << "time is " << t << std::endl;
if (t == -1) {
DPRINT(std::cerr << "mktime returned -1" << std::endl;)
m_val.iVal = 0;
m_bCommandFailed = true;
} else {
m_val.iVal = t;
}
}
// if (m_ucLen == 8 && m_cMyUnit != NULL) {
// m_cMyUnit->decodeAsCycleTime(m_ucBuffer+7,8);
// }
} else {
m_val.iVal = 0;
m_bCommandFailed = true;
}
// if ((m_val.iVal > 130) or m_bCommandFailed) {
// // just to check why there are values around 6k
// FILE *dumpFile = NULL;
// dumpFile = fopen("/tmp/vconn.dump", "a");
// if (dumpFile == NULL) {
// m_val.iVal = 0;
// m_bCommandFailed = true;
// return;
// }
// time_t t = time(NULL);
// struct tm *ts = localtime(&t);
//
// fprintf(dumpFile, "+++\nvConnect large value at time: %s\n", asctime(ts));
// fprintf(dumpFile, "val = %d\n", m_val.iVal);
// fprintf(dumpFile, "m_bCommandFailed = %s\n", m_bCommandFailed ? "true" : "false");
// fprintf(dumpFile, "buffer:\n");
// dumpBuffer(m_ucBuffer, m_totalBufferLen, dumpFile);
// fprintf(dumpFile, "+++\n\n");
// fclose(dumpFile);
// }
#endif
}
char command::readOneByte() {
int n;
char c;
n = read(m_fd, &c, 1);
if (n ==1) {
return c;
}
if (n == 0) {
fprintf(stderr, "read error in: readOneByte: read returned wirh 0\n");
}
if (n == -1) {
fprintf(stderr, "read error in: readOneByte\n");
perror("x");
}
return 0;
}
void command::dumpBuffer(unsigned char *buffer, int len, FILE *dumpTo) {
int i;
for (i=0; i<len; i++) {
fprintf(dumpTo, "buffer[0x%02d] = 0x%02x\n", i, (unsigned char) buffer[i]);
}
}
std::ostream& operator<< (std::ostream &out, command cCommand)
{
if (cCommand.m_bCommandFailed) {
out << cCommand.get_myName() << ".value U";
} else {
out << cCommand.get_myName() << ".value " << cCommand.get_UnitValue();
}
return out;
}
void p300::open_socket(const char *hostName, int port) throw(char *)
{
struct sockaddr_in serv_addr;
struct hostent *server;
m_fd = socket(AF_INET, SOCK_STREAM, 0);
if (m_fd < 0) {
std::string e1 = "opening socket: ";
std::string e2 = strerror(errno);
throw(e1 + e2);
}
server = gethostbyname(hostName);
if (server == NULL) {
throw("no such host");
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(port);
if (connect(m_fd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
perror("ERROR connecting");
m_file = fdopen(m_fd, "r+");
DPRINT(fprintf (stderr, "Connected:\n");)
}
void p300::open_device(const char *deviceName, int baud, int stopbits, int parity)
{
int status=0;
struct termios currentTermios;
m_bInitSuccess = false; // at this stage nothing is initialised
// open the device
if ((m_fd = open(deviceName, O_RDWR) < 0)) {
throw ("open failed");
}
// get the linediscipline and save it
status = tcgetattr(m_fd, &m_savedTermios);
if (status < 0) {
throw ("tcgetattr failed");
}
currentTermios = m_savedTermios;
// set the viessmann values 4800 8NE2
cfmakeraw(¤tTermios);
currentTermios.c_cflag = (CLOCAL | B4800 | CS8 | CREAD| PARENB | CSTOPB);
status = tcsetattr(m_fd, TCSAFLUSH, ¤tTermios);
if (status < 0) {
throw ("tcsetattr failed");
}
/* DTR High fuer Spannungsversorgung */
int modemctl = 0;
ioctl(m_fd, TIOCMGET, &modemctl);
modemctl |= TIOCM_DTR;
status = ioctl(m_fd,TIOCMSET,&modemctl);
if (status < 0) {
throw ("ioctl failed");
}
// reopen the file as a stream
m_file = fdopen(m_fd, "r+");
// now we good
m_bInitSuccess = true;
}
void p300::init_vito()
{
char val = 0;
char initVal = 0x04;
char initSeq[] = { 0x16, 0, 0 };
m_bInitSuccess = false;
DPRINT(fprintf(stderr, "init: start:\n");)
flush_file();
init_again:
DPRINT(fprintf(stderr, "init: write initVal:\n");)
p300_write(&initVal);
val = p300_read();
if (val == 0x6) { // in this state a 0x06 should not happen, but it
// did, sometimes when there is no cleanup at the end
// so write the init sequence again
DPRINT(fprintf(stderr, "init; state 1 got 6: init sequence done??? Let's try again...\n");)
//m_bInitSuccess = true;
//flush_file();
//return;
goto init_again;
}
do {
DPRINT(fprintf(stderr, "init: state 2 write initSeq:\n");)
p300_write(initSeq, sizeof(initSeq));
val = p300_read();
DPRINT(fprintf(stderr, "init: state 2 val=%d\n", val);)
} while (val == 0x05);
// val = p300_read();
if (val == 0x6) {
DPRINT(fprintf(stderr, "init; state 3 got 6: init sequence done\n");)
m_bInitSuccess = true;
}
flush_file();
DPRINT(fprintf(stderr, "init: done:\n");)
}
p300::~p300() {
// last thing to do is write a 4 to reset vito to default sending 5's
char c=0x04;
p300_write(&c, 1);
}
ssize_t /* Read "n" bytes from a descriptor. */
readn(int fd, void *vptr, size_t n)
{
size_t nleft;
ssize_t nread;
char *ptr;
ptr = (char *)vptr;
nleft = n;
while (nleft > 0) {
if ( (nread = read(fd, ptr, nleft)) < 0) {
if (errno == EINTR)
nread = 0; /* and call read() again */
else
return(-1);
} else if (nread == 0)
break; /* EOF */
nleft -= nread;
ptr += nread;
}
return(n - nleft); /* return >= 0 */
}
void p300::flush_file() {
DPRINT(fprintf(stderr, "flush_file:\n");)
char string[100];
tcflush(m_fd, TCIOFLUSH); // ok, this IS low level, but hey
fseek(m_file, 0, SEEK_END);
fcntl(m_fd,F_SETFL,O_NONBLOCK);
while(readn(m_fd,string,sizeof(string))>0);
fcntl(m_fd,F_SETFL,!O_NONBLOCK);
}
char p300::p300_read()
{
int n=0;
char c=0;
n = read(m_fd, &c, 1);
if (n < 0) {
perror("p300_read error");
}
return c;
}
void p300::p300_write( char *c, int count)
{
int n=0;
n = write(m_fd, c, count);
if (n < 0) {
perror("p300_write error\n");
exit(EXIT_FAILURE);
}
DPRINT(fprintf(stderr, "p300_write: n=%d, count=%d\n", n, count);)
assert(n == count);
DPRINT(fprintf(stderr, "p300_write: %d\n", n);)
tcdrain(m_fd);
}