Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Cframe.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Cframe.C
1 /*
2 ** Cframe.C
3 **
4 ** Author: $Author: purschke $
5 ** Date: $Date: 2000/07/21 01:51:10 $
6 **
7 ** $Log: Cframe.C,v $
8 ** Revision 1.1.1.1 2000/07/21 01:51:10 purschke
9 ** mlp -- adding the new automakified "basic" module to CVS.
10 **
11 **
12 ** Revision 1.10 1999/05/17 19:47:16 markacs
13 ** (stephen) added findNextError and whole indirection chain
14 **
15 ** Revision 1.9 1999/04/16 20:12:11 markacs
16 ** (stephen) fixing findFrameErrorStart, findFrameHistoryStart, adjustFrameLength to deal correctly with padding
17 **
18 ** Revision 1.8 1999/04/13 18:27:38 markacs
19 ** (stephen) put history block access in
20 **
21 ** Revision 1.7 1998/12/16 15:40:52 markacs
22 ** (stephen markacs) changes to rawFormat code to stop using bool,true,false
23 **
24 ** Revision 1.6 1998/12/11 22:01:54 markacs
25 ** (stephen markacs) adding log into cvs tags
26 **
27 */
28 #include "phenixOnline.h"
29 #include "frameHdr.h"
30 
31 #include "Cframe.h"
32 #include "CframeV1.h"
33 #include <stdlib.h>
34 
35 /* Returns a pointer to the start of the frame data block.
36 **
37 ** This function does not depend on the version of the frame header since header
38 ** length is in the same location for all versions.
39 */
41 #ifdef STRONG_CHECK
42  if (!validFrameHdr(frame_ptr)) return ptrFailure;
43 #endif
44  return (PHDWORD*)frame_ptr + getFrameHdrLength(frame_ptr);
45 }
46 
47 /* Return a pointer to the end of the frame data block/
48  */
50  PHDWORD* start_ptr = findFrameDataStart(frame_ptr);
51  PHDWORD length = getFrameDataLength(frame_ptr);
52  if ((start_ptr == ptrFailure)||(length==valueFailure))
53  return ptrFailure;
54  else
55  return start_ptr + length - 1;
56 }
57 
58 
59 /*
60 ** checkFrameHdrVersion checks that FrameMark and FrameHdrLength
61 ** are correct for a frame of the given version and, if so,
62 ** returns FrameHdrVersion. Elsewise it returns valueFailure.
63 */
65 {
66  PHDWORD frameMark;
67  PHDWORD frameHdrLength;
68  UINT version = getFrameHdrVersion(frame_ptr);
69  if (version != valueFailure) {
70  frameMark = getFrameMark(frame_ptr);
71  frameHdrLength = getFrameHdrLength(frame_ptr);
72  if ( (frameMark == frameMarkV[version]) &&
73  (frameHdrLength == frameHdrLengthV[version])) {
75  return version;
76  }
77  else {
78  setFrameError(FORMAT_ERR_INVALID_FRAMEMARK, frame_ptr, frameMark);
79  return valueFailure;
80  }
81  }
82  else return valueFailure;
83 }
84 
85 /* Return a pointer to the last word in the frame.
86  */
88 {
89  if (validFrameHdr(frame_ptr))
90  {
91  PHDWORD frameLength = getFrameLength(frame_ptr);
92  if (frameLength != valueFailure)
93  {
95  return ( (PHDWORD*)frame_ptr + frameLength - 1);
96  }
97  else return ptrFailure;
98  }
99  else return ptrFailure;
100 }
101 
102 /*
103 ** checkFrameEndianism returns 1 if the frame header is valid
104 ** as it is, returns 0 if the frame header would be valid if swapped,
105 ** and returns valueFailure otherwise.
106 */
108 {
109  if (validFrameHdr(frame_ptr)) return 1;
110  else
111  { PHDWORD * buffer = (PHDWORD*)malloc(4*maxFrameHdrLength);
112  dwordByteSwap(buffer, frame_ptr, maxFrameHdrLength);
113  if (validFrameHdr(buffer)) return 0;
114  else return valueFailure;
115  }
116 }
117 
118 
119 
120 /* adjustFrameLength adds addDwords to the frame, with padding
121  if pad is TRUE, and returns the new frame length */
122 VALUE_ret adjustFrameLength (FRAME_ptr frame_ptr, UINT maxFrameLength,
123  UINT addDwords , LOGIC_ret pad)
124 {
125  UINT padDwords;
126  UINT currentLength;
127  UINT newDwords;
128  UINT newLength;
129 
130 #ifdef STRONGCHECK
131  if (!validFrameHdr(frame_ptr)) return valueFailure;
132 #endif
133 
134  if (pad) removeFramePadding(frame_ptr);
135  currentLength = getFrameLength(frame_ptr);
136 
137  newDwords = addDwords;
138  newLength = currentLength + newDwords;
139 
140  /* Perform the padding calculation if requested
141  */
142  if (pad) {
143  UINT modulo = newLength % currentFrameQuantum;
144  if (modulo > 0) padDwords = currentFrameQuantum - modulo;
145  else padDwords = 0;
146 
147  newLength += padDwords;
148  newDwords += padDwords;
149  }
150  else padDwords = 0;
151 
152  /* Check for buffer overflow
153  */
154  if (newLength > maxFrameLength) {
155  setFrameError(FORMAT_ERR_BUFFER_OVERFLOW, frame_ptr, newLength);
156  return valueFailure;
157  }
158 
159  /*
160  ** below:Check to make sure we don't overflow the 32 bit length field.
161  ** Note that this calculation may look "funny", but it is necessary
162  ** to evaluate it like this to prevent the calculation from
163  ** suffering a 32-bit overflow itself. Also note that
164  ** a length that fills the 32-bit length field corresponds
165  ** to a frame taking up over 16 gigabytes of memory.
166  */
167  if (maxDwordValue - currentLength < newDwords) {
168  setFrameError(FORMAT_ERR_LENGTH_OVERFLOW, frame_ptr, newLength);
169  return valueFailure;
170  }
171 
172  setFrameLength(frame_ptr, newLength);
173  if (padDwords != 0)
174  {
175  PHDWORD* pad_ptr = frame_ptr + newLength - padDwords;
176  dwordClear (pad_ptr, padDwords); /* clear padding words */
177  setFramePadding(frame_ptr, padDwords);
178  }
179  return newLength;
180 }
181 
182 
183 /* removeFramePadding strips padding off a frame and
184  returns the length of the frame */
185 
187 {
188  UINT length;
189  UINT padDwords = getFramePadding(frame_ptr);
190  length = getFrameLength(frame_ptr);
191  length-=padDwords;
192  setFrameLength(frame_ptr, length);
193  setFramePadding(frame_ptr, 0);
194  return length;
195 }
196 
197 
198 /* Here begins the code for the version-DEPENDENT functions */
199 /* (only if not in DCM mode) */
200 /* ======================================================== */
201 
202 #ifndef DCM
203 
214 
225 
226 /*
227 ** IMPORTANT: The zeroth element in the indirection arrays is defined in the
228 ** lines below to be a null pointer. It is thus very important
229 ** that anywhere the array is deferenced, it is checked that
230 ** it is not the zeroth element that is dereferenced. If it
231 ** is, a null pointer will be dereferenced and s segmentation
232 ** fault or some other ugliness will occur.
233 */
243 
247 
253 
261 
264 
266 
272 
274 {
275  UINT version = getFrameHdrVersion(frame_ptr);
276  if (version != valueFailure)
277  return (*getFrameStatusV[version])(frame_ptr);
278  else
279  return valueFailure;
280 }
281 
283 {
284  UINT version = getFrameHdrVersion(frame_ptr);
285  if (version != valueFailure)
286  return (*getFrameTypeV[version])(frame_ptr);
287  else
288  return valueFailure;
289 }
290 
291 
293 {
294  UINT version = getFrameHdrVersion(frame_ptr);
295  if (version != valueFailure)
296  return (*getFrameSourceIdV[version])(frame_ptr);
297  else
298  return valueFailure;
299 }
300 
301 
303 {
304  UINT version = getFrameHdrVersion(frame_ptr);
305  if (version != valueFailure)
306  return (*getFrameDataTypeV[version])(frame_ptr);
307  else
308  return valueFailure;
309 }
310 
311 
313 {
314  UINT version = getFrameHdrVersion(frame_ptr);
315  if (version != valueFailure)
316  return (*getFrameDataLengthV[version])(frame_ptr);
317  else
318  return valueFailure;
319 }
320 
321 
323 {
324  UINT version = getFrameHdrVersion(frame_ptr);
325  if (version != valueFailure)
326  return (*getFrameHistoryLengthV[version])(frame_ptr);
327  else
328  return valueFailure;
329 }
330 
331 
333 {
334  UINT version = getFrameHdrVersion(frame_ptr);
335  if (version != valueFailure)
336  return (*getFrameErrorLengthV[version])(frame_ptr);
337  else
338  return valueFailure;
339 }
340 
341 
343 {
344  UINT version = getFrameHdrVersion(frame_ptr);
345  if (version != valueFailure)
346  return (*getFrameAlignLengthV[version])(frame_ptr);
347  else
348  return valueFailure;
349 }
350 
351 
353 {
354  UINT version = getFrameHdrVersion(frame_ptr);
355  if (version != valueFailure)
356  return (*findFrameAlignBlockV[version])(frame_ptr);
357  else
358  return ptrFailure;
359 }
360 
361 
363 {
364  UINT version = getFrameHdrVersion(frame_ptr);
365  if (version != valueFailure)
366  return (*findFrameHistoryStartV[version])(frame_ptr);
367  else
368  return ptrFailure;
369 }
370 
371 
373 {
374  UINT version = getFrameHdrVersion(frame_ptr);
375  if (version != valueFailure)
376  return (*findFrameErrorStartV[version])(frame_ptr);
377  else
378  return ptrFailure;
379 }
380 
381 
382 VALUE_ret orFrameStatus (FRAME_ptr frame_ptr, UINT statusBits)
383 {
384  UINT version = getFrameHdrVersion(frame_ptr);
385  if (version != valueFailure)
386  return (*orFrameStatusV[version])(frame_ptr, statusBits);
387  else
388  return valueFailure;
389 }
390 
391 
393 {
394  UINT version = getFrameHdrVersion(frame_ptr);
395  if (version != valueFailure)
396  return (*getFramePaddingV[version])(frame_ptr);
397  else
398  return valueFailure;
399 }
400 
401 
402 /* Construct a header for a frame using the "current" header version
403  */
404 VALUE_ret makeFrameHdr (FRAME_ptr frame_ptr, UINT maxFrameLen, UINT dataType,
405  UINT frameType, UINT sourceId)
406 {
407  return (*makeFrameHdrV[currentFrameHdrVersion])(frame_ptr, maxFrameLen, dataType,
408  frameType, sourceId);
409 }
410 
412 {
413  UINT version = getFrameHdrVersion(frame_ptr);
414  if (version != valueFailure)
415  return (*adjustFrameDataLengthV[version])(frame_ptr, newDwords);
416  else
417  return valueFailure;
418 }
419 
420 
422 {
423  UINT version = getFrameHdrVersion(frame_ptr);
424  if (version != valueFailure)
425  return (*adjustFrameHistoryLengthV[version])(frame_ptr, newDwords);
426  else
427  return valueFailure;
428 }
429 
431 {
432  UINT version = getFrameHdrVersion(frame_ptr);
433  if (version != valueFailure)
434  return (*adjustFrameErrorLengthV[version])(frame_ptr, newDwords);
435  else
436  return valueFailure;
437 }
438 
439 VALUE_ret setFramePadding (FRAME_ptr frame_ptr, UINT padDwords)
440 {
441  UINT version = getFrameHdrVersion(frame_ptr);
442  if (version != valueFailure)
443  return (*setFramePaddingV[version])(frame_ptr, padDwords);
444  else
445  return valueFailure;
446 }
447 
448 LOGIC_ret setDataType (FRAME_ptr frame_ptr, UINT dataType)
449 {
450  UINT version = getFrameHdrVersion(frame_ptr);
451  if (version != valueFailure)
452  return (*setDataTypeV[version])(frame_ptr, dataType);
453  else
454  return FALSE;
455 }
456 
457 LOGIC_ret setFrameType (FRAME_ptr frame_ptr, UINT frameType)
458 {
459  UINT version = getFrameHdrVersion(frame_ptr);
460  if (version != valueFailure)
461  return (*setFrameTypeV[version])(frame_ptr, frameType);
462  else
463  return FALSE;
464 }
465 
466 LOGIC_ret setSourceId (FRAME_ptr frame_ptr, UINT sourceId)
467 {
468  UINT version = getFrameHdrVersion(frame_ptr);
469  if (version != valueFailure)
470  return (*setSourceIdV[version])(frame_ptr, sourceId);
471  else
472  return FALSE;
473 }
474 
476 {
477  UINT version = getFrameHdrVersion(frame_ptr);
478  if (version != valueFailure)
479  return (*setFrameHistoryLengthV[version])(frame_ptr, historyLength);
480  else
481  return FALSE;
482 }
483 
484 LOGIC_ret setFrameErrorLength (FRAME_ptr frame_ptr, UINT errorLength)
485 {
486  UINT version = getFrameHdrVersion(frame_ptr);
487  if (version != valueFailure)
488  return (*setFrameErrorLengthV[version])(frame_ptr, errorLength);
489  else
490  return FALSE;
491 }
492 
493 LOGIC_ret setFrameAlignLength (FRAME_ptr frame_ptr, UINT alignLength)
494 {
495  UINT version = getFrameHdrVersion(frame_ptr);
496  if (version != valueFailure)
497  return (*setFrameAlignLengthV[version])(frame_ptr, alignLength);
498  else
499  return FALSE;
500 }
501 
503 {
504  UINT version = getFrameHdrVersion(frame_ptr);
505  if (version != valueFailure)
506  return (*setFrameStatusV[version])(frame_ptr, status);
507  else
508  return FALSE;
509 }
510 
511 VALUE_ret getAlignBlock (FRAME_ptr frame_ptr, PHDWORD* alignDestination, UINT maxNumDwords)
512 {
513  UINT version = getFrameHdrVersion(frame_ptr);
514  if (version != valueFailure)
515  return (*getAlignBlockV[version])(frame_ptr, alignDestination, maxNumDwords);
516  else
517  return valueFailure;
518 }
519 
520 LOGIC_ret setAlignBlock (FRAME_ptr frame_ptr,PHDWORD* alignSource, UINT numDwords)
521 {
522  UINT version = getFrameHdrVersion(frame_ptr);
523  if (version != valueFailure)
524  return (*setAlignBlockV[version])(frame_ptr, alignSource, numDwords);
525  else
526  return FALSE;
527 }
528 
530 {
531  UINT version = getFrameHdrVersion(frame_ptr);
532  if (version != valueFailure)
533  return (*getHistoryEntryV[version])(frame_ptr, index);
534  else
535  return valueFailure;
536 }
537 
539 {
540  UINT version = getFrameHdrVersion(frame_ptr);
541  if (version != valueFailure)
542  return (*getHistoryStageV[version])(frame_ptr, index);
543  else
544  return valueFailure;
545 }
546 
548 {
549  UINT version = getFrameHdrVersion(frame_ptr);
550  if (version != valueFailure)
551  return (*getHistorySourceIndexV[version])(frame_ptr, index);
552  else
553  return valueFailure;
554 }
555 
557 {
558  UINT version = getFrameHdrVersion(frame_ptr);
559  if (version != valueFailure)
560  return (*getHistoryStatusV[version])(frame_ptr, index);
561  else
562  return valueFailure;
563 }
564 
565 PTR_ret findNextError (FRAME_ptr frame_ptr, FRAME_ptr thisError)
566 {
567  UINT version = getFrameHdrVersion(frame_ptr);
568  if (version != valueFailure)
569  return (*findNextErrorV[version])(frame_ptr, thisError);
570  else
571  return ptrFailure;
572 }
573 
574 #endif /* end of ifndef DCM */
575 
576 /* ====================================================== */
577 /* Here ends the code for the version-DEPENDENT functions */
578 
579 
580 
581 
582 
583 
584 
585 
586 
587 
588 
589