/* VIDARR FC7 SFP port mapping to front-end AMBs */ /* Input: text database for AMC stations and their SFP ports */ /* Output: front-end board location on the detector stave */ #include /* Standard input/output definitions */ #include /* Standard libraries definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ #include #define amcs 3 /* q-ty of fc7 stations in uTCA basket */ #define ports 16 /* number of SFP ports */ int ahex2int(char a, char b) { a = (a <= '9') ? a - '0' : (a & 0x7) + 9; b = (b <= '9') ? b - '0' : (b & 0x7) + 9; return (a << 4) + b; } int main(void) { int ncol = amcs+1, nrow = ports+1, wSize = 4; char map[ncol][nrow][wSize+1]; /* additional NULL character to terminate string */ for (int i = 0; i < ncol; i++) for (int j = 0; j < nrow; j++) strncpy(map[i][j], "XXXX", wSize+1); int readout[amcs][ports]; /* each array element represents AMB location, port range 15..8 for side A, 7..0 for side B */ for (int i = 0; i < amcs; i++) for (int j = 0; j < ports; j++) readout[i][j] = 99; /* Open file for read */ FILE *fp = fopen("backend.txt", "r"); if (fp == NULL) {printf("no ini file found\n"); return (0);} fseek (fp, 0, SEEK_END); long tSize = ftell(fp); rewind(fp); // printf("file size in bytes %ld\n", tSize); /* allocate buffer for the entire file */ char *text = (char *) malloc(sizeof(char) *tSize); /* memory for the entire file */ char **lines = (char **)malloc(sizeof(void *)*tSize); /* memory for lines */ char **words = (char **)malloc(sizeof(void *)*tSize); /* memory for words */ if (text == NULL || lines == NULL || words == NULL) {perror("Unable to allocate memory for data\n"); exit(1);} /* copy the entire file into buffer */ fread(text, 1, tSize, fp); fclose(fp); /* get rid of these pesky CRs (this will result in empty lines) */ char *index = text; for (int i = 0; i < tSize; i++) if (*(index + i) == '\r') *(index + i) = '\n'; /* int i will be casted to long */ /* split text into lines and get rid of empty ones */ int lindex = 0, nscan = 0; while ((( *(lines + lindex) = strsep(&text, "\n")) != NULL)) { char *ltoken = strsep((lines + lindex), "\n"); if (*ltoken != 0x00 && nscan < nrow) { const char delimiters[] = " ,;:\t"; int windex = 0, nrun = 0; while ((( *(words + windex) = strsep(<oken, delimiters)) != NULL)) { char *wtoken = strsep((words + windex), delimiters); if (*wtoken != 0x00 && nrun < ncol) { // printf("col %02d row %02d %s\n", nrun, nscan, wtoken); strncpy(map[nrun][nscan], wtoken, wSize+1); nrun++; } /* if wtoken */ windex++; } /* while windex */ if (nrun < ncol) ncol = nrun; nscan++; } /* if ltoken */ lindex++; } /* while lindex */ if (nscan < nrow) nrow = nscan; /* free up memory */ free (text); free (lines); free (words); /* wrap ncol / nrow values to avoid segmentation fault */ // printf("columns %d rows %d\n", ncol, nrow); if (ncol > amcs + 1) ncol = amcs + 1; if (nrow > ports + 1) nrow = ports + 1; /* fill output array */ for (int i = 1; i < ncol; i++) for (int j = 1; j < nrow; j++) { char *fc7 = map[i][0]; char b[] = "0"; int amc = atoi(strncpy(b, fc7+wSize/2+1, 1)); char *sfp = map[0][j]; char sfpa = sfp[wSize/2]; char sfpb = sfp[wSize/2+1]; int bit = ahex2int(sfpa, sfpb); char *side = (bit < 8) ? "B" : "A"; char *element = map[i][j]; char arg[] = "99"; strncpy(arg, element, wSize/2); /* just replace content without touching NULL */ int amb = atoi(arg); /* test range for AMC and SFP index */ if (amc < 4 || amc > 6 || bit < 0 || bit > 15) printf("values out of range %02d %02d\n", amc, bit); else readout[amc-4][bit] = (strcmp(element, "NCNC") != 0) ? amb : 99; if (bit == 0) printf("\n"); printf("FC7 #%01d port %02d FE map index %02d side %s\n", amc, bit, readout[i-1][j-1], side); } /* for i,j */ return 1; } /* main */