/*
	
	serial_download.cpp
	
	Copyright 1996 Moebius Technologies Inc., All Rights Reserved.
	
	Attempt to link the BeBox with another computer via the
	serial port!
	
	Version 1.0 beta!
	
	Programmers : Frederic Tessier
*/

#include <stdio.h>
#include <string.h>
#include <SerialPort.h>

#define DEVICE_NAME "serial1"
#define DATA_MODE "8"
#define STOP_MODE "1"
#define PARITY_MODE "NO"
#define BAUD_MODE "19200"
#define FLOW_MODE "NO"

char	usage[] = "Version 1.1 (AA DR9 Version)§ (c) Moebius Technologies Inc. 1997\nAuthor : Frederic Tessier, tessier@moebius.qc.ca\nSyntax : [options] [filename] Valid rascii options:\n\t-b[baud rate] 0 - 115200\n\t-d[device name] serial1, serial2, serial3, serial4\n\t-f[flow control] XON, CTS\n\t-p[parity mode] EVEN, ODD, NO\n\t-s[stop bits] 1, 2\n\t-D[data bits] 7, 8\n\t-t[trace mode] Turn On Trace Mode\n\t-v[version]\n\t-?[help]\n";

main(int argc, char **argv)
{
	int status, tracemode, bytecount;
	char data_serial[1];
	char data_char;
	char data_output[] = "BeBox Rulez!!!!";
	char *devicename;
	data_bits databits;
	stop_bits stopbits;
	parity_mode paritymode;
	data_rate baudrate;
	ulong flowcontrol;
	
	char *datamode;
	char *stopmode;
	char *paritymodt;
	char *baudmode;
	char *flowmode;
	char *filename;
	
	FILE * dlfile = NULL;
	
	BSerialPort *connection;
	
	argv++; /* skip program name */
	status = 0;
	devicename = DEVICE_NAME;
	datamode = DATA_MODE;
	databits = B_DATA_BITS_8;
	stopmode = STOP_MODE;
	stopbits = B_STOP_BITS_1;
	paritymodt = PARITY_MODE;
	paritymode = B_NO_PARITY;
	baudmode = BAUD_MODE;
	baudrate = B_19200_BPS;
	flowmode = FLOW_MODE;
	flowcontrol = 0;
	tracemode = 1;
	dlfile = stdout;
	while(*argv && **argv == '-')
	{
			char	c;
			char *argum;
			int x;
				
			(*argv)++;		/* skip the '-' */
			c = **argv;		/* option letter */
			(*argv)++;
			argum = *argv; /* argument to verify! */
			switch(c)
			{
					case 'b':		/* baud data rate */
							if (**argv == '\0')
									baudrate = B_19200_BPS;
							else
							{
									/* verify the speed to setup */
									status = 1;
									if (strcmp(argum, "0") == 0) 
									{
										status = 0;
										baudrate = B_0_BPS;
										baudmode = "0";
									}
									if (strcmp(argum, "50") == 0)
									{
										status = 0;
										baudrate = B_50_BPS;
										baudmode = "50";
									}
									if (strcmp(argum, "75") == 0) 
									{
										status = 0;
										baudrate = B_75_BPS;
										baudmode = "75";
									}
									if (strcmp(argum, "110") == 0)
									{
										status = 0;
										baudrate = B_110_BPS;
										baudmode = "110";
									}
									if (strcmp(argum, "134") == 0)
									{
										status = 0;
										baudrate = B_134_BPS;
										baudmode = "134";
									}
									if (strcmp(argum, "150") == 0)
									{
										status = 0;
										baudrate = B_150_BPS;
										baudmode = "150";
									}
									if (strcmp(argum, "200") == 0) 
									{
										status = 0;
										baudrate = B_200_BPS;
										baudmode = "200";
									}
									if (strcmp(argum, "300") == 0)
									{
										status = 0;
										baudrate = B_300_BPS;
										baudmode = "300";
									}
									if (strcmp(argum, "600") == 0)
									{
										status = 0;
										baudrate = B_600_BPS;
										baudmode = "600";
									}
									if (strcmp(argum, "1200") == 0)
									{
										status = 0;
										baudrate = B_1200_BPS;
										baudmode = "1200";
									}
									if (strcmp(argum, "1800") == 0)
									{
										status = 0;
										baudrate = B_1800_BPS;
										baudmode = "1800";
									}
									if (strcmp(argum, "2400") == 0)
									{
										status = 0;
										baudrate = B_2400_BPS;
										baudmode = "2400";
									}
									if (strcmp(argum, "4800") == 0)
									{
										status = 0;
										baudrate = B_4800_BPS;
										baudmode = "4800";
									}
									if (strcmp(argum, "9600") == 0)
									{
										status = 0;
										baudrate = B_9600_BPS;
										baudmode = "9600";
									}
									if (strcmp(argum, "19200") == 0)
									{
										status = 0;
										baudrate = B_19200_BPS;
										baudmode = "19200";
									}
									if (strcmp(argum, "38400") == 0) 
									{
										status = 0;
										baudrate = B_38400_BPS;
										baudmode = "38400";
									}
									if (strcmp(argum, "57600") == 0)
									{
										status = 0;
										baudrate = B_57600_BPS;
										baudmode = "57600";
									}
									if (strcmp(argum, "115200") == 0)
									{
										status = 0;
										baudrate = B_115200_BPS;
										baudmode = "115200";
									}
							}									
							break;
					case 'd':		/* device serialx */
							if (**argv == '\0')
									devicename = "serial1";
							else
									devicename = argum;
							break;
					case 'f':		/* flow control */
							if (**argv == '\0')
									flowcontrol = 0;
							else
							{
									status = 1;
									if (strcmp(argum, "XON") == 0)
									{
										status = 0;
										flowcontrol = B_SOFTWARE_CONTROL;
										flowmode = "XON/XOFF";
									} 
									if (strcmp(argum, "CTS") == 0)
									{
										status = 0;
										flowcontrol = B_SOFTWARE_CONTROL;
										flowmode = "CTS/RTS";
									} 
							}
							break;
					case 'p':		/* parity mode */
							if (**argv == '\0')
									paritymode = B_NO_PARITY;
							else
							{
									status = 1;
									if (strcmp(argum, "EVEN") == 0)
									{
										status = 0;
										paritymode = B_EVEN_PARITY;
										paritymodt = "EVEN";
									} 
									if (strcmp(argum, "ODD") == 0)
									{
										status = 0;
										paritymode = B_ODD_PARITY;
										paritymodt = "ODD";
									} 
									if (strcmp(argum, "NO") == 0)
									{
										status = 0;
										paritymode = B_NO_PARITY;
										paritymodt = "NO";
									} 
							}
							break;
					case 's':		/* stop bits */
							if (**argv == '\0')
									stopbits = B_STOP_BITS_1;
							else
							{
									status = 1;
									if (strcmp(argum, "1") == 0)
									{
										status = 0;
										stopbits = B_STOP_BITS_1;
										stopmode = "1";
									} 
									if (strcmp(argum, "2") == 0)
									{
										status = 0;
										stopbits = B_STOP_BITS_2;
										stopmode = "2";
									} 
							}
							break;		
					case 'D':		/* data bits */
							if (**argv == '\0')
									databits = B_DATA_BITS_8;
							else
							{
									status = 1;
									if (strcmp(argum, "7") == 0)
									{
										status = 0;
										databits = B_DATA_BITS_7;
										datamode = "7";
									} 
									if (strcmp(argum, "8") == 0)
									{
										status = 0;
										databits = B_DATA_BITS_8;
										datamode = "8";
									} 
							}
							break;
					case 't':		/* trace mode */
							tracemode = 0;
							break;
					case 'v':		/* version # */
							fprintf(stderr,"rascii Version 1.1 (AA DR9 Version)§ (c) Moebius Technologies Inc. 1997\n");
							fprintf(stderr,"Author : Frederic Tessier, tessier@moebius.qc.ca\n");
							return(status);
							break;
					case '?':		/* usage - options */
							fprintf(stderr, usage);
							return(status);
							break;
					default:
							(*argv)--; (*argv)--;
							fprintf(stderr, "Illegal option: [%s]\n",
									*argv);
							(*argv)++; (*argv)++;
							status++;
							break;
			}
			argv++;
	}

	if (status)
	{
			fprintf(stderr, usage);
			return(status);
	}
	if(*argv)
	{
			if((dlfile = fopen(*argv, "w")) == NULL)
					perror(*argv);
			else
					filename = *argv;
	}
	else
			filename = "stdout";

	connection = new BSerialPort;
	if (connection->Open(devicename) > 0)
	{
		/* this seem to have been removed from DR7/DR8 
		connection->SetNumBytes(1);
		*/
		connection->SetFlowControl(flowcontrol);
		connection->SetDataRate(baudrate);
		connection->SetStopBits(stopbits);
		connection->SetDataBits(databits);
		connection->SetParityMode(paritymode);  
		fprintf(stderr,"Succesfully open %s\n",devicename);
		fprintf(stderr,"Baud : %s BPS  Flow Control : %s  Stop Bits : %s  Data Bits : %s  Parity Mode : %s\n",baudmode,flowmode,datamode,stopmode,paritymodt); 
		if (tracemode == 0)
				fprintf(stderr,"Trace Mode : ON\n");
		else
				fprintf(stderr,"Trace Mode : OFF\n");
		fprintf(stderr,"Receiving File In %s\n",filename);
		connection->Read(data_serial, 1); 
		data_char = data_serial[0];
		bytecount = 1;
		while (data_char != '\032')
		{
			if (tracemode == 0)
			{
					if (bytecount == 1024)
					{
							bytecount = 1;
							fprintf(stderr,"#"); 
					}
					else
							bytecount++;
			}
			fprintf(dlfile,"%c",data_char);
			connection->Read(data_serial, 1);
			data_char = data_serial[0];
		} 
		connection->Write(data_output, 14); 
		fprintf(stderr,"\nFile Transfer Complete\n");
		fprintf(stderr,"Now closing %s\n",devicename); 
		connection->Close();
	}
	else
	{
		fprintf(stderr,"Failed to open %s\n",devicename);
		fprintf(stderr,"Please verify your connection\n");
	}
	
	delete(connection);
	return(0);
}


