Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

ltkcpp_frameencode.cpp

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

Generated on Wed Nov 26 12:27:45 2008 for LTKCPP-- LLRP Toolkit C Plus Plus Library by  doxygen 1.3.9.1