LTKCPP-- LLRP Toolkit C Plus Plus Library
ltkcpp_frameencode.cpp
00001 
00002 /*
00003  *****************************************************************************
00004  *                                                                           *
00005  *                 IMPINJ CONFIDENTIAL AND PROPRIETARY                       *
00006  *                                                                           *
00007  * This source code is the sole property of Impinj, Inc.  Reproduction or    *
00008  * utilization of this source code in whole or in part is forbidden without  *
00009  * the prior written consent of Impinj, Inc.                                 *
00010  *                                                                           *
00011  * (c) Copyright Impinj, Inc. 2007,2008. All rights reserved.                *
00012  *                                                                           *
00013  *****************************************************************************/
00014 
00015 
00016 
00017 #include <assert.h>
00018 
00019 #include "ltkcpp_platform.h"
00020 #include "ltkcpp_base.h"
00021 #include "ltkcpp_frame.h"
00022 
00023 
00024 namespace LLRP
00025 {
00026 
00027 CFrameEncoder::CFrameEncoder (
00028   unsigned char *               pBuffer,
00029   unsigned int                  nBuffer)
00030  : CEncoder()
00031 {
00032     m_pBuffer        = pBuffer;
00033     m_nBuffer        = nBuffer;
00034 
00035     m_iNext          = 0;
00036     m_BitFieldBuffer = 0;
00037     m_nBitFieldResid = 0;
00038 }
00039 
00040 CFrameEncoder::~CFrameEncoder (void)
00041 {
00042 }
00043 
00044 void
00045 CFrameEncoder::encodeElement (
00046   const CElement *              pElement)
00047 {
00048     CFrameEncoderStream         MyEncoderStream(this);
00049 
00050     MyEncoderStream.putElement(pElement);
00051 }
00052 
00053 unsigned int
00054 CFrameEncoder::getLength(void)
00055 {
00056     return m_iNext;
00057 }
00058 
00059 void
00060 CFrameEncoderStream::putRequiredSubParameter (
00061   const CParameter *            pParameter,
00062   const CTypeDescriptor *       pRefType)
00063 {
00064     if(NULL == pParameter)
00065     {
00066         CErrorDetails *         pError = &m_pEncoder->m_ErrorDetails;
00067 
00068         pError->missingParameter(pRefType);
00069 
00070         return;
00071     }
00072 
00073     CFrameEncoderStream         NestEncoderStream(this);
00074 
00075     NestEncoderStream.putElement(pParameter);
00076 }
00077 
00078 void
00079 CFrameEncoderStream::putOptionalSubParameter (
00080   const CParameter *            pParameter,
00081   const CTypeDescriptor *       pRefType)
00082 {
00083     if(NULL == pParameter)
00084     {
00085         return;
00086     }
00087 
00088     CFrameEncoderStream         NestEncoderStream(this);
00089 
00090     NestEncoderStream.putElement(pParameter);
00091 }
00092 
00093 void
00094 CFrameEncoderStream::putRequiredSubParameterList (
00095   const tListOfParameters *     pParameterList,
00096   const CTypeDescriptor *       pRefType)
00097 {
00098     if(pParameterList->empty())
00099     {
00100         CErrorDetails *         pError = &m_pEncoder->m_ErrorDetails;
00101 
00102         pError->missingParameter(pRefType);
00103 
00104         return;
00105     }
00106 
00107     for(
00108         tListOfParameters::const_iterator Cur = pParameterList->begin();
00109         Cur != pParameterList->end();
00110         Cur++)
00111     {
00112         putRequiredSubParameter(*Cur, pRefType);
00113     }
00114 }
00115 
00116 void
00117 CFrameEncoderStream::putOptionalSubParameterList (
00118   const tListOfParameters *     pParameterList,
00119   const CTypeDescriptor *       pRefType)
00120 {
00121     for(
00122         tListOfParameters::const_iterator Cur = pParameterList->begin();
00123         Cur != pParameterList->end();
00124         Cur++)
00125     {
00126         putRequiredSubParameter(*Cur, pRefType);
00127     }
00128 }
00129 
00130 
00131 void
00132 CFrameEncoder::next_u8 (
00133   llrp_u8_t                     Value)
00134 {
00135     assert(m_iNext + 1u <= m_nBuffer);
00136 
00137     m_pBuffer[m_iNext++] = Value;
00138 }
00139 
00140 void
00141 CFrameEncoder::next_u16 (
00142   llrp_u16_t                    Value)
00143 {
00144     assert(m_iNext + 2u <= m_nBuffer);
00145 
00146     m_pBuffer[m_iNext++] = Value >> 8u;
00147     m_pBuffer[m_iNext++] = Value >> 0u;
00148 }
00149 
00150 void
00151 CFrameEncoder::next_u32 (
00152   llrp_u32_t                    Value)
00153 {
00154     assert(m_iNext + 4u <= m_nBuffer);
00155 
00156     m_pBuffer[m_iNext++] = Value >> 24u;
00157     m_pBuffer[m_iNext++] = Value >> 16u;
00158     m_pBuffer[m_iNext++] = Value >> 8u;
00159     m_pBuffer[m_iNext++] = Value >> 0u;
00160 }
00161 
00162 void
00163 CFrameEncoder::next_u64 (
00164   llrp_u64_t                    Value)
00165 {
00166     assert(m_iNext + 8u <= m_nBuffer);
00167 
00168     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 56u);
00169     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 48u);
00170     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 40u);
00171     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 32u);
00172     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 24u);
00173     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 16u);
00174     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 8u);
00175     m_pBuffer[m_iNext++] = (llrp_byte_t)(Value >> 0u);
00176 }
00177 
00178 /*
00179  * 8-bit types
00180  */
00181 
00182 void
00183 CFrameEncoderStream::put_u8 (
00184   llrp_u8_t                     Value,
00185   const CFieldDescriptor *      pFieldDescriptor)
00186 {
00187     if(checkAvailable(1u, pFieldDescriptor))
00188     {
00189         m_pEncoder->next_u8(Value);
00190     }
00191 }
00192 
00193 void
00194 CFrameEncoderStream::put_s8 (
00195   llrp_s8_t                     Value,
00196   const CFieldDescriptor *      pFieldDescriptor)
00197 {
00198     if(checkAvailable(1u, pFieldDescriptor))
00199     {
00200         m_pEncoder->next_u8(Value);
00201     }
00202 }
00203 
00204 void
00205 CFrameEncoderStream::put_u8v (
00206   llrp_u8v_t                    Value,
00207   const CFieldDescriptor *      pFieldDescriptor)
00208 {
00209     unsigned int                nByte;
00210 
00211     nByte = 2u + Value.m_nValue * 1u;
00212 
00213     if(checkAvailable(nByte, pFieldDescriptor))
00214     {
00215         unsigned int            i;
00216 
00217         m_pEncoder->next_u16(Value.m_nValue);
00218         for(i = 0; i < Value.m_nValue; i++)
00219         {
00220             m_pEncoder->next_u8(Value.m_pValue[i]);
00221         }
00222     }
00223 }
00224 
00225 void
00226 CFrameEncoderStream::put_s8v (
00227   llrp_s8v_t                    Value,
00228   const CFieldDescriptor *      pFieldDescriptor)
00229 {
00230     unsigned int                nByte;
00231 
00232     nByte = 2u + Value.m_nValue * 1u;
00233 
00234     if(checkAvailable(nByte, pFieldDescriptor))
00235     {
00236         unsigned int            i;
00237 
00238         m_pEncoder->next_u16(Value.m_nValue);
00239         for(i = 0; i < Value.m_nValue; i++)
00240         {
00241             m_pEncoder->next_u8(Value.m_pValue[i]);
00242         }
00243     }
00244 }
00245 
00246 /*
00247  * 16-bit types
00248  */
00249 
00250 void
00251 CFrameEncoderStream::put_u16 (
00252   llrp_u16_t                    Value,
00253   const CFieldDescriptor *      pFieldDescriptor)
00254 {
00255     if(checkAvailable(2u, pFieldDescriptor))
00256     {
00257         m_pEncoder->next_u16(Value);
00258     }
00259 }
00260 
00261 void
00262 CFrameEncoderStream::put_s16 (
00263   llrp_s16_t                    Value,
00264   const CFieldDescriptor *      pFieldDescriptor)
00265 {
00266     if(checkAvailable(2u, pFieldDescriptor))
00267     {
00268         m_pEncoder->next_u16(Value);
00269     }
00270 }
00271 
00272 void
00273 CFrameEncoderStream::put_u16v (
00274   llrp_u16v_t                   Value,
00275   const CFieldDescriptor *      pFieldDescriptor)
00276 {
00277     unsigned int                nByte;
00278 
00279     nByte = 2u + Value.m_nValue * 2u;
00280 
00281     if(checkAvailable(nByte, pFieldDescriptor))
00282     {
00283         unsigned int            i;
00284 
00285         m_pEncoder->next_u16(Value.m_nValue);
00286         for(i = 0; i < Value.m_nValue; i++)
00287         {
00288             m_pEncoder->next_u16(Value.m_pValue[i]);
00289         }
00290     }
00291 }
00292 
00293 void
00294 CFrameEncoderStream::put_s16v (
00295   llrp_s16v_t                   Value,
00296   const CFieldDescriptor *      pFieldDescriptor)
00297 {
00298     unsigned int                nByte;
00299 
00300     nByte = 2u + Value.m_nValue * 2u;
00301 
00302     if(checkAvailable(nByte, pFieldDescriptor))
00303     {
00304         unsigned int            i;
00305 
00306         m_pEncoder->next_u16(Value.m_nValue);
00307         for(i = 0; i < Value.m_nValue; i++)
00308         {
00309             m_pEncoder->next_u16(Value.m_pValue[i]);
00310         }
00311     }
00312 }
00313 
00314 /*
00315  * 32-bit types
00316  */
00317 
00318 void
00319 CFrameEncoderStream::put_u32 (
00320   llrp_u32_t                    Value,
00321   const CFieldDescriptor *      pFieldDescriptor)
00322 {
00323     if(checkAvailable(4u, pFieldDescriptor))
00324     {
00325         m_pEncoder->next_u32(Value);
00326     }
00327 }
00328 
00329 void
00330 CFrameEncoderStream::put_s32 (
00331   llrp_s32_t                    Value,
00332   const CFieldDescriptor *      pFieldDescriptor)
00333 {
00334     if(checkAvailable(4u, pFieldDescriptor))
00335     {
00336         m_pEncoder->next_u32(Value);
00337     }
00338 }
00339 
00340 void
00341 CFrameEncoderStream::put_u32v (
00342   llrp_u32v_t                   Value,
00343   const CFieldDescriptor *      pFieldDescriptor)
00344 {
00345     unsigned int                nByte;
00346 
00347     nByte = 2u + Value.m_nValue * 4u;
00348 
00349     if(checkAvailable(nByte, pFieldDescriptor))
00350     {
00351         unsigned int            i;
00352 
00353         m_pEncoder->next_u16(Value.m_nValue);
00354         for(i = 0; i < Value.m_nValue; i++)
00355         {
00356             m_pEncoder->next_u32(Value.m_pValue[i]);
00357         }
00358     }
00359 }
00360 
00361 void
00362 CFrameEncoderStream::put_s32v (
00363   llrp_s32v_t                   Value,
00364   const CFieldDescriptor *      pFieldDescriptor)
00365 {
00366     unsigned int                nByte;
00367 
00368     nByte = 2u + Value.m_nValue * 4u;
00369 
00370     if(checkAvailable(nByte, pFieldDescriptor))
00371     {
00372         unsigned int            i;
00373 
00374         m_pEncoder->next_u16(Value.m_nValue);
00375         for(i = 0; i < Value.m_nValue; i++)
00376         {
00377             m_pEncoder->next_u32(Value.m_pValue[i]);
00378         }
00379     }
00380 }
00381 
00382 /*
00383  * 64-bit types
00384  */
00385 
00386 void
00387 CFrameEncoderStream::put_u64 (
00388   llrp_u64_t                    Value,
00389   const CFieldDescriptor *      pFieldDescriptor)
00390 {
00391     if(checkAvailable(8u, pFieldDescriptor))
00392     {
00393         m_pEncoder->next_u64(Value);
00394     }
00395 }
00396 
00397 void
00398 CFrameEncoderStream::put_s64 (
00399   llrp_s64_t                    Value,
00400   const CFieldDescriptor *      pFieldDescriptor)
00401 {
00402     if(checkAvailable(8u, pFieldDescriptor))
00403     {
00404         m_pEncoder->next_u64(Value);
00405     }
00406 }
00407 
00408 void
00409 CFrameEncoderStream::put_u64v (
00410   llrp_u64v_t                   Value,
00411   const CFieldDescriptor *      pFieldDescriptor)
00412 {
00413     unsigned int                nByte;
00414 
00415     nByte = 2u + Value.m_nValue * 8u;
00416 
00417     if(checkAvailable(nByte, pFieldDescriptor))
00418     {
00419         unsigned int            i;
00420 
00421         m_pEncoder->next_u16(Value.m_nValue);
00422         for(i = 0; i < Value.m_nValue; i++)
00423         {
00424             m_pEncoder->next_u64(Value.m_pValue[i]);
00425         }
00426     }
00427 }
00428 
00429 void
00430 CFrameEncoderStream::put_s64v (
00431   llrp_s64v_t                   Value,
00432   const CFieldDescriptor *      pFieldDescriptor)
00433 {
00434     unsigned int                nByte;
00435 
00436     nByte = 2u + Value.m_nValue * 8u;
00437 
00438     if(checkAvailable(nByte, pFieldDescriptor))
00439     {
00440         unsigned int            i;
00441 
00442         m_pEncoder->next_u16(Value.m_nValue);
00443         for(i = 0; i < Value.m_nValue; i++)
00444         {
00445             m_pEncoder->next_u64(Value.m_pValue[i]);
00446         }
00447     }
00448 }
00449 
00450 /*
00451  * Special types
00452  */
00453 
00454 void
00455 CFrameEncoderStream::put_u1 (
00456   llrp_u1_t                     Value,
00457   const CFieldDescriptor *      pFieldDescriptor)
00458 {
00459     putBitField(1u, Value, pFieldDescriptor);
00460 }
00461 
00462 void
00463 CFrameEncoderStream::put_u1v (
00464   llrp_u1v_t                    Value,
00465   const CFieldDescriptor *      pFieldDescriptor)
00466 {
00467     unsigned int                nByte;
00468 
00469     nByte = (Value.m_nBit + 7u) / 8u;
00470 
00471     if(checkAvailable(2u + nByte, pFieldDescriptor))
00472     {
00473         unsigned int            i;
00474 
00475         m_pEncoder->next_u16(Value.m_nBit);
00476         for(i = 0; i < nByte; i++)
00477         {
00478             m_pEncoder->next_u8(Value.m_pValue[i]);
00479         }
00480     }
00481 }
00482 
00483 void
00484 CFrameEncoderStream::put_u2 (
00485   llrp_u2_t                     Value,
00486   const CFieldDescriptor *      pFieldDescriptor)
00487 {
00488     putBitField(2u, Value, pFieldDescriptor);
00489 }
00490 
00491 void
00492 CFrameEncoderStream::put_u96 (
00493   llrp_u96_t                    Value,
00494   const CFieldDescriptor *      pFieldDescriptor)
00495 {
00496     if(checkAvailable(12u, pFieldDescriptor))
00497     {
00498         unsigned int            i;
00499 
00500         for(i = 0; i < 12u; i++)
00501         {
00502             m_pEncoder->next_u8(Value.m_aValue[i]);
00503         }
00504     }
00505 }
00506 
00507 void
00508 CFrameEncoderStream::put_utf8v (
00509   llrp_utf8v_t                  Value,
00510   const CFieldDescriptor *      pFieldDescriptor)
00511 {
00512     unsigned int                nByte;
00513 
00514     nByte = 2u + Value.m_nValue * 1u;
00515 
00516     if(checkAvailable(nByte, pFieldDescriptor))
00517     {
00518         unsigned int            i;
00519 
00520         m_pEncoder->next_u16(Value.m_nValue);
00521         for(i = 0; i < Value.m_nValue; i++)
00522         {
00523             m_pEncoder->next_u8(Value.m_pValue[i]);
00524         }
00525     }
00526 }
00527 
00528 void
00529 CFrameEncoderStream::put_bytesToEnd (
00530   llrp_bytesToEnd_t             Value,
00531   const CFieldDescriptor *      pFieldDescriptor)
00532 {
00533     unsigned int                nByte;
00534 
00535     nByte = Value.m_nValue * 1u;
00536 
00537     if(checkAvailable(nByte, pFieldDescriptor))
00538     {
00539         unsigned int            i;
00540 
00541         for(i = 0; i < Value.m_nValue; i++)
00542         {
00543             m_pEncoder->next_u8(Value.m_pValue[i]);
00544         }
00545     }
00546 }
00547 
00548 /*
00549  * Enumerated types of various sizes
00550  */
00551 
00552 void
00553 CFrameEncoderStream::put_e1 (
00554   int                           eValue,
00555   const CFieldDescriptor *      pFieldDescriptor)
00556 {
00557     put_u1((llrp_u1_t)eValue, pFieldDescriptor);
00558 }
00559 
00560 void
00561 CFrameEncoderStream::put_e2 (
00562   int                           eValue,
00563   const CFieldDescriptor *      pFieldDescriptor)
00564 {
00565     put_u2((llrp_u2_t)eValue, pFieldDescriptor);
00566 }
00567 
00568 void
00569 CFrameEncoderStream::put_e8 (
00570   int                           eValue,
00571   const CFieldDescriptor *      pFieldDescriptor)
00572 {
00573     put_u8((llrp_u8_t)eValue, pFieldDescriptor);
00574 }
00575 
00576 void
00577 CFrameEncoderStream::put_e16 (
00578   int                           eValue,
00579   const CFieldDescriptor *      pFieldDescriptor)
00580 {
00581     put_u16((llrp_u16_t)eValue, pFieldDescriptor);
00582 }
00583 
00584 void
00585 CFrameEncoderStream::put_e32 (
00586   int                           eValue,
00587   const CFieldDescriptor *      pFieldDescriptor)
00588 {
00589     put_u32((llrp_u32_t)eValue, pFieldDescriptor);
00590 }
00591 
00592 void
00593 CFrameEncoderStream::put_e8v (
00594   llrp_u8v_t                    Value,
00595   const CFieldDescriptor *      pFieldDescriptor)
00596 {
00597     put_u8v(Value, pFieldDescriptor);
00598 }
00599 
00600 /*
00601  * Reserved types are some number of bits
00602  */
00603 
00604 void
00605 CFrameEncoderStream::put_reserved (
00606   unsigned int                  nBits)
00607 {
00608     CErrorDetails *             pError = &m_pEncoder->m_ErrorDetails;
00609 
00610     if(RC_OK != pError->m_eResultCode)
00611     {
00612         return;
00613     }
00614 
00615     while(nBits > 0)
00616     {
00617         unsigned int            Step = 7u & nBits;
00618 
00619         if(0 != m_pEncoder->m_nBitFieldResid)
00620         {
00621             if(Step != m_pEncoder->m_nBitFieldResid)
00622             {
00623                 pError->m_eResultCode = RC_UnalignedReservedBits;
00624                 pError->m_pWhatStr    = "unaligned reserved bits";
00625                 pError->m_pRefType    = m_pRefType;
00626                 pError->m_pRefField   = NULL;
00627                 pError->m_OtherDetail = m_pEncoder->m_iNext;
00628                 return;
00629             }
00630 
00631             m_pEncoder->next_u8(m_pEncoder->m_BitFieldBuffer);
00632             nBits -= Step;
00633             m_pEncoder->m_BitFieldBuffer = 0;
00634             m_pEncoder->m_nBitFieldResid = 0;
00635         }
00636         else
00637         {
00638             if(0 != Step)
00639             {
00640                 pError->m_eResultCode = RC_UnalignedReservedBits;
00641                 pError->m_pWhatStr    = "unaligned reserved bits";
00642                 pError->m_pRefType    = m_pRefType;
00643                 pError->m_pRefField   = NULL;
00644                 pError->m_OtherDetail = m_pEncoder->m_iNext;
00645                 return;
00646             }
00647 
00648             if(m_pEncoder->m_iNext >= m_pEncoder->m_nBuffer)
00649             {
00650                 pError->m_eResultCode = RC_ReservedBitsOverrun;
00651                 pError->m_pWhatStr    = "overrun at reserved bits";
00652                 pError->m_pRefType    = m_pRefType;
00653                 pError->m_pRefField   = NULL;
00654                 pError->m_OtherDetail = m_pEncoder->m_iNext;
00655                 return;
00656             }
00657 
00658             m_pEncoder->next_u8(0);
00659             nBits -= 8;
00660         }
00661     }
00662 }
00663 
00664 CFrameEncoderStream::CFrameEncoderStream (
00665   CFrameEncoder *               pEncoder)
00666 {
00667     m_pEncoder                  = pEncoder;
00668     m_pEnclosingEncoderStream   = NULL;
00669     m_iBegin                    = m_pEncoder->m_iNext;
00670     m_pRefType                  = NULL;
00671 }
00672 
00673 CFrameEncoderStream::CFrameEncoderStream (
00674   CFrameEncoderStream *         pEnclosingEncoderStream)
00675 {
00676     m_pEncoder                  = pEnclosingEncoderStream->m_pEncoder;
00677     m_pEnclosingEncoderStream   = pEnclosingEncoderStream;
00678     m_iBegin                    = m_pEncoder->m_iNext;
00679     m_pRefType                  = NULL;
00680 }
00681 
00682 void
00683 CFrameEncoderStream::putElement (
00684   const CElement *              pElement)
00685 {
00686     CErrorDetails *             pError = &m_pEncoder->m_ErrorDetails;
00687     enum { MSG, TLV, TV, CUST_MSG, CUST_TLV } eFormat;
00688 
00689     if(RC_OK != pError->m_eResultCode)
00690     {
00691         return;
00692     }
00693 
00694     m_pRefType = pElement->m_pType;
00695 
00696     if(m_pRefType->m_bIsMessage)
00697     {
00698         eFormat = (NULL == m_pRefType->m_pVendorDescriptor) ? MSG : CUST_MSG;
00699     }
00700     else if(NULL == m_pRefType->m_pVendorDescriptor &&
00701             128 > m_pRefType->m_TypeNum)
00702     {
00703         /* TV parameter, never custom, no length */
00704         eFormat = TV;
00705     }
00706     else
00707     {
00708         /* TLV parameter */
00709         eFormat = (NULL == m_pRefType->m_pVendorDescriptor) ? TLV : CUST_TLV;
00710     }
00711 
00712     /*
00713      * Format the element header. The length part, if one,
00714      * is a place holder and back-patched later.
00715      */
00716     switch(eFormat)
00717     {
00718     default:
00719         assert(0);
00720         break;
00721 
00722     case MSG:
00723         {
00724             llrp_u16_t          VersType;
00725 
00726             VersType = (1u << 10u) | m_pRefType->m_TypeNum;
00727             put_u16(VersType, &g_fdMessageHeader_Type);
00728             put_u32(0, &g_fdMessageHeader_Length);
00729             put_u32(((const CMessage *)pElement)->getMessageID(),
00730                 &g_fdMessageHeader_MessageID);
00731         }
00732         break;
00733 
00734     case CUST_MSG:
00735         {
00736             llrp_u16_t          VersType;
00737 
00738             /* Custom message */
00739             VersType = (1u << 10u) | 1023u;
00740             put_u16(VersType,
00741                 &g_fdMessageHeader_Type);
00742             /* length is a placeholder */
00743             put_u32(0, &g_fdMessageHeader_Length);
00744             put_u32(((const CMessage *)pElement)->getMessageID(),
00745                 &g_fdMessageHeader_MessageID);
00746             put_u32(
00747                 m_pRefType->m_pVendorDescriptor->m_VendorID,
00748                 &g_fdMessageHeader_VendorPEN);
00749             put_u8(m_pRefType->m_TypeNum, &g_fdMessageHeader_Subtype);
00750         }
00751         break;
00752 
00753     case TV:
00754         put_u8(m_pRefType->m_TypeNum | 0x80u, &g_fdParameterHeader_TVType);
00755         break;
00756 
00757     case TLV:
00758         /* Standard parameter */
00759         put_u16(m_pRefType->m_TypeNum, &g_fdParameterHeader_TLVType);
00760         put_u16(0, &g_fdParameterHeader_TLVLength);
00761         break;
00762 
00763     case CUST_TLV:
00764         /* Custom parameter */
00765         put_u16(1023u, &g_fdParameterHeader_TLVType);
00766         put_u16(0, &g_fdParameterHeader_TLVLength);
00767         put_u32(
00768             m_pRefType->m_pVendorDescriptor->m_VendorID,
00769             &g_fdParameterHeader_VendorPEN);
00770         put_u32(m_pRefType->m_TypeNum, &g_fdParameterHeader_Subtype);
00771         break;
00772     }
00773 
00774     if(RC_OK != pError->m_eResultCode)
00775     {
00776         return;
00777     }
00778 
00779     pElement->encode(this);
00780 
00781     unsigned int        nLength;
00782     unsigned char *     pLen;
00783 
00784     nLength = m_pEncoder->m_iNext - m_iBegin;
00785     pLen = &m_pEncoder->m_pBuffer[m_iBegin];
00786 
00787     switch(eFormat)
00788     {
00789     default:
00790         assert(0);
00791         break;
00792 
00793     case MSG:
00794     case CUST_MSG:
00795         assert(nLength >= 10);
00796         pLen += 2;
00797         pLen[0] = nLength >> 24u;
00798         pLen[1] = nLength >> 16u;
00799         pLen[2] = nLength >> 8u;
00800         pLen[3] = nLength >> 0u;
00801         break;
00802 
00803     case TV:
00804         break;
00805 
00806     case TLV:
00807     case CUST_TLV:
00808         assert(nLength >= 4);
00809         pLen += 2;
00810         pLen[0] = nLength >> 8u;
00811         pLen[1] = nLength >> 0u;
00812         break;
00813     }
00814 }
00815 
00816 llrp_bool_t
00817 CFrameEncoderStream::checkAvailable (
00818   unsigned int                  nByte,
00819   const CFieldDescriptor *      pFieldDescriptor)
00820 {
00821     CErrorDetails *             pError = &m_pEncoder->m_ErrorDetails;
00822 
00823     if(RC_OK != pError->m_eResultCode)
00824     {
00825         return FALSE;
00826     }
00827 
00828     if(m_pEncoder->m_iNext + nByte > m_pEncoder->m_nBuffer)
00829     {
00830         pError->m_eResultCode = RC_FieldOverrun;
00831         pError->m_pRefField   = pFieldDescriptor;
00832         pError->m_pRefType    = m_pRefType;
00833         pError->m_pWhatStr    = "overrun at field";
00834         pError->m_OtherDetail = m_pEncoder->m_iNext;
00835 
00836         return FALSE;
00837     }
00838 
00839     if(0 != m_pEncoder->m_nBitFieldResid)
00840     {
00841         pError->m_eResultCode = RC_UnalignedBitField;
00842         pError->m_pRefField   = pFieldDescriptor;
00843         pError->m_pRefType    = m_pRefType;
00844         pError->m_pWhatStr    = "unalign/incomplete bit field";
00845         pError->m_OtherDetail = m_pEncoder->m_iNext;
00846 
00847         return FALSE;
00848     }
00849 
00850     return TRUE;
00851 }
00852 
00853 llrp_bool_t
00854 CFrameEncoderStream::putBitField (
00855   unsigned int                  nBit,
00856   unsigned int                  Value,
00857   const CFieldDescriptor *      pFieldDescriptor)
00858 {
00859     CErrorDetails *             pError = &m_pEncoder->m_ErrorDetails;
00860 
00861     if(0 == m_pEncoder->m_nBitFieldResid)
00862     {
00863         if(!checkAvailable(1u, pFieldDescriptor))
00864         {
00865             return FALSE;
00866         }
00867         m_pEncoder->m_BitFieldBuffer = 0;
00868         m_pEncoder->m_nBitFieldResid = 8u;
00869     }
00870 
00871     if(m_pEncoder->m_nBitFieldResid < nBit)
00872     {
00873         pError->m_eResultCode = RC_UnalignedBitField;
00874         pError->m_pWhatStr    = "unalign/incomplete bit field";
00875         pError->m_pRefType    = m_pRefType;
00876         pError->m_pRefField   = pFieldDescriptor;
00877         pError->m_OtherDetail = m_pEncoder->m_iNext;
00878         return FALSE;
00879     }
00880 
00881     m_pEncoder->m_nBitFieldResid -= nBit;
00882 
00883     Value &= (1u << nBit) - 1u;
00884 
00885     m_pEncoder->m_BitFieldBuffer |= Value << m_pEncoder->m_nBitFieldResid;
00886 
00887 
00888     if(0 == m_pEncoder->m_nBitFieldResid)
00889     {
00890         m_pEncoder->next_u8(m_pEncoder->m_BitFieldBuffer);
00891         m_pEncoder->m_BitFieldBuffer = 0;
00892         m_pEncoder->m_nBitFieldResid = 0;
00893     }
00894 
00895     return TRUE;
00896 }
00897 
00898 };