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