mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-04-24 15:20:02 +02:00
Merge branch 'refactor' of github.com:slsdetectorgroup/slsDetectorPackage into refactor
This commit is contained in:
commit
d96cbe02a2
@ -1 +1 @@
|
|||||||
../../slsSupportLib/include/communication_funcs.c
|
../slsDetectorServer/communication_funcs.c
|
@ -1 +1 @@
|
|||||||
../../slsSupportLib/include/communication_funcs.h
|
../slsDetectorServer/communication_funcs.h
|
@ -1,21 +1,18 @@
|
|||||||
CROSS = bfin-uclinux-
|
CROSS = bfin-uclinux-
|
||||||
CC = $(CROSS)gcc
|
CC = $(CROSS)gcc
|
||||||
|
|
||||||
CFLAGS += -Wall -DGOTTHARDD -DDEBUG # -DVERBOSE #-DVERYVERBOSE #-DVIRTUAL
|
CFLAGS += -Wall -DGOTTHARDD -DDEBUG # -DVERBOSE #-DVERYVERBOSE #-DVIRTUAL
|
||||||
|
|
||||||
|
|
||||||
PROGS= gotthardDetectorServer
|
PROGS= gotthardDetectorServer
|
||||||
INSTDIR= /tftpboot
|
INSTDIR= /tftpboot
|
||||||
INSTMODE= 0777
|
INSTMODE= 0777
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BINS = testlib_sharedlibc
|
BINS = testlib_sharedlibc
|
||||||
SRCS = server.c firmware_funcs.c server_funcs.c communication_funcs.c
|
SRCS = server.c firmware_funcs.c server_funcs.c communication_funcs.c
|
||||||
OBJS = $(SRCS:%.c=%.o)
|
OBJS = $(SRCS:%.c=%.o)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
all: clean versioning $(PROGS)
|
all: clean versioning $(PROGS)
|
||||||
|
|
||||||
boot: $(OBJS)
|
boot: $(OBJS)
|
||||||
@ -24,8 +21,8 @@ versioning:
|
|||||||
@echo `tput setaf 6; ./updateGitVersion.sh; tput sgr0;`
|
@echo `tput setaf 6; ./updateGitVersion.sh; tput sgr0;`
|
||||||
|
|
||||||
$(PROGS): $(OBJS)
|
$(PROGS): $(OBJS)
|
||||||
# echo $(OBJS)
|
echo $(OBJS)
|
||||||
$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@)
|
$(CC) $(CFLAGS) -o $@ $^ $(LDLIBS_$@) $(LDFLAGS_$@)
|
||||||
rm gotthardDetectorServer.gdb
|
rm gotthardDetectorServer.gdb
|
||||||
|
|
||||||
|
|
||||||
|
@ -1 +1 @@
|
|||||||
../../slsSupportLib/include/communication_funcs.c
|
../slsDetectorServer/communication_funcs.c
|
@ -1 +1 @@
|
|||||||
../../slsSupportLib/include/communication_funcs.h
|
../slsDetectorServer/communication_funcs.h
|
@ -1 +1 @@
|
|||||||
../../slsSupportLib/include/communication_funcs.c
|
../slsDetectorServer/communication_funcs.c
|
@ -1 +1 @@
|
|||||||
../../slsSupportLib/include/communication_funcs.h
|
../slsDetectorServer/communication_funcs.h
|
@ -1 +0,0 @@
|
|||||||
../commonFiles/communication_funcs.c
|
|
570
slsDetectorServers/slsDetectorServer/communication_funcs.c
Executable file
570
slsDetectorServers/slsDetectorServer/communication_funcs.c
Executable file
@ -0,0 +1,570 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include "communication_funcs.h"
|
||||||
|
//#include <sys/socket.h>
|
||||||
|
#include <netinet/tcp.h> /* for TCP_NODELAY */
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <sys/time.h>
|
||||||
|
char lastClientIP[INET_ADDRSTRLEN];
|
||||||
|
char thisClientIP[INET_ADDRSTRLEN];
|
||||||
|
int lockStatus;
|
||||||
|
int differentClients;
|
||||||
|
|
||||||
|
//int socketDescriptor, file_des;
|
||||||
|
const int send_rec_max_size=SEND_REC_MAX_SIZE;
|
||||||
|
extern int errno;
|
||||||
|
|
||||||
|
|
||||||
|
char dummyClientIP[INET_ADDRSTRLEN];
|
||||||
|
|
||||||
|
|
||||||
|
fd_set readset, tempset;
|
||||||
|
int isock=0, maxfd;
|
||||||
|
|
||||||
|
|
||||||
|
int myport=-1;
|
||||||
|
|
||||||
|
//struct sockaddr_in address;
|
||||||
|
//#define VERBOSE
|
||||||
|
|
||||||
|
|
||||||
|
int bindSocket(unsigned short int port_number) {
|
||||||
|
int i;
|
||||||
|
|
||||||
|
struct sockaddr_in addressS;
|
||||||
|
int socketDescriptor;
|
||||||
|
//int file_des;
|
||||||
|
|
||||||
|
//file_des= -1;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (myport==port_number)
|
||||||
|
return -10;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp
|
||||||
|
|
||||||
|
//socketDescriptor = socket(PF_INET, SOCK_STREAM, 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (socketDescriptor < 0) {
|
||||||
|
printf("Can not create socket\n");
|
||||||
|
} else {
|
||||||
|
|
||||||
|
i = 1;
|
||||||
|
setsockopt(socketDescriptor, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
|
||||||
|
// setsockopt(socketDescriptor, IPPROTO_TCP, TCP_NODELAY, (char *) &i, sizeof(i));
|
||||||
|
// TCP_CORK
|
||||||
|
|
||||||
|
// Set some fields in the serverAddress structure.
|
||||||
|
addressS.sin_family = AF_INET;
|
||||||
|
addressS.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||||
|
addressS.sin_port = htons(port_number);
|
||||||
|
|
||||||
|
// memset(&address.sin_addr, 0, sizeof(address.sin_addr));
|
||||||
|
|
||||||
|
|
||||||
|
if(bind(socketDescriptor,(struct sockaddr *) &addressS,sizeof(addressS))<0){
|
||||||
|
|
||||||
|
printf("Can not create socket\n");
|
||||||
|
|
||||||
|
socketDescriptor=-1;
|
||||||
|
} else {
|
||||||
|
if (listen(socketDescriptor, 5)==0) {
|
||||||
|
|
||||||
|
if (isock==0) {
|
||||||
|
FD_ZERO(&readset);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
FD_SET(socketDescriptor, &readset);
|
||||||
|
isock++;
|
||||||
|
maxfd = socketDescriptor;
|
||||||
|
printf ("%d port %d fd %d\n",isock, port_number,socketDescriptor);
|
||||||
|
myport=port_number;
|
||||||
|
} else
|
||||||
|
printf("error on listen");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//int getrlimit(int resource, struct rlimit *rlim);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return socketDescriptor;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int getServerError(int socketDescriptor)
|
||||||
|
{
|
||||||
|
if (socketDescriptor<0) return 1;
|
||||||
|
else return 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
int acceptConnection(int socketDescriptor) {
|
||||||
|
|
||||||
|
|
||||||
|
int j;
|
||||||
|
|
||||||
|
|
||||||
|
struct sockaddr_in addressC;
|
||||||
|
int file_des=-1;
|
||||||
|
struct timeval tv;
|
||||||
|
int result;
|
||||||
|
|
||||||
|
|
||||||
|
//socklen_t address_length;
|
||||||
|
socklen_t address_length=sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
|
if (socketDescriptor<0)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memcpy(&tempset, &readset, sizeof(tempset));
|
||||||
|
tv.tv_sec = 10000000;
|
||||||
|
tv.tv_usec = 0;
|
||||||
|
result = select(maxfd + 1, &tempset, NULL, NULL, &tv);
|
||||||
|
|
||||||
|
if (result == 0) {
|
||||||
|
printf("select() timed out!\n");
|
||||||
|
} else if (result < 0 && errno != EINTR) {
|
||||||
|
printf("Error in select(): %s\n", strerror(errno));
|
||||||
|
} else if (result > 0) {
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("select returned!\n");
|
||||||
|
#endif
|
||||||
|
for (j=0; j<maxfd+1; j++) {
|
||||||
|
if (FD_ISSET(j, &tempset)) {
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("fd %d is set\n",j);
|
||||||
|
#endif
|
||||||
|
FD_CLR(j, &tempset);
|
||||||
|
|
||||||
|
|
||||||
|
if ((file_des = accept(j,(struct sockaddr *) &addressC, &address_length)) < 0) {
|
||||||
|
printf("Error in accept(): %s\n", strerror(errno));
|
||||||
|
printf("Error: with server accept, connection refused %d\n", errno);
|
||||||
|
|
||||||
|
|
||||||
|
switch(errno) {
|
||||||
|
case EWOULDBLOCK:
|
||||||
|
printf("ewouldblock eagain\n");
|
||||||
|
break;
|
||||||
|
case EBADF:
|
||||||
|
printf("ebadf\n");
|
||||||
|
break;
|
||||||
|
case ECONNABORTED:
|
||||||
|
printf("econnaborted\n");
|
||||||
|
break;
|
||||||
|
case EFAULT:
|
||||||
|
printf("efault\n");
|
||||||
|
break;
|
||||||
|
case EINTR:
|
||||||
|
printf("eintr\n");
|
||||||
|
break;
|
||||||
|
case EINVAL:
|
||||||
|
printf("einval\n");
|
||||||
|
break;
|
||||||
|
case EMFILE:
|
||||||
|
printf("emfile\n");
|
||||||
|
break;
|
||||||
|
case ENFILE:
|
||||||
|
printf("enfile\n");
|
||||||
|
break;
|
||||||
|
case ENOTSOCK:
|
||||||
|
printf("enotsock\n");
|
||||||
|
break;
|
||||||
|
case EOPNOTSUPP:
|
||||||
|
printf("eOPNOTSUPP\n");
|
||||||
|
break;
|
||||||
|
case ENOBUFS:
|
||||||
|
printf("ENOBUFS\n");
|
||||||
|
break;
|
||||||
|
case ENOMEM:
|
||||||
|
printf("ENOMEM\n");
|
||||||
|
break;
|
||||||
|
case ENOSR:
|
||||||
|
printf("ENOSR\n");
|
||||||
|
break;
|
||||||
|
case EPROTO:
|
||||||
|
printf("EPROTO\n");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
printf("unknown error\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// should remove descriptor
|
||||||
|
|
||||||
|
socketDescriptor=-1;
|
||||||
|
} else {
|
||||||
|
|
||||||
|
inet_ntop(AF_INET, &(addressC.sin_addr), dummyClientIP, INET_ADDRSTRLEN);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("connection accepted %d\n",file_des);
|
||||||
|
#endif
|
||||||
|
FD_SET(file_des, &readset);
|
||||||
|
maxfd = (maxfd < file_des)?file_des:maxfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return file_des;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void closeConnection(int file_des) {
|
||||||
|
#ifdef VERY_VERBOSE
|
||||||
|
#endif
|
||||||
|
if(file_des>=0)
|
||||||
|
close(file_des);
|
||||||
|
FD_CLR(file_des, &readset);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exitServer(int socketDescriptor) {
|
||||||
|
if (socketDescriptor>=0)
|
||||||
|
close(socketDescriptor);
|
||||||
|
#ifdef VERY_VERBOSE
|
||||||
|
printf("Closing server\n");
|
||||||
|
#endif
|
||||||
|
FD_CLR(socketDescriptor, &readset);
|
||||||
|
socketDescriptor=-1;
|
||||||
|
isock--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void swapData(void* val,int length,intType itype){
|
||||||
|
int i;
|
||||||
|
int16_t* c= (int16_t*)val;
|
||||||
|
int32_t* a= (int32_t*)val;
|
||||||
|
int64_t* b= (int64_t*)val;
|
||||||
|
for(i=0; length > 0; i++){
|
||||||
|
switch(itype){
|
||||||
|
case INT16:
|
||||||
|
c[i] = ((c[i] & 0x00FF) << 8) | ((c[i] & 0xFF00) >> 8);
|
||||||
|
length -= sizeof(int16_t);
|
||||||
|
break;
|
||||||
|
case INT32:
|
||||||
|
a[i]=((a[i] << 8) & 0xFF00FF00) | ((a[i] >> 8) & 0xFF00FF );
|
||||||
|
a[i]=(a[i] << 16) | ((a[i] >> 16) & 0xFFFF);
|
||||||
|
length -= sizeof(int32_t);
|
||||||
|
break;
|
||||||
|
case INT64:
|
||||||
|
b[i] = ((b[i] << 8) & 0xFF00FF00FF00FF00ULL ) | ((b[i] >> 8) & 0x00FF00FF00FF00FFULL );
|
||||||
|
b[i] = ((b[i] << 16) & 0xFFFF0000FFFF0000ULL ) | ((b[i] >> 16) & 0x0000FFFF0000FFFFULL );
|
||||||
|
b[i] = (b[i] << 32) | ((b[i] >> 32) & 0xFFFFFFFFULL);
|
||||||
|
length -= sizeof(int64_t);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
length = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int sendData(int file_des, void* buf,int length, intType itype){
|
||||||
|
#ifndef PCCOMPILE
|
||||||
|
#ifdef EIGERD
|
||||||
|
swapData(buf, length, itype);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
return sendDataOnly(file_des, buf, length);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int receiveData(int file_des, void* buf,int length, intType itype){
|
||||||
|
int ret = receiveDataOnly(file_des, buf, length);
|
||||||
|
#ifndef PCCOMPILE
|
||||||
|
#ifdef EIGERD
|
||||||
|
if (ret >= 0) swapData(buf, length, itype);
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int sendDataOnly(int file_des, void* buf,int length) {
|
||||||
|
if (!length)
|
||||||
|
return 0;
|
||||||
|
int ret = write(file_des, buf, length); //value of -1 is other end socket crash as sigpipe is ignored
|
||||||
|
if (ret < 0) cprintf(BG_RED, "Error writing to socket. Possible socket crash\n");
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int receiveDataOnly(int file_des, void* buf,int length) {
|
||||||
|
|
||||||
|
int total_received=0;
|
||||||
|
int nreceiving;
|
||||||
|
int nreceived;
|
||||||
|
if (file_des<0) return -1;
|
||||||
|
#ifdef VERY_VERBOSE
|
||||||
|
printf("want to receive %d Bytes\n", length);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
while(length > 0) {
|
||||||
|
nreceiving = (length>send_rec_max_size) ? send_rec_max_size:length; // (condition) ? if_true : if_false
|
||||||
|
nreceived = read(file_des,(char*)buf+total_received,nreceiving);
|
||||||
|
if(!nreceived){
|
||||||
|
if(!total_received) {
|
||||||
|
return -1; //to handle it
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
length-=nreceived;
|
||||||
|
total_received+=nreceived;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (total_received>0)
|
||||||
|
strcpy(thisClientIP,dummyClientIP);
|
||||||
|
|
||||||
|
if (strcmp(lastClientIP,thisClientIP))
|
||||||
|
differentClients=1;
|
||||||
|
else
|
||||||
|
differentClients=0;
|
||||||
|
|
||||||
|
return total_received;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int sendModule(int file_des, sls_detector_module *myMod) {
|
||||||
|
return sendModuleGeneral(file_des, myMod, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int sendModuleGeneral(int file_des, sls_detector_module *myMod, int sendAll) {
|
||||||
|
int ts=0;
|
||||||
|
#ifdef VERBOSE
|
||||||
|
int idac;
|
||||||
|
#endif
|
||||||
|
int nChips=myMod->nchip;
|
||||||
|
int nChans=myMod->nchan;
|
||||||
|
int nAdcs=myMod->nadc;
|
||||||
|
int nDacs=myMod->ndac;
|
||||||
|
//ts+= sendDataOnly(file_des,myMod,sizeof(sls_detector_module));
|
||||||
|
ts+=sendData(file_des,&(myMod->serialnumber),sizeof(myMod->serialnumber),INT32);
|
||||||
|
ts+=sendData(file_des,&(myMod->nchan),sizeof(myMod->nchan),INT32);
|
||||||
|
ts+=sendData(file_des,&(myMod->nchip),sizeof(myMod->nchip),INT32);
|
||||||
|
ts+=sendData(file_des,&(myMod->ndac),sizeof(myMod->ndac),INT32);
|
||||||
|
ts+=sendData(file_des,&(myMod->nadc),sizeof(myMod->nadc),INT32);
|
||||||
|
ts+=sendData(file_des,&(myMod->reg),sizeof(myMod->reg),INT32);
|
||||||
|
ts+=sendData(file_des,&(myMod->gain), sizeof(myMod->gain),OTHER);
|
||||||
|
ts+=sendData(file_des,&(myMod->offset), sizeof(myMod->offset),OTHER);
|
||||||
|
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("module of size %d sent\n",ts);
|
||||||
|
#endif
|
||||||
|
ts+= sendData(file_des,myMod->dacs,sizeof(int)*nDacs,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("dacs of size %d sent\n",ts);
|
||||||
|
int idac;
|
||||||
|
for (idac=0; idac< nDacs; idac++)
|
||||||
|
printf("dac %d is %d\n",idac,(int)myMod->dacs[idac]);
|
||||||
|
#endif
|
||||||
|
ts+= sendData(file_des,myMod->adcs,sizeof(int)*nAdcs,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("adcs of size %d sent\n", ts);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/*some detectors dont require sending all trimbits etc.*/
|
||||||
|
if(sendAll){
|
||||||
|
ts+=sendData(file_des,myMod->chipregs,sizeof(int)*nChips,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("chips of size %d sent\n", ts);
|
||||||
|
#endif
|
||||||
|
ts+=sendData(file_des,myMod->chanregs,sizeof(int)*nChans,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("chans of size %d sent - %d\n", ts, myMod->nchan);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("module of size %d sent register %x\n", ts, myMod->reg);
|
||||||
|
#endif
|
||||||
|
return ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int receiveModule(int file_des, sls_detector_module* myMod) {
|
||||||
|
return receiveModuleGeneral(file_des,myMod,1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int receiveModuleGeneral(int file_des, sls_detector_module* myMod, int receiveAll) {
|
||||||
|
int ts=0;
|
||||||
|
int *dacptr=myMod->dacs;
|
||||||
|
int *adcptr=myMod->adcs;
|
||||||
|
int *chipptr=myMod->chipregs, *chanptr=myMod->chanregs;
|
||||||
|
int nChips, nchipold=myMod->nchip, nchipdiff;
|
||||||
|
int nChans, nchanold=myMod->nchan, nchandiff;
|
||||||
|
int nDacs, ndold=myMod->ndac, ndacdiff;
|
||||||
|
int nAdcs, naold=myMod->nadc, nadcdiff;
|
||||||
|
ts+=receiveData(file_des,&(myMod->serialnumber),sizeof(myMod->serialnumber),INT32);
|
||||||
|
ts+=receiveData(file_des,&(myMod->nchan),sizeof(myMod->nchan),INT32);
|
||||||
|
ts+=receiveData(file_des,&(myMod->nchip),sizeof(myMod->nchip),INT32);
|
||||||
|
ts+=receiveData(file_des,&(myMod->ndac),sizeof(myMod->ndac),INT32);
|
||||||
|
ts+=receiveData(file_des,&(myMod->nadc),sizeof(myMod->nadc),INT32);
|
||||||
|
ts+=receiveData(file_des,&(myMod->reg),sizeof(myMod->reg),INT32);
|
||||||
|
ts+=receiveData(file_des,&(myMod->gain), sizeof(myMod->gain),OTHER);
|
||||||
|
ts+=receiveData(file_des,&(myMod->offset), sizeof(myMod->offset),OTHER);
|
||||||
|
|
||||||
|
myMod->dacs=dacptr;
|
||||||
|
myMod->adcs=adcptr;
|
||||||
|
myMod->chipregs=chipptr;
|
||||||
|
myMod->chanregs=chanptr;
|
||||||
|
|
||||||
|
#ifdef EIGERD
|
||||||
|
//feature to exclude sending of trimbtis, nchips=0,nchans=0 in that case
|
||||||
|
if(myMod->nchip == 0 && myMod->nchan == 0) {
|
||||||
|
receiveAll=0;
|
||||||
|
nchipold=0;
|
||||||
|
nchanold=0;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
nChips=myMod->nchip;
|
||||||
|
nchipdiff=nChips-nchipold;
|
||||||
|
if (nchipold!=nChips) {
|
||||||
|
printf("received wrong number of chips\n");
|
||||||
|
}
|
||||||
|
#ifdef VERBOSE
|
||||||
|
else
|
||||||
|
printf("received %d chips\n",nChips);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
nChans=myMod->nchan;
|
||||||
|
nchandiff=nChans-nchanold;
|
||||||
|
if (nchanold!=nChans) {
|
||||||
|
printf("received wrong number of channels\n");
|
||||||
|
}
|
||||||
|
#ifdef VERBOSE
|
||||||
|
else
|
||||||
|
printf("received %d chans\n",nChans);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
nDacs=myMod->ndac;
|
||||||
|
ndacdiff=nDacs-ndold;
|
||||||
|
if (ndold!=nDacs) {
|
||||||
|
printf("received wrong number of dacs\n");
|
||||||
|
}
|
||||||
|
#ifdef VERBOSE
|
||||||
|
else
|
||||||
|
printf("received %d dacs\n",nDacs);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
nAdcs=myMod->nadc;
|
||||||
|
nadcdiff=nAdcs-naold;
|
||||||
|
if (naold!=nAdcs) {
|
||||||
|
printf("received wrong number of adcs\n");
|
||||||
|
}
|
||||||
|
#ifdef VERBOSE
|
||||||
|
else
|
||||||
|
printf("received %d adcs\n",nAdcs);
|
||||||
|
#endif
|
||||||
|
if (ndacdiff<=0) {
|
||||||
|
ts+=receiveData(file_des,myMod->dacs, sizeof(int)*nDacs,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("dacs received\n");
|
||||||
|
int id;
|
||||||
|
for (id=0; id<nDacs; id++)
|
||||||
|
printf("dac %d val %d\n",id, (int)myMod->dacs[id]);
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
dacptr=(int*)malloc(ndacdiff*sizeof(int));
|
||||||
|
myMod->ndac=ndold;
|
||||||
|
ts+=receiveData(file_des,myMod->dacs, sizeof(int)*ndold,INT32);
|
||||||
|
ts+=receiveData(file_des,dacptr, sizeof(int)*ndacdiff,INT32);
|
||||||
|
free(dacptr);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nadcdiff<=0) {
|
||||||
|
ts+=receiveData(file_des,myMod->adcs, sizeof(int)*nAdcs,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("adcs received\n");
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
adcptr=(int*)malloc(nadcdiff*sizeof(int));
|
||||||
|
myMod->nadc=naold;
|
||||||
|
ts+=receiveData(file_des,myMod->adcs, sizeof(int)*naold,INT32);
|
||||||
|
ts+=receiveData(file_des,adcptr, sizeof(int)*nadcdiff,INT32);
|
||||||
|
free(adcptr);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*some detectors dont require sending all trimbits etc.*/
|
||||||
|
if(receiveAll){
|
||||||
|
|
||||||
|
if (nchipdiff<=0) {
|
||||||
|
ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nChips,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("chips received\n");
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
chipptr=(int*)malloc(nchipdiff*sizeof(int));
|
||||||
|
myMod->nchip=nchipold;
|
||||||
|
ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nchipold,INT32);
|
||||||
|
ts+=receiveData(file_des,chipptr, sizeof(int)*nchipdiff,INT32);
|
||||||
|
free(chipptr);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nchandiff<=0) {
|
||||||
|
ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nChans,INT32);
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("chans received\n");
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
chanptr=(int*)malloc(nchandiff*sizeof(int));
|
||||||
|
myMod->nchan=nchanold;
|
||||||
|
ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nchanold,INT32);
|
||||||
|
ts+=receiveData(file_des,chanptr, sizeof(int)*nchandiff,INT32);
|
||||||
|
free(chanptr);
|
||||||
|
return FAIL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef VERBOSE
|
||||||
|
printf("received module of size %d register %x\n",ts,myMod->reg);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return ts;
|
||||||
|
}
|
@ -1 +0,0 @@
|
|||||||
../commonFiles/communication_funcs.h
|
|
47
slsDetectorServers/slsDetectorServer/communication_funcs.h
Executable file
47
slsDetectorServers/slsDetectorServer/communication_funcs.h
Executable file
@ -0,0 +1,47 @@
|
|||||||
|
#ifndef COMMUNICATION_FUNCS_H
|
||||||
|
#define COMMUNICATION_FUNCS_H
|
||||||
|
|
||||||
|
#define SEND_REC_MAX_SIZE 4096
|
||||||
|
#define DEFAULT_PORTNO 1952
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <netinet/in.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#include "sls_detector_defs.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
typedef enum{
|
||||||
|
INT16,
|
||||||
|
INT32,
|
||||||
|
INT64,
|
||||||
|
OTHER
|
||||||
|
}intType;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
int bindSocket(unsigned short int port_number);
|
||||||
|
int acceptConnection(int socketDescriptor);
|
||||||
|
void closeConnection(int file_Des);
|
||||||
|
void exitServer(int socketDescriptor);
|
||||||
|
|
||||||
|
void swapData(void* val,int length,intType itype);
|
||||||
|
int sendData(int file_des, void* buf,int length, intType itype);
|
||||||
|
int receiveData(int file_des, void* buf,int length, intType itype);
|
||||||
|
int sendDataOnly(int file_des, void* buf,int length);
|
||||||
|
int receiveDataOnly(int file_des, void* buf,int length);
|
||||||
|
|
||||||
|
|
||||||
|
int getServerError(int socketDescriptor);
|
||||||
|
int sendModule(int file_des, sls_detector_module *myMod);
|
||||||
|
int sendModuleGeneral(int file_des, sls_detector_module *myMod, int sendAll);
|
||||||
|
int receiveModule(int file_des, sls_detector_module* myMod);
|
||||||
|
int receiveModuleGeneral(int file_des, sls_detector_module* myMod, int receiveAll);
|
||||||
|
|
||||||
|
#endif
|
@ -1,570 +0,0 @@
|
|||||||
|
|
||||||
|
|
||||||
#include "communication_funcs.h"
|
|
||||||
//#include <sys/socket.h>
|
|
||||||
#include <netinet/tcp.h> /* for TCP_NODELAY */
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include <sys/time.h>
|
|
||||||
char lastClientIP[INET_ADDRSTRLEN];
|
|
||||||
char thisClientIP[INET_ADDRSTRLEN];
|
|
||||||
int lockStatus;
|
|
||||||
int differentClients;
|
|
||||||
|
|
||||||
//int socketDescriptor, file_des;
|
|
||||||
const int send_rec_max_size=SEND_REC_MAX_SIZE;
|
|
||||||
extern int errno;
|
|
||||||
|
|
||||||
|
|
||||||
char dummyClientIP[INET_ADDRSTRLEN];
|
|
||||||
|
|
||||||
|
|
||||||
fd_set readset, tempset;
|
|
||||||
int isock=0, maxfd;
|
|
||||||
|
|
||||||
|
|
||||||
int myport=-1;
|
|
||||||
|
|
||||||
//struct sockaddr_in address;
|
|
||||||
//#define VERBOSE
|
|
||||||
|
|
||||||
|
|
||||||
int bindSocket(unsigned short int port_number) {
|
|
||||||
int i;
|
|
||||||
|
|
||||||
struct sockaddr_in addressS;
|
|
||||||
int socketDescriptor;
|
|
||||||
//int file_des;
|
|
||||||
|
|
||||||
//file_des= -1;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (myport==port_number)
|
|
||||||
return -10;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp
|
|
||||||
|
|
||||||
//socketDescriptor = socket(PF_INET, SOCK_STREAM, 0);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (socketDescriptor < 0) {
|
|
||||||
printf("Can not create socket\n");
|
|
||||||
} else {
|
|
||||||
|
|
||||||
i = 1;
|
|
||||||
setsockopt(socketDescriptor, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
|
|
||||||
// setsockopt(socketDescriptor, IPPROTO_TCP, TCP_NODELAY, (char *) &i, sizeof(i));
|
|
||||||
// TCP_CORK
|
|
||||||
|
|
||||||
// Set some fields in the serverAddress structure.
|
|
||||||
addressS.sin_family = AF_INET;
|
|
||||||
addressS.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
||||||
addressS.sin_port = htons(port_number);
|
|
||||||
|
|
||||||
// memset(&address.sin_addr, 0, sizeof(address.sin_addr));
|
|
||||||
|
|
||||||
|
|
||||||
if(bind(socketDescriptor,(struct sockaddr *) &addressS,sizeof(addressS))<0){
|
|
||||||
|
|
||||||
printf("Can not create socket\n");
|
|
||||||
|
|
||||||
socketDescriptor=-1;
|
|
||||||
} else {
|
|
||||||
if (listen(socketDescriptor, 5)==0) {
|
|
||||||
|
|
||||||
if (isock==0) {
|
|
||||||
FD_ZERO(&readset);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
FD_SET(socketDescriptor, &readset);
|
|
||||||
isock++;
|
|
||||||
maxfd = socketDescriptor;
|
|
||||||
printf ("%d port %d fd %d\n",isock, port_number,socketDescriptor);
|
|
||||||
myport=port_number;
|
|
||||||
} else
|
|
||||||
printf("error on listen");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//int getrlimit(int resource, struct rlimit *rlim);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return socketDescriptor;
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int getServerError(int socketDescriptor)
|
|
||||||
{
|
|
||||||
if (socketDescriptor<0) return 1;
|
|
||||||
else return 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
int acceptConnection(int socketDescriptor) {
|
|
||||||
|
|
||||||
|
|
||||||
int j;
|
|
||||||
|
|
||||||
|
|
||||||
struct sockaddr_in addressC;
|
|
||||||
int file_des=-1;
|
|
||||||
struct timeval tv;
|
|
||||||
int result;
|
|
||||||
|
|
||||||
|
|
||||||
//socklen_t address_length;
|
|
||||||
socklen_t address_length=sizeof(struct sockaddr_in);
|
|
||||||
|
|
||||||
if (socketDescriptor<0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
memcpy(&tempset, &readset, sizeof(tempset));
|
|
||||||
tv.tv_sec = 10000000;
|
|
||||||
tv.tv_usec = 0;
|
|
||||||
result = select(maxfd + 1, &tempset, NULL, NULL, &tv);
|
|
||||||
|
|
||||||
if (result == 0) {
|
|
||||||
printf("select() timed out!\n");
|
|
||||||
} else if (result < 0 && errno != EINTR) {
|
|
||||||
printf("Error in select(): %s\n", strerror(errno));
|
|
||||||
} else if (result > 0) {
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("select returned!\n");
|
|
||||||
#endif
|
|
||||||
for (j=0; j<maxfd+1; j++) {
|
|
||||||
if (FD_ISSET(j, &tempset)) {
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("fd %d is set\n",j);
|
|
||||||
#endif
|
|
||||||
FD_CLR(j, &tempset);
|
|
||||||
|
|
||||||
|
|
||||||
if ((file_des = accept(j,(struct sockaddr *) &addressC, &address_length)) < 0) {
|
|
||||||
printf("Error in accept(): %s\n", strerror(errno));
|
|
||||||
printf("Error: with server accept, connection refused %d\n", errno);
|
|
||||||
|
|
||||||
|
|
||||||
switch(errno) {
|
|
||||||
case EWOULDBLOCK:
|
|
||||||
printf("ewouldblock eagain\n");
|
|
||||||
break;
|
|
||||||
case EBADF:
|
|
||||||
printf("ebadf\n");
|
|
||||||
break;
|
|
||||||
case ECONNABORTED:
|
|
||||||
printf("econnaborted\n");
|
|
||||||
break;
|
|
||||||
case EFAULT:
|
|
||||||
printf("efault\n");
|
|
||||||
break;
|
|
||||||
case EINTR:
|
|
||||||
printf("eintr\n");
|
|
||||||
break;
|
|
||||||
case EINVAL:
|
|
||||||
printf("einval\n");
|
|
||||||
break;
|
|
||||||
case EMFILE:
|
|
||||||
printf("emfile\n");
|
|
||||||
break;
|
|
||||||
case ENFILE:
|
|
||||||
printf("enfile\n");
|
|
||||||
break;
|
|
||||||
case ENOTSOCK:
|
|
||||||
printf("enotsock\n");
|
|
||||||
break;
|
|
||||||
case EOPNOTSUPP:
|
|
||||||
printf("eOPNOTSUPP\n");
|
|
||||||
break;
|
|
||||||
case ENOBUFS:
|
|
||||||
printf("ENOBUFS\n");
|
|
||||||
break;
|
|
||||||
case ENOMEM:
|
|
||||||
printf("ENOMEM\n");
|
|
||||||
break;
|
|
||||||
case ENOSR:
|
|
||||||
printf("ENOSR\n");
|
|
||||||
break;
|
|
||||||
case EPROTO:
|
|
||||||
printf("EPROTO\n");
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
printf("unknown error\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
// should remove descriptor
|
|
||||||
|
|
||||||
socketDescriptor=-1;
|
|
||||||
} else {
|
|
||||||
|
|
||||||
inet_ntop(AF_INET, &(addressC.sin_addr), dummyClientIP, INET_ADDRSTRLEN);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("connection accepted %d\n",file_des);
|
|
||||||
#endif
|
|
||||||
FD_SET(file_des, &readset);
|
|
||||||
maxfd = (maxfd < file_des)?file_des:maxfd;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return file_des;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void closeConnection(int file_des) {
|
|
||||||
#ifdef VERY_VERBOSE
|
|
||||||
#endif
|
|
||||||
if(file_des>=0)
|
|
||||||
close(file_des);
|
|
||||||
FD_CLR(file_des, &readset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void exitServer(int socketDescriptor) {
|
|
||||||
if (socketDescriptor>=0)
|
|
||||||
close(socketDescriptor);
|
|
||||||
#ifdef VERY_VERBOSE
|
|
||||||
printf("Closing server\n");
|
|
||||||
#endif
|
|
||||||
FD_CLR(socketDescriptor, &readset);
|
|
||||||
socketDescriptor=-1;
|
|
||||||
isock--;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void swapData(void* val,int length,intType itype){
|
|
||||||
int i;
|
|
||||||
int16_t* c= (int16_t*)val;
|
|
||||||
int32_t* a= (int32_t*)val;
|
|
||||||
int64_t* b= (int64_t*)val;
|
|
||||||
for(i=0; length > 0; i++){
|
|
||||||
switch(itype){
|
|
||||||
case INT16:
|
|
||||||
c[i] = ((c[i] & 0x00FF) << 8) | ((c[i] & 0xFF00) >> 8);
|
|
||||||
length -= sizeof(int16_t);
|
|
||||||
break;
|
|
||||||
case INT32:
|
|
||||||
a[i]=((a[i] << 8) & 0xFF00FF00) | ((a[i] >> 8) & 0xFF00FF );
|
|
||||||
a[i]=(a[i] << 16) | ((a[i] >> 16) & 0xFFFF);
|
|
||||||
length -= sizeof(int32_t);
|
|
||||||
break;
|
|
||||||
case INT64:
|
|
||||||
b[i] = ((b[i] << 8) & 0xFF00FF00FF00FF00ULL ) | ((b[i] >> 8) & 0x00FF00FF00FF00FFULL );
|
|
||||||
b[i] = ((b[i] << 16) & 0xFFFF0000FFFF0000ULL ) | ((b[i] >> 16) & 0x0000FFFF0000FFFFULL );
|
|
||||||
b[i] = (b[i] << 32) | ((b[i] >> 32) & 0xFFFFFFFFULL);
|
|
||||||
length -= sizeof(int64_t);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
length = 0;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int sendData(int file_des, void* buf,int length, intType itype){
|
|
||||||
#ifndef PCCOMPILE
|
|
||||||
#ifdef EIGERD
|
|
||||||
swapData(buf, length, itype);
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
return sendDataOnly(file_des, buf, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int receiveData(int file_des, void* buf,int length, intType itype){
|
|
||||||
int ret = receiveDataOnly(file_des, buf, length);
|
|
||||||
#ifndef PCCOMPILE
|
|
||||||
#ifdef EIGERD
|
|
||||||
if (ret >= 0) swapData(buf, length, itype);
|
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int sendDataOnly(int file_des, void* buf,int length) {
|
|
||||||
if (!length)
|
|
||||||
return 0;
|
|
||||||
int ret = write(file_des, buf, length); //value of -1 is other end socket crash as sigpipe is ignored
|
|
||||||
if (ret < 0) cprintf(BG_RED, "Error writing to socket. Possible socket crash\n");
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int receiveDataOnly(int file_des, void* buf,int length) {
|
|
||||||
|
|
||||||
int total_received=0;
|
|
||||||
int nreceiving;
|
|
||||||
int nreceived;
|
|
||||||
if (file_des<0) return -1;
|
|
||||||
#ifdef VERY_VERBOSE
|
|
||||||
printf("want to receive %d Bytes\n", length);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
while(length > 0) {
|
|
||||||
nreceiving = (length>send_rec_max_size) ? send_rec_max_size:length; // (condition) ? if_true : if_false
|
|
||||||
nreceived = read(file_des,(char*)buf+total_received,nreceiving);
|
|
||||||
if(!nreceived){
|
|
||||||
if(!total_received) {
|
|
||||||
return -1; //to handle it
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
length-=nreceived;
|
|
||||||
total_received+=nreceived;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (total_received>0)
|
|
||||||
strcpy(thisClientIP,dummyClientIP);
|
|
||||||
|
|
||||||
if (strcmp(lastClientIP,thisClientIP))
|
|
||||||
differentClients=1;
|
|
||||||
else
|
|
||||||
differentClients=0;
|
|
||||||
|
|
||||||
return total_received;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int sendModule(int file_des, sls_detector_module *myMod) {
|
|
||||||
return sendModuleGeneral(file_des, myMod, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int sendModuleGeneral(int file_des, sls_detector_module *myMod, int sendAll) {
|
|
||||||
int ts=0;
|
|
||||||
#ifdef VERBOSE
|
|
||||||
int idac;
|
|
||||||
#endif
|
|
||||||
int nChips=myMod->nchip;
|
|
||||||
int nChans=myMod->nchan;
|
|
||||||
int nAdcs=myMod->nadc;
|
|
||||||
int nDacs=myMod->ndac;
|
|
||||||
//ts+= sendDataOnly(file_des,myMod,sizeof(sls_detector_module));
|
|
||||||
ts+=sendData(file_des,&(myMod->serialnumber),sizeof(myMod->serialnumber),INT32);
|
|
||||||
ts+=sendData(file_des,&(myMod->nchan),sizeof(myMod->nchan),INT32);
|
|
||||||
ts+=sendData(file_des,&(myMod->nchip),sizeof(myMod->nchip),INT32);
|
|
||||||
ts+=sendData(file_des,&(myMod->ndac),sizeof(myMod->ndac),INT32);
|
|
||||||
ts+=sendData(file_des,&(myMod->nadc),sizeof(myMod->nadc),INT32);
|
|
||||||
ts+=sendData(file_des,&(myMod->reg),sizeof(myMod->reg),INT32);
|
|
||||||
ts+=sendData(file_des,&(myMod->gain), sizeof(myMod->gain),OTHER);
|
|
||||||
ts+=sendData(file_des,&(myMod->offset), sizeof(myMod->offset),OTHER);
|
|
||||||
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("module of size %d sent\n",ts);
|
|
||||||
#endif
|
|
||||||
ts+= sendData(file_des,myMod->dacs,sizeof(int)*nDacs,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("dacs of size %d sent\n",ts);
|
|
||||||
int idac;
|
|
||||||
for (idac=0; idac< nDacs; idac++)
|
|
||||||
printf("dac %d is %d\n",idac,(int)myMod->dacs[idac]);
|
|
||||||
#endif
|
|
||||||
ts+= sendData(file_des,myMod->adcs,sizeof(int)*nAdcs,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("adcs of size %d sent\n", ts);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*some detectors dont require sending all trimbits etc.*/
|
|
||||||
if(sendAll){
|
|
||||||
ts+=sendData(file_des,myMod->chipregs,sizeof(int)*nChips,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("chips of size %d sent\n", ts);
|
|
||||||
#endif
|
|
||||||
ts+=sendData(file_des,myMod->chanregs,sizeof(int)*nChans,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("chans of size %d sent - %d\n", ts, myMod->nchan);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("module of size %d sent register %x\n", ts, myMod->reg);
|
|
||||||
#endif
|
|
||||||
return ts;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int receiveModule(int file_des, sls_detector_module* myMod) {
|
|
||||||
return receiveModuleGeneral(file_des,myMod,1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int receiveModuleGeneral(int file_des, sls_detector_module* myMod, int receiveAll) {
|
|
||||||
int ts=0;
|
|
||||||
int *dacptr=myMod->dacs;
|
|
||||||
int *adcptr=myMod->adcs;
|
|
||||||
int *chipptr=myMod->chipregs, *chanptr=myMod->chanregs;
|
|
||||||
int nChips, nchipold=myMod->nchip, nchipdiff;
|
|
||||||
int nChans, nchanold=myMod->nchan, nchandiff;
|
|
||||||
int nDacs, ndold=myMod->ndac, ndacdiff;
|
|
||||||
int nAdcs, naold=myMod->nadc, nadcdiff;
|
|
||||||
ts+=receiveData(file_des,&(myMod->serialnumber),sizeof(myMod->serialnumber),INT32);
|
|
||||||
ts+=receiveData(file_des,&(myMod->nchan),sizeof(myMod->nchan),INT32);
|
|
||||||
ts+=receiveData(file_des,&(myMod->nchip),sizeof(myMod->nchip),INT32);
|
|
||||||
ts+=receiveData(file_des,&(myMod->ndac),sizeof(myMod->ndac),INT32);
|
|
||||||
ts+=receiveData(file_des,&(myMod->nadc),sizeof(myMod->nadc),INT32);
|
|
||||||
ts+=receiveData(file_des,&(myMod->reg),sizeof(myMod->reg),INT32);
|
|
||||||
ts+=receiveData(file_des,&(myMod->gain), sizeof(myMod->gain),OTHER);
|
|
||||||
ts+=receiveData(file_des,&(myMod->offset), sizeof(myMod->offset),OTHER);
|
|
||||||
|
|
||||||
myMod->dacs=dacptr;
|
|
||||||
myMod->adcs=adcptr;
|
|
||||||
myMod->chipregs=chipptr;
|
|
||||||
myMod->chanregs=chanptr;
|
|
||||||
|
|
||||||
#ifdef EIGERD
|
|
||||||
//feature to exclude sending of trimbtis, nchips=0,nchans=0 in that case
|
|
||||||
if(myMod->nchip == 0 && myMod->nchan == 0) {
|
|
||||||
receiveAll=0;
|
|
||||||
nchipold=0;
|
|
||||||
nchanold=0;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
nChips=myMod->nchip;
|
|
||||||
nchipdiff=nChips-nchipold;
|
|
||||||
if (nchipold!=nChips) {
|
|
||||||
printf("received wrong number of chips\n");
|
|
||||||
}
|
|
||||||
#ifdef VERBOSE
|
|
||||||
else
|
|
||||||
printf("received %d chips\n",nChips);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
nChans=myMod->nchan;
|
|
||||||
nchandiff=nChans-nchanold;
|
|
||||||
if (nchanold!=nChans) {
|
|
||||||
printf("received wrong number of channels\n");
|
|
||||||
}
|
|
||||||
#ifdef VERBOSE
|
|
||||||
else
|
|
||||||
printf("received %d chans\n",nChans);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
nDacs=myMod->ndac;
|
|
||||||
ndacdiff=nDacs-ndold;
|
|
||||||
if (ndold!=nDacs) {
|
|
||||||
printf("received wrong number of dacs\n");
|
|
||||||
}
|
|
||||||
#ifdef VERBOSE
|
|
||||||
else
|
|
||||||
printf("received %d dacs\n",nDacs);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
nAdcs=myMod->nadc;
|
|
||||||
nadcdiff=nAdcs-naold;
|
|
||||||
if (naold!=nAdcs) {
|
|
||||||
printf("received wrong number of adcs\n");
|
|
||||||
}
|
|
||||||
#ifdef VERBOSE
|
|
||||||
else
|
|
||||||
printf("received %d adcs\n",nAdcs);
|
|
||||||
#endif
|
|
||||||
if (ndacdiff<=0) {
|
|
||||||
ts+=receiveData(file_des,myMod->dacs, sizeof(int)*nDacs,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("dacs received\n");
|
|
||||||
int id;
|
|
||||||
for (id=0; id<nDacs; id++)
|
|
||||||
printf("dac %d val %d\n",id, (int)myMod->dacs[id]);
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
dacptr=(int*)malloc(ndacdiff*sizeof(int));
|
|
||||||
myMod->ndac=ndold;
|
|
||||||
ts+=receiveData(file_des,myMod->dacs, sizeof(int)*ndold,INT32);
|
|
||||||
ts+=receiveData(file_des,dacptr, sizeof(int)*ndacdiff,INT32);
|
|
||||||
free(dacptr);
|
|
||||||
return FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nadcdiff<=0) {
|
|
||||||
ts+=receiveData(file_des,myMod->adcs, sizeof(int)*nAdcs,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("adcs received\n");
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
adcptr=(int*)malloc(nadcdiff*sizeof(int));
|
|
||||||
myMod->nadc=naold;
|
|
||||||
ts+=receiveData(file_des,myMod->adcs, sizeof(int)*naold,INT32);
|
|
||||||
ts+=receiveData(file_des,adcptr, sizeof(int)*nadcdiff,INT32);
|
|
||||||
free(adcptr);
|
|
||||||
return FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/*some detectors dont require sending all trimbits etc.*/
|
|
||||||
if(receiveAll){
|
|
||||||
|
|
||||||
if (nchipdiff<=0) {
|
|
||||||
ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nChips,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("chips received\n");
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
chipptr=(int*)malloc(nchipdiff*sizeof(int));
|
|
||||||
myMod->nchip=nchipold;
|
|
||||||
ts+=receiveData(file_des,myMod->chipregs, sizeof(int)*nchipold,INT32);
|
|
||||||
ts+=receiveData(file_des,chipptr, sizeof(int)*nchipdiff,INT32);
|
|
||||||
free(chipptr);
|
|
||||||
return FAIL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nchandiff<=0) {
|
|
||||||
ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nChans,INT32);
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("chans received\n");
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
chanptr=(int*)malloc(nchandiff*sizeof(int));
|
|
||||||
myMod->nchan=nchanold;
|
|
||||||
ts+=receiveData(file_des,myMod->chanregs, sizeof(int)*nchanold,INT32);
|
|
||||||
ts+=receiveData(file_des,chanptr, sizeof(int)*nchandiff,INT32);
|
|
||||||
free(chanptr);
|
|
||||||
return FAIL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#ifdef VERBOSE
|
|
||||||
printf("received module of size %d register %x\n",ts,myMod->reg);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return ts;
|
|
||||||
}
|
|
@ -1,47 +0,0 @@
|
|||||||
#ifndef COMMUNICATION_FUNCS_H
|
|
||||||
#define COMMUNICATION_FUNCS_H
|
|
||||||
|
|
||||||
#define SEND_REC_MAX_SIZE 4096
|
|
||||||
#define DEFAULT_PORTNO 1952
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#include <netdb.h>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "sls_detector_defs.h"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef enum{
|
|
||||||
INT16,
|
|
||||||
INT32,
|
|
||||||
INT64,
|
|
||||||
OTHER
|
|
||||||
}intType;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int bindSocket(unsigned short int port_number);
|
|
||||||
int acceptConnection(int socketDescriptor);
|
|
||||||
void closeConnection(int file_Des);
|
|
||||||
void exitServer(int socketDescriptor);
|
|
||||||
|
|
||||||
void swapData(void* val,int length,intType itype);
|
|
||||||
int sendData(int file_des, void* buf,int length, intType itype);
|
|
||||||
int receiveData(int file_des, void* buf,int length, intType itype);
|
|
||||||
int sendDataOnly(int file_des, void* buf,int length);
|
|
||||||
int receiveDataOnly(int file_des, void* buf,int length);
|
|
||||||
|
|
||||||
|
|
||||||
int getServerError(int socketDescriptor);
|
|
||||||
int sendModule(int file_des, sls_detector_module *myMod);
|
|
||||||
int sendModuleGeneral(int file_des, sls_detector_module *myMod, int sendAll);
|
|
||||||
int receiveModule(int file_des, sls_detector_module* myMod);
|
|
||||||
int receiveModuleGeneral(int file_des, sls_detector_module* myMod, int receiveAll);
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
x
Reference in New Issue
Block a user