home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / MSDOS / WATTCP / COMD.ZIP / COMD.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-22  |  7.5 KB  |  305 lines

  1. /******************************************************************************
  2.  
  3.     TCPINFO - display configuration info to the screen
  4.  
  5.     Copyright (C) 1991, University of Waterloo
  6.     portions Copyright (C) 1990, National Center for Supercomputer Applications
  7.  
  8.     This program is free software; you can redistribute it and/or modify
  9.     it, but you may not sell it.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but without any warranty; without even the implied warranty of
  13.     merchantability or fitness for a particular purpose.
  14.  
  15.         Erick Engelke                   or via E-Mail
  16.         Faculty of Engineering
  17.         University of Waterloo          Erick@development.watstar.uwaterloo.ca
  18.         200 University Ave.,
  19.         Waterloo, Ont., Canada
  20.         N2L 3G1
  21.  
  22. ******************************************************************************/
  23.  
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <tcp.h>
  27. #include "com.h"
  28. #include "litecomm.h"
  29.  
  30. extern longword set_timeout();
  31.  
  32. struct com_info coms[5];
  33. int currentport = 0;
  34.  
  35. char *testmsg = "This is a test of the serial port";
  36.  
  37. static void (*other)();
  38. static void mine(char *name, char *value)
  39. {
  40.     int temp;
  41.     struct com_info *c;
  42.  
  43.     c = &coms[ currentport ];
  44.  
  45.     if ( !strcmp( name, "COM.PORT" )) {
  46.     temp = atoi( value );
  47.     if (coms[temp].active)
  48.         printf("Warning: com port %u was defined more than once\n", temp );
  49.     currentport = temp;
  50.     coms[temp].active = 1;
  51.     } else if ( !strcmp(name,"COM.BAUD" )) {
  52.     if (temp = atoi( value ))
  53.         c->baud = temp;
  54.     else
  55.         printf("Warning: com port %u given baud %u\n", currentport, temp );
  56.     } else if ( !strcmp(name,"COM.PARITY")) {
  57.     if (!stricmp(value,"EVEN"))
  58.         c->parity = EPARITY;
  59.     else if (!stricmp(value,"ODD"))
  60.         c->parity = OPARITY;
  61.     else if (!stricmp(value,"NONE"))
  62.         c->parity = NPARITY;
  63.     else if (!stricmp(value,"MARK"))
  64.         c->parity = MPARITY;
  65.     else if (!stricmp(value,"SPACE"))
  66.         c->parity = SPARITY;
  67.     else
  68.         printf("Warning: incorrect parity statement '%s'\n",value);
  69.     } else if (!strcmp(name,"COM.BITS")) {
  70.     c->bits = atoi(value) - 5;
  71.     } else if (!strcmp(name,"COM.STOP")) {
  72.     c->stops = (atoi(value) - 1) * 4;
  73.     } else if (!strcmp(name,"COM.HANDSHAKE")) {
  74.     if (!stricmp(value,"XON")) c->handshake = FALSE;
  75.     if (!stricmp(value,"MODEM")) c->handshake = TRUE;
  76.     } else if (!strcmp(name,"COM.TCPPORT")) {
  77.     c->tcpport = atoi( value );
  78.     } else if (!strcmp(name,"COM.PASSWORD")) {
  79.     strncpy(c->password, value, PASSLENGTH );
  80.     c->password[ PASSLENGTH-1]=0;
  81.     } else if (!strcmp(name,"COM.MODE" )) {
  82.     if (!stricmp(value,"RS232")) c->mode = RS232;
  83.     if (!stricmp(value,"TEST")) c->mode = TEST;
  84.     if (!stricmp(value,"HAYESMODEM")) c->mode = HAYESMODEM;
  85.     } else if (!strcmp(name,"COM.DELAY" )) {
  86.     c->delay = atoi( value );
  87.     c->tickdelay = (c->delay * 9) / 5;
  88.     if (c->delay) c->tickdelay ++;
  89.     } else
  90.     if (other)
  91.         (*other)( name, value );
  92. }
  93.  
  94.  
  95. char *parities[] = { "NONE", "ODD", "ERR", "EVEN", "ERR","MARK", "ERR","SPACE" };
  96. char *modes[] = {"RS232","TEST","HAYESMODEM" };
  97.  
  98. show_port( int port )
  99. {
  100.     struct com_info *c;
  101.  
  102.     c = &coms[port];
  103.  
  104.     printf("    MODE     : %s\n", modes[ c->mode ]);
  105.     printf("    PASSWORD : %s\n", (*c->password) ? "ON" : "OFF");
  106.     printf("    TCP PORT : %u\n", c->tcpport );
  107.     printf("    DELAY    : %0u.%0u seconds\n", c->delay / 10, c->delay % 10 );
  108. printf("delay = %u\n",c->tickdelay);
  109.  
  110.     printf("    HANDSHAKE: %s\n", (c->handshake) ? "DTR/RTS" : "XON/XOFF");
  111.     printf("    BAUD     : %u\n", c->baud);
  112.     printf("    PARITY   : %s\n", parities[ c->parity / 8 ]);
  113.     printf("    BITS     : %u\n", c->bits + 5);
  114.     printf("    STOP     : %u\n", (c->stops / 4) + 1);
  115. }
  116.  
  117. interactive( int port, struct com_info *c )
  118. {
  119.     char ch;
  120.  
  121.     puts("( ^Z to leave interactive mode )");
  122.  
  123.     do {
  124.     if (kbhit()) {
  125.         ch = getch();
  126.         if (ch == 26) return;
  127.         lc_put(port,ch );
  128.     }
  129.     if (lc_icnt(port)) {
  130.         ch = lc_getw(port);
  131.         if (ch == -1)
  132.         puts("ERROR received from port");
  133.         else putch( ch );
  134.     }
  135.     } while(1);
  136. }
  137.  
  138. init_coms()
  139. {
  140.     struct com_info *c;
  141.     int status, i;
  142.  
  143.  
  144.     for ( i = 1; i < 5; ++i ) {
  145.     c = &coms[i];
  146.     if ( c->active ) {
  147.         c->status = INIT;
  148.         status = comm_opn( i , c->baud, c->parity, c->bits,
  149.         c->stops, COMSIZIN, COMSIZOUT, FALSE );
  150.         if ( status == -1 ) {
  151.         printf("Error: initializing COM %u\n", i );
  152.         show_port( i );
  153.  
  154.         c->active = 0;
  155.         } else {
  156.         lc_setmdm( i , DTR | RTS );
  157.         lc_xoff( i, (c->handshake == HS_XOFF)? TRUE : FALSE );
  158.         printf("COM %u : initialized\n", i);
  159.         show_port( i );
  160.         }
  161.  
  162.         if (c->mode == TEST ) {
  163.         lc_puts(i,testmsg, strlen( testmsg ));
  164.         puts("Printed message... now interactive ...");
  165.         interactive(i, &coms[i] );
  166.         puts("Continuing");
  167.         }
  168.     }
  169.     }
  170. }
  171.  
  172.  
  173. poll_read( int port, struct com_info *c)
  174. {
  175.     int count;
  176.     char buffer[ 1024 ];
  177.  
  178.     if ( !chk_timeout( c->lastxmit ))
  179.     return;
  180.  
  181.     if (count = lc_icnt( port )) {
  182.     /* read the port and write the data to the socket */
  183.     if (count > sizeof( buffer ))
  184.         count = sizeof( buffer );
  185.     if (count > sock_tbleft( & c->sock ))
  186.         count = sock_tbleft( &c->sock);
  187.  
  188.     /* if we can get anything... */
  189.     if ( count ) {
  190.         c->lastxmit = set_timeout( 0 ) + (longword)(c->tickdelay);
  191.         lc_gets( port, buffer, count );
  192.         sock_fastwrite( &c->sock, buffer, count );
  193. printf("writing %u bytes on port %u\n" , count, port );
  194.     }
  195.     }
  196. }
  197.  
  198. poll_write( int port, struct com_info *c )
  199. {
  200.     int count, temp;
  201.     char buffer[ 1024 ];
  202.  
  203.     if (count = sock_rbused( &c->sock )) {
  204.         temp = COMSIZOUT - lc_ocnt( port );
  205.         if (count > temp )
  206.             count = temp;
  207.  
  208.         if ( count ) {
  209.             sock_fastread( &c->sock, buffer, count );
  210.             lc_puts( port, buffer, count );
  211.         }
  212.     }
  213. }
  214.  
  215. chk_status( int port, struct com_info *c )
  216. {
  217.     if ( !tcp_tick( &c->sock )) {
  218.     /* generate brk */
  219.     sock_close( &c->sock );
  220.     c->status = CLOSING;
  221.     }
  222.  
  223.     if ( lc_gotbrk( port ) ) {
  224.     sock_close( &c->sock );
  225.     c->status = CLOSING;
  226.     }
  227.     return( c->status == CLOSING );
  228. }
  229.  
  230.  
  231. loop( int port, struct com_info *c )
  232. {
  233.     if (!c->active) return;
  234.  
  235.     switch ( c->status ) {
  236.     case INIT :
  237.         tcp_listen( &c->sock, c->tcpport, 0, 0, NULL, 0);
  238.         printf( "COM %u : listening on tcp port %u\n",
  239.             port, c->tcpport );
  240.         c->status = WAITING;
  241.         break;
  242.     case WAITING :
  243.         tcp_tick( NULL );
  244.         if (sock_established( &c->sock )) {
  245.             printf( "COM %u : incomming session\n", port);
  246.             c->status = INITCOM;
  247.         }
  248.         break;
  249.     case INITCOM :
  250.         switch ( c->mode ) {
  251.             case RS232 :
  252.                 lc_puts( port, "HELLO\n\r", 7 );
  253.                 c->status = ACTIVE;
  254.                 break;
  255.             case HAYESMODEM :
  256.                 /* must do dialing */
  257.                 c->status = ACTIVE;
  258.                 break;
  259.         }
  260.         break;
  261.     case ACTIVE :
  262.         if (!chk_status( port, c )) {
  263.             poll_read( port, c );
  264.             poll_write( port, c );
  265.         }
  266.         break;
  267.     case CLOSING :
  268.         /* clean up modem stuff */
  269.  
  270.         c->status = CLOSED;
  271.         break;
  272.     case CLOSED :
  273.         if (!tcp_tick( &c->sock )) {
  274.             c->status = INIT;
  275.         }
  276.         break;
  277.     }
  278. }
  279.  
  280.  
  281. main( int argc, char **argv)
  282. {
  283.     int i;
  284.  
  285.     dbuginit();
  286.     printf("Reading Waterloo TCP configuration file.\n");
  287.  
  288.     memset( coms, 0, sizeof( coms ));
  289.  
  290.     other = usr_init;
  291.     usr_init = mine;
  292.  
  293.     sock_init();
  294.  
  295.     init_coms();
  296.  
  297.     do {
  298.     for ( i = 1; i < 5 ; ++i )
  299.         loop( i, &coms[i] );
  300.     kbhit();
  301.     } while ( 1 );
  302.  
  303.     exit( 0 );
  304. }
  305.