Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:41

0001 /* 
0002 ** packetRoutines.C
0003 ** 
0004 ** Author: $Author: pinkenbu $  
0005 **   Date: $Date: 2004/10/19 19:21:05 $ 
0006 ** 
0007 ** $Log: packetRoutines.C,v $
0008 ** Revision 1.4  2004/10/19 19:21:05  pinkenbu
0009 ** fix insure warnings
0010 **
0011 ** Revision 1.3  2004/07/15 13:06:07  phoncs
0012 ** DLW: add extendPacketDataBlock call to extendUnstructDataBlock
0013 **
0014 ** Revision 1.8  2001/03/12 17:13:00  kelly
0015 ** committed packetRoutines bugfix
0016 **
0017 ** Revision 1.7  1998/12/16 15:40:52  markacs
0018 ** (stephen markacs) changes to rawFormat code to stop using bool,true,false
0019 **
0020 ** Revision 1.6  1998/12/11 22:02:06  markacs
0021 ** (stephen markacs) adding log into cvs tags
0022 ** 
0023 */
0024 /*
0025 **  Contains routines which manipulate packets in a fixed-length contiguous
0026 **    array in memory. Uses generic routines in Cpacket to extract fields
0027 **    from the packet headersand uses routines in dataBlock to maintain the
0028 **    relevant fields in the data block descriptor.
0029 */
0030 
0031 #ifndef VXWORKS
0032 #include "malloc.h"
0033 #include <stdlib.h>
0034 #endif
0035 
0036 #include "phenixOnline.h"
0037 #include "packetPublic.h"
0038 #include "Cpacket.h"
0039 #include "dataBlock.h"
0040 #include "packetRoutines.h"
0041 
0042 /*
0043 ** makeEmptyPacket
0044 **
0045 **   Routine to make a new packet header in a buffer pointed to by "newPacketPtr". 
0046 **   The header is created with "empty" data, debug and error blocks.
0047 **
0048 **  A pointer to the PHDWORD immediately following the "packet" (the header) is returned
0049 */
0050 
0051 PTR_ret makeEmptyPacket (PACKET_ptr packet_ptr, UINT maxPacketLen, UINT packetId)
0052 {   
0053   UINT packetLength = makePacketHdr (packet_ptr, maxPacketLen);
0054   if (packetLength == valueFailure) return ptrFailure;
0055  
0056   /*
0057   **    Now set user-specified fields
0058   */
0059   setPacketId(packet_ptr, packetId);
0060 
0061   /*
0062   ** Success
0063   */
0064   return findPacketEnd(packet_ptr)+1;
0065 }
0066 
0067 /*
0068 ** setPacketUnstructured
0069 */
0070 LOGIC_ret setPacketUnstructured (PACKET_ptr packet_ptr, UINT inWordSize,  
0071                  UINT inHitFormat)
0072 {
0073   /*
0074   **  Check for valid input
0075   */
0076   if (inWordSize > 4 ) return logicFailure;
0077 
0078   /*
0079   **  Set the packet structure field
0080   */
0081   setPacketStructure(packet_ptr, Unstructured);
0082 
0083   /*
0084   **  Make the data descriptor.
0085   */
0086 
0087   makeUnstructPacketDataDescr (packet_ptr, inWordSize,  inHitFormat);
0088 
0089   return logicSuccess;
0090 }
0091 
0092 /*
0093 **  Make an unstructured empty packet. a pointer to the PHDWORD following the empty
0094 **  packet is returned
0095 */
0096 PTR_ret makeUnstructPacket (PACKET_ptr packet_ptr, UINT maxPacketLength,  
0097                 UINT packetId, UINT inWordSize,  UINT inHitFormat)
0098 {
0099   VALUE_ret packetLength;
0100   LOGIC_ret logicalResult;
0101 
0102   packetLength = makePacketHdr (packet_ptr, maxPacketLength);
0103   if (packetLength == valueFailure) return ptrFailure;
0104         
0105   /*
0106   **  Now set user-specified fields
0107   */
0108   setPacketId(packet_ptr, packetId);
0109   setPacketStructure(packet_ptr, Unstructured);
0110 
0111   logicalResult = makeUnstructPacketDataDescr(packet_ptr, inWordSize, inHitFormat);
0112   if (!logicalResult) return ptrFailure;
0113   else return findPacketEnd(packet_ptr)+1;
0114 }
0115 
0116 /*
0117 **  Store a new error entry in the packet. Return the length of the 
0118 **    resulting error block
0119 */
0120 LOGIC_ret appendPacketError (PACKET_ptr packet_ptr,  UINT maxPacketLength, 
0121                  ERRORENTRYV1_ptr errorEntry_ptr)
0122 {
0123   PHDWORD newLength;
0124   PHDWORD* newError_ptr = findPacketErrorStart(packet_ptr) + 
0125     getPacketErrorLength(packet_ptr);
0126 
0127   /*
0128   ** Extend the length of the error block (and the packet)
0129   */
0130   newLength = extendPacketErrorBlock(packet_ptr, maxPacketLength, 
0131                      errorEntryV1Length);
0132   if (newLength == valueFailure) return logicFailure;
0133 
0134   /*
0135   **  Now copy in the error entry
0136   */
0137   dwordCopy(newError_ptr, (PHDWORD*) errorEntry_ptr, errorEntryV1Length);
0138   return logicSuccess;
0139 }
0140 
0141 /*
0142 **  Reserve space for numDwords in the packet debug block. Return a pointer to the 
0143 **  start of the debug block. The assumption is that the user will completely
0144 **  fill the requested space.
0145 */
0146 PTR_ret reservePacketDebugData (PACKET_ptr packet_ptr, UINT maxPacketLength, 
0147                 UINT numDwords)
0148 {
0149   PHDWORD newLength;
0150   
0151   /*
0152   **  We can only do this when there's no error block
0153   */
0154   if (getPacketErrorLength(packet_ptr) > 0) {
0155     setPacketError(FORMAT_ERR_INVALID_APPEND,packet_ptr,0);
0156   }
0157 
0158   /*
0159   **  Extend the length of the debug block.
0160   */
0161   newLength = extendPacketDebugBlock(packet_ptr, maxPacketLength, numDwords);
0162   if (newLength == valueFailure) return ptrFailure;
0163   else return findPacketDebugStart(packet_ptr);
0164 }
0165 
0166 /*
0167 **  Initiate a write operation into a pristine unstructured packet.
0168 **
0169 **    The packet must be initially empty. It is extended to allow space
0170 **    fir maxNumWords of unstructured data. A pointer to the start of
0171 **    the packet is returned.
0172 */
0173 PTR_ret startUnstructDataWrite (PACKET_ptr packet_ptr,  UINT maxPacketLength, 
0174                 PHDWORD maxNumWords)
0175 {
0176   PHDWORD* write_ptr;
0177   PHDWORD dataBlockLength;
0178  
0179   /*
0180   **  
0181   **    in the packet. Also make sure there's no debug or error blocks yet because
0182   **    we assume that these will be written after the data
0183   */
0184   if (!emptyPacket(packet_ptr)) {
0185     setUserError(FORMAT_ERR_INVALID_APPEND, getPacketLength(packet_ptr));
0186     return ptrFailure;
0187   }
0188 
0189   /*
0190   **  Check for unstructured packet.
0191   */
0192   if (getPacketStructure(packet_ptr) != Unstructured) {
0193     setUserError (FORMAT_ERR_WRONG_STRUCTURE, getPacketStructure(packet_ptr));
0194     return ptrFailure;
0195   }
0196 
0197   /*
0198   **  Try to extend the unstructured data block
0199   */
0200   dataBlockLength = extendUnstructPacketDataBlock(packet_ptr, maxNumWords);
0201   if (dataBlockLength == valueFailure) return ptrFailure;
0202 
0203   /*
0204   ** Now update the packet length
0205   */
0206   extendPacketDataBlock(packet_ptr, maxPacketLength, dataBlockLength);
0207 
0208   write_ptr = findPacketDataStart (packet_ptr);
0209 
0210   /*
0211   **  Now return to the user the pointer to where data is to be written
0212   */
0213   return write_ptr;
0214 }
0215 
0216 /*
0217 **  Finish writing unstructured block
0218 */
0219 PTR_ret finishUnstructDataWrite (PACKET_ptr packet_ptr, UINT maxPacketLength, 
0220                    PHDWORD actualWords)
0221 {
0222   PHDWORD deltaWords;
0223   PHDWORD newLength;
0224   PHDWORD maxNumWords = getUnstructPacketDataLengthWords(packet_ptr);
0225 
0226 
0227   /*
0228   **  Check to make sure that user hasn't written beyond the pre-set maximum
0229   */
0230   if (actualWords > maxNumWords) {
0231     /*
0232     **  This is a serious error, data will be corrupted
0233     */
0234     return ptrFailure;
0235   }
0236 
0237   /*
0238   ** Now update the length
0239   */
0240   deltaWords = maxNumWords - actualWords;
0241   newLength = extendUnstructPacketDataBlock(packet_ptr, -((int) deltaWords) );
0242   if (newLength == valueFailure) return ptrFailure;
0243 
0244   return findPacketEnd(packet_ptr)+1;
0245 }
0246 
0247 /*
0248 **  storePacketHits
0249 **
0250 **    General routine to store data in a packet. Works for unstructured and (hopefully)
0251 **    all structured packets. Currently only implementationis for unstructured packets.
0252 **
0253 **    For unstructured data, numHits = number of words of size "wordSize"
0254 **                           data_arr should point to the input data.
0255 **                           address_arr is ignored.
0256 **
0257 */
0258 VALUE_ret storePacketHits (PACKET_ptr packet_ptr,  UINT maxPacketLen, 
0259                UINT* address_arr, BYTE* data_arr,  
0260                UINT numHits,  UINT maxNumHits) 
0261 {
0262   /*
0263   **  Check to make sure packet has valid header and is empty
0264   */
0265   if (!validPacketHdr(packet_ptr))
0266   {  
0267     setPacketError(FORMAT_ERR_INVALID_HEADER,packet_ptr,0);
0268     return valueFailure;
0269   }
0270   if (!emptyPacket(packet_ptr))
0271   {
0272     setPacketError(FORMAT_ERR_NONEMPTY_PACKET,packet_ptr,0);
0273     return valueFailure;
0274   }
0275 
0276   /*
0277   **  Handle different structure types
0278   */
0279   switch (getPacketStructure(packet_ptr)) {
0280   case Unstructured: {
0281     PHDWORD newLength, result;
0282 
0283     /*
0284     **  Get the size of the data block.
0285     */
0286     newLength = extendUnstructPacketDataBlock(packet_ptr, numHits);
0287     
0288     result = extendPacketDataBlock(packet_ptr, maxPacketLen, newLength);
0289     if (result == valueFailure) return valueFailure;
0290     else {
0291       BYTE *data_ptr;
0292       PHDWORD numBytes = numHits*getUnstructPacketWordSize(packet_ptr);
0293 
0294       /*
0295       **  Now copy the data in.
0296       */ 
0297       data_ptr = (BYTE*) findPacketDataStart(packet_ptr);
0298       byteCopy (data_ptr, data_arr, numBytes);
0299       data_ptr += numBytes;
0300       byteClear (data_ptr, getUnstructPacketDataPadding(packet_ptr));
0301 
0302       return getPacketLength(packet_ptr);
0303     }
0304     break;
0305   }
0306   default:
0307     setPacketError(FORMAT_ERR_WRONG_STRUCTURE,packet_ptr,0);
0308     return valueFailure;
0309   }
0310   
0311   /*
0312   ** Success
0313   */
0314   return getPacketLength(packet_ptr);
0315 }
0316 
0317 /*
0318 **  fetchPacketHits
0319 */
0320 VALUE_ret fetchPacketHits (PACKET_ptr packet_ptr, UINT** address_arr, BYTE** hits_arr, UINT* hitLength) 
0321 {
0322   /*
0323   **  Make sure we got a pointer to a real packet
0324   */
0325   if (!validPacketHdr (packet_ptr))
0326     return valueFailure;
0327   
0328   /*
0329   **  Handle different structure types
0330   */
0331   switch (getPacketStructure (packet_ptr)) {
0332   case Unstructured:
0333     {
0334       PHDWORD numWords = getUnstructPacketDataLengthWords(packet_ptr);
0335       UINT wordSize = getUnstructPacketWordSize(packet_ptr);
0336       PHDWORD* unformBlock_ptr = findPacketDataStart (packet_ptr);
0337       PHDWORD numBytes = numWords*wordSize;
0338       
0339       /*
0340       **    Allocate space for data array
0341       */
0342       BYTE* hits_ptr    = (BYTE *) malloc (numBytes);
0343       byteCopy (hits_ptr, (BYTE *) unformBlock_ptr, numBytes);
0344       
0345       /*
0346       **  Alllocate space for one address (0)
0347       */
0348       *address_arr = (UINT*) malloc (sizeof(UINT));
0349       **address_arr = 0;
0350       
0351       *hits_arr = hits_ptr;
0352       *hitLength = wordSize;
0353       return numWords;
0354     }
0355 }
0356   
0357   /*
0358   ** Success
0359   */
0360   
0361   return 0;
0362 }
0363 
0364 
0365 
0366 
0367 
0368 
0369 
0370 
0371 
0372 
0373 
0374 
0375 
0376 
0377 
0378 
0379