/* 
 * serialtest.c -- Simple test prog for testing serial ports
 * 
 * Copyright (C) 2000  Axis Communications AB
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id$
 *
 */

#include <sys/types.h>          /* socket, bind, accept */
#include <sys/stat.h>           /* open */
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>              /* stdin, stdout, stderr */
#include <sys/ioctl.h>
#include <termios.h>
#include <stdlib.h>
#include <sys/time.h>           /* timeval */
#include <stdlib.h>
#include <string.h>             /* memset used by FD_ZERO */


#define DEFAULT_DEV "/dev/ttyS0"
#define DEFAULT_SPEED 115200


int translate_speed(int spd);
void fd_setup(int fd, int speed, int flow);

int main(int argc, char **argv)
{
  int serfd;
  int i, c=0, error=0, len, do_echo = 0;
  char txdata[127];
  char rxdata[256];

  if (argc>2)
  {
    printf("wrong syntax ! ./sertest [-e]");
    exit(1);
  }
  
  if (argc == 2){  
    if ((strcmp("-e", argv[1]))==0)
      do_echo=1;
    else
      printf("wrong syntax ! ./sertest [-e]\n");
  }
  
  printf("Opening %s\n", DEFAULT_DEV);
  serfd = open(DEFAULT_DEV, O_RDWR);

  if (serfd < 0)
    exit(1);

  fd_setup(serfd, DEFAULT_SPEED, 1);

  for (i=0;i<127;i++)
    txdata[i]=i+64;

  if (!do_echo)
  {      
    while (!error){      
      len = 0;
      i=0;
      printf("-%d- Sending data...", c++);
      write(serfd, txdata, 127);
      printf("done.\n");

      /* wait for 127 chars */
      while (len < 127)
      {
        len += read(serfd, rxdata+len, 127);
        printf("Got [%d/127]...\r", len);
        fflush(stdout);
      }

      /* now compare the received data */
      printf("Got response, comparing... ");
      
      if (memcmp(rxdata, rxdata, 127) == 0)
        printf("OK.\n");
      else
        error = 1;      
    }
  }
  else
  {
    printf("Running as echo server.\n");
    while (!error) {      
      len = 0;
      
      /* wait for 127 chars */
      while (len < 127)
      {
        len += read(serfd, rxdata+len, 127);
        printf("Got [%d/127]...\r", len);
        fflush(stdout);
      }
      
       /* now compare the received data */
      printf("-%d- Checking RX data... ", c++);
      
      if (memcmp(txdata, rxdata, 127) == 0)
      {
        printf("OK...echoing.\n");
        write(serfd, rxdata, 127);
      }
      else
      {
        printf("ERROR\n");
        error = 1;
      }
    }
  }

  printf("Not OK !\n");
  for (i=0;i<127;i++)
    printf("%c", txdata[i]);
  
  printf("\n");
  
  exit(1);
}

int translate_speed(int spd)
{
  int speed_c = 0;

  switch(spd)
  {
   case 9600:
    speed_c = B9600;
    break;
   case 19200:
    speed_c = B19200;
    break;
   case 38400:
    speed_c = B38400;
    break;
   case 57600:
    speed_c = B57600;
    break;
   case 115200:
    speed_c = B115200;
    break;
   case 230400:
    speed_c = B230400;
    break;
   case 460800:
    speed_c = B460800;
    break;
   default:
    printf("Bad baudrate %d.\n", spd);
    break;
  }
  return speed_c;
}
                                                         
void fd_setup(int fd, int speed, int flow)
{
  struct termios t;

  if (fd < 0)
  {
    perror("fd_setup");
    exit(1);
  }
	
  if (tcgetattr(fd, &t) < 0)
  {  
    perror("tcgetattr");
    exit(1);
  }

  cfmakeraw(&t);
	
  t.c_cflag &= ~CBAUD;
  t.c_cflag |= translate_speed(speed) | CS8 | CLOCAL;
  t.c_oflag = 0; /* turn off output processing */
  t.c_lflag = 0; /* no local modes */
  
  if (flow)
    t.c_cflag |= CRTSCTS;
  else
    t.c_cflag &= ~CRTSCTS;
  
  if (tcsetattr(fd, TCSANOW, &t) < 0)
  {
    perror("fd_setup : tcsetattr");
    exit(1);
  }
  return;
}

