1 | //
|
---|
2 | // Class processing user input
|
---|
3 | //
|
---|
4 |
|
---|
5 | #include "User.h"
|
---|
6 | #include <readline/readline.h>
|
---|
7 |
|
---|
8 | using namespace std;
|
---|
9 |
|
---|
10 | // Branch table for command evaluation
|
---|
11 | static const struct CL_Struct { const char *Name;
|
---|
12 | void (User::*CommandPointer)();
|
---|
13 | unsigned int MinNumParameter;
|
---|
14 | const char *Parameters;
|
---|
15 | const char *Help;
|
---|
16 | } CommandList[] =
|
---|
17 | {{"pixel", &User::cmd_hv, 2, "<pixel id> <v>", "Change bias of pixel"},
|
---|
18 | {"channel", &User::cmd_hv, 2, "<channel range> <v>", "Change bias of (all) channels of active crate"},
|
---|
19 | {"gs", &User::cmd_gs, 1, "[crate] <volt>", "Global voltage set active crate"},
|
---|
20 | {"reset", &User::cmd_reset, 0, "", "Reset active crate"},
|
---|
21 | {"synch", &User::cmd_synch, 0, "", "Synchronize active crate"}, {"crate", &User::cmd_crate, 1, "<number>", "Select active crate"},
|
---|
22 | {"status", &User::cmd_status, 0, "[dac|current]", "Show status information (DAC values if requested)"},
|
---|
23 | {"mode", &User::cmd_mode, 1, "<static|dynamic>", "Set voltage stabilization mode (experimental)"},
|
---|
24 | {"load", &User::cmd_load, 1, "<file>", "Load and set bias settings from file"},
|
---|
25 | {"save", &User::cmd_save, 1, "<file>", "Save current bias settings to file"},
|
---|
26 | {"rate", &User::cmd_rate, 1, "<rate>", "Set refresh rate in Hz"},
|
---|
27 | {"timeout", &User::cmd_timeout, 1, "<time>", "Set timeout to return from read in seconds"},
|
---|
28 | {"help", &User::cmd_help, 0, "", "Print help"},
|
---|
29 | {"exit", &User::cmd_exit, 0, "", "Exit program"}};
|
---|
30 |
|
---|
31 |
|
---|
32 | //
|
---|
33 | // Constructor
|
---|
34 | //
|
---|
35 | User::User(): EvidenceServer(SERVER_NAME) {
|
---|
36 |
|
---|
37 | MainThread = pthread_self();
|
---|
38 |
|
---|
39 | // DIM console service used in PrintMessage()
|
---|
40 | ConsoleText = NULL;
|
---|
41 | ConsoleOut = new DimService(SERVER_NAME"/ConsoleOut", (char *) "");
|
---|
42 |
|
---|
43 | // Get configuration data
|
---|
44 | vector<string> Boards = Tokenize(GetConfig("Boards"), " \t");
|
---|
45 | Boards = Tokenize("FTE00FOH", " \t");
|
---|
46 | fTimeOut = atof(GetConfig("TimeOut").c_str());
|
---|
47 | fStatusRefreshRate = atof(GetConfig("StatusRefreshRate").c_str());
|
---|
48 | fMaxDiff = atof(GetConfig("HVMaxDiffNew").c_str());
|
---|
49 |
|
---|
50 | if (fStatusRefreshRate < MIN_RATE || fStatusRefreshRate > MAX_RATE) fStatusRefreshRate = 1;
|
---|
51 |
|
---|
52 | // Open devices
|
---|
53 | for (unsigned int i=0; i<Boards.size(); i++) {
|
---|
54 |
|
---|
55 | class Crate *New = new class Crate(Boards[i], Crates.size(), this);
|
---|
56 |
|
---|
57 | if (New->InitOK && New->Synch()) {
|
---|
58 | PrintMessage("Synchronized and reset crate %s (#%d)\n", Boards[i].c_str(), Crates.size());
|
---|
59 | Crates.push_back(New);
|
---|
60 | }
|
---|
61 | else {
|
---|
62 | Message(WARN, "Failed to synchronize crate %s", Boards[i].c_str());
|
---|
63 | delete New;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | // Set active crate
|
---|
68 | if (!Boards.empty()) ActiveCrate = 0;
|
---|
69 | else ActiveCrate = -1;
|
---|
70 |
|
---|
71 | // Create PixelMap instance (map from config server)
|
---|
72 | DimRpcInfo RPC((char *) "ConfigRequest", (char *) "");
|
---|
73 | RPC.setData((char *) "Misc PixelMap");
|
---|
74 | PixMap = new PixelMap(std::string(RPC.getString(), RPC.getSize()));
|
---|
75 |
|
---|
76 | // Install DIM command (after all initialized)
|
---|
77 | DIMCommand = new DimCommand((char *) SERVER_NAME"/Command", (char *) "C", this);
|
---|
78 |
|
---|
79 | // Create monitor thread and make accessible for sending signal
|
---|
80 | if ((pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchMonitor,(void *) this)) != 0) {
|
---|
81 | Message(FATAL, "pthread_create() failed with Monitor thread");
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | //
|
---|
86 | // Destructor
|
---|
87 | //
|
---|
88 | User::~User() {
|
---|
89 |
|
---|
90 | // Wait for thread to quit
|
---|
91 | if (pthread_cancel(Thread) != 0) Message(ERROR, "pthread_cancel() failed in ()");
|
---|
92 | if (pthread_join(Thread, NULL) != 0) Message(ERROR, "pthread_join() failed in ()");
|
---|
93 |
|
---|
94 | // Delete all crates
|
---|
95 | for (unsigned int i=0; i<Crates.size(); i++) delete Crates[i];
|
---|
96 |
|
---|
97 | delete DIMCommand;
|
---|
98 | delete PixMap;
|
---|
99 | delete ConsoleOut;
|
---|
100 | free(ConsoleText);
|
---|
101 | }
|
---|
102 |
|
---|
103 | //
|
---|
104 | // Process user input
|
---|
105 | //
|
---|
106 | void User::commandHandler() {
|
---|
107 |
|
---|
108 | // Build string safely
|
---|
109 | string Command = string(getCommand()->getString(), getCommand()->getSize());
|
---|
110 |
|
---|
111 | // Check if command is legal and ignore empty commands
|
---|
112 | if (getCommand() != DIMCommand || Command.size() < 2) return;
|
---|
113 |
|
---|
114 | // Shell command
|
---|
115 | if(Command[0]=='.') {
|
---|
116 | system(Command.c_str()+1);
|
---|
117 | return;
|
---|
118 | }
|
---|
119 |
|
---|
120 | // Parse command into tokens
|
---|
121 | Parameter = Tokenize(Command, " ");
|
---|
122 |
|
---|
123 | // Search for command in command list
|
---|
124 | for(unsigned int CmdNumber=0; CmdNumber<sizeof(CommandList)/sizeof(CL_Struct); CmdNumber++) {
|
---|
125 | if (Match(Parameter[0], CommandList[CmdNumber].Name)) {
|
---|
126 | if(Parameter.size()-1 < CommandList[CmdNumber].MinNumParameter) {
|
---|
127 | PrintMessage("Usage: %s %s\n", CommandList[CmdNumber].Name, CommandList[CmdNumber].Parameters);
|
---|
128 | return;
|
---|
129 | }
|
---|
130 |
|
---|
131 | // Jump to command function
|
---|
132 | (this->*CommandList[CmdNumber].CommandPointer)();
|
---|
133 | return;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | PrintMessage("Unknown command '%s'\n", Parameter[0].c_str());
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | // Print help
|
---|
141 | void User::cmd_help() {
|
---|
142 |
|
---|
143 | char Buffer[MAX_COM_SIZE];
|
---|
144 | for(unsigned int i=0; i<sizeof(CommandList)/sizeof(CL_Struct); i++) {
|
---|
145 | snprintf(Buffer, sizeof(Buffer), "%s %s", CommandList[i].Name, CommandList[i].Parameters);
|
---|
146 | PrintMessage("%-28s%s\n", Buffer, CommandList[i].Help);
|
---|
147 | }
|
---|
148 |
|
---|
149 | PrintMessage(".<command> Execute shell command\n\n"
|
---|
150 | "Items in <> are mandatory, in [] optional, | indicates mutual exclusive or.\n"
|
---|
151 | "Channel ranges can be 'all', a single number or in the form 'a-b'.\n");
|
---|
152 | }
|
---|
153 |
|
---|
154 | //
|
---|
155 | // Synchronize boards
|
---|
156 | //
|
---|
157 | void User::cmd_synch() {
|
---|
158 |
|
---|
159 | if (ActiveCrate == -1) return;
|
---|
160 |
|
---|
161 | if (Crates[ActiveCrate]->Synch()) PrintMessage("Synchronized crate %d\n", ActiveCrate);
|
---|
162 | else PrintMessage("Failed to synchronize crate %d\n", ActiveCrate);
|
---|
163 | }
|
---|
164 |
|
---|
165 | //
|
---|
166 | // Select active crate
|
---|
167 | //
|
---|
168 | void User::cmd_crate() {
|
---|
169 |
|
---|
170 | int Crate;
|
---|
171 |
|
---|
172 | if (!ConvertToInt(Parameter[1], &Crate)) {
|
---|
173 | PrintMessage("Error: Wrong number format\n");
|
---|
174 | return;
|
---|
175 | }
|
---|
176 |
|
---|
177 | // Check limits
|
---|
178 | if (Crate<0 || Crate>=(int) Crates.size()) {
|
---|
179 | PrintMessage("Crate number %d not existing\n", Crate);
|
---|
180 | return;
|
---|
181 | }
|
---|
182 |
|
---|
183 | ActiveCrate = Crate;
|
---|
184 | }
|
---|
185 |
|
---|
186 | //
|
---|
187 | // Set new bias voltage
|
---|
188 | //
|
---|
189 | void User::cmd_hv() {
|
---|
190 |
|
---|
191 | unsigned int Errors=0;
|
---|
192 | double Double;
|
---|
193 | struct Range Crt, Chan;
|
---|
194 | vector< map<unsigned int, double> > Voltages (Crates.size());
|
---|
195 |
|
---|
196 | // Loop over all parameters
|
---|
197 | for (unsigned int n=1; n < Parameter.size()-1; n+=2) {
|
---|
198 |
|
---|
199 | // Pixel identification?
|
---|
200 | if (Match(Parameter[0], "pixel")) {
|
---|
201 | // Skip if first character is not digit
|
---|
202 | if (isdigit(Parameter[n][0] == 0)) continue;
|
---|
203 | // Skip if pixel ID not existing
|
---|
204 | if (PixMap->Pixel_to_HVcrate(atoi(Parameter[n].c_str())) == PixMap->PM_ERROR_CODE) continue;
|
---|
205 |
|
---|
206 | Crt.Min = Crt.Max = PixMap->Pixel_to_HVcrate(atoi(Parameter[n].c_str()));
|
---|
207 | Chan.Min = Chan.Max = PixMap->Pixel_to_HVboard(atoi(Parameter[n].c_str()))*NUM_CHANNELS + PixMap->Pixel_to_HVchannel(atoi(Parameter[n].c_str()));
|
---|
208 | }
|
---|
209 | // Channel identification
|
---|
210 | else {
|
---|
211 | if (ActiveCrate == -1) continue;
|
---|
212 | else Crt.Min = Crt.Max = ActiveCrate;
|
---|
213 |
|
---|
214 | Chan.Min = 0;
|
---|
215 | Chan.Max = MAX_NUM_BOARDS*NUM_CHANNELS-1;
|
---|
216 |
|
---|
217 | if (!ConvertToRange(Parameter[n], Chan)) {
|
---|
218 | PrintMessage("Numeric conversion or out-of-range error for parameter %d, skipping channel\n", n);
|
---|
219 | continue;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | // Convert voltage value and check format
|
---|
224 | if (!ConvertToDouble(Parameter[n+1], &Double)) {
|
---|
225 | PrintMessage("Error: Wrong number format for voltage setting\n");
|
---|
226 | continue;
|
---|
227 | }
|
---|
228 |
|
---|
229 | // Loop over given crates and channels
|
---|
230 | for (int i=Crt.Min; i<=Crt.Max; i++) for (int j=Chan.Min; j<=Chan.Max; j++) {
|
---|
231 | // Check that indices are in legal range
|
---|
232 | if (i<0 || i>=(int) Crates.size() || j<0 || j >=MAX_NUM_BOARDS*NUM_CHANNELS) continue;
|
---|
233 |
|
---|
234 | // Voltage change (number starts with + oder -) ignored if current DAC value is zero
|
---|
235 | if (isdigit(Parameter[n+1][0])==0 && Crates[i]->GetDAC(j) == 0) continue;
|
---|
236 |
|
---|
237 | // Relative or absolute change?
|
---|
238 | if (isdigit(Parameter[n+1][0]) == 0) Voltages[i][j] = Crates[i]->GetVoltage(j) + Double;
|
---|
239 | else Voltages[i][j] = Double;
|
---|
240 | } // Channels
|
---|
241 | } // Loop over command argument
|
---|
242 |
|
---|
243 | // Ramp voltages and update DIM services
|
---|
244 | for (unsigned int i=0; i<Voltages.size(); i++) {
|
---|
245 | Errors += RampVoltages(i, Voltages[i]);
|
---|
246 | Crates[i]->UpdateDIM();
|
---|
247 | }
|
---|
248 |
|
---|
249 | // Error message only if not yet too many errors
|
---|
250 | if (Errors > 0) {
|
---|
251 | for (unsigned int i=0; i<Crates.size(); i++) {
|
---|
252 | if (Crates[i]->ErrorCount > MAX_ERR_COUNT) return;
|
---|
253 | }
|
---|
254 | Message(ERROR, "%d errors occurred from SetChannels()", Errors);
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | //
|
---|
259 | // Load bias settings from file
|
---|
260 | //
|
---|
261 | void User::cmd_load() {
|
---|
262 |
|
---|
263 | char Buffer[MAX_COM_SIZE];
|
---|
264 | int Errors = 0, Channel;
|
---|
265 | double Value;
|
---|
266 | FILE *File;
|
---|
267 | map<unsigned int, double> Voltages;
|
---|
268 |
|
---|
269 | // Open file
|
---|
270 | if ((File=fopen(Parameter[1].c_str(), "r")) == NULL) {
|
---|
271 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
---|
272 | return;
|
---|
273 | }
|
---|
274 |
|
---|
275 | // Scan through file line by line
|
---|
276 | while (fgets(Buffer, sizeof(Buffer), File) != NULL) {
|
---|
277 | for (unsigned int Crate=0; Crate<Crates.size(); Crate++) if (Match(Crates[Crate]->Name, Buffer)) {
|
---|
278 |
|
---|
279 | if ((int) Crate != ActiveCrate) continue;
|
---|
280 | PrintMessage("Found bias settings for crate %s (#%d)\n\r", Crates[Crate]->Name, Crate);
|
---|
281 |
|
---|
282 | Voltages.clear();
|
---|
283 | Channel = 0;
|
---|
284 | while (fscanf(File, "%lf", &Value)==1 && Channel<MAX_NUM_BOARDS*NUM_CHANNELS) {
|
---|
285 | Voltages[Channel++] = Value;
|
---|
286 | }
|
---|
287 |
|
---|
288 | // Ramp channels
|
---|
289 | Errors += RampVoltages(Crate, Voltages);
|
---|
290 |
|
---|
291 | // Update DIM service
|
---|
292 | Crates[Crate]->UpdateDIM();
|
---|
293 |
|
---|
294 | if (ferror(File) != 0) {
|
---|
295 | PrintMessage("Error reading DAC value from file, terminating. (%s)\n",strerror(errno));
|
---|
296 | return;
|
---|
297 | }
|
---|
298 | else PrintMessage("\nFinished updating board\n");
|
---|
299 | } // Loop over boards
|
---|
300 | } // while()
|
---|
301 |
|
---|
302 | if (Errors != 0) PrintMessage("Warning: %d error(s) occurred\n", Errors);
|
---|
303 | if (fclose(File) != 0) PrintMessage("Error: Could not close file '%s'\n", Parameter[1].c_str());
|
---|
304 | }
|
---|
305 |
|
---|
306 | //
|
---|
307 | // Set refresh rate
|
---|
308 | //
|
---|
309 | void User::cmd_rate() {
|
---|
310 |
|
---|
311 | double Rate;
|
---|
312 |
|
---|
313 | if (!ConvertToDouble(Parameter[1], &Rate)) {
|
---|
314 | PrintMessage("Error: Wrong number format\n");
|
---|
315 | return;
|
---|
316 | }
|
---|
317 |
|
---|
318 | // Check limits
|
---|
319 | if (Rate<MIN_RATE || Rate>MAX_RATE) {
|
---|
320 | PrintMessage("Refresh rate out of range (min: %.2f Hz, max: %.2f Hz)\n", MIN_RATE, MAX_RATE);
|
---|
321 | return;
|
---|
322 | }
|
---|
323 |
|
---|
324 | fStatusRefreshRate = Rate;
|
---|
325 | PrintMessage("Refresh rate set to %.2f Hz\n", fStatusRefreshRate);
|
---|
326 | }
|
---|
327 |
|
---|
328 | //
|
---|
329 | // Reset crates
|
---|
330 | //
|
---|
331 | void User::cmd_reset() {
|
---|
332 |
|
---|
333 | if (ActiveCrate == -1) return;
|
---|
334 |
|
---|
335 | if (Crates[ActiveCrate]->SystemReset() == 1) PrintMessage("System reset of crate %d\n", ActiveCrate);
|
---|
336 | else PrintMessage("Error: Could not reset crate %d\n", ActiveCrate);
|
---|
337 |
|
---|
338 | }
|
---|
339 |
|
---|
340 | //
|
---|
341 | // Read channel
|
---|
342 | //
|
---|
343 | void User::cmd_gs() {
|
---|
344 |
|
---|
345 | double Voltage;
|
---|
346 |
|
---|
347 | if (ActiveCrate == -1) return;
|
---|
348 |
|
---|
349 | if (!ConvertToDouble(Parameter[1], &Voltage)) {
|
---|
350 | PrintMessage("Error: Wrong number format\n");
|
---|
351 | return;
|
---|
352 | }
|
---|
353 |
|
---|
354 | if (Crates[ActiveCrate]->GlobalSet(Voltage) != 1) {
|
---|
355 | PrintMessage("Error: Could not global set crate %d\n", ActiveCrate);
|
---|
356 | }
|
---|
357 | }
|
---|
358 |
|
---|
359 | //
|
---|
360 | // Save bias settings of all boards
|
---|
361 | //
|
---|
362 | void User::cmd_save() {
|
---|
363 |
|
---|
364 | FILE *File;
|
---|
365 | time_t Time = time(NULL);
|
---|
366 |
|
---|
367 | if ((File = fopen(Parameter[1].c_str(), "w")) == NULL) {
|
---|
368 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
---|
369 | return;
|
---|
370 | }
|
---|
371 |
|
---|
372 | fprintf(File,"********** Bias settings of %s **********\n\n", ctime(&Time));
|
---|
373 |
|
---|
374 | for (unsigned int i=0; i<Crates.size(); i++) {
|
---|
375 | fprintf(File, "%s\n\n", Crates[i]->Name);
|
---|
376 |
|
---|
377 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) fprintf(File,"%.3f ",Crates[i]->GetVoltage(j));
|
---|
378 | fprintf(File, "\n");
|
---|
379 | }
|
---|
380 |
|
---|
381 | if (fclose(File) != 0) {
|
---|
382 | PrintMessage("Error: Could not close file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | //
|
---|
387 | // Set operation mode
|
---|
388 | //
|
---|
389 | void User::cmd_mode() {
|
---|
390 |
|
---|
391 | if (Match(Parameter[1], "static")) Mode = mode_static;
|
---|
392 | else {
|
---|
393 | Mode = mode_dynamic;
|
---|
394 | for (unsigned int i=0; i<Crates.size(); i++) {
|
---|
395 | Crates[i]->SetRefCurrent();
|
---|
396 | }
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
400 | //
|
---|
401 | // Print status
|
---|
402 | //
|
---|
403 | void User::cmd_status() {
|
---|
404 |
|
---|
405 | PrintMessage(" Number of crates: %d\n", Crates.size());
|
---|
406 | PrintMessage(" Active crate: %d\n", ActiveCrate);
|
---|
407 | PrintMessage(" Refresh rate: %.2f Hz\n", fStatusRefreshRate);
|
---|
408 | PrintMessage(" Time out: %.2f s\n\n", fTimeOut);
|
---|
409 | PrintMessage(" MaxDiff : %u\n", fMaxDiff);
|
---|
410 |
|
---|
411 | for (unsigned int i=0; i<Crates.size(); i++) {
|
---|
412 | PrintMessage(" CRATE %d (%s)\n Wrap counter: %s (%d) Reset: %s Error count: %d\n ",
|
---|
413 | i, Crates[i]->Name, Crates[i]->WrapOK ? "ok":"error", Crates[i]->WrapCount,
|
---|
414 | Crates[i]->ResetHit ? "yes" : "no", Crates[i]->ErrorCount);
|
---|
415 |
|
---|
416 | if (Parameter.size() == 1) PrintMessage("Channel voltages (in V)");
|
---|
417 | else if (Match(Parameter[1], "dac")) PrintMessage("Channel voltages (in DAC values)");
|
---|
418 | else PrintMessage("Channel currents (in uA)");
|
---|
419 |
|
---|
420 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) {
|
---|
421 | if (j%12 == 0) PrintMessage("\n%3.1d: ", j);
|
---|
422 | if (!Crates[i]->Present[j/NUM_CHANNELS][j%NUM_CHANNELS]) PrintMessage(" - ");
|
---|
423 | else if (Parameter.size() == 1) PrintMessage("%#5.2f ",Crates[i]->GetVoltage(j));
|
---|
424 | else if (Match(Parameter[1], "dac")) PrintMessage("%5d ", Crates[i]->GetDAC(j));
|
---|
425 | else PrintMessage("%#5.2f %s ", Crates[i]->GetCurrent(j), Crates[i]->OC[j/NUM_CHANNELS][j%NUM_CHANNELS] ? "OC":"");
|
---|
426 | }
|
---|
427 | PrintMessage("\n");
|
---|
428 | }
|
---|
429 | }
|
---|
430 |
|
---|
431 | //
|
---|
432 | // Set timeout to return from read
|
---|
433 | //
|
---|
434 | void User::cmd_timeout() {
|
---|
435 |
|
---|
436 | double Timeout;
|
---|
437 |
|
---|
438 | if (!ConvertToDouble(Parameter[1], &Timeout)) {
|
---|
439 | PrintMessage("Error: Wrong number format\n");
|
---|
440 | return;
|
---|
441 | }
|
---|
442 |
|
---|
443 | fTimeOut = Timeout;
|
---|
444 | PrintMessage("Timeout set to %.2f s\n", fTimeOut);
|
---|
445 | }
|
---|
446 |
|
---|
447 | //
|
---|
448 | // Exit program (Signal makes readline return and sets ExitRequest)
|
---|
449 | //
|
---|
450 | void User::cmd_exit() {
|
---|
451 |
|
---|
452 | pthread_kill(MainThread, SIGTERM);
|
---|
453 | }
|
---|
454 |
|
---|
455 |
|
---|
456 | //
|
---|
457 | // Print message to screen and to DIM text service
|
---|
458 | //
|
---|
459 | void User::PrintMessage(const char *Format, ...) {
|
---|
460 |
|
---|
461 | static char Error[] = "vasprintf() failed in PrintMessage()";
|
---|
462 | char *Text;
|
---|
463 |
|
---|
464 | // Evaluate arguments
|
---|
465 | va_list ArgumentPointer;
|
---|
466 | va_start(ArgumentPointer, Format);
|
---|
467 | if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
|
---|
468 | va_end(ArgumentPointer);
|
---|
469 |
|
---|
470 | // Print to console
|
---|
471 | printf("%s", Text); // New prompt
|
---|
472 | fflush(stdout);
|
---|
473 | if (strlen(Text)>0 && Text[strlen(Text)-1]=='\n') rl_on_new_line(); // New prompt
|
---|
474 |
|
---|
475 | // Send to DIM text service
|
---|
476 | ConsoleOut->updateService(Text);
|
---|
477 |
|
---|
478 | // Free old text
|
---|
479 | if (ConsoleText != Error) free(ConsoleText);
|
---|
480 | ConsoleText = Text;
|
---|
481 | }
|
---|
482 |
|
---|
483 |
|
---|
484 | // Ramp to new voltage with maximum step size given in fMaxDiff
|
---|
485 | // No ramping when decreasing voltage
|
---|
486 | unsigned int User::RampVoltages(int Crate, map<unsigned int, double> Voltages) {
|
---|
487 |
|
---|
488 | map<unsigned int, double> Target;
|
---|
489 | int Errors = 0;
|
---|
490 |
|
---|
491 | // Ramp until all channels at desired value
|
---|
492 | while (!Voltages.empty() && Errors < MAX_ERR_COUNT) {
|
---|
493 | // Remove channels already at target (check for DAC, not for floating-point voltage)
|
---|
494 | for (map<unsigned int, double>::iterator it = Voltages.begin(); it != Voltages.end(); ++it) {
|
---|
495 | //if (Crates[Crate]->GetDAC(it->first) == (unsigned int ) (it->second/90.0*0x0fff)) Voltages.erase(it);
|
---|
496 | if (fabs(Crates[Crate]->GetVoltage(it->first)-it->second) < 0.001) Voltages.erase(it);
|
---|
497 | }
|
---|
498 |
|
---|
499 | // Limit voltage changes to fMaxDiff
|
---|
500 | Target = Voltages;
|
---|
501 | for (map<unsigned int, double>::iterator it = Target.begin(); it != Target.end(); ++it) {
|
---|
502 | if (Crates[Crate]->GetVoltage(it->first) + fMaxDiff < it->second) {
|
---|
503 | it->second = Crates[Crate]->GetVoltage(it->first) + fMaxDiff;
|
---|
504 | }
|
---|
505 | }
|
---|
506 |
|
---|
507 | // Set channels to next target and wait 10 ms
|
---|
508 | if (Crates[Crate]->SetChannels(Target) != 1) Errors++;
|
---|
509 | usleep(10000);
|
---|
510 | }
|
---|
511 |
|
---|
512 | return Errors;
|
---|
513 | }
|
---|
514 |
|
---|
515 |
|
---|
516 | //
|
---|
517 | // Check status
|
---|
518 | //
|
---|
519 | void User::Monitor() {
|
---|
520 |
|
---|
521 | static bool Warned = false;
|
---|
522 | int Ret;
|
---|
523 |
|
---|
524 | while (!ExitRequest) {
|
---|
525 |
|
---|
526 | // Wait (this is the only allowed cancelation point)
|
---|
527 | if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) != 0) {
|
---|
528 | Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
|
---|
529 | }
|
---|
530 | if (usleep((unsigned long) floor(1000000./fStatusRefreshRate)) == -1) {
|
---|
531 | Message(FATAL, "usleep() failed (%s)", strerror(errno));
|
---|
532 | }
|
---|
533 | if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL)) != 0) {
|
---|
534 | Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
|
---|
535 | }
|
---|
536 |
|
---|
537 | // Check all crates
|
---|
538 | for (unsigned int i=0; i<Crates.size(); i++) {
|
---|
539 | if (Crates[i]->ErrorCount > MAX_ERR_COUNT) {
|
---|
540 | if (!Warned) {
|
---|
541 | Warned = true;
|
---|
542 | Message(WARN, "Crate %d has many read/write errors, further error reporting disabled", i);
|
---|
543 | }
|
---|
544 | continue;
|
---|
545 | }
|
---|
546 |
|
---|
547 | if (Crates[i]->ResetHit) {
|
---|
548 | Message(INFO, "Manual reset of board %d, setting voltages to zero and issuing system reset", i);
|
---|
549 | Crates[i]->GlobalSet(0);
|
---|
550 | Crates[i]->SystemReset();
|
---|
551 | }
|
---|
552 |
|
---|
553 | if (!Crates[i]->WrapOK) {
|
---|
554 | Message(ERROR, "Wrap counter mismatch of board %d", i);
|
---|
555 | }
|
---|
556 |
|
---|
557 | if (Crates[i]->ReadAll() != 1) {
|
---|
558 | Message(ERROR, "Monitor could not read status from crate %d", i);
|
---|
559 | continue;
|
---|
560 | }
|
---|
561 |
|
---|
562 | map<unsigned int, double> Voltages;
|
---|
563 |
|
---|
564 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) {
|
---|
565 | if (Crates[i]->OC[j/NUM_CHANNELS][j%NUM_CHANNELS]) {
|
---|
566 | Message(WARN, "Overcurrent on crate %d, board %d, channel %d, setting voltage to zero", i, j/NUM_CHANNELS, j%NUM_CHANNELS);
|
---|
567 | Voltages[j] = 0;
|
---|
568 | }
|
---|
569 | }
|
---|
570 | if (!Voltages.empty()) {
|
---|
571 | Crates[i]->SetChannels(Voltages);
|
---|
572 | Crates[i]->SystemReset();
|
---|
573 | }
|
---|
574 |
|
---|
575 | if (Mode == mode_dynamic) Crates[i]->AdaptVoltages();
|
---|
576 | } // for
|
---|
577 | } // while
|
---|
578 | }
|
---|
579 |
|
---|
580 | // Call monitor loop inside class
|
---|
581 | void User::LaunchMonitor(User *m) {
|
---|
582 |
|
---|
583 | m->Monitor();
|
---|
584 | }
|
---|
585 |
|
---|
586 |
|
---|
587 | //
|
---|
588 | // Check if two strings match (min 1 character must match)
|
---|
589 | //
|
---|
590 | bool User::Match(string str, const char *cmd) {
|
---|
591 |
|
---|
592 | return strncasecmp(str.c_str(),cmd,strlen(str.c_str())==0 ? 1:strlen(str.c_str())) ? false:true;
|
---|
593 | }
|
---|
594 |
|
---|
595 | //
|
---|
596 | // Conversion function from string to double or int
|
---|
597 | //
|
---|
598 | // Return false if conversion did not stop on whitespace or EOL character
|
---|
599 | bool User::ConvertToDouble(string String, double *Result) {
|
---|
600 |
|
---|
601 | char *EndPointer;
|
---|
602 |
|
---|
603 | *Result = strtod(String.c_str(), &EndPointer);
|
---|
604 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
---|
605 | return true;
|
---|
606 | }
|
---|
607 |
|
---|
608 | bool User::ConvertToInt(string String, int *Result) {
|
---|
609 |
|
---|
610 | char *EndPointer;
|
---|
611 |
|
---|
612 | *Result = (int) strtol(String.c_str(), &EndPointer, 0);
|
---|
613 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
---|
614 | return true;
|
---|
615 | }
|
---|
616 |
|
---|
617 | //
|
---|
618 | // Interprets a range
|
---|
619 | //
|
---|
620 | bool User::ConvertToRange(string String, struct User::Range &R) {
|
---|
621 |
|
---|
622 | int N, M;
|
---|
623 |
|
---|
624 | // Full range
|
---|
625 | if (Match(String, "all")) return true;
|
---|
626 |
|
---|
627 | // Single number
|
---|
628 | if (ConvertToInt(String, &N)) {
|
---|
629 | if (N>= R.Min && N<=R.Max) {
|
---|
630 | R.Max = R.Min = N;
|
---|
631 | return true;
|
---|
632 | }
|
---|
633 | return false;
|
---|
634 | }
|
---|
635 |
|
---|
636 | // Range a-b
|
---|
637 | vector<string> V = EvidenceServer::Tokenize(String, "-");
|
---|
638 | if (V.size()==2 && ConvertToInt(V[0], &N) && ConvertToInt(V[1], &M) && N>=R.Min && M<=R.Max) {
|
---|
639 | R.Min = N;
|
---|
640 | R.Max = M;
|
---|
641 | return true;
|
---|
642 | }
|
---|
643 |
|
---|
644 | return false;
|
---|
645 | }
|
---|