/* * server.c - C version of server to readout scalers data from FADC by using client GUI. * * 28 Feb 2012 A.S. Server prototype taken from hallb, * some modifications for the bcal/fcal beam tests. * 16 Mar 2012 A.S. Small modifications in the communication format; * made it compartible with the client * 3 Jul 2013: R.P. Mod: add comuunication protocol for DiagGUI from Ben. * add commands Read32, Write32, BlockRead32 by using vme address. * add mutex FALOCk, FAUNLOCK to faReadThresholds() and to faReadDAC(). * add check FADC board presence in the slot in faSoftInit(). * 17 Jul 2013: R.P. Mod: add reading of new config file, same as in coda readout list (vme_list.c), * functions from file 'confutils.c' are used. * add function to print fadc pedestals: faPrintPed(id). */ #include #include #include #include #include #include #include #include #include #include #include #include "jvme.h" #include "fadcLib.h" #include "ctpLib.h" #include "tiLib.h" #include "tsLib.h" #include "sdLib.h" #include "VMERemote.h" #include "conf_utils.h" #include "conf_utils.c" #define hardware 1 #define SERVER_PORT_NUM 9090 /* server port by default */ #define SERVER_MAX_CONNECTIONS 4 #define REQUEST_MSG_SIZE 1024 /* max size of request message */ extern pthread_mutex_t faMutex; /*** this mutex is defined in 'fadcLib.c', I think we have to use it ***/ #define FALOCK if(pthread_mutex_lock(&faMutex)<0) perror("pthread_mutex_lock"); #define FAUNLOCK if(pthread_mutex_unlock(&faMutex)<0) perror("pthread_mutex_unlock"); extern int fadcA32Base; extern volatile struct fadc_struct *FAp[(FA_MAX_BOARDS+1)]; /* defined in 'fadcLib.c' */ extern int nfadc; /* Number of FADC250s verified with the library */ extern int fadcID[FA_MAX_BOARDS]; /* Array of slot numbers, discovered by the library */ char *strcasestr(const char *haystack, const char *needle); int fadc_id=16; int fsc_flag=1; /* Latch Scalers before read */ unsigned int sclr_buff[FA_MAX_ADC_CHANNELS+1]; /* 17 */ unsigned short thr_buff[FA_MAX_ADC_CHANNELS]; /* 16 */ unsigned int ts_buff[66]; #define BYTESWAP_OUTGOING { \ tempPtr = (unsigned int *)&OutgoingMsg; \ nwords = (OutgoingMsg.Length+3)/4; \ orig_length=OutgoingMsg.Length; \ for(iword=0;iword(FA_MAX_BOARDS+1)) || (FAp[id] == NULL)) { printf("faReadThreshold: ERROR : ADC in slot %d is not initialized \n",id); return(-1); } FALOCK; for(ii=0;iiadc_thres[ii])) & FA_DAC_VALUE_MASK; } FAUNLOCK; return (0); } /************************************************************************ * faReadDAC() - reads DAC's set for selected FADC * Params: id - fadc slot, data - array[16] DAC values from FADC * Returns: 0 - success, -1 - otherwise. * */ int faReadDAC(int id, unsigned short *data, int flag) { int ii; if((id<=0) || (id>21) || (FAp[id] == NULL)) { printf("faReadDAC: ERROR : ADC in slot %d is not initialized \n",id); return (-1); } FALOCK; for(ii=0;iidac[ii])) & FA_DAC_VALUE_MASK; FAUNLOCK; return (0); } /************************************************************************ * faPrintPed(int id) - prints pedestals from FADC in slot = id * */ void faPrintPed(int id) { int ii; printf(" Pedestal Settings for FADC in slot %d:",id); for(ii=0;iinewFd, &readSet); FD_SET(targ->newFd, &writeSet); FD_SET(targ->newFd, &exceptSet); printf(" I am in the new thread %d %d %s\n", targ->port, targ->newFd, targ->address); int msg_received = 0; // while(1){ while ((rec= select(targ->newFd+1, &readSet, NULL, &exceptSet, NULL))>=0) { if (FD_ISSET(targ->newFd, &exceptSet)) { perror("socket exception"); break; } if (FD_ISSET(targ->newFd, &readSet)) { if( (len=recv(targ->newFd, (char *)&IncomingMsg, 4, 0)) == 4) { printf(" ---- Message Length ---- %d \n", NTOHL(IncomingMsg.Length) ); if(NTOHL(IncomingMsg.Length) <= sizeof(RemoteMsgStruct) - 4){ if( (len=recv(targ->newFd, (char *)&IncomingMsg.BoardType, NTOHL(IncomingMsg.Length),0)) == NTOHL(IncomingMsg.Length)) { printf ("FROM CLIENT (length %d): message 0x%x type 0x%x convert type 0x%x ..\n", len, IncomingMsg.BoardType, IncomingMsg.MsgType, NTOHL(IncomingMsg.MsgType)); /************************/ /* Executing the message */ if(len) { int msg_type = NTOHL(IncomingMsg.MsgType); int board = NTOHL(IncomingMsg.BoardType); int channel, threshold; switch(msg_type){ case REMOTE_CMD_READ_TS: { printf(" ===== Receive request to read TS scalers ==== \n"); unsigned int board = 5; Cmd_Read_TS *pCmd_Read_TS; pCmd_Read_TS = (Cmd_Read_TS *)IncomingMsg.Msg; status = NTOHL(pCmd_Read_TS->Status); /* preparing replay message */ OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Read_TS)-4); OutgoingMsg.BoardType = HTONL(board); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_READ_TS); pCmd_Read_TS = (Cmd_Read_TS *)OutgoingMsg.Msg; ret = 0; ret = tsReadScalersMon(ts_buff); printf(" tsReadScalersMon: Read %d words \n", ret); for(i = 0; i < 66; i++) { printf("TS scaler %d = %d \n", i, ts_buff[i]); pCmd_Read_TS->Value[i] = HTONL(ts_buff[i]); ts_buff[i] = 0; /* clear buffer */ } pCmd_Read_TS->Status = HTONL( ret ); int size = NTOHL(OutgoingMsg.Length) + 4; send(targ->newFd, (const char *)&OutgoingMsg, size, 0); break; } case REMOTE_CMD_READ_SC_ALL: { board = 5; printf(" ===== Receive read requests ==== \n"); Cmd_Read_SC_ALL *pCmd_Read_SC_ALL; pCmd_Read_SC_ALL = (Cmd_Read_SC_ALL *)IncomingMsg.Msg; int fadc_first = NTOHL(pCmd_Read_SC_ALL->IDF); int fadc_last = NTOHL(pCmd_Read_SC_ALL->IDL); status = NTOHL(pCmd_Read_SC_ALL->Status); printf(" FADC Slot First: %d Last %d \n", fadc_first, fadc_last); printf(" Status: %d\n", status); /* preparing replay message */ OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Read_SC_ALL)-4); OutgoingMsg.BoardType = HTONL(board); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_READ_SC_ALL); pCmd_Read_SC_ALL = (Cmd_Read_SC_ALL *)OutgoingMsg.Msg; pCmd_Read_SC_ALL->IDF = HTONL( fadc_first ); pCmd_Read_SC_ALL->IDL = HTONL( fadc_last ); int read_sc = 1; ret = 0; if(fadc_first < 3 || fadc_first > 20){ printf(" FATAL: READ_SC_ALL wrong first fadc slot %d \n", fadc_first); read_sc = 0; } if(fadc_last < 3 || fadc_last > 20){ printf(" FATAL: READ_SC_ALL wrong last fadc slot %d \n", fadc_last); read_sc = 0; } if(hardware && (read_sc == 1)){ switch( status&0xf) { case 0: /* scalers readout all channels */ // fsc_flag = 3; /* latch and then reset */ fsc_flag = 1; /* latch scalers */ /* read all 16 channels + timer */ int bd_id = 0; int fadc_bd; for(fadc_bd = fadc_first; fadc_bd <= fadc_last; fadc_bd++){ if(fadc_bd == 11) continue; if(fadc_bd == 12) continue; // Slot 3: bd_id = 0 Slot 20: bd_id = 15 if(fadc_bd > 12) bd_id = fadc_bd - 5; else bd_id = fadc_bd - 3; ret = faReadScalers(fadc_bd, sclr_buff, 0xffff, fsc_flag); printf(" FADC in slot %d: faReadSaclers() returns %d \n",fadc_bd, ret); for(i = 0; i < 17; i++) { printf("ch_%d: %d\n", i, sclr_buff[i]); int ind = bd_id*17 + i; pCmd_Read_SC_ALL->Value[ind] = HTONL(sclr_buff[i]); sclr_buff[i] = 0; /* clear buffer */ } } break; default: ret = -2; break; } } /* end if(hardware && (read_sc == 1)) */ pCmd_Read_SC_ALL->Status = HTONL( ret ); int size = NTOHL(OutgoingMsg.Length) + 4; send(targ->newFd, (const char *)&OutgoingMsg, size, 0); break; } case REMOTE_CMD_READ_SC: { board = 5; printf(" ===== Receive read requests ==== \n"); Cmd_Read_SC *pCmd_Read_SC; pCmd_Read_SC = (Cmd_Read_SC *)IncomingMsg.Msg; fadc_id = NTOHL(pCmd_Read_SC->ID); status = NTOHL(pCmd_Read_SC->Status); printf(" Slot: %d\n", fadc_id); printf(" Status: %d\n", status); /* preparing replay message */ OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Read_SC)-4); OutgoingMsg.BoardType = HTONL(board); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_READ_SC); pCmd_Read_SC = (Cmd_Read_SC *)OutgoingMsg.Msg; pCmd_Read_SC->ID = HTONL( fadc_id); if(hardware){ switch( status&0xf) { case 0: /* scalers readout all channels */ fsc_flag=3; /* latch and then reset */ /* read all 16 channels + timer */ // faPrintScalers(6, 3); ret=faReadScalers(fadc_id, sclr_buff, 0xffff, fsc_flag); printf(" faReadSaclers() returns: %d \n",ret); for(i=0; i<17; i++) { printf("ch_%d: %d\n", i, sclr_buff[i]); pCmd_Read_SC->Value[i] = HTONL(sclr_buff[i]); sclr_buff[i] = 0; /* clear buffer */ } break; case 1: /* thresholds readout all channels */ ret=faReadThreshold(fadc_id, thr_buff, 0); printf(" faReadThreshold() returns: %d \n",ret); for(i=0;i<8;i++) { printf(" threshold( %2d ): %5d ( %2d ): %5d \n", (i+1), thr_buff[i],(i+1+8), thr_buff[i+8] ); pCmd_Read_SC->Value[i] = HTONL(thr_buff[i]); pCmd_Read_SC->Value[i+8] = HTONL(thr_buff[i+8]); } break; case 2: /* DAC set readout all channels */ ret=faReadDAC(fadc_id, thr_buff, 0); printf(" faReadDAC() returns: %d \n",ret); for(i=0;i<8;i++) { printf(" DAC set( %2d ): %4d ( %2d ): %4d\n", (i+1), thr_buff[i], (i+1+8), thr_buff[i+8]); pCmd_Read_SC->Value[i] = HTONL(thr_buff[i]); pCmd_Read_SC->Value[i+8] = HTONL(thr_buff[i+8]); } break; case 10: /* CTP scalers for the PSC and PS */ // ctpReadSpectrometerData( latch, clear, *data); ctp_ndata = ctpReadSpectrometerData( 1, 1, (unsigned int *)&ctp_data); // latch and reset printf(" ctpReadSpectrometerData returns: %d \n",ctp_ndata); if(ctp_ndata > 0){ printf("Timestamp : 0x%04x 0x%08x\n",ctp_data[0],ctp_data[1]); printf("Left Arm Counter : %d\n",ctp_data[2]); printf("Right Arm Counter : %d\n",ctp_data[3]); printf("Coinc Counter : %d\n",ctp_data[4]); printf("Hit channel (R) : %d (L) : %d\n", (ctp_data[5] & 0xFF00)>>8,(ctp_data[5] & 0xFF)); } for(i = 0; i < 6; i++) { if( i == 5 ) { pCmd_Read_SC->Value[5] = HTONL( (ctp_data[5] & 0xFF00)>>8 ); pCmd_Read_SC->Value[6] = HTONL( ctp_data[5] & 0xFF ); ctp_data[5] = 0; /* clear buffer */ } else { pCmd_Read_SC->Value[i] = HTONL(ctp_data[i]); ctp_data[i] = 0; /* clear buffer */ } } break; default: ret = -2; break; } } // if(hardware) pCmd_Read_SC->Status = HTONL( ret); int size = NTOHL(OutgoingMsg.Length) + 4; send(targ->newFd, (const char *)&OutgoingMsg, size, 0); break; } case REMOTE_CMD_WRITE_SC: { Cmd_Write_SC *pCmd_Write32; pCmd_Write32 = (Cmd_Write_SC *)IncomingMsg.Msg; printf(" ===== Receive write requests ==== \n"); fadc_id = NTOHL(pCmd_Write32->ID); status = NTOHL(pCmd_Write32->Status); channel = NTOHL(pCmd_Write32->Address); threshold = NTOHL(pCmd_Write32->Value); printf(" Slot: %d\n", fadc_id); printf(" Status: %d\n", status); printf(" Channel: %d\n", channel); printf(" Value: %d\n", threshold); if(channel>0) chmask = (0x1)<<(channel-1); else chmask = 0; /* set all channels */ switch(status) { case 1: /* set thresholds */ printf(" Set threshods chan: %04x val: %d\n", chmask, threshold); ret = faSetThreshold(fadc_id, threshold, chmask); faPrintThreshold(fadc_id); faReadThreshold(fadc_id, thr_buff, 0); for(i=0;i<8;i++) { printf(" threshold( %2d ): %5d ( %2d ): %5d \n", (i+1), thr_buff[i],(i+1+8), thr_buff[i+8] ); } break; case 2: /* set dac values */ printf(" Set DAC chan: %04x val: %d\n", chmask, threshold); ret = faSetDAC(fadc_id, threshold, chmask); faPrintDAC(fadc_id); faReadDAC(fadc_id, thr_buff, 0); for(i=0;i<8;i++) { printf(" DAC set( %2d ): %4d ( %2d ): %4d\n", (i+1), thr_buff[i], (i+1+8), thr_buff[i+8]); } break; } break; } /*** Read16 ***/ case REMOTE_CMD_READ16: { Cmd_Read16 *pCmd_Read16 = (Cmd_Read16 *)IncomingMsg.Msg; Cmd_Read16_Rsp *pCmd_Read16_Rsp = (Cmd_Read16_Rsp *)OutgoingMsg.Msg; unsigned short *pData; unsigned int addr; addr = NTOHL(pCmd_Read16->Address); #ifdef VXWORKS sysBusToLocalAdrs(0x39, addr,(unsigned long *)&pData); #else vmeBusToLocalAdrs(0x39, (char *)addr,(char **)&pData); #endif OutgoingMsg.BoardType = HTONL(IncomingMsg.BoardType); OutgoingMsg.Length = REMOTE_MSG_SIZE(Cmd_Read16_Rsp)-4; OutgoingMsg.MsgType = CMD_RSP(REMOTE_CMD_READ16); vmeBusLock(); pCmd_Read16_Rsp->Value = *pData; vmeBusUnlock(); BYTESWAP_OUTGOING; send(targ->newFd, (const char *)&OutgoingMsg, orig_length + 4, 0); #if DEBUG_REMOTE_CMD printf("Cmd_Read16: Address = 0x%08X, Data = 0x%04X\n", addr, pCmd_Read16_Rsp->Value); #endif break; } /*** Write16 ***/ case REMOTE_CMD_WRITE16: { Cmd_Write16 *pCmd_Write16 = (Cmd_Write16 *)IncomingMsg.Msg; unsigned short *pData; unsigned int addr, val; addr = NTOHL(pCmd_Write16->Address); val = NTOHL(pCmd_Write16->Value); #ifdef VXWORKS sysBusToLocalAdrs(0x39, addr,(unsigned long *)&pData); #else vmeBusToLocalAdrs(0x39, (char *)addr,(char **)&pData); #endif vmeBusLock(); vmeWrite16(pData, val); vmeBusUnlock(); #if DEBUG_REMOTE_CMD printf("Cmd_Write16: Address = 0x%08X, Data = 0x%04hX\n", addr, val); #endif break; } /*** BlockRead16 ***/ case REMOTE_CMD_BLKREAD16: { Cmd_BlkRead16 *pCmd_BlkRead16 = (Cmd_BlkRead16 *)IncomingMsg.Msg; Cmd_BlkRead16_Rsp *pCmd_BlkRead16_Rsp = (Cmd_BlkRead16_Rsp *)OutgoingMsg.Msg; unsigned short *pData; int i; unsigned int addr; addr = NTOHL(pCmd_BlkRead16->Address); #ifdef VXWORKS sysBusToLocalAdrs(0x39, addr,(unsigned long *)&pData); #else vmeBusToLocalAdrs(0x39, (char *)addr,(char **)&pData); #endif OutgoingMsg.BoardType = HTONL(IncomingMsg.BoardType); OutgoingMsg.Length = REMOTE_MSG_SIZE(Cmd_BlkRead16_Rsp)-sizeof(Cmd_BlkRead16_Rsp)+pCmd_BlkRead16->Count*sizeof(unsigned short)-4; OutgoingMsg.MsgType = CMD_RSP(REMOTE_CMD_BLKREAD16); vmeBusLock(); for(i = pCmd_BlkRead16->Count-1; i >= 0 ; i--) pCmd_BlkRead16_Rsp->Values[i] = pData[i]; vmeBusUnlock(); BYTESWAP_OUTGOING; send(targ->newFd, (const char *)&OutgoingMsg, orig_length + 4, 0); break; } /*** Read32 ***/ case REMOTE_CMD_READ32: { Cmd_Read32 *pCmd_Read32 = (Cmd_Read32 *)IncomingMsg.Msg; /*** Cmd_Read32 *pCmd_Read32 = (Cmd_Read32 *)pRemoteMsgStruct->Msg; ***/ Cmd_Read32_Rsp *pCmd_Read32_Rsp = (Cmd_Read32_Rsp *)OutgoingMsg.Msg; unsigned int *pData; unsigned int addr; addr = NTOHL(pCmd_Read32->Address); #ifdef VXWORKS sysBusToLocalAdrs(0x39, addr,(unsigned long *)&pData); #else vmeBusToLocalAdrs(0x39, (char *)addr,(char **)&pData); #endif OutgoingMsg.BoardType = HTONL(IncomingMsg.BoardType); OutgoingMsg.Length = REMOTE_MSG_SIZE(Cmd_Read32_Rsp)-4; OutgoingMsg.MsgType = CMD_RSP(REMOTE_CMD_READ32); vmeBusLock(); pCmd_Read32_Rsp->Value = *pData; /*** TEST ONLY !!!! ***/ /** pCmd_Read32_Rsp->Value = 0xaa55;**/ vmeBusUnlock(); BYTESWAP_OUTGOING; send(targ->newFd, (const char *)&OutgoingMsg, orig_length + 4, 0); /*** if(send(gClientSocket, (const char *)&OutgoingMsgStruct, orig_length + 4, 0) <= 0) return 1; ***/ #if DEBUG_REMOTE_CMD printf("Cmd_Read32: Address = 0x%08X, Data = 0x%08X\n", addr, pCmd_Read32_Rsp->Value); /** Value is byte swaped after VME readout**/ #endif break; } /*** Write32 ***/ case REMOTE_CMD_WRITE32: { Cmd_Write32 *pCmd_Write32 = (Cmd_Write32 *)IncomingMsg.Msg; unsigned int *pData; unsigned int addr, val; addr = NTOHL(pCmd_Write32->Address); val = NTOHL( pCmd_Write32->Value); #ifdef VXWORKS /*** sysBusToLocalAdrs(0x39, pCmd_Write32->Address,(unsigned long *)&pData);***/ sysBusToLocalAdrs(0x39, addr,(unsigned long *)&pData); #else /*** vmeBusToLocalAdrs(0x39, (char *)pCmd_Write32->Address,(char **)&pData); ***/ vmeBusToLocalAdrs(0x39, (char *)addr,(char **)&pData); #endif vmeBusLock(); vmeWrite32(pData, val); vmeBusUnlock(); #if DEBUG_REMOTE_CMD /*** printf("Cmd_Write32: Address = 0x%08X, Data = 0x%08X\n", pCmd_Write32->Address, pCmd_Write32->Value); ***/ printf("Cmd_Write32: Address = 0x%08X, Data = 0x%08X\n", addr, val); #endif break; } /*** BlockRead32 ***/ case REMOTE_CMD_BLKREAD32: { Cmd_BlkRead32 *pCmd_BlkRead32 = (Cmd_BlkRead32 *)IncomingMsg.Msg; Cmd_BlkRead32_Rsp *pCmd_BlkRead32_Rsp = (Cmd_BlkRead32_Rsp *)OutgoingMsg.Msg; volatile unsigned int *pData; unsigned int addr; int i; addr = NTOHL(pCmd_BlkRead32->Address); #ifdef VXWORKS sysBusToLocalAdrs(0x39, addr,(unsigned long *)&pData); #else vmeBusToLocalAdrs(0x39, (char *)addr,(char **)&pData); #endif OutgoingMsg.BoardType = HTONL(IncomingMsg.BoardType); OutgoingMsg.Length = REMOTE_MSG_SIZE(Cmd_BlkRead32_Rsp)-sizeof(Cmd_BlkRead32_Rsp)+pCmd_BlkRead32->Count*sizeof(unsigned int)-4; OutgoingMsg.MsgType = CMD_RSP(REMOTE_CMD_BLKREAD32); vmeBusLock(); if(pCmd_BlkRead32->NoAddressInc) { for(i = 0; i < pCmd_BlkRead32->Count; i++) { pCmd_BlkRead32_Rsp->Values[i] = pData[0]; #if DEBUG_REMOTE_CMD printf("{0x%08X => 0x%08X}\n", pCmd_BlkRead32->Address, LSWAP(pCmd_BlkRead32_Rsp->Values[i])); #endif } } else { for(i = pCmd_BlkRead32->Count-1; i >= 0 ; i--) { pCmd_BlkRead32_Rsp->Values[i] = pData[i]; #if DEBUG_REMOTE_CMD printf("{0x%08X => 0x%08X}\n", pCmd_BlkRead32->Address + i*4, LSWAP(pCmd_BlkRead32_Rsp->Values[i])); #endif } } vmeBusUnlock(); BYTESWAP_OUTGOING; send(targ->newFd, (const char *)&OutgoingMsg, orig_length + 4, 0); /*** if(send(gClientSocket, (const char *)&OutgoingMsgStruct, orig_length + 4, 0) <= 0) return 1; ***/ break; } default: break; } if(msg_receivednewFd); /* close server socket connection */ /* terminate calling thread */ pthread_exit(NULL); } /************************************************************************/ /************************************************************************/ /************************************************************************/ /************************************************************************/ int main(int argc, char **argv) { struct sockaddr_in serverAddr; struct sockaddr_in clientAddr; int sockAddrSize; /* size of socket address structure */ int sFd; /* socket file descriptor */ int portnum = SERVER_PORT_NUM; /* desired port number */ FILE *fd, *fGUI; char host[80]; char fGUI_dir[255] = "/home/hdops/CDAQ/config/hd_server"; /* dir for open ports files */ char fname[255] = ""; /* config file name */ char dname[255] = ""; /* detector name as a part for fname */ char myfile[STRLEN] = ""; /* config file name for read_conf_common */ char file_GUI[STRLEN] = ""; int ii, jj, ch, val, args = -1; int slot, channel; int not_trig1 = 1; int rc = -1; int status; char str_tmp[STRLEN]; char keyword[ROCLEN]; char str[STRLEN]; char str_val[80]; int SL_MIN = 3; int SL_MAX = NBD_FADC250; int board; unsigned int ctp_vmemask = 0; gethostname(host,80); printf("\nhostname: >%s<\n",host); printf("\nargc: >%d<\n",argc); if (strcasestr(host,"rocps") != NULL) sprintf(dname,"ps"); else if(strcasestr(host,"rocfcal") != NULL) sprintf(dname,"fcal"); else if(strcasestr(host,"rocbcal") != NULL) sprintf(dname,"bcal"); else if(strcasestr(host,"roctof") != NULL) sprintf(dname,"tof"); else if(strcasestr(host,"rocst") != NULL) sprintf(dname,"st"); else if(strcasestr(host,"roctag") != NULL) sprintf(dname,"tag"); else if(strcasestr(host,"roctrig") != NULL) sprintf(dname,"trig"); sprintf(fname,"%s/%s.cnf", fGUI_dir,dname); if(argc>1) sprintf(fname,"%s",argv[1]); printf("config file name: >%s<\n",fname); if((fd=fopen(fname,"r")) == NULL) { printf("\n Can't open config file >%s<\n\n",fname); return(-1); } /* Parsing of config file */ while ((ch = getc(fd)) != EOF) { if ( ch == '#' || ch == ' ' || ch == '\t' ) { while (getc(fd) != '\n') {} } else if( ch == '\n' ) {} else { ungetc(ch,fd); fgets(str_tmp, sizeof(str_tmp), fd); sscanf (str_tmp, "%s %s", keyword, str); /* Start parsing real config inputs */ SCAN_B_SSETS("FADC250_COM_DIR", fa250[jj].conf_common_dir); SCAN_B_SSETS("FADC250_COM_VER", fa250[jj].conf_common_ver); SCAN_B_SETS("FADC250_TRIG_THR", fa250[jj].trig_thr,SL_MIN,SL_MAX); SCAN_TDP("FADC250_TRIG_BL", fa250[jj].trig_bl, SL_MIN,SL_MAX); } } fclose(fd); if((strlen(fa250[3].conf_common_dir) != 0) && (strlen(fa250[3].conf_common_ver) != 0) ) sprintf(myfile,"%s/%s_fadc250_%s.cnf", fa250[3].conf_common_dir,host,fa250[3].conf_common_ver); else if(strcasestr(host,"roctrig") != NULL) { not_trig1 = 0; } else { printf(BOLDRED "\n FATAL: Have to set FADC250_COM_DIR and FADC250_COM_VER in the global configuration file %s %s \n" RESET, fa250[3].conf_common_dir, fa250[3].conf_common_ver ); return(-2); } printf("myfile name: >%s<\n",myfile); if(hardware && not_trig1) { /* Set FADC250_DAC values */ rc = read_conf_common(host); if(rc != 0) { printf(BOLDRED " FATAL: Fail to read file with crate specific parameters %d \n" RESET, rc); } /* Check FADC250 settings */ // print_fadc250_conf(5); // print_fadc250_conf(6); printf("\n===================================================\n\n"); /* VME initialization */ fadcA32Base=0x09000000; status = vmeOpenDefaultWindows(); printf(" vmeOpenDefaultWindows() returns: %d\n", status); printf(BOLDBLUE "\n==========================================================\n\n" RESET); printf(BOLDBLUE " INITIALIZE TI \n\n" RESET); tiInit((21<<19),3,0); printf(BOLDBLUE "\n==========================================================\n\n" RESET); printf(BOLDBLUE " INITIALIZE CTP \n\n" RESET); ctpInit(1); // printf(BOLDBLUE "\n==========================================================\n\n" RESET); // sdInit(1); /* Initialize the SD library */ // sdSetActiveVmeSlots(1<<6); /* Use the fadcSlotMask to configure the SD */ // sdStatus(); if(strcmp(host, "rocps1") == 0 || strcmp(host, "rocps2") == 0){ tiSetClockSource(TI_CLOCK_INTERNAL); tiSetSyncResetType(1); tiSetSyncSource(TI_SYNC_LOOPBACK); tiClockReset(); taskDelay(2); tiTrigLinkReset(); tiEnableVXSSignals(); tiSetBlockBufferLevel(1); } int iFlag = 0; /* Sync Source */ iFlag |= (1<<0); /* VXS */ /* Trigger Source */ iFlag |= (1<<2); /* VXS */ /* Clock Source */ iFlag |= (0<<5); /* self*/ status = faInit((unsigned int)(3<<19), (1<<19), 20, iFlag); int NFADC = nfadc; /* Init fADC250, scan and check if FADC in slot */ // for(slot = SL_MIN; slot < SL_MAX; slot++) { for( board = 0; board < NFADC; board++) { slot = fadcID[board]; if(fa250[slot].group > 0) { printf(BOLDBLUE "\n==========================================================\n\n" RESET); printf(BOLDBLUE " INITIALIZE fADC250 , Slot number %d \n\n" RESET, slot); status = 0; // status = faInit((unsigned int)(slot<<19), 0, 1, iFlag); if(status==0) { /* FADC is present in slot */ faSetProcMode(slot, 1, 500, 100, 2, 3, 1, 0); /* load DACs, thresholds ... */ for(channel=0; channel<16; channel++) { faSetDAC(slot, fa250[slot].dac[channel], (0x1<CTP alignment if(strcmp(host, "rocps1") == 0 || strcmp(host, "rocps2") == 0){ printf(BOLDBLUE "------ Switch Clock Source ---- \n" RESET); faSetClockSource(slot,2); faSoftReset(slot,0); faResetTriggerCount(slot); faEnableSyncSrc(slot); faSetMGTTestMode(slot,0); faSetMGTTestMode(slot,1); } faStatus(slot,0); } else { /* FADC is not found in slot */ printf(" FADC not found in slot: %hd\n", slot); } } } faGStatus(1); if(strcmp(host, "rocps1") == 0 || strcmp(host, "rocps2") == 0){ if(strcmp(host, "rocps1") == 0){ ctp_vmemask = 0x1FE7F8; // 16 fadcs in the crate ctpSetVmeSlotEnableMask(ctp_vmemask); ctpSetSpectrometerMode(1, 10 ); printf(BOLDMAGENTA "SET CTP SPECTROMETER MODE TO 1 Window Width = %d \n" RESET, ctpGetSpectrometerWidth() ); } if(strcmp(host, "rocps2") == 0){ ctp_vmemask = (1<<6); ctpSetVmeSlotEnableMask(ctp_vmemask); ctpSetSpectrometerMode(2,10); printf(BOLDMAGENTA "SET CTP SPECTROMETER MODE TO 2 Window Width = %d \n" RESET, ctpGetSpectrometerWidth() ); } int iwait = 0; int allchanup = 0; while(allchanup != (0x7) ) { iwait++; allchanup = ctpGetAllChanUp(); if(iwait > 1000) { printf("iwait timeout allchup - 0x%x\n",allchanup); break; } } ctpFiberReset(); ctpPayloadReset(); ctpAlignAtSyncReset(1); taskDelay(15); ctpStatus(1); ctpResetScalers(); tiSyncReset(1); taskDelay(20); ctpGetAlignmentStatus(1,10); tiStatus(1); } } /* end VME initialization */ else { if(strcmp(host, "roctrig1") == 0 ){ status = vmeOpenDefaultWindows(); printf("=================== Initialize TS ===================\n\n"); tsInit(0xA80000,0,0); tsStatus(1); } } memset(sclr_buff,0,sizeof(sclr_buff)); memset(ts_buff,0,sizeof(ts_buff)); memset(thr_buff,0,sizeof(thr_buff)); /************** server part ************************/ /* some cleanup */ sockAddrSize = sizeof(struct sockaddr_in); memset((char *)&serverAddr, 0, sockAddrSize); memset((char *)&clientAddr, 0, sockAddrSize); /* creates an endpoint for communication and returns a socket file descriptor */ if((sFd = socket(AF_INET, SOCK_STREAM, 0)) == ERROR) { perror("socket"); return(ERROR); } /* set up the local address */ serverAddr.sin_family = AF_INET; serverAddr.sin_port = htons(portnum); serverAddr.sin_addr.s_addr = htonl(INADDR_ANY); /* create a TCP-based socket */ /* bind socket to local address */ while(bind(sFd, (struct sockaddr *)&serverAddr, sockAddrSize) == ERROR) { printf(" main: trying port %d\n",portnum); perror(" main: bind"); /* try another port (just increment on one) */ portnum ++; if((portnum-SERVER_PORT_NUM) > 10) { close(sFd); return(ERROR); } serverAddr.sin_port = htons(portnum); } printf(" main: bind on port %d\n",portnum); printf(" main: hostname >%s<\n",host); sprintf(file_GUI,"%s/%s.gui", fGUI_dir,host); if((fGUI=fopen(file_GUI,"w")) == NULL) { printf("\n Can't open config file >%s<\n\n",file_GUI); return(-3); } fprintf(fGUI,"%d\n", portnum); fclose(fGUI); /* create queue for client connection requests */ if(listen (sFd, SERVER_MAX_CONNECTIONS) == ERROR) { perror ("listen"); close (sFd); return (ERROR); } prctl(PR_SET_NAME,"fadc_scaler_server"); /* set process name */ while(1) { if((targ.newFd = accept (sFd, (struct sockaddr *) &clientAddr, (socklen_t *)&sockAddrSize)) == ERROR) { perror ("accept"); close (sFd); return (ERROR); } /* cout << " Accept connection from the port = " << ntohs(clientAddr.sin_port) << " Socket = " << targ.newFd << endl; */ targ.address = inet_ntoa(clientAddr.sin_addr); targ.port = ntohs (clientAddr.sin_port); printf(" Accept connection from: address=>%s< port=%d socket=%d\n", targ.address, targ.port, targ.newFd); fflush(stdout); /* Start a new thread per request */ int ret; pthread_t id; pthread_attr_t detached_attr; pthread_attr_init(&detached_attr); //pthread_attr_setdetachstate(&detached_attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&detached_attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setscope(&detached_attr, PTHREAD_SCOPE_SYSTEM); ret = pthread_create(&id, &detached_attr, tcpServerWorkTask, (void *) &targ); if(ret!=0) { printf("ERROR: pthread_create(CODAtcpServerWorkTask) returned %d\n", ret); close(targ.newFd); } /* Waiting for the thread completion */ void *status; pthread_attr_destroy(&detached_attr); rc = pthread_join(id,&status); /* cout << " Thread completed: After join " << endl; */ } if(hardware) status = vmeCloseDefaultWindows(); return(0); } #if 0 #endif