/**************************************************************
$Workfile:   diagFileIo.c  $
***************************************************************
**
** FIRMWARE, Copyright (C) 2000-2002        
** Ikanos Communication, Fremont, CA  
** All rights reserved.
**
** The information and contents of this file are the
** proprietary information of Ikanos Communication and may not
** be disclosed or used without the formal written approval of
** Ikanos Communication
**
$Revision: 1.1 $
$Date: 2006/10/16 04:34:50 $
**************************************************************/


/****************************************************************************/
/*                                                                          */
/* Function.. : binToByteArray                                                          */
/*                                                                          */
/* Purpose... : function to convert bin file to a byte array                            */
/*                                                                                                                                                      */
/* Parameters : None                                                                                                            */
/*                                                                          */
/* Returns... : N/A                                                         */
/*                                                                          */
/* Notes..... : can't run this function on the fly in shell. b'cos path as  */
/*                              as the vxWorks.bin file will not be present in the target   */
/*                              so we need to make an executable file for this function         */ 
/****************************************************************************/

void binToByteArray (void)
{
  unsigned int numread = 0, tot = 0;
  unsigned char tmpChar;

  FILE *binFile = NULL;
  FILE *arrayFile = NULL;

  binFile = fopen("vxWorks.bin","rb");
  arrayFile = fopen("bmeCode.h","w");

  fwrite ("BYTE BMECodeArray[512*1024] = {\n", sizeof(char), 17, arrayFile);

  if (binFile)
  {
    while (1)
    {
      /* Attempt to read in 4 characters at a time */
      numread = fread(&tmpChar, sizeof(char), 1, binFile);
      tot += numread;

      if (numread <=0)
      {
        fwrite ("\n};", sizeof (char), 3, arrayFile);
        break;
      }

      fprintf (arrayFile, "0x%02x, ", tmpChar);

      if (!(tot%16))
        fwrite("\n", sizeof(char), 1, arrayFile);

    }
    fclose(binFile);        
    fclose(arrayFile);
  }
  IPOS_PRINTF("File total %d %d\n", tot, numread);

  return; 
} 

/**************************************************/
/*   File access test functions                   */
/**************************************************/

void new () 
{
  FILE *pFile;
  pFile=NULL;

  pFile = fopen ("/tgtsvr/testfile","w");
  if (pFile!=NULL)
  {
    fprintf (pFile,"hello world");
    fclose (pFile);
  }
  else IPOS_PRINTF ("failed open");

}


void write_to_file(
                  BYTE *list_of_bytes,
                  int list_length,
                  char *filename
                  )
{
  FILE *filehandle;
  int count;
  char s[100];

/*open the file and set it up*/
  strcpy(s,"/tgtsvr/");

  filehandle = fopen(strcat(s,filename),"w");

  if (filehandle==NULL)
  {
    IPOS_PRINTF ("\nFailed to open file for write\n");
    IPOS_PRINTF ("\n ### Relaunch shell. Do Not Reset Host CPU \n");
  }
  else
  {
    for (count=0; count< list_length; count++)
    {
      fprintf(filehandle, "%x\n", *(list_of_bytes + count));
    }

    fclose(filehandle);
  }
}

