| 1 |
|
|---|
| 2 | /********************************************************************\
|
|---|
| 3 |
|
|---|
| 4 | ProcessIO.cc
|
|---|
| 5 |
|
|---|
| 6 | Main class processing user input
|
|---|
| 7 |
|
|---|
| 8 | Sebastian Commichau, Sabrina Stark-Schneebeli, Oliver Grimm
|
|---|
| 9 |
|
|---|
| 10 | \********************************************************************/
|
|---|
| 11 |
|
|---|
| 12 | #include "ProcessIO.h"
|
|---|
| 13 |
|
|---|
| 14 | static char* state_str[] = {"active", "stopped", "n.a."};
|
|---|
| 15 |
|
|---|
| 16 | // Branch table for command evaluation
|
|---|
| 17 | static const struct CL_Struct { const char *Name;
|
|---|
| 18 | void (ProcessIO::*CommandPointer)();
|
|---|
| 19 | unsigned int MinNumParameter;
|
|---|
| 20 | const char *Parameters;
|
|---|
| 21 | const char *Help;
|
|---|
| 22 | } CommandList[] =
|
|---|
| 23 | {{"board", &ProcessIO::cmd_board, 1, "<i>|<i j>|<all>" ,"Address board i, boards i-j or all boards or list boards"},
|
|---|
| 24 | {"hv", &ProcessIO::cmd_hv, 2, "<id>|<ch>|<all> <v>", "Change bias of pixel or (all) chan. of active boards"},
|
|---|
| 25 | {"status", &ProcessIO::cmd_status, 0, "[dac]", "Show status information (DAC values if requested)"},
|
|---|
| 26 | {"config", &ProcessIO::cmd_config, 0, "", "Print configuration"},
|
|---|
| 27 | {"load", &ProcessIO::cmd_load, 1, "<file>", "Load and set bias settings from file"},
|
|---|
| 28 | {"save", &ProcessIO::cmd_save, 1, "<file>", "Save current bias settings to file"},
|
|---|
| 29 | {"exit", &ProcessIO::cmd_exit, 0, "", "Exit program"},
|
|---|
| 30 | {"rate", &ProcessIO::cmd_rate, 1, "<rate>", "Set status refresh rate in Hz"},
|
|---|
| 31 | {"timeout", &ProcessIO::cmd_timeout, 1, "<time>", "Set timeout to return from read in seconds"},
|
|---|
| 32 | {"reset", &ProcessIO::cmd_reset, 0, "", "Reset active bias board"},
|
|---|
| 33 | {"start", &ProcessIO::cmd_start, 0, "", "Start bias status monitor"},
|
|---|
| 34 | {"stop", &ProcessIO::cmd_stop, 0, "", "Stop bias status monitor"},
|
|---|
| 35 | {"uptime", &ProcessIO::cmd_uptime, 0, "", "Get program uptime"},
|
|---|
| 36 | {"help", &ProcessIO::cmd_help, 0, "", "Print help"}};
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | using namespace std;
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | // Constructor
|
|---|
| 43 | ProcessIO::ProcessIO(): EvidenceServer(SERVER_NAME) {
|
|---|
| 44 |
|
|---|
| 45 | // Get program start time
|
|---|
| 46 | time (&StartTime);
|
|---|
| 47 |
|
|---|
| 48 | // Initialize status variables
|
|---|
| 49 | state = active;
|
|---|
| 50 | ConsoleText = NULL;
|
|---|
| 51 |
|
|---|
| 52 | NumHVBoards = 0;
|
|---|
| 53 | FirstBoard = 0;
|
|---|
| 54 | LastBoard = -1;
|
|---|
| 55 |
|
|---|
| 56 | // Initialize mutex for thread synchronisation
|
|---|
| 57 | if (pthread_mutex_init(&Mutex, NULL) != 0) {
|
|---|
| 58 | Message(FATAL, "pthread_mutex_init() failed");
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | // DIM console service used in PrintMessage()
|
|---|
| 62 | ConsoleOut = new DimService(SERVER_NAME"/ConsoleOut", (char *) "");
|
|---|
| 63 |
|
|---|
| 64 | // Get configuration data
|
|---|
| 65 | char *Boards = GetConfig("Boards");
|
|---|
| 66 | fPixMapTable = GetConfig("PixMapTable");
|
|---|
| 67 | fTimeOut = atof(GetConfig("TimeOut"));
|
|---|
| 68 | fStatusRefreshRate = atof(GetConfig("StatusRefreshRate"));
|
|---|
| 69 | DACMin = atoi(GetConfig("DACMin"));
|
|---|
| 70 | DACMax = atoi(GetConfig("DACMax"));
|
|---|
| 71 | fHVCalibOffset = atof(GetConfig("HVCalibOffset"));
|
|---|
| 72 | fHVCalibSlope = atof(GetConfig("HVCalibSlope"));
|
|---|
| 73 | fHVMaxDiff = atoi(GetConfig("HVMaxDiff"));
|
|---|
| 74 |
|
|---|
| 75 | if (fStatusRefreshRate < MIN_RATE || fStatusRefreshRate > MAX_RATE) fStatusRefreshRate = 1;
|
|---|
| 76 |
|
|---|
| 77 | // Open HV devices
|
|---|
| 78 | fHVBoard = new HVBoard* [strlen(Boards)];
|
|---|
| 79 | char *Token = strtok(Boards, " \t");
|
|---|
| 80 | while (Token != NULL) {
|
|---|
| 81 | fHVBoard[NumHVBoards] = new HVBoard(NumHVBoards, Token, this);
|
|---|
| 82 | if(fHVBoard[NumHVBoards]->fDescriptor >= 0) {
|
|---|
| 83 | PrintMessage("Synchronized and reset board %d (%s)\n",NumHVBoards,fHVBoard[NumHVBoards]->BoardName);
|
|---|
| 84 | NumHVBoards++;
|
|---|
| 85 | }
|
|---|
| 86 | else {
|
|---|
| 87 | PrintMessage(All, "Failed to synchronize board %d (%s)\n",NumHVBoards,fHVBoard[NumHVBoards]->BoardName);
|
|---|
| 88 | delete fHVBoard[NumHVBoards];
|
|---|
| 89 | }
|
|---|
| 90 | Token = strtok(NULL, " \t");
|
|---|
| 91 | }
|
|---|
| 92 | LastBoard = NumHVBoards-1;
|
|---|
| 93 |
|
|---|
| 94 | // Create instances
|
|---|
| 95 | calib = new HVCalib(this);
|
|---|
| 96 | pm = new PixelMap(fPixMapTable);
|
|---|
| 97 |
|
|---|
| 98 | // Install DIM command (after all initialized)
|
|---|
| 99 | Command = new DimCommand((char *) SERVER_NAME"/Command", (char *) "C", this);
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | // Destructor
|
|---|
| 104 | ProcessIO::~ProcessIO() {
|
|---|
| 105 |
|
|---|
| 106 | delete Command;
|
|---|
| 107 |
|
|---|
| 108 | for (int i=0; i<NumHVBoards; i++) delete fHVBoard[i];
|
|---|
| 109 | delete[] fHVBoard;
|
|---|
| 110 |
|
|---|
| 111 | delete pm;
|
|---|
| 112 | delete calib;
|
|---|
| 113 | delete ConsoleOut;
|
|---|
| 114 | free(ConsoleText);
|
|---|
| 115 |
|
|---|
| 116 | // Destroy mutex
|
|---|
| 117 | if (pthread_mutex_destroy(&Mutex) != 0) Message(ERROR, "pthread_mutex_destroy() failed");
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | // Process user input
|
|---|
| 122 | void ProcessIO::CommandControl(char *Command) {
|
|---|
| 123 |
|
|---|
| 124 | // Ignore empty commands
|
|---|
| 125 | if (strlen(Command)==0) return;
|
|---|
| 126 |
|
|---|
| 127 | // Shell command
|
|---|
| 128 | if(Command[0]=='.') {
|
|---|
| 129 | system(&(Command[1]));
|
|---|
| 130 | return;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | // Parse command into tokens
|
|---|
| 134 | Parameter.clear();
|
|---|
| 135 | char *Start;
|
|---|
| 136 | while(true) {
|
|---|
| 137 | while (isspace(*Command)) Command++; // Ignore initial white spaces
|
|---|
| 138 | if(*Command=='\0') break;
|
|---|
| 139 | if (*Command == '\"') {
|
|---|
| 140 | Start = ++Command;
|
|---|
| 141 | while(*Command!='\"' && *Command!='\0') Command++;
|
|---|
| 142 | }
|
|---|
| 143 | else {
|
|---|
| 144 | Start = Command;
|
|---|
| 145 | while(!isspace(*Command) && *Command!='\0') Command++;
|
|---|
| 146 | }
|
|---|
| 147 | if(*Command != '\0') *Command++ = '\0';
|
|---|
| 148 | Parameter.push_back(Start);
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | // Search for command in command list
|
|---|
| 152 | for(unsigned int CmdNumber=0; CmdNumber<sizeof(CommandList)/sizeof(CL_Struct); CmdNumber++) {
|
|---|
| 153 | if (Match(Parameter[0], CommandList[CmdNumber].Name)) {
|
|---|
| 154 | if(Parameter.size()-1 < CommandList[CmdNumber].MinNumParameter) {
|
|---|
| 155 | PrintMessage("Usage: %s %s\n", CommandList[CmdNumber].Name, CommandList[CmdNumber].Parameters);
|
|---|
| 156 | return;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | // Jump to command function
|
|---|
| 160 | (this->*CommandList[CmdNumber].CommandPointer)();
|
|---|
| 161 | return;
|
|---|
| 162 | }
|
|---|
| 163 | }
|
|---|
| 164 | PrintMessage("Unknown command '%s'\n", Parameter[0].c_str());
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 |
|
|---|
| 168 | // Adress board
|
|---|
| 169 | void ProcessIO::cmd_board() {
|
|---|
| 170 |
|
|---|
| 171 | // Print list of boards
|
|---|
| 172 | if (Parameter.size() == 1) {
|
|---|
| 173 | for (int i=0; i<NumHVBoards; i++) {
|
|---|
| 174 | PrintMessage("Board %d: %s\n", fHVBoard[i]->GetBoardNumber(), fHVBoard[i]->BoardName);
|
|---|
| 175 | }
|
|---|
| 176 | return;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | //Select board(s)
|
|---|
| 180 | if (Match(Parameter[1].c_str(), "all")) {
|
|---|
| 181 | FirstBoard = 0;
|
|---|
| 182 | LastBoard = NumHVBoards-1;
|
|---|
| 183 | }
|
|---|
| 184 | else if (Parameter.size()==2 && atoi(Parameter[1].c_str())>=0 && atoi(Parameter[1].c_str())<NumHVBoards) {
|
|---|
| 185 | FirstBoard = atoi(Parameter[1].c_str());
|
|---|
| 186 | LastBoard = FirstBoard;
|
|---|
| 187 | }
|
|---|
| 188 | else if (Parameter.size()==3 && atoi(Parameter[1].c_str())>=0 && atoi(Parameter[1].c_str())<NumHVBoards &&
|
|---|
| 189 | atoi(Parameter[2].c_str())>0 && atoi(Parameter[2].c_str())<NumHVBoards) {
|
|---|
| 190 | FirstBoard = atoi(Parameter[1].c_str());
|
|---|
| 191 | LastBoard = atoi(Parameter[2].c_str());
|
|---|
| 192 | }
|
|---|
| 193 | else PrintMessage("Cannot address board(s), out of range.\n");
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | // Print configuration
|
|---|
| 198 | void ProcessIO::cmd_config() {
|
|---|
| 199 |
|
|---|
| 200 | PrintMessage( " Pixel map table: %s\n"
|
|---|
| 201 | " %d USB devices:\n", fPixMapTable, NumHVBoards);
|
|---|
| 202 |
|
|---|
| 203 | for (int i=0; i<NumHVBoards; i++) PrintMessage(" Board %d: %s\n", i, fHVBoard[i]->BoardName);
|
|---|
| 204 |
|
|---|
| 205 | PrintMessage( " StatusRefreshRate: %.2f Hz\n"
|
|---|
| 206 | " DACMin value: %d\n"
|
|---|
| 207 | " DACMax value: %d\n"
|
|---|
| 208 | " HVCalibOffset : %f\n"
|
|---|
| 209 | " HVCalibSlope : %f\n"
|
|---|
| 210 | " HVMaxDiff : %u\n",
|
|---|
| 211 | fStatusRefreshRate, DACMin,
|
|---|
| 212 | DACMax, fHVCalibOffset, fHVCalibSlope, fHVMaxDiff);
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | // Print help
|
|---|
| 216 | void ProcessIO::cmd_help() {
|
|---|
| 217 |
|
|---|
| 218 | char Buffer[MAX_COM_SIZE];
|
|---|
| 219 | for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) {
|
|---|
| 220 | snprintf(Buffer, sizeof(Buffer), "%s %s", CommandList[i].Name, CommandList[i].Parameters);
|
|---|
| 221 | PrintMessage("%-28s%s\n", Buffer, CommandList[i].Help);
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | PrintMessage(".<command> Execute shell command\n\n"
|
|---|
| 225 | "Items in <> are mandatory, in [] optional, | indicates mutual exclusive or.\n");
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | // Set new bias voltage
|
|---|
| 229 | void ProcessIO::cmd_hv() {
|
|---|
| 230 |
|
|---|
| 231 | int Int, SingleChannel;
|
|---|
| 232 | unsigned int DACValue, Errors=0;
|
|---|
| 233 | double Double;
|
|---|
| 234 | bool SetDac;
|
|---|
| 235 |
|
|---|
| 236 | // Interpretation as DAC value requested?
|
|---|
| 237 | if (Parameter.size() == 4 && Match(Parameter[3], "dac")) SetDac = true;
|
|---|
| 238 | else SetDac = false;
|
|---|
| 239 |
|
|---|
| 240 | // Interprete first number (might be channel number)
|
|---|
| 241 | if (!ConvertToInt(Parameter[1], &SingleChannel)) SingleChannel = -1;
|
|---|
| 242 |
|
|---|
| 243 | // Loop over all boards
|
|---|
| 244 | for (int i=FirstBoard; i<=LastBoard; i++) {
|
|---|
| 245 | // Loop over all channels given as command parameter
|
|---|
| 246 | for (unsigned int n=1; n<Parameter.size()-1; n+=2) {
|
|---|
| 247 | // Loop over all channels
|
|---|
| 248 | for (int j=0; j<NUM_CHAINS; j++) for (int k=0; k<NUM_CHANNELS; k++) {
|
|---|
| 249 |
|
|---|
| 250 | // Current channel must be considered?
|
|---|
| 251 | if (!Match(Parameter[1], "all") && !(i == (int) pm->Pixel_to_HVboard(Parameter[n]) &&
|
|---|
| 252 | j == (int) pm->Pixel_to_HVchain(Parameter[n]) && k == (int) pm->Pixel_to_HVchannel(Parameter[n])) &&
|
|---|
| 253 | !(j == SingleChannel/NUM_CHANNELS && k == SingleChannel%NUM_CHANNELS)) continue;
|
|---|
| 254 |
|
|---|
| 255 | // Voltage change (number starts with + oder -) ignored if current DAC value is zero
|
|---|
| 256 | if (isdigit(Parameter[n+1][0])==0 && fHVBoard[i]->HV[j][k] == 0) continue;
|
|---|
| 257 |
|
|---|
| 258 | // Set new voltage/DAC value
|
|---|
| 259 | if (!SetDac){
|
|---|
| 260 | // Convert voltage value and check format
|
|---|
| 261 | if (!ConvertToDouble(Parameter[n+1], &Double)) {
|
|---|
| 262 | PrintMessage("Error: Wrong number format for voltage setting\n");
|
|---|
| 263 | goto LeaveLoop;
|
|---|
| 264 | }
|
|---|
| 265 | // Adjust voltage and DAV value
|
|---|
| 266 | if (isdigit(Parameter[n+1][0]) == 0) fHVBoard[i]->HVV[j][k] += Double;
|
|---|
| 267 | else fHVBoard[i]->HVV[j][k] = Double;
|
|---|
| 268 | DACValue = calib->HVToDAC(fHVBoard[i]->HVV[j][k], i, j, k);
|
|---|
| 269 | }
|
|---|
| 270 | else {
|
|---|
| 271 | // Convert DAC value and check format
|
|---|
| 272 | if (!ConvertToInt(Parameter[n+1], &Int)) {
|
|---|
| 273 | PrintMessage("Error: Wrong number format for DAC voltage setting\n");
|
|---|
| 274 | goto LeaveLoop;
|
|---|
| 275 | }
|
|---|
| 276 | // Adjust DAC value
|
|---|
| 277 | if (isdigit(Parameter[n+1][0]) == 0) DACValue = fHVBoard[i]->HV[j][k] + Int;
|
|---|
| 278 | else DACValue = Int;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | // Set new voltage (if DAC value, update calibrated value)
|
|---|
| 282 | if (!RampVoltage(DACValue, i, j, k)) Errors++;
|
|---|
| 283 | if (SetDac) fHVBoard[i]->HVV[j][k] = calib->DACToHV(fHVBoard[i]->HV[j][k], i, j, k);
|
|---|
| 284 | } // Channels and chains
|
|---|
| 285 | } // Loop over command argument
|
|---|
| 286 |
|
|---|
| 287 | // Update DIM service for this boar
|
|---|
| 288 | LeaveLoop:
|
|---|
| 289 | fHVBoard[i]->BiasVolt->updateService();
|
|---|
| 290 | } // Boards
|
|---|
| 291 |
|
|---|
| 292 | if (Errors > 0) PrintMessage("Errors on %d channel(s) occurred\n", Errors);
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | // Load bias settings from file
|
|---|
| 296 | void ProcessIO::cmd_load() {
|
|---|
| 297 |
|
|---|
| 298 | char Buffer[MAX_COM_SIZE];
|
|---|
| 299 | int NBoards = 0, Errors = 0, Chain, Channel;
|
|---|
| 300 | unsigned int DACValue;
|
|---|
| 301 | FILE *File;
|
|---|
| 302 |
|
|---|
| 303 | if ((File=fopen(Parameter[1].c_str(), "r")) == NULL) {
|
|---|
| 304 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 305 | return;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | while (fgets(Buffer, sizeof(Buffer), File) != NULL) {
|
|---|
| 309 | for (int Board=0; Board<NumHVBoards; Board++) {
|
|---|
| 310 | if (Match(fHVBoard[Board]->BoardName, Buffer)) {
|
|---|
| 311 | PrintMessage("Found bias settings for board %d (%s)\n\r",fHVBoard[Board]->GetBoardNumber(), fHVBoard[Board]->BoardName);
|
|---|
| 312 |
|
|---|
| 313 | Chain = 0; Channel = 0;
|
|---|
| 314 | while (fscanf(File, "%u", &DACValue)==1 && Chain<NUM_CHAINS) {
|
|---|
| 315 | if (!RampVoltage(DACValue, Board, Chain, Channel)) {
|
|---|
| 316 | Errors++;
|
|---|
| 317 | PrintMessage("Error: Could not ramp chain %d, channel %d\n", Chain, Channel);
|
|---|
| 318 | }
|
|---|
| 319 | else {
|
|---|
| 320 | PrintMessage("Ramped chain %d, channel %d to %u (%.2f V) \r",
|
|---|
| 321 | Chain,Channel,DACValue,calib->DACToHV(DACValue,Board,Chain,Channel));
|
|---|
| 322 | }
|
|---|
| 323 | fHVBoard[Board]->HVV[Chain][Channel] = calib->DACToHV(fHVBoard[Board]->HV[Chain][Channel], Board, Chain, Channel);
|
|---|
| 324 |
|
|---|
| 325 | if(++Channel == NUM_CHANNELS) {
|
|---|
| 326 | Chain++;
|
|---|
| 327 | Channel = 0;
|
|---|
| 328 | }
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | // Update DIM service
|
|---|
| 332 | fHVBoard[Board]->BiasVolt->updateService();
|
|---|
| 333 |
|
|---|
| 334 | if (ferror(File) != 0) {
|
|---|
| 335 | PrintMessage("Error reading DAC value from file, terminating. (%s)\n",strerror(errno));
|
|---|
| 336 | return;
|
|---|
| 337 | }
|
|---|
| 338 | else PrintMessage("\nFinished updating board\n");
|
|---|
| 339 | NBoards++;
|
|---|
| 340 | }
|
|---|
| 341 | } // Loop over boards
|
|---|
| 342 | } // while()
|
|---|
| 343 |
|
|---|
| 344 | if (NBoards != NumHVBoards) {
|
|---|
| 345 | PrintMessage("Warning: Could not load bias settings for all connected HV boards\n");
|
|---|
| 346 | }
|
|---|
| 347 | else if (Errors == 0) PrintMessage("Success: Read bias settings for all connected HV boards\n");
|
|---|
| 348 | if (Errors != 0) PrintMessage("Warning: Errors on %d channel(s) occurred\n", Errors);
|
|---|
| 349 |
|
|---|
| 350 | if (fclose(File) != 0) PrintMessage("Error: Could not close file '%s'\n",Parameter[1].c_str());
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | // Set status refresh rate
|
|---|
| 354 | void ProcessIO::cmd_rate() {
|
|---|
| 355 |
|
|---|
| 356 | double Rate;
|
|---|
| 357 |
|
|---|
| 358 | if (!ConvertToDouble(Parameter[1], &Rate)) {
|
|---|
| 359 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 360 | return;
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | // Check limits
|
|---|
| 364 | if (Rate<MIN_RATE || Rate>MAX_RATE) {
|
|---|
| 365 | PrintMessage("Refresh rate out of range (min: %.2f Hz, max: %.2f Hz)\n", MIN_RATE, MAX_RATE);
|
|---|
| 366 | return;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | fStatusRefreshRate = Rate;
|
|---|
| 370 | PrintMessage("Refresh rate set to %.2f Hz\n", fStatusRefreshRate);
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | // Reset
|
|---|
| 374 | void ProcessIO::cmd_reset() {
|
|---|
| 375 |
|
|---|
| 376 | for (int i=FirstBoard; i<=LastBoard; i++) ResetBoard(i);
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | // Save bias settings of all boards
|
|---|
| 380 | void ProcessIO::cmd_save() {
|
|---|
| 381 |
|
|---|
| 382 | FILE *File;
|
|---|
| 383 | time_t time_now_secs;
|
|---|
| 384 | struct tm *Time;
|
|---|
| 385 |
|
|---|
| 386 | time(&time_now_secs);
|
|---|
| 387 | Time = localtime(&time_now_secs);
|
|---|
| 388 |
|
|---|
| 389 | if ((File = fopen(Parameter[1].c_str(), "w")) == NULL) {
|
|---|
| 390 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 391 | return;
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | fprintf(File,"********** Bias settings, %04d %02d %02d, %02d:%02d:%02d **********\n\n",
|
|---|
| 395 | 1900 + Time->tm_year, 1 + Time->tm_mon,
|
|---|
| 396 | Time->tm_mday, Time->tm_hour, Time->tm_min, Time->tm_sec);
|
|---|
| 397 |
|
|---|
| 398 | for (int i=0; i<NumHVBoards; i++) {
|
|---|
| 399 | fprintf(File, "%s\n\n", fHVBoard[i]->BoardName);
|
|---|
| 400 |
|
|---|
| 401 | for (int j=0; j<NUM_CHAINS; j++) {
|
|---|
| 402 | for (int k=0; k<NUM_CHANNELS; k++) fprintf(File,"%5d ",fHVBoard[i]->HV[j][k]);
|
|---|
| 403 | fprintf(File, "\n");
|
|---|
| 404 | }
|
|---|
| 405 | fprintf(File, "\n");
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | if (fclose(File) != 0) {
|
|---|
| 409 | PrintMessage("Error: Could not close file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 410 | }
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | // Start monitoring
|
|---|
| 414 | void ProcessIO::cmd_start() {
|
|---|
| 415 |
|
|---|
| 416 | state = active;
|
|---|
| 417 | pthread_kill(HVMonitor, SIGUSR1);
|
|---|
| 418 | PrintMessage(All, "Status monitoring activated\n");
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | // Print status
|
|---|
| 422 | void ProcessIO::cmd_status() {
|
|---|
| 423 |
|
|---|
| 424 | PrintMessage("\n Status monitor: %s\n", state_str[state]);
|
|---|
| 425 | PrintMessage(" Status refresh rate [Hz]: %.2f\n", fStatusRefreshRate);
|
|---|
| 426 | PrintMessage(" Total number of boards: %d\n", NumHVBoards);
|
|---|
| 427 | PrintMessage(" Active boards: %d\n\n", LastBoard - FirstBoard + 1);
|
|---|
| 428 |
|
|---|
| 429 | for (int i=FirstBoard; i<=LastBoard; i++) {
|
|---|
| 430 | PrintMessage(" BOARD %d (%s) Wrap counter: %s (%d) Manual reset: %s\n Time-out: %.2f s Error count: %d\n\n",
|
|---|
| 431 | fHVBoard[i]->GetBoardNumber(), fHVBoard[i]->BoardName,
|
|---|
| 432 | fHVBoard[i]->WrapOK ? "ok":"error", fHVBoard[i]->LastWrapCount,
|
|---|
| 433 | fHVBoard[i]->ResetButton ? "yes" : "no", fHVBoard[i]->fTimeOut, fHVBoard[i]->ErrorCount);
|
|---|
| 434 |
|
|---|
| 435 | for (int j=0; j<NUM_CHAINS; j++) {
|
|---|
| 436 | PrintMessage(" CHAIN %d Over-current: %s\n", j, fHVBoard[i]->Overcurrent[j] ? "yes" : "no");
|
|---|
| 437 | for (int l=0; l<NUM_CHANNELS; l++) {
|
|---|
| 438 | if (l%8 == 0) PrintMessage("\r%3.1d: ", j*NUM_CHANNELS+l);
|
|---|
| 439 | if (Parameter.size() == 2) PrintMessage("%5d ",fHVBoard[i]->HV[j][l]);
|
|---|
| 440 | else PrintMessage("%#5.2f ",fHVBoard[i]->HVV[j][l]);
|
|---|
| 441 | if (l%8 == 7) PrintMessage("\n");
|
|---|
| 442 | }
|
|---|
| 443 | }
|
|---|
| 444 | }
|
|---|
| 445 | }
|
|---|
| 446 |
|
|---|
| 447 | // Stop monitoring
|
|---|
| 448 | void ProcessIO::cmd_stop() {
|
|---|
| 449 |
|
|---|
| 450 | state = stopped;
|
|---|
| 451 | pthread_kill(HVMonitor, SIGUSR1);
|
|---|
| 452 | PrintMessage(All, "Status monitor stopped\n");
|
|---|
| 453 | }
|
|---|
| 454 |
|
|---|
| 455 | // Set timeout to return from read
|
|---|
| 456 | void ProcessIO::cmd_timeout() {
|
|---|
| 457 |
|
|---|
| 458 | double Timeout;
|
|---|
| 459 |
|
|---|
| 460 | if (!ConvertToDouble(Parameter[1], &Timeout)) {
|
|---|
| 461 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 462 | return;
|
|---|
| 463 | }
|
|---|
| 464 |
|
|---|
| 465 | for (int i=0; i<NumHVBoards; i++) fHVBoard[i]->SetTimeOut(Timeout);
|
|---|
| 466 | if (NumHVBoards > 0) PrintMessage("Timeout set to %.2f s for all boards\n", Timeout);
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | // Print uptime
|
|---|
| 470 | void ProcessIO::cmd_uptime() {
|
|---|
| 471 |
|
|---|
| 472 | time_t ActualT;
|
|---|
| 473 | time (&ActualT);
|
|---|
| 474 | PrintMessage("%d:%02d:%02d\n", (int) difftime(ActualT, StartTime)/3600, ((int) difftime(ActualT, StartTime)/60)%60, (int) difftime(ActualT, StartTime)%60);
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | // Exit program
|
|---|
| 478 | void ProcessIO::cmd_exit() {
|
|---|
| 479 |
|
|---|
| 480 | ExitRequest = true;
|
|---|
| 481 | pthread_kill(HVMonitor, SIGUSR1);
|
|---|
| 482 | }
|
|---|
| 483 |
|
|---|
| 484 |
|
|---|
| 485 | // Print message to selected target
|
|---|
| 486 | void ProcessIO::PrintMessage(MsgTarget Target, const char *Format, ...) {
|
|---|
| 487 |
|
|---|
| 488 | va_list ArgumentPointer;
|
|---|
| 489 | va_start(ArgumentPointer, Format);
|
|---|
| 490 | DoPrintMessage(Format, ArgumentPointer, Target);
|
|---|
| 491 | va_end(ArgumentPointer);
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | // Print message to log file, and screen or socket (depending on command origin)
|
|---|
| 495 | void ProcessIO::PrintMessage(const char *Format, ...) {
|
|---|
| 496 |
|
|---|
| 497 | va_list ArgumentPointer;
|
|---|
| 498 | va_start(ArgumentPointer, Format);
|
|---|
| 499 | DoPrintMessage(Format, ArgumentPointer, Console);
|
|---|
| 500 | va_end(ArgumentPointer);
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 | // Function doing the actual printing work
|
|---|
| 504 | // Be careful with overloading variadic functions!
|
|---|
| 505 | void ProcessIO::DoPrintMessage(const char *Format, va_list ArgumentPointer, MsgTarget Target) {
|
|---|
| 506 |
|
|---|
| 507 | static char Error[] = "vasprintf() failed in DoPrintMessage()";
|
|---|
| 508 | char *Text;
|
|---|
| 509 |
|
|---|
| 510 | // Evaluate arguments
|
|---|
| 511 | if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
|
|---|
| 512 |
|
|---|
| 513 | // Print to console
|
|---|
| 514 | if(Target & Console) {
|
|---|
| 515 | if(strlen(Text)>0 && Text[strlen(Text)-1]=='\n') printf("\r%s%s", Text, Prompt); // New prompt
|
|---|
| 516 | else printf("%s", Text);
|
|---|
| 517 | fflush(stdout);
|
|---|
| 518 | }
|
|---|
| 519 |
|
|---|
| 520 | // Send to DIM console service and log
|
|---|
| 521 | ConsoleOut->updateService(Text);
|
|---|
| 522 | if(Target & Log) SendToLog("%s %s", SERVER_NAME, Text);
|
|---|
| 523 |
|
|---|
| 524 | // Free old text
|
|---|
| 525 | if (ConsoleText != Error) free(ConsoleText);
|
|---|
| 526 | ConsoleText = Text;
|
|---|
| 527 | }
|
|---|
| 528 |
|
|---|
| 529 | // Ramp to new voltage with maximum step size given in fHVMaxDiff
|
|---|
| 530 | // No ramping when decreasing voltage
|
|---|
| 531 | bool ProcessIO::RampVoltage(unsigned int Target, int Board, int Chain, int Channel) {
|
|---|
| 532 |
|
|---|
| 533 | while (fHVBoard[Board]->HV[Chain][Channel] != (int) Target) {
|
|---|
| 534 | int Diff = Target - fHVBoard[Board]->HV[Chain][Channel];
|
|---|
| 535 | if (Diff > (int) fHVMaxDiff) Diff = fHVMaxDiff;
|
|---|
| 536 |
|
|---|
| 537 | if (fHVBoard[Board]->SetHV(Chain, Channel, fHVBoard[Board]->HV[Chain][Channel]+Diff) != 1) {
|
|---|
| 538 | Message(ERROR, "Could not set bias of board %d, chain %d, channel %d. Skipping channel\n",fHVBoard[Board]->GetBoardNumber(),Chain,Channel);
|
|---|
| 539 | return false;
|
|---|
| 540 | }
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | return true;
|
|---|
| 544 | }
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 | // Check board status (ignore board if it has more than 10 read/write errors)
|
|---|
| 548 | void ProcessIO::Monitor() {
|
|---|
| 549 |
|
|---|
| 550 | static bool Warned = false;
|
|---|
| 551 | int Ret;
|
|---|
| 552 |
|
|---|
| 553 | // Lock because command execution runs in different thread
|
|---|
| 554 | if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
|
|---|
| 555 | Message(FATAL, "pthread_mutex_lock() failed in commandHandler() (%s)", strerror(Ret));
|
|---|
| 556 | }
|
|---|
| 557 |
|
|---|
| 558 | for (int i=0; i<NumHVBoards; i++) {
|
|---|
| 559 | if (fHVBoard[i]->ErrorCount > 10) {
|
|---|
| 560 | if (!Warned) {
|
|---|
| 561 | Warned = true;
|
|---|
| 562 | PrintMessage(All, "Warning: Some board has many read/write errors, status monitor disabled\n");
|
|---|
| 563 | Message(WARN, "Warning: Some board has many read/write errors, status monitor disabled\n");
|
|---|
| 564 | }
|
|---|
| 565 | continue;
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | if (fHVBoard[i]->GetStatus() != 1) {
|
|---|
| 569 | PrintMessage(All, "Error: Monitor could not read status of board %d\n", fHVBoard[i]->GetBoardNumber());
|
|---|
| 570 | Message(ERROR, "Error: Monitor could not read status of board %d\n", fHVBoard[i]->GetBoardNumber());
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | if (fHVBoard[i]->ResetButton) {
|
|---|
| 574 | PrintMessage(All, "Manual reset of board %d\n",fHVBoard[i]->GetBoardNumber());
|
|---|
| 575 | Message(INFO, "Manual reset of board %d\n",fHVBoard[i]->GetBoardNumber());
|
|---|
| 576 | ResetBoard(i);
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | if (!fHVBoard[i]->WrapOK) {
|
|---|
| 580 | PrintMessage(All, "Error: Wrap counter mismatch board %d\n",fHVBoard[i]->GetBoardNumber());
|
|---|
| 581 | Message(ERROR, "Error: Wrap counter mismatch board %d\n",fHVBoard[i]->GetBoardNumber());
|
|---|
| 582 | }
|
|---|
| 583 |
|
|---|
| 584 | for (int j=0; j<NUM_CHAINS; j++) {
|
|---|
| 585 | if (fHVBoard[i]->Overcurrent[j]) {
|
|---|
| 586 | PrintMessage(All, "Warning: Overcurrent in chain %d of board %d\n",j,fHVBoard[i]->GetBoardNumber());
|
|---|
| 587 | Message(WARN, "Warning: Overcurrent in chain %d of board %d\n",j,fHVBoard[i]->GetBoardNumber());
|
|---|
| 588 | ResetBoard(i);
|
|---|
| 589 | }
|
|---|
| 590 | }
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | // Unlock
|
|---|
| 594 | if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
|
|---|
| 595 | Message(FATAL, "pthread_mutex_lock() failed in commandHandler() (%s)", strerror(Ret));
|
|---|
| 596 | }
|
|---|
| 597 |
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 |
|
|---|
| 601 | // Send reset to board and clear voltage arrays
|
|---|
| 602 | void ProcessIO::ResetBoard(int i) {
|
|---|
| 603 |
|
|---|
| 604 | if (fHVBoard[i]->Reset() == 1) {
|
|---|
| 605 | PrintMessage("Reset of board %d\n", fHVBoard[i]->GetBoardNumber());
|
|---|
| 606 | PrintBoardStatus(i);
|
|---|
| 607 | }
|
|---|
| 608 | else PrintMessage("Error: Could not reset board %d\n",fHVBoard[i]->GetBoardNumber());
|
|---|
| 609 | }
|
|---|
| 610 |
|
|---|
| 611 |
|
|---|
| 612 | // Print current board status
|
|---|
| 613 | void ProcessIO::PrintBoardStatus(int i) {
|
|---|
| 614 |
|
|---|
| 615 | PrintMessage("Status board %d (%s): MR %s OC0 %s OC1 %s OC2 %s OC3 %s WC %s (%d)\n",
|
|---|
| 616 | fHVBoard[i]->GetBoardNumber(), fHVBoard[i]->BoardName,
|
|---|
| 617 | fHVBoard[i]->ResetButton ? "yes" : "no",
|
|---|
| 618 | fHVBoard[i]->Overcurrent[0] ? "yes" : "no",
|
|---|
| 619 | fHVBoard[i]->Overcurrent[1] ? "yes" : "no",
|
|---|
| 620 | fHVBoard[i]->Overcurrent[2] ? "yes" : "no",
|
|---|
| 621 | fHVBoard[i]->Overcurrent[3] ? "yes" : "no",
|
|---|
| 622 | fHVBoard[i]->WrapOK ? "ok":"error", fHVBoard[i]->LastWrapCount);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 | // Command handling (mutex needed because of monitor thread)
|
|---|
| 627 | void ProcessIO::commandHandler() {
|
|---|
| 628 |
|
|---|
| 629 | int Ret;
|
|---|
| 630 |
|
|---|
| 631 | if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
|
|---|
| 632 | Message(FATAL, "pthread_mutex_lock() failed in commandHandler() (%s)", strerror(Ret));
|
|---|
| 633 | }
|
|---|
| 634 |
|
|---|
| 635 | if (getCommand() == Command) CommandControl(getCommand()->getString());
|
|---|
| 636 |
|
|---|
| 637 | if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
|
|---|
| 638 | Message(FATAL, "pthread_mutex_unlock() failed in commandHandler() (%s)", strerror(Ret));
|
|---|
| 639 | }
|
|---|
| 640 | }
|
|---|
| 641 |
|
|---|
| 642 | // Check if two strings match (min 1 character must match)
|
|---|
| 643 | bool Match(string str, const char *cmd) {
|
|---|
| 644 | return strncasecmp(str.c_str(),cmd,strlen(str.c_str())==0 ? 1:strlen(str.c_str())) ? false:true;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 | // Convert string to double
|
|---|
| 649 | // Returns false if conversion did not stop on whitespace or EOL character
|
|---|
| 650 | bool ConvertToDouble(string String, double *Result) {
|
|---|
| 651 |
|
|---|
| 652 | char *EndPointer;
|
|---|
| 653 |
|
|---|
| 654 | *Result = strtod(String.c_str(), &EndPointer);
|
|---|
| 655 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
|---|
| 656 | return true;
|
|---|
| 657 | }
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 | // Convert string to int
|
|---|
| 661 | // Returns false if conversion did not stop on whitespace or EOL character
|
|---|
| 662 | bool ConvertToInt(string String, int *Result) {
|
|---|
| 663 |
|
|---|
| 664 | char *EndPointer;
|
|---|
| 665 |
|
|---|
| 666 | *Result = (int) strtol(String.c_str(), &EndPointer, 0);
|
|---|
| 667 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
|---|
| 668 | return true;
|
|---|
| 669 | }
|
|---|