/*->c.mnpevent */
/*GLOBAL********************************************************************

        _event_wait - wait for event or timeout

***************************************************************************/


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>

#include "h.mnpdat"
#include "h.llvl"
#include "h.mnp"




void suspend(int delay)
{
  /* delay of 100ms */
 delay=clock()+10*delay;
 while(clock()<delay) pollzt();
}



SIGN_16 event_wait(USIGN_16 time,USIGN_16 flag)
{

register USIGN_16 i;

i = 0;
time -= 2;              /* to offset overhead in this routine */
                                /* 2/10 of a second - just an estimate */

switch (flag)
        {
        
        case FRAME_SND:
                if (frame_snt)
                        {
                        frame_snt = FALSE;
                        break;
                        }
                /* set_int(); */
                for (;(frame_snt == FALSE) && (i <= time); i++)
                        {
                        suspend (1);
                        if (lne_stat())
                                return (NO_PHYSICAL);
                        }
                if (frame_snt)
                        {
                        frame_snt = FALSE;
                        return(SUCCESS);
                        }
                else
                        return(TIME_OUT);

        case FRAME_RCV:
                if (frame_rcvd)
                        {
                        frame_rcvd = FALSE;
                        break;
                        }
                /* set_int(); */
                for (;(frame_rcvd == FALSE) && (i <=time); i++)
                        {
                        suspend(1);
                        if (lne_stat())
                                return (NO_PHYSICAL);
                        }
                if (frame_rcvd)
                        {
                        frame_rcvd = FALSE;
                        return(SUCCESS);
                        }
                else
                        return(TIME_OUT);

        case FRAME_DN:
                if (frame_dne)
                        {
                        frame_dne = FALSE;
                        break;
                        }
                /* set_int(); */
                for (;(frame_dne == FALSE) && (i <= time); i++)
                        {
                        suspend (1);
                        if (lne_stat())
                                return (NO_PHYSICAL);
                        }
                if (frame_dne)
                        {
                        frame_dne = FALSE;
                        return(SUCCESS);
                        }
                else
                        return(TIME_OUT);

        default:
                /* set_int(); */
                return(FAILURE);
        }

/* set_int(); */
return(SUCCESS);

}
