From: hadeshyp Date: Wed, 3 Feb 2010 18:32:04 +0000 (+0000) Subject: added tcp server to trbcmd, Boris, Jan X-Git-Tag: v6.0~296 X-Git-Url: https://jspc29.x-matter.uni-frankfurt.de/git/?a=commitdiff_plain;h=d1a947209c8136021a3a056e2f6ba72fd5ada93f;p=trbnettools.git added tcp server to trbcmd, Boris, Jan --- diff --git a/libtrbnet/trbcmd.c b/libtrbnet/trbcmd.c index bfd3437..7ff39b6 100644 --- a/libtrbnet/trbcmd.c +++ b/libtrbnet/trbcmd.c @@ -1,3 +1,13 @@ +/* +* Changes by Boris: +* +* 1. Original code is in a sub-routine called start() +* 2. usage has a tcp parameter +* 3. main() can enter a tcp loop or just call start() +* +*/ + + #define _GNU_SOURCE #include #include @@ -14,9 +24,28 @@ #define HEXMODE 0 #endif + +// Monitoring libraries +//********************* +#include +#include +#include +#include +#include + +// Configuration +//************** +#define MYPORT 55555 +#define BACKLOG 10 +#define READ_MEM_SIZE 1048576 +#define tcp_debug 1 // turn to 0 to suppress TCP/IP output + +static FILE * STDOUT; +static FILE * STDERR; + static int hexMode = HEXMODE; -static const char trbcmd_version[] = "$Revision: 2.39 $"; +static const char trbcmd_version[] = "$Revision: 2.40 $"; /* ---- User Buffer Size ----------------------------------------------- */ @@ -27,53 +56,54 @@ static size_t USER_BUFFER_SIZE = 0; void usage(const char *progName) { - printf("Usage: %s [-h] [-f script-file] [-n number] [-d level] " + fprintf(STDOUT, "Usage: %s [-h] [-f script-file] [-n number] [-d level] " "[-H] [-V] \n", progName); - printf("Options:\n"); - printf(" -h give this help\n"); - printf(" -f execute commands given in script-file\n"); - printf(" -n repeat COMMAND number times, -1 = endless loop\n"); - printf(" -M turn on HighMemoryMode (maximum usage is 20MByte, default: " + fprintf(STDOUT, "Options:\n"); + fprintf(STDOUT, " -h give this help\n"); + fprintf(STDOUT, " -tcp start in TCP/IP-server mode (!)\n"); + fprintf(STDOUT, " -f execute commands given in script-file\n"); + fprintf(STDOUT, " -n repeat COMMAND number times, -1 = endless loop\n"); + fprintf(STDOUT, " -M turn on HighMemoryMode (maximum usage is 20MByte, default: " "3MByte)\n"); - printf(" -d turn on Debugging Information\n"); - printf(" level 1: TRB_Package debugging\n"); - printf(" level 2: +FIFO debugging\n"); - printf(" -D FIFO DMA-Mode\n"); - printf(" -l lazy-mode: skip most consistency-checks of packages\n"); - printf(" -H hex-mode: all arguments will be interpreted " + fprintf(STDOUT, " -d turn on Debugging Information\n"); + fprintf(STDOUT, " level 1: TRB_Package debugging\n"); + fprintf(STDOUT, " level 2: +FIFO debugging\n"); + fprintf(STDOUT, " -D FIFO DMA-Mode\n"); + fprintf(STDOUT, " -l lazy-mode: skip most consistency-checks of packages\n"); + fprintf(STDOUT, " -H hex-mode: all arguments will be interpreted " "as hexadecimal-numbers\n"); - printf(" -V Version number\n"); - printf("\nCommands:\n"); - printf(" r -> " + fprintf(STDOUT, " -V Version number\n"); + fprintf(STDOUT, "\nCommands:\n"); + fprintf(STDOUT, " r -> " "read register\n"); - printf(" w -> " + fprintf(STDOUT, " w -> " "write register\n"); - printf(" rm -> " + fprintf(STDOUT, " rm -> " "read register-memory\n"); - printf(" wm -> " + fprintf(STDOUT, " wm -> " "write to register-memory from ASCII-file ('-' = stdin)\n"); - printf(" i -> " + fprintf(STDOUT, " i -> " "read unique ID\n"); - printf(" s -> " + fprintf(STDOUT, " s -> " "set trb-address\n"); - printf(" T -> " + fprintf(STDOUT, " T -> " "send trigger\n", '%'); - printf(" TR -> " + fprintf(STDOUT, " TR -> " "send trigger to RICH only\n", '%'); - printf(" I -> " + fprintf(STDOUT, " I -> " "read IPU data\n", '%'); - printf(" reload -> " + fprintf(STDOUT, " reload -> " "reload FPGA\n"); - printf(" reset -> " + fprintf(STDOUT, " reset -> " "reset TRBNetwork\n"); - printf(" comreset -> " + fprintf(STDOUT, " comreset -> " "reset Etrax-FIFO Logic\n"); - printf(" f -> " + fprintf(STDOUT, " f -> " "flush FIFO of channel\n"); - printf(" R -> " + fprintf(STDOUT, " R -> " "read register of the FPGA\n"); - printf(" W -> " + fprintf(STDOUT, " W -> " "write to register of the FPGA\n"); } @@ -81,8 +111,8 @@ void usage(const char *progName) #define CMD_SIZE 256 #define CMD_MAX_NUM 10 -int main(int argc, char ** argv) -{ + +int start(int argc, char ** argv, char* command){ FILE *scriptFile = NULL; char scriptFileName[256] = ""; char cmd[CMD_MAX_NUM][CMD_SIZE]; @@ -95,7 +125,11 @@ int main(int argc, char ** argv) int loopCtr = 0; uint16_t trgCtr = 0; /* counter for the %ctr option */ int opt; - + int i; + + for(i = 0; i < CMD_MAX_NUM; i++) { + cmd[i][0] = 0; + } /* LowMem Settings, i.e. 3 MBye maximum */ USER_BUFFER_SIZE = 786432; /* 0xc000 * 4 Byte*/ @@ -107,10 +141,10 @@ int main(int argc, char ** argv) switch (opt) { case '?': usage(basename(argv[0])); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); case 'h': usage(basename(argv[0])); - exit(EXIT_SUCCESS); + return (EXIT_SUCCESS); case 'f': strncpy(scriptFileName, optarg, 256); break; @@ -132,7 +166,7 @@ int main(int argc, char ** argv) case 'V': printf("%s %s, using libtrbnet %s\n", basename(argv[0]), trbcmd_version, trbnet_version); - exit(EXIT_SUCCESS); + return (EXIT_SUCCESS); break; case 'M': /* HighMem Setting, i.e. 20 MByte maximum 0x500000 * 4*/ @@ -147,13 +181,13 @@ int main(int argc, char ** argv) if (strlen(scriptFileName) > 0) { if (strncmp(scriptFileName, "-", 256) == 0) { scriptFile = stdin; - fprintf(stderr, "name: %s\n", "STDIN"); + fprintf(STDERR, "name: %s\n", "STDIN"); } else { scriptFile = fopen(scriptFileName, "r"); if (scriptFile == NULL) { - fprintf(stderr, "Error opening ScriptFile '%s': %s\n", + fprintf(STDERR, "Error opening ScriptFile '%s': %s\n", scriptFileName, strerror(errno)); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } } @@ -168,7 +202,7 @@ int main(int argc, char ** argv) /* Start script-file-loop */ while (scriptStatus != -1) { - if (scriptFile == NULL) { + if (scriptFile == NULL && command == NULL) { /* Get command from function-call */ unsigned int i; cmdLen = argc - optind; @@ -177,30 +211,35 @@ int main(int argc, char ** argv) } scriptStatus = -1; } else { - /* Get next command from file */ char *c = NULL; - unsigned int i; - - lineCtr++; - /* Initialize */ - for (i = 0; i < CMD_MAX_NUM; i++) { - cmd[i][0] = '\0'; - } - - if ((scriptStatus = - getline(&cmdLine, &cmdLineLen, scriptFile)) == -1) { - if (feof(scriptFile) != 0) { - /* EOF reached */ - rewind(scriptFile); - continue; - } else { - /* Error reading line */ - fprintf(stderr, "Error reading script-file\n"); - exit(EXIT_FAILURE); + + if (scriptFile != NULL) { + /* Get next command from file */ + + lineCtr++; + /* Initialize */ + for (i = 0; i < CMD_MAX_NUM; i++) { + cmd[i][0] = '\0'; } - } + if ((scriptStatus = + getline(&cmdLine, &cmdLineLen, scriptFile)) == -1) { + if (feof(scriptFile) != 0) { + /* EOF reached */ + rewind(scriptFile); + continue; + } else { + /* Error reading line */ + fprintf(STDERR, "Error reading script-file\n"); + return (EXIT_FAILURE); + } + } + } + else { + cmdLine = command; + scriptStatus = -1; + } /* Remove newline and comments */ if ((c = strchr(cmdLine, '\n')) != NULL) { *c = '\0'; @@ -210,6 +249,7 @@ int main(int argc, char ** argv) } /* Split up cmdLine */ + sscanf(cmdLine, "%s %s %s %s %s %s %s %s %s %s", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4], cmd[5], cmd[6], cmd[7], cmd[8], cmd[9]); @@ -221,8 +261,10 @@ int main(int argc, char ** argv) /* Empty Line */ continue; } - - fprintf(stdout, "#Line %d: %s\n", lineCtr, cmdLine); + + if(scriptFile != NULL) { + fprintf(STDOUT, "#Line %d: %s\n", lineCtr, cmdLine); + } } if (strncmp(cmd[0], "w", CMD_SIZE) == 0) { @@ -235,11 +277,11 @@ int main(int argc, char ** argv) if (cmdLen != 4) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); @@ -248,7 +290,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: WRITE: trb_address: 0x%04x, reg_address: 0x%04x, " "value: 0x%08x\n", trb_address, reg_address, value); @@ -256,20 +298,20 @@ int main(int argc, char ** argv) if (trb_register_write(trb_address, reg_address, value) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("write_register failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -286,11 +328,11 @@ int main(int argc, char ** argv) if (cmdLen != 3) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); @@ -298,7 +340,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: READ: trb_address: 0x%04x, " "reg_address: 0x%04x\n", trb_address, reg_address); @@ -311,15 +353,15 @@ int main(int argc, char ** argv) data, NUM_ENDPOINTS * 2); if (status == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("read_register failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { for (i = 0; i < status; i += 2) { - fprintf(stdout, "0x%04x 0x%08x\n", + fprintf(STDOUT, "0x%04x 0x%08x\n", data[i], data[i + 1]); } @@ -327,14 +369,33 @@ int main(int argc, char ** argv) if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } +//<<<<<<< trbcmd.c +// for (i = 0; i < status; i += 2) { +// fprintf(STDOUT, "0x%04x 0x%08x\n", +// data[i], data[i + 1]); +// } + + /* Check Status-Bits */ + ///////////////////////////////////////////////////////////////////////////////// + // BORIS for the HUB: + /*if ((trb_term.status_common != 0x01) || + (trb_term.status_channel != 0)) { + if (scriptFile != NULL) { + fprintf(STDERR, "Line #%d: ", lineCtr); + } + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); + }*/ + //end BORIS ///////////////////////////////////////////////////////////////////// +//======= free(data); +//>>>>>>> 2.39 } else if (strncmp(cmd[0], "rm", CMD_SIZE) == 0) { @@ -353,11 +414,11 @@ int main(int argc, char ** argv) if (cmdLen != 5) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); @@ -367,7 +428,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: READ_MEM: " "trb_address: 0x%04x, " "reg_address: 0x%04x, " @@ -383,11 +444,11 @@ int main(int argc, char ** argv) size, data, USER_BUFFER_SIZE); if (status == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("read_register_mem failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { /* Print data-buffer */ @@ -395,26 +456,45 @@ int main(int argc, char ** argv) end = p + status; while (p < end) { len = (*p >> 16) & 0xffff; - fprintf(stdout, "0x%04x 0x%04x\n", (*p++) & 0xffff, len); + fprintf(STDOUT, "0x%04x 0x%04x\n", (*p++) & 0xffff, len); for (i = 0; (i < len) && (p < end); i++) { - fprintf(stdout, "0x%04x 0x%08x\n", + fprintf(STDOUT, "0x%04x 0x%08x\n", reg_address + i, *p++); } } +//<<<<<<< trbcmd.c +// } + + /* Check Status-Bits */ + ///////////////////////////////////////////////////////////////////////////////// + // BORIS for the HUB: + /*if ((trb_term.status_common != 0x01) || + (trb_term.status_channel != 0)) { + if (scriptFile != NULL) { + fprintf(STDERR, "Line #%d: ", lineCtr); +//======= /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); +//>>>>>>> 2.39 } +//<<<<<<< trbcmd.c +// fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); +// }*/ + ///////////////////////////////////////////////////////////////////////////////// +// if (data != NULL) free(data); +//======= } free(data); +//>>>>>>> 2.39 } else if (strncmp(cmd[0], "wm", CMD_SIZE) == 0) { /*******************************************/ @@ -433,11 +513,11 @@ int main(int argc, char ** argv) if (cmdLen != 5) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); @@ -451,9 +531,9 @@ int main(int argc, char ** argv) } else { file = fopen(fileName, "r"); if (file == NULL) { - fprintf(stderr, "Error opening file '%s': %s\n", + fprintf(STDERR, "Error opening file '%s': %s\n", fileName, strerror(errno)); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } @@ -472,7 +552,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: WRITE_MEM: trb_address: 0x%04x, " "reg_address: 0x%04x, " "option: %d, " @@ -485,20 +565,20 @@ int main(int argc, char ** argv) data, size); if (status == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("write_register_memory failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -517,18 +597,18 @@ int main(int argc, char ** argv) if (cmdLen != 2) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: READ_UID: trb_address: 0x%04x\n", trb_address); } @@ -539,15 +619,15 @@ int main(int argc, char ** argv) status = trb_read_uid(trb_address, uidBuffer, NUM_ENDPOINTS * 4); if (status == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("read_uid failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { for (i = 0; (i < status) && (i < 128); i += 4) { - fprintf(stdout, "0x%04x 0x%08x%08x 0x%02x\n", + fprintf(STDOUT, "0x%04x 0x%08x%08x 0x%02x\n", uidBuffer[i + 3], uidBuffer[i], uidBuffer[i + 1], @@ -557,9 +637,9 @@ int main(int argc, char ** argv) if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -578,11 +658,11 @@ int main(int argc, char ** argv) if (cmdLen != 4) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } uid = strtoull(cmd[1], NULL, hexMode == 1 ? 16 : 0); @@ -591,7 +671,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: SET_ADDRESS: " "uid: 0x%016llx, " "endpoint: 0x%02x, " @@ -601,20 +681,20 @@ int main(int argc, char ** argv) if (trb_set_address(uid, endpoint, trb_address) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("set_address failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -632,11 +712,11 @@ int main(int argc, char ** argv) if (cmdLen != 5) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } type = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0) & 0x0f; @@ -645,7 +725,7 @@ int main(int argc, char ** argv) if (strncmp(cmd[4], "%ctr", CMD_SIZE) == 0) { if (scriptFile == NULL) { usage(basename(argv[0])); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } number = trgCtr; } else { @@ -654,7 +734,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: SEND_TRIGGER: " "type: 0x%01x, " "random: 0x%02x, " @@ -665,20 +745,20 @@ int main(int argc, char ** argv) if (trb_send_trigger(type, info, random, number) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("send_trigger failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -691,24 +771,24 @@ int main(int argc, char ** argv) if (cmdLen != 1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, "Command: RESET:\n"); + fprintf(STDERR, "Command: RESET:\n"); } if (network_reset() == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("TRBNet RESET failed"); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else if (strncmp(cmd[0], "comreset", CMD_SIZE) == 0) { @@ -719,24 +799,24 @@ int main(int argc, char ** argv) if (cmdLen != 1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, "Command: COMRESET:\n"); + fprintf(STDERR, "Command: COMRESET:\n"); } if (com_reset() == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("Etrax RESET failed"); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else if (strncmp(cmd[0], "reload", CMD_SIZE) == 0) { @@ -747,37 +827,37 @@ int main(int argc, char ** argv) if (cmdLen != 2) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } trb_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: RELOAD: trb_address: 0x%04x\n", trb_address); } if (trb_register_write(trb_address, 0x0020, 0x8000) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("FPGA reload failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -796,11 +876,11 @@ int main(int argc, char ** argv) if (cmdLen != 6) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } input = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0) & 0x03; @@ -811,7 +891,7 @@ int main(int argc, char ** argv) if (strncmp(cmd[5], "%ctr", CMD_SIZE) == 0) { if (scriptFile == NULL) { usage(basename(argv[0])); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } number = trgCtr; } else { @@ -820,7 +900,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: SEND_TRIGGER: " "input: 0x%01x, " "type: 0x%01x, " @@ -832,20 +912,20 @@ int main(int argc, char ** argv) if (trb_send_trigger_rich(input, type, info, random, number) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("send_trigger failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -866,11 +946,11 @@ int main(int argc, char ** argv) if (cmdLen != 5) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } type = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0) & 0x0f; @@ -879,7 +959,7 @@ int main(int argc, char ** argv) if (strncmp(cmd[4], "%ctr", CMD_SIZE) == 0) { if (scriptFile == NULL) { usage(basename(argv[0])); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } number = trgCtr; } else { @@ -888,7 +968,7 @@ int main(int argc, char ** argv) /* DEBUG Info */ if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: READ_IPU_DATA: " "type: 0x%01x, " "random: 0x%02x, " @@ -904,24 +984,24 @@ int main(int argc, char ** argv) buffer, USER_BUFFER_SIZE); if (status == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("read_ipu_data failed"); if (trb_errno != TRB_ENDPOINT_NOT_REACHED) { - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else { for (i = 0; i < status; i++) { - fprintf(stdout, "0x%08x\n", buffer[i]); + fprintf(STDOUT, "0x%08x\n", buffer[i]); } /* Check Status-Bits */ if ((trb_term.status_common != 0x01) || (trb_term.status_channel != 0)) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } - fprintf(stderr, "WARNING Status-Bits:\n%s\n", + fprintf(STDERR, "WARNING Status-Bits:\n%s\n", trb_strterm(trb_term)); } } @@ -938,24 +1018,24 @@ int main(int argc, char ** argv) if (cmdLen != 2) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } channel = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); if (trb_debug > 0) { - fprintf(stderr, "Command: FIFO_FLUSH_CHANNEL #%d\n", channel); + fprintf(STDERR, "Command: FIFO_FLUSH_CHANNEL #%d\n", channel); } if (trb_fifo_flush(channel) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("trb_fifo_flush failed"); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } else if (strncmp(cmd[0], "R", CMD_SIZE) == 0) { @@ -969,17 +1049,17 @@ int main(int argc, char ** argv) if (cmdLen != 2) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } reg_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: READ_FIFO_REGISTER:" "reg_address: 0x%04x\n", reg_address); @@ -987,12 +1067,12 @@ int main(int argc, char ** argv) if (fpga_register_read(reg_address, &value) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("fpga_register_read failed"); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } else { - fprintf(stdout, "0x%04x 0x%08x\n", reg_address, value); + fprintf(STDOUT, "0x%04x 0x%08x\n", reg_address, value); } } else if (strncmp(cmd[0], "W", CMD_SIZE) == 0) { @@ -1006,32 +1086,32 @@ int main(int argc, char ** argv) if (cmdLen != 3) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } reg_address = strtoul(cmd[1], NULL, hexMode == 1 ? 16 : 0); value = strtoul(cmd[2], NULL, hexMode == 1 ? 16 : 0); if (trb_debug > 0) { - fprintf(stderr, + fprintf(STDERR, "Command: WRITE_FIFO_REGISTER:" "reg_address: 0x%04x, " "value: 0x%08x\n", reg_address, value); } - + if (fpga_register_write(reg_address, value) == -1) { if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: ", lineCtr); + fprintf(STDERR, "Line #%d: ", lineCtr); } trb_error("fpga_register_write failed"); - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } - + } else { /*******************************************/ @@ -1039,11 +1119,11 @@ int main(int argc, char ** argv) /*******************************************/ if (scriptFile != NULL) { - fprintf(stderr, "Line #%d: Invalid command\n", lineCtr); + fprintf(STDERR, "Line #%d: Invalid command\n", lineCtr); } else { usage(basename(argv[0])); } - exit(EXIT_FAILURE); + return (EXIT_FAILURE); } } /* End script-file-loop */ @@ -1051,11 +1131,137 @@ int main(int argc, char ** argv) } /* End repeat-loop */ /* Cleanup */ + + if (scriptFile != NULL) { fclose(scriptFile); + free(cmdLine); } - free(cmdLine); - exit(EXIT_SUCCESS); -} + return (EXIT_SUCCESS); + +} + + + +int main(int argc, char ** argv) +{ + STDOUT = stdout; + STDERR = stderr; + + // MONITORING - s t a r t - + //************************* + + + + if (argc == 2 && strcmp(argv[1], "tcp") == 0){ + + + // TCP/IP SERVER INIT + //******************* + + int sockfd, new_fd; + struct sockaddr_in my_addr; + struct sockaddr_in their_addr; + int sin_size; + int yes = 1; + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if(sockfd == -1){ + perror("socket() error!"); + return (1); + } + else + printf("\nsocket() OK...\n"); + + my_addr.sin_family = AF_INET; + my_addr.sin_port = htons(MYPORT); + my_addr.sin_addr.s_addr = INADDR_ANY; + memset(&(my_addr.sin_zero), 0, 8); + + if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) { + perror("setsockopt() error!"); + return (1); + } + else printf("setsockopt() OK...\n"); + + if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1){ + perror("bind() error!"); + return (1); + } + else printf("bind() OK...\n"); + + if(listen(sockfd, BACKLOG) == -1){ + perror("listen() error!"); + return (1); + } + else printf("listen() OK...\n\n* server initialized [OK]\n\n"); + + sin_size = sizeof(struct sockaddr_in); + + + // Init values and memory + //*********************** + int mainStatus = 1; + int len, bytes_sent; + char command[256]; + int bytes_got; + int status = 1; + int i,j; + char buffer[32]; + char response[256000]; + + // Enter an infinite loop + // to acquire instructions + //************************ + while (mainStatus != 0){ // main loop, mainstatus can break the loop to close the server + + printf("\n\nlisten...\n\n"); + new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); + if(new_fd == -1) perror("accept() error!"); + else if (tcp_debug>0) printf("accept() OK...\n"); + write(new_fd,"\n",1); + FILE *clientout = fdopen(new_fd, "a"); + + STDOUT = clientout; + STDERR = clientout; + while (status!= 0){ // the status can break the loop... + do { + bytes_got = recv(new_fd, (void *)command, 256, 0); + } while(bytes_got == -1); // receive the instruction + command[bytes_got-2] = 0; + if (tcp_debug > 0) { + printf("received: %i\n",bytes_got); + if (bytes_got>0) fprintf(stdout,"command: %s\n",command); + else { + fprintf(stdout,"\nclient terminated\n"); + break; + } + } + + if (command[0] == '#'){ // aendern + // Monitoring commands + } else { + //trbcmd + start(argc, argv, command); + fprintf(clientout,"\n----------------------------\n\n"); + fflush(clientout); + } + + + + } + fclose(clientout); + } + + + + //********************* + // MONITORING - e n d - + } +//Run normal TRBCMD + else + start(argc, argv, NULL); + return 0; +} // end main