/*
    Copyright 2000 (c) by David Schweikert,
    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/Net/old/NetTest.c,v $
    Author(s):             
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2922 $
    Creation Date:         
    Last Date of Change:   $Date: 2007-03-19 17:48:52 +0100 (Mon, 19 Mär 2007) $      by: $Author: baderma $
 
 
    $Log: NetTest.c,v $
    Revision 1.4  2001/01/23 09:45:52  cjeker
    moved to Net/old
 
    Revision 1.3  2001/01/10 11:34:19  blutz
    getting the network stack to work and adapting stuff to new IO structure
 
    Revision 1.2  2000/11/15 10:12:14  ruf
    Repository Reorganisation
 
    Revision 1.1  2000/03/31 17:50:35  gfa
    Merged with /Net from several term projects
 
*/
#include <Topsy.h>
#include <Syscall.h>
#include <UserSupport.h>

#include <NetAttr.h>
#include <NetBuf.h>
#include <NetModules.h>
#include <NetDebug.h>

static ThreadId tty2;

void NetTestGetTime(ThreadArg arg)
{
  NetBuf buf;
  NetAttrs attrs;
  unsigned short port;
  NetModMsg msg;
  ThreadId tty;

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

  /* the daytime-protocol is as follows: send a UDP datagram to port 13
   * (the contents arn't important) and then a reply UDP datagram
   * containing a ASCII representation of time will be sent back */

  /* Send a listen request to the UDP module, with port-nr. 0, so that
   * the first free port is reserved */
  netmodListen(NETMODULE_UDP, 0, 0);

  /* Wait for LISTENREPLY */
  if(!netmodMsgRecv(&msg))
    return;

  /* msg.value contains the reserved port nr. */
  port = msg.value;
  netdbgPrintf(0,"port = %d\n",port);

  /* send empty UDP datagram */
  netbufAlloc(&buf, 0);
  netattrNew(&attrs);
  netattrSet(attrs, NETATTR_UDP_FROM, port);
  netattrSet(attrs, NETATTR_UDP_TO, 13);
  netattrSet(attrs, NETATTR_IP_TO_0, 192);
  netattrSet(attrs, NETATTR_IP_TO_1, 168);
  netattrSet(attrs, NETATTR_IP_TO_2, 2);
  netattrSet(attrs, NETATTR_IP_TO_3, 1);
  netmodSendDown(NETMODULE_UDP, buf, attrs);

  /* wait for reply containing the time-string */
  /* This could block if the destination address isn't resolved */
  /* because no answer will come :-( */
  if(!netmodMsgRecv(&msg))
    return;
  buf = msg.buf;

  /* display string on screen */
  printf(NETBUF_DATA(buf));

  /* close port */
  netmodClose(NETMODULE_UDP, 0, port);
}

/* little server for udp port 100, which echoes on screen what it receives. */
void nettestUDP()
{
  /* Listen to the UDP port */
  netmodListen(NETMODULE_UDP, 0, 100);

  /* We will now receive packets from the UDP module */

  while(1)
  {
    NetModMsg msg;
    NetBuf buf;

    if(!netmodMsgRecv(&msg))
      return;

    if(msg.id != NETMOD_SENDUP)
    {
      printf("unknown message!\n");
      continue;
    }

    buf = msg.buf;
    printf("\nUDP datagram received (len=%d):\n", netbufLen(buf));
    printf("Attributes:\n");
    netattrDebug(0,"udp",msg.attrs);
    printf("Contents: <");
    while(buf)
    {
      printf( NETBUF_DATA(buf));
      buf = buf->next;
    }
    printf(">\n");
  }
}

void NetTest(ThreadArg arg)
{
  ThreadId threadid;

  ioOpen(IO_CONSOLE, &tty2);
  ioInit(tty2);

  /* Start udp-test-server */
  if (tmStart(&threadid, nettestUDP, (ThreadArg)0, "udp-test") != TM_STARTOK)
  {
    return;
  }
}
