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