From 80e55cd4da24bb6962ffbf5c8907af06ad878952 Mon Sep 17 00:00:00 2001 From: Dhanya Thattil Date: Mon, 6 Apr 2020 17:28:05 +0200 Subject: [PATCH] WIP --- .../mythen3DetectorServer/CMakeLists.txt | 2 +- .../slsDetectorFunctionList.c | 77 +++++++++---- .../slsDetectorServer_defs.h | 3 - .../include/communication_funcs.h | 7 ++ .../include/communication_virtual.h | 14 +++ .../src/communication_virtual.c | 107 ++++++++++++++++++ .../slsDetectorServer/src/slsDetectorServer.c | 104 ++++++++--------- 7 files changed, 230 insertions(+), 84 deletions(-) create mode 100755 slsDetectorServers/slsDetectorServer/include/communication_virtual.h create mode 100755 slsDetectorServers/slsDetectorServer/src/communication_virtual.c diff --git a/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt b/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt index cf4269bed..72bb24351 100644 --- a/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt +++ b/slsDetectorServers/mythen3DetectorServer/CMakeLists.txt @@ -10,7 +10,7 @@ add_executable(mythen3DetectorServer_virtual ../slsDetectorServer/src/LTC2620_Driver.c ../slsDetectorServer/src/ALTERA_PLL_CYCLONE10.c ../slsDetectorServer/src/programFpgaNios.c - ../slsDetectorServer/src/clogger.c + ../slsDetectorServer/src/communication_virtual.c ) include_directories( diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c index 8c4a3b68d..18d111da8 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorFunctionList.c @@ -8,6 +8,7 @@ #include "ALTERA_PLL_CYCLONE10.h" #ifdef VIRTUAL #include "communication_funcs_UDP.h" +#include "communication_virtual.h" #endif #include @@ -33,7 +34,6 @@ int initCheckDone = 0; char initErrorMessage[MAX_STR_LENGTH]; #ifdef VIRTUAL -//int pipeFDs[2]; pthread_t pthread_virtual_tid; int virtual_status = 0; int virtual_stop = 0; @@ -326,6 +326,12 @@ void initStopServer() { LOG(logERROR, ("Stop Server: Map Fail. Dangerous to continue. Goodbye!\n")); exit(EXIT_FAILURE); } +#ifdef VIRTUAL + virtual_stop = 0; + if (!isControlServer) { + ComVirtual_writeStop(virtual_stop); + } +#endif } @@ -350,6 +356,12 @@ void setupDetector() { dacValues[i] = 0; } } +#ifdef VIRTUAL + virtual_status = 0; + if (isControlServer) { + ComVirtual_writeStatus(virtual_status); + } +#endif // pll defines ALTERA_PLL_C10_SetDefines(REG_OFFSET, BASE_READOUT_PLL, BASE_SYSTEM_PLL, PLL_RESET_REG, PLL_RESET_REG, PLL_RESET_READOUT_MSK, PLL_RESET_SYSTEM_MSK, READOUT_PLL_VCO_FREQ_HZ, SYSTEM_PLL_VCO_FREQ_HZ); @@ -1353,16 +1365,21 @@ int startStateMachine(){ LOG(logINFOBLUE, ("Starting State Machine\n")); // set status to running virtual_status = 1; - /*if (isControlServer) { - //write(pipeFDs[PIPE_WRITE], &virtual_status, sizeof(virtual_status)); - }*/ - virtual_stop = 0; + if (isControlServer) { + ComVirtual_writeStatus(virtual_status); + ComVirtual_readStop(&virtual_stop); + if (virtual_stop != 0) { + LOG(logERROR, ("Cant start acquisition. " + "Stop server has not updated stop status to 0\n")); + return FAIL; + } + } if(pthread_create(&pthread_virtual_tid, NULL, &start_timer, NULL)) { LOG(logERROR, ("Could not start Virtual acquisition thread\n")); virtual_status = 0; - /*if (isControlServer) { - //write(pipeFDs[PIPE_WRITE], &virtual_status, sizeof(virtual_status)); - }*/ + if (isControlServer) { + ComVirtual_writeStatus(virtual_status); + } return FAIL; } LOG(logINFOGREEN, ("Virtual Acquisition started\n")); @@ -1383,6 +1400,10 @@ int startStateMachine(){ #ifdef VIRTUAL void* start_timer(void* arg) { + if (!isControlServer) { + return NULL; + } + int64_t periodNs = getPeriod(); int numFrames = (getNumFrames() * getNumTriggers() ); @@ -1409,6 +1430,8 @@ void* start_timer(void* arg) { // loop over number of frames for (frameNr = 0; frameNr != numFrames; ++frameNr) { + // update the virtual stop from stop server + ComVirtual_readStop(&virtual_stop); //check if virtual_stop is high if(virtual_stop == 1){ break; @@ -1463,9 +1486,9 @@ void* start_timer(void* arg) { closeUDPSocket(0); virtual_status = 0; - /*if (isControlServer) { - //write(pipeFDs[PIPE_WRITE], &virtual_status, sizeof(virtual_status)); - } */ + if (isControlServer) { + ComVirtual_writeStatus(virtual_status); + } LOG(logINFOBLUE, ("Finished Acquiring\n")); return NULL; } @@ -1475,7 +1498,18 @@ void* start_timer(void* arg) { int stopStateMachine(){ LOG(logINFORED, ("Stopping State Machine\n")); #ifdef VIRTUAL - virtual_stop = 0; + if (!isControlServer) { + virtual_stop = 1; + ComVirtual_writeStop(virtual_stop); + // read till status is idle + int tempStatus = 1; + while(tempStatus == 1) { + ComVirtual_readStatus(&tempStatus); + } + virtual_stop = 0; + ComVirtual_writeStop(virtual_stop); + LOG(logINFO, ("Stopped State Machine\n")); + } return OK; #endif //stop state machine @@ -1486,13 +1520,9 @@ int stopStateMachine(){ enum runStatus getRunStatus(){ #ifdef VIRTUAL - /*if (!isControlServer) { - LOG(logINFORED, ("***** reading\n")); - while (read(pipeFDs[PIPE_READ], &virtual_status, sizeof(virtual_status)) > 0) { - LOG(logINFORED, ("virtual status:%d\n", virtual_status)); - } - LOG(logINFORED, ("***** final %d\n", virtual_status)); - }*/ + if (!isControlServer) { + ComVirtual_readStatus(&virtual_status); + } if(virtual_status == 0){ LOG(logINFOBLUE, ("Status: IDLE\n")); return IDLE; @@ -1568,12 +1598,9 @@ void readFrame(int *ret, char *mess) { u_int32_t runBusy() { #ifdef VIRTUAL - /*if (!isControlServer) { - LOG(logINFORED, ("runbusy \n")); - while (read(pipeFDs[PIPE_READ], &virtual_status, sizeof(virtual_status)) > 0) { - LOG(logINFORED, ("runbusy virtual status:%d\n", virtual_status)); - } - }*/ + if (!isControlServer) { + ComVirtual_readStatus(&virtual_status); + } return virtual_status; #endif u_int32_t s = (bus_r(PAT_STATUS_REG) & PAT_STATUS_RUN_BUSY_MSK); diff --git a/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h b/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h index c41d0ef1d..0486b21af 100644 --- a/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h +++ b/slsDetectorServers/mythen3DetectorServer/slsDetectorServer_defs.h @@ -5,9 +5,6 @@ #define CTRL_SRVR_INIT_TIME_US (300 * 1000) -#define PIPE_WRITE 1 -#define PIPE_READ 0 - /* Hardware Definitions */ #define NCOUNTERS (3) #define MAX_COUNTER_MSK (0x7) diff --git a/slsDetectorServers/slsDetectorServer/include/communication_funcs.h b/slsDetectorServers/slsDetectorServer/include/communication_funcs.h index bfe60c75a..7b465af6a 100755 --- a/slsDetectorServers/slsDetectorServer/include/communication_funcs.h +++ b/slsDetectorServers/slsDetectorServer/include/communication_funcs.h @@ -11,6 +11,13 @@ typedef enum{ OTHER }intType; +// communciate with stop server +#ifdef VIRTUAL +#define FILE_STATUS "/tmp/Sls_virtual_server_status_" +#define FILE_STOP "/tmp/Sls_virtual_server_stop_" +#define FD_STATUS 0 +#define FD_STOP 1 +#endif int bindSocket(unsigned short int port_number); int acceptConnection(int socketDescriptor); diff --git a/slsDetectorServers/slsDetectorServer/include/communication_virtual.h b/slsDetectorServers/slsDetectorServer/include/communication_virtual.h new file mode 100755 index 000000000..1354f5fc7 --- /dev/null +++ b/slsDetectorServers/slsDetectorServer/include/communication_virtual.h @@ -0,0 +1,14 @@ +#pragma once +#ifdef VIRTUAL +// communciate between control and stop server + +int ComVirtual_createFiles(const int port); +void ComVirtual_setFileNames(const int port); +int ComVirtual_writeStatus(int value); +int ComVirtual_readStatus(int* value); +int ComVirtual_writeStop(int value); +int ComVirtual_readStop(int* value); +int ComVirtual_writeToFile(int value, const char* fname, const char* serverName); +int ComVirtual_readFromFile(int* value, const char* fname, const char* serverName); + +#endif diff --git a/slsDetectorServers/slsDetectorServer/src/communication_virtual.c b/slsDetectorServers/slsDetectorServer/src/communication_virtual.c new file mode 100755 index 000000000..686bae70b --- /dev/null +++ b/slsDetectorServers/slsDetectorServer/src/communication_virtual.c @@ -0,0 +1,107 @@ +#ifdef VIRTUAL +#include "communication_virtual.h" +#include "clogger.h" + +#include + +#define FILE_STATUS "/tmp/sls_virtual_server_status_" +#define FILE_STOP "/tmp/sls_virtual_server_stop_" +#define FD_STATUS 0 +#define FD_STOP 1 +#define FILE_NAME_LENGTH 1000 + +FILE* fd[2] = {NULL, NULL}; +char fnameStatus[FILE_NAME_LENGTH]; +char fnameStop[FILE_NAME_LENGTH]; +int portNumber = 0; + +int ComVirtual_createFiles(const int port) { + portNumber = port; + // control server writign status file + memset(fnameStatus, 0, FILE_NAME_LENGTH); + sprintf(fnameStatus, "%s%d", FILE_STATUS, port); + FILE* fd = NULL; + if (NULL == (fd = fopen(fnameStatus, "w"))) { + LOG(logERROR, ("Could not open the file %s for virtual communication\n", + fnameStatus)); + return 0; + } + fclose(fd); + LOG(logINFOBLUE, ("Created status file %s\n", fnameStatus)); + + // stop server writing stop file + memset(fnameStop, 0, FILE_NAME_LENGTH); + sprintf(fnameStop, "%s%d", FILE_STOP, port); + if (NULL == (fd = fopen(fnameStop, "w"))) { + LOG(logERROR, ("Could not open the file %s for virtual communication\n", + fnameStop)); + return 0; + } + fclose(fd); + LOG(logINFOBLUE, ("Created stop file %s\n", fnameStop)); + + return 1; +} + +void ComVirtual_setFileNames(const int port) { + portNumber = port; + memset(fnameStatus, 0, FILE_NAME_LENGTH); + memset(fnameStop, 0, FILE_NAME_LENGTH); + sprintf(fnameStatus, "%s%d", FILE_STATUS, port); + sprintf(fnameStatus, "%s%d", FILE_STOP, port); +} + +int ComVirtual_writeStatus(int value) { + return ComVirtual_writeToFile(value, FILE_STATUS, "Control"); +} + +int ComVirtual_readStatus(int* value) { + return ComVirtual_readFromFile(value, FILE_STATUS, "Control"); +} + +int ComVirtual_writeStop(int value) { + return ComVirtual_writeToFile(value, FILE_STOP, "Stop"); +} + +int ComVirtual_readStop(int* value) { + return ComVirtual_readFromFile(value, FILE_STOP, "Stop"); +} + +int ComVirtual_writeToFile(int value, const char* fname, const char* serverName) { + FILE* fd = NULL; + if (NULL == (fd = fopen(fname, "w"))) { + LOG(logERROR, ("Vritual %s Server [%d] could not open " + "the file %s for writing\n", + serverName, portNumber, fname)); + return 0; + } + while (fwrite(&value, sizeof(value), 1, fd) < 1) { + LOG(logERROR, ("Vritual %s Server [%d] could not write " + "to file %s\n", + serverName, portNumber, fname)); + return 0; + } + fclose(fd); + return 1; +} + +int ComVirtual_readFromFile(int* value, const char* fname, const char* serverName) { + FILE* fd = NULL; + if (NULL == (fd = fopen(fname, "r"))) { + LOG(logERROR, ("Vritual %s Server [%d] could not open " + "the file %s for reading\n", + serverName, portNumber, fname)); + return 0; + } + while (fread(value, sizeof(int), 1, fd) < 1) { + LOG(logERROR, ("Vritual %s Server [%d] could not read " + "from file %s\n", + serverName, portNumber, fname)); + return 0; + } + fclose(fd); + return 1; +} + + +#endif \ No newline at end of file diff --git a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c index d7b163324..b1c137129 100755 --- a/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c +++ b/slsDetectorServers/slsDetectorServer/src/slsDetectorServer.c @@ -7,11 +7,13 @@ #include "slsDetectorServer_funcs.h" #include "slsDetectorServer_defs.h" #include "versionAPI.h" +#ifdef VIRTUAL +#include "communication_virtual.h" +#endif #include #include #include -#include // Global variables from communication_funcs extern int isControlServer; @@ -24,9 +26,6 @@ extern int checkModuleFlag; // Global variables from slsDetectorFunctionList -#ifdef VIRTUAL -//extern int pipeFDs[2]; -#endif #ifdef GOTTHARDD extern int phaseShift; #endif @@ -55,8 +54,9 @@ int main(int argc, char *argv[]) { } int portno = DEFAULT_PORTNO; - int retval = OK; - int fd = 0; + isControlServer = 1; + debugflag = 0; + checkModuleFlag = 1; // if socket crash, ignores SISPIPE, prevents global signal handler // subsequent read/write to socket gives error - must handle locally @@ -66,7 +66,11 @@ int main(int argc, char *argv[]) { { int i; for (i = 1; i < argc; ++i) { - if(!strcasecmp(argv[i],"-devel")){ + if(!strcasecmp(argv[i],"-stopserver")) { + LOG(logINFO, ("Detected stop server\n")); + isControlServer = 0; + } + else if(!strcasecmp(argv[i],"-devel")){ LOG(logINFO, ("Detected developer mode\n")); debugflag = 1; } @@ -83,7 +87,7 @@ int main(int argc, char *argv[]) { LOG(logERROR, ("cannot decode port value %s. Exiting.\n", argv[i + 1])); return -1; } - LOG(logINFO, ("Detected control port: %d\n", portno)); + LOG(logINFO, ("Detected port: %d\n", portno)); } #ifdef GOTTHARDD else if(!strcasecmp(argv[i],"-phaseshift")){ @@ -101,62 +105,51 @@ int main(int argc, char *argv[]) { } } - - // stop server + // control server + if (isControlServer) { #ifdef STOP_SERVER - - // create pipes to communicate with stop server + // start stop server process + char cmd[MAX_STR_LENGTH]; + memset(cmd, 0, MAX_STR_LENGTH); + { + int i; + for (i = 0; i < argc; ++i) { + if (i > 0) { + strcat(cmd, " "); + } + strcat(cmd, argv[i]); + } + char temp[50]; + memset(temp, 0, sizeof(temp)); + sprintf(temp, " -stopserver -port %d &", portno + 1); + strcat(cmd, temp); + + LOG(logDEBUG1, ("Command to start stop server:%s\n", cmd)); + system(cmd); + } + LOG(logINFOBLUE, ("Control Server [%d]\n", portno)); #ifdef VIRTUAL - /*if (pipe(pipeFDs) < 0) { - LOG(logERROR, ("Could not create pipes to communicate with stop server\n")); - return -1; - }*/ + // creating files for virtual servers to communicate with each other + if (!ComVirtual_createFiles(portno)) { + return -1; + } #endif - - // fork stop server - pid_t cpid = fork(); - if (cpid < 0) { - LOG(logERROR, ("Could not create fork a stop server\n")); - return -1; - } - //fcntl(pipeFDs[PIPE_READ], F_SETFL, O_NONBLOCK); - //fcntl(pipeFDs[PIPE_WRITE], F_SETFL, O_NONBLOCK); - - // stop server (child process) - if (cpid == 0) { - isControlServer = 0; - ++portno; - LOG(logINFOBLUE, ("Stop server [%d]\n", portno)); - // change name of stop server to distinguish - char stopServerSuffix[30]; - memset(stopServerSuffix, 0, sizeof(stopServerSuffix)); - sprintf(stopServerSuffix, " Stop_Server %d", portno); - //strcat(argv[0], stopServerSuffix); -#ifdef VIRTUAL - //close(pipeFDs[PIPE_WRITE]); - //fcntl(pipeFDs[PIPE_READ], F_SETFL, O_NONBLOCK); #endif - } else { - isControlServer = 1; - LOG(logINFOBLUE, ("Control server [%d]\n", portno)); + } + // stop server + else { + LOG(logINFOBLUE, ("Stop Server [%d]\n", portno)); #ifdef VIRTUAL - //close(pipeFDs[PIPE_READ]); + ComVirtual_setFileNames(portno); #endif } -#endif - - - init_detector(); - - { // bind socket - sockfd = bindSocket(portno); - if (ret == FAIL) - return -1; - } - + // bind socket + sockfd = bindSocket(portno); + if (ret == FAIL) + return -1; // assign function table function_table(); @@ -167,8 +160,9 @@ int main(int argc, char *argv[]) { } // waits for connection + int retval = OK; while(retval != GOODBYE && retval != REBOOT) { - fd = acceptConnection(sockfd); + int fd = acceptConnection(sockfd); if (fd > 0) { retval = decode_function(fd); closeConnection(fd);