diff --git a/slsDetectorServers/slsDetectorServer/include/loadPattern.h b/slsDetectorServers/slsDetectorServer/include/loadPattern.h index e99d5c7fb..d2e2e43ff 100644 --- a/slsDetectorServers/slsDetectorServer/include/loadPattern.h +++ b/slsDetectorServers/slsDetectorServer/include/loadPattern.h @@ -3,10 +3,6 @@ #include "Pattern.h" #include "clogger.h" -int loadPattern(char *mess, enum TLogLevel printLevel, patternParameters *pat); -int getPattern(char *mess, patternParameters *pat); -int loadPatternFile(char *patFname, char *errMessage); - #if defined(CHIPTESTBOARDD) || defined(MOENCHD) #ifdef VIRTUAL void initializePatternWord(); @@ -55,4 +51,9 @@ uint64_t getPatternBitMask(); #ifdef MYTHEN3D void startPattern(); #endif + +int loadPattern(char *mess, enum TLogLevel printLevel, patternParameters *pat); +int getPattern(char *mess, patternParameters *pat); +int loadPatternFile(char *patFname, char *errMessage); + #endif diff --git a/slsDetectorServers/slsDetectorServer/src/loadPattern.c b/slsDetectorServers/slsDetectorServer/src/loadPattern.c index 5ba40ccc1..0b4c42dc5 100644 --- a/slsDetectorServers/slsDetectorServer/src/loadPattern.c +++ b/slsDetectorServers/slsDetectorServer/src/loadPattern.c @@ -23,388 +23,6 @@ extern u_int32_t bus_r(u_int32_t offset); extern int64_t get64BitReg(int aLSB, int aMSB); extern int64_t set64BitReg(int64_t value, int aLSB, int aMSB); -int loadPattern(char *message, enum TLogLevel printLevel, - patternParameters *pat) { - LOG(logINFOBLUE, ("Loading Pattern from structure\n")); - int ret = OK; -#ifdef MYTHEN3D - trimmingPrint = printLevel; -#endif - // words - for (int i = 0; i < MAX_PATTERN_LENGTH; ++i) { - if ((i % 10 == 0) && pat->word[i] != 0) { - LOG(logDEBUG5, ("Setting Pattern Word (addr:0x%x, word:0x%llx)\n", - i, (long long int)pat->word[i])); - } - ret = validate_writePatternWord(message, i, pat->word[i]); - if (ret == FAIL) { - break; - } - } - // iocontrol -#ifndef MYTHEN3D - if (ret == OK) { - ret = validate_writePatternIOControl(message, pat->ioctrl); - } -#endif - // limits - if (ret == OK) { - ret = validate_setPatternLoopLimits(message, pat->limits[0], - pat->limits[1]); - } - - if (ret == OK) { - for (int i = 0; i <= 2; ++i) { - // loop addr - ret = validate_setPatternLoopAddresses( - message, i, pat->loop[i * 2 + 0], pat->loop[i * 2 + 1]); - if (ret == FAIL) { - break; - } - - // num loops - ret = validate_setPatternLoopCycles(message, i, pat->nloop[i]); - if (ret == FAIL) { - break; - } - - // wait addr - ret = validate_setPatternWaitAddresses(message, i, pat->wait[i]); - if (ret == FAIL) { - break; - } - - // wait time - ret = validate_setPatternWaitTime(message, i, pat->waittime[i]); - if (ret == FAIL) { - break; - } - } - } -#ifdef MYTHEN3D - trimmingPrint = logINFO; -#endif - return ret; -} - -int getPattern(char *message, patternParameters *pat) { - LOG(logINFO, ("Getting Pattern into structure\n")); - - int ret = OK; - uint64_t retval64 = 0; - int retval1 = -1, retval2 = -1; - // words - for (int i = 0; i < MAX_PATTERN_LENGTH; ++i) { - ret = validate_readPatternWord(message, i, &retval64); - if (ret == FAIL) { - break; - } - pat->word[i] = retval64; - } - // iocontrol -#ifndef MYTHEN3D - if (ret == OK) { - validate_readPatternIOControl(); - } -#endif - // limits - if (ret == OK) { - validate_getPatternLoopLimits(&retval1, &retval2); - pat->limits[0] = retval1; - pat->limits[1] = retval2; - } - if (ret == OK) { - for (int i = 0; i <= 2; ++i) { - // loop addr - ret = validate_getPatternLoopAddresses(message, i, &retval1, - &retval2); - if (ret == FAIL) { - break; - } - pat->loop[i * 2 + 0] = retval1; - pat->loop[i * 2 + 1] = retval2; - - // num loops - ret = validate_getPatternLoopCycles(message, i, &retval1); - if (ret == FAIL) { - break; - } - pat->nloop[i] = retval1; - - // wait addr - ret = validate_getPatternWaitAddresses(message, i, &retval1); - if (ret == FAIL) { - break; - } - pat->wait[i] = retval1; - - // wait time - ret = validate_getPatternWaitTime(message, i, &retval64); - if (ret == FAIL) { - break; - } - pat->waittime[i] = retval64; - } - } - return ret; -} - -int loadPatternFile(char *patFname, char *errMessage) { - char fname[128]; - if (getAbsPath(fname, 128, patFname) == FAIL) { - return FAIL; - } - - // open config file - FILE *fd = fopen(fname, "r"); - if (fd == NULL) { - sprintf(errMessage, "Could not open pattern file [%s].\n", patFname); - LOG(logERROR, ("%s\n\n", errMessage)); - return FAIL; - } - LOG(logINFOBLUE, ("Reading default pattern file %s\n", patFname)); - - // Initialization - const size_t LZ = 256; - char line[LZ]; - memset(line, 0, LZ); - char command[LZ]; - char temp[MAX_STR_LENGTH]; - memset(temp, 0, MAX_STR_LENGTH); - - // keep reading a line - while (fgets(line, LZ, fd)) { - - // ignore comments - if (line[0] == '#') { - LOG(logDEBUG1, ("Ignoring Comment\n")); - continue; - } - - // ignore empty lines - if (strlen(line) <= 1) { - LOG(logDEBUG1, ("Ignoring Empty line\n")); - continue; - } - - // removing leading spaces - if (line[0] == ' ' || line[0] == '\t') { - int len = strlen(line); - // find first valid character - int i = 0; - for (i = 0; i < len; ++i) { - if (line[i] != ' ' && line[i] != '\t') { - break; - } - } - // ignore the line full of spaces (last char \n) - if (i >= len - 1) { - LOG(logDEBUG1, ("Ignoring line full of spaces\n")); - continue; - } - // copying only valid char - char temp[LZ]; - memset(temp, 0, LZ); - memcpy(temp, line + i, strlen(line) - i); - memset(line, 0, LZ); - memcpy(line, temp, strlen(temp)); - LOG(logDEBUG1, ("Removing leading spaces.\n")); - } - - LOG(logDEBUG1, ("Command to process: (size:%d) %.*s\n", strlen(line), - strlen(line) - 1, line)); - memset(command, 0, LZ); - - // patword - if (!strncmp(line, "patword", strlen("patword"))) { - int addr = 0; - uint64_t word = 0; - - // cannot scan values -#ifdef VIRTUAL - if (sscanf(line, "%s 0x%x 0x%lx", command, &addr, &word) != 3) { -#else - if (sscanf(line, "%s 0x%x 0x%llx", command, &addr, &word) != 3) { -#endif - strcpy(temp, "Could not scan patword arguments.\n"); - break; - } - - if (validate_writePatternWord(temp, addr, word) == FAIL) { - break; - } - } - - // patioctrl -#ifndef MYTHEN3D - if (!strncmp(line, "patioctrl", strlen("patioctrl"))) { - uint64_t arg = 0; - - // cannot scan values -#ifdef VIRTUAL - if (sscanf(line, "%s 0x%lx", command, &arg) != 2) { -#else - if (sscanf(line, "%s 0x%llx", command, &arg) != 2) { -#endif - strcpy(temp, "Could not scan patioctrl arguments.\n"); - break; - } - - if (validate_writePatternIOControl(temp, arg) == FAIL) { - break; - } - } -#endif - - // patlimits - if (!strncmp(line, "patlimits", strlen("patlimits"))) { - int startAddr = 0; - int stopAddr = 0; - - // cannot scan values - if (sscanf(line, "%s 0x%x 0x%x", command, &startAddr, &stopAddr) != - 3) { - strcpy(temp, "Could not scan patlimits arguments.\n"); - break; - } - - if (validate_setPatternLoopLimits(temp, startAddr, stopAddr) == - FAIL) { - break; - } - } - - // patloop - if ((!strncmp(line, "patloop0", strlen("patloop0"))) || - (!strncmp(line, "patloop1", strlen("patloop1"))) || - (!strncmp(line, "patloop2", strlen("patloop2")))) { - - // level - int level = -1; - if (!strncmp(line, "patloop0", strlen("patloop0"))) { - level = 0; - } else if (!strncmp(line, "patloop1", strlen("patloop1"))) { - level = 1; - } else { - level = 2; - } - - int startAddr = 0; - int stopAddr = 0; - // cannot scan values - if (sscanf(line, "%s 0x%x 0x%x", command, &startAddr, &stopAddr) != - 3) { - sprintf(temp, "Could not scan patloop%d arguments.\n", level); - break; - } - - if (validate_setPatternLoopAddresses(temp, level, startAddr, - stopAddr) == FAIL) { - break; - } - } - - // patnloop - if ((!strncmp(line, "patnloop0", strlen("patnloop0"))) || - (!strncmp(line, "patnloop1", strlen("patnloop1"))) || - (!strncmp(line, "patnloop2", strlen("patnloop2")))) { - - // level - int level = -1; - if (!strncmp(line, "patnloop0", strlen("patnloop0"))) { - level = 0; - } else if (!strncmp(line, "patnloop1", strlen("patnloop1"))) { - level = 1; - } else { - level = 2; - } - - int numLoops = -1; - // cannot scan values - if (sscanf(line, "%s %d", command, &numLoops) != 2) { - sprintf(temp, "Could not scan patnloop %d arguments.\n", level); - break; - } - - if (validate_setPatternLoopCycles(temp, level, numLoops) == FAIL) { - break; - } - } - - // patwait - if ((!strncmp(line, "patwait0", strlen("patwait0"))) || - (!strncmp(line, "patwait1", strlen("patwait1"))) || - (!strncmp(line, "patwait2", strlen("patwait2")))) { - - // level - int level = -1; - if (!strncmp(line, "patwait0", strlen("patwait0"))) { - level = 0; - } else if (!strncmp(line, "patwait1", strlen("patwait1"))) { - level = 1; - } else { - level = 2; - } - - int addr = 0; - // cannot scan values - if (sscanf(line, "%s 0x%x", command, &addr) != 2) { - sprintf(temp, "Could not scan patwait%d arguments.\n", level); - break; - } - - if (validate_setPatternWaitAddresses(temp, level, addr) == FAIL) { - break; - } - } - - // patwaittime - if ((!strncmp(line, "patwaittime0", strlen("patwaittime0"))) || - (!strncmp(line, "patwaittime1", strlen("patwaittime1"))) || - (!strncmp(line, "patwaittime2", strlen("patwaittime2")))) { - - // level - int level = -1; - if (!strncmp(line, "patwaittime0", strlen("patwaittime0"))) { - level = 0; - } else if (!strncmp(line, "patwaittime1", strlen("patwaittime1"))) { - level = 1; - } else { - level = 2; - } - - uint64_t waittime = 0; - - // cannot scan values -#ifdef VIRTUAL - if (sscanf(line, "%s %ld", command, &waittime) != 2) { -#else - if (sscanf(line, "%s %lld", command, &waittime) != 2) { -#endif - sprintf(temp, "Could not scan patwaittime%d arguments.\n", - level); - break; - } - - if (validate_setPatternWaitTime(temp, level, waittime) == FAIL) { - break; - } - } - - memset(line, 0, LZ); - } - - fclose(fd); - - if (strlen(temp)) { - sprintf(errMessage, "%s(Default pattern file. Line: %s)\n", temp, line); - return FAIL; - } - - LOG(logINFOBLUE, ("Successfully read default pattern file\n")); - return OK; -} - #if defined(CHIPTESTBOARDD) || defined(MOENCHD) #ifdef VIRTUAL void initializePatternWord() { @@ -973,4 +591,386 @@ void startPattern() { } LOG(logINFOBLUE, ("Pattern done\n")); } -#endif \ No newline at end of file +#endif + +int loadPattern(char *message, enum TLogLevel printLevel, + patternParameters *pat) { + LOG(logINFOBLUE, ("Loading Pattern from structure\n")); + int ret = OK; +#ifdef MYTHEN3D + trimmingPrint = printLevel; +#endif + // words + for (int i = 0; i < MAX_PATTERN_LENGTH; ++i) { + if ((i % 10 == 0) && pat->word[i] != 0) { + LOG(logDEBUG5, ("Setting Pattern Word (addr:0x%x, word:0x%llx)\n", + i, (long long int)pat->word[i])); + } + ret = validate_writePatternWord(message, i, pat->word[i]); + if (ret == FAIL) { + break; + } + } + // iocontrol +#ifndef MYTHEN3D + if (ret == OK) { + ret = validate_writePatternIOControl(message, pat->ioctrl); + } +#endif + // limits + if (ret == OK) { + ret = validate_setPatternLoopLimits(message, pat->limits[0], + pat->limits[1]); + } + + if (ret == OK) { + for (int i = 0; i <= 2; ++i) { + // loop addr + ret = validate_setPatternLoopAddresses( + message, i, pat->loop[i * 2 + 0], pat->loop[i * 2 + 1]); + if (ret == FAIL) { + break; + } + + // num loops + ret = validate_setPatternLoopCycles(message, i, pat->nloop[i]); + if (ret == FAIL) { + break; + } + + // wait addr + ret = validate_setPatternWaitAddresses(message, i, pat->wait[i]); + if (ret == FAIL) { + break; + } + + // wait time + ret = validate_setPatternWaitTime(message, i, pat->waittime[i]); + if (ret == FAIL) { + break; + } + } + } +#ifdef MYTHEN3D + trimmingPrint = logINFO; +#endif + return ret; +} + +int getPattern(char *message, patternParameters *pat) { + LOG(logINFO, ("Getting Pattern into structure\n")); + + int ret = OK; + uint64_t retval64 = 0; + int retval1 = -1, retval2 = -1; + // words + for (int i = 0; i < MAX_PATTERN_LENGTH; ++i) { + ret = validate_readPatternWord(message, i, &retval64); + if (ret == FAIL) { + break; + } + pat->word[i] = retval64; + } + // iocontrol +#ifndef MYTHEN3D + if (ret == OK) { + validate_readPatternIOControl(); + } +#endif + // limits + if (ret == OK) { + validate_getPatternLoopLimits(&retval1, &retval2); + pat->limits[0] = retval1; + pat->limits[1] = retval2; + } + if (ret == OK) { + for (int i = 0; i <= 2; ++i) { + // loop addr + ret = validate_getPatternLoopAddresses(message, i, &retval1, + &retval2); + if (ret == FAIL) { + break; + } + pat->loop[i * 2 + 0] = retval1; + pat->loop[i * 2 + 1] = retval2; + + // num loops + ret = validate_getPatternLoopCycles(message, i, &retval1); + if (ret == FAIL) { + break; + } + pat->nloop[i] = retval1; + + // wait addr + ret = validate_getPatternWaitAddresses(message, i, &retval1); + if (ret == FAIL) { + break; + } + pat->wait[i] = retval1; + + // wait time + ret = validate_getPatternWaitTime(message, i, &retval64); + if (ret == FAIL) { + break; + } + pat->waittime[i] = retval64; + } + } + return ret; +} + +int loadPatternFile(char *patFname, char *errMessage) { + char fname[128]; + if (getAbsPath(fname, 128, patFname) == FAIL) { + return FAIL; + } + + // open config file + FILE *fd = fopen(fname, "r"); + if (fd == NULL) { + sprintf(errMessage, "Could not open pattern file [%s].\n", patFname); + LOG(logERROR, ("%s\n\n", errMessage)); + return FAIL; + } + LOG(logINFOBLUE, ("Reading default pattern file %s\n", patFname)); + + // Initialization + const size_t LZ = 256; + char line[LZ]; + memset(line, 0, LZ); + char command[LZ]; + char temp[MAX_STR_LENGTH]; + memset(temp, 0, MAX_STR_LENGTH); + + // keep reading a line + while (fgets(line, LZ, fd)) { + + // ignore comments + if (line[0] == '#') { + LOG(logDEBUG1, ("Ignoring Comment\n")); + continue; + } + + // ignore empty lines + if (strlen(line) <= 1) { + LOG(logDEBUG1, ("Ignoring Empty line\n")); + continue; + } + + // removing leading spaces + if (line[0] == ' ' || line[0] == '\t') { + int len = strlen(line); + // find first valid character + int i = 0; + for (i = 0; i < len; ++i) { + if (line[i] != ' ' && line[i] != '\t') { + break; + } + } + // ignore the line full of spaces (last char \n) + if (i >= len - 1) { + LOG(logDEBUG1, ("Ignoring line full of spaces\n")); + continue; + } + // copying only valid char + char temp[LZ]; + memset(temp, 0, LZ); + memcpy(temp, line + i, strlen(line) - i); + memset(line, 0, LZ); + memcpy(line, temp, strlen(temp)); + LOG(logDEBUG1, ("Removing leading spaces.\n")); + } + + LOG(logDEBUG1, ("Command to process: (size:%d) %.*s\n", strlen(line), + strlen(line) - 1, line)); + memset(command, 0, LZ); + + // patword + if (!strncmp(line, "patword", strlen("patword"))) { + int addr = 0; + uint64_t word = 0; + + // cannot scan values +#ifdef VIRTUAL + if (sscanf(line, "%s 0x%x 0x%lx", command, &addr, &word) != 3) { +#else + if (sscanf(line, "%s 0x%x 0x%llx", command, &addr, &word) != 3) { +#endif + strcpy(temp, "Could not scan patword arguments.\n"); + break; + } + + if (validate_writePatternWord(temp, addr, word) == FAIL) { + break; + } + } + + // patioctrl +#ifndef MYTHEN3D + if (!strncmp(line, "patioctrl", strlen("patioctrl"))) { + uint64_t arg = 0; + + // cannot scan values +#ifdef VIRTUAL + if (sscanf(line, "%s 0x%lx", command, &arg) != 2) { +#else + if (sscanf(line, "%s 0x%llx", command, &arg) != 2) { +#endif + strcpy(temp, "Could not scan patioctrl arguments.\n"); + break; + } + + if (validate_writePatternIOControl(temp, arg) == FAIL) { + break; + } + } +#endif + + // patlimits + if (!strncmp(line, "patlimits", strlen("patlimits"))) { + int startAddr = 0; + int stopAddr = 0; + + // cannot scan values + if (sscanf(line, "%s 0x%x 0x%x", command, &startAddr, &stopAddr) != + 3) { + strcpy(temp, "Could not scan patlimits arguments.\n"); + break; + } + + if (validate_setPatternLoopLimits(temp, startAddr, stopAddr) == + FAIL) { + break; + } + } + + // patloop + if ((!strncmp(line, "patloop0", strlen("patloop0"))) || + (!strncmp(line, "patloop1", strlen("patloop1"))) || + (!strncmp(line, "patloop2", strlen("patloop2")))) { + + // level + int level = -1; + if (!strncmp(line, "patloop0", strlen("patloop0"))) { + level = 0; + } else if (!strncmp(line, "patloop1", strlen("patloop1"))) { + level = 1; + } else { + level = 2; + } + + int startAddr = 0; + int stopAddr = 0; + // cannot scan values + if (sscanf(line, "%s 0x%x 0x%x", command, &startAddr, &stopAddr) != + 3) { + sprintf(temp, "Could not scan patloop%d arguments.\n", level); + break; + } + + if (validate_setPatternLoopAddresses(temp, level, startAddr, + stopAddr) == FAIL) { + break; + } + } + + // patnloop + if ((!strncmp(line, "patnloop0", strlen("patnloop0"))) || + (!strncmp(line, "patnloop1", strlen("patnloop1"))) || + (!strncmp(line, "patnloop2", strlen("patnloop2")))) { + + // level + int level = -1; + if (!strncmp(line, "patnloop0", strlen("patnloop0"))) { + level = 0; + } else if (!strncmp(line, "patnloop1", strlen("patnloop1"))) { + level = 1; + } else { + level = 2; + } + + int numLoops = -1; + // cannot scan values + if (sscanf(line, "%s %d", command, &numLoops) != 2) { + sprintf(temp, "Could not scan patnloop %d arguments.\n", level); + break; + } + + if (validate_setPatternLoopCycles(temp, level, numLoops) == FAIL) { + break; + } + } + + // patwait + if ((!strncmp(line, "patwait0", strlen("patwait0"))) || + (!strncmp(line, "patwait1", strlen("patwait1"))) || + (!strncmp(line, "patwait2", strlen("patwait2")))) { + + // level + int level = -1; + if (!strncmp(line, "patwait0", strlen("patwait0"))) { + level = 0; + } else if (!strncmp(line, "patwait1", strlen("patwait1"))) { + level = 1; + } else { + level = 2; + } + + int addr = 0; + // cannot scan values + if (sscanf(line, "%s 0x%x", command, &addr) != 2) { + sprintf(temp, "Could not scan patwait%d arguments.\n", level); + break; + } + + if (validate_setPatternWaitAddresses(temp, level, addr) == FAIL) { + break; + } + } + + // patwaittime + if ((!strncmp(line, "patwaittime0", strlen("patwaittime0"))) || + (!strncmp(line, "patwaittime1", strlen("patwaittime1"))) || + (!strncmp(line, "patwaittime2", strlen("patwaittime2")))) { + + // level + int level = -1; + if (!strncmp(line, "patwaittime0", strlen("patwaittime0"))) { + level = 0; + } else if (!strncmp(line, "patwaittime1", strlen("patwaittime1"))) { + level = 1; + } else { + level = 2; + } + + uint64_t waittime = 0; + + // cannot scan values +#ifdef VIRTUAL + if (sscanf(line, "%s %ld", command, &waittime) != 2) { +#else + if (sscanf(line, "%s %lld", command, &waittime) != 2) { +#endif + sprintf(temp, "Could not scan patwaittime%d arguments.\n", + level); + break; + } + + if (validate_setPatternWaitTime(temp, level, waittime) == FAIL) { + break; + } + } + + memset(line, 0, LZ); + } + + fclose(fd); + + if (strlen(temp)) { + sprintf(errMessage, "%s(Default pattern file. Line: %s)\n", temp, line); + return FAIL; + } + + LOG(logINFOBLUE, ("Successfully read default pattern file\n")); + return OK; +} \ No newline at end of file