-
Notifications
You must be signed in to change notification settings - Fork 8
/
vitalk.c
116 lines (99 loc) · 2.65 KB
/
vitalk.c
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
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <unistd.h>
#include "vito_io.h"
#include "telnet.h"
#include "vito_parameter.h"
// This define enables communication with Vitodens:
#define VITOCOM
// Global:
fd_set master_fds; // Aktive Filedeskriptoren fuer select()
fd_set read_fds; // Ergebnis des select() - Aufrufs
// Signal Handler:
void exit_handler( int exitcode )
{
printf("\n");
fprintf(stderr, "\nAbort caught!\n" );
#ifdef VITOCOM
sleep(3);
vito_close();
closetty();
#endif
exit( exitcode );
}
int main(int argc, char **argv)
{
// Option processing
int c;
// Diverse Options:
char *tty_devicename = NULL;
// Struktur fuer select() timeout
struct timeval *timeout = (struct timeval *) malloc( sizeof(struct timeval) );
// Option processing with GNU getopt
while ((c = getopt (argc, argv, "hft:")) != -1)
switch(c)
{
case 'h':
printf("Vitalk, Viessmann Vitodens 300 (B3HA) Interface\n"
" (c) by KWS, 2013\n\n"
"Usage: vitalk [option...]\n"
" -h give this help list\n"
" -f activate framedebugging\n"
" -t <tty_dev> set tty Devicename\n"
);
exit(1);
case 'f':
frame_debug = 1;
break;
case 't':
tty_devicename = optarg;
break;
case '?':
exit (8);
}
// Do some checks:
if ( !tty_devicename )
{
fprintf(stderr, "ERROR: Need tty Devicename!\n");
exit(5);
}
/////////////////////////////////////////////////////////////////////////////
signal(SIGINT, exit_handler);
signal(SIGHUP, exit_handler);
// to prevent dieing by writing to closed sockets
// // -> We handle this locally
signal(SIGPIPE, SIG_IGN);
#ifdef VITOCOM
opentty( tty_devicename );
vito_init();
#endif
// Das machen wir sicherheitshalber erst nach vito_init(), fuer den Fall dass
// ein client sehr schnell ist:
telnet_init();
// Main Event-Loop. (Kann nur durch die Signalhandler beendet werden.)
for (;;)
{
timeout->tv_sec = 60;
timeout->tv_usec = 0;
read_fds = master_fds;
if ( select ( MAX_DESCRIPTOR+1, &read_fds, NULL, NULL, timeout ) > 0 ) // SELECT
{
telnet_task();
}
// Nach einer gewissen Zeit der Inaktivitaet wird das 300er Protokoll
// anscheinend wieder deaktiviert. (ca. nach 10 minuten)
// Daher haben wir hier eine Keepalive-Funktion:
if ( time(NULL) - vito_keepalive > 500 )
{
//fprintf( stdout, "Keepalive: %s\n", get_v("deviceid") );
get_v("deviceid");
}
}
}