/* $Id: multiEzca.c,v 1.39 2007/10/14 03:28:04 strauman Exp $ */ /* multi-PV EZCA calls */ /* Author: Till Straumann , 2002-2003 */ /* LICENSE: EPICS open license, see ../LICENSE file */ #include #include #include #include #include #include #include #ifdef TESTING #include #else /* Stupid m$$ has no stdint.h/inttypes.h C99 headers */ #include #endif #if defined(WIN32) || defined(_WIN32) #include #include #define snprintf epicsSnprintf #define vsnprintf epicsVsnprintf #define isnan _isnan #else #include /* _POSIX_TIMERS */ #endif #include /* fortran/C name conversion for scilab */ /* CA includes */ #include #include #include #include #include #include #define epicsExportSharedSymbols #include "shareLib.h" #include "multiEzca.h" #include "lcaError.h" #ifndef NAN #if defined(WIN32) || defined(_WIN32) static unsigned long mynan[2] = { 0xffffffff, 0x7fffffff }; #define NAN (*(double*)mynan) #else #define NAN (0./0.) #endif #endif /* GLOBAL VARIABLES */ static int ezcaSeverityWarnLevel = INVALID_ALARM; static int ezcaSeverityRejectLevel = INVALID_ALARM; /* FWD DECLS */ static char * my_strdup(const char *str); #undef TESTING /* ezca needs a bugfix for correctly reading Nelem in grouped mode */ #define EZCA_BUG_FIXED 1 /* patch applied */ #if EZCA_BUG_FIXED #define EZCA_START_NELEM_GROUP() ezcaStartGroup() #define EZCA_END_NELEM_GROUP(m,pe) do_end_group(0, m, pe) #else /* EZCA Bug: ezcaGetNelem() doesn't work in grouped mode :-( */ #define EZCA_START_NELEM_GROUP() (0) #define EZCA_END_NELEM_GROUP(m,pe) (0) #endif /* in TESTING mode, we fake a couple of ezca routines for debugging * conversion and packing issues */ #ifdef TESTING static int ezcaPut(char *name, char type, int nelms, void *bufp) { int j; printf("%30s:",name); for (j=0; j3) { printf("Invalid index\n"); return -1; } for ( i=0; i<3; i++) switch ( t ) { case 'S': if ((short)0xdead == tstSht[3*idx+i]) { *pn = i; return 0; } break; case 'L': if (0xdead == tstLng[3*idx+i]) { *pn = i; return 0; } break; case 'F': if (isnan(tstFlt[3*idx+i])) { *pn = i; return 0; } break; case 'D': if (isnan(tstDbl[3*idx+i])) { *pn = i; return 0; } break; case 'C': if (!tstChr[3*idx+i]) { *pn = i; return 0; } break; default: printf("Invalid fake name \n"); return -1; } *pn = 3; return 0; } static int ezcaGetWithStatus(char *name, char type, int nelms, void *bufp, TS_STAMP *pts, short *st, short *se) { unsigned idx,i; while ( !isdigit(*name) && *name ) name++; if (1!=sscanf(name,"%i",&idx) || idx >3) { printf("Invalid index\n"); return -1; } if ( nelms < 0 || nelms > 3 ) { printf("Invalid nelms\n"); return -1; } for (i=0; ierrs, &nrcs); assert(nrcs == m); if ( EZCA_OK != rval && dims ) { for ( i=0; ierrs; pe->errs = lcaMalloc(sizeof(*pe->errs)*m); memcpy(pe->errs, tmp); ezcaFree(tmp); } #endif if ( EZCA_OK == rval ) { ezcaFree(pe->errs); pe->errs = 0; } if ( pe->errs ) pe->nerrs = m; } else { rval = ezcaEndGroup(); } return rval; } /* our 'strdup' implementation (*IMPORTANT*, in order for the matlab implementation using mxMalloc!!) * also, strdup is not POSIX... */ static char * my_strdup(const char *str) { char *rval = 0; /* under matlab, 'malloc' will be replaced by mxMalloc by means of macro uglyness */ if ( str && (rval = lcaMalloc(sizeof(*rval) * (strlen(str) + 1) )) ) { strcpy(rval, str); } return rval; } #if !BASE_IS_MIN_VERSION(3,14,0) #if !defined(_POSIX_TIMERS) /* compiler flags may need to define -D_POSIX_C_SOURCE=199506L */ struct timespec { time_t tv_sec; /* seconds since some epoch */ long tv_nsec; /* nanoseconds within the second */ }; #endif /* provide bogus epicsTimeToTimespec */ static void epicsTimeToTimespec(struct timespec *tspec, TS_STAMP *tstamp) { mexPrintf("WARNING: epicsTimeToTimespec not implemented\n"); tspec->tv_sec = 0; tspec->tv_nsec = 0; } #endif /* convert timestamps into complex array */ void epicsShareAPI multi_ezca_ts_cvt(int m, TS_STAMP *pts, double *pre, double *pim) { int i; struct timespec ts; for ( i=0; ierr = rc; ezcaGetErrorString(nm,&msg); if (msg) { #if 0 /* message is already reported in *pe; don't print twice */ /* long strings passed to sciprint crash * scilab :-(, so we break them up... */ for ( b=msg, l=strlen(msg); l > CHUNK; b+=CHUNK, l-=CHUNK ) { char ch = b[CHUNK]; b[CHUNK]=0; mexPrintf(b); b[CHUNK]=ch; } mexPrintf(b); #endif if ( pe ) { /* Look for first error */ for ( b=msg; (nl = strchr(b,'\n')); ) { /* if no "OK\n" is there, or it is found after the newline * then an error message is there first */ if ( (ok = strstr(b,"OK\n")) && ok < nl ) { b = ok + 3; } else { break; } } l = sizeof(pe->msg) - 1; if ( nl ) { if ( l > (nl-b) ) l = nl-b; } strncpy(pe->msg, b, l); pe->msg[l] = 0; } } else { if ( pe ) strcpy(pe->msg,"Unknown Error\n"); } ezcaFree(msg); } static void ezErr1(int rc, char *msg, LcaError *pe) { lcaSetError(pe, rc, msg); } /* determine the size of an ezcaXXX type */ static int typesize(char type) { switch (type) { case ezcaByte: return sizeof(epicsInt8); case ezcaShort: return sizeof(epicsInt16); case ezcaLong: return sizeof(epicsInt32); case ezcaFloat: return sizeof(float); case ezcaDouble: return sizeof(double); case ezcaString: return sizeof(dbr_string_t); default: break; } assert(!"must never get here"); return -1; } static char nativeType(char *pv, int acceptString, int acceptNotConn) { chid *pid; if ( EZCA_OK == ezcaPvToChid( pv, &pid ) && pid ) { switch( ca_field_type(*pid) ) { case TYPENOTCONN: if ( !acceptNotConn ) return -1; /* else fall through and default to FLOAT */ default: break; case DBF_CHAR: return ezcaByte; /* case DBF_INT: */ case DBF_SHORT: return ezcaShort; case DBF_LONG: return ezcaLong; case DBF_FLOAT: return ezcaFloat; case DBF_DOUBLE: return ezcaDouble; case DBF_STRING: case DBF_ENUM: if ( acceptString ) return ezcaString; break; } } return ezcaFloat; } int epicsShareAPI multi_ezca_get_nelem(char **nms, int m, int *dims, LcaError *pe) { int i, rc; EZCA_START_NELEM_GROUP(); for ( i=0; i 1 ? i : 0); \ for ( ; j typesz ) { typesz = tmp; } } rowsize = n * typesz; /* get buffers; since we do asynchronous processing and we possibly * use different native types for different rows, we * need to buffer the full array - we cannot do it row-wise */ if ( !(cbuf = lcaMalloc( m * rowsize )) ) { ezErr1( EZCA_FAILEDMALLOC, "multi_ezca_put: not enough memory", pe); goto cleanup; } /* transpose and convert */ for ( i=0, bufp = cbuf; i= sizeof(dbr_string_t) ) { \ ezErr1(EZCA_FAILEDMALLOC,"string too long", pe);\ goto cleanup; \ } else \ strcpy(&(*cpt)[0],*fpt) ); break; } dims[i] = j; } ezcaStartGroup(); for ( i=0, bufp = cbuf; i 0 && dims[i] > nreq ) dims[i] = nreq; if ( dims[i] > n ) n = dims[i]; /* nativeType uses ezcaPvToChid() which is non-groupable */ if ( ezcaString == (types[i] = ezcaNative == *type ? nativeType(nms[i], 1, 1) : *type) ) nstrings++; if ( (tmp = typesize(types[i])) > typesz ) typesz = tmp; } if ( nstrings ) { if ( nstrings !=m ) { ezErr1( EZCA_INVALIDARG, "multi_ezca_get: type mismatch native 'string/enum' PVs cannot be\n" "mixed with numericals -- use 'char' type to enforce conversion\n", pe); goto cleanup; } else { *type = ezcaString; } } rowsize = n * typesz; if ( !(cbuf = lcaMalloc( m * rowsize )) || !(stat = lcaCalloc( m, sizeof(*stat))) || !(ts = lcaMalloc( m * sizeof(TS_STAMP))) || !(sevr = lcaMalloc( m * sizeof(*sevr))) ) { ezErr1( EZCA_FAILEDMALLOC, "multi_ezca_get: not enough memory", pe); goto cleanup; } /* get the values along with status */ ezcaStartGroup(); for ( i=0, bufp=cbuf; i= ezcaSeverityWarnLevel ) mexPrintf("Warning: PV (%s) with alarm status: %s (severity %s)\n", nms[i], alarmStatusString[stat[i]], alarmSeverityString[sevr[i]]); /* refuse to return an invalid VAL field */ if ( sevr[i] >= ezcaSeverityRejectLevel && ( !(dotp=strrchr(nms[i],'.')) || !strcmp(dotp, ".VAL") ) ) dims[i] = 0; } /* allocate the target buffer */ if ( ezcaString == *type ) /* Scilab expects NULL terminated char** list */ fbuf = lcaCalloc( m*n+1, sizeof(char*) ); else fbuf = lcaMalloc( m*n * sizeof(double) ); if ( !fbuf ) { ezErr1( EZCA_FAILEDMALLOC, "multi_ezca_get: not enough memory", pe); goto cleanup; } /* transpose and convert */ for ( i=0, bufp = cbuf; i=dims[i] ? NAN : *cpt ); break; case ezcaShort: CVTVEC( double, (0), epicsInt16, *fpt= j>=dims[i] ? NAN : *cpt ); break; case ezcaLong : CVTVEC( double, (0), epicsInt32, *fpt= j>=dims[i] ? NAN : *cpt ); break; case ezcaFloat: CVTVEC( double, (0), float, *fpt= j>=dims[i] ? NAN : *cpt ); break; case ezcaDouble: CVTVEC( double, (0), double, *fpt= j>=dims[i] ? NAN : *cpt ); break; case ezcaString: CVTVEC( char *, (0), dbr_string_t, do { \ if ( !(*fpt=my_strdup(j>=dims[i] ? "" : &(*cpt)[0])) ) { \ ezErr1(EZCA_FAILEDMALLOC, "strdup: no memory", pe); \ goto cleanup; \ } \ } while (0) ); break; } dims[i] = j; } *pres = fbuf; fbuf = 0; *pts = ts; ts = 0; *pn = n; rval = m; cleanup: if ( fbuf && ezcaString == *type ) { for ( i=0; i= 10 ) { level-=10; mexPrintf("Setting severity REJECTION level to %i\n", level); ezcaSeverityRejectLevel = level; } else { ezcaSeverityWarnLevel = level; } } int epicsShareAPI multi_ezca_clear_channels(char **nms, int m, LcaError *pe) { int rval = EZCA_OK, i, r = EZCA_OK; if ( !nms ) { if ( (rval = ezcaPurge( m<0 ? 0 : 1 )) ) ezErr(rval, "multi_ezca_clear_channels - ", pe); } else { for ( i=0; ierr = 0; pe->msg[0] = 0; pe->nerrs = 0; pe->errs = 0; } static LcaError theLastError = {0, {0}, 0, 0}; void epicsShareAPI lcaSaveLastError(LcaError *pe) { ezcaFree(theLastError.errs); memcpy(&theLastError, pe, sizeof(*pe)); /* we took over the (optional) error vector */ pe->errs = 0; pe->nerrs = 0; } LcaError * epicsShareAPI lcaGetLastError() { return &theLastError; } void epicsShareAPI lcaSetError(LcaError *pe, int err, char *fmt, ...) { va_list ap; if ( !pe ) return; pe->err = err; va_start(ap, fmt); vsnprintf(pe->msg, sizeof(pe->msg), fmt, ap); va_end(ap); pe->msg[sizeof(pe->msg)-1] = 0; }