Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

keepalive.c

Go to the documentation of this file.
00001 
00006 /*
00007  *  The contents of this file are subject to the Mozilla Public License
00008  *  Version 1.0 (the "License"); you may not use this file except in
00009  *  compliance with the License. You may obtain a copy of the License at
00010  *  http://www.mozilla.org/MPL/
00011  *
00012  *  Software distributed under the License is distributed on an "AS IS"
00013  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
00014  *  License for the specific language governing rights and limitations
00015  *  under the License.
00016  *
00017  *  The Original Code is legOS code, released October 2, 1999.
00018  *
00019  *  The Initial Developer of the Original Code is Markus L. Noga.
00020  *  Portions created by Markus L. Noga are Copyright (C) 1999
00021  *  Markus L. Noga. All Rights Reserved.
00022  *
00023  *  Contributor(s): 
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 // keepalive settings
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 }

Generated on Fri Feb 25 08:02:39 2005 for brickos by  doxygen 1.3.9.1