/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1987, 1991 by John E. Lloyd.			    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*---------------------------------------------------------------------------
 *		
 * MODULE NAME:	setSig
 *
 * DESCRIPTION:	Functions to request that a signal be sent to the 
 *		planning task when certain motion motion conditions are met.
 * 
 * AUTHOR/DATE:	John Lloyd, Spring 1989
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

#include <signal.h>

#include "../h/rcclDefs.h"
#include "../h/errorCodes.h"

#include "../rccl/motionRequest.h"
#include "../rccl/setpoint.h"
#include "../rccl/rcclAutoAbort.h"
#include "../rccl/rcclErrorMsgs.h"

extern RCCL_GMEM *rcclGmem;	/* global memory blackboard */

setCompletedSig (mnp, signum)
MANIP *mnp;
int signum;
{
	/* RCCL primitive to request a signal the next time the
	   'completed' flag is set in 'mnp'.
	 */

	char *fxnName = "setCompletedSig()";
	MOTION_CONTROL_BLOCK *mcb = (MOTION_CONTROL_BLOCK*)mnp->mcb;

	if (!planningLevelCheck(fxnName))
	 { return (-1);
	 }
	if (signum >= NSIG || signum == SIGHUP || signum == SIGXCPU)
	 { errorMsg (ERange, rcclBadSignumMsg, fxnName, signum);
	   RETURN_ERROR(-1);
	 }
	if (signum < 0)
	 { signum = 0;
	 }
	mcb->completedSig = signum;
	
	RETURN_NORMAL (1);	
}

setStopSig (mnp, signum, mflagNum)
MANIP *mnp;
int signum;
int mflagNum;
{
	/* RCCL primitive to request a signal when the motion specified
	   by 'mflagNum' ends.
	 */

	char *fxnName = "setStopSig()";
	MOTION_CONTROL_BLOCK *mcb = (MOTION_CONTROL_BLOCK*)mnp->mcb;

	if (!planningLevelCheck(fxnName))
	 { return (-1);
	 }
	if (signum >= NSIG || signum == SIGHUP || signum == SIGXCPU)
	 { errorMsg (ERange, rcclBadSignumMsg, fxnName, signum);
	   RETURN_ERROR(-1);
	 }
	if (signum < 0)
	 { signum = 0;
	 }
	if((mflagNum = mapFlag(mflagNum)) < 0)
	 { errorMsg (EDescriptor, rcclBadMflagMsg, fxnName);
	   RETURN_ERROR (-1);
	 }
	rcclGmem->mflagBuf[mflagNum].stopSig = signum;
	if ((rcclGmem->mflagBuf[mflagNum].status & M_FINISHED) &&
	    (rcclGmem->mflagBuf[mflagNum].stopSig))
	 { kill (getpid(), signum);
	 }
	
	RETURN_NORMAL (1);	
}
