/* $Id: devWfPatternDbgBuf.c,v 1.1 2011/04/12 18:54:44 strauman Exp $ */ #include "patternDbgBuf.h" #undef __STRICT_ANSI__ #include #include #include #include #include #include #include #include #include static void my_cantProceed(const char *fmt, ...) { va_list ap; char buf[512]; va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); cantProceed(buf); } /* just some pointer */ #define REG_ID patternDbgBufPutBytes int patternDbgBufPutBytes(PatternDbgBuf buf, void *data, int nelm, epicsTimeStamp *sync) { int nbytes, ntrans, sz; ntrans = buf->nelm - buf->nord; if ( ntrans < nelm ) nelm = ntrans; if ( nelm > 0 ) { if ( 0 == buf->nord ) { /* Wait for synchronization */ if ( !sync ) return 0; /* Tag record time with time we begin */ buf->wf_p->time = *sync; } sz = dbValueSize( buf->wf_p->ftvl ); nbytes = nelm * sz; memcpy( buf->wf_p->bptr + buf->nord*sz, data, nbytes ); buf->nord = buf->wf_p->nord = buf->nord + nelm; if ( buf->nord == buf->wf_p->nelm ) callbackRequestProcessCallback( &buf->cb, buf->wf_p->prio, buf->wf_p ); } return nelm; } static void create_buf(void *arg) { PatternDbgBuf buf; const char *name = arg; /* Name already present */ if ( registryFind( REG_ID, name ) ) { my_cantProceed("devWfPatternDbgBuf: name %s already created by someone else!\n", name); } buf = mallocMustSucceed( sizeof(*buf), "devWfPatternDbgBuf: no memory\n"); buf->nord = 0; buf->nelm = 0; buf->name = name; buf->wf_p = 0; if ( ! registryAdd( REG_ID, buf->name, buf ) ) my_cantProceed("devWfPatternDbgBuf: Unable to create registry entry\n"); } PatternDbgBuf patternDbgBufGet(char *name) { static epicsThreadOnceId id = EPICS_THREAD_ONCE_INIT; if ( !name ) return 0; epicsThreadOnce( &id, create_buf, name ); return registryFind( REG_ID, name ); } static long init_record(waveformRecord *wf_p) { PatternDbgBuf buf; char *name = wf_p->inp.value.vmeio.parm; int key; if ( VME_IO != wf_p->inp.type ) { my_cantProceed("devWfPatternDbg: %s has an invalid INP field (VME_IO required)\n", wf_p->name); } if ( ! name || ! *name ) { my_cantProceed("devWfPatternDbg: %s has an invalid INP field ('parm' required)\n", wf_p->name); } buf = patternDbgBufGet( name ); if ( !buf ) { my_cantProceed("devWfPatternDbgBuf: init_record(%s) unable to locate buffer %s\n", wf_p->name, name ); } if ( buf->wf_p ) { my_cantProceed("devWfPatternDbgBuf: init_record(%s) buffer %s has already a waveform attached (%s)\n", wf_p->name, name, buf->wf_p->name ); } wf_p->dpvt = buf; key = epicsInterruptLock(); buf->nord = wf_p->nelm; buf->nelm = wf_p->nelm; buf->wf_p = wf_p; epicsInterruptUnlock( key ); return 0; } static long read_wf(waveformRecord *wf_p) { PatternDbgBuf buf = wf_p->dpvt; if ( wf_p->rarm ) { if ( ! wf_p->pact ) { /* Phase 1 */ wf_p->pact = TRUE; /* Assume buf->nord is written atomically; use no lock */ buf->nord = wf_p->nord = 0; } else { /* Phase 2 */ wf_p->nord = buf->nord; wf_p->rarm = 0; } } return 0; } static struct DSET { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record; DEVSUPFUN get_ioint_info; DEVSUPFUN read_wf; } devWfPatternDbgBuf = { 5, NULL, NULL, init_record, NULL, read_wf }; epicsExportAddress(dset, devWfPatternDbgBuf);