GophHub - go4retro/tcpser/src/tcpmdm.c


Raw File

    1	#include <sys/time.h>
    2	#include <stdlib.h>
    3	#include <stdio.h>
    4	#include <sys/param.h>
    5	#include <termios.h>
    6	#include <sys/ioctl.h>
    7	
    8	#include "serial.h"
    9	#include "ip.h"
   10	#include "debug.h"
   11	#include "tcpser.h"
   12	
   13	
   14	
   15	int main(int argc, char* argv[]) {
   16	  struct timeval timer;  
   17	  struct timeval* ptimer;  
   18	  unsigned char tty[255]="/dev/ttyS2";
   19	  //unsigned char addy[255]="dilbert:6400";
   20	  unsigned char init[255]="at";
   21	  unsigned char cr[1]="\x0d";
   22	  int speed=38400;
   23	  int sfd=-1;
   24	  int csd=-1;
   25	  //int ssd=-1;
   26	  int max_fd=0;
   27	  fd_set readfs; 
   28	  int res=0;
   29	  unsigned char buf[255];
   30	  int rc;
   31	
   32	  log_init();
   33	  log_set_trace_flags(TRACE_MODEM_IN | TRACE_MODEM_OUT);
   34	  log_set_level(7);
   35	  // open serial port
   36	  // config serial port
   37	  if(0 > (sfd = ser_init_conn(tty,speed))) {
   38	    ELOG(LOG_FATAL,"Could not open serial port");
   39	    exit(-1);
   40	  }
   41	
   42	  // send init string
   43	  if(strlen(init) > 0) {
   44	    ser_write(sfd,init,strlen(init));
   45	    ser_write(sfd,cr,1);
   46	  }
   47	
   48	  // if configured, listen on ip address
   49	
   50	  // check for carrier detect line
   51	  // when line goes high, connect to IP:port
   52	  // when line goes low, disconnect
   53	  for(;;) {
   54	    FD_ZERO(&readfs);
   55	    max_fd=sfd;
   56	    FD_SET(sfd, &readfs); 
   57	    if(-1 < csd) {
   58	      // we are connected.
   59	      max_fd=MAX(max_fd,csd);
   60	      FD_SET(csd, &readfs); 
   61	    }
   62	    max_fd++;
   63	
   64	    timer.tv_sec=0;
   65	    timer.tv_usec=1000;
   66	    ptimer=&timer;
   67	    
   68	    LOG(LOG_ALL,"csd=%d",csd);
   69	    rc=select(max_fd, &readfs, NULL, NULL, ptimer);
   70	    if(-1 == rc) {
   71	      ELOG(LOG_WARN,"Select returned error");
   72	    } else if (0 == rc) {
   73	      // timer popped.
   74	      if(-1 == csd && (ser_get_control_lines(sfd) & TIOCM_CD) > 0) {
   75	        // we need to connect
   76	        // commented out for now.
   77	        //csd=ip_connect(addy);
   78	      } else if(-1 < csd && (ser_get_control_lines(sfd) & TIOCM_CD) == 0) {
   79	        // we need to disconnect.
   80	          ip_disconnect(csd);
   81	          csd=-1;
   82	      } 
   83	    } else if(rc > 0) {
   84	      // we got data from someone.
   85	      if (FD_ISSET(sfd,&readfs)) {  // serial port
   86	        LOG(LOG_DEBUG,"Data available on serial port");
   87	        res = ser_read(sfd,buf,sizeof(buf) -1);
   88	        if(res > 0 && csd > -1) {
   89	          ip_write(csd,buf,res);
   90	        }
   91	      }
   92	      if (csd > -1 && FD_ISSET(csd,&readfs)) {  // ip port
   93	        LOG(LOG_DEBUG,"Data available on tcp/ip port");
   94	        res = ip_read(sfd,buf,sizeof(buf) -1);
   95	        if(res==0) {
   96	          // conn closed, disconnect.
   97	          ip_disconnect(csd);
   98	          csd=-1;
   99	        }
  100	        if(res > 0 && csd > -1) {
  101	          ser_write(csd,buf,res);
  102	        }
  103	      }
  104	
  105	    }
  106	 
  107	    
  108	
  109	  }
  110	
  111	
  112	
  113	
  114	
  115	
  116	}
  117	

Generated by GNU Enscript 1.6.6, and GophHub 1.3.