/*************************************************************************\
* Copyright (c) 2002 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 2002 The Regents of the University of California, as
* Operator of Los Alamos National Laboratory.
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution. 
\*************************************************************************/

/*
 * $Log: not supported by cvs2svn $
 * Revision 1.1  1999/06/10 19:43:10  soliday
 * Moved to oag tree.
 *
 * Revision 1.1  1998/05/26 15:48:59  borland
 * First version.  Not actually used because we have routines written in
 * Tcl that provide the same functions.  However, the code here does work.
 *
 */

#include <stdlib.h>
#include <errno.h>
#include <tclRunControl.h>
#include <cadef.h>
#include <ezca.h>
#include <libruncontrol.h>

/*****************************/
/* Local Function Prototypes */
/*****************************/


/* Function to initialize a run control record, 
 * giving the process control over the record if it is
 * available.
 */
 
int tclRunControlInitRec(Tcl_Interp *interp, int argc, char *argv[]) 
{
  char *pvName, *description;
  float timeout;
  char handle[256];
  
  if (argc != 4) {
    Tcl_SetResult(interp, "usage: runControl init <pvName> <description> <timeout(msec)>\nreturns TCL_OK with run-control handle, or TCL_ERROR with RUNCONTROL_DENIED, RUNCONTROL_ERROR, RUNCONTOL_BADARGS",NULL);
    return TCL_ERROR;    
  }

  pvName = argv[1];
  description = argv[2];
  if (strlen(description)>39)
    description[39] = 0;
  if (sscanf(argv[3], "%f", &timeout)!=1) {
    Tcl_SetResult(interp, "RUNCONTROL_BADARGS", NULL);
    return TCL_ERROR;
  }
  
  switch (runControlInit(pvName, description, timeout, handle)) {
  case RUNCONTROL_OK:
    Tcl_SetResult(interp, handle, NULL);
    return TCL_OK;
  case RUNCONTROL_DENIED:
    Tcl_SetResult(interp, "RUNCONTROL_DENIED", NULL);
    return TCL_ERROR;
  case RUNCONTROL_ERROR:
  default:
    Tcl_SetResult(interp, "RUNCONTROL_ERROR", NULL);
    return TCL_ERROR;
  }
}

/* Pings a previously-initialized run-control record, using the
 * handle returned by tclRunControlInitRec 
 */

int tclRunControlPing(Tcl_Interp *interp, int argc, char *argv[]) 
{
  char *handle;
  
  if (argc != 2) {
    Tcl_SetResult(interp, "usage: runControl ping <handle>\nreturns TCL_OK,  or TCL_ERROR with RUNCONTROL_ABORT, RUNCONTROL_TIMEOUT, RUNCONTROL_ERROR, or RUNCONTOL_BADARGS",NULL);
    return TCL_ERROR;    
  }

  handle = argv[1];
  if (!strlen(handle)) {
    Tcl_SetResult(interp, "RUNCONTROL_BADARGS", NULL);
    return TCL_ERROR;
  }
  
  switch (runControlPing(handle)) {
  case RUNCONTROL_OK:
    Tcl_SetResult(interp, handle, NULL);
    return TCL_OK;
  case RUNCONTROL_ABORT:
    Tcl_SetResult(interp, "RUNCONTROL_ABORT", NULL);
    return TCL_ERROR;
  case RUNCONTROL_TIMEOUT:
    Tcl_SetResult(interp, "RUNCONTROL_TIMEOUT", NULL);
    return TCL_ERROR;
  case RUNCONTROL_ERROR:
  default:
    Tcl_SetResult(interp, "RUNCONTROL_ERROR", NULL);
    return TCL_ERROR;
  }
}


/* Releases a previously-initialized run-control record, using the
 * handle returned by tclRunControlInitRec 
 */

int tclRunControlExit(Tcl_Interp *interp, int argc, char *argv[]) 
{
  char *handle;
  
  if (argc != 2) {
    Tcl_SetResult(interp, "usage: runControl exit <handle>\nreturns TCL_OK,  or TCL_ERROR with RUNCONTROL_ERROR or RUNCONTOL_BADARGS",NULL);
    return TCL_ERROR;    
  }

  handle = argv[1];
  if (!strlen(handle)) {
    Tcl_SetResult(interp, "RUNCONTROL_BADARGS", NULL);
    return TCL_ERROR;
  }
  
  switch (runControlExit(handle)) {
  case RUNCONTROL_OK:
    Tcl_SetResult(interp, handle, NULL);
    return TCL_OK;
  case RUNCONTROL_ERROR:
  default:
    Tcl_SetResult(interp, "RUNCONTROL_ERROR", NULL);
    return TCL_ERROR;
  }
}


/* Logs a message with previously-initialized run-control record, using the
 * handle returned by tclRunControlInitRec 
 */

#include <alarm.h>
/* strictly, I should get this from <alarmString.h>, but the 
 * version there is missing the "static", so if I include it
 * here I'll get a linker error because another library also
 * includes it.
 */

static char * alarmSeverityString[]={
	"NO_ALARM",
	"MINOR",
	"MAJOR",
	"INVALID"
	};

int tclRunControlLogMsg(Tcl_Interp *interp, int argc, char *argv[]) 
{
  char *handle, *message, *severityString;
  short severity;
  
  if (argc != 4) {
    Tcl_SetResult(interp, "usage: runControl logMsg <handle> <message> <severity>\nreturns TCL_OK,  or TCL_ERROR with RUNCONTROL_ABORT, RUNCONTROL_TIMEOUT, RUNCONTROL_ERROR, or RUNCONTOL_BADARGS",NULL);
    return TCL_ERROR;    
  }

  handle = argv[1];
  if (!strlen(handle)) {
    Tcl_SetResult(interp, "RUNCONTROL_BADARGS", NULL);
    return TCL_ERROR;
  }
  message = argv[2];
  if (strlen(message)>39)
    message[39] = 0;
  severityString = argv[3];
  for (severity=0; severity<ALARM_NSEV; severity++) {
    if (strcmp(severityString, alarmSeverityString[severity])==0)
      break;
  }
  if (severity==ALARM_NSEV) {
    Tcl_SetResult(interp, "RUNCONTROL_BADARGS", NULL);
    return TCL_ERROR;
  }
  
  switch (runControlLogMessage(handle, message, severity)) {
  case RUNCONTROL_OK:
    Tcl_SetResult(interp, handle, NULL);
    return TCL_OK;
  case RUNCONTROL_ERROR:
  default:
    Tcl_SetResult(interp, "RUNCONTROL_ERROR", NULL);
    return TCL_ERROR;
  }
}



/******************************************
 * Provided for tcl to initialize the run control extension.
 * Does nothing at present.
 *****************************************/

int tclRunControlInit(Tcl_Interp *interp) {
  return TCL_OK;
}


