| 1 | /********************************************************************\
|
|---|
| 2 |
|
|---|
| 3 | Class interfacing to FAD board
|
|---|
| 4 |
|
|---|
| 5 | \********************************************************************/
|
|---|
| 6 |
|
|---|
| 7 | #include "FADBoard.h"
|
|---|
| 8 | using namespace std;
|
|---|
| 9 |
|
|---|
| 10 | //
|
|---|
| 11 | // Constructor
|
|---|
| 12 | //
|
|---|
| 13 | FADBoard::FADBoard(string Server, unsigned short ServerPort, class FAD *Parent, unsigned int Num) {
|
|---|
| 14 |
|
|---|
| 15 | int Ret;
|
|---|
| 16 |
|
|---|
| 17 | // Initialization
|
|---|
| 18 | m = Parent;
|
|---|
| 19 | Active = true;
|
|---|
| 20 | Continue = true;
|
|---|
| 21 | CommOK = false;
|
|---|
| 22 | ACalib.Time = -1;
|
|---|
| 23 | Status.Update.tv_sec = -1;
|
|---|
| 24 | Port = ServerPort;
|
|---|
| 25 |
|
|---|
| 26 | Name = new char [Server.size()+1]; // Name in permanent memory for DIM service
|
|---|
| 27 | strcpy(Name, Server.c_str());
|
|---|
| 28 |
|
|---|
| 29 | // Initialise mutex for synchronization
|
|---|
| 30 | pthread_mutexattr_t Attr;
|
|---|
| 31 |
|
|---|
| 32 | if ((Ret = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
|
|---|
| 33 | m->Message(m->ERROR, "pthread_mutex_settype() failed (%s)", strerror(Ret));
|
|---|
| 34 | }
|
|---|
| 35 | if ((Ret = pthread_mutex_init(&Mutex, &Attr)) != 0) {
|
|---|
| 36 | m->Message(m->FATAL, "pthread_mutex_init() failed (%s)", strerror(Ret));
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | // Initialise condition variable for synchronization
|
|---|
| 40 | if ((Ret = pthread_cond_init(&CondVar, NULL)) != 0) {
|
|---|
| 41 | m->Message(m->FATAL, "pthread_cond_init() failed (%s)", strerror(Ret));
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | // Construct DIM service name prefix
|
|---|
| 45 | stringstream ID;
|
|---|
| 46 | ID << SERVER_NAME"/Board" << setfill('0') << setw(2) << Num << "/";
|
|---|
| 47 |
|
|---|
| 48 | DIM_Name = new DimService((ID.str()+"Server").c_str(), Name);
|
|---|
| 49 | DIM_ID = new DimService((ID.str()+"BoardID").c_str(), (char *) "S", NULL, 0);
|
|---|
| 50 | DIM_Temp = new DimService((ID.str()+"Temperature").c_str(), (char *) "F", NULL, 0);
|
|---|
| 51 | DIM_DAC = new DimService((ID.str()+"DAC").c_str(), (char *) "S", NULL, 0);
|
|---|
| 52 | DIM_ROI = new DimService((ID.str()+"ROI").c_str(), (char *) "S", NULL, 0);
|
|---|
| 53 |
|
|---|
| 54 | // Create thread that connects and receives data
|
|---|
| 55 | if ((Ret = pthread_create(&Thread, NULL, (void * (*)(void *)) LaunchThread,(void *) this)) != 0) {
|
|---|
| 56 | m->Message(m->FATAL, "pthread_create() failed in FADBoard() (%s)", strerror(Ret));
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | // Start thread to connect to other sockets
|
|---|
| 60 | DimThread::start();
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | //
|
|---|
| 64 | // Destructor
|
|---|
| 65 | //
|
|---|
| 66 | FADBoard::~FADBoard() {
|
|---|
| 67 |
|
|---|
| 68 | int Ret;
|
|---|
| 69 |
|
|---|
| 70 | // Cancel thread (if it did not quit already) and wait for it to quit
|
|---|
| 71 | if ((Ret = pthread_cancel(Thread)) != 0 && Ret != ESRCH) {
|
|---|
| 72 | m->Message(m->ERROR, "pthread_cancel() failed in ~FADBoard() (%s)", strerror(Ret));
|
|---|
| 73 | }
|
|---|
| 74 | if ((Ret = pthread_join(Thread, NULL)) != 0) {
|
|---|
| 75 | m->Message(m->ERROR, "pthread_join() failed in ~FADBoard (%s)", strerror(Ret));
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | delete DIM_Name;
|
|---|
| 79 | delete DIM_ID;
|
|---|
| 80 | delete DIM_Temp;
|
|---|
| 81 | delete DIM_DAC;
|
|---|
| 82 | delete DIM_ROI;
|
|---|
| 83 | delete[] Name;
|
|---|
| 84 |
|
|---|
| 85 | // Delete condition variable
|
|---|
| 86 | if ((Ret = pthread_cond_destroy(&CondVar)) != 0) {
|
|---|
| 87 | m->Message(m->ERROR, "pthread_cond_destroy() failed in ~FADBoard (%s)", strerror(Ret));
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | // Delete mutex
|
|---|
| 91 | if ((Ret = pthread_mutex_destroy(&Mutex)) != 0) {
|
|---|
| 92 | m->Message(m->ERROR, "pthread_mutex_destroy() failed in ~FADBoard (%s)", strerror(Ret));
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | //
|
|---|
| 98 | // Send data to board
|
|---|
| 99 | //
|
|---|
| 100 | void FADBoard::Send(const void *Data, size_t Bytes) {
|
|---|
| 101 |
|
|---|
| 102 | // Do not send if not active or communication problem
|
|---|
| 103 | if (!Active || !CommOK) return;
|
|---|
| 104 |
|
|---|
| 105 | // Write data
|
|---|
| 106 | ssize_t Result = write(Socket, Data, Bytes);
|
|---|
| 107 |
|
|---|
| 108 | // Check result
|
|---|
| 109 | if (Result == -1) m->PrintMessage("Error: Could not write to socket (%s)\n", strerror(errno));
|
|---|
| 110 | else if ((size_t) Result < Bytes) m->PrintMessage("Error: Could only write %d bytes out of %d to socket\n", Result, Bytes);
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | void FADBoard::Send(unsigned short Data) {
|
|---|
| 114 |
|
|---|
| 115 | unsigned short Buffer = htons(Data);
|
|---|
| 116 |
|
|---|
| 117 | Send(&Buffer, sizeof(unsigned short));
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | //
|
|---|
| 122 | // Get board status (mutex protected to avoid concurrent access in ReadLoop)
|
|---|
| 123 | //
|
|---|
| 124 | struct FADBoard::BoardStatus FADBoard::GetStatus() {
|
|---|
| 125 |
|
|---|
| 126 | int Ret;
|
|---|
| 127 | struct BoardStatus S;
|
|---|
| 128 |
|
|---|
| 129 | // Lock
|
|---|
| 130 | if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
|
|---|
| 131 | m->Message(m->FATAL, "pthread_mutex_lock() failed in ReadLoop() (%s)", strerror(Ret));
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | S = Status;
|
|---|
| 135 |
|
|---|
| 136 | // Unlock
|
|---|
| 137 | if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
|
|---|
| 138 | m->Message(m->FATAL, "pthread_mutex_unlock() failed in Unlock() (%s)", strerror(Ret));
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | return S;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 | //
|
|---|
| 146 | // Perform amplitude calibration in steps
|
|---|
| 147 | //
|
|---|
| 148 | // The steps are intended to assure that up to date data is available
|
|---|
| 149 | void FADBoard::AmplitudeCalibration() {
|
|---|
| 150 |
|
|---|
| 151 | vector<unsigned short> ROICmd;
|
|---|
| 152 | unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
|
|---|
| 153 | string Message = string("ACALIBDONE")+Name;
|
|---|
| 154 |
|
|---|
| 155 | switch (State) {
|
|---|
| 156 | // ====== Part A: Check if amplitude calibration should start and initialise =====
|
|---|
| 157 | case standbye:
|
|---|
| 158 | if (m->Mode != m->acalib) break;
|
|---|
| 159 |
|
|---|
| 160 | // Invalidate current calibration
|
|---|
| 161 | ACalib.Time = -1;
|
|---|
| 162 | Count = 0;
|
|---|
| 163 |
|
|---|
| 164 | // Save initial board status, set all ROIs to 1024 and set DAC values
|
|---|
| 165 | InitialStatus = GetStatus();
|
|---|
| 166 |
|
|---|
| 167 | for (unsigned int i=0; i<NChips*NChannels; i++) {
|
|---|
| 168 | ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
|
|---|
| 169 | ROICmd.push_back(htons(NBins));
|
|---|
| 170 | }
|
|---|
| 171 | Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
|
|---|
| 172 |
|
|---|
| 173 | DACCmd[1] = htons(0);
|
|---|
| 174 | DACCmd[3] = htons(0);
|
|---|
| 175 | DACCmd[5] = htons(0);
|
|---|
| 176 | Send(DACCmd, sizeof(DACCmd));
|
|---|
| 177 |
|
|---|
| 178 | // Clear sum vector and set state to accumulate
|
|---|
| 179 | memset(Sum, 0, sizeof(Sum));
|
|---|
| 180 | State = baseline;
|
|---|
| 181 | break;
|
|---|
| 182 |
|
|---|
| 183 | // ====== Part B: Baseline calibration =====
|
|---|
| 184 | case baseline:
|
|---|
| 185 | // Check for stopping
|
|---|
| 186 | if (m->Mode != m->acalib) {
|
|---|
| 187 | State = cleanup;
|
|---|
| 188 | break;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | // Average
|
|---|
| 192 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
|---|
| 193 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
|---|
| 194 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
|---|
| 195 | Sum[Chip][Chan][(i+Status.TriggerCell[Chip]) % NBins] += Data[Chip][Chan][i];
|
|---|
| 196 | }
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | Count++;
|
|---|
| 200 |
|
|---|
| 201 | // Determine baseline if integration finished
|
|---|
| 202 | if (Count < m->NumEventsRequested) break;
|
|---|
| 203 |
|
|---|
| 204 | for (unsigned int i=0; i<NChips; i++) {
|
|---|
| 205 | for (unsigned int j=0; j<NChannels; j++) {
|
|---|
| 206 | for (unsigned int k=0; k<NBins; k++) {
|
|---|
| 207 | ACalib.Baseline[i][j][k] = Sum[i][j][k] / m->NumEventsRequested;
|
|---|
| 208 | }
|
|---|
| 209 | }
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | // Set new DAC values and start accumulation
|
|---|
| 213 | DACCmd[1] = htons(50000);
|
|---|
| 214 | DACCmd[3] = htons(50000);
|
|---|
| 215 | DACCmd[5] = htons(50000);
|
|---|
| 216 | Send(DACCmd, sizeof(DACCmd));
|
|---|
| 217 |
|
|---|
| 218 | // Clear sum vector and set state to accumulate
|
|---|
| 219 | memset(Sum, 0, sizeof(Sum));
|
|---|
| 220 | Count = 0;
|
|---|
| 221 | State = gain;
|
|---|
| 222 | break;
|
|---|
| 223 |
|
|---|
| 224 | // ====== Part C: Gain calibration =====
|
|---|
| 225 | case gain:
|
|---|
| 226 | // Check for stopping
|
|---|
| 227 | if (m->Mode != m->acalib) {
|
|---|
| 228 | State = cleanup;
|
|---|
| 229 | break;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | // Average
|
|---|
| 233 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
|---|
| 234 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
|---|
| 235 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
|---|
| 236 | Sum[Chip][Chan][(i+Status.TriggerCell[Chip]) % NBins] += Data[Chip][Chan][i];
|
|---|
| 237 | }
|
|---|
| 238 | }
|
|---|
| 239 | }
|
|---|
| 240 | Count++;
|
|---|
| 241 |
|
|---|
| 242 | // Determine gain if integration finished
|
|---|
| 243 | if (Count < m->NumEventsRequested) break;
|
|---|
| 244 |
|
|---|
| 245 | for (unsigned int i=0; i<NChips; i++) {
|
|---|
| 246 | for (unsigned int j=0; j<NChannels; j++) {
|
|---|
| 247 | for (unsigned int k=0; k<NBins; k++) {
|
|---|
| 248 | ACalib.Gain[i][j][k] = (Sum[i][j][k] / m->NumEventsRequested) - ACalib.Baseline[i][j][k];
|
|---|
| 249 | }
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | // Set new DAC values and start accumulation
|
|---|
| 254 | DACCmd[1] = htons(0);
|
|---|
| 255 | DACCmd[3] = htons(0);
|
|---|
| 256 | DACCmd[5] = htons(0);
|
|---|
| 257 | Send(DACCmd, sizeof(DACCmd));
|
|---|
| 258 |
|
|---|
| 259 | // Clear sum vector and set state to accumulate
|
|---|
| 260 | memset(Sum, 0, sizeof(Sum));
|
|---|
| 261 | Count = 0;
|
|---|
| 262 | State = secondary;
|
|---|
| 263 | break;
|
|---|
| 264 |
|
|---|
| 265 | // ====== Part D: Secondary calibration =====
|
|---|
| 266 | case secondary:
|
|---|
| 267 | // Check for stopping
|
|---|
| 268 | if (m->Mode != m->acalib) {
|
|---|
| 269 | State = cleanup;
|
|---|
| 270 | break;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | // Average
|
|---|
| 274 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
|---|
| 275 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
|---|
| 276 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
|---|
| 277 | Sum[Chip][Chan][i] = Data[Chip][Chan][i] - ACalib.Baseline[Chip][Chan][(i-Status.TriggerCell[Chip]) % NBins];
|
|---|
| 278 | }
|
|---|
| 279 | }
|
|---|
| 280 | }
|
|---|
| 281 | Count++;
|
|---|
| 282 |
|
|---|
| 283 | // Determine secondary baseline if integration finished
|
|---|
| 284 | if (Count < m->NumEventsRequested) break;
|
|---|
| 285 |
|
|---|
| 286 | for (unsigned int i=0; i<NChips; i++) {
|
|---|
| 287 | for (unsigned int j=0; j<NChannels; j++) {
|
|---|
| 288 | for (unsigned int k=0; k<NBins; k++) {
|
|---|
| 289 | ACalib.Secondary[i][j][k] = Sum[i][j][k] / (double) m->NumEventsRequested;
|
|---|
| 290 | }
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | // Store calibration time and temperature
|
|---|
| 295 | ACalib.DNA = Status.DNA;
|
|---|
| 296 | ACalib.Frequency = Status.Frequency;
|
|---|
| 297 | ACalib.Time = time(NULL);
|
|---|
| 298 | ACalib.Temp = 0;
|
|---|
| 299 | for (unsigned int i=0; i<NTemp; i++) ACalib.Temp += Status.Temp[i] / NTemp;
|
|---|
| 300 |
|
|---|
| 301 | // Inform event thread that calibration is finished for this board
|
|---|
| 302 | if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
|
|---|
| 303 | m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard::AmplitudeCalibration (%s)", strerror(errno));
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | State = cleanup;
|
|---|
| 307 | break;
|
|---|
| 308 |
|
|---|
| 309 | // ====== Part E: Write back original ROI and DAC settings =====
|
|---|
| 310 | case cleanup:
|
|---|
| 311 | // ROI values
|
|---|
| 312 | ROICmd.clear();
|
|---|
| 313 | for (unsigned int i=0; i<NChips*NChannels; i++) {
|
|---|
| 314 | ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
|
|---|
| 315 | ROICmd.push_back(htons(InitialStatus.ROI[i/NChannels][i%NChannels]));
|
|---|
| 316 | }
|
|---|
| 317 | Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
|
|---|
| 318 |
|
|---|
| 319 | // DAC values
|
|---|
| 320 | DACCmd[1] = htons(InitialStatus.DAC[1]);
|
|---|
| 321 | DACCmd[3] = htons(InitialStatus.DAC[2]);
|
|---|
| 322 | DACCmd[5] = htons(InitialStatus.DAC[3]);
|
|---|
| 323 | Send(DACCmd, sizeof(DACCmd));
|
|---|
| 324 |
|
|---|
| 325 | State = wait;
|
|---|
| 326 | break;
|
|---|
| 327 |
|
|---|
| 328 | // ====== Wait for Mode not being idle =====
|
|---|
| 329 | case wait:
|
|---|
| 330 | if (m->Mode == m->idle) State = standbye;
|
|---|
| 331 | break;
|
|---|
| 332 | }
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | //
|
|---|
| 336 | // Connect to board and read data
|
|---|
| 337 | //
|
|---|
| 338 | void FADBoard::ReadLoop() {
|
|---|
| 339 |
|
|---|
| 340 | char Buffer[READ_BUFFER_SIZE];
|
|---|
| 341 | unsigned int Pos = 0, Temp;
|
|---|
| 342 | const PEVNT_HEADER *Header = (PEVNT_HEADER *) Buffer;
|
|---|
| 343 | ssize_t Result;
|
|---|
| 344 | struct sockaddr_in SocketAddress;
|
|---|
| 345 | struct BoardStatus PrevStatus;
|
|---|
| 346 | int Ret;
|
|---|
| 347 |
|
|---|
| 348 | // Resolve hostname
|
|---|
| 349 | struct hostent *Host = gethostbyname(Name);
|
|---|
| 350 | if (Host == 0) {
|
|---|
| 351 | m->Message(m->WARN, "Could not resolve host name for %s", Name);
|
|---|
| 352 | return;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | SocketAddress.sin_family = PF_INET;
|
|---|
| 356 | SocketAddress.sin_port = htons(Port);
|
|---|
| 357 | SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
|
|---|
| 358 |
|
|---|
| 359 | // Open socket descriptor
|
|---|
| 360 | if ((Socket = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
|
|---|
| 361 | m->Message(m->ERROR, "Could not open socket for %s (%s)\n", Name, strerror(errno));
|
|---|
| 362 | return;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | // Connect to server
|
|---|
| 366 | if (connect(Socket, (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
|
|---|
| 367 | m->PrintMessage("Could not connect to %s at port %d (%s)\n", Name, Port, strerror(errno));
|
|---|
| 368 | }
|
|---|
| 369 | else CommOK = true;
|
|---|
| 370 | memset(&PrevStatus, 0, sizeof(PrevStatus));
|
|---|
| 371 |
|
|---|
| 372 | // Leave loop if program termination requested or board communication not OK
|
|---|
| 373 | while (!m->ExitRequest && CommOK) {
|
|---|
| 374 | // Read data from socket
|
|---|
| 375 | Result = read(Socket, Buffer + Pos, sizeof(Buffer)-Pos);
|
|---|
| 376 |
|
|---|
| 377 | // Check result of read
|
|---|
| 378 | if (Result == -1) {
|
|---|
| 379 | m->PrintMessage("Error: Could not read from socket, exiting read loop (%s)\n", strerror(errno));
|
|---|
| 380 | CommOK = false;
|
|---|
| 381 | break;
|
|---|
| 382 | }
|
|---|
| 383 | else if (Result == 0) {
|
|---|
| 384 | m->PrintMessage("Server not existing anymore, exiting read loop\n");
|
|---|
| 385 | CommOK = false;
|
|---|
| 386 | break;
|
|---|
| 387 | }
|
|---|
| 388 |
|
|---|
| 389 | // If not active, discard incoming data
|
|---|
| 390 | if (!Active) continue;
|
|---|
| 391 |
|
|---|
| 392 | // Advance write pointer
|
|---|
| 393 | Pos += Result;
|
|---|
| 394 |
|
|---|
| 395 | // Check if internal buffer full
|
|---|
| 396 | if (Pos == sizeof(Buffer)) {
|
|---|
| 397 | m->PrintMessage("Internal buffer full, deleting all data in buffer\n");
|
|---|
| 398 | Pos = 0;
|
|---|
| 399 | continue;
|
|---|
| 400 | }
|
|---|
| 401 |
|
|---|
| 402 | // Check if buffer starts with start_package_flag, remove data if not
|
|---|
| 403 | Temp = 0;
|
|---|
| 404 | while (ntohs(*((unsigned short *) (Buffer+Temp))) != 0xfb01 && Temp<Pos) Temp++;
|
|---|
| 405 | if (Temp != 0) {
|
|---|
| 406 | memmove(Buffer, Buffer+Temp, Pos-Temp);
|
|---|
| 407 | Pos -= Temp;
|
|---|
| 408 | m->PrintMessage("Removed %d bytes because of start_package_flag not found for %s\n", Temp, Name);
|
|---|
| 409 | continue;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | // Wait until the buffer contains at least enough bytes to potentially hold a PEVNT_HEADER
|
|---|
| 413 | if (Pos < sizeof(PEVNT_HEADER)) continue;
|
|---|
| 414 |
|
|---|
| 415 | unsigned int Length = ntohs(Header->package_length)*2*sizeof(char);
|
|---|
| 416 | if (Pos < Length) continue;
|
|---|
| 417 |
|
|---|
| 418 | // Extract data if event end package flag correct
|
|---|
| 419 | if (ntohs(*(unsigned short *) (Buffer+Length-sizeof(unsigned short))) == 0x04FE) {
|
|---|
| 420 |
|
|---|
| 421 | // Prepare pointers to channel data (channels stored in order 0,9,18,27 - 1,10,19,28 - ... - 8,17,26,35)
|
|---|
| 422 | PCHANNEL *Channel[NChips*NChannels], *Pnt=(PCHANNEL *) (Header+1);
|
|---|
| 423 | for(unsigned int i=0; i<NChips*NChannels; i++) {
|
|---|
| 424 | Channel[i] = Pnt;
|
|---|
| 425 | Pnt = (PCHANNEL *) ((short *) (Channel[i] + 1) + ntohs(Channel[i]->roi));
|
|---|
| 426 | }
|
|---|
| 427 |
|
|---|
| 428 | PrevStatus = Status;
|
|---|
| 429 |
|
|---|
| 430 | // Wait until event thread processed the previous data and lock to avoid concurrent access in GetStatus()
|
|---|
| 431 | Lock();
|
|---|
| 432 | while (!Continue) {
|
|---|
| 433 | if ((Ret = pthread_cond_wait(&CondVar, &Mutex)) != 0) {
|
|---|
| 434 | m->Message(m->ERROR, "pthread_cond_wait() failed (%s)", strerror(Ret));
|
|---|
| 435 | }
|
|---|
| 436 | }
|
|---|
| 437 | gettimeofday(&Status.Update, NULL);
|
|---|
| 438 |
|
|---|
| 439 | // Extract board and trigger information
|
|---|
| 440 | Status.BoardID = ntohs(Header->board_id);
|
|---|
| 441 | Status.FirmwareRevision = ntohs(Header->version_no);
|
|---|
| 442 | Status.BoardTime = ntohl(Header->time);
|
|---|
| 443 | Status.EventCounter = ntohl(Header->fad_evt_counter);
|
|---|
| 444 | Status.TriggerID = ntohl(Header->trigger_id);
|
|---|
| 445 | Status.TriggerType = ntohs(Header->trigger_type);
|
|---|
| 446 | Status.TriggerCRC = ntohs(Header->trigger_crc);
|
|---|
| 447 | Status.DNA = Header->DNA;
|
|---|
| 448 |
|
|---|
| 449 | // Extract frequency related information
|
|---|
| 450 | Status.Frequency = ntohl(Header->REFCLK_frequency)/1.0e3*2.048;
|
|---|
| 451 | Status.PhaseShift = Header->adc_clock_phase_shift;
|
|---|
| 452 | for (unsigned int i=0; i<NChips; i++) {
|
|---|
| 453 | if ((ntohs(Header->PLLLCK)>>12 & (1<<i)) != 0) Status.Lock[i] = true;
|
|---|
| 454 | else Status.Lock[i] = false;
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | // Extract Firmware status info
|
|---|
| 458 | Status.denable = (bool) ( ntohs(Header->PLLLCK) & (1<<11) );
|
|---|
| 459 | Status.dwrite = (bool) ( ntohs(Header->PLLLCK) & (1<<10) );
|
|---|
| 460 | Status.DCM_lock = (bool) ( ntohs(Header->PLLLCK) & (1<<9) );
|
|---|
| 461 | Status.DCM_ready = (bool) ( ntohs(Header->PLLLCK) & (1<<8) );
|
|---|
| 462 | Status.spi_clk = (bool) ( ntohs(Header->PLLLCK) & (1<<7) );
|
|---|
| 463 |
|
|---|
| 464 | // Extract temperatures (MSB indicates if temperature is positive or negative)
|
|---|
| 465 | for (unsigned int i=0; i<NTemp; i++) {
|
|---|
| 466 | if ((ntohs(Header->drs_temperature[i]) & 0x8000) == 0) Status.Temp[i] = float(ntohs(Header->drs_temperature[i]) >> 3)/16;
|
|---|
| 467 | else Status.Temp[i] = float(0xE000 | (ntohs(Header->drs_temperature[i])) >> 3)/16;
|
|---|
| 468 | }
|
|---|
| 469 |
|
|---|
| 470 | // Extract DAC channels
|
|---|
| 471 | for (unsigned int i=0; i<NDAC; i++) Status.DAC[i] = ntohs(Header->dac[i]);
|
|---|
| 472 |
|
|---|
| 473 | short Buf;
|
|---|
| 474 | for (unsigned int Chip=0; Chip<NChips; Chip++) {
|
|---|
| 475 | // Extract trigger cells
|
|---|
| 476 | Status.TriggerCell[Chip] = (int) ntohs(Channel[Chip]->start_cell);
|
|---|
| 477 |
|
|---|
| 478 | for (unsigned int Chan=0; Chan<NChannels; Chan++) {
|
|---|
| 479 | // Extract ROI
|
|---|
| 480 | Status.ROI[Chip][Chan] = ntohs(Channel[Chip+NChips*Chan]->roi);
|
|---|
| 481 |
|
|---|
| 482 | // Extract ADC data (stored in 12 bit signed twis complement with out-of-range-bit and leading zeroes)
|
|---|
| 483 | for (int i=0; i<Status.ROI[Chip][Chan]; i++) {
|
|---|
| 484 | Buf = ntohs(Channel[Chip+NChips*Chan]->adc_data[i]);
|
|---|
| 485 | (Buf <<= 4) >>= 4; //delete the sign-bit by shifting left and shift back
|
|---|
| 486 | Data[Chip][Chan][i] = Buf;
|
|---|
| 487 | }
|
|---|
| 488 | }
|
|---|
| 489 | }
|
|---|
| 490 |
|
|---|
| 491 | // Prepare predicate for condition variable
|
|---|
| 492 | Continue = false;
|
|---|
| 493 | Unlock();
|
|---|
| 494 |
|
|---|
| 495 | // Amplitude calibration (will check if Mode is acalib)
|
|---|
| 496 | AmplitudeCalibration();
|
|---|
| 497 |
|
|---|
| 498 | // Update DIM services if necessary
|
|---|
| 499 | if (memcmp(PrevStatus.Temp, Status.Temp, sizeof(Status.Temp)) != 0) {
|
|---|
| 500 | DIM_Temp->updateService(Status.Temp, sizeof(Status.Temp));
|
|---|
| 501 | }
|
|---|
| 502 | if (memcmp(PrevStatus.DAC, Status.DAC, sizeof(Status.DAC)) != 0) {
|
|---|
| 503 | DIM_DAC->updateService(Status.DAC, sizeof(Status.DAC));
|
|---|
| 504 | }
|
|---|
| 505 | if (memcmp(PrevStatus.ROI, Status.ROI, sizeof(Status.ROI)) != 0) {
|
|---|
| 506 | DIM_ROI->updateService(Status.ROI, sizeof(Status.ROI));
|
|---|
| 507 | }
|
|---|
| 508 | if (PrevStatus.BoardID != Status.BoardID) {
|
|---|
| 509 | DIM_ID->updateService(&Status.BoardID, sizeof(Status.BoardID));
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | // Inform event thread of new data
|
|---|
| 513 | string Message = string("EVENT")+Name;
|
|---|
| 514 | if (write(m->Pipe[1], Message.data(), Message.size()) == -1) {
|
|---|
| 515 | m->Message(m->ERROR, "write() to Pipe[1] failed in class FADBoard (%s)", strerror(errno));
|
|---|
| 516 | m->ExitRequest = true;
|
|---|
| 517 | }
|
|---|
| 518 | }
|
|---|
| 519 | else m->PrintMessage("End package flag incorrect, removing corrupt event\n");
|
|---|
| 520 |
|
|---|
| 521 | // Remove event data from internal buffer
|
|---|
| 522 | memmove(Buffer, Buffer+Length, Pos-Length);
|
|---|
| 523 | Pos = Pos-Length;
|
|---|
| 524 | } // while()
|
|---|
| 525 |
|
|---|
| 526 | // Close socket descriptor
|
|---|
| 527 | if (close(Socket) == -1) {
|
|---|
| 528 | m->PrintMessage("Could not close socket descriptor for board %s (%s)", Name, strerror(errno));
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | }
|
|---|
| 532 |
|
|---|
| 533 |
|
|---|
| 534 | //
|
|---|
| 535 | // Launch read thread inside class
|
|---|
| 536 | //
|
|---|
| 537 | void FADBoard::LaunchThread(class FADBoard *m) {
|
|---|
| 538 |
|
|---|
| 539 | m->ReadLoop();
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 | //
|
|---|
| 544 | // Lock and unlock mutex
|
|---|
| 545 | //
|
|---|
| 546 | void FADBoard::Lock() {
|
|---|
| 547 |
|
|---|
| 548 | int Ret;
|
|---|
| 549 |
|
|---|
| 550 | if ((Ret = pthread_mutex_lock(&Mutex)) != 0) {
|
|---|
| 551 | m->Message(m->FATAL, "pthread_mutex_lock() failed in class FADBoard (%s)", strerror(Ret));
|
|---|
| 552 | }
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | void FADBoard::Unlock() {
|
|---|
| 556 |
|
|---|
| 557 | int Ret;
|
|---|
| 558 |
|
|---|
| 559 | if ((Ret = pthread_mutex_unlock(&Mutex)) != 0) {
|
|---|
| 560 | m->Message(m->FATAL, "pthread_mutex_unlock() failed in class FADBoard (%s)", strerror(Ret));
|
|---|
| 561 | }
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 | //
|
|---|
| 566 | // Open other sockets
|
|---|
| 567 | //
|
|---|
| 568 | void FADBoard::threadHandler() {
|
|---|
| 569 |
|
|---|
| 570 | int List[] = {5001, 5002, 5003, 5004, 5005, 5006, 5007};
|
|---|
| 571 | int Socket[sizeof(List)/sizeof(int)], MaxSocketNum, Ret;
|
|---|
| 572 | fd_set DescriptorList;
|
|---|
| 573 | char Buffer[1000000];
|
|---|
| 574 |
|
|---|
| 575 | // Resolve hostname
|
|---|
| 576 | struct hostent *Host = gethostbyname(Name);
|
|---|
| 577 | if (Host == 0) {
|
|---|
| 578 | m->PrintMessage("OtherSockets: Could not resolve host name for %s\n", Name);
|
|---|
| 579 | return;
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | // Connect to server
|
|---|
| 583 | struct sockaddr_in SocketAddress;
|
|---|
| 584 | SocketAddress.sin_family = PF_INET;
|
|---|
| 585 | SocketAddress.sin_addr = *(struct in_addr*) Host->h_addr;
|
|---|
| 586 |
|
|---|
| 587 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
|
|---|
| 588 | // Open socket descriptor
|
|---|
| 589 | if ((Socket[i] = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
|
|---|
| 590 | m->PrintMessage("OtherSockets: Could not open socket for port %d (%s)\n", List[i], strerror(errno));
|
|---|
| 591 | return;
|
|---|
| 592 | }
|
|---|
| 593 | MaxSocketNum = *max_element(Socket, Socket+sizeof(List)/sizeof(int));
|
|---|
| 594 |
|
|---|
| 595 | // Connect to server
|
|---|
| 596 | SocketAddress.sin_port = htons((unsigned short) List[i]);
|
|---|
| 597 | if (connect(Socket[i], (struct sockaddr *) &SocketAddress, sizeof(SocketAddress)) == -1) {
|
|---|
| 598 | m->PrintMessage("OtherSockets: Could not connect to port %d (%s)\n", List[i], strerror(errno));
|
|---|
| 599 | return;
|
|---|
| 600 | }
|
|---|
| 601 | }
|
|---|
| 602 |
|
|---|
| 603 | while(true) {
|
|---|
| 604 | // Wait for data from sockets
|
|---|
| 605 | FD_ZERO(&DescriptorList);
|
|---|
| 606 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) FD_SET(Socket[i], &DescriptorList);
|
|---|
| 607 | if (select(MaxSocketNum+1, &DescriptorList, NULL, NULL, NULL) == -1) {
|
|---|
| 608 | m->PrintMessage("OtherSockets: Error with select() (%s)\n", strerror(errno));
|
|---|
| 609 | break;
|
|---|
| 610 | }
|
|---|
| 611 |
|
|---|
| 612 | // Data from socket
|
|---|
| 613 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) if (FD_ISSET(Socket[i], &DescriptorList)) {
|
|---|
| 614 | Ret = read(Socket[i], Buffer, sizeof(Buffer));
|
|---|
| 615 | if(Ret == 0) m->PrintMessage("OtherSockets: Connection to port %d not existing anymore\n", List[i]);
|
|---|
| 616 | else if (Ret == -1) m->PrintMessage("OtherSockets: Error reading from port %d (%s)\n", List[i], strerror(errno));
|
|---|
| 617 | }
|
|---|
| 618 | }
|
|---|
| 619 |
|
|---|
| 620 | // Close all sockets
|
|---|
| 621 | for (unsigned int i=0; i<sizeof(List)/sizeof(int); i++) {
|
|---|
| 622 | if ((Socket[i] != -1) && (close(Socket[i]) == -1)) {
|
|---|
| 623 | m->PrintMessage("OtherSockets: Could not close socket of port %d (%s)", List[i], strerror(errno));
|
|---|
| 624 | }
|
|---|
| 625 | }
|
|---|
| 626 | }
|
|---|