/*
    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/NetDebug.c,v $
    Affiliation:           ETH Zuerich, TIK
    Version:               $Revision: 2936 $
    Last Date of Change:   $Date: 2007-03-21 15:23:42 +0100 (Mit, 21 Mär 2007) $  by: $Author: baderma $
 
*/
#include "Topsy.h"
#include "Syscall.h"
#include "IO.h"
#include "NetDebug.h"
#include "NetBuf.h"
#include "NetAttr.h"
#include "Support.h"
#include "Lock.h"

#ifdef NETDEBUG

ThreadId netdbg_tty;

void netdbgInit()
{
  ioOpen(IO_CONSOLE, &netdbg_tty);
  //	ioInit(netdbg_tty);
}

void netdbgEnd()
{
  ioClose(netdbg_tty);
}

void netdbgDisplay(unsigned long family, char *str)
{
  netdbgPrintf(family, "%s", str);
}


void netdbgPrintf(unsigned long family, char *fmt, ...)
{
  va_list ap;
  unsigned long int len = 0;
  char netdbgBuf[160];

  if((family==0) || (family & NETDEBUG_MASK))
  {
    va_start(ap, fmt);
    len = vsnprintf(netdbgBuf, 160, fmt, ap);
    len = len>=160?160:len;
    ioWrite(netdbg_tty, netdbgBuf, &len);
    va_end(ap);
  }
}

void netdbgDumpBuf(unsigned long family, NetBuf buf)
{
  NetBuf	currBuf;

  currBuf=buf;
  while (currBuf)
  {
    int len=NETBUF_LEN(currBuf);
    unsigned char *ptr=NETBUF_DATA(currBuf);

    if(!(family & NETDEBUG_MASK))
      return;

    netdbgPrintf(family,
                 "next: %p, pool: %p, bufnr: %d, size: %d, start: %d, end: %d\n",
                 currBuf->next, currBuf->pool, currBuf->bufnr,
                 currBuf->size, currBuf->start, currBuf->end);

    while(len>=4)
    {
      netdbgPrintf(family, "%p: %02x %02x %02x %02x\n", ptr,
                   (int) ptr[0],(int) ptr[1], (int) ptr[2], (int) ptr[3]);
      ptr+=4;
      len-=4;
    }
    if ( len > 0 )
    {
      netdbgPrintf(family, "%p: %02x %02x %02x 00\n", ptr,  (int) ptr[0],
                   len > 1?(int) ptr[1]:0, len > 2?(int) ptr[2]:0);
    }
    currBuf=currBuf->next;
  }

}


void netdbgDumpBufLong(unsigned long family, NetBuf buf)
{
  NetBuf	currBuf;
  int		nCount=0;

  currBuf=buf;
  while (currBuf)
  {
    int len=NETBUF_LEN(currBuf);
    unsigned char *ptr=NETBUF_DATA(currBuf);

    if(!(family & NETDEBUG_MASK))
      return;

    while(len>0)
    {
      if ((nCount%16)==0)
        netdbgPrintf(family, "\n");
      netdbgPrintf(family, ",%d", *ptr++);
      len--;
      nCount++;
    }
    currBuf=currBuf->next;
  }

}

void netdbgDumpAttr(unsigned long family, NetAttrs attr)
{
  netattrDebug(family, NULL, attr);
}
#endif
