/* capser.c
   (C) 2004-5 Captain http://www.captain.at
   
   Helper functions for "ser"
   
   Used for testing the PIC-MMC test-board
   http://www.captain.at/electronic-index.php
*/
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

int writePort(int fd, char *chars) {
   if (write(fd, chars, strlen(chars))< 0) {
      fputs("write failed!\n", stderr);
      return 0;
   }
   return 1;
}

int readPort(int fd, char *result) {
   int iIn;
   
   iIn = read(fd, result, 254);
   result[iIn-1] = 0;
   if (iIn < 0) {
      if (errno == EAGAIN) {
         printf("Access problem with serial port\n");
         return 0;
      } else {
         printf("SERIAL read error %d %s\n", errno, strerror(errno));
         return 0;
      }
   }                    
   return 1;
}

int getBaud(int fd) {

   struct termios termAttr;
   int inputSpeed;
   speed_t baudRate;
   
   inputSpeed = -1;
   tcgetattr(fd, &termAttr);
   /* Get the input speed.                              */
   baudRate = cfgetispeed(&termAttr);
   switch (baudRate) {
      case B0:      inputSpeed = 0; break;
      case B50:     inputSpeed = 50; break;
      case B110:    inputSpeed = 110; break;
      case B134:    inputSpeed = 134; break;
      case B150:    inputSpeed = 150; break;
      case B200:    inputSpeed = 200; break;
      case B300:    inputSpeed = 300; break;
      case B600:    inputSpeed = 600; break;
      case B1200:   inputSpeed = 1200; break;
      case B1800:   inputSpeed = 1800; break;
      case B2400:   inputSpeed = 2400; break;
      case B4800:   inputSpeed = 4800; break;
      case B9600:   inputSpeed = 9600; break;
      case B19200:  inputSpeed = 19200; break;
      case B38400:  inputSpeed = 38400; break;
   }
   return inputSpeed;
}

int initPort(char * file, int baud) {

   struct termios options;
   int fd; 
      /*
        O_RDWR   = read/write mode
        O_NOCTTY = cant be controlling terminal
        O_NDELAY = buffered (non blocking)
      */
   if ((fd = open(file, O_RDWR | O_NOCTTY | O_NDELAY)) == -1) {
      return (-1);
   } else {
      fcntl(fd, F_SETFL, 0);
   }
   
   // Get the current options for the port...
   tcgetattr(fd, &options);
   bzero(&options, sizeof(options)); /* clear struct for new port settings */
   
   // Enable the receiver and set local mode...
   options.c_cflag = baud | CS8 | CLOCAL | CREAD;
 //  options.c_lflag |= ~(ICANON | ECHO | ECHOE);
   options.c_iflag = IGNPAR | IGNBRK ;
          
   // Set the new options for the port...
   tcflush(fd, TCIFLUSH);
   tcsetattr(fd, TCSANOW, &options);
      
   return fd;
}


/*

int main(int argc, char **argv) {

   char sCmd[254];
   char sResult[254];
   char port[254];
   int fd;
   
   strcpy(port, "/dev/ttyS0");

   if ((fd = initPort(port)) == -1) {
      printf("Unable to open serial port %s\n ", port);
      return 1;
   } 

   strcpy (sCmd, "ate0i0\r");

   if (!writePort(fd, sCmd)) {
      printf("write failed\n");
      close(fd);
      return 1;
   }

   printf("written:%s\n", sCmd);
   

   usleep(500000);

   if (!readPort(fd, sResult)) {
      printf("read failed\n");
      close(fd);
      return 1;
   }
   printf("readport=%s\n", sResult);
   
  
   
   close(fd);
   return 0;
}

*/
