/****************************************************************************/
/* Copyright 2002 MBARI							*/
/****************************************************************************/
/* Summary  : Main Routine for OASIS Mooring Controller                     */
/* Filename : oasis.c                                                       */
/* Author   : Robert Herlien (rah)					    */
/* Project  : OASIS Mooring Replacement (OASIS3)			*/
/* Revision : 0.1							*/
/* Created  : 10/14/2002						*/
/*									    */
/* MBARI provides this documentation and code "as is", with no warranty,    */
/* express or implied, of its quality or consistency. It is provided without*/
/* support and without obligation on the part of the Monterey Bay Aquarium  */
/* Research Institute to assist in its use, correction, modification, or    */
/* enhancement. This information should not be published or distributed to  */
/* third parties without specific written permission from MBARI.            */
/*									    */
/****************************************************************************/
/* Modification History:						    */
/* 10oct2002 rah - created from OASIS oasis.c				*/
/****************************************************************************/

#include	<mbariTypes.h>		/* MBARI type definitions	    */
#include	<mbariConst.h>		/* MBARI constants		    */
#include	<cfxbios.h>		// Persistor BIOS and I/O Definitions
#include	<cfxpico.h>		// Persistor PicoDOS Definitions
#include	<stdio.h>
#include	<fcntl.h>
#include	<unistd.h>

static char	*filename = "TEST.DAT";
static char	buffer[32768];

#define CPU_CLOCK	7360L
	

/************************************************************************/
/* Function    : main							*/
/* Purpose     : Main entry point					*/
/* Inputs      : None							*/
/* Outputs     : None							*/
/* Comments    : Never returns						*/
/************************************************************************/
int main( int argc, char **argv)
{
#pragma unused (argc, argv)
  Nat32		i, opentime, iotime, closetime, deltime;
  Nat16		filesize = 32;
  Nat16		sz;
  Nat32		cursize, remain;
  int		fd;
  FILE		*fp;
  RTCTimer	rt;

  //  BIOSInit();
  TMGSetSpeed(CPU_CLOCK);
  // Identify the progam and build
  printf("\nProgram: %s: %s %s \n", __FILE__, __DATE__, __TIME__);
  // Identify the device and its firmware
  printf("Persistor CF%d SN:%ld   BIOS:%d.%d   PicoDOS:%d.%d\n", CFX,
	 BIOSGVT.CFxSerNum, BIOSGVT.BIOSVersion, BIOSGVT.BIOSRelease, 
	 BIOSGVT.PICOVersion, BIOSGVT.PICORelease);
	
  printf("OASIS file test program\n\n");
  //  chdir("\FOO");
  //  getcwd(buffer, sizeof(buffer));
  //  printf("Working directory is %s\n", buffer);

  while(TRUE)
  {
    printf("\n");
    printf("Type 'R' to read file using fread\n");
    printf("Type 'r' to read file using read\n");
    printf("Type 'W' to write file using fwrite\n");
    printf("Type 'w' to write file using write\n");
    printf("Type 'S' to change size of file\n");
    printf("Type 'F' to get disk free space\n");
    printf("Type 'X' to exit\n");

    switch(cgetc())
    {
      case 'X':
      case 'x':
      case 3:
	printf("Goodbye\n");
	return(0);

      case 'S':
      case 's':
	printf("Current file size is %d KB.  New file size in KBytes:  ",
	       filesize);
	if ((scanf("%d", &sz) < 1) || (sz < 1) || (sz > 2048))
	{
	  printf("\nBad input value\n");
	  break;
	}
	filesize = sz;
	printf("\nNew file size is %d KB\n", filesize);
	break;

      case 'R':
	RTCElapsedTimerSetup(&rt);
	if ((fp = fopen(filename, "rb")) == NULL)
	{
	  perror("Filetest:  Can't open file");
	  break;
	}
	opentime = RTCElapsedTime(&rt);

	RTCElapsedTimerSetup(&rt);
	for (remain = 1024L * filesize; remain > 0L; )
	{
	  cursize = (remain < sizeof(buffer)) ? remain : sizeof(buffer);
	  fread(buffer, cursize, 1, fp);
	  remain -= cursize;
	}
	iotime = RTCElapsedTime(&rt);
	
	RTCElapsedTimerSetup(&rt);
	fclose(fp);
	closetime = RTCElapsedTime(&rt);

	printf("Open time %ld us, Read time %ld us, Close time %ld us\n\n",
	       opentime, iotime, closetime);
	break;

      case 'W':
	for (i = 0; i < sizeof(buffer); i++)
	  buffer[i] = (char)i;

	RTCElapsedTimerSetup(&rt);
	if ((fp = fopen(filename, "wb")) == NULL)
	{
	  perror("Filetest:  Can't create file");
	  break;
	}
	opentime = RTCElapsedTime(&rt);

	RTCElapsedTimerSetup(&rt);
	for (remain = 1024L * filesize; remain > 0L; )
	{
	  cursize = (remain < sizeof(buffer)) ? remain : sizeof(buffer);
	  fwrite(buffer, cursize, 1, fp);
	  remain -= cursize;
	}
	iotime = RTCElapsedTime(&rt);
	
	RTCElapsedTimerSetup(&rt);
	fclose(fp);
	closetime = RTCElapsedTime(&rt);

	printf("Open time %ld us, Write time %ld us, Close time %ld us\n\n",
	       opentime, iotime, closetime);
	break;

      case 'r':
	RTCElapsedTimerSetup(&rt);
	if ((fd = open(filename, O_RDONLY)) < 0)
	{
	  perror("Filetest:  Can't open file");
	  break;
	}
	opentime = RTCElapsedTime(&rt);

	RTCElapsedTimerSetup(&rt);
	for (remain = 1024L * filesize; remain > 0L; )
	{
	  cursize = (remain < sizeof(buffer)) ? remain : sizeof(buffer);
	  read(fd, buffer, cursize);
	  remain -= cursize;
	}
	iotime = RTCElapsedTime(&rt);
	
	RTCElapsedTimerSetup(&rt);
	close(fd);
	closetime = RTCElapsedTime(&rt);

	printf("Open time %ld us, Read time %ld us, Close time %ld us\n\n",
	       opentime, iotime, closetime);
	break;

      case 'w':
	for (i = 0; i < sizeof(buffer); i++)
	  buffer[i] = (char)i;

	RTCElapsedTimerSetup(&rt);
	unlink(filename);
	deltime = RTCElapsedTime(&rt);

	RTCElapsedTimerSetup(&rt);
	if ((fd = open(filename, O_WRONLY | O_CREAT)) < 0)
	{
	  perror("Filetest:  Can't create file");
	  break;
	}
	opentime = RTCElapsedTime(&rt);

	RTCElapsedTimerSetup(&rt);
	for (remain = 1024L * filesize; remain > 0L; )
	{
	  cursize = (remain < sizeof(buffer)) ? remain : sizeof(buffer);
	  write(fd, buffer, cursize);
	  remain -= cursize;
	}
	iotime = RTCElapsedTime(&rt);
	
	RTCElapsedTimerSetup(&rt);
	close(fd);
	closetime = RTCElapsedTime(&rt);

	printf("Del time %ld us, Open time %ld us, Write time %ld us, Close time %ld us\n\n",
	       deltime, opentime, iotime, closetime);
	break;

      case 'F':
      case 'f':
	RTCElapsedTimerSetup(&rt);
	cursize = DIRFreeSpace("C:");
	opentime = RTCElapsedTime(&rt);
	printf("Free space = %ld, time = %ld us\n", cursize, opentime);
	break;
    }
  }

  return(0);

}	//____ main() ____//

