#include #include #include #include #include #include #include #include #include #include #include #include #include #undef __STRICT_ANSI__ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "blmMacros.h" #include "drvPadUdpComm.h" #include "fcomBlm.h" #define NUM_DATA_KINDS 3 /* We calculate single-precision for now */ typedef float BpmNumber; /* Buffer the last couple of packets received */ static UdpCommPkt lastPkt[MAX_BPM][NUM_DATA_KINDS] = { {0, 0, 0} }; typedef uint32_t TimeStamp; #ifdef __PPC__ uint32_t roundtripMax = 0; uint32_t roundtripAvg = 0; uint32_t totTimeMax = 0; uint32_t totTimeAvg = 0; #endif uint32_t drvPadUdpCommDebug = 0; int numPadReplies = 0; /* 5/23/8 new */ int numPadFrees = 0; /* 5/23/8 new */ int numPadSaves = 0; /* 5/23/8 new */ /* Error stati can be ORed */ #define BPM_AMPL_OK 0 /* Overrange detected */ #define BPM_AMPL_OVR_GRN (1<<0) #define BPM_AMPL_OVR_RED (1<<1) #define BPM_AMPL_OVR_BLU (1<<2) #define BPM_AMPL_OVR_YLO (1<<3) #define BPM_AMPL_OVR_MSK (0xf<<0) /* Underrange detected */ #define BPM_AMPL_UDR_GRN (1<<4) #define BPM_AMPL_UDR_RED (1<<5) #define BPM_AMPL_UDR_BLU (1<<6) #define BPM_AMPL_UDR_YLO (1<<7) #define BPM_AMPL_UDR_MSK (0xf<<4) #define BPM_AMPL_RNG_MSK(chnl) \ ( ~(BPM_AMPL_OVR_MSK|BPM_AMPL_UDR_MSK) \ | (chnl >=0 ? (BPM_AMPL_OVR_GRN<<(chnl)) : 0) \ | (chnl >=0 ? (BPM_AMPL_UDR_GRN<<(chnl)) : 0) ) #define BPM_AMPL_TIMEDOUT (1<<8) #define BPM_AMPL_INVALID (1<<9) volatile uint32_t drvPadUdpCommBpmInUse = 0; struct { PadRequestRec req; PadStrmCommandRec cmd[MAX_BPM]; } padStrmCmd; static epicsMutexId padStrmCmdLock; /* This is actually optimized away by gcc */ static int isbe() { union { uint8_t b[2]; uint16_t test; } endian = { b: {0xbe, 00 } }; return endian.test == 0xbe00; } static void strmCmdInit() { int i; /* 7/25/8 why doesn't this know channel? */ epicsPrintf("drvPadUdpComm strmCmdInit: port %d \n", drvPadUdpCommPort + 1); padStrmCmd.req.version = PADPROTO_VERSION2; padStrmCmd.req.nCmds = MAX_BPM; padStrmCmd.req.cmdSize = sizeof(padStrmCmd.cmd[0]); for ( i=0; i= MAX_BPM ) return -1; key = epicsInterruptLock(); drvPadUdpCommBpmInUse &= ~(1<= MAX_BPM ) return -1; epicsMutexLock(padStrmCmdLock); padStrmCmd.cmd[channel].type = PADCMD_STRM | PADCMD_QUIET; scmd = padStrmCmd.cmd[channel]; epicsMutexUnlock(padStrmCmdLock); epicsTimeGetCurrent(&ts); /* 8/4/8: add "| PADCMD_QUIET" in to avoid the timeout check in padRequest */ rval = padRequest(drvPadUdpCommSd, channel, PADCMD_STRM | PADCMD_QUIET, drvPadUdpCommGetXid(), ts.secPastEpoch, ts.nsec, &scmd, 0, drvPadUdpCommTimeout); epicsPrintf("padRequest returns %d\n", rval); key = epicsInterruptLock(); drvPadUdpCommBpmInUse |= (1<= MAX_BPM || kind < PadDataBpm || kind > PadDataCalGrn ) return p; key = epicsInterruptLock(); oldp = lastPkt[slot][kind]; lastPkt[slot][kind] = p; epicsInterruptUnlock(key); return oldp; } /* may return 0 if no data is * available (=> READ_ALARM) * * If a non-null item is returned, * the user must free it with * padDataFree(); */ void * padDataGet(unsigned slot, PadDataKind kind) { return padDataSwap(slot, kind, 0); } PadReply padReplyFind(void *data_p) { numPadReplies++; /* 5/23/8 new */ return data_p ? (PadReply)udpCommBufPtr(data_p) : 0; } void padDataFree(void *item) { if(item) { numPadFrees++; udpCommFreePacket(item); } } static int padDataSave(unsigned slot, PadDataKind kind, UdpCommPkt p) { int rval; UdpCommPkt oldp; if ( !p ) return -1; numPadSaves++; oldp = padDataSwap(slot, kind, p); /* failure -- invalid arguments */ rval = ( oldp == p ) ? -1 : 0; padDataFree(oldp); return rval; } static long padUdpCommReport(int level) { epicsPrintf("Udp Communication with PAD\n"); epicsPrintf("Udp num PAD replies = %d\n", numPadReplies); epicsPrintf("Udp num PAD frees = %d\n", numPadFrees); epicsPrintf("Udp num PAD saves = %d\n", numPadSaves); return 0; } uint32_t drvPadUdpCommGetXid() { static volatile uint32_t xid = 0; uint32_t rval; int key; key = epicsInterruptLock(); rval = xid++; epicsInterruptUnlock(key); return rval; } int drvPadUdpCommGetNsamples() { /*return 1;*/ return 2; } int drvPadUdpCommSendStartCmd(int nsamples, uint32_t xid, int channel) { PadStrmCommandRec scmd; epicsTimeStamp ts; scmd.type = PADCMD_STRM; scmd.flags = (!isbe()) ? PADCMD_STRM_FLAG_LE : 0; scmd.port = htons(drvPadUdpCommPort + 1); scmd.nsamples = htonl(nsamples); /* Dont ask for a reply if we broadcast */ if ( channel < 0 ) scmd.type |= PADCMD_QUIET; epicsPrintf("drvPadUdpCommSendStartCmd: calling padRequest\n"); epicsTimeGetCurrent(&ts); return padRequest(drvPadUdpCommSd, channel, scmd.type, xid, ts.secPastEpoch, ts.nsec, &scmd, 0, drvPadUdpCommTimeout); } /* 5/28/8: so I am invoking this from drvPadUdpCommListener with NO ts, and so it is using deadbeef! */ int drvPadUdpCommSendStartAll(epicsTimeStamp *ts_p) { int rval; epicsMutexLock(padStrmCmdLock); padStrmCmd.req.xid = htonl(drvPadUdpCommGetXid()); if ( ts_p ) { padStrmCmd.req.timestampHi = htonl(ts_p->secPastEpoch); padStrmCmd.req.timestampLo = htonl(ts_p->nsec); } else { padStrmCmd.req.timestampHi = htonl(0xdeadbeef); padStrmCmd.req.timestampLo = htonl(0xdeadbeef); } if ( drvPadUdpCommDebug ) printf("drvPadUdpCommSendStartAll -- padStream version= %d\n", padStrmCmd.req.version); rval = udpCommSend(drvPadUdpCommSd, &padStrmCmd, sizeof(padStrmCmd)); epicsMutexUnlock(padStrmCmdLock); return rval; } static void drvPadUdpCommListener(void *arg) { int sd; UdpCommPkt p = 0; PadReply rply; PadDataKind kind; unsigned int chan; unsigned int nsamples; /* 5/29/8 */ epicsTimeStamp myTimeStamp; int rc; /* Open a socket for incoming streamed data */ sd = udpCommSocket(PADPROTO_STRM_PORT); if ( sd < 0 ) { epicsPrintf("drvPadUdpCommListener -- unable to create socket: %s\n", strerror(-sd)); return; } epicsPrintf("drvPadUdpCommListener -- socket %d opened from incoming streamed data\n", sd); while ( 1 ) { /* release old buffer */ if ( drvPadUdpCommDebug ) epicsPrintf("drvPadUdpCommListener -- release old buffer\n"); padDataFree(p); /* keep sending start command until we receive something */ while ( !( p = udpCommRecv(sd, drvPadUdpCommTimeout)) ) { epicsTimeGetCurrent(&myTimeStamp); drvPadUdpCommSendStartAll(&myTimeStamp); if ( drvPadUdpCommDebug ) epicsPrintf("drvPadUdpCommListener -- send start all cmd repeatedly until get something\n"); } rply = padReplyFind(p); if ( drvPadUdpCommDebug ) epicsPrintf("drvPadUdpCommListener -- got packet\n"); #ifdef __PPC__ { uint32_t tbl; __asm__ __volatile__ ("mftb %0":"=r"(tbl)); tbl -= ntohl(rply->timestampLo); if ( tbl > roundtripMax ) roundtripMax = tbl; /* to read AVG, divide by 16 */ roundtripAvg += (tbl - (roundtripAvg >> 4)); } #endif chan = rply->chnl; nsamples = PADRPLY_STRM_NSAMPLES(rply); if ( chan >= MAX_BPM ) { /* drop invalid channels */ if ( drvPadUdpCommDebug ) errlogPrintf("drvPadUdpCommListener: invalid BPM channel #%i\n", chan); continue; } /* We assume Row-major format */ if ( rply->strm_cmd_flags & PADCMD_STRM_FLAG_CM ) { if ( drvPadUdpCommDebug ) errlogPrintf("drvPadUdpCommListener: bad data format -- row-major expected\n"); continue; } if ( isbe() != !(rply->strm_cmd_flags & PADCMD_STRM_FLAG_LE) ) { if ( drvPadUdpCommDebug ) errlogPrintf("drvPadUdpCommListener: bad data endianness\n"); continue; } kind = PADRPLY_STRM_FLAG_TYPE_GET(rply->strm_cmd_flags); if ( drvPadUdpCommDebug & 1 ) { errlogPrintf("STRM: C %2u N %3u K %1u TS: 0x%08"PRIx32" XID: 0x%08"PRIx32"\n", chan, nsamples, kind, ntohl(rply->timestampLo), ntohl(rply->xid)); } if ( PadDataBpm != kind ) goto FCOM_EXIT; /* * FCOM precessing */ fcomBlmProc(rply); FCOM_EXIT: /* send buffer for waveforms to pick up */ if ( 0 == padDataSave(chan, kind, p) ) p = 0; /* Scan lists only on the beam pulse. Waveforms * listening to the cal pulses will also be on * the same scanlist. */ if ( PadDataBpm == kind ) { scanIoRequest(scanlist[chan]); } } } static long padUdpCommInit(void) { int err,i; in_addr_t ipaddr; int rc; if ( !drvPadUdpCommPeerIp || INADDR_NONE == (ipaddr = inet_addr(drvPadUdpCommPeerIp)) ) { epicsPrintf("drvPadUdpComm -- invalid or unset peer IP; set drvPadUdpCommPeerIp to a string in 'dot' notation...\n"); return -1; } if ( (drvPadUdpCommSd = udpCommSocket(drvPadUdpCommPort)) < 0 ) { epicsPrintf("drvPadUdpComm -- unable to create socket: %s\n", strerror(-drvPadUdpCommSd)); return -1; } if ( (err=udpCommConnect(drvPadUdpCommSd, ipaddr, drvPadUdpCommPort)) ) { epicsPrintf("drvPadUdpComm -- unable to connect socket: %s\n", strerror(-err)); udpCommClose(drvPadUdpCommSd); drvPadUdpCommSd = -1; return -1; } rc = fcomBlmInit(); if(rc <0) { epicsPrintf("drvPadUdpCommListener -- fcomBlmInit failed\n"); return -1; } strmCmdInit(); for ( i=0; idpvt; if ( dpvt->channel >= MAX_BPM ) return -1; *ppvt = scanlist[dpvt->channel]; return 0; } /* 7/25/8 idea: pass in nsamples here (from init_record of llrf waveform */ int drvPadUdpCommDpvtInit(DrvPadUdpCommDpvt padUdpCommPart, unsigned channel) { padUdpCommPart->channel = channel; return 0; } long drvPadUdpComm_read_raw_ai_stripl(aiRecord *pai) { unsigned c = pai->inp.value.vmeio.card; if ( c >= MAX_BPM ) { pai->pact = TRUE; errlogPrintf("drvPadUdpComm -- read_raw_ai_strip - invalid channel # %i\n", c); return -1; } return 2; } static struct { long number; DRVSUPFUN report; DRVSUPFUN init; } drvPadUdpComm = { 2, padUdpCommReport, padUdpCommInit }; epicsExportAddress(drvet,drvPadUdpComm); static const struct iocshArg arg1 = { "peer ", /* iocshArgPersistentString is broken (3.14.8) - * it doesn't check for an empty string before * duplicating :-( */ iocshArgString }; static const struct iocshArg *bloatp[] = { &arg1, 0 }; struct iocshFuncDef drvPadUdpCommSetupDesc = { "drvPadUdpCommSetup", 1, bloatp }; void drvPadUdpCommSetup(const char *arg) { char *col, *str; if ( !(str=epicsStrDup(arg)) ) { epicsPrintf("drvPadUdpCommSetup: not enough memory for strdup\n"); return; } if ( (col = strchr(str, ':')) ) *col++=0; if ( INADDR_NONE == inet_addr(str) ) { epicsPrintf("drvPadUdpCommSetup: invalid IP address '%s'\n", str); } else { drvPadUdpCommPeerIp = str; epicsPrintf("drvPadUdpCommSetup: IP address is %s\n", str); } if ( col && 1 != sscanf(col, "%i", &drvPadUdpCommPort) ) { epicsPrintf("drvPadUdpCommSetup: invalid port number: '%s'\n", col); drvPadUdpCommPort = PADPROTO_PORT; } if ( 0 == drvPadUdpCommPort ) drvPadUdpCommPort = PADPROTO_PORT; epicsPrintf("drvPadUdpCommSetup: using port %d\n", drvPadUdpCommPort); } void drvPadUdpCommSetupFunc(const iocshArgBuf *args) { if ( !args[0].sval ) { epicsPrintf("usage: drvPadUdpCommSetup(\"peer_ip:port\")\n"); return; } drvPadUdpCommSetup(args[0].sval); } static void drvPadUdpCommRegistrar(void) { iocshRegister(&drvPadUdpCommSetupDesc, drvPadUdpCommSetupFunc); } epicsExportRegistrar(drvPadUdpCommRegistrar);