forked from mqu/vitalk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvitalk.c
132 lines (110 loc) · 2.9 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#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"
#include "version.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
short unsigned int vitalkport = PORT;
// Signal Handler:
void exit_handler( int exitcode )
{
printf("\n");
fprintf(stderr, "\nAbort caught!\n" );
#ifdef VITOCOM
sleep(3);
vito_close();
closetty();
#endif
exit( exitcode );
}
#define xstr(s) str(s)
#define str(s) #s
#define PORT_S xstr(PORT)
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:p:")) != -1)
switch(c)
{
case 'h':
printf("viTalk, Viessmann Optolink Interface\n"
" (c) by KWS, 2013\n"
" version %s\n\n"
"Usage: vitalk [option...]\n"
" -h give this help list\n"
" -f activate framedebugging\n"
" -t <tty_dev> set tty Devicename\n"
" -p <port> set port (default: " PORT_S ")\n",
version
);
exit(1);
case 'f':
frame_debug = 1;
break;
case 't':
tty_devicename = optarg;
break;
case 'p':
vitalkport = atoi(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");
}
}
}