/*-=-=-=-=-=-=-=-=-=-=-=- RCCL/RCI Notice of Copyright -=-=-=-=-=-=-=-=-=-=-*
 *									    *
 *	Copyright (C) 1991 by John E. Lloyd.				    *
 *									    *
 *	Information on copyright permissions, as well as a complete	    *
 *	disclaimer of warranty, is given in the file COPYRIGHT.		    *
 *									    *
 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/
#ifdef VXWORKS_CFG

#include <vxWorks.h>
#include <signal.h>
#include <varargs.h>

#include <sys/time.h>       
#include "../h/cdefs.h"
#include "../h/errorStack.h"
#include "../h/rciTask.h"
#include "vxwCtrlTask.h"

extern int rciCpu;
extern int rciPlanTaskId;
extern int rciCtrlTaskId;

rciInKernelMode()
{
	/* give the equivalent information: are we in the
	   planTask or ctrlTask context? */

	return (taskIdSelf() == rciCtrlTaskId);
}

rciSignal(num)
int num;
{
	if (num <= 0 || num >= NUM_SIGNALS)
	 { return (-1);
	 }
	kill (rciPlanTaskId, num);
}

rciPrintToTerminal(set)
int set;
{
	/* printing always goes to the console */
	return (0);
}

#define MAX_PMSG	512

rciPrintf (va_alist)
va_dcl
{
	char buf[MAX_PMSG+1];
	va_list ap;
	char *fmt;

	va_start(ap);
	fmt = va_arg(ap, char*);
	vsnprintf(buf, MAX_PMSG, fmt, ap);
	va_end(ap);
	printf ("%s", buf);
}

rciKPrintf (va_alist)
va_dcl
{
	/* turns out to be indentical to printf */

	char buf[MAX_PMSG+1];
	va_list ap;
	char *fmt;

	va_start(ap);
	fmt = va_arg(ap, char*);
	vsnprintf(buf, MAX_PMSG, fmt, ap);
	va_end(ap);
	printf("%s", buf);
}

rciBadAddr (address, size)
void *address;
int size;
{
	char rbuf[4];

	if (size != 1 && size != 2 && size != 4)
	 { return (-1);
	 }
	if (vxMemProbe (address, READ, size, rbuf) == OK)
	 { return (0);
	 }
	else
	 { return (1);
	 }
}

struct timeval rciWallClock()
{
	struct timeval tv;
	
	/* wall clock not supported right now */

	tv.tv_sec = tv.tv_usec = 0;
}

rciSetTrace(c)
int c;
{
	/* set trace functions not supported right now */
	return (-1);
}

rciClearTrace(c)
int c;
{
	/* set trace functions not supported right now */
	return (-1);
}

void *rciMalloc (size)
int size;
{
	return ((char*)malloc(size));
}

rciFree (mem)
void *mem;
{
	free(mem);
}

/* These functions are used internally ... */

rciSignalAux (cpu, sig)
int cpu;
int sig;
{
	/* not implemented right now */
}

rciSchedAst (clock, when, fxn, arg)
int clock;
int when;
int (*fxn)();
int arg;
{
	extern EXEC_REQ timerExr;
	extern int timerReqCount;
	int s;

	/* clock is assumed to be ARBITER_CPU in this implementation */

	s = VXINT_LOCK();

	if (fxn)
	 { timerReqCount = when;
	   timerExr.fxn = fxn;
	   timerExr.arg = arg;
	 }
	else
	 { timerReqCount = UNDEF;
	   timerExr.fxn = 0;
	 }
	timerExr.request = 0;

	VXINT_UNLOCK(s);
	return(1);
}

#endif
