/*****************************************************************/
/**                                                             **/
/**             (C) Copyright 1992 the Regents of the           **/
/**        University of California.  All Rights Reserved.      **/
/**                                                             **/
/**        This work was produced under the sponsorship of      **/
/**         the U.S. Department of Energy.  The Government      **/
/**                retains certain rights therein.              **/
/**                                                             **/
/**                         Written by                          **/
/**         Distributed Programming Environments Project        **/
/**           Computer Software and Networking Division,        **/
/**                   Livermore Computer Center,                **/
/**            Lawrence Livermore National Laboratory           **/
/**                                                             **/
/*****************************************************************/

static char sccs_ASNgetP_start[] = "@(#)ASNgetP_st.c	1.14 10/19/92 13:19:50  LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	10/12/90

	ROUTINE:	getP_start
	Version:	__ROr_ASNgetP_start

PURPOSE:
	This routine gets the PPDU header

IN:
	instance -

OUT:


CALLED ROUTINES:
	

	
MODIFICATION HISTORY:
	Added sending_PPDUs field to instance record (ls 920414)


*/

#include <elros/instance.h>
#include <elros/typetags.h>

int __ROr_ASNgetP_start(instance)

	__RO_Instance_Record *instance;

{
  if (instance->sending_PPDUs) {
    __RO_int32 type;
    unsigned long length;

    int pci;
    unsigned long pcisize = sizeof(pci),
                  pcilength = sizeof(pci);

    while (instance->get_state.ppdu_state != __ROm_ppdu_state3){

      if (instance->get_state.ppdu_state == __ROm_ppdu_state1)
        instance->receive_state.done_available = 1;

      if ((instance->getP_header)(instance,&type,&length))
        return(EXCEPTION);

      switch (instance->get_state.ppdu_state)
       {
         case __ROm_ppdu_state1:
            if (type != TAG_FULLY_ENCODED_PPDU)
               __RO_RETURN_RUNTIME(instance,__ROx_PPDU_EXPECTFULLY);
            instance->get_state.ppdu_state = __ROm_ppdu_state2;
            break;
         case __ROm_ppdu_state2:
            if (type == TAG_EOC)
               instance->get_state.ppdu_state = __ROm_ppdu_state1;
            else if (type == TAG_SEQUENCE)
               instance->get_state.ppdu_state = __ROm_ppdu_state3;
            else
               __RO_RETURN_RUNTIME(instance,__ROx_PPDU_EXPECTPDVSEQ);
            break;
         case __ROm_ppdu_state3:
               /* never happens */
            break;
         default:
            __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_MUTUAL);
            break;
       }

    } /* while */

    if ((instance->getP_header)(instance,&type,&length))
      return(EXCEPTION);
    if (type == TAG_ObjectIdentifier) {
	/* We're ignoring the value here for now. */
      if (__ROr_BERskip_oid(instance,NULL,NULL,&length,NULL,NULL))
         return(EXCEPTION);
      if ((instance->getP_header)(instance,&type,&length))
         return(EXCEPTION);
    }

    if (type != TAG_Integer)
      __RO_RETURN_RUNTIME(instance,__ROx_PPDU_EXPECTPCITAG);
    if (instance->getP_Integer(instance,&pci,&pcisize,&length,&pcilength,NULL))
      return(EXCEPTION);
    if (pci != instance->ISO_Contexts.specific_pcid)
      __RO_RETURN_RUNTIME(instance,__ROx_PPDU_BADPCI);

    if ((instance->getP_header)(instance,&type,&length))
      return(EXCEPTION);
    if (type != TAG_SINGLE_ASN_VALUE)
      __RO_RETURN_RUNTIME(instance,__ROx_PPDU_EXPECTSINGLEASN);

  }

  else { /* Not sending_PPDUs */
    instance->receive_state.done_available = 1;
  }
    
  return(SUCCESS);
}

