/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1991 by John E. Lloyd and Vincent Hayward.	    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
/*---------------------------------------------------------------------------
 *		
 * MODULE NAME:	setMonitors
 *
 * DESCRIPTION:	Contains RCCL primitives to set up permanent and
 *		per-motion monitor functions.
 * 
 * AUTHOR/DATE:	John Lloyd, McGill University, Fall 1988,
 *		after Vincent Hayward, Purdue University, 1983
 *
 * MODIFICATION HISTORY:
 *
 *--------------------------------------------------------------------------*/

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

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

runMotionFxn (mnp, function, arg)
MANIP *mnp;
int (*function)();
int arg;
{
	/* Request a monitor function to be executed for the
	   duration of the next motion request only.
	 */

	MOTION_VALUE_BLOCK *mvb = (MOTION_VALUE_BLOCK *)mnp->mvb;
	char *fxnName = "runMotionFxn()";
	int k;
	
	if (!planningLevelCheck(fxnName))
	 { return (-1);
	 }
	if ((k = mvb->numMotionFxns) >= MAX_MONITOR_FXNS)
	 { errorMsg (ETooMany, rcclTooManyMsg, 
			       fxnName, "functions", MAX_MONITOR_FXNS);
	   RETURN_ERROR(-1);
	 }
	mvb->motionFxns[k].fxn = function;
	mvb->motionFxns[k].arg = arg;
	mvb->numMotionFxns++;
	RETURN_NORMAL(1);
}

runMonitorFxn (mnp, function, arg)
MANIP *mnp;
int (*function)();
int arg;
{
	/* Set a permanent monitor function running and get back a
	   handle with which it may be later deleted.
	*/

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

	if (!planningLevelCheck(fxnName))
	 { return (-1);
	 }
	if (!rcclActive())
	 { errorMsg (ETaskActive, rcclNotActiveMsg, fxnName);
	   RETURN_ERROR(-1);
	 }
	if (mcb->monitorFxnsActive >= MAX_MONITOR_FXNS)
	 { errorMsg (ETooMany, rcclTooManyMsg,
			       fxnName, "functions", MAX_MONITOR_FXNS);
	   RETURN_ERROR(-1);
	 }
	mcb->monitorFxn = function;
	mcb->monitorArg = arg;
	mcb->monitorFxnCmd = RUN_MONITOR_FXN;
	waitfor (mcb->monitorFxnCmd == 0);
	RETURN_NORMAL(mcb->monitorRunId);
}

deleteMonitorFxn (mnp, id)
MANIP *mnp;
int id;
{
	/* Delete a permanent monitor function */

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

	if (!planningLevelCheck(fxnName))
	 { return (-1);
	 }
	if (!rcclActive())
	 { errorMsg (ETaskActive, rcclNotActiveMsg, fxnName);
	   RETURN_ERROR(-1);
	 }
	mcb->monitorDeleteId = id;
	mcb->monitorFxnCmd = DELETE_MONITOR_FXN;
	waitfor (mcb->monitorFxnCmd == 0);
	RETURN_NORMAL(1);
}
