| 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 Default voltage: %.2f Pixel IDs: ", Channel < DefaultVoltage.size() ? DefaultVoltage[Channel] : 0);
|
|---|
| 276 | for (unsigned int j=0; j<List.size(); j++) PrintMessage("%u ", List[j]);
|
|---|
| 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 | if (Channel < DefaultVoltage.size()) Double = DefaultVoltage[Channel];
|
|---|
| 299 | else Double = 0;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | // Relative or absolute change?
|
|---|
| 303 | if (isdigit(Parameter[n+1][0]) == 0) Voltages[Crate][Channel] = Crates[Crate]->GetVoltage(Channel) + Double;
|
|---|
| 304 | else Voltages[Crate][Channel] = Double;
|
|---|
| 305 | } // Channels
|
|---|
| 306 | } // Loop over command argument
|
|---|
| 307 |
|
|---|
| 308 | // Ramp voltages and update DIM services
|
|---|
| 309 | for (unsigned int i=0; i<Voltages.size(); i++) {
|
|---|
| 310 | Errors += RampVoltages(i, Voltages[i]);
|
|---|
| 311 | Crates[i]->UpdateDIM();
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | // Error message only if not yet too many errors
|
|---|
| 315 | if (Errors > 0) {
|
|---|
| 316 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 317 | if (Crates[i]->ErrorCount > MAX_ERR_COUNT) return;
|
|---|
| 318 | }
|
|---|
| 319 | Message(ERROR, "%d errors occurred from SetChannels()", Errors);
|
|---|
| 320 | }
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | //
|
|---|
| 324 | // Load bias settings from file
|
|---|
| 325 | //
|
|---|
| 326 | void User::cmd_load() {
|
|---|
| 327 |
|
|---|
| 328 | char Buffer[MAX_COM_SIZE];
|
|---|
| 329 | int Errors = 0, Channel;
|
|---|
| 330 | double Value;
|
|---|
| 331 | FILE *File;
|
|---|
| 332 | map<unsigned int, double> Voltages;
|
|---|
| 333 |
|
|---|
| 334 | // Open file
|
|---|
| 335 | if ((File=fopen(Parameter[1].c_str(), "r")) == NULL) {
|
|---|
| 336 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 337 | return;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | // Scan through file line by line
|
|---|
| 341 | while (fgets(Buffer, sizeof(Buffer), File) != NULL) {
|
|---|
| 342 | for (unsigned int Crate=0; Crate<Crates.size(); Crate++) if (Match(Crates[Crate]->Name, Buffer)) {
|
|---|
| 343 |
|
|---|
| 344 | if (Crate != ActiveCrate) continue;
|
|---|
| 345 | PrintMessage("Found bias settings for crate %s (#%d)\n\r", Crates[Crate]->Name, Crate);
|
|---|
| 346 |
|
|---|
| 347 | Voltages.clear();
|
|---|
| 348 | Channel = 0;
|
|---|
| 349 | while (fscanf(File, "%lf", &Value)==1 && Channel<MAX_NUM_BOARDS*NUM_CHANNELS) {
|
|---|
| 350 | Voltages[Channel++] = Value;
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | // Ramp channels
|
|---|
| 354 | Errors += RampVoltages(Crate, Voltages);
|
|---|
| 355 |
|
|---|
| 356 | // Update DIM service
|
|---|
| 357 | Crates[Crate]->UpdateDIM();
|
|---|
| 358 |
|
|---|
| 359 | if (ferror(File) != 0) {
|
|---|
| 360 | PrintMessage("Error reading DAC value from file, terminating. (%s)\n",strerror(errno));
|
|---|
| 361 | return;
|
|---|
| 362 | }
|
|---|
| 363 | else PrintMessage("\nFinished updating board\n");
|
|---|
| 364 | } // Loop over boards
|
|---|
| 365 | } // while()
|
|---|
| 366 |
|
|---|
| 367 | if (Errors != 0) PrintMessage("Warning: %d error(s) occurred\n", Errors);
|
|---|
| 368 | if (fclose(File) != 0) PrintMessage("Error: Could not close file '%s'\n", Parameter[1].c_str());
|
|---|
| 369 | }
|
|---|
| 370 |
|
|---|
| 371 | //
|
|---|
| 372 | // Set refresh rate
|
|---|
| 373 | //
|
|---|
| 374 | void User::cmd_rate() {
|
|---|
| 375 |
|
|---|
| 376 | double Rate;
|
|---|
| 377 |
|
|---|
| 378 | if (!ConvertToDouble(Parameter[1], &Rate)) {
|
|---|
| 379 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 380 | return;
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | // Check limits
|
|---|
| 384 | if (Rate<MIN_RATE || Rate>MAX_RATE) {
|
|---|
| 385 | PrintMessage("Refresh rate out of range (min: %.2f Hz, max: %.2f Hz)\n", MIN_RATE, MAX_RATE);
|
|---|
| 386 | return;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | fStatusRefreshRate = Rate;
|
|---|
| 390 | PrintMessage("Refresh rate set to %.2f Hz\n", fStatusRefreshRate);
|
|---|
| 391 | }
|
|---|
| 392 |
|
|---|
| 393 | //
|
|---|
| 394 | // Reset crates
|
|---|
| 395 | //
|
|---|
| 396 | void User::cmd_reset() {
|
|---|
| 397 |
|
|---|
| 398 | if (Crates[ActiveCrate]->SystemReset() == 1) PrintMessage("System reset of crate %d\n", ActiveCrate);
|
|---|
| 399 | else PrintMessage("Error: Could not reset crate %d\n", ActiveCrate);
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | //
|
|---|
| 403 | // Read channel
|
|---|
| 404 | //
|
|---|
| 405 | void User::cmd_gs() {
|
|---|
| 406 |
|
|---|
| 407 | double Voltage;
|
|---|
| 408 |
|
|---|
| 409 | if (!ConvertToDouble(Parameter[1], &Voltage)) {
|
|---|
| 410 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 411 | return;
|
|---|
| 412 | }
|
|---|
| 413 |
|
|---|
| 414 | if (Crates[ActiveCrate]->GlobalSet(Voltage) != 1) {
|
|---|
| 415 | PrintMessage("Error: Could not global set crate %d\n", ActiveCrate);
|
|---|
| 416 | }
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | //
|
|---|
| 420 | // Save bias settings of all boards
|
|---|
| 421 | //
|
|---|
| 422 | void User::cmd_save() {
|
|---|
| 423 |
|
|---|
| 424 | FILE *File;
|
|---|
| 425 | time_t Time = time(NULL);
|
|---|
| 426 |
|
|---|
| 427 | if ((File = fopen(Parameter[1].c_str(), "w")) == NULL) {
|
|---|
| 428 | PrintMessage("Error: Could not open file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 429 | return;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | fprintf(File,"********** Bias settings of %s **********\n\n", ctime(&Time));
|
|---|
| 433 |
|
|---|
| 434 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 435 | fprintf(File, "%s\n\n", Crates[i]->Name);
|
|---|
| 436 |
|
|---|
| 437 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) fprintf(File,"%.3f ",Crates[i]->GetVoltage(j));
|
|---|
| 438 | fprintf(File, "\n");
|
|---|
| 439 | }
|
|---|
| 440 |
|
|---|
| 441 | if (fclose(File) != 0) {
|
|---|
| 442 | PrintMessage("Error: Could not close file '%s' (%s)\n", Parameter[1].c_str(), strerror(errno));
|
|---|
| 443 | }
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | //
|
|---|
| 447 | // Set operation mode
|
|---|
| 448 | //
|
|---|
| 449 | void User::cmd_mode() {
|
|---|
| 450 |
|
|---|
| 451 | if (Match(Parameter[1], "static")) Mode = mode_static;
|
|---|
| 452 | else {
|
|---|
| 453 | Mode = mode_dynamic;
|
|---|
| 454 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 455 | Crates[i]->SetRefCurrent();
|
|---|
| 456 | }
|
|---|
| 457 | }
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | //
|
|---|
| 461 | // Print status
|
|---|
| 462 | //
|
|---|
| 463 | void User::cmd_status() {
|
|---|
| 464 |
|
|---|
| 465 | PrintMessage(" Number of crates: %d\n", Crates.size());
|
|---|
| 466 | PrintMessage(" Active crate: %d\n", ActiveCrate);
|
|---|
| 467 | PrintMessage(" Refresh rate: %.2f Hz\n", fStatusRefreshRate);
|
|---|
| 468 | PrintMessage(" Time out: %.2f s\n\n", fTimeOut);
|
|---|
| 469 | PrintMessage(" MaxDiff : %u\n", fMaxDiff);
|
|---|
| 470 |
|
|---|
| 471 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 472 | PrintMessage(" CRATE %d (%s)\n Wrap counter: %s (%d) Reset: %s Error count: %d\n ",
|
|---|
| 473 | i, Crates[i]->Name, Crates[i]->WrapOK ? "ok":"error", Crates[i]->WrapCount,
|
|---|
| 474 | Crates[i]->ResetHit ? "yes" : "no", Crates[i]->ErrorCount);
|
|---|
| 475 |
|
|---|
| 476 | if (Parameter.size() == 1) PrintMessage("Channel voltages (in V)");
|
|---|
| 477 | else if (Match(Parameter[1], "dac")) PrintMessage("Channel voltages (in DAC values)");
|
|---|
| 478 | else PrintMessage("Channel currents (in uA)");
|
|---|
| 479 |
|
|---|
| 480 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) {
|
|---|
| 481 | if (j%12 == 0) PrintMessage("\n%3.1d: ", j);
|
|---|
| 482 | if (!Crates[i]->Present[j/NUM_CHANNELS][j%NUM_CHANNELS]) PrintMessage(" - ");
|
|---|
| 483 | else if (Parameter.size() == 1) PrintMessage("%#5.2f ",Crates[i]->GetVoltage(j));
|
|---|
| 484 | else if (Match(Parameter[1], "dac")) PrintMessage("%5d ", Crates[i]->GetDAC(j));
|
|---|
| 485 | else PrintMessage("%#5.2f %s ", Crates[i]->GetCurrent(j), Crates[i]->OC[j/NUM_CHANNELS][j%NUM_CHANNELS] ? "OC":"");
|
|---|
| 486 | }
|
|---|
| 487 | PrintMessage("\n");
|
|---|
| 488 | }
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | //
|
|---|
| 492 | // Set timeout to return from read
|
|---|
| 493 | //
|
|---|
| 494 | void User::cmd_timeout() {
|
|---|
| 495 |
|
|---|
| 496 | double Timeout;
|
|---|
| 497 |
|
|---|
| 498 | if (!ConvertToDouble(Parameter[1], &Timeout)) {
|
|---|
| 499 | PrintMessage("Error: Wrong number format\n");
|
|---|
| 500 | return;
|
|---|
| 501 | }
|
|---|
| 502 |
|
|---|
| 503 | fTimeOut = Timeout;
|
|---|
| 504 | PrintMessage("Timeout set to %.2f s\n", fTimeOut);
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | //
|
|---|
| 508 | // Exit program (Signal makes readline return and sets ExitRequest)
|
|---|
| 509 | //
|
|---|
| 510 | void User::cmd_exit() {
|
|---|
| 511 |
|
|---|
| 512 | pthread_kill(MainThread, SIGTERM);
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | //
|
|---|
| 516 | // Execute shell command
|
|---|
| 517 | //
|
|---|
| 518 | void User::cmd_shell() {
|
|---|
| 519 |
|
|---|
| 520 | if (system(Parameter[1].c_str()) == -1) PrintMessage("Error with system() call\n");
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 |
|
|---|
| 524 | //
|
|---|
| 525 | // Print message to screen and to DIM text service
|
|---|
| 526 | //
|
|---|
| 527 | void User::PrintMessage(const char *Format, ...) {
|
|---|
| 528 |
|
|---|
| 529 | static char Error[] = "vasprintf() failed in PrintMessage()";
|
|---|
| 530 | char *Text;
|
|---|
| 531 |
|
|---|
| 532 | // Evaluate arguments
|
|---|
| 533 | va_list ArgumentPointer;
|
|---|
| 534 | va_start(ArgumentPointer, Format);
|
|---|
| 535 | if (vasprintf(&Text, Format, ArgumentPointer) == -1) Text = Error;
|
|---|
| 536 | va_end(ArgumentPointer);
|
|---|
| 537 |
|
|---|
| 538 | // Print to console
|
|---|
| 539 | printf("%s", Text); // New prompt
|
|---|
| 540 | fflush(stdout);
|
|---|
| 541 | if (strlen(Text)>0 && Text[strlen(Text)-1]=='\n') rl_on_new_line(); // New prompt
|
|---|
| 542 |
|
|---|
| 543 | // Send to DIM text service
|
|---|
| 544 | ConsoleOut->updateService(Text);
|
|---|
| 545 |
|
|---|
| 546 | // Free old text
|
|---|
| 547 | if (ConsoleText != Error) free(ConsoleText);
|
|---|
| 548 | ConsoleText = Text;
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 | // Ramp to new voltage with maximum step size given in fMaxDiff
|
|---|
| 553 | // No ramping when decreasing voltage
|
|---|
| 554 | unsigned int User::RampVoltages(int Crate, map<unsigned int, double> Voltages) {
|
|---|
| 555 |
|
|---|
| 556 | map<unsigned int, double> Target;
|
|---|
| 557 | int Errors = 0;
|
|---|
| 558 |
|
|---|
| 559 | // Ramp until all channels at desired value
|
|---|
| 560 | while (!Voltages.empty() && Errors < MAX_ERR_COUNT) {
|
|---|
| 561 | // Remove channels already at target (check for DAC, not for floating-point voltage)
|
|---|
| 562 | for (map<unsigned int, double>::iterator it = Voltages.begin(); it != Voltages.end(); ++it) {
|
|---|
| 563 | if (fabs(Crates[Crate]->GetVoltage(it->first)-it->second) < 0.001) Voltages.erase(it);
|
|---|
| 564 | }
|
|---|
| 565 |
|
|---|
| 566 | // Limit voltage changes to fMaxDiff
|
|---|
| 567 | Target = Voltages;
|
|---|
| 568 | for (map<unsigned int, double>::iterator it = Target.begin(); it != Target.end(); ++it) {
|
|---|
| 569 | if (Crates[Crate]->GetVoltage(it->first) + fMaxDiff < it->second) {
|
|---|
| 570 | it->second = Crates[Crate]->GetVoltage(it->first) + fMaxDiff;
|
|---|
| 571 | }
|
|---|
| 572 | }
|
|---|
| 573 |
|
|---|
| 574 | // Set channels to next target and wait 10 ms
|
|---|
| 575 | if (Crates[Crate]->SetChannels(Target) != 1) Errors++;
|
|---|
| 576 | usleep(10000);
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | return Errors;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 | //
|
|---|
| 584 | // Check status
|
|---|
| 585 | //
|
|---|
| 586 | void User::Monitor() {
|
|---|
| 587 |
|
|---|
| 588 | static bool Warned = false;
|
|---|
| 589 | int Ret;
|
|---|
| 590 | unsigned int Count=0;
|
|---|
| 591 |
|
|---|
| 592 | while (!ExitRequest) {
|
|---|
| 593 |
|
|---|
| 594 | // Wait (this is the only allowed cancelation point)
|
|---|
| 595 | if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)) != 0) {
|
|---|
| 596 | Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
|
|---|
| 597 | }
|
|---|
| 598 | if (usleep((unsigned long) floor(1000000./fStatusRefreshRate)) == -1) {
|
|---|
| 599 | Message(FATAL, "usleep() failed (%s)", strerror(errno));
|
|---|
| 600 | }
|
|---|
| 601 | if ((Ret=pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL)) != 0) {
|
|---|
| 602 | Message(FATAL, "pthread_setcancelstate() failed (%s)", strerror(Ret));
|
|---|
| 603 | }
|
|---|
| 604 |
|
|---|
| 605 | // Check all crates
|
|---|
| 606 | for (unsigned int i=0; i<Crates.size(); i++) {
|
|---|
| 607 | if (Crates[i]->ErrorCount > MAX_ERR_COUNT) {
|
|---|
| 608 | if (!Warned) {
|
|---|
| 609 | Warned = true;
|
|---|
| 610 | Message(WARN, "Crate %d has many read/write errors, further error reporting disabled", i);
|
|---|
| 611 | }
|
|---|
| 612 | continue;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | if (Crates[i]->ResetHit) {
|
|---|
| 616 | Message(INFO, "Manual reset of crate %d, setting voltages to zero and issuing system reset", i);
|
|---|
| 617 | Crates[i]->GlobalSet(0);
|
|---|
| 618 | Crates[i]->SystemReset();
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | if (!Crates[i]->WrapOK) {
|
|---|
| 622 | Message(ERROR, "Wrap counter mismatch of crate %d", i);
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | if (Crates[i]->ReadAll() != 1) {
|
|---|
| 626 | Message(ERROR, "Monitor could not read status from crate %d", i);
|
|---|
| 627 | continue;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | map<unsigned int, double> Voltages;
|
|---|
| 631 |
|
|---|
| 632 | for (int j=0; j<MAX_NUM_BOARDS*NUM_CHANNELS; j++) {
|
|---|
| 633 | if (Crates[i]->OC[j/NUM_CHANNELS][j%NUM_CHANNELS]) {
|
|---|
| 634 | if (Count++ < 5) Message(WARN, "Overcurrent on crate %d, board %d, channel %d, setting voltage to zero", i, j/NUM_CHANNELS, j%NUM_CHANNELS);
|
|---|
| 635 | if (Count == 5) Message(ERROR, "Five overcurrent warnings, no more!");
|
|---|
| 636 | Voltages[j] = 0;
|
|---|
| 637 | }
|
|---|
| 638 | }
|
|---|
| 639 | if (!Voltages.empty()) {
|
|---|
| 640 | Crates[i]->SetChannels(Voltages);
|
|---|
| 641 | Crates[i]->SystemReset();
|
|---|
| 642 | }
|
|---|
| 643 |
|
|---|
| 644 | if (Mode == mode_dynamic) Crates[i]->AdaptVoltages();
|
|---|
| 645 | } // for
|
|---|
| 646 | } // while
|
|---|
| 647 | }
|
|---|
| 648 |
|
|---|
| 649 | // Call monitor loop inside class
|
|---|
| 650 | void User::LaunchMonitor(User *m) {
|
|---|
| 651 |
|
|---|
| 652 | m->Monitor();
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 |
|
|---|
| 656 | //
|
|---|
| 657 | // Check if two strings match (min 1 character must match)
|
|---|
| 658 | //
|
|---|
| 659 | bool User::Match(string str, const char *cmd) {
|
|---|
| 660 |
|
|---|
| 661 | return strncasecmp(str.c_str(),cmd,strlen(str.c_str())==0 ? 1:strlen(str.c_str())) ? false:true;
|
|---|
| 662 | }
|
|---|
| 663 |
|
|---|
| 664 | //
|
|---|
| 665 | // Conversion function from string to double or int
|
|---|
| 666 | //
|
|---|
| 667 | // Return false if conversion did not stop on whitespace or EOL character
|
|---|
| 668 | bool User::ConvertToDouble(string String, double *Result) {
|
|---|
| 669 |
|
|---|
| 670 | char *EndPointer;
|
|---|
| 671 |
|
|---|
| 672 | *Result = strtod(String.c_str(), &EndPointer);
|
|---|
| 673 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
|---|
| 674 | return true;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | bool User::ConvertToInt(string String, int *Result) {
|
|---|
| 678 |
|
|---|
| 679 | char *EndPointer;
|
|---|
| 680 |
|
|---|
| 681 | *Result = (int) strtol(String.c_str(), &EndPointer, 0);
|
|---|
| 682 | if(!isspace(*EndPointer) && *EndPointer!='\0') return false;
|
|---|
| 683 | return true;
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 | //
|
|---|
| 687 | // Interprets a range
|
|---|
| 688 | //
|
|---|
| 689 | bool User::ConvertToRange(string String, struct User::Range &R) {
|
|---|
| 690 |
|
|---|
| 691 | int N=0, M=0; // Init to avoid compiler warning
|
|---|
| 692 |
|
|---|
| 693 | // Full range
|
|---|
| 694 | if (Match(String, "all")) return true;
|
|---|
| 695 |
|
|---|
| 696 | // Single number
|
|---|
| 697 | if (ConvertToInt(String, &N)) {
|
|---|
| 698 | if (N>= R.Min && N<=R.Max) {
|
|---|
| 699 | R.Max = R.Min = N;
|
|---|
| 700 | return true;
|
|---|
| 701 | }
|
|---|
| 702 | return false;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | // Range a-b
|
|---|
| 706 | vector<string> V = EvidenceServer::Tokenize(String, "-");
|
|---|
| 707 | if (V.size()==2 && ConvertToInt(V[0], &N) && ConvertToInt(V[1], &M) && N>=R.Min && M<=R.Max) {
|
|---|
| 708 | R.Min = N;
|
|---|
| 709 | R.Max = M;
|
|---|
| 710 | return true;
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | return false;
|
|---|
| 714 | }
|
|---|