/*
 * program Camcom("DEV=116-IOCCAMAC")
 * program Camcom("LOCA=LI21")
 */
program Camcom

option -c;
option -r;

%%#include "epicsTime.h"
%%#include "epicsThread.h"
%%#include "epicsPrint.h" /* epicsPrintf    */
%%#include "string.h"     /* memcpy, memset */
%%#include "stdio.h"      /* sprintf        */

%%static char* comp_date = __DATE__; /* 12 characters long */
%%static char* comp_comment = "Version 1.0"; 

%%#define CAMCOM_FREE 0
%%#define CAMCOM_TAKEN 1
%%#define CAMCOM_HOST_DONE 2
%%#define CAMCOM_BUSY 3
%%#define CAMCOM_DONE 4

/* required macro:
 * macro used LCLS to buid standard device name CAMC:<loca>:CV01  
 * macro used by SPEAR, device name uses different pattern than LCLS 
 */
char *LOCA;
char *DEV;

/* Local Process variables */

short  last_active_token;
short  active_token;        assign active_token       to "";  string active_token_pv;  monitor active_token;
short  in_buff_len;         assign in_buff_len        to "";  string in_buff_len_pv;
char   real_buff[16348];    assign real_buff          to "";  string real_buff_pv;
char   in_buff[16348];      assign in_buff            to "";  string in_buff_pv;
short  camcom_status;       assign camcom_status      to "";  string camcom_status_pv;  monitor camcom_status;
string camcom_reservation;  assign camcom_reservation to "";  string camcom_reservation_pv;
string camcom_seq_ver;      assign camcom_seq_ver     to "";  string camcom_seq_ver_pv;

entry {

  /* fill and check macros */
   LOCA = macValueGet("LOCA");
   /* Are we using LCLS device name standard? */
   if (LOCA)
     sprintf(DEV,"CAMC:%s:CV01:",LOCA);
   else
   /* Nope, allow a more generic device name to be used -- spear */
     DEV = macValueGet("DEV");  
   printf("\nCamcom: SNL program started\n");
   printf("Camcom: using device name %s\n",DEV);

  /* setup epics process variables */
  sprintf(camcom_seq_ver_pv, "%sSEQ_VER",DEV);
  pvAssign(camcom_seq_ver, camcom_seq_ver_pv);

  sprintf(camcom_reservation_pv, "%sNOTE",DEV);
  pvAssign(camcom_reservation, camcom_reservation_pv);

  sprintf(camcom_status_pv, "%sSTATUS",DEV);
  pvAssign(camcom_status, camcom_status_pv);

  sprintf(in_buff_pv, "%sINBUFF",DEV);
  pvAssign(in_buff, in_buff_pv);

  sprintf(real_buff_pv, "%sREALBUFF",DEV);
  pvAssign(real_buff, real_buff_pv);

  sprintf(in_buff_len_pv, "%sINBUFF_LEN",DEV);
  pvAssign(in_buff_len, in_buff_len_pv);

  sprintf(active_token_pv, "%sACTIVE_TOKEN",DEV);
  pvAssign(active_token, active_token_pv);
  
  } /* entry */


ss DOcamcom {
    state init {
	when (1) {
          sprintf(camcom_seq_ver,"%s : %s",comp_date,comp_comment);
          pvPut(camcom_seq_ver);
          camcom_status=CAMCOM_FREE; pvPut(camcom_status);
          last_active_token = -1;
	} state process
    }

    state process {
        when (camcom_status==CAMCOM_TAKEN && last_active_token!=active_token) {
            last_active_token=active_token;
         } state process

        when (camcom_status==CAMCOM_FREE && last_active_token == active_token) {
            last_active_token--;
        } state process

        when (camcom_status==CAMCOM_HOST_DONE) {
            camcom_status=CAMCOM_BUSY; pvPut(camcom_status);
            pvGet(in_buff_len); pvGet(in_buff);
            pvGet(camcom_reservation);  /* Just for debug?? */
%%          memcpy((void *)&real_buff[0],(void *)&in_buff[0],2*in_buff_len);
            pvPut(real_buff, SYNC); /* Start Camac operation */
            pvGet(real_buff, SYNC); /* Get results */
%%          memcpy((void *)&in_buff[0],(void *)&real_buff[0],2*in_buff_len);
            pvPut(in_buff, SYNC);    /* Put results back to in_buff */
            camcom_status=CAMCOM_DONE; pvPut(camcom_status);
            last_active_token=-1;
        } state process
    }

}




