#####################################################################
# file:                st.cmd
# purpose:             EPICS Startup script for padTest software running on Interlocking PADs in Klystron test stations
#						KLYSTRON PAD RTEMS STARTUP SCRIPT at Test Station TSD05 PAD_KLY app
# created:             24-Mar-2011
# property of:         Stanford Linear Accelerator Center,
#
# revision history:
#   24-May-2011        Shantha Condamoor             initial versiion; uses autosave-restore.
#
####################################################################

# Change directory to TOP of application
pwd()
chdir("../..")

ld("bin/RTEMS-uC5282/pad-kly.obj") 
pwd()

# ---> Source envPaths
iocshCmd("< iocBoot/eioc-b044-ts05/envPaths")

## /data now mapped by pre...
nfsMountsShow()

lsmod()

## Set common environment variables
epicsEnvSet("IOC_NAME",  "TSD05")
epicsEnvSet("LOCA_NAME", "B44-KLYTSD05")

# For iocAdmin
epicsEnvSet("ENGINEER","Shantha Condamoor")
epicsEnvSet("LOCATION","B044")

## Set app-specific environment variable for the PAD
epicsEnvSet ("EPICS_CA_MAX_ARRAY_BYTES", "1000000")
#epicsEnvSet ("EPICS_CA_ADDR_LIST","134.79.219.255 134.79.216.110")
epicsEnvSet ("EPICS_IOC_LOG_INET", "134.79.216.110")
epicsEnvSet ("EPICS_IOC_LOG_PORT", "7005")

## Register all support components
dbLoadDatabase("dbd/pad.dbd")
pad_registerRecordDeviceDriver(pdbbase) 

## Load common databases
dbLoadRecords("db/iocAdminRTEMS.db", "IOC=EIOC:B044:TSD05" )
dbLoadRecords("db/iocRelease.db","IOC=EIOC:B044:TSD05")

## Load record instances
dbLoadTemplate("db/padTS05.kly_template")

## Load database for autosave/restore status pv's
dbLoadRecords("db/save_restoreStatus.db","P=EIOC:B044:TSD05:")

## ==============================================
## autosave/restore settings

save_restoreSet_status_prefix( "EIOC:B044:TSD05:")
## #save_restoreSet_IncompleteSetsOk(1)
## #save_restoreSet_DatedBackupFiles(1)

set_requestfile_path("/data/autosave-req") 
set_savefile_path("/data/autosave")

## ==============================================
## New method for autosave and restore
#  Decide what is saved by specifying it in the
#  EPICS databases
## =============================================
set_pass0_restoreFile("info_settings.sav")
set_pass1_restoreFile("info_settings.sav")

## =============================================
## Load Access Security File

# asSetFilename("iocBoot/padAccessControl.acf")

## =============================================
## Initialize/Start the EPICS Kernel
## =============================================

iocInit()

## =============================================
# optional, needed if the IOC takes a very long time to boot.
epicsThreadSleep( 5.0)

## ===========================================================
## Start autosave routines to save our data
## ===========================================================

## Handle autosave 'commands' contained in loaded databases.
# change directory to ioc startup area
chdir("/data/autosave-req")
#iocshCmd("makeAutosaveFiles()")

# Start the save task 
create_monitor_set("info_positions.req", 5, "LOCA=B044, UNIT=TSD05")
create_monitor_set("info_settings.req",30,"LOCA=B044, UNIT=TSD05")

# change directory to local mountpoint so that files don't need path to dir in their name
# chdir("/data") # for lcls-dev
# pwd()

#rtems_gdb_start(0,0)

# Start any sequence programs. Note: RTEMS Cexp syntax
# Skip state machine for nonlin test
# seq(&sncpad,"PRIM=EIOC,LOCA=B044,UNIT=TSD05,PAD=TST") 

dbpf("EIOC:B044:TSD05:TST_INT_CTRL","1")

epicsEnvShow()


# =====================================================

#####################################################################
# To use the caPutLog client in iocs running under LCLSDEV network:
# Start iocLogServer on a Unix host (example:134.79.216.110)
# after setting EPICS_IOC_LOG related env variables:
#
# > export EPICS_IOC_LOG_FILE_NAME=/afs/slac/g/lcls/epics/iocTop/padTest/test.log
# > export EPICS_IOC_LOG_PORT=7005
# > iocLogServer &
#
# Messages are logged in test.log as follows, whenever caPut pv value changes:
# eioc-b044-tsd05.slac.stanford.e Fri Mar 25 16:25:26 2011 25-Mar-11 16:25:18 cdlx02 scondam
#EIOC:B044:TS05:TST_0_OFST.VAL new=256 old=128
# eioc-b044-tsd05.slac.stanford.e Fri Mar 25 16:29:21 2011 25-Mar-11 16:29:15 cdlx02 scondam
#EIOC:B044:TS05:TST_0_OFST.VAL new=128 old=256
#
#####################################################################

# =====================================================
# Turn on caPutLogging:
# Log values only on change to the iocLogServer:
#caPutLogInit("172.27.8.31:7004")	# PROD
#caPutLogInit("134.79.216.110")		# Use the Default port of 7011

# substr=malloc(500)
# sprintf (substr, "%s:%s", getenv("EPICS_IOC_LOG_INET"), getenv("EPICS_IOC_LOG_PORT"))
# printf("%s\n",substr)
# caPutLogInit(substr)	# kly-gtf01.slac.stanford.edu - 134.79.216.110, Port 7005.
# free(substr)

# =====================================================
# If the Log server has been started successfully, the following messages should appear at the ioc shell:

# Cexp@eioc-b044-tsd05>caPutLogInit("134.79.216.110:7005")
# sevr=info caPutLog: task already running
# sevr=info caPutLog: successfully initialized
# 0x00000000 (0)


# =====================================================

# caPutLogShow(2)
