/* tripdiag.c Trippy diagnostic program Linux version (may also run under NT Posix environment with no mods) Revised Jan 10 2000, Douglas W. Jones From the shell: tripdiag [ ] may include: -d defaults to DIAGDIR -f defaults to defaults to SERIALPORT Performs a diagnostic of the attached Trippy by reading a diagnostic file. If no diagnostic file is specified, the trippy is queried with BT# to get the version number, and the reply is concatenated with the diagnostic directory to make the name of the name of the diagnostic file. The diagnostic file is a text file, read and processed line by line. Each line may be: T: R: -- Anything else is echoed to standard output In the text to be sent to the Trippy, the line is first scanned for the _ charachter. Strings of the form __ are processed two digits (8 bits) at a time and converted as follows: _ABCDEF_ becomes 171<205+239< This is useful for loading bitstrings into the Trippy's memory for use with the IR facility. Invalid hex digits are flagged. */ #define SERIALPORT "/dev/ttyS0" #define DIAGDIR "diag" #include #include #include #include #include #include #include #include #include /************************************ * package to support I/O to Trippy * ************************************/ int tripin, tripout; /* these are set by tripopen */ void tripopen( f ) char * f; { tripin = open( f, O_RDONLY /* | O_NDELAY */ ); if (tripin < 0) { perror( "open trippy input file failed," ); exit(-1); } /* configure trippy input file */ { struct termios term; int stat; term.c_iflag = PARMRK | INPCK; term.c_oflag = 0; term.c_cflag = CS8 | CREAD | PARENB | PARODD | CLOCAL; term.c_lflag = 0; (void) cfsetospeed( &term, B9600 ); (void) cfsetispeed( &term, B9600 ); stat = tcsetattr( tripin, TCSANOW, &term ); if (stat < 0) { perror( "tcsetattr failed," ); exit(-1); } } /* input is now 8-bit odd parity 9600 baud raw I/O */ /* parity error reads as [RUB] NULL, break reads as NULL */ tripout = open( f, O_WRONLY ); /* why not use tripout = dup(tripin); */ /* configure trippy output file */ if (tripout < 0) { perror( "open tripout failed," ); exit(-1); } } void tripputs( s ) char * s; /* put string s out to Trippy */ { int outlen = strlen( s ); int stat; stat = write( tripout, s, outlen ); if (stat < 0) { perror( "write failure in tripputs" ); exit( 0 ); } } char tripgetc() /* await and return one character from Trippy */ { int stat; char ch; stat = read( tripin, &ch, 1 ); if (stat < 0) { perror( "\nRead failed," ); exit( 0 ); } return( ch ); } char tripgets( s, size ) char * s; int size; /* await and return \n terminated line s from Trippy, strlen(s) < size */ { for (;;) { char ch; if (size <= 1) break; ch = tripgetc(ch); *s = ch; s++; size--; if (ch == '\n') break; } if (size > 0) *s = '\0'; } /************************************************************* * package to print strings with printable control chars etc * *************************************************************/ int printable; /* was most recent character printable? */ putoutbegin( s ) char *s; { if (s) fputs( s, stdout ); printable = 0; } putoutend( s ) char *s; { if (printable) fputs( "\" ", stdout ); if (s) fputs( s, stdout ); } putoutchar( ch ) char ch; { int highbit = 0; if (ch & 0x80) { /* first strip off 8th bit into highbit */ if (printable) fputs( "\" ", stdout ); highbit = 1; printable = 0; ch = ch & 0x7F; } if (ch < 0x20) { /* we have control char, output by name */ static char *cc_name[] = { "NULL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK", "BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI", "DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB", "CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US" }; if (printable) putchar( '"' ); putchar(' '); if (highbit) putchar('['); fputs( cc_name[ch], stdout ); if (highbit) putchar(']'); printable = 0; } else if (ch <= '~') { /* we have printable text */ if (highbit) { if (printable) putchar('"'); fputs( " [", stdout ); putchar( ch ); putchar( ']' ); printable = 0; } else { if (!printable) fputs( " \"", stdout ); if ((ch == '"')||(ch == '\\')) putchar('\\'); putchar( ch ); printable = 1; } } else { /* we have rubout */ if (printable) putchar( '"' ); putchar(' '); if (highbit) putchar('['); fputs( "RUB", stdout ); if (highbit) putchar(']'); printable = 0; } } putouts( s ) char * s; { while (*s) { putoutchar( *s ); s++; } } /************************* * Input editing routine * *************************/ void tripfixup( in, out ) char * in; char * out; { while (*in) { if ('_' != *in) { /* normal copy */ *out = *in; in++; out++; } else { /* we have hex prefix */ int term = '<'; /* terminator for byte */ *in++; if ('_' == *in) { /* replace __ with _ */ *out = '_'; *in++; *out++; } else { while (isxdigit(*in)) { char c1 = *in++; if (isxdigit(*in)) { char c2 = *in++; int d1, d2, d3, v; if isdigit(c1) { d1 = c1 - '0'; } else { c1 = toupper(c1); d1 = (c1 - 'A') + 10; } if isdigit(c2) { d2 = c2 - '0'; } else { c2 = toupper(c2); d2 = (c2 - 'A') + 10; } v = (d1 << 4) + d2; d3 = v % 10; v = v / 10; d2 = v % 10; d1 = v / 10; *out++ = d1 + '0'; *out++ = d2 + '0'; *out++ = d3 + '0'; *out++ = term; term = term ^ ('<' ^ '+'); } else { perror( "\nOdd length hex string" ); exit(-1); } } if ('\n' == *in) { perror( "\nUnterminated hex string" ); exit(-1); } if ('_' != *in) { perror( "\nIllegal hex digit" ); exit(-1); } *in++; } } } *out++ = (char)0; } /*********************** * Trippy test routine * ***********************/ int verify( stimulus, response ) char * stimulus; char * response; { int bugs = 0; char ch; char * bufp; char * resp; char buffer[80]; bufp = buffer; resp = response; ch = '\0'; while (*resp) { for (;;) { if (ch == '\n') break; ch = tripgetc(); *bufp = ch; if (bufp < &buffer[79]) bufp++; if (ch == *resp) break; bugs++; } resp++; } if (bugs) { *bufp = '\0'; putoutbegin( "Sent: " ); putouts( stimulus ); putoutend( "\n" ); putoutbegin( "Expected: " ); putouts( response ); putoutend( "\n" ); putoutbegin( "Got: " ); putouts( buffer ); putoutend( "\n" ); return 0; } else { return 1; } } /**************** * Main Program * ****************/ main (argc, argv) int argc; char * argv[]; { char * filename = NULL; char * diagdir = NULL; char * diagfile = NULL; int argno = 1; char version[8]; FILE * diag; while (argno < argc) { if (argv[argno][0] == '-') { /* options */ if (strcmp( argv[argno], "-d") == 0) { argno = argno + 1; if (argno < argc) { if (diagdir == NULL) { diagdir = argv[argno]; } else { puts( "only one prefix allowed!\n" "try tripinit -help\n" ); exit(-1); } } else { puts( "Missing diagnostic prefix\n" "try tripinit -help\n" ); exit(-1); } } else if (strcmp( argv[argno], "-f") == 0) { argno = argno + 1; if (argno < argc) { if (diagfile == NULL) { diagfile = argv[argno]; } else { puts( "only one diag file allowed!\n" "try tripinit -help\n" ); exit(-1); } } else { puts( "Missing diagnostic file\n" "try tripinit -help\n" ); exit(-1); } } else { /* option not recognized */ puts( "tripdiag [ ]\n" " -d defaults to " DIAGDIR"\n" " -f defaults to " "\n" " defaults to "SERIALPORT"\n" "Test the Trippy attached to given port\n" ); exit(-1); } } else { if (filename == NULL) { filename = argv[argno]; } else { puts( "only one file name allowed!\n" "try tripinit -help\n" ); exit(-1); } } argno = argno + 1; } if (filename == NULL) { filename = SERIALPORT; } if (diagdir == NULL) { diagdir = DIAGDIR; } fputs( "OPENING TRIPPY INTERFACE: ", stdout ); fputs( filename, stdout ); fputs( "\n", stdout ); tripopen( filename ); fputs( " OPENED!\n", stdout ); if (diagfile == NULL) { fputs( "GETTING VERSION NUMBER!\n", stdout ); tripputs( "BT#" ); tripgets( version, 8 ); fputs( " FOUND VERSION: ", stdout ); fputs( version, stdout ); diagfile = (char *)malloc( 90 ); (void) strncpy( diagfile, diagdir, 8 ); (void) strncat( diagfile, version, 80 ); { int i; for (i = 0; i < 80; i++) if (diagfile[i] < ' ') diagfile[i] = '\0'; } } fputs( "OPENING DIAGNOSTIC FILE: ", stdout ); fputs( diagfile, stdout ); fputs( "\n", stdout ); if ((diag = fopen( diagfile, "r" )) == NULL) { fputs( " COULD NOT OPEN\n", stdout ); exit(-1); } fputs( " OPENED\n", stdout ); while (!feof(diag)) { /* main diagnostic loop, read and interpret lines of diag */ char line[80]; char modline[160]; int nonblank; char stimulus[160]; line[2] == '\n'; if (fgets( line, 80, diag ) == NULL) break; nonblank = 2; while (line[nonblank] == ' ') nonblank++; if ((line[0] == 'T') && (line[1] == ':')) { /* line to output to the Trippy */ tripfixup( &line[nonblank], modline ); tripputs( modline ); (void) strncpy( stimulus, modline, 160 ); } else if ((line[0] == 'R') && (line[1] == ':')) { /* expected reply from the Trippy */ verify( stimulus, &line[nonblank] ); } else if ((line[0] == '-') && (line[1] == '-')) { /* ignore the line */ } else { /* text to be echoed */ fputs( line, stdout ); } } }