#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static pthread_mutex_t getBld_Mtx = PTHREAD_MUTEX_INITIALIZER; #define __LOCK() pthread_mutex_lock( & getBld_Mtx ) #define __UNLOCK() pthread_mutex_unlock( & getBld_Mtx ) extern "C" { extern epicsEventId pulseIdReady; epicsEventWaitStatus waitStatus; /* #include "dbSubBld.h" extern EvgPulseId evgTimeStamps[]; extern void getPulseId(int *pulseid, unsigned int *sec, unsigned int *nsec); */ } /* Example usage; from st.cmd prompt issue the following commnad: epics> bldServer("./bldServerTest -l 1 -d 10 -i eth0 -a 239.255.24.4") */ using namespace std; #define SERVER_PORT 10148 /* #define NCHANNELS 4 */ #define HxxUm6Imb02 -268494843 /* 239.255.24.5 */ #define HxxUm6Imb01 -268494844 /* 239.255.24.4 */ #define Nh2Sb1Ipm01 -268494845 /* 239.255.24.3 */ #define FEEGasDetEnergy -268494846 /* 239.255.24.2 */ #define PhaseCavity -268494847 /* 239.255.24.1 */ #define eBeam -268494848 /* 239.255.24.0 */ /** * Imb Data: all IMB share the same data type > XRT: > > HxxUm6Imb01, > HxxUm6Imb02, > HfxDg2Imb01, > HfxDg2Imb02, > XcsDg3Imb03, > XcsDg3Imb04, > HfxDg3Imb01, > HfxDg3Imb02, > HfxMonImb01, > HfxMonImb02, > HfxMonImb03 > > CXI Local: > > CxiDg1Imb01, > CxiDg2Imb01, > CxiDg2Imb02, > CxiDg4Imb01 */ /* Imb data type: */ int Rate=120; // Expected rate of BLD packets; 120Hz,60Hz,30Hz;10Hz;5Hz;1Hz;0.5Hz namespace Pds { namespace Ipimb { #pragma pack(push,4) //IPIMB Configuration class ConfigV2 { public: uint64_t triggerCounter; uint64_t serialID; uint16_t chargeAmpRange; uint16_t calibrationRange; uint32_t resetLength; uint32_t resetDelay; float chargeAmpRefVoltage; float calibrationVoltage; float diodeBias; uint16_t status; uint16_t errors; uint16_t calStrobeLength; uint16_t pad0; uint32_t trigDelay; uint32_t trigPsDelay; uint32_t adcDelay; }; // IPIMB Raw data class DataV2 { public: uint64_t triggerCounter; uint16_t config0; uint16_t config1; uint16_t config2; uint16_t channel0; //< Raw counts value returned from channel 0. uint16_t channel1; //< Raw counts value returned from channel 1. uint16_t channel2; //< Raw counts value returned from channel 2. uint16_t channel3; //< Raw counts value returned from channel 3. uint16_t channel0ps; //< Raw counts value returned from channel 0. uint16_t channel1ps; //< Raw counts value returned from channel 1. uint16_t channel2ps; //< Raw counts value returned from channel 2. uint16_t channel3ps; //< Raw counts value returned from channel 3. uint16_t checksum; }; #pragma pack(pop) } // namespace Ipimb } // namespace Pds namespace Pds { namespace Lusi { // Processed results class IpmFexV1 { public: enum { NCHANNELS = 4 }; float channel[NCHANNELS]; float sum; float xpos; float ypos; }; } // namespace Lusi } // namespace Pds namespace Pds { namespace Bld { class BLDHeader { public: uint32_t tv_sec; uint32_t tv_nsec; uint32_t mbz1; /* must be zero */ uint32_t fiducialId; uint32_t mbz2; /* must be zero */ /* Xtc Section 1 */ uint32_t damage; uint32_t logicalId; /* source 1 */ uint32_t physicalId; /* source 2 */ uint32_t dataType; /* Contains - this is the version field */ uint32_t extentSize; /* Extent - size of data following Xtc Section 2 */ /* Xtc Section 2 */ uint32_t damage2; uint32_t logicalId2; uint32_t physicalId2; uint32_t dataType2; uint32_t extentSize2; /* Extent - size of data following Xtc Section 2 */ void report(void); void file_report(ofstream &recvfile); /* pass file by reference */ void recv_time(ofstream &recvfile); }; class BldDataIpimbV1 { public: Ipimb::DataV2 ipimbData; Ipimb::ConfigV2 ipimbConfig; Lusi::IpmFexV1 ipmFexData; void report(void); void file_report(ofstream &recvfile); }; class BldDataPCavV1 { public: double fitTime1; // in pico-seconds double fitTime2; // in pico-seconds double charge1; // in pico-columbs double charge2; // in pico-columbs void report(void); void file_report(ofstream &recvfile); }; class BldDataBeamV6 { public: uint32_t uDamageMask; double ebeamCharge; /* in nC */ double ebeamL3Energy; /* in MeV */ double ebeamLTUPosX; /* in mm */ double ebeamLTUPosY; /* in mm */ double ebeamLTUAngX; /* in mrad */ double ebeamLTUAngY; /* in mrad */ double ebeamBC2Current; /* in Amps */ /* Added in VERSION_1 */ double ebeamBC2Energy; /* in mm */ double ebeamBC1Current; /* in Amps */ double ebeamBC1Energy; /* in mm */ /* Added in VERSION_2 */ double ebeamUndPosX; /* in mm */ double ebeamUndPosY; /* in mm */ double ebeamUndAngX; /* in mrad */ double ebeamUndAngY; /* in mrad */ /* Added in VERSION_3 */ double ebeamXTCAVAmpl; /* in MeV */ double ebeamXTCAVPhase; /* in deg */ double ebeamDMP502Charge; /* in Nel */ /* Added in VERSION_4 */ /* Shantha Condamoor: 7-Jul-2014 */ /* shot-to-shot Photon Energy Calculation added */ /* Addition of shot-to-shot X position of BPMS:LTU1:[250/450]:X */ double ebeamPhotonEnergy;/* in eV */ double ebeamLTU250PosX; /* in mm */ double ebeamLTU450PosX; /* in mm */ void report(void); void file_report(ofstream &recvfile); }; } // namespace Bld } // namespace Pds std::string addressToStr( unsigned int uAddr ) { unsigned int uNetworkAddr = htonl(uAddr); const unsigned char* pcAddr = (const unsigned char*) &uNetworkAddr; std::stringstream sstream; sstream << (int) pcAddr[0] << "." << (int) pcAddr[1] << "." << (int) pcAddr[2] << "." << (int) pcAddr[3]; return sstream.str(); } /*struct msghdr include/linux/socket.h struct iovec { void *iov_base; __kernel_size_t iov_len; }; struct msghdr { void *msg_name; // not really a name, but a pointer to a sockaddr_in structure, // which contains an IP address and a port number int msg_namelen; //describes the length of this sockaddr_in structure struct iovec *msg_iov; // refers to an array of iovec structures, which reference the payload // payload can be present in a series of individual blocks, where each block is denoted // in an iovec structure by its initial address (iov_base) and its length (iov_len) __kernel_size_t msg_iovlen; // can be used to pass protocol-specific control messages void *msg_control; __kernel_size_t msg_controllen; unsigned msg_flags; // used to pass different flags both from the user process to the kernel and in the opposite direction }; */ class receiver { public: receiver(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, char* sInterfaceIp = NULL); ~receiver(); bool connect(); bool run(long packets); void setReport(int report); void setLevel(int level); protected: int _sock; unsigned int _uMaxDataSize; unsigned int _uAddr, _uPort; bool _bInitialized; char *_sInterfaceIp; struct msghdr _hdr; // Control structure socket receive struct iovec _iov[2]; // Buffer description socket receive - bld hdr, bld data, bld spare struct sockaddr_in _src; // Socket name source machine int Report; int Level; }; receiver::receiver(unsigned uAddr, unsigned uPort, unsigned int uMaxDataSize, char* sInterfaceIp) : _sock(-1), _uMaxDataSize(uMaxDataSize), _uAddr(uAddr), _uPort(uPort), _bInitialized(false), _sInterfaceIp(sInterfaceIp) { Report=0; Level=0; } receiver::~receiver() { } void receiver::setReport(int report) { printf("Setting report to %d\n",report); Report = report; } void receiver::setLevel(int level) { printf("Setting level to %d\n",level); Level = level; } bool receiver::connect() { __LOCK(); _sock = socket(AF_INET, SOCK_DGRAM, 0); __UNLOCK(); if ( _sock == -1 ) { printf("ERROR: socket()\n"); return false; } int iRecvBufSize = _uMaxDataSize + sizeof(struct sockaddr_in); #ifdef __linux__ iRecvBufSize += 2048; // 1125 #endif __LOCK(); if(setsockopt(_sock, SOL_SOCKET, SO_RCVBUF, (char*)&iRecvBufSize, sizeof(iRecvBufSize)) == -1) { __UNLOCK(); printf("ERROR: setsockopt(SO_RECVBUF)\n"); return false; } __UNLOCK(); int iYes = 1; __LOCK(); if(setsockopt(_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&iYes, sizeof(iYes)) == -1) { __UNLOCK(); printf("ERROR: setsockopt(SO_REUSEADDR)\n"); return false; } sockaddr_in sockaddrSrc; sockaddrSrc.sin_family = AF_INET; sockaddrSrc.sin_addr.s_addr = htonl(_uAddr); sockaddrSrc.sin_port = htons(_uPort); __UNLOCK(); printf("Address=%d Port=%d\n", _uAddr, _uPort); __LOCK(); if (bind( _sock, (struct sockaddr*) &sockaddrSrc, sizeof(sockaddrSrc) ) == -1 ) { __UNLOCK(); printf("ERROR: bind\n"); return false; } __UNLOCK(); sockaddr_in sockaddrName; unsigned iLength = sizeof(sockaddrName); if(getsockname(_sock, (sockaddr*)&sockaddrName, &iLength) == 0) { unsigned int uSockAddr = ntohl(sockaddrName.sin_addr.s_addr); unsigned int uSockPort = (unsigned int )ntohs(sockaddrName.sin_port); printf( "Server addr: %s Port %u Buffer Size %u\n", addressToStr(uSockAddr).c_str(), uSockPort, iRecvBufSize ); } else { printf("ERROR: getsockname\n"); return false; } __LOCK(); memset((void*)&_hdr, 0, sizeof(_hdr)); _hdr.msg_name = (caddr_t)&_src; _hdr.msg_namelen = sizeof(_src); _hdr.msg_iov = _iov; _hdr.msg_iovlen = 2; /* bld hdr, bld data, bld spare */ __UNLOCK(); in_addr_t uiInterface; if (_sInterfaceIp == NULL || _sInterfaceIp[0] == 0 ) { uiInterface = INADDR_ANY; } else { if ( _sInterfaceIp[0] < '0' || _sInterfaceIp[0] > '9' ) { struct ifreq ifr; strcpy( ifr.ifr_name, _sInterfaceIp ); int iError = ioctl( _sock, SIOCGIFADDR, (char*)&ifr ); if ( iError == 0 ) { uiInterface = ntohl( *(unsigned int*) &(ifr.ifr_addr.sa_data[2]) ); } else { printf( "Cannot get IP address from network interface %s\n", _sInterfaceIp ); uiInterface = 0; } } else { uiInterface = ntohl(inet_addr(_sInterfaceIp)); } if ( uiInterface != 0 ) { printf( "multicast interface IP: %s, inet_addr=%d\n", addressToStr(uiInterface).c_str(),htonl(_uAddr)); struct ip_mreq ipMreq; memset((char*)&ipMreq, 0, sizeof(ipMreq)); ipMreq.imr_multiaddr.s_addr = htonl(_uAddr); ipMreq.imr_interface.s_addr = htonl(uiInterface); if (setsockopt (_sock, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&ipMreq, sizeof(ipMreq)) < 0 ) { printf("ERROR: setsockopt(IP_ADD_MEMBERSHIP)\n"); return false; } } _bInitialized = true; } return true; } bool receiver::run(long packets) { ofstream recvfile; Pds::Bld::BLDHeader bldHdr; Pds::Bld::BldDataIpimbV1 imbData; Pds::Bld::BldDataPCavV1 pcavData; Pds::Bld::BldDataBeamV6 eBeamData; int pulseid_mismatch_rcvd[120] = {0}; int pulseid_mismatch_evg[120] = {0}; int dupCnt[120] = {0}; int zeroCnt[120] = {0}; int missPulseId[120] = {0}; int missPrevPulseId[120] = {0}; int c=0; int cn=0; int cnt=0; int cont=0; uint32_t prev_pulseid = 0; bool first = true; long count = 0; if ( !_bInitialized ) { printf( "BldServerSlim::fetch() : BldServerSlim is not initialized successfully\n" ); return false; } __LOCK(); _iov[0].iov_base = &bldHdr; _iov[0].iov_len = sizeof(bldHdr); switch ( (int) _uAddr) { case eBeam: _iov[1].iov_base = &eBeamData; _iov[1].iov_len = sizeof(eBeamData); break; case PhaseCavity: _iov[1].iov_base = &pcavData; _iov[1].iov_len = sizeof(pcavData); break; case HxxUm6Imb01: case HxxUm6Imb02: _iov[1].iov_base = &imbData; _iov[1].iov_len = sizeof(imbData); break; } __UNLOCK(); if (Report > 0) { switch ( (int) _uAddr) { case eBeam: recvfile.open ("EBeam.txt"); break; case PhaseCavity: recvfile.open ("PhaseCavity.txt"); break; case HxxUm6Imb01: recvfile.open ("HxxUm6Imb01.txt"); break; case HxxUm6Imb02: recvfile.open ("HxxUm6Imb02.txt"); break; } } /*int pulseid = 0; unsigned int sec = 0; unsigned int nsec = 0; */ epicsTimeStamp epicsTs; int rc; while (count <= packets) { unsigned int iRecvDataSize = 0; __LOCK(); iRecvDataSize = recvmsg(_sock, &_hdr, 0); __UNLOCK(); if ( iRecvDataSize <= 0 ) { printf( "bldServerTest:main(): bldServer.fetch() failed. Error Code = %d\n", errno); return false; } else { switch(_hdr.msg_flags) { case MSG_EOR: puts("MSG_EOR"); break; case MSG_TRUNC: puts("MSG_TRUNC"); break; case MSG_CTRUNC: puts("MSG_CTRUNC"); break; case MSG_OOB: puts("MSG_OOB"); break; case MSG_ERRQUEUE: puts("MSG_ERRQUEUE"); break; case MSG_DONTWAIT: puts("MSG_DONTWAIT"); break; } /* pulseid = 0; sec = 0; nsec = 0; getPulseId(&pulseid, &sec, &nsec); printf("%d %d\n", pulseid, bldHdr.fiducialId);*/ rc = evrTimeGetFromPipeline(&epicsTs, evrTimeActive, 0, 0, 0, 0, 0); if (first) { /* first BLD packet received */ first = false; } else { if (bldHdr.fiducialId!=PULSEID(epicsTs)) { pulseid_mismatch_evg[cont] = PULSEID(epicsTs); pulseid_mismatch_rcvd[cont] = bldHdr.fiducialId; cont++; } if (bldHdr.fiducialId==0) { // pulseID is 0 if (prev_pulseid != 131037) // not pulseID rollover zeroCnt[cn++] = prev_pulseid; } else { // pulseID is non-zero if (prev_pulseid == bldHdr.fiducialId) { dupCnt[c++] = bldHdr.fiducialId; } else { // check for missed pulseIDs if ((bldHdr.fiducialId - prev_pulseid) != (360/Rate)) { missPulseId[cnt] = bldHdr.fiducialId; missPrevPulseId[cnt++]=prev_pulseid; } } } } prev_pulseid = bldHdr.fiducialId; if (Level > 0) { std::cout << "Evgpulseid = " << PULSEID(epicsTs) << std::endl; /* PulseID from EVG */ std::cout << "Evgnsec = " << epicsTs.nsec << std::endl; /* nSec from EVG */ std::cout << "Evgsec = " << epicsTs.secPastEpoch << std::endl; /* seconds from EVG */ bldHdr.report(); } if ((Report > 0) && (recvfile.is_open())) { bldHdr.recv_time(recvfile); bldHdr.file_report(recvfile); } if (Level > 0) { switch ( (int) _uAddr) { case eBeam: eBeamData.report(); break; case PhaseCavity: pcavData.report(); break; case HxxUm6Imb01: case HxxUm6Imb02: imbData.report(); break; } } if (Report > 0) { switch ( (int) _uAddr) { case eBeam: eBeamData.file_report(recvfile); break; case PhaseCavity: pcavData.file_report(recvfile); break; case HxxUm6Imb01: case HxxUm6Imb02: imbData.file_report(recvfile); break; } } } count++; } switch ( (int) _uAddr) { case eBeam: puts("EBeam"); break; case PhaseCavity: puts("PhaseCavity"); break; case HxxUm6Imb01: puts("HxxUm6Imb01"); break; case HxxUm6Imb02: puts("HxxUm6Imb02"); break; } for (int i=0; i < c; i++) std::cout << "Duplicates: " << dupCnt[i] << std::endl; for (int i=0; i < cn; i++) std::cout << "Zeros: " << zeroCnt[i] << std::endl; for (int i=0; i < cnt; i++) std::cout << "Missed Timeslots between: " << missPrevPulseId[i] << " " << missPulseId[i] << std::endl; for (int i=0; i < cont; i++) std::cout << "PulseID mismatch: " << pulseid_mismatch_evg[i] << " " << pulseid_mismatch_rcvd[i] << std::endl; if ((Report > 0) && (recvfile.is_open())) { for (int i=0; i < c; i++) recvfile << "Duplicates: " << dupCnt[i] << std::endl; for (int i=0; i < cn; i++) recvfile << "Zeros: " << zeroCnt[i] << std::endl; for (int i=0; i < cnt; i++) recvfile << "Missed Timeslots between: " << missPrevPulseId[i] << " " << missPulseId[i] << std::endl; recvfile.close(); } return true; } void Pds::Bld::BLDHeader::recv_time (ofstream &recvfile) { struct timeval tv; struct tm* ptm; char time_string[40]; /* long milliseconds; */ /* Obtain the time of day, and convert it to a tm struct. */ gettimeofday (&tv, NULL); ptm = localtime (&tv.tv_sec); /* Format the date and time, down to a single second. */ strftime (time_string, sizeof (time_string), "%Y-%m-%d %H:%M:%S", ptm); /* Compute milliseconds from microseconds. */ /*milliseconds = tv.tv_usec / 1000; */ /* Print the formatted time, in seconds, followed by a decimal point and the milliseconds. */ recvfile << time_string << "." << tv.tv_usec << " "; } void Pds::Bld::BLDHeader::file_report(ofstream &recvfile) { recvfile << tv_sec << " " << tv_nsec << " " << mbz1 << " " /* must be zero */ << fiducialId << " " << mbz2 << " " /* must be zero */ /* Xtc Section 1 */ << damage << " " << logicalId << " " /* source 1 */ << physicalId << " " /* source 2 */ << dataType << " " /* Contains - this is the version field */ << extentSize << " " /* Extent - size of data following Xtc Section 2 */ /* Xtc Section 2 */ << damage2 << " " << logicalId2 << " " << physicalId2 << " " << dataType2 << " " << extentSize2 << std::endl; /* Extent - size of data following Xtc Section 2 */ } void Pds::Bld::BLDHeader::report(void) { std::cout << "****************** BLD Header ****************** " << std::endl; std::cout << "tv_sec = " << std::dec<< tv_sec << std::endl; std::cout << "tv_nsec = " << std::dec << tv_nsec << std::endl; std::cout << "mbz1 = 0x" << std::hex << mbz1 << std::endl; /* must be zero */ std::cout << "fiducialId = " << std::dec << fiducialId << std::endl; std::cout << "mbz2 = 0x" << std::hex << mbz2 << std::endl; /* must be zero */ /* Xtc Section 1 */ std::cout << "damage = 0x" << std::hex << damage << std::endl; std::cout << "logicalId = 0x" << std::hex << logicalId << std::endl; /* source 1 */ std::cout << "physicalId = 0x" << std::hex << physicalId << std::endl; /* source 2 */ std::cout << "dataType = 0x" << std::hex << dataType << std::endl; /* Contains - this is the version field */ std::cout << "extentSize = 0x" << std::hex << extentSize << std::endl; /* Extent - size of data following Xtc Section 2 */ /* Xtc Section 2 */ std::cout << "damage2 = 0x" << std::hex << damage2 << std::endl; std::cout << "logicalId2 = 0x" << std::hex << logicalId2 << std::endl; std::cout << "physicalId2 = 0x" << std::hex << physicalId2 << std::endl; std::cout << "dataType2 = 0x" << std::hex << dataType2 << std::endl; std::cout << "extentSize2 = 0x" << std::hex << extentSize2 << std::endl; /* Extent - size of data following Xtc Section 2 */ } void Pds::Bld::BldDataBeamV6::file_report(ofstream &recvfile) { recvfile << " " << uDamageMask << " " << ebeamCharge << " nC " << ebeamL3Energy << " MeV " << ebeamLTUPosX << " mm " << ebeamLTUPosY << " mm " << ebeamLTUAngX << " mrad " << ebeamLTUAngY << " mrad " << ebeamBC2Current << " Amps " << ebeamBC2Energy << " mm " << ebeamBC1Current << " Amps " << ebeamBC1Energy << " mm " << ebeamUndPosX << " mm " << ebeamUndPosY << " mm " << ebeamUndAngX << " mrad " << ebeamUndAngY << " mrad " << ebeamXTCAVAmpl << " MeV " << ebeamXTCAVPhase << " deg " << ebeamDMP502Charge << " Nel " << ebeamPhotonEnergy << " eV " << ebeamLTU250PosX << " mm " << ebeamLTU450PosX << " mm" << std::endl; } void Pds::Bld::BldDataBeamV6::report(void) { std::cout << "****************** BLD Payload ****************** " << std::endl; std::cout << "uDamageMask = " << std::dec << uDamageMask << std::endl; std::cout << "ebeamCharge = " << std::dec << ebeamCharge << " nC" << std::endl; std::cout << "ebeamL3Energy = " << std::dec << ebeamL3Energy << " MeV" << std::endl; std::cout << "ebeamLTUPosX = " << std::dec << ebeamLTUPosX << " mm" << std::endl; std::cout << "ebeamLTUPosY = " << std::dec << ebeamLTUPosY << " mm" << std::endl; std::cout << "ebeamLTUAngX = " << std::dec << ebeamLTUAngX << " mrad" << std::endl; std::cout << "ebeamLTUAngY = " << std::dec << ebeamLTUAngY << " mrad" << std::endl; std::cout << "ebeamBC2Current = " << std::dec << ebeamBC2Current << " Amps" << std::endl; std::cout << "ebeamBC2Energy = " << std::dec << ebeamBC2Energy << " mm" << std::endl; std::cout << "ebeamBC1Current = " << std::dec << ebeamBC1Current << " Amps" << std::endl; std::cout << "ebeamBC1Energy = " << std::dec << ebeamBC1Energy << " mm" << std::endl; std::cout << "ebeamUndPosX = " << std::dec << ebeamUndPosX << " mm" << std::endl; std::cout << "ebeamUndPosY = " << std::dec << ebeamUndPosY << " mm" << std::endl; std::cout << "ebeamUndAngX = " << std::dec << ebeamUndAngX << " mrad" << std::endl; std::cout << "ebeamUndAngY = " << std::dec << ebeamUndAngY << " mrad" << std::endl; std::cout << "ebeamXTCAVAmpl = " << std::dec << ebeamXTCAVAmpl << " MeV" << std::endl; std::cout << "ebeamXTCAVPhase = " << std::dec << ebeamXTCAVPhase << " deg" << std::endl; std::cout << "ebeamDMP502Charge = " << std::dec << ebeamDMP502Charge << " Nel" << std::endl; std::cout << "ebeamPhotonEnergy = " << std::dec << ebeamPhotonEnergy << " eV" << std::endl; std::cout << "ebeamLTU250PosX = " << std::dec << ebeamLTU250PosX << " mm" << std::endl; std::cout << "ebeamLTU450PosX = " << std::dec << ebeamLTU450PosX << " mm" << std::endl; std::cout << "********************************************* " << std::endl; } void Pds::Bld::BldDataPCavV1::file_report(ofstream &recvfile) { recvfile << " " << fitTime1 << " " << fitTime2 << " " << charge1 << " " << charge2 << std::endl; } void Pds::Bld::BldDataPCavV1::report(void) { std::cout << "****************** BLD Payload ****************** " << std::endl; std::cout << "fitTime1 = " << std::dec << fitTime1 << std::endl; std::cout << "fitTime2 = " << std::dec << fitTime2 << std::endl; std::cout << "charge1 = " << std::dec << charge1 << std::endl; std::cout << "charge2 = " << std::dec << charge2 << std::endl; std::cout << "********************************************* " << std::endl; } void Pds::Bld::BldDataIpimbV1::file_report(ofstream &recvfile) { recvfile << " " << ipimbData.triggerCounter << " " << ipimbData.config0 << " " << ipimbData.config1 << " " << ipimbData.config2 << " " << ipimbData.channel0 << " " /**< Raw counts value returned from channel 0. */ << ipimbData.channel1 << " " /**< Raw counts value returned from channel 1. */ << ipimbData.channel2 << " " /**< Raw counts value returned from channel 2. */ << ipimbData.channel3 << " " /**< Raw counts value returned from channel 3. */ << ipimbData.channel0ps << " " /**< Raw counts value returned from channel 0. */ << ipimbData.channel1ps << " " /**< Raw counts value returned from channel 1. */ << ipimbData.channel2ps << " " /**< Raw counts value returned from channel 2. */ << ipimbData.channel3ps << " " /**< Raw counts value returned from channel 3. */ << ipimbData.checksum << " " /* IPIMB Configuration */ << ipimbConfig.triggerCounter << " " << ipimbConfig.serialID << " " << ipimbConfig.chargeAmpRange << " " << ipimbConfig.calibrationRange << " " << ipimbConfig.resetLength << " " << ipimbConfig.resetDelay << " " << ipimbConfig.chargeAmpRefVoltage << " " << ipimbConfig.calibrationVoltage << " " << ipimbConfig.diodeBias << " " << ipimbConfig.status << " " << ipimbConfig.errors << " " << ipimbConfig.calStrobeLength << " " << ipimbConfig.pad0 << " " << ipimbConfig.trigDelay << " " << ipimbConfig.trigPsDelay << " " << ipimbConfig.adcDelay << " " << ipmFexData.channel[0] << " " << ipmFexData.channel[1] << " " << ipmFexData.channel[2] << " " << ipmFexData.channel[3] << " " << ipmFexData.sum << " " << ipmFexData.xpos << " " << ipmFexData.ypos << std::endl; } void Pds::Bld::BldDataIpimbV1::report(void) { std::cout << "****************** BLD Payload ****************** " << std::endl; std::cout << "triggerCounter = " << std::dec << ipimbData.triggerCounter << std::endl; std::cout << "config0 = " << std::dec << ipimbData.config0 << std::endl; std::cout << "config1 = " << std::dec << ipimbData.config1 << std::endl; std::cout << "config2 = " << std::dec << ipimbData.config2 << std::endl; std::cout << "channel0 = " << std::dec << ipimbData.channel0 << std::endl; /**< Raw counts value returned from channel 0. */ std::cout << "channel1 = " << std::dec << ipimbData.channel1 << std::endl; /**< Raw counts value returned from channel 1. */ std::cout << "channel2 = " << std::dec << ipimbData.channel2 << std::endl; /**< Raw counts value returned from channel 2. */ std::cout << "channel3 = " << std::dec << ipimbData.channel3 << std::endl; /**< Raw counts value returned from channel 3. */ std::cout << "channel0ps = " << std::dec << ipimbData.channel0ps << std::endl; /**< Raw counts value returned from channel 0. */ std::cout << "channel1ps = " << std::dec << ipimbData.channel1ps << std::endl; /**< Raw counts value returned from channel 1. */ std::cout << "channel2ps = " << std::dec << ipimbData.channel2ps << std::endl; /**< Raw counts value returned from channel 2. */ std::cout << "channel3ps = " << std::dec << ipimbData.channel3ps << std::endl; /**< Raw counts value returned from channel 3. */ std::cout << "checksum = " << std::dec << ipimbData.checksum << std::endl; /* IPIMB Configuration */ std::cout << "triggerCounterConfig = " << std::dec << ipimbConfig.triggerCounter << std::endl; std::cout << "serialID = " << std::dec << ipimbConfig.serialID << std::endl; std::cout << "chargeAmpRange = " << std::dec << ipimbConfig.chargeAmpRange << std::endl; std::cout << "calibrationRange = " << std::dec << ipimbConfig.calibrationRange << std::endl; std::cout << "resetLength = " << std::dec << ipimbConfig.resetLength << std::endl; std::cout << "resetDelay = " << std::dec << ipimbConfig.resetDelay << std::endl; std::cout << "chargeAmpRefVoltage = " << std::dec << ipimbConfig.chargeAmpRefVoltage << std::endl; std::cout << "calibrationVoltage = " << std::dec << ipimbConfig.calibrationVoltage << std::endl; std::cout << "diodeBias = " << std::dec << ipimbConfig.diodeBias << std::endl; std::cout << "status = " << std::dec << ipimbConfig.status << std::endl; std::cout << "errors = " << std::dec << ipimbConfig.errors << std::endl; std::cout << "calStrobeLength = " << std::dec << ipimbConfig.calStrobeLength << std::endl; std::cout << "pad0 = " << std::dec << ipimbConfig.pad0 << std::endl; std::cout << "trigDelay = " << std::dec << ipimbConfig.trigDelay << std::endl; std::cout << "trigPsDelay = " << std::dec << ipimbConfig.trigPsDelay << std::endl; std::cout << "adcDelay = " << std::dec << ipimbConfig.adcDelay << std::endl; std::cout << "channel 0 = " << std::dec << ipmFexData.channel[0] << std::endl; std::cout << "channel 1 = " << std::dec << ipmFexData.channel[1] << std::endl; std::cout << "channel 2 = " << std::dec << ipmFexData.channel[2] << std::endl; std::cout << "channel 3 = " << std::dec << ipmFexData.channel[3] << std::endl; std::cout << "sum = " << std::dec << ipmFexData.sum << std::endl; std::cout << "xpos = " << std::dec << ipmFexData.xpos << std::endl; std::cout << "ypos = " << std::dec << ipmFexData.ypos << std::endl; std::cout << "********************************************* " << std::endl; } static void usage(const char *nm) { fprintf(stderr,"Usage: %s [-h] [-l level] [-d bld_pkts] [-p port] [-i interface] [-a multicast IP1 multicast_IP2...]\n", nm); fprintf(stderr," -h: this message\n"); fprintf(stderr," -d bld_pkts: Number of BLD packets to receive\n\n"); fprintf(stderr," -p port: port on which to listen (default: %u)\n", SERVER_PORT); fprintf(stderr," -i eth: Ethernet interface (default: eth0)\n"); fprintf(stderr," -a addr1 addr2...: Multicast address\n"); } int bldServerTest(int argc, char** argv) { int ch; long bld_pkts = 0; unsigned short port = SERVER_PORT; char eth[] = "eth0"; int report=0; // file report print level. 0= No file print; 1 = reports printed to file int level=0; // report print level. 0= No print; 1 = reports printed while ( (ch = getopt(argc, argv, "f:l:d:hp:i:a")) >= 0 ) { switch (ch) { case 'f': if ( 1 != sscanf(optarg,"%s",&report) ) { fprintf(stderr,"Unable to scan argument to '-f' (short int)\n"); return 1; } break; case 'l': if ( 1 != sscanf(optarg,"%s",&level) ) { fprintf(stderr,"Unable to scan argument to '-l' (short int)\n"); return 1; } break; case 'i': if ( 1 != sscanf(optarg,"%s",ð) ) { fprintf(stderr,"Unable to scan argument to '-i' (short int)\n"); return 1; } break; case 'p': if ( 1 != sscanf(optarg,"%hi",&port) ) { fprintf(stderr,"Unable to scan argument to '-p' (short int)\n"); return 1; } break; case 'h': usage(argv[0]); return 0; case 'd': if ( 1 != sscanf(optarg,"%i",&bld_pkts) ) { fprintf(stderr,"Unable to scan argument to '-d' (int)\n"); return 1; } break; } } /** counts the number of arguments to make sure they are valid */ if(optind >= argc){ fprintf(stderr, "error: argument needed to identify device\n"); return -1; } printf("argc %d optind %d %s %s %s %s %s %s %s\n",argc,optind,argv[0],argv[1],argv[2],argv[3],argv[4],argv[6],argv[optind]); // PCAV address *.1 // receiver r(ntohl(inet_addr("239.255.24.1")), port, 1024, "eth0"); pid_t* pID = new pid_t[argc-optind]; // array to store process ids for (int i = 0; i < argc-optind; ++i) { pID[i] = fork(); if(pID[i] == 0) { // child process printf("\n Child Process for receiver %d [%s]\n",i,argv[optind+i]); receiver r(ntohl(inet_addr(argv[optind+i])), port, 1024, eth); r.setReport(report); r.setLevel(level); if (!r.connect()) { fprintf(stderr, "ERROR: unable to connect to %s\n",argv[optind+i]); exit(0); // could not connect receiver - terminate child process } else { if (!r.run(bld_pkts)) fprintf(stderr, "ERROR: Child Process for receiver %d: Failed to run",i); } exit(0); //Child process ends } else if (pID[i] < 0) { // fork failed - returns -1 on failure printf("\n Fork failed for receiver d [%s], quitting!!!!!!\n",i,argv[optind+i]); exit(0); } } /* int evgpulseid = 0; unsigned int evgsec=0; unsigned int evgnsec = 0; while (1) { waitStatus = epicsEventWait(pulseIdReady); if (waitStatus == epicsEventWaitOK) { getPulseId(&evgpulseid, &evgsec, &evgnsec); } } */ // won't wait in right order but stops soon after last child dies /*for (int i =0; i < argc-optind; ++i) { int status; while (-1 == waitpid(pID[i], &status, 0)); if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { exit(1); } }*/ return 0; } // bldServerTest() #include #include /* This is the command, which the vxWorks shell will call directly */ void bldServer(char *name) { char seps[] = " ,\t\n"; char *token[10]; int count=0; /* Establish string and get the first token: */ token[count] = strtok( name, seps ); while( token[count] != NULL ) { /* While there are tokens in "string" */ count++; /* Get next token: */ token[count] = strtok( NULL, seps ); } bldServerTest(count, token); } /* Information needed by iocsh */ static iocshArg bldServerArg0 = {"name", iocshArgString}; static iocshArg *bldServerArgs[] = {&bldServerArg0}; static iocshFuncDef bldServerFuncDef = {"bldServer", 1, bldServerArgs}; /* Wrapper called by iocsh, selects the argument types that bldServer needs */ static void bldServerCallFunc(const iocshArgBuf *args) { bldServer(args[0].sval); } /* Registration routine, runs at startup */ static void bldServerRegister(void) { iocshRegister(&bldServerFuncDef, bldServerCallFunc); } epicsExportRegistrar(bldServerRegister);