//
/* Server.c
	This program is designed to read a file and send its contents to through serial port
	start of file transfer is indicated by a START character (0xF8) and end of file is
	indicated by an END character (0xC0)

Author: Nan Ying


*/

#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>

#include "wsc.h"
#include "keycode.h"
#include "sayerror.h"



#define END             0xC0//300    /* indicates end of file */
#define START			0xF8		/*indicates start of file*/
#define ESC             0xDB //0333    /* indicates byte stuffing */
#define ESC_END         0xDC //0334    /* ESC ESC_END means END data byte */
#define ESC_ESC         0xDD //0x0335    /* ESC ESC_ESC means ESC data byte */
char file_name[1000];
int Port;
int Baud;

// trap WSC error codes

int ErrorCheck(int Port, int Code)
{if(Code<0)
   {SayError(Code,NULL);
    SioDone(Port);
	getch();
    exit(1);
   }
 return Code;
}

//** main **

void main(int argc, char *argv[])
{char c;
 int  i = 1;
	FILE *file1;

 printf("This is a console mode program & is designed to run from a command window.\n");

printf("Enter the port number(0 =COM1, 1 =COM2) and baud rate in the format <port> <baud>.\n");
scanf("%i %i", &Port, &Baud);
 // process args
 //if(argc!=3)
 //  {printf("Usage: CONSOLE <port> <baud>\n");
  //  printf("   Eg: CONSOLE 1 19200\n");
 //   return;
 // }
 //Port = 1;
 // pass the key code
 if(SioKeyCode(WSC_KEY_CODE)<0)
   {printf("ERROR: Bad Key Code!");
    exit(1);
   }

 // set defaults for all ports. Note 'Port' argument is -1
 ErrorCheck( -1, SioReset(-1,1,1)); // DTR & RTS set at port initialization
 ErrorCheck( -1, SioParms(-1,WSC_NoParity,WSC_OneStopBit,WSC_WordLength8));
 // reset (initialize) the port
 ErrorCheck( Port, SioReset(Port,1024,1024) );
 ErrorCheck( Port, SioBaud(Port,Baud) );
 // DTR and RTS already set by SioReset(-1,1,1)
   ///ErrorCheck( SioDTR(Port,'S') );
   ///ErrorCheck( SioRTS(Port,'S') );
 //printf("\nEnter terminal loop ( Type ^Z to exit )\n");
 // enter terminal loop

printf("Enter the name of the file to be transfered. \n");
scanf("%s", file_name);



file1 = fopen(file_name, "r");


if (file1 == NULL)
{
	printf("error opening file\n");
	return -1;
	
}



	/* send an initial END character to flush out any data that may
	 * have accumulated in the receiver due to line noise
	*/
	 SioPutc(Port,'\r');

	/* for each byte in the packet, send the appropriate character
	* sequence
	*/

	 printf("Sending %s through the serial port\n", file_name);
	SioPutc(Port,START);
	i = fscanf(file1, "%c", &c);
	

	while(i !=EOF)
	{
	
		switch(c)
		{
  			/* if it's the same code as an END character, we send a
   	 		 * special two character code so as not to make the
   	 		 * receiver think we sent an END
		     */
    		case END:
            	 SioPutc(Port, ESC);
            	 SioPutc(Port, ESC_END);
            	break;

    		/* if it's the same code as an ESC character,
     		 * we send a special two character code so as not
     		 * to make the receiver think we sent an ESC
    		 */
    		case ESC:
            	 SioPutc(Port, ESC);
				SioPutc(Port, ESC_ESC);
            	break;

    		/* otherwise, we just send the character
     		 */
    		default:
            	 SioPutc(Port,c);
    	}

	i = fscanf(file1, "%c", &c);  		
  
  	}

	/* tell the receiver that we're done sending the packet
  	*/
 SioPutc(Port, END);
 fclose(file1);
 printf("Done sending file.\n");
 printf("Press any key to close the command window.\n");
 getch();

}

