1 /* 2 TSA Liedetector project. 3 -- RKeene 4 */ 5 6 #include <stdio.h> 7 #ifndef GO32 8 #include <asm/io.h> 9 #define OUTBYTE(x,y) outb(x,y) 10 #define INBYTE(x) inb(x) 11 #define IOPERM(x,y,z) ioperm(x,y,z) 12 #else 13 #include <inlines/pc.h> 14 #define OUTBYTE(x,y) outportb(x,y) 15 #define INBYTE(x) inportb(x) 16 #define IOPERM(x,y,z) (1) 17 #endif 18 #include <unistd.h> 19 #include <string.h> 20 #include <signal.h> 21 #define CNT 8 22 #define DEBUG 0 23 int BASE=0x2f8; /* default to ttyS1 (COM2) */ 24 int NOTERM=0; 25 26 27 /* Clear the screen with ANSI escape sequences. */ 28 void clear(void) { 29 write(STDOUT_FILENO, "\033[2J\033[H",7); 30 fsync(STDOUT_FILENO); 31 } 32 33 34 /* Reset the port turnning OFF the led. */ 35 void handleC(void) { 36 if (NOTERM==1) { 37 signal(SIGINT,(void *)handleC); 38 #ifndef GO32 39 signal(SIGTSTP,(void *)handleC); 40 #endif 41 return; 42 } 43 OUTBYTE(2,BASE+4); 44 IOPERM(BASE,CNT,0); 45 printf("\033[0m"); 46 clear(); 47 printf("Killed\n"); 48 exit(1); 49 } 50 51 void killed(void) { 52 NOTERM=0; 53 handleC(); 54 printf("Hmm.. handleC() failed to terminate! This is bad.. exiting.\n"); 55 while(1) { exit(127); } 56 } 57 58 void printstatus(int online, int value) { 59 char sonline[31]; 60 char liar[31]; 61 int trueValue; 62 if (online==1) { strcpy(sonline,"Connected "); } else { strcpy(sonline,"Not connected"); } 63 trueValue=((value-61)*-1); 64 if (trueValue<30) { strcpy(liar,"the truth"); } else { strcpy(liar,"a lie "); } 65 printf("%c[0m%c[H",27,27); 66 printf("%c[1;37;44m%c[KStatus: %s %c[%i;37;44m Measuring: %i\n%c[%i;37;41m%c[KSubject is telling %s",27,27,sonline,27,online,trueValue,27,online,27,liar); 67 printf("\n"); 68 } 69 70 /* I was REALLY bored... */ 71 void flashled(char morse[128]) { 72 int i,online,length=0,delay=150000; 73 int lenstr=strlen(morse); 74 OUTBYTE(1,BASE+4); 75 for (i=0;i<5;i++) { online=INBYTE(BASE+6); } 76 if (online!=128) { return; } 77 for (i=0;i<lenstr;i++) { 78 if (morse[i]=='.') length=150000; 79 if (morse[i]=='-') length=450000; 80 if (morse[i]==',') { usleep(650000); } 81 if (morse[i]<=46 && morse[i]>=45) { 82 OUTBYTE(1,BASE+4); 83 usleep(length); 84 OUTBYTE(2,BASE+4); 85 usleep(delay); 86 } 87 } 88 } 89 90 int main(int argc, char *argv[]) { 91 int i; 92 int val[CNT]; 93 int online=2; 94 int ionline; 95 int valueLie=0; 96 if (argc>=2) { 97 if (strncasecmp("COM1",argv[1],4)==0) { BASE=0x3f8; } 98 if (strncasecmp("COM2",argv[1],4)==0) { BASE=0x2f8; } 99 } 100 #if (DEBUG==1) 101 oldmain(); 102 return(0); 103 #endif 104 signal(SIGINT,(void *)handleC); 105 signal(SIGTERM,(void *)killed); 106 #ifndef GO32 107 signal(SIGTSTP,(void *)handleC); 108 if (IOPERM(BASE,CNT,1)==-1) { printf("%c[0mYou must be %c[1;37mroot%c[0m to run this program.\n",27,27,27); return(1); } 109 #endif 110 flashled(".-.,---,-.--,-.-,.,.,-.,.,"); 111 clear(); 112 while(1) { 113 usleep(1); 114 for (i=0;i<CNT;i++) { 115 OUTBYTE(1,BASE+i); 116 val[i]=INBYTE(BASE+i); 117 } 118 ionline=((val[6]-1)/128); 119 if (online!=ionline || val[0]!=valueLie) { 120 online=ionline; 121 valueLie=val[0]; 122 printstatus(online,val[0]); 123 } 124 } 125 return(0); 126 } 127 128 129 /* 130 Old main, used for learning what pins do what and what not, prints 131 status. 132 */ 133 int oldmain(void) 134 { 135 int val[CNT]; 136 int i; 137 IOPERM(BASE,CNT,1); 138 while (1) { 139 for (i=0;i<CNT;i++) 140 { 141 OUTBYTE(1,BASE+i); 142 val[i]=INBYTE(BASE+i); 143 printf("%i ",val[i]); 144 } 145 printf("\n"); 146 } 147 return 0; 148 } liedetector.c is the source code. |