/* These functions return JLS and KYN structures that can be accessed
   ny an RCI task. */

#include "../h/rciTask.h"
#include "../h/errorCodes.h"
#include "../rci/loadRobot.h"
#include "../h/robot_kynvar.h"

extern RCI_RBT *rciRbtDescList;
extern int rciTaskId;

extern ROBOT_DESC *createRobotDesc();

/* ----- Error Messages. Defined externally to save space ----- */

static char *nullMsg	      = "%s --\n";
static char *robotNotSetupMsg = "%s -- robot %s not set up yet\n";

JLS *rciGetJls(name)
const char *name;
{
	char *fxnName = "rciGetJls()";
	ROBOT_DESC *rdesc;
	RCI_RBT *rbt;

	/* search the rbtDescList for name */
	for (rbt=rciRbtDescList; rbt; rbt=rbt->next)
	 { if (strcmp(rbt->name, name) == 0)
	    { break;
	    }
	 }
	if (rbt)
	 { return (rbt->jls);
	 }
	if (rciTaskId != 0)
	 { errorMsg (ENotDefined, robotNotSetupMsg, fxnName);
	   return (NULL);
	 }
	else
	 { if ((rdesc = createRobotDesc(name)) == NULL)
	    { errorMsg (getErrorCode(0), nullMsg, fxnName);
	      return (NULL);
	    }
	   else
	    { return (rdesc->jls);
	    }
	 }
}

KYN *rciGetKyn(name, sizep)
const char *name;
int *sizep;
{
	char *fxnName = "rciGetKyn()";
	ROBOT_DESC *rdesc;
	RCI_RBT *rbt;

	/* search the rbtDescList for name */
	for (rbt=rciRbtDescList; rbt; rbt=rbt->next)
	 { if (strcmp(rbt->name, name) == 0)
	    { break;
	    }
	 }
	if (rbt)
	 { if (sizep)
	    { *sizep = rbt->kynSize;
	    }
	   return (rbt->kyn);
	 }
	if (rciTaskId != 0)
	 { errorMsg (ENotDefined, robotNotSetupMsg, fxnName);
	   return (NULL);
	 }
	else
	 { if ((rdesc = createRobotDesc(name)) == NULL)
	    { errorMsg (getErrorCode(0), nullMsg, fxnName);
	      return (NULL);
	    }
	   else
	    { if (sizep)
	       { *sizep = rdesc->kynSize;
	       }
	      return (rdesc->kyn);
	    }
	 }
}
