/* $Header: /home/cobweb3/surendar/networks/RCS/defs.h,v 2.2 1993/09/02 23:11:46 surendar Exp $ */

/* \input cnoweb
 * \title $Source: /home/cobweb3/surendar/networks/RCS/defs.h,v $
 * \synopsis{ typedefs and #defines
 * 
 * Author:	$Author: surendar $
 * 		Dept. of Computer Sciences
 * 		Worcester Polytechnic Institute.
 * Date:	$Date: 1993/09/02 23:11:46 $
 *
 * Copyright (c) 1993 $Author: surendar $
 */

/*
 * $Log: defs.h,v $
 * Revision 2.2  1993/09/02  23:11:46  surendar
 * Latest Version
 *
 * Revision 2.1  1993/01/29  08:09:07  surendar
 * Assignment II release
 *
 * Revision 1.4  1993/01/29  08:06:15  surendar
 * Prepare for assignment II
 *
 * Revision 1.3  1993/01/20  06:19:14  surendar
 * More stricter Physical layer.
 * Accepts only FRAMESIZE characters
 *
 * Revision 1.2  1993/01/14  19:07:36  surendar
 * Checked in for Assignment I
 *
 */

#define IP 		  0
#ifdef DEMO
#define DATASIZE  512		/* Size of packet sent by data link layer */
#define CTRLSIZE  16		/* Size of the control packet */
#define FRAMESIZE 128		/* Size of the physical frame */
#else
#error Have to be defined
#endif /* DEMO */

#define UNIQUE_KEY getuid()

#ifndef HAVE_CADDR_T
typedef char *caddr_t;
#endif /* HAVE_CADDR_T */

typedef struct phys_ctrl {
    char dest[256];
    long speed;			/* in bauds */
    long error_rate;		/* [ 0 .. 100 ] % */
    long seed;			/* For the random number generator in */
				/* physical layer  */
    long valid;
} phys_ctrl_t;

typedef struct queue {
    void *elem;
    size_t len;
    struct queue *nextptr;	/* Doubly linked structure */
    struct queue *prevptr;
} queue_t;
    
typedef enum {
    Hybrid,
    Datalink,
    Physical
} layer_t;			/* Current layer */

typedef enum {			/* Various timers */
    Ack_Timer = 0
} timer_t;

typedef enum err {
    Success = 0,
    Failure,
    E_Inappr,
    E_Nodata,
    E_Noack,
    E_Notimer
} err_t;			/* Network error */

typedef enum {
    False = 0,
    True = 1
} boolean_t;

typedef enum {
    Data,
    Ctrl,
    Eof,
    Stat
} packet_type_t;

typedef struct data_packet {
    packet_type_t kind;
    size_t 	len;
    char 	buf[DATASIZE - sizeof (packet_type_t) - sizeof(size_t)];
} data_packet_t;			/* Hybrid layer structre */

typedef struct ctrl_packet {
    packet_type_t kind;
    char 	buf[CTRLSIZE - sizeof(packet_type_t)];
} ctrl_packet_t;

typedef enum {
    f_data,
    f_ack,
    f_nak
} framekind_t;

typedef struct frame {		/* to physical layer */
    framekind_t kind;
    long   seq;
    long   ack;
	long   len;
    data_packet_t info;
    long crc;
} frame_t;

#define hldl_send(mesg, len) __send(Hybrid, aldl_fd, mesg, len)	/* HL -> DL */
#define dlhl_recv(mesg, len, delay) __recv(Hybrid, dlal_fd, mesg, len, delay)	/* HL <- DL */

#define dlpl_send(mesg, len) __send(Datalink, dlpl_fd, mesg, len) /* DL -> PL */
#define pldl_recv(mesg, len, delay) __recv(Datalink, pldl_fd, mesg, len, delay) /* DL <- PL */

#define dlhl_send(mesg, len) __send(Datalink, dlal_fd, mesg, len) /* DL -> HL */
#define hldl_recv(mesg, len, delay) __recv(Datalink, aldl_fd, mesg, len, delay) /* DL <- HL */

#define pldl_send(mesg, len) __send(Physical, pldl_fd, mesg, len) /* PL -> DL */
#define dlpl_recv(mesg, len, delay) __recv(Physical, dlpl_fd, mesg, len, delay)/* DL <- PL */

/* Wrapper for the error message functions */
#define fatal(mesg) _fatal(mesg, __FILE__, __LINE__)
#define nerror(mesg) _nerror(mesg, __FILE__, __LINE__)
#define log(level, mesg) _log(level, mesg, __FILE__, __LINE__)
#define dump(mesg, buf, len) _dump(mesg, buf, len, __FILE__, __LINE__)

static size_t __len;		/* Don't care what this is */
#define FromHybridLayer(packet, len, delay) hldl_recv(packet, len, delay)
#define ToHybridLayer(packet, len)   dlhl_send(packet, len)
				/*Wait for a long time*/

#ifndef PRIVATE
#define FromPhysicalLayer(frame, len, delay) pldl_recv(frame, len, delay) 
#define ToPhysicalLayer(frame, len) dlpl_send(frame, len)
#else
#define FromPhysicalLayer(frame, delay) pldl_recv(frame, &__len, delay) 
#define ToPhysicalLayer(frame) dlpl_send(frame, FRAMESIZE)
#endif				/* PRIVATE */

#define inc(num) (num) = (((num) + 1) % MaxSeq)
#include	"proto.h"	/* As much as I hate to have include files */
				/* within include files :-< */
#if HAVE_MALLOC_H
#include	<malloc.h>
#endif /* HAVE_MALLOC_H */

#ifdef __hpux
#define bzero(buff, len) memset(buff, '\0', len)
#define bcopy(b1, b2, len) memcpy(b2, b1, len)
#define srandom(seed) srand(seed)
#define random() rand()
#define GETDTABLESIZE 64
#else
#define GETDTABLESIZE getdtablesize()
#endif /* __hpux */

/* \endc */


