/*
    PacketDriverTest.c, Copyright 1.5.97 (c) by Bernard Stauffer,
    Swiss Federal Institute of Technology,
    Computer Engineering and Networks Laboratory.
 
    TOPSY -- A Teachable Operating System.
             Implementation of a tiny and simple
             micro kernel for teaching purposes.
 
    For further information, please visit http://www.tik.ee.ethz.ch/~topsy
 
    This software is provided under the terms of the GNU General Public Licence.
 
    A full copy of the GNU GPL is provided in the file COPYING found in the 
    development root of Topsy.
*/
/*
  File:                  $Source: /home/ruf/cvs_Topsy/cvs/Topsy/User/PacketDriverTest.c,v $
  Author(s):             Bernard Stauffer 
  Affiliation:           ETH Zuerich, TIK
  Version:               $Revision: 2936 $
  Creation Date:         1.5.97
  Last Date of Change:   $Date: 2007-03-21 15:23:42 +0100 (Mit, 21 Mär 2007) $      by: $Author: baderma $
  
*/

#include "../Topsy/Topsy.h"
#include "../Topsy/Syscall.h"
#include "../IO/IOConsole.h"
#include "UserSupport.h"
#include "PacketDriverTest.h"

/* ---------------------------------------------------------------------------- */
/* A simple test programm, which helps you to debug your driver. A string is    */
/* sent to the driver and expected to be received. Some 'display'-functions     */
/* are included to enabled fast debugging.                                      */

void PacketDriverTest()
{

  char                fromNet[25], s[5];
  ThreadId            tty, driver;
  unsigned long int   sizelen1 = 0, sizelen2 = 0, read_bytes = 0, total_bytes = 0;

  char hello1[] = "\nTest PacketDriver\n";
  char hello2[] = "\nHello World over FPGA\n";

  sizelen1 = stringLength(hello1);
  sizelen2 = stringLength(hello2);

  ioOpen(IO_CONSOLE, &tty);
  ioInit(tty);
  ioOpen(IO_FPGA_COMM, &driver);
  ioInit(driver);

  /* Writing the welcome message to console */
  ioWrite(tty, hello1, &sizelen1);

  /* Writing the message to the net */
  if (ioWrite(driver, hello2, &sizelen2) == IO_WRITEFAILED)
  {
    display(tty,"ioWrite failed");
  }
  else
  {
    display(tty,"\nMessage sent to FPGA. Waiting to receive ...\n");
    read_bytes = 25;
    total_bytes = 0;
    while (total_bytes != sizelen2)
    {       /* Receiving the message from the net */
      if (ioRead(driver, fromNet, &read_bytes) == IO_READFAILED)
      {
        display(tty,"ioRead failed");
        read_bytes = 0;
      }
      else
      {
        total_bytes = total_bytes + read_bytes;   /* Count the received bytes */
        itoa(read_bytes, s);
        display(tty,s);
        display(tty," bytes received.\n");
      }
    }
    /* Writing the received message to console */
    ioWrite(tty, fromNet, &total_bytes);

  }

}
