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

ltkcpp_framedecode.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 CFrameDecoder::CFrameDecoder (
00033   const CTypeRegistry *         pTypeRegistry,
00034   unsigned char *               pBuffer,
00035   unsigned int                  nBuffer)
00036  : CDecoder(pTypeRegistry)
00037 {
00038     m_pBuffer        = pBuffer;
00039     m_nBuffer        = nBuffer;
00040 
00041     m_iNext          = 0;
00042     m_BitFieldBuffer = 0;
00043     m_nBitFieldResid = 0;
00044 }
00045 
00046 CFrameDecoder::~CFrameDecoder (void)
00047 {
00048 }
00049 
00050 CMessage *
00051 CFrameDecoder::decodeMessage (void)
00052 {
00053     CFrameDecoderStream     DecoderStream(this);
00054     CMessage *              pMessage;
00055 
00056     pMessage = DecoderStream.getMessage();
00057 
00058     return pMessage;
00059 }
00060 
00061 llrp_u8_t
00062 CFrameDecoder::next_u8 (void)
00063 {
00064     llrp_u8_t                   Value;
00065 
00066     assert(m_iNext + 1u <= m_nBuffer);
00067 
00068     Value = m_pBuffer[m_iNext++];
00069 
00070     return Value;
00071 }
00072 
00073 llrp_u16_t
00074 CFrameDecoder::next_u16 (void)
00075 {
00076     llrp_u16_t                  Value;
00077 
00078     assert(m_iNext + 2u <= m_nBuffer);
00079 
00080     Value = m_pBuffer[m_iNext++];
00081     Value <<= 8u;
00082     Value |= m_pBuffer[m_iNext++];
00083 
00084     return Value;
00085 }
00086 
00087 llrp_u32_t
00088 CFrameDecoder::next_u32 (void)
00089 {
00090     llrp_u32_t                  Value;
00091 
00092     assert(m_iNext + 4u <= m_nBuffer);
00093 
00094     Value = m_pBuffer[m_iNext++];
00095     Value <<= 8u;
00096     Value |= m_pBuffer[m_iNext++];
00097     Value <<= 8u;
00098     Value |= m_pBuffer[m_iNext++];
00099     Value <<= 8u;
00100     Value |= m_pBuffer[m_iNext++];
00101 
00102     return Value;
00103 }
00104 
00105 llrp_u64_t
00106 CFrameDecoder::next_u64 (void)
00107 {
00108     llrp_u64_t                  Value;
00109 
00110     assert(m_iNext + 8u <= m_nBuffer);
00111 
00112     Value = m_pBuffer[m_iNext++];
00113     Value <<= 8u;
00114     Value |= m_pBuffer[m_iNext++];
00115     Value <<= 8u;
00116     Value |= m_pBuffer[m_iNext++];
00117     Value <<= 8u;
00118     Value |= m_pBuffer[m_iNext++];
00119     Value <<= 8u;
00120     Value |= m_pBuffer[m_iNext++];
00121     Value <<= 8u;
00122     Value |= m_pBuffer[m_iNext++];
00123     Value <<= 8u;
00124     Value |= m_pBuffer[m_iNext++];
00125     Value <<= 8u;
00126     Value |= m_pBuffer[m_iNext++];
00127 
00128     return Value;
00129 }
00130 
00131 llrp_u8_t
00132 CFrameDecoderStream::get_u8 (
00133   const CFieldDescriptor *      pFieldDescriptor)
00134 {
00135     llrp_u8_t                   Value;
00136 
00137     if(checkAvailable(1u, pFieldDescriptor))
00138     {
00139         Value = m_pDecoder->next_u8();
00140     }
00141     else
00142     {
00143         Value = 0;
00144     }
00145 
00146     return Value;
00147 }
00148 
00149 llrp_s8_t
00150 CFrameDecoderStream::get_s8 (
00151   const CFieldDescriptor *      pFieldDescriptor)
00152 {
00153     llrp_s8_t                   Value;
00154 
00155     if(checkAvailable(1u, pFieldDescriptor))
00156     {
00157         Value = m_pDecoder->next_u8();
00158     }
00159     else
00160     {
00161         Value = 0;
00162     }
00163 
00164     return Value;
00165 }
00166 
00167 llrp_u8v_t
00168 CFrameDecoderStream::get_u8v (
00169   const CFieldDescriptor *      pFieldDescriptor)
00170 {
00171     llrp_u16_t                  nValue;
00172     llrp_u8v_t                  Value;
00173 
00174     nValue = getVarlenCount(pFieldDescriptor);
00175 
00176     if(0 < nValue)
00177     {
00178         if(checkAvailable(1u * nValue, pFieldDescriptor))
00179         {
00180             Value = llrp_u8v_t(nValue);
00181             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00182             {
00183                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00184                 {
00185                     Value.m_pValue[Ix] = m_pDecoder->next_u8();
00186                 }
00187             }
00188         }
00189     }
00190 
00191     return Value;
00192 }
00193 
00194 llrp_s8v_t
00195 CFrameDecoderStream::get_s8v (
00196   const CFieldDescriptor *      pFieldDescriptor)
00197 {
00198     llrp_u16_t                  nValue;
00199     llrp_s8v_t                  Value;
00200 
00201     nValue = getVarlenCount(pFieldDescriptor);
00202 
00203     if(0 < nValue)
00204     {
00205         if(checkAvailable(1u * nValue, pFieldDescriptor))
00206         {
00207             Value = llrp_s8v_t(nValue);
00208             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00209             {
00210                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00211                 {
00212                     Value.m_pValue[Ix] = m_pDecoder->next_u8();
00213                 }
00214             }
00215         }
00216     }
00217 
00218     return Value;
00219 }
00220 
00221 llrp_u16_t
00222 CFrameDecoderStream::get_u16 (
00223   const CFieldDescriptor *      pFieldDescriptor)
00224 {
00225     llrp_u16_t                  Value;
00226 
00227     if(checkAvailable(2u, pFieldDescriptor))
00228     {
00229         Value = m_pDecoder->next_u16();
00230     }
00231     else
00232     {
00233         Value = 0;
00234     }
00235 
00236     return Value;
00237 }
00238 
00239 llrp_s16_t
00240 CFrameDecoderStream::get_s16 (
00241   const CFieldDescriptor *      pFieldDescriptor)
00242 {
00243     llrp_u16_t                  Value;
00244 
00245     if(checkAvailable(2u, pFieldDescriptor))
00246     {
00247         Value = m_pDecoder->next_u16();
00248     }
00249     else
00250     {
00251         Value = 0;
00252     }
00253 
00254     return Value;
00255 }
00256 
00257 llrp_u16v_t
00258 CFrameDecoderStream::get_u16v (
00259   const CFieldDescriptor *      pFieldDescriptor)
00260 {
00261     llrp_u16_t                  nValue;
00262     llrp_u16v_t                 Value;
00263 
00264     nValue = getVarlenCount(pFieldDescriptor);
00265 
00266     if(0 < nValue)
00267     {
00268         if(checkAvailable(2u * nValue, pFieldDescriptor))
00269         {
00270             Value = llrp_u16v_t(nValue);
00271             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00272             {
00273                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00274                 {
00275                     Value.m_pValue[Ix] = m_pDecoder->next_u16();
00276                 }
00277             }
00278         }
00279     }
00280 
00281     return Value;
00282 }
00283 
00284 llrp_s16v_t
00285 CFrameDecoderStream::get_s16v (
00286   const CFieldDescriptor *      pFieldDescriptor)
00287 {
00288     llrp_u16_t                  nValue;
00289     llrp_s16v_t                 Value;
00290 
00291     nValue = getVarlenCount(pFieldDescriptor);
00292 
00293     if(0 < nValue)
00294     {
00295         if(checkAvailable(2u * nValue, pFieldDescriptor))
00296         {
00297             Value = llrp_s16v_t(nValue);
00298             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00299             {
00300                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00301                 {
00302                     Value.m_pValue[Ix] = m_pDecoder->next_u16();
00303                 }
00304             }
00305         }
00306     }
00307 
00308     return Value;
00309 }
00310 
00311 
00312 
00313 llrp_u32_t
00314 CFrameDecoderStream::get_u32 (
00315   const CFieldDescriptor *      pFieldDescriptor)
00316 {
00317     llrp_u32_t                  Value;
00318 
00319     if(checkAvailable(4u, pFieldDescriptor))
00320     {
00321         Value = m_pDecoder->next_u32();
00322     }
00323     else
00324     {
00325         Value = 0;
00326     }
00327 
00328     return Value;
00329 }
00330 
00331 llrp_s32_t
00332 CFrameDecoderStream::get_s32 (
00333   const CFieldDescriptor *      pFieldDescriptor)
00334 {
00335     llrp_s32_t                  Value;
00336 
00337     if(checkAvailable(4u, pFieldDescriptor))
00338     {
00339         Value = m_pDecoder->next_u32();
00340     }
00341     else
00342     {
00343         Value = 0;
00344     }
00345 
00346     return Value;
00347 }
00348 
00349 llrp_u32v_t
00350 CFrameDecoderStream::get_u32v (
00351   const CFieldDescriptor *      pFieldDescriptor)
00352 {
00353     llrp_u16_t                  nValue;
00354     llrp_u32v_t                 Value;
00355 
00356     nValue = getVarlenCount(pFieldDescriptor);
00357 
00358     if(0 < nValue)
00359     {
00360         if(checkAvailable(4u * nValue, pFieldDescriptor))
00361         {
00362             Value = llrp_u32v_t(nValue);
00363             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00364             {
00365                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00366                 {
00367                     Value.m_pValue[Ix] = m_pDecoder->next_u32();
00368                 }
00369             }
00370         }
00371     }
00372 
00373     return Value;
00374 }
00375 
00376 llrp_s32v_t
00377 CFrameDecoderStream::get_s32v (
00378   const CFieldDescriptor *      pFieldDescriptor)
00379 {
00380     llrp_u16_t                  nValue;
00381     llrp_s32v_t                 Value;
00382 
00383     nValue = getVarlenCount(pFieldDescriptor);
00384 
00385     if(0 < nValue)
00386     {
00387         if(checkAvailable(4u * nValue, pFieldDescriptor))
00388         {
00389             Value = llrp_s32v_t(nValue);
00390             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00391             {
00392                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00393                 {
00394                     Value.m_pValue[Ix] = m_pDecoder->next_u32();
00395                 }
00396             }
00397         }
00398     }
00399 
00400     return Value;
00401 }
00402 
00403 
00404 llrp_u64_t
00405 CFrameDecoderStream::get_u64 (
00406   const CFieldDescriptor *      pFieldDescriptor)
00407 {
00408     llrp_u64_t                  Value;
00409 
00410     if(checkAvailable(8u, pFieldDescriptor))
00411     {
00412         Value = m_pDecoder->next_u64();
00413     }
00414     else
00415     {
00416         Value = 0;
00417     }
00418 
00419     return Value;
00420 }
00421 
00422 llrp_s64_t
00423 CFrameDecoderStream::get_s64 (
00424   const CFieldDescriptor *      pFieldDescriptor)
00425 {
00426     llrp_s64_t                  Value;
00427 
00428     if(checkAvailable(8u, pFieldDescriptor))
00429     {
00430         Value = m_pDecoder->next_u64();
00431     }
00432     else
00433     {
00434         Value = 0;
00435     }
00436 
00437     return Value;
00438 }
00439 
00440 llrp_u64v_t
00441 CFrameDecoderStream::get_u64v (
00442   const CFieldDescriptor *      pFieldDescriptor)
00443 {
00444     llrp_u16_t                  nValue;
00445     llrp_u64v_t                 Value;
00446 
00447     nValue = getVarlenCount(pFieldDescriptor);
00448 
00449     if(0 < nValue)
00450     {
00451         if(checkAvailable(8u * nValue, pFieldDescriptor))
00452         {
00453             Value = llrp_u64v_t(nValue);
00454             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00455             {
00456                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00457                 {
00458                     Value.m_pValue[Ix] = m_pDecoder->next_u64();
00459                 }
00460             }
00461         }
00462     }
00463 
00464     return Value;
00465 }
00466 
00467 llrp_s64v_t
00468 CFrameDecoderStream::get_s64v (
00469   const CFieldDescriptor *      pFieldDescriptor)
00470 {
00471     llrp_u16_t                  nValue;
00472     llrp_s64v_t                 Value;
00473 
00474     nValue = getVarlenCount(pFieldDescriptor);
00475 
00476     if(0 < nValue)
00477     {
00478         if(checkAvailable(8u * nValue, pFieldDescriptor))
00479         {
00480             Value = llrp_s64v_t(nValue);
00481             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00482             {
00483                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00484                 {
00485                     Value.m_pValue[Ix] = m_pDecoder->next_u64();
00486                 }
00487             }
00488         }
00489     }
00490 
00491     return Value;
00492 }
00493 
00494 llrp_u1_t
00495 CFrameDecoderStream::get_u1 (
00496   const CFieldDescriptor *      pFieldDescriptor)
00497 {
00498     llrp_u1_t                   Value;
00499 
00500     Value = getBitField(1, pFieldDescriptor);
00501 
00502     return Value;
00503 }
00504 
00505 llrp_u1v_t
00506 CFrameDecoderStream::get_u1v (
00507   const CFieldDescriptor *      pFieldDescriptor)
00508 {
00509     llrp_u16_t                  nBit;
00510     llrp_u1v_t                  Value;
00511 
00512     nBit = getVarlenCount(pFieldDescriptor);
00513 
00514     if(0 < nBit)
00515     {
00516         unsigned int                nByte = (nBit + 7u) / 8u;
00517 
00518         if(checkAvailable(nByte, pFieldDescriptor))
00519         {
00520             Value = llrp_u1v_t(nBit);
00521             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00522             {
00523                 for(unsigned int Ix = 0; Ix < nByte; Ix++)
00524                 {
00525                     Value.m_pValue[Ix] = m_pDecoder->next_u8();
00526                 }
00527             }
00528         }
00529     }
00530 
00531     return Value;
00532 }
00533 
00534 llrp_u2_t
00535 CFrameDecoderStream::get_u2 (
00536   const CFieldDescriptor *      pFieldDescriptor)
00537 {
00538     llrp_u2_t                   Value;
00539 
00540     Value = getBitField(2, pFieldDescriptor);
00541 
00542     return Value;
00543 }
00544 
00545 llrp_u96_t
00546 CFrameDecoderStream::get_u96 (
00547   const CFieldDescriptor *      pFieldDescriptor)
00548 {
00549     llrp_u96_t                  Value;
00550 
00551     if(checkAvailable(12u, pFieldDescriptor))
00552     {
00553         for(unsigned int Ix = 0; Ix < 12u; Ix++)
00554         {
00555             Value.m_aValue[Ix] = m_pDecoder->next_u8();
00556         }
00557     }
00558 
00559     return Value;
00560 }
00561 
00562 llrp_utf8v_t
00563 CFrameDecoderStream::get_utf8v (
00564   const CFieldDescriptor *      pFieldDescriptor)
00565 {
00566     llrp_u16_t                  nValue;
00567     llrp_utf8v_t                Value;
00568 
00569     nValue = getVarlenCount(pFieldDescriptor);
00570 
00571     if(0 < nValue)
00572     {
00573         if(checkAvailable(1u * nValue, pFieldDescriptor))
00574         {
00575             Value = llrp_utf8v_t(nValue);
00576             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00577             {
00578                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00579                 {
00580                     Value.m_pValue[Ix] = m_pDecoder->next_u8();
00581                 }
00582             }
00583         }
00584     }
00585 
00586     return Value;
00587 }
00588 
00589 llrp_bytesToEnd_t
00590 CFrameDecoderStream::get_bytesToEnd (
00591   const CFieldDescriptor *      pFieldDescriptor)
00592 {
00593     llrp_u16_t                  nValue;
00594     llrp_bytesToEnd_t           Value;
00595 
00596     nValue = getRemainingByteCount();
00597 
00598     if(0 < nValue)
00599     {
00600         if(checkAvailable(1u * nValue, pFieldDescriptor))
00601         {
00602             Value = llrp_bytesToEnd_t(nValue);
00603             if(verifyVectorAllocation(Value.m_pValue, pFieldDescriptor))
00604             {
00605                 for(unsigned int Ix = 0; Ix < nValue; Ix++)
00606                 {
00607                     Value.m_pValue[Ix] = m_pDecoder->next_u8();
00608                 }
00609             }
00610         }
00611     }
00612 
00613     return Value;
00614 }
00615 
00616 int
00617 CFrameDecoderStream::get_e1 (
00618   const CFieldDescriptor *      pFieldDescriptor)
00619 {
00620     int                         eValue;
00621 
00622     eValue = (int)get_u1(pFieldDescriptor);
00623 
00624     return eValue;
00625 }
00626 
00627 int
00628 CFrameDecoderStream::get_e2 (
00629   const CFieldDescriptor *      pFieldDescriptor)
00630 {
00631     int                         eValue;
00632 
00633     eValue = (int)get_u2(pFieldDescriptor);
00634 
00635     return eValue;
00636 }
00637 
00638 int
00639 CFrameDecoderStream::get_e8 (
00640   const CFieldDescriptor *      pFieldDescriptor)
00641 {
00642     int                         eValue;
00643 
00644     eValue = (int)get_u8(pFieldDescriptor);
00645 
00646     return eValue;
00647 }
00648 
00649 int
00650 CFrameDecoderStream::get_e16 (
00651   const CFieldDescriptor *      pFieldDescriptor)
00652 {
00653     int                         eValue;
00654 
00655     eValue = (int)get_u16(pFieldDescriptor);
00656 
00657     return eValue;
00658 }
00659 
00660 int
00661 CFrameDecoderStream::get_e32 (
00662   const CFieldDescriptor *      pFieldDescriptor)
00663 {
00664     int                         eValue;
00665 
00666     eValue = (int)get_u32(pFieldDescriptor);
00667 
00668     return eValue;
00669 }
00670 
00671 llrp_u8v_t
00672 CFrameDecoderStream::get_e8v (
00673   const CFieldDescriptor *      pFieldDescriptor)
00674 {
00675     return get_u8v(pFieldDescriptor);
00676 }
00677 
00678 void
00679 CFrameDecoderStream::get_reserved (
00680   unsigned int                  nBit)
00681 {
00682     CErrorDetails *             pError = &m_pDecoder->m_ErrorDetails;
00683 
00684     if(RC_OK != pError->m_eResultCode)
00685     {
00686         return;
00687     }
00688 
00689     while(0 < nBit)
00690     {
00691         unsigned int            Step = 7u & nBit;
00692 
00693         if(0 != m_pDecoder->m_nBitFieldResid)
00694         {
00695             if(Step != m_pDecoder->m_nBitFieldResid)
00696             {
00697                 pError->m_eResultCode = RC_UnalignedReservedBits;
00698                 pError->m_pWhatStr    = "unaligned reserved bits";
00699                 pError->m_pRefType    = m_pRefType;
00700                 pError->m_pRefField   = NULL;
00701                 pError->m_OtherDetail = m_pDecoder->m_iNext;
00702                 return;
00703             }
00704 
00705             nBit -= Step;
00706             m_pDecoder->m_nBitFieldResid = 0;
00707         }
00708         else
00709         {
00710             if(0 != Step)
00711             {
00712                 pError->m_eResultCode = RC_UnalignedReservedBits;
00713                 pError->m_pWhatStr    = "unaligned reserved bits";
00714                 pError->m_pRefType    = m_pRefType;
00715                 pError->m_pRefField   = NULL;
00716                 pError->m_OtherDetail = m_pDecoder->m_iNext;
00717                 return;
00718             }
00719 
00720             if(m_pDecoder->m_iNext >= m_iLimit)
00721             {
00722                 pError->m_eResultCode = RC_ReservedBitsUnderrun;
00723                 pError->m_pWhatStr    = "underrun at reserved bits";
00724                 pError->m_pRefType    = m_pRefType;
00725                 pError->m_pRefField   = NULL;
00726                 pError->m_OtherDetail = m_pDecoder->m_iNext;
00727                 return;
00728             }
00729 
00730             m_pDecoder->next_u8();
00731             nBit -= 8;
00732         }
00733     }
00734 }
00735 
00736 CFrameDecoderStream::CFrameDecoderStream (
00737   CFrameDecoder *               pDecoder)
00738 {
00739     m_pDecoder                  = pDecoder;
00740     m_pEnclosingDecoderStream   = NULL;
00741     m_iBegin                    = pDecoder->m_iNext;
00742     m_iLimit                    = pDecoder->m_nBuffer;
00743     m_pRefType                  = NULL;
00744 }
00745 
00746 CFrameDecoderStream::CFrameDecoderStream (
00747   CFrameDecoderStream *         pEnclosingDecoderStream)
00748 {
00749     m_pDecoder                  = pEnclosingDecoderStream->m_pDecoder;
00750     m_pEnclosingDecoderStream   = pEnclosingDecoderStream;
00751     m_iBegin                    = m_pDecoder->m_iNext;
00752     m_iLimit                    = pEnclosingDecoderStream->m_iLimit;
00753     m_pRefType                  = NULL;
00754 }
00755 
00756 CMessage *
00757 CFrameDecoderStream::getMessage (void)
00758 {
00759     CErrorDetails *             pError = &m_pDecoder->m_ErrorDetails;
00760     const CTypeRegistry *       pRegistry = m_pDecoder->m_pRegistry;
00761     const CTypeDescriptor *     pTypeDescriptor = NULL;
00762     llrp_u16_t                  Type;
00763     llrp_u16_t                  Vers;
00764     llrp_u32_t                  nLength;
00765     unsigned int                iLimit;
00766     llrp_u32_t                  MessageID;
00767 
00768     if(RC_OK != pError->m_eResultCode)
00769     {
00770         return NULL;
00771     }
00772 
00773     Type = get_u16(&g_fdMessageHeader_Type);
00774     Vers = (Type >> 10) & 3;
00775     Type &= 0x3FF;
00776 
00777     if(RC_OK != pError->m_eResultCode)
00778     {
00779         return NULL;
00780     }
00781 
00782     if(1u != Vers)
00783     {
00784         pError->m_eResultCode = RC_BadVersion;
00785         pError->m_pWhatStr    = "unsupported version";
00786         pError->m_pRefType    = NULL;
00787         pError->m_pRefField   = &g_fdMessageHeader_Type;
00788         pError->m_OtherDetail = m_pDecoder->m_iNext;
00789         return NULL;
00790     }
00791 
00792     nLength = get_u32(&g_fdMessageHeader_Length);
00793 
00794     if(RC_OK != pError->m_eResultCode)
00795     {
00796         return NULL;
00797     }
00798 
00799     if(10u > nLength)
00800     {
00801         pError->m_eResultCode = RC_InvalidLength;
00802         pError->m_pWhatStr    = "message length too small";
00803         pError->m_pRefType    = NULL;
00804         pError->m_pRefField   = &g_fdMessageHeader_Length;
00805         pError->m_OtherDetail = m_pDecoder->m_iNext;
00806         return NULL;
00807     }
00808 
00809     iLimit = m_iBegin + nLength;
00810 
00811     if(iLimit > m_iLimit)
00812     {
00813         pError->m_eResultCode = RC_ExcessiveLength;
00814         pError->m_pWhatStr    = "message length exceeds enclosing length";
00815         pError->m_pRefType    = NULL;
00816         pError->m_pRefField   = &g_fdMessageHeader_Length;
00817         pError->m_OtherDetail = m_pDecoder->m_iNext;
00818         return NULL;
00819     }
00820 
00821     m_iLimit = iLimit;
00822 
00823     MessageID = get_u32(&g_fdMessageHeader_MessageID);
00824 
00825     if(RC_OK != pError->m_eResultCode)
00826     {
00827         return NULL;
00828     }
00829 
00830     /* Custom? */
00831     if(1023u == Type)
00832     {
00833         llrp_u32_t              VendorPEN;
00834         llrp_u8_t               Subtype;
00835 
00836         VendorPEN = get_u32(&g_fdMessageHeader_VendorPEN);
00837         Subtype   = get_u8(&g_fdMessageHeader_Subtype);
00838 
00839         if(RC_OK != pError->m_eResultCode)
00840         {
00841             return NULL;
00842         }
00843 
00844         pTypeDescriptor = pRegistry->lookupCustomMessage(VendorPEN, Subtype);
00845         if(NULL == pTypeDescriptor)
00846         {
00847             /*
00848              * If we don't have a definition for a particular
00849              * CUSTOM message, just use the generic one.
00850              */
00851             m_pDecoder->m_iNext -= 5;   /* back up to VendorPEN and SubType */
00852             pTypeDescriptor = pRegistry->lookupMessage(1023u);
00853         }
00854     }
00855     else
00856     {
00857         pTypeDescriptor = pRegistry->lookupMessage(Type);
00858     }
00859 
00860     if(NULL == pTypeDescriptor)
00861     {
00862         pError->m_eResultCode = RC_UnknownMessageType;
00863         pError->m_pWhatStr    = "unknown message type";
00864         pError->m_pRefType    = NULL;
00865         pError->m_pRefField   = &g_fdMessageHeader_Type;
00866         pError->m_OtherDetail = 0;
00867         return NULL;
00868     }
00869 
00870     m_pRefType = pTypeDescriptor;
00871 
00872     CMessage *                  pMessage;
00873 
00874     pMessage = (CMessage *) pTypeDescriptor->constructElement();
00875 
00876     if(NULL == pMessage)
00877     {
00878         pError->m_eResultCode = RC_MessageAllocationFailed;
00879         pError->m_pWhatStr    = "message allocation failed";
00880         pError->m_pRefType    = pTypeDescriptor;
00881         pError->m_pRefField   = NULL;
00882         pError->m_OtherDetail = m_pDecoder->m_iNext;
00883         return NULL;
00884     }
00885 
00886     pMessage->setMessageID(MessageID);
00887 
00888     pMessage->decodeFields(this);
00889 
00890     if(RC_OK != pError->m_eResultCode)
00891     {
00892         delete pMessage;
00893         return NULL;
00894     }
00895 
00896     /*
00897      * Subparameters
00898      */
00899     while(0 < getRemainingByteCount() &&
00900           RC_OK == pError->m_eResultCode)
00901     {
00902         CFrameDecoderStream         NestStream(this);
00903         CParameter *                pParameter;
00904 
00905         pParameter = NestStream.getParameter();
00906 
00907         if(NULL == pParameter)
00908         {
00909             if(RC_OK == pError->m_eResultCode)
00910             {
00911                 pError->m_eResultCode = RC_Botch;
00912                 pError->m_pWhatStr    = "botch -- no param and no error";
00913                 pError->m_pRefType    = pTypeDescriptor;
00914                 pError->m_pRefField   = NULL;
00915                 pError->m_OtherDetail = m_pDecoder->m_iNext;
00916             }
00917             break;
00918         }
00919 
00920         pParameter->m_pParent = pMessage;
00921         pMessage->addSubParameterToAllList(pParameter);
00922 
00923     }
00924 
00925     if(RC_OK == pError->m_eResultCode)
00926     {
00927         if(m_pDecoder->m_iNext != m_iLimit)
00928         {
00929             pError->m_eResultCode = RC_ExtraBytes;
00930             pError->m_pWhatStr    = "extra bytes at end of message";
00931             pError->m_pRefType    = pTypeDescriptor;
00932             pError->m_pRefField   = NULL;
00933             pError->m_OtherDetail = m_pDecoder->m_iNext;
00934         }
00935     }
00936 
00937     if(RC_OK != pError->m_eResultCode)
00938     {
00939         delete pMessage;
00940         return NULL;
00941     }
00942 
00943     pMessage->assimilateSubParameters(pError);
00944 
00945     if(RC_OK != pError->m_eResultCode)
00946     {
00947         delete pMessage;
00948         return NULL;
00949     }
00950 
00951     return pMessage;
00952 }
00953 
00954 CParameter *
00955 CFrameDecoderStream::getParameter (void)
00956 {
00957     CErrorDetails *             pError = &m_pDecoder->m_ErrorDetails;
00958     const CTypeRegistry *       pRegistry = m_pDecoder->m_pRegistry;
00959     const CTypeDescriptor *     pTypeDescriptor = NULL;
00960     llrp_u16_t                  Type;
00961     bool                        bIsTV;
00962 
00963     if(RC_OK != pError->m_eResultCode)
00964     {
00965         return NULL;
00966     }
00967 
00968     Type = get_u8(&g_fdParameterHeader_TVType);
00969 
00970     if(RC_OK != pError->m_eResultCode)
00971     {
00972         return NULL;
00973     }
00974 
00975     if(0 != (Type&0x80))
00976     {
00977         /*
00978          * Type-Value (TV).
00979          * All we can do is inherit the length from
00980          * the enclosing element.
00981          */
00982         Type &= 0x7F;
00983         bIsTV = TRUE;
00984     }
00985     else
00986     {
00987         /*
00988          * Type-Length-Value (TLV).
00989          * Back up and get the real type number,
00990          * then get the length.
00991          */
00992         m_pDecoder->m_iNext--;
00993         Type = get_u16(&g_fdParameterHeader_TLVType);
00994         Type &= 0x3FF;
00995 
00996         if(RC_OK != pError->m_eResultCode)
00997         {
00998             return NULL;
00999         }
01000 
01001         llrp_u16_t              nLength;
01002 
01003         nLength = get_u16(&g_fdParameterHeader_TLVLength);
01004 
01005         if(RC_OK != pError->m_eResultCode)
01006         {
01007             return NULL;
01008         }
01009 
01010         if(4u > nLength)
01011         {
01012             pError->m_eResultCode = RC_InvalidLength;
01013             pError->m_pWhatStr    = "TLV parameter length too small";
01014             pError->m_pRefType    = NULL;
01015             pError->m_pRefField   = &g_fdParameterHeader_TLVLength;
01016             pError->m_OtherDetail = m_pDecoder->m_iNext;
01017             return NULL;
01018         }
01019 
01020         unsigned int            iLimit;
01021 
01022         iLimit = m_iBegin + nLength;
01023 
01024         if(iLimit > m_iLimit)
01025         {
01026             pError->m_eResultCode = RC_ExcessiveLength;
01027             pError->m_pWhatStr    =
01028                             "TLV parameter length exceeds enclosing length";
01029             pError->m_pRefType    = NULL;
01030             pError->m_pRefField   = &g_fdParameterHeader_TLVLength;
01031             pError->m_OtherDetail = m_pDecoder->m_iNext;
01032             return NULL;
01033         }
01034 
01035         m_iLimit = iLimit;
01036 
01037         bIsTV = FALSE;
01038     }
01039 
01040     /* Custom? */
01041     if(1023u == Type)
01042     {
01043         llrp_u32_t              VendorPEN;
01044         llrp_u32_t               Subtype;
01045 
01046         VendorPEN = get_u32(&g_fdParameterHeader_VendorPEN);
01047         Subtype   = get_u32(&g_fdParameterHeader_Subtype);
01048 
01049         if(RC_OK != pError->m_eResultCode)
01050         {
01051             return NULL;
01052         }
01053 
01054         pTypeDescriptor = pRegistry->lookupCustomParameter(VendorPEN, Subtype);
01055         if(NULL == pTypeDescriptor)
01056         {
01057             /*
01058              * If we don't have a definition for a particular
01059              * CUSTOM message, just use the generic one.
01060              */
01061             m_pDecoder->m_iNext -= 8;   /* back up to VendorPEN and SubType */
01062             pTypeDescriptor = pRegistry->lookupParameter(1023u);
01063         }
01064     }
01065     else
01066     {
01067         pTypeDescriptor = pRegistry->lookupParameter(Type);
01068     }
01069 
01070     if(NULL == pTypeDescriptor)
01071     {
01072         pError->m_eResultCode = RC_UnknownParameterType;
01073         pError->m_pWhatStr    = "unknown parameter type";
01074         pError->m_pRefType    = NULL;
01075         if(bIsTV)
01076         {
01077             pError->m_pRefField = &g_fdParameterHeader_TVType;
01078         }
01079         else
01080         {
01081             pError->m_pRefField = &g_fdParameterHeader_TLVType;
01082         }
01083         pError->m_OtherDetail = m_pDecoder->m_iNext;
01084         return NULL;
01085     }
01086 
01087     m_pRefType = pTypeDescriptor;
01088 
01089     CParameter *                pParameter;
01090 
01091     pParameter = (CParameter *) pTypeDescriptor->constructElement();
01092 
01093     if(NULL == pParameter)
01094     {
01095         pError->m_eResultCode = RC_ParameterAllocationFailed;
01096         pError->m_pWhatStr    = "parameter allocation failed";
01097         pError->m_pRefType    = pTypeDescriptor;
01098         pError->m_pRefField   = NULL;
01099         pError->m_OtherDetail = m_pDecoder->m_iNext;
01100         return NULL;
01101     }
01102 
01103     pParameter->decodeFields(this);
01104 
01105     if(RC_OK != pError->m_eResultCode)
01106     {
01107         delete pParameter;
01108         return NULL;
01109     }
01110 
01111     if(!bIsTV)
01112     {
01113         /*
01114          * Subparameters
01115          */
01116         while(0 < getRemainingByteCount() &&
01117           RC_OK == pError->m_eResultCode)
01118         {
01119             CFrameDecoderStream         NestStream(this);
01120             CParameter *                pSubParameter;
01121 
01122             pSubParameter = NestStream.getParameter();
01123 
01124             if(NULL == pSubParameter)
01125             {
01126                 if(RC_OK == pError->m_eResultCode)
01127                 {
01128                     pError->m_eResultCode = RC_Botch;
01129                     pError->m_pWhatStr    = "botch -- no param and no error";
01130                     pError->m_pRefType    = pTypeDescriptor;
01131                     pError->m_pRefField   = NULL;
01132                     pError->m_OtherDetail = m_pDecoder->m_iNext;
01133                 }
01134                 break;
01135             }
01136 
01137             pSubParameter->m_pParent = pParameter;
01138             pParameter->addSubParameterToAllList(pSubParameter);
01139         }
01140 
01141         if(RC_OK == pError->m_eResultCode)
01142         {
01143             if(m_pDecoder->m_iNext != m_iLimit)
01144             {
01145                 pError->m_eResultCode = RC_ExtraBytes;
01146                 pError->m_pWhatStr    = "extra bytes at end of TLV parameter";
01147                 pError->m_pRefType    = pTypeDescriptor;
01148                 pError->m_pRefField   = NULL;
01149                 pError->m_OtherDetail = m_pDecoder->m_iNext;
01150             }
01151         }
01152 
01153         if(RC_OK != pError->m_eResultCode)
01154         {
01155             delete pParameter;
01156             return NULL;
01157         }
01158 
01159         pParameter->assimilateSubParameters(pError);
01160 
01161         if(RC_OK != pError->m_eResultCode)
01162         {
01163             delete pParameter;
01164             return NULL;
01165         }
01166     }
01167 
01168     return pParameter;
01169 }
01170 
01171 unsigned int
01172 CFrameDecoderStream::getRemainingByteCount (void)
01173 {
01174     if(m_pDecoder->m_iNext < m_iLimit)
01175     {
01176         return m_iLimit - m_pDecoder->m_iNext;
01177     }
01178     else
01179     {
01180         return 0;
01181     }
01182 }
01183 
01184 llrp_bool_t
01185 CFrameDecoderStream::checkAvailable (
01186   unsigned int                  nByte,
01187   const CFieldDescriptor *      pFieldDescriptor)
01188 {
01189     CErrorDetails *             pError = &m_pDecoder->m_ErrorDetails;
01190 
01191     if(RC_OK != pError->m_eResultCode)
01192     {
01193         return FALSE;
01194     }
01195 
01196     if(m_pDecoder->m_iNext + nByte > m_iLimit)
01197     {
01198         pError->m_eResultCode = RC_FieldUnderrun;
01199         pError->m_pWhatStr    = "underrun at field";
01200         pError->m_pRefType    = m_pRefType;
01201         pError->m_pRefField   = pFieldDescriptor;
01202         pError->m_OtherDetail = m_pDecoder->m_iNext;
01203         return FALSE;
01204     }
01205 
01206     if(0 != m_pDecoder->m_nBitFieldResid)
01207     {
01208         pError->m_eResultCode = RC_UnalignedBitField;
01209         pError->m_pWhatStr    = "unaligned/incomplete bit field";
01210         pError->m_pRefType    = m_pRefType;
01211         pError->m_pRefField   = pFieldDescriptor;
01212         pError->m_OtherDetail = m_pDecoder->m_iNext;
01213         return FALSE;
01214     }
01215 
01216     return TRUE;
01217 }
01218 
01219 unsigned int
01220 CFrameDecoderStream::getBitField (
01221   unsigned int                  nBit,
01222   const CFieldDescriptor *      pFieldDescriptor)
01223 {
01224     CErrorDetails *             pError = &m_pDecoder->m_ErrorDetails;
01225     unsigned int                Value;
01226 
01227     if(0 == m_pDecoder->m_nBitFieldResid)
01228     {
01229         if(checkAvailable(1u, pFieldDescriptor))
01230         {
01231             m_pDecoder->m_BitFieldBuffer = m_pDecoder->next_u8();
01232             m_pDecoder->m_nBitFieldResid = 8u;
01233         }
01234         else
01235         {
01236             return 0;
01237         }
01238     }
01239 
01240     if(m_pDecoder->m_nBitFieldResid < nBit)
01241     {
01242         pError->m_eResultCode = RC_UnalignedBitField;
01243         pError->m_pWhatStr    = "unaligned/incomplete bit field";
01244         pError->m_pRefType    = m_pRefType;
01245         pError->m_pRefField   = pFieldDescriptor;
01246         pError->m_OtherDetail = m_pDecoder->m_iNext;
01247         return 0;
01248     }
01249 
01250     m_pDecoder->m_nBitFieldResid -= nBit;
01251 
01252     Value = m_pDecoder->m_BitFieldBuffer >> m_pDecoder->m_nBitFieldResid;
01253     Value &= (1u << nBit) - 1u;
01254 
01255     return Value;
01256 }
01257 
01258 llrp_u16_t
01259 CFrameDecoderStream::getVarlenCount (
01260   const CFieldDescriptor *      pFieldDescriptor)
01261 {
01262     llrp_u16_t                  nValue;
01263 
01264     if(checkAvailable(2u, pFieldDescriptor))
01265     {
01266         nValue = m_pDecoder->next_u16();
01267     }
01268     else
01269     {
01270         nValue = 0;
01271     }
01272 
01273     return nValue;
01274 }
01275 
01276 llrp_bool_t
01277 CFrameDecoderStream::verifyVectorAllocation (
01278   const void *              pValue,
01279   const CFieldDescriptor *  pFieldDescriptor)
01280 {
01281     if(NULL == pValue)
01282     {
01283         CErrorDetails *     pError = &m_pDecoder->m_ErrorDetails;
01284 
01285         pError->m_eResultCode = RC_FieldAllocationFailed;
01286         pError->m_pWhatStr    = "field allocation failed";
01287         pError->m_pRefType    = m_pRefType;
01288         pError->m_pRefField   = pFieldDescriptor;
01289         pError->m_OtherDetail = m_pDecoder->m_iNext;
01290 
01291         return FALSE;
01292     }
01293     else
01294     {
01295         return TRUE;
01296     }
01297 }
01298 
01299 };  /* namespace LLRP */

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