/***************************************************************************
                          main.cpp  -  description
                             -------------------
    begin                : Mit Mai 28 09:48:41 CEST 2003
    copyright            : (C) 2003 by Markus Raab
    email                : markus.raab@aon.at
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "cserial.h"
#include <iostream>
#include <fstream>
#include <getopt.h>
#include <string>

int main(int argc, char ** argv)
{
   CSerial serial;
   int count = 0;
   int index = 0;
   string port = "/dev/ttyS0";
   string str;
   bool receive = true;
   bool send    = true;
   ifstream fin;
   static struct option opt [] =
   {
   	{"baudrate",1,0,'b'},
		{"device",1,0,'d'},
      {"help",0,0,'h'},
      {"length",1,0,'l'},
      {"parity",0,0,'p'},
      {"receive",0,0,'r'},
      {"send",0,0,'s'},
      {"twostop",0,0,'t'},
		{"verbose",0,0,'v'},
      {"wait",1,0,'w'}
   };

   while ((count = getopt_long(argc, argv, "b:d:hl:prstvw:",opt,&index)) != -1)
   {
      switch (count)
      {
      case 0:
         cout << "Parameter " << opt[index].name << " eingegeben." << endl;
         break;
      case 'b':
         cout << "Baudrate: " << optarg << endl;
         serial.setBaud(atoi(optarg));
         break;
      case 'd':
         cout << "Device: " << optarg << endl;
         port = optarg;
         break;
      case 'l':
         cout << "Anzahl der Bits: " << optarg << endl;
         serial.setBits((atoi(optarg) - 5)*20);
         break;
      case 'p':
         cout << "Parity: " << endl;
         serial.setPari(true);
         break;
      case 'r':
      	cout << "Nur empfangen" << endl;
         send = false;
         break;
      case 's':
      	cout << "Nur senden" << endl;
         receive = false;
         break;
     case 't':
         cout << "Zwei Stoppbits eingeschalten." << endl;
         serial.setStop(true);
         break;
      case 'v':
         cout << "Verbose Mode" << endl;
         serial.setDebug(true);
         break;
      case 'w':
         cout << "Wartezeit pro Byte: " << optarg << endl;
         serial.setWait(atoi(optarg));
         break;
      default:
         cout << "\t-b, --baudrate [Baudrate]\n";
         cout << "Setzt die Baudrate. Der gewünschte Wert ist bitte der Tabelle zu entnehmen\n";
			cout << "B0  \t0000000\tB600  \t0000010\n";
			cout << "B50 \t0000001\tB1200 \t0000011\n";
			cout << "B75 \t0000002\tB1800 \t0000012\n";
			cout << "B110\t0000003\tB2400 \t0000013\n";
			cout << "B134\t0000004\tB4800 \t0000014\n";
			cout << "B150\t0000005\tB9600 \t0000015\n";
			cout << "B200\t0000006\tB19200\t0000016\n";
			cout << "B300\t0000007\tB38400\t0000017\n";
         cout << "\t-d, --device Device\n";
         cout << "Device für die Übertragung.\n";
         cout << "\t-l, --length Bits\n";
         cout << "Anzahl der Bits.\n";
         cout << "\t-p, --parity\n";
         cout << "Schaltet zusätlich noch Paritätsprüfung ein.\n";
         cout << "\t-r, --receive\n";
         cout << "Deaktiviert den Schreibmodus.\n";
         cout << "\t-s, --send\n";
         cout << "Deaktiviert den Empfangemodus.\n";
         cout << "\t-t, --twostop\n";
         cout << "Verwendet zwei stopbits statt einem.";
         cout << "\t-v, --verbose\n";
         cout << "Gibt exakte Informationen über den Übertragungsfortschritt auf stderr aus.\n";
         cout << "\t-w, --wait Time\n";
         cout << "Veranlasst die Unterbrechung der Übertragung nach jedem Byte für die\n";
         cout << "angegebene Zeit in Microsekunden (= 1/1000 ms, = 1/1000000s) zu warten.\n";
         cout << "Diese Option ist sehr hilfreich, wenn der empfänger zu langsam für die\n";
         cout << "normale Geschwindigkeit ist\n";
         exit (1);
      }
   }

	// Öffnen des Portes
   serial.open(port.c_str());

	// Senden, wenn notwendig
   if (send)
   {
      while (optind < argc)	// Zusätliches Bearbeiten von Dateien notwendig
      {
         fin.open(argv[optind++]);
         cout << argv[optind++] << endl;
         while (getline(fin, str, '\n'))
         {
            str += '\n';
            serial.send ((void *) str.c_str(), str.length());
         }
      }
      while (getline(cin, str, '\n'))
      {
         str += '\n';
         serial.send ((void *) str.c_str(), str.length());
      }
	}

   if (receive)
   {
   	while (serial.empfange_string (str))
   	{
      	cout << str;
   	}
	}
}

