ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
framePackets.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file framePackets.C
1 /*
2 ** framePackets.C
3 **
4 ** Author: $Author: phnxbld $
5 ** Date: $Date: 2009/08/19 12:30:51 $
6 **
7 ** $Log: framePackets.C,v $
8 ** Revision 1.3 2009/08/19 12:30:51 phnxbld
9 ** fix check on failure
10 **
11 ** Revision 1.2 2002/05/31 22:15:42 purschke
12 ** mlp -- went through the insure report and tried to fix
13 ** every little problem there is, unused variables, dead statements,
14 ** all. It'll probably take another round to complete, but it should get
15 ** rid of most warnings.
16 **
17 ** Revision 1.1.1.1 2000/07/21 01:51:13 purschke
18 ** mlp -- adding the new automakified "basic" module to CVS.
19 **
20 **
21 ** Revision 1.6 1998/12/23 20:34:57 markacs
22 ** fixed findFramePacketId so it returns ptrFailure if called for an index of 0 in the case that there are no packets in the frame
23 **
24 ** Revision 1.5 1998/12/11 22:02:04 markacs
25 ** (stephen markacs) adding log into cvs tags
26 **
27 */
28 /*
29 ** Routines that perform functions on packets with knowledge of the
30 ** frame that the packets reside in.
31 **
32 **/
33 
34 #include "framePackets.h"
35 #include "Cframe.h"
36 #include "Cpacket.h"
37 #include "packetPublic.h"
38 #include "framePublic.h"
39 #include "frameRoutines.h"
40 #include "packetRoutines.h"
41 #include "formatError.h"
42 
43 
44 PACKET_ptr appendEmptyFramePacket (FRAME_ptr frame_ptr, PHDWORD maxFrameLength, UINT packetId)
45 {
46  UINT extendDwords;
47  UINT maxPacketLength;
48  PACKET_ptr newPacket_ptr;
49 
50  /*
51  ** Check for valid frame header
52  */
53  if (!validFrameHdr (frame_ptr))
54  return ptrFailure;
55 
56  /*
57  ** Now extend the frame to hold the new packet header
58  */
59  extendDwords = extendFrameData(frame_ptr, maxFrameLength, packetV1HdrLength);
60  if (extendDwords == valueFailure) {
62  return ptrFailure;
63  }
64 
65  /*
66  ** Get pointer to data section
67  */
68  newPacket_ptr = findFrameDataStart(frame_ptr);
69 
70  /*
71  ** Now make the empty packet
72  */
73  maxPacketLength = maxFrameLength - (UINT) (newPacket_ptr - frame_ptr);
74  makeEmptyPacket(newPacket_ptr, maxPacketLength, packetId);
75 
77  return newPacket_ptr;
78 }
79 
81 {
82  PACKET_ptr lastPacket_ptr;
83 
84  /*
85  ** Check for valid frame, packet
86  */
87  if (!validFrameHdr (frame_ptr)) return FALSE;
88  if (!validPacketHdr (frame_ptr)) return FALSE;
89 
90  /*
91  ** Now find the last packet
92  */
93  lastPacket_ptr = findLastFramePacket (frame_ptr);
94 
95  return (lastPacket_ptr == packet_ptr);
96 
97 }
98 
99 /*
100 ** Return the index'th packet in the frame where the indexing starts from 0.
101 ** (i.e. findFramePacketIndex(frame_ptr, 0) returns the first packet)
102 */
104 {
105  PACKET_ptr testPacket_ptr;
106 
107  /*
108  ** Check first for valid frame header
109  */
110  if (!validFrameHdr (frame_ptr)) {
111  return ptrFailure;
112  }
113 
114  /*
115  ** Point to first packet
116  */
117  testPacket_ptr = (PACKET_ptr) findFrameDataStart (frame_ptr);
118 
119  if (index > 0) {
120  UINT iPacket;
121 
122  /*
123  ** Loop the necessary number of times but check to make sure we have valid pointer
124  */
125  for (iPacket=0; iPacket<index ; iPacket++) {
126  testPacket_ptr = findNextFramePacket (frame_ptr, testPacket_ptr);
127  if (testPacket_ptr == ptrFailure) {
128  return ptrFailure;
129  }
130  }
131  }
132  else /* if index=0 make sure there's a packet there */
133  {
134  if (!validPacketHdr(testPacket_ptr)) return ptrFailure;
135  }
136 
137  /*
138  ** If we get here we have the requested packet pointer
139  */
140  return testPacket_ptr;
141 }
142 
143 /*
144 ** Return a pointer to the first packet in the frame
145 */
147 {
148  return findFramePacketIndex(frame_ptr, 0);
149 }
150 
151 /*
152 ** Return a pointer to the first packet in the frame
153 */
155 {
156  PACKET_ptr thisPacket_ptr, nextPacket_ptr;
157 
158  /*
159  ** Find the first packet
160  */
161  if (!(nextPacket_ptr = findFirstFramePacket (frame_ptr))) return ptrFailure;
162 
163  /*
164  ** Now iterate to the end
165  */
166  while (nextPacket_ptr != ptrFailure) {
167  thisPacket_ptr = nextPacket_ptr;
168  nextPacket_ptr = findNextFramePacket (frame_ptr, nextPacket_ptr);
169  }
170 
171  return thisPacket_ptr;
172 }
173 
174 /*
175 ** findFramePacketID
176 **
177 ** Find a packet with a specified packet ID in a frame starting at the
178 ** packet in the frame. If no packet with the ID is found ptrFailure is returned.
179 **
180 */
182 {
183  PACKET_ptr testPacket_ptr;
184 
185  /*
186  ** Check to make sure we have a good frame
187  */
188  if (!validFrameHdr (frame_ptr)) {
189  return ptrFailure;
190  }
191 
192  /*
193  ** Point to first packet
194  */
195  testPacket_ptr = (PACKET_ptr) findFrameDataStart (frame_ptr);
196 
197  /*
198  ** Now iterate until we fail to get enxt packet or find the one we want
199  */
200  while ( (testPacket_ptr != ptrFailure) && (getPacketId(testPacket_ptr) != packetId) )
201  testPacket_ptr = findNextFramePacket (frame_ptr, testPacket_ptr);
202 
203  return testPacket_ptr;
204 }
205 
206 
207 PACKET_ptr findNextFramePacketId (FRAME_ptr frame_ptr, PACKET_ptr previousPacket_ptr, UINT packetId)
208 {
209  /*
210  ** BEWARE !!! No check on valid frame once we're past the first packet.
211  */
212 
213  /*
214 ** Skip the "previous" packet
215 */
216  PACKET_ptr testPacket_ptr = findNextFramePacket(frame_ptr, previousPacket_ptr);
217 
218  /*
219  ** Now iterate until we fail to get enxt packet or find the one we want
220  */
221  while ( (testPacket_ptr != ptrFailure) && (getPacketId(testPacket_ptr) != packetId) )
222  testPacket_ptr = findNextFramePacket (frame_ptr, testPacket_ptr);
223 
224  return testPacket_ptr;
225 }
226 
227 /*
228 ** Private routine to search for a packet in a unbroken data block of packets
229 */
230 PACKET_ptr findNextFramePacket (FRAME_ptr frame_ptr, PACKET_ptr currentPacket_ptr)
231 {
232  PACKET_ptr nextPacket_ptr;
233 
234  /*
235  ** Figure out where the frame ends
236  */
237  PHDWORD* frameDataStart_ptr = findFrameDataStart(frame_ptr);
238  PHDWORD* frameDataEnd_ptr = findFrameDataEnd(frame_ptr);
239 
240  /*
241  ** Check for valid current packet pointer
242  */
243  if ((currentPacket_ptr < frameDataStart_ptr) || (currentPacket_ptr > frameDataEnd_ptr)) {
244  return ptrFailure;
245  }
246 
247  /*
248  ** Now skip past "current" packet
249  */
250  nextPacket_ptr = findPacketEnd(currentPacket_ptr) + 1;
251 
252  if (nextPacket_ptr > frameDataEnd_ptr) return ptrFailure;
253  else return nextPacket_ptr;
254 }
255 
256 
257 /*
258 ** routine to find the number of packets in a frame
259 */
261 {
262  UINT numPackets = 1;
263  PACKET_ptr packet_ptr = findFirstFramePacket(frame_ptr);
264  if (packet_ptr == ptrFailure) return 0;
265  while ( (packet_ptr = findNextFramePacket(frame_ptr,packet_ptr)) != ptrFailure )
266  numPackets++;
267  return numPackets;
268 }