ctb server: (bug fixes, udp sending feature instead of tcp, better debugging prints,

digital readout mode (fifoempty flag in status register is always up, so it is idle for this readout mode),
pattern loop bugs, max_pattern_length redefined in server side, do not update period for ctb and moench in setreceiver
This commit is contained in:
2019-02-28 08:46:26 +01:00
parent 1fe473e830
commit f80483de6e
18 changed files with 378 additions and 103 deletions

View File

@ -0,0 +1,115 @@
#pragma once
#include "logger.h"
#include "sls_detector_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
extern const enum detectorType myDetectorType;
extern int nSamples;
extern int dataBytes;
extern int nframes;
extern char* ramValues;
#define UDP_PACKET_HEADER_VERSION (0x1)
#define UDP_PACKET_MAX_DATA_BYTES (1280)
uint32_t udpPacketNumber = 0;
uint64_t udpFrameNumber = 0;
int numSamplesPerPacket = 0;
int dataBytesPerSample = 0;
int dataBytesPerPacket = 0;
int udpHeaderOffset = 0;
uint32_t getUDPPacketNumber() {
return udpPacketNumber;
}
uint64_t getUDPFrameNumber() {
return udpFrameNumber;
}
void calculateDataBytesPerSample() {
dataBytesPerSample = dataBytes / nSamples;
numSamplesPerPacket = (double)UDP_PACKET_MAX_DATA_BYTES / (double)dataBytesPerSample;
dataBytesPerPacket = dataBytesPerSample * numSamplesPerPacket;
FILE_LOG(logDEBUG2, ("Databytes/Sample = %d, numSamples/Packet:%d dataBytes/Packet:%d\n",
dataBytesPerSample, numSamplesPerPacket, dataBytesPerPacket));
}
/**
* Called for each UDP packet header creation
*
*/
void createUDPPacketHeader(char* buffer, uint16_t id) {
memset(buffer, 0, sizeof(sls_detector_header));
sls_detector_header* header = (sls_detector_header*)(buffer);
header->modId = id;
// row and column remains 0 (only used by ctb now)
// uint64_t timestamp FIXME: needed?
header->detType = (uint16_t)myDetectorType;
header->version = UDP_PACKET_HEADER_VERSION;
// reset offset
udpHeaderOffset = 0;
// reset frame number
udpFrameNumber = 0;
}
int fillUDPPacket(char* buffer) {
FILE_LOG(logDEBUG2, ("Databytes:%d offset:%d\n", dataBytes, udpHeaderOffset));
// reached end of data for one frame
if (udpHeaderOffset >= dataBytes) {
// reset offset
udpHeaderOffset = 0;
return 0;
}
sls_detector_header* header = (sls_detector_header*)(buffer);
// first packet (update frame number)
if (udpHeaderOffset == 0) {
// increment frame number (starts at 1)
++udpFrameNumber;
header->frameNumber = udpFrameNumber;
udpPacketNumber = -1;
}
// increment udp packet number (starts at 0)
++udpPacketNumber;
// copy packet number
FILE_LOG(logDEBUG2, ("Creating packet number %d (fnum:%lld)\n", udpPacketNumber, (long long int) udpFrameNumber));
header->packetNumber = udpPacketNumber;
// calculate number of bytes to write
int numBytesToWrite = ((udpHeaderOffset + dataBytesPerPacket) <= dataBytes) ?
dataBytesPerPacket : (dataBytes - udpHeaderOffset);
// copy number of samples in current packet
header->reserved = (numBytesToWrite / dataBytesPerSample);
if (numBytesToWrite % dataBytesPerSample) {
FILE_LOG(logERROR, ("fillUDPPacketHeader: numBytesToWrite is not divisible by dataBytesPerSample! Calculation error\n"));
}
// copy date
memcpy(buffer + sizeof(sls_detector_header), ramValues + udpHeaderOffset, numBytesToWrite);
// increment offset
udpHeaderOffset += numBytesToWrite;
return numBytesToWrite + sizeof(sls_detector_header);
}

View File

@ -168,6 +168,7 @@ int mapCSP0(void) {
(long long unsigned int)CSP0BASE,
(long long unsigned int)(CSP0BASE+MEM_SIZE)));
FILE_LOG(logINFO, ("Status Register: %08x\n", bus_r(STATUS_REG)));
FILE_LOG(logINFO, ("looka t me Register: %08x\n", bus_r(LOOK_AT_ME_REG)));
}else
FILE_LOG(logINFO, ("Memory already mapped before\n"));
return OK;

View File

@ -12,8 +12,6 @@ typedef enum{
}intType;
int bindSocket(unsigned short int port_number);
int acceptConnection(int socketDescriptor);
void closeConnection(int file_Des);

View File

@ -0,0 +1,113 @@
#pragma once
#include "logger.h"
#include "sls_detector_defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
int udpSockfd = -1;
struct addrinfo* udpServerAddrInfo = 0;
unsigned short int udpDestinationPort = 0;
char udpDestinationIp[MAX_STR_LENGTH] = "";
//DEFAULT_TX_UDP_PORT;// src port
int setUDPDestinationDetails(const char* ip, unsigned short int port) {
udpDestinationPort = port;
size_t len = strlen(ip);
strncpy(udpDestinationIp, ip, len > MAX_STR_LENGTH ? MAX_STR_LENGTH : len );
if (udpServerAddrInfo) {
freeaddrinfo(udpServerAddrInfo);
udpServerAddrInfo = 0;
}
// convert ip to internet address
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_flags = 0;
hints.ai_protocol = 0;
char sport[100];
memset(sport, 0, 100);
sprintf(sport, "%d", udpDestinationPort);
int err = getaddrinfo(udpDestinationIp, sport, &hints, &udpServerAddrInfo);
if (err != 0) {
FILE_LOG(logERROR, ("Failed to resolve remote socket address %s at port %d. "
"(Error code:%d, %s)\n", udpDestinationIp, udpDestinationPort, err, gai_strerror(err)));
return FAIL;
}
if (udpServerAddrInfo == NULL) {
FILE_LOG(logERROR, ("Failed to resolve remote socket address %s at port %d "
"(getaddrinfo returned NULL)\n", udpDestinationIp, udpDestinationPort));
udpServerAddrInfo = 0;
return FAIL;
}
return OK;
}
int createUDPSocket() {
if (!strlen(udpDestinationIp)) {
FILE_LOG(logERROR, ("No destination UDP ip specified.\n"));
return FAIL;
}
if (udpSockfd != -1) {
FILE_LOG(logERROR, ("Strange that Udp socket was still open. Closing it to create a new one\n"));
close(udpSockfd);
udpSockfd = -1;
}
// Creating socket file descriptor
udpSockfd = socket(udpServerAddrInfo->ai_family, udpServerAddrInfo->ai_socktype, udpServerAddrInfo->ai_protocol);
if (udpSockfd == -1 ) {
FILE_LOG(logERROR, ("UDP socket at port %d failed. (Error code:%d, %s)\n",
udpDestinationPort, errno, gai_strerror(errno)));
return FAIL;
}
FILE_LOG(logINFO, ("Udp client socket created for server (port %d, ip:%s)\n",
udpDestinationPort, udpDestinationIp));
// connecting allows to use "send/write" instead of "sendto", avoiding checking for server address for each packet
// using write without a connect will end in segv
if (connect(udpSockfd,udpServerAddrInfo->ai_addr, udpServerAddrInfo->ai_addrlen)==-1) {
FILE_LOG(logERROR, ("Could not connect to UDP server at ip:%s, port:%d. (Error code:%d, %s)\n",
udpDestinationIp, udpDestinationPort, errno, gai_strerror(errno)));
}
FILE_LOG(logINFO, ("Udp client socket connected\n",
udpDestinationPort, udpDestinationIp));
return OK;
}
int sendUDPPacket(const char* buf, int length) {
int n = write(udpSockfd, buf, length);
// udp sends atomically, no need to handle partial data
if (n == -1) {
FILE_LOG(logERROR, ("Could not send udp packet. (Error code:%d, %s)\n",
n, errno, gai_strerror(errno)));
} else {
FILE_LOG(logDEBUG2, ("%d bytes sent\n", n));
}
return n;
}
void closeUDPSocket() {
close(udpSockfd);
udpSockfd = -1;
if (udpServerAddrInfo) {
freeaddrinfo(udpServerAddrInfo);
udpServerAddrInfo = 0;
}
}

View File

@ -326,7 +326,7 @@ enum runStatus getRunStatus();
void readFrame(int *ret, char *mess);
#if defined(CHIPTESTBOARDD) || defined(MOENCHD)
void unsetFifoReadStrobes();
void readSample();
void readSample(int ns);
int checkDataPresent();
int readFrameFromFifo();
#endif

View File

@ -3,6 +3,13 @@
#include "communication_funcs.h"
#include "logger.h"
#if defined(CHIPTESTBOARDD) || defined(MOENCHD)
#include "communication_funcs_UDP.h"
#include "UDPPacketHeaderGenerator.h"
extern uint64_t udpFrameNumber;
extern uint32_t udpPacketNumber;
#endif
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
@ -37,8 +44,7 @@ int sockfd = 0;
int debugflag = 0;
#if defined(CHIPTESTBOARDD) || defined(MOENCHD)
int dataBytes = 0;
uint16_t *ramValues = 0;
int nframes = 0;
char* ramValues = 0;
#endif
// Local variables
@ -1372,9 +1378,6 @@ int start_acquisition(int file_des) {
FILE_LOG(logDEBUG1, ("Starting Acquisition\n"));
// only set
if (Server_VerifyLock() == OK) {
#if defined(CHIPTESTBOARDD) || defined(MOENCHD)
nframes = 0;
#endif
ret = startStateMachine();
if (ret == FAIL) {
sprintf(mess, "Could not start acquisition\n");
@ -1456,12 +1459,9 @@ int start_and_read_all(int file_des) {
FILE_LOG(logDEBUG1, ("Starting Acquisition and read all frames\n"));
// start state machine
FILE_LOG(logDEBUG1, ("Stopping Acquisition\n"));
FILE_LOG(logDEBUG1, ("Starting Acquisition\n"));
// only set
if (Server_VerifyLock() == OK) {
#if defined(CHIPTESTBOARDD) || defined(MOENCHD)
nframes = 0;
#endif
ret = startStateMachine();
if (ret == FAIL) {
sprintf(mess, "Could not start acquisition\n");
@ -1492,14 +1492,34 @@ int read_all(int file_des) {
#if defined(CHIPTESTBOARDD) || defined(MOENCHD)
// read from fifo enabled
if (!sendUDP(-1)) {
FILE_LOG(logDEBUG1, ("Reading from 1G UDP\n"));
if (setUDPDestinationDetails("129.129.205.171", 50001) == OK) { // 10g,1 g
if (createUDPSocket() == OK) {
char buffer[UDP_PACKET_MAX_DATA_BYTES + sizeof(sls_detector_header)];
createUDPPacketHeader(buffer, getHardwareSerialNumber());
calculateDataBytesPerSample();
// keep reading frames
while(readFrameFromFifo() == OK) {
int bytesToSend = 0;
int n = 0;
// fill packet with pnum, nsamples per packet and data
while((bytesToSend = fillUDPPacket(buffer))) {
n += sendUDPPacket(buffer, bytesToSend);
}
if (n >= dataBytes)
FILE_LOG(logINFO, (" Frame %lld sent (%d packets, %d databytes, n:%d bytes sent)\n",
udpFrameNumber, udpPacketNumber + 1, dataBytes, n));
}
closeUDPSocket();
}
}
// keep reading frames
while(readFrameFromFifo() == OK) {
// (to the receiver)
Server_SendResult(file_des, INT32, NO_UPDATE, ramValues, dataBytes);// (or get as arg first)send number of bytes (dataBytes) first //FIXME
FILE_LOG(logDEBUG1, ("Frame %d sent\n", nframes));
++nframes;
}
// finished readng frames
// frames left to give status
@ -1515,9 +1535,13 @@ int read_all(int file_des) {
Server_SendResult(file_des, INT32, UPDATE, NULL, 0); // to the client
}
// read from receiver
else
#endif
else {
FILE_LOG(logDEBUG1, ("Reading via UDP\n"));
readFrame(&ret, mess);
}
#else
readFrame(&ret, mess);
#endif
}
return Server_SendResult(file_des, INT32, UPDATE, NULL, 0);
}
@ -2541,7 +2565,7 @@ int set_ctb_pattern(int file_des) {
#else
int addr = (int)args[0];
uint64_t word = args[1];
FILE_LOG(logDEBUG1, ("addr:0x%x word:0x%llx\n", addr, word));
FILE_LOG(logDEBUG1, (" addr:0x%x word:0x%llx\n", addr, word));
if ((word == -1) || (Server_VerifyLock() == OK)) {
@ -2558,21 +2582,21 @@ int set_ctb_pattern(int file_des) {
switch (addr) {
case -1:
strcpy(tempName, "Pattern (I/O Control Register)");
FILE_LOG(logDEBUG1, ("Setting %s to 0x%llx\n", tempName, (long long int) word));
FILE_LOG(logDEBUG1, (" Setting %s, word to 0x%llx\n", tempName, (long long int) word));
retval64 = writePatternIOControl(word);
break;
case -2:
strcpy(tempName, "Pattern (Clock Control Register)");
FILE_LOG(logDEBUG1, ("Setting %s to 0x%llx\n", tempName, (long long int) word));
FILE_LOG(logDEBUG1, (" Setting %s, word to 0x%llx\n", tempName, (long long int) word));
retval64 = writePatternClkControl(word);
break;
default:
sprintf(tempName, "Pattern (Word, addr:0x%x)", addr);
FILE_LOG(logDEBUG1, ("Setting %s to 0x%llx\n", tempName, (long long int) word));
FILE_LOG(logDEBUG1, (" Setting %s, word to 0x%llx\n", tempName, (long long int) word));
retval64 = writePatternWord(addr, word);
break;
}
FILE_LOG(logDEBUG1, ("%s: 0x%llx\n", tempName, (long long int)retval64));
FILE_LOG(logDEBUG1, (" %s: 0x%llx\n", tempName, (long long int)retval64));
validate64(word, retval64, tempName, HEX);
}
}
@ -2598,7 +2622,7 @@ int set_ctb_pattern(int file_des) {
int startAddr = (int)args[1];
int stopAddr = (int)args[2];
int numLoops = (int)args[3];
FILE_LOG(logDEBUG1, ("loopLevel:%d startAddr:0x%x stopAddr:0x%x numLoops:%d word:0x%llx\n", loopLevel, startAddr, stopAddr, numLoops));
FILE_LOG(logDEBUG1, (" loopLevel:%d startAddr:0x%x stopAddr:0x%x numLoops:%d\n", loopLevel, startAddr, stopAddr, numLoops));
if (loopLevel < -1 || loopLevel > 2) { // -1 complete pattern
ret = FAIL;
@ -2607,11 +2631,11 @@ int set_ctb_pattern(int file_des) {
}
// level 0-2, addr upto patternlength + 1
else if ((loopLevel != -1) && (startAddr > (MAX_PATTERN_LENGTH + 1) || stopAddr > (MAX_PATTERN_LENGTH + 1))) {
else if ((loopLevel != -1) && (startAddr > MAX_PATTERN_LENGTH || stopAddr > MAX_PATTERN_LENGTH )) {
ret = FAIL;
sprintf(mess, "Cannot set Pattern (Pattern Loop, level:%d, startaddr:0x%x, stopaddr:0x%x). "
"Addr must be less than 0x%x\n",
loopLevel, startAddr, stopAddr, MAX_PATTERN_LENGTH + 1);
loopLevel, startAddr, stopAddr, MAX_PATTERN_LENGTH);
FILE_LOG(logERROR, (mess));
}
@ -2650,26 +2674,26 @@ int set_ctb_pattern(int file_des) {
#else
int loopLevel = (int)args[0];
int addr = (int)args[1];
FILE_LOG(logDEBUG1, ("loopLevel:%d addr:0x%x\n", loopLevel, addr));
FILE_LOG(logDEBUG1, (" loopLevel:%d addr:0x%x\n", loopLevel, addr));
if ((addr == -1) || (Server_VerifyLock() == OK)) {
if (loopLevel < 0 || loopLevel > 2) {
ret = FAIL;
sprintf(mess, "Pattern (Wait Address) Level (0x%x) is not implemented for this detector\n", loopLevel);
FILE_LOG(logERROR,(mess));
} else if (addr > (MAX_PATTERN_LENGTH + 1)) {
} else if (addr > MAX_PATTERN_LENGTH) {
ret = FAIL;
sprintf(mess, "Cannot set Pattern (Wait Address, addr:0x%x). Addr must be less than 0x%x\n",
addr, MAX_PATTERN_LENGTH + 1);
addr, MAX_PATTERN_LENGTH);
FILE_LOG(logERROR, (mess));
} else {
char tempName[100];
memset(tempName, 0, 100);
sprintf(tempName, "Pattern (Wait Address, Level:%d)", loopLevel);
FILE_LOG(logDEBUG1, ("Setting %s to 0x%x\n", tempName, addr));
FILE_LOG(logDEBUG1, (" Setting %s to 0x%x\n", tempName, addr));
retval32 = setPatternWaitAddress(loopLevel, addr);
FILE_LOG(logDEBUG1, ("%s: 0x%x\n", tempName, retval32));
FILE_LOG(logDEBUG1, (" %s: 0x%x\n", tempName, retval32));
validate(addr, retval32, tempName, HEX);
}
}
@ -2693,7 +2717,7 @@ int set_ctb_pattern(int file_des) {
#else
int loopLevel = (int)args[0];
uint64_t timeval = args[1];
FILE_LOG(logDEBUG1, ("loopLevel:%d timeval:0x%lld\n", loopLevel, timeval));
FILE_LOG(logDEBUG1, (" loopLevel:%d timeval:0x%lld\n", loopLevel, timeval));
if ((timeval == -1) || (Server_VerifyLock() == OK)) {
if (loopLevel < 0 || loopLevel > 2) {
@ -2705,9 +2729,9 @@ int set_ctb_pattern(int file_des) {
memset(tempName, 0, 100);
sprintf(tempName, "Pattern (Wait Time, Level:%d)", loopLevel);
FILE_LOG(logDEBUG1, ("Setting %s to 0x%lld\n", tempName, (long long int)timeval));
FILE_LOG(logDEBUG1, (" Setting %s to 0x%lld\n", tempName, (long long int)timeval));
retval64 = setPatternWaitTime(loopLevel, timeval);
FILE_LOG(logDEBUG1, ("%s: 0x%lld\n", tempName, (long long int)retval64));
FILE_LOG(logDEBUG1, (" %s: 0x%lld\n", tempName, (long long int)retval64));
validate64(timeval, retval64, tempName, HEX);
}
}