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