/*****************************************************************/
/**                                                             **/
/**             (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 sccsid_ASNsendRO_start[] = "@(#)ASNsendRO_st.c	1.22 10/19/92 13:20:22 LLNL ";

/*
	Embedded Language for Remote Operations Services
	ELROS

	AUTHOR:		James A. Guse
			Network Systems Division

	DATE WRITTEN:	6/27/90

	ROUTINE:	sendRO_start
	Version:	__ROr_ASNsendRO_start

PURPOSE:
	This routine sends the ROSE APDU header.  The internal number is
	used to derive the necessary information to send using 'givetable'.
	It stops after sending the opening
	header for the argument SET/SEQUENCE if any.

IN:
	instance - Give table
	senderID - pointer to invokeID/linkedID to send.
		This may be NULL for INVOKEs (linkedID case).
		It must be valid for RESULTs and ERRORs. If NULL for REJECT,
		then a NULL invokeID is sent.
	internal - index into givetable to specify operation-specific encoded
		information.

OUT:
	instance - Send state is updated.  Exception state is updated.


CALLED ROUTINES:
	__ROr_BERsendP_Integer
	__ROr_sendP_1element
	__ROr_sendP_2element
	__ROr_sendP_Nelement

	
MODIFICATION HISTORY:


*/

#include <elros/instance.h>

#include <elros/typecodes.h>
#include <elros/sendP_element.h>

int __ROr_ASNsendRO_start(instance,senderID,linkedID,internal)

   __RO_Instance_Record *	instance;
   long *			senderID, linkedID;
   int *			internal;

{

   __RO_Give_table_Entry *thisop = &(instance->give_table[*internal]);
   unsigned long length;
   unsigned long integer_length1 = 4;


	/* Start the PDV */

   if ((instance->sendPDV_start)(instance))
      return(EXCEPTION);



	/* NOW, Start the Remote Operation (APDU) */


   __ROr_sendP_2element(instance,
		__ROe_ROHtags[thisop->operation_class]);

   switch (thisop->operation_class)
   {
      case __RO_INVOKE:

		/* Send the invokeID from the instance record,
		   and increment the value there.  */

         __ROr_sendP_1element(instance,
			__ROe_Integer_header);

	 if (senderID)
	   instance->sending_invokeID = *senderID;

         length = sizeof(instance->sending_invokeID);
         if (instance->sendP_Integer(instance,
			&(instance->sending_invokeID),
			&length,
			&integer_length1,
			NULL))
            return (EXCEPTION);
         instance->sending_invokeID++;

		/* If 'linkedID' was passed, send it. */

         if (linkedID)
         {
            __ROr_sendP_1element(instance,
			__ROe_linkedID_header);
            if(instance->sendP_Integer(instance,
			linkedID,
			(length = sizeof(*senderID),&length),
			&integer_length1,
			NULL))
               return(EXCEPTION);
         }

         __ROr_sendP_Nelement(instance,
			thisop->encoded_representation,
			thisop->encoded_length);
         break;
      case __RO_RESULT:
         /***** This is the old way
            if (!senderID)
               __RO_RETURN_RUNTIME(instance,__ROx_RO_REQUIREDID);
         ******/
         __ROr_sendP_1element(instance,__ROe_Integer_header);
         if(instance->sendP_Integer(instance,
			senderID ? senderID : &instance->received_invokeID,
			(length = sizeof(*senderID),&length),
			&integer_length1,
			NULL))
            return(EXCEPTION);
         if (thisop->encoded_representation)
         {
            __ROr_sendP_2element(instance,__ROe_SEQUENCE_header);
            __ROr_sendP_Nelement(instance,
			thisop->encoded_representation,
			thisop->encoded_length);
         }
         break;
      case __RO_ERROR:
         /***** This is the old way
            if (!senderID)
               __RO_RETURN_RUNTIME(instance,__ROx_RO_REQUIREDID);
         ******/
         __ROr_sendP_1element(instance,
                         __ROe_Integer_header);
         if(instance->sendP_Integer(instance,
			senderID ? senderID : &instance->received_invokeID,
			(length = sizeof(*senderID),&length),
			&integer_length1,
			NULL))
            return(EXCEPTION);
         __ROr_sendP_Nelement(instance,
			thisop->encoded_representation,
			thisop->encoded_length);
         break;
      case __RO_REJECT:
         if (senderID)
         {
            __ROr_sendP_1element(instance,
			__ROe_Integer_header);
            if (instance->sendP_Integer(instance,
			senderID,
			(length = sizeof(*senderID),&length),
			&integer_length1,
			NULL))
               return(EXCEPTION);
         }
         else
         {
            __ROr_sendP_2element(instance,
			__ROe_Null);
         }
         __ROr_sendP_Nelement(instance,
			thisop->encoded_representation,
			thisop->encoded_length);
         break;
      default:
         __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_BADGTCLASS);
   }
   switch (thisop->argument_header)
   {
      case __RO_OIDTREE_SETHDR:
         __ROr_sendP_2element(instance,__ROe_SET_header);
         break;
      case __RO_OIDTREE_SEQHDR:
         __ROr_sendP_2element(instance,__ROe_SEQUENCE_header);
         break;
      case __RO_OIDTREE_NOHDR:
         break;
      default:
         __RO_RETURN_RUNTIME(instance,__ROx_INTERNAL_BADGTARGHDR);
   }
   return(SUCCESS);
}

