/*============================================================================= Name: cacUtil.c Abs: The cacUtil module contains utilities for CA clients Auth: 10 jul 2009, drogind created Rev: -----------------------------------------------------------------------------*/ #include "copyright_SLAC.h" /* SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) =============================================================================*/ #include "debugPrint.h" #ifdef DEBUG_PRINT #include int cacUtilFlag = DP_ERROR; /* Set to DP_INFO for production */ #endif /* c includes */ #include /* memcpy */ #include "cadef.h" /* channel access */ #include "dbDefs.h" #include #include #include #include /* function prototypes */ LOCAL void printChidInfo(chid chId, char *message); void connectionCallback (struct connection_handler_args args); /*============================================================================= Name: getChannel Abs: create a CA channel to the EPICS PV named Args: network_ps pointer to channel struct ffthread_e index into thread struct - can be null if channel does not belong to a thread Rem: Side: Ret: int channel access error code ==============================================================================*/ int getChannel(network_ts * network_ps, ffThread_te ffThread_e ) { int status; chid chId; /* install a connection callback handler when calling ca_create_channel */ /* in order to re-create */ if ((status = (ca_create_channel((char *)(network_ps->pvName_ca), connectionCallback, NULL, 10, &chId )) )!=ECA_NORMAL ) { DEBUGPRINT (DP_ERROR, cacUtilFlag, ("cacUtil:ca_create_channel failure, status = %d\n", status)); status = NOT_OK; errlogPrintf("CA Create Channel failure for %s.\n", (char *)(network_ps->pvName_ca)); return(status); } else { status = OKOK; network_ps->chStatus = status; network_ps->chId = chId; network_ps->chStatus = status; network_ps->ffThread_e = ffThread_e; DEBUGPRINT (DP_INFO, cacUtilFlag, ("cacUtil:created ca channel for %s\n", network_ps->pvName_ca )); } /* Note: ca_pend_io is not needed for making connections when ca_create_channel specifies a callback - * this makes the connection request asynchronous - non-blocking * After the note: even though doc says it is unnecessary, I think this ensures that the channel * is connected such that subsequent reads don't bomb... * */ /* if ((status = (ca_pend_io(1.0)))!=ECA_NORMAL ) { DEBUGPRINT (DP_ERROR, cacUtilFlag, ("cacUtil:ca_pend_io failure on connect for %s\n",network_ps->pvName_ca)); status = NOT_OK; */ /* failure, release the channel - this causes seg fault if ( (network_ps->chId) && (network_ps->chStatus==ECA_NORMAL) ) { status = ca_clear_channel(network_ps->chId); status = ca_pend_io(0.1); }*/ /* }*/ return(status); } /*============================================================================= Name: connectionCallback Abs: callback registered with ca_create_channel Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- char * name_c read only pointer to name of EPICS pv unsigned short pIndex read only primary device index unsigned short uIndex read only unit index Rem: Side: The client library will monitor the state of servers on the network and connect or reconnect with the process variable's server as it becomes available. Thus, this function does not need to track connection states. Ret: int channel access error code ==============================================================================*/ void connectionCallback (struct connection_handler_args args) { printChidInfo(args.chid,"connectionCallback"); } LOCAL void printChidInfo(chid chId, char *message) { DEBUGPRINT (DP_INFO, cacUtilFlag, ("\n%s\n",message)); DEBUGPRINT (DP_INFO, cacUtilFlag, ("pv: %s type(%d) nelements(%ld) host(%s)", ca_name(chId),ca_field_type(chId),ca_element_count(chId), ca_host_name(chId)) ); DEBUGPRINT (DP_INFO, cacUtilFlag, (" read(%d) write(%d) state(%d)\n", ca_read_access(chId),ca_write_access(chId),ca_state(chId)) ); } /*============================================================================= Name: setCaMonitor Abs: create a CA subscription to the EPICS PV named. Pass in a pointer to the global unit structure needed by the subscription callback. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- const unsigned short pIndex read only primary index const unsigned short uIndex read only uIndex index Rem: this routine will assign the subscription callback and pass in the pointer to the corresponding primCollect_as[p].unit_as[u] structure. Side: Ret: vmsstat_t vms style return code ==============================================================================*/ int setCaMonitor(network_ts * network_ps, unsigned short dataType, void * callback ) { int status = OKOK; DEBUGPRINT (DP_DEBUG, cacUtilFlag, ("LoopCAC: begin of setCaMonitor on %s\n", (char *) network_ps->pvName_ca)); if ((status = (ca_create_subscription(dataType, 1, network_ps->chId , DBE_VALUE, callback, (void *)network_ps, &(network_ps->evId))) )!=ECA_NORMAL ) { DEBUGPRINT (DP_FATAL, cacUtilFlag, ("LoopCAC:ca_create_subscription failure for %s, status = %d\n", (char *) network_ps->pvName_ca, status)); status = NOT_OK; errlogPrintf("CA Create Subscription failure for %s \n", (char *) network_ps->pvName_ca); } else { status = OKOK; DEBUGPRINT (DP_DEBUG, cacUtilFlag, ("LoopCAC: Success! ca_create_subscription on network_ps= %ld\n", (unsigned long)network_ps)); } /* if ((status = (ca_pend_io(1.0)))!=ECA_NORMAL ) { DEBUGPRINT (DP_FATAL, cacUtilFlag, ("LoopCAC:ca_pend_io failure on create_subscr for %s, status=%d\n", (char *) network_ps->pvName_ca, status)); status = NOT_OK; } else { DEBUGPRINT (DP_DEBUG, cacUtilFlag, ("LoopCAC: Success! ca_pend_io on network_ps= %ld\n", (unsigned long)network_ps)); }*/ return (status); } /*============================================================================= Name: caPendIO Abs: issus CAC's ca_pend_io for a channel. This should be called after many ca_gets or ca_puts such that the get and puts are sent in parallel, followed up with a ca_pend_io to increase performance No need to be called after a ca_create_channel if a callback is specified. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- Rem: Side: Ret: ==============================================================================*/ int caPendIO (void) { int status = OKOK; if ((status = (ca_pend_io(1.0)))!=ECA_NORMAL ) { status = NOT_OK; } else { status = OKOK; DEBUGPRINT (DP_DEBUG, cacUtilFlag, ("LoopCAC: Success for ca_pend_io\n")); } return (status); } /*============================================================================= Name: caPutShort Abs: caput for a channel. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- Rem: Side: Ret: ==============================================================================*/ int caPutShort(network_ts * network_ps, const unsigned short newValue) { int status = OKOK; chid chId; chId = network_ps->chId; enum channel_state state; state = ca_state (chId); if (state != cs_conn) { DEBUGPRINT (DP_ERROR, cacUtilFlag, ("cacUtil: caPutShort - %s is not connected; c_state = %u\n", (char *) network_ps->pvName_ca, state)); status = NOT_OK; } else { status = OKOK; if ( (status = (ca_put(DBR_SHORT, chId,(const void *)&newValue)))!=ECA_NORMAL ) { DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caPutShort failure on ca_put, status = %d\n", status)); } /* if ( (status = (ca_pend_io(1.0)))!=ECA_NORMAL ) { DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caPutShort failure on ca_put, status=%d\n", status)); }*/ } return (status); } /*============================================================================= Name: caPutDouble Abs: caput for a channel. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- Rem: Side: Ret: ==============================================================================*/ int caPutDouble(network_ts * network_ps, const double newValue) { int status = OKOK; chid chId; chId = network_ps->chId; enum channel_state state; state = ca_state (chId); if (state != cs_conn) { DEBUGPRINT (DP_ERROR, cacUtilFlag, ("cacUtil: caPutDouble - %s is not connected; c_state = %u\n", (char *) network_ps->pvName_ca, state)); status = NOT_OK; } else { if ( (status = (ca_put(DBR_DOUBLE, chId,(const void *)&newValue)))!=ECA_NORMAL ) { status = NOT_OK; DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caPutDouble failure on ca_put, status = %d\n", status)); } else status = OKOK; /* if ( (status = (ca_pend_io(1.0)))!=ECA_NORMAL ) { DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caPutDouble failure on ca_pend_io, status=%d\n", status)); }*/ } return (status); } /*============================================================================= Name: caGetShort Abs: caget for a channel. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- Rem: getch Side: Ret: ==============================================================================*/ int caGetShort (network_ts * network_ps) { int status = OKOK; chid chId; chId = network_ps->chId; enum channel_state state; state = ca_state (chId); if (state != cs_conn) { DEBUGPRINT (DP_ERROR, cacUtilFlag, ("cacUtil: caGetShort - %s is not connected; c_state = %u\n", (char *) network_ps->pvName_ca, state)); status = NOT_OK; } else { if ( (status = (ca_get(DBR_SHORT, chId, &network_ps->value_s)))!=ECA_NORMAL ) { status = NOT_OK; DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caGetShort failure on ca_get, status = %d\n", status)); } else status = OKOK; /* if ( (status = (ca_pend_io(1.0)))!=ECA_NORMAL ) { DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caGetShort failure on ca_pend_io, status=%d\n", status)); }*/ } return (status); } /*============================================================================= Name: caGetDouble Abs: caget for a channel. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- Rem: Side: Ret: ==============================================================================*/ int caGetDouble (network_ts * network_ps) { int status = OKOK; chid chId; dbr_float_t fval; chId = network_ps->chId; fval = network_ps->value_d; enum channel_state state; state = ca_state (chId); if (state != cs_conn) { DEBUGPRINT (DP_ERROR, cacUtilFlag, ("cacUtil: caGetDouble - %s is not connected; c_state = %u\n", (char *) network_ps->pvName_ca, state)); status = NOT_OK; } else { if ( (status = (ca_get(DBR_FLOAT, chId, &fval)))!=ECA_NORMAL ) { status = NOT_OK; DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caGetDouble ca_get failure for %s on ca_get, status = %d\n", (char *) network_ps->pvName_ca, status)); } else status = OKOK; /* if ( (status = (ca_pend_io(1.0)))!=ECA_NORMAL ) { DEBUGPRINT (DP_FATAL, cacUtilFlag, ("cacUtil: caGetDouble ca_pend_io failure for %s on ca_get, status = %d\n", (char *) network_ps->pvName_ca, status)); }*/ } return (status); }