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