00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <stdio.h>
00027 #include <stdlib.h>
00028 #include <fcntl.h>
00029 #include <termios.h>
00030 #include <unistd.h>
00031 #include <sys/time.h>
00032 #include <sys/types.h>
00033 #include <signal.h>
00034
00035 #if defined(_WIN32)
00036 #include <windows.h>
00037 #endif
00038
00039 #include "rcxtty.h"
00040 #include "keepalive.h"
00041
00042
00043
00044 #define KEEPALIVE_TIMEOUT_S 3
00045 #define KEEPALIVE_TIMEOUT_US 700000
00046 #define KEEPALIVE_BYTE 0xff
00047
00048 extern int verbose_flag;
00049
00051 const static char keepaliveByte = KEEPALIVE_BYTE;
00052
00054 void keepaliveRenew()
00055 {
00056 static struct itimerval it =
00057 {
00058 {KEEPALIVE_TIMEOUT_S, KEEPALIVE_TIMEOUT_US},
00059 {KEEPALIVE_TIMEOUT_S, KEEPALIVE_TIMEOUT_US}};
00060
00061 setitimer(ITIMER_REAL, &it, NULL);
00062 }
00063
00065 static void keepaliveSend(FILEDESCR fd)
00066 {
00067 if (verbose_flag)
00068 fputs("\nKeepAliveSend: keeping the IR tower alive...",stderr);
00069
00070 if (mywrite(fd, &keepaliveByte, 1) != 1) {
00071 myperror("sending keepalive");
00072 exit(-1);
00073 }
00074 keepaliveRenew();
00075 }
00076
00078 static void keepaliveHandler(int arg)
00079 {
00080 if (verbose_flag)
00081 fputs("\nHandler: keeping the IR tower alive...\n",stderr);
00082 keepaliveSend(rcxFD());
00083 }
00084
00086 void keepaliveInit(void)
00087 {
00088 if (verbose_flag) fputs("KeepAlive Init...", stderr);
00089 signal(SIGALRM, keepaliveHandler);
00090 keepaliveSend(rcxFD());
00091 }
00092
00094 void keepaliveShutdown()
00095 {
00096 static const struct itimerval it =
00097 {
00098 {0, 0},
00099 {0, 0}};
00100
00101 setitimer(ITIMER_REAL, &it, 0);
00102 signal(SIGALRM, SIG_DFL);
00103 }