ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Cpacket.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Cpacket.C
1 /*
2 ** Cpacket.C
3 **
4 ** Author: $Author: phoncs $
5 ** Date: $Date: 2004/02/14 00:17:03 $
6 **
7 ** $Log: Cpacket.C,v $
8 ** Revision 1.2 2004/02/14 00:17:03 phoncs
9 ** J.J change by brian to fix the padding problem
10 **
11 ** Revision 1.1.1.1 2000/07/21 01:51:10 purschke
12 ** mlp -- adding the new automakified "basic" module to CVS.
13 **
14 **
15 ** Revision 1.6 1998/12/17 15:18:16 phoncs
16 ** (stephen markacs) more bounds checking in Cpacket.C (one instance) and removal of debugging comments from checkFrame.C
17 **
18 ** Revision 1.5 1998/12/11 22:01:59 markacs
19 ** (stephen markacs) adding log into cvs tags
20 **
21 */
22 /*
23 ** These are the version independent functions which
24 ** provide access to and mocification of information
25 ** in the header, but which are not inlined.
26 **
27 */
28 
29 #include "phenixOnline.h"
30 #include "packetPublic.h"
31 #include "packetHdr.h"
32 #include "Cpacket.h"
33 #include "CpacketV1.h"
34 
36 {
37 #ifdef STRONGCHECK
38  if (!validPacketHdr(packet_ptr)) {
39  setPacketError(FORMAT_ERR_INVALID_HDR, packet_ptr, 0);
40  return logicFailure;
41  }
42 #endif
43 
44  PHDWORD packetLength = getPacketLength (packet_ptr);
45  if (packetLength != valueFailure) {
47  return ((PHDWORD *) packet_ptr + packetLength - 1);
48  }
49  else
50  return ptrFailure;
51 }
52 
54 {
55  if (validPacketHdr(packet_ptr))
56  return ((PHDWORD*)packet_ptr + getPacketHdrLength(packet_ptr) +
57  getPacketDataDescrLength(packet_ptr) - 1);
58  else
59  return ptrFailure;
60 }
61 
62 /*
63 ** Adjust the length of the data block in the packet.
64 **
65 ** Note: a negative extension is allowed
66 */
67 VALUE_ret extendPacketDataBlock(const PACKET_ptr packet_ptr, UINT maxPacketLength,
68  int addDwords)
69 {
70  /*
71  ** Extend the packet itself
72  */
73  PHDWORD newLength = extendPacketLength (packet_ptr, maxPacketLength, addDwords);
74 
75  /*
76  ** Update the length of the data block itself (NOOP for V1)
77  */
78  if (newLength != valueFailure) {
79  return adjustPacketDataLength(packet_ptr, addDwords);
80  }
81  else
82  return valueFailure;
83 }
84 
85 /*
86 ** Adjust the length of the debug block in the packet.
87 **
88 ** Note: a negative extension is allowed
89 */
90 VALUE_ret extendPacketDebugBlock(const PACKET_ptr packet_ptr, UINT maxPacketLength,
91  int addDwords)
92 {
93  /*
94  ** Extend the packet itself
95  */
96  PHDWORD newLength = extendPacketLength (packet_ptr, maxPacketLength, addDwords);
97 
98  /*
99  ** Update the length of the debug blcok itself (NOOP) for V1
100  */
101  if (newLength != valueFailure)
102  return adjustPacketDebugLength(packet_ptr, addDwords);
103  else
104  return valueFailure;
105 }
106 
107 
108 /*
109 ** Adjust the length of the error block in the packet.
110 **
111 ** Note: a negative extension is allowed
112 */
113 VALUE_ret extendPacketErrorBlock(const PACKET_ptr packet_ptr, UINT maxPacketLength,
114  int addDwords)
115 {
116  /*
117  ** Extend the packet itself
118  */
119  PHDWORD newLength = extendPacketLength (packet_ptr, maxPacketLength, addDwords);
120 
121  /*
122  ** Update the length of the debug blcok itself (NOOP) for V1
123  */
124  if (newLength != valueFailure)
125  return adjustPacketErrorLength(packet_ptr, addDwords);
126  else
127  return valueFailure;
128 }
129 
130 
131 /*
132 ** Adjust the length of the packet and update the padding count.
133 **
134 ** Note: a negative extension is allowed
135 */
136 VALUE_ret extendPacketLength(const PACKET_ptr packet_ptr, UINT maxPacketLength,
137  int addDwords)
138 {
139  UINT nPadDwords;
140  PHDWORD newLength;
141 
142  /*
143  ** Drop any existing padding.
144  */
145  if (!removePacketPadding(packet_ptr)) {
146  return valueFailure;
147  }
148 
149  /*
150  ** Check for buffer overflow
151  */
152  newLength = getPacketLength(packet_ptr) + addDwords;
153 
154  if (newLength > maxPacketLength) {
155  setPacketError(FORMAT_ERR_BUFFER_OVERFLOW,packet_ptr,newLength);
156  return valueFailure;
157  }
158 
159  {
160  /*
161  ** Now calculate the padding
162  */
163  UINT modulo = newLength % packetQuantum;
164  if (modulo > 0) nPadDwords = packetQuantum - modulo;
165  else nPadDwords = 0;
166  }
167 
168  newLength += nPadDwords;
169 
170  /*
171  ** Store the number of padding words in the header
172  */
173  setPacketPadding(packet_ptr, nPadDwords);
174 
175  /*
176  ** Update the packet length
177  */
178  setPacketLength(packet_ptr, newLength);
179 
180  return newLength;
181 }
182 
184 {
185  /*
186  ** Get the current number of padding dwords
187  */
188  UINT nPadDwords = getPacketPadding(packet_ptr);
189 
190  /*
191  ** decrease the packet length by this amount
192  */
193  if (nPadDwords > 0) {
194  PHDWORD newPacketLength = getPacketLength(packet_ptr) - nPadDwords;
195 
196  /*
197  ** Check for reasonable result
198  */
199  if (newPacketLength < packetV1HdrLength) {
200  /*
201  ** Oops, there's a problem. Set error result and return failure.
202  */
204  return logicFailure;
205  }
206 
207  /*
208  ** Update the header length
209  */
210  setPacketLength(packet_ptr, newPacketLength);
211  setPacketPadding(packet_ptr, 0);
212  }
213 
214  return logicSuccess;
215 }
216 
217 
218 #ifndef DCM
219 /*
220 ** Define indirection pointer arrays to various frame access functions
221 ** To avoid conflict with frame indirection pointer arrays, a "P" has
222 ** been placed in each definition before the word "FUNCTION".
223 */
230 
231 
238 
239 #ifdef __cplusplus
240 ACCESSPFUNC* const NULLACCESSPFUNC = 0;
242 CHECKPFUNC* const NULLCHECKPFUNC = 0;
243 MODIFYPFUNC* const NULLMODIFYPFUNC = 0;
244 ADJUSTPFUNC* const NULLADJUSTPFUNC = 0;
245 ORPFUNC* const NULLORPFUNC = 0;
246 #else
247 /*
248 ** Unfortunately for some C compilers we have to lose the benefits of
249 ** type-checking because const's are not considered valid initializers
250 */
251 #define NULLACCESSPFUNC 0
252 #define NULLPTRACCESSPFUNC 0
253 #define NULLCHECKPFUNC 0
254 #define NULLMODIFYPFUNC 0
255 #define NULLADJUSTPFUNC 0
256 #define NULLADJUSTDATAPFUNC 0
257 #define NULLORPFUNC 0
258 #endif
259 
260 
271 
279 
281 
285 
292 
297 //PTRACCESSPFUNCPTR_arr CONSTANT findPacketDataStartV = {NULLPTRACCESSPFUNC, &findPacketV1DataStart};
301 
304 
305 /*
306 ** "indirection" routines.
307 **
308 */
309 
310 VALUE_ret makePacketHdr (const PACKET_ptr packet_ptr, UINT maxPacketLength)
311 {
313  if (version != valueFailure)
314  return (*makePacketHdrV[version])(packet_ptr, maxPacketLength);
315  else
316  return valueFailure;
317 }
318 
319 
321 {
322  UINT version = getPacketHdrVersion(packet_ptr);
323  if (version != valueFailure)
324  return (*getPacketStatusV[version])(packet_ptr);
325  else
326  return valueFailure;
327 }
328 
330 {
331  UINT version = getPacketHdrVersion(packet_ptr);
332  if (version != valueFailure)
333  return (*orPacketStatusV[version])(packet_ptr, inBits);
334  else
335  return valueFailure;
336 }
337 
339 {
340  UINT version = getPacketHdrVersion (packet_ptr);
341  if (version != valueFailure)
342  return getPacketDataLengthV[version](packet_ptr);
343  else
344 
345  return valueFailure;
346 }
347 
349 {
350  UINT version = getPacketHdrVersion (packet_ptr);
351  if (version != valueFailure)
352  return getPacketErrorLengthV[version](packet_ptr);
353  else
354 
355  return valueFailure;
356 }
357 
359 {
360  UINT version = getPacketHdrVersion (packet_ptr);
361  if (version != valueFailure)
362  return getPacketDebugLengthV[version](packet_ptr);
363  else
364  return valueFailure;
365 }
366 
368 {
369  UINT version = getPacketHdrVersion (packet_ptr);
370  if (version != valueFailure)
371  return getPacketNumErrorsV[version](packet_ptr);
372  else
373  return valueFailure;
374 }
375 
376 VALUE_ret getPacketId (const PACKET_ptr packet_ptr)
377 {
378  UINT version = getPacketHdrVersion (packet_ptr);
379  if (version != valueFailure)
380  return getPacketIdV[version](packet_ptr);
381  else
382  return valueFailure;
383 }
384 
386 {
387  UINT version = getPacketHdrVersion (packet_ptr);
388  if (version != valueFailure)
389  return getPacketStructureV[version](packet_ptr);
390  else return valueFailure;
391 }
392 
394 {
395  UINT version = getPacketHdrVersion (packet_ptr);
396  if (version != valueFailure)
397  return getPacketEndianismV[version](packet_ptr);
398  else
399  return valueFailure;
400 }
401 
403 {
404  UINT version = getPacketHdrVersion (packet_ptr);
405  if (version != valueFailure)
406  return getPacketPaddingV[version](packet_ptr);
407  else
408  return valueFailure;
409 }
410 
412 {
413  UINT version = getPacketHdrVersion (packet_ptr);
414  if (version != valueFailure)
415  return getPacketDataDescrLengthV[version](packet_ptr);
416  else
417  return valueFailure;
418 }
419 
420 
422 {
423  UINT version = getPacketHdrVersion (packet_ptr);
424  if (version != valueFailure)
425  return setPacketDebugLengthV[version](packet_ptr, inLength);
426  else
427  return logicFailure;
428 }
429 
430 /*
431 ** This was commented out up top.
432 **
433 **
434 ** LOGIC_ret setPacketNumErrors(PACKET_ptr packet_ptr, UINT inLength)
435 ** {
436 ** UINT version = getPacketHdrVersion (packet_ptr);
437 ** if (version != valueFailure)
438 ** return setPacketNumErrorsV[version](packet_ptr, numErrors);
439 ** else
440 ** return logicFailure;
441 ** }
442 */
443 
445 {
446  UINT version = getPacketHdrVersion (packet_ptr);
447  if (version != valueFailure)
448  return setPacketIdV[version](packet_ptr, inId);
449  else
450  return valueFailure;
451 }
452 
454 {
455  UINT version = getPacketHdrVersion (packet_ptr);
456  if (version != valueFailure)
457  return setPacketStructureV[version](packet_ptr, structure);
458  else
459  return logicFailure;
460 }
461 
463 {
464  UINT version = getPacketHdrVersion (packet_ptr);
465  if (version != valueFailure)
466  return setPacketEndianismV[version](packet_ptr, endianism);
467  else
468  return logicFailure;
469 }
470 
472 {
473  UINT version = getPacketHdrVersion (packet_ptr);
474  if (version != valueFailure)
475  return setPacketPaddingV[version](packet_ptr, padding);
476  else
477  return logicFailure;
478 }
479 
481 {
482  UINT version = getPacketHdrVersion (packet_ptr);
483  if (version != valueFailure)
484  return setPacketStatusV[version](packet_ptr, status);
485  else
486  return logicFailure;
487 }
488 
490 {
491  UINT version = getPacketHdrVersion (packet_ptr);
492  if (version != valueFailure)
493  return setPacketErrorLengthV[version](packet_ptr, errorLength);
494  else
495  return logicFailure;
496 }
497 
499 {
500  UINT version = getPacketHdrVersion (packet_ptr);
501  if (version != valueFailure)
502  return setPacketDataDescrLengthV[version](packet_ptr, descrLength);
503  else
504  return logicFailure;
505 }
506 
508 {
509  UINT version = getPacketHdrVersion (packet_ptr);
510  if (version != valueFailure)
511  return findPacketDataEndV[version](packet_ptr);
512  else
513  return ptrFailure;
514 }
515 
517 {
518  UINT version = getPacketHdrVersion (packet_ptr);
519  if (version != valueFailure)
520  return findPacketDebugStartV[version](packet_ptr);
521  else
522  return ptrFailure;
523 }
524 
526 {
527  UINT version = getPacketHdrVersion (packet_ptr);
528  if (version != valueFailure)
529  return (*findPacketErrorStartV[version])(packet_ptr);
530  else
531  return ptrFailure;
532 }
533 
535 {
536  UINT version = getPacketHdrVersion (packet_ptr);
537  if (version != valueFailure)
538  return (*findPacketDataDescrV[version])(packet_ptr);
539  else
540  return ptrFailure;
541 }
542 
543 
545 {
546  UINT version = getPacketHdrVersion (packet_ptr);
547  if ( (version > 0) && (version < NUM_PACKET_VERSIONS) )
548  return validPacketHdrV[version](packet_ptr);
549  else
550  return logicFailure;
551 }
552 
554 {
555  UINT version = getPacketHdrVersion (packet_ptr);
556  if (version != valueFailure)
557  return emptyPacketV[version](packet_ptr);
558  else
559  return logicFailure;
560 }
561 
562 VALUE_ret adjustPacketDataLength(PACKET_ptr packet_ptr, int addDwords)
563 {
564  UINT version = getPacketHdrVersion (packet_ptr);
565  if (version != valueFailure)
566  return adjustPacketDataLengthV[version](packet_ptr, addDwords);
567  else
568  return valueFailure;
569 }
570 
571 VALUE_ret adjustPacketDebugLength(PACKET_ptr packet_ptr, int addLength)
572 {
573  UINT version = getPacketHdrVersion (packet_ptr);
574  if (version != valueFailure)
575  return adjustPacketDebugLengthV[version](packet_ptr, addLength);
576  else
577  return valueFailure;
578 }
579 
580 VALUE_ret adjustPacketErrorLength(PACKET_ptr packet_ptr, int addLength)
581 {
582  UINT version = getPacketHdrVersion (packet_ptr);
583  if (version != valueFailure)
584  return adjustPacketErrorLengthV[version](packet_ptr, addLength);
585  else
586  return valueFailure;
587 }
588 
589 #endif
590 
591 
592 
593 
594 
595 
596 
597 
598 
599