
/* $Header: /home/cobweb3/surendar/networks/RCS/datalink_sample.c,v 1.1 1993/09/02 23:11:46 surendar Exp $ */

/*
 * Module:	This module supports the data link layer functions.
 * Author:	$Author: surendar $ 
 * 		Dept. of Computer Sciences 
 * 		Worcester Polytechnic Institute. 
 * Date:	$Date: 1993/09/02 23:11:46 $ 
 */

/* 
 * $Log: datalink_sample.c,v $
 * Revision 1.1  1993/09/02  23:11:46  surendar
 * Initial revision
 *
 * Revision 1.1  1993/01/14  19:07:36  surendar
 * Checked in for Assignment I
 * 
 */

#ifndef lint
static char rcs_ident[] = "$Header: /home/cobweb3/surendar/networks/RCS/datalink_sample.c,v 1.1 1993/09/02 23:11:46 surendar Exp $";
#endif /* lint */

#include	<stdio.h>
#include	<sys/types.h>
#include	<malloc.h>
#include	<memory.h>

#if HAVE_STDARG_H
#include	<stdarg.h>
#endif /* HAVE_STDARG_H */

#include	"netcp.h"

/*
 * ====================================================================
 *
 * Function: dl_process
 *
 * Arguments: void
 *
 * Return: void
 *
 * Description: Does all processing for the data link layer. The current
 * 	     version just gets packets from either AL or the PL and send
 * 	     them over to PL or AL respectively. You may want to add stuff
 * 	     to add checksum to the packet from AL and check the checksum
 * 	     for packets from PL. [ Very similar to Unrestricted Simplex
 * 	     Protocol[ Page 217]
 *
 * ===================================================================
 */
void
    dl_process(void)
{
    err_t ret_value;
    size_t len;
    char data_pkt[1024], *recv_pkt;
    frame_t phys_frame, *recv_frame;
    
    log(10, "Data link layer up");
    
    while(1)
	{
	    /* Anything from Hybrid layer?? */
	    if ((ret_value = FromHybridLayer((void **)&recv_pkt, &len, 0)) ==
		Success)
		{
#ifdef DEMO			/* Just send it across, DL does nothing */
		    dump("AL_RECV", recv_pkt, len);
		    
		    /* Send it to physical layer */
		    ToPhysicalLayer(recv_pkt, len);
#else				/* Somethings that you might want to add */
		    phys_frame.kind = f_data;
		    phys_frame.seq = 0;	/* Don't care */
		    phys_frame.ack = 0;	/* Don't care */
		    phys_frame.len = len; /* Length of packet */
		    memcpy((void *)&phys_frame.info,
			   (void *)recv_pkt,
			   sizeof(phys_frame.info));
		    dump("AL_RECV", recv_pkt, sizeof(frame_t));
		    
		    /* Send it to physical layer */
		    ToPhysicalLayer(&phys_frame, sizeof(frame_t));
#endif /* DEMO */
		    free(recv_pkt); /* Not required anymore */
		}
	    
	    /* Anything from Physical layer?? */
#ifdef DEMO			/* Just get stuff from Physical layer, */
				/* don't interpret the size of receive */
				/* buffer */ 
	    if ((ret_value = FromPhysicalLayer((void **)&recv_frame, &len, 0)) ==
		Success)
		{
		    /* Send it across to Hybrid layer!! */
		    dump("PL_RECV", recv_frame, len);
		    ToHybridLayer(recv_frame, len);
		    free(recv_frame);
		}
#else
	    /* We actually know how bif the physical frame is and so need */
	    /* to operate accordingly, interpreting the length from the */
	    /* received packet */
	    if ((ret_value = FromPhysicalLayer((void **)&recv_frame, 0)) ==
		Success)
		{
		    /* Send it across to Hybrid layer!! */
		    dump("PL_RECV", recv_frame, sizeof(frame_t));
		    memcpy(&data_pkt, &recv_frame->info, sizeof(data_pkt));
		    ToHybridLayer(&recv_frame->info, recv_frame->len);
		    free(recv_frame);
		}
#endif /* DEMO */
	}
}
