ltkc_frameencode.c
Go to the documentation of this file.
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 "ltkc_platform.h"
00025 #include "ltkc_base.h"
00026 #include "ltkc_frame.h"
00027 
00028 
00029 /*
00030  * BEGIN forward declarations
00031  */
00032 
00033 LLRP_tSFrameEncoder *
00034 LLRP_FrameEncoder_construct (
00035   unsigned char *               pBuffer,
00036   unsigned int                  nBuffer);
00037 
00038 static void
00039 encoderDestruct (
00040   LLRP_tSEncoder *              pBaseEncoder);
00041 
00042 static void
00043 encodeElement (
00044   LLRP_tSEncoder *              pBaseEncoder,
00045   const LLRP_tSElement *        pElement);
00046 
00047 static void
00048 next_u8 (
00049   LLRP_tSFrameEncoder *         pEncoder,
00050   llrp_u8_t                     Value);
00051 
00052 static void
00053 next_u16 (
00054   LLRP_tSFrameEncoder *         pEncoder,
00055   llrp_u16_t                    Value);
00056 
00057 static void
00058 next_u32 (
00059   LLRP_tSFrameEncoder *         pEncoder,
00060   llrp_u32_t                    Value);
00061 
00062 static void
00063 next_u64 (
00064   LLRP_tSFrameEncoder *         pEncoder,
00065   llrp_u64_t                    Value);
00066 
00067 static void
00068 putRequiredSubParameter (
00069   LLRP_tSEncoderStream *        pBaseEncoderStream,
00070   const LLRP_tSParameter *      pParameter,
00071   const LLRP_tSTypeDescriptor * pRefType);
00072 
00073 static void
00074 putOptionalSubParameter (
00075   LLRP_tSEncoderStream *        pBaseEncoderStream,
00076   const LLRP_tSParameter *      pParameter,
00077   const LLRP_tSTypeDescriptor * pRefType);
00078 
00079 static void
00080 putRequiredSubParameterList (
00081   LLRP_tSEncoderStream *        pBaseEncoderStream,
00082   const LLRP_tSParameter *      pParameterList,
00083   const LLRP_tSTypeDescriptor * pRefType);
00084 
00085 static void
00086 putOptionalSubParameterList (
00087   LLRP_tSEncoderStream *        pBaseEncoderStream,
00088   const LLRP_tSParameter *      pParameterList,
00089   const LLRP_tSTypeDescriptor * pRefType);
00090 
00091 static void
00092 put_u8 (
00093   LLRP_tSEncoderStream *        pBaseEncoderStream,
00094   const llrp_u8_t               Value,
00095   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00096 
00097 static void
00098 put_s8 (
00099   LLRP_tSEncoderStream *        pBaseEncoderStream,
00100   const llrp_s8_t               Value,
00101   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00102 
00103 static void
00104 put_u8v (
00105   LLRP_tSEncoderStream *        pBaseEncoderStream,
00106   const llrp_u8v_t              Value,
00107   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00108 
00109 static void
00110 put_s8v (
00111   LLRP_tSEncoderStream *        pBaseEncoderStream,
00112   const llrp_s8v_t              Value,
00113   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00114 
00115 static void
00116 put_u16 (
00117   LLRP_tSEncoderStream *        pBaseEncoderStream,
00118   const llrp_u16_t              Value,
00119   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00120 
00121 static void
00122 put_s16 (
00123   LLRP_tSEncoderStream *        pBaseEncoderStream,
00124   const llrp_s16_t              Value,
00125   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00126 
00127 static void
00128 put_u16v (
00129   LLRP_tSEncoderStream *        pBaseEncoderStream,
00130   const llrp_u16v_t             Value,
00131   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00132 
00133 static void
00134 put_s16v (
00135   LLRP_tSEncoderStream *        pBaseEncoderStream,
00136   const llrp_s16v_t             Value,
00137   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00138 
00139 static void
00140 put_u32 (
00141   LLRP_tSEncoderStream *        pBaseEncoderStream,
00142   const llrp_u32_t              Value,
00143   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00144 
00145 static void
00146 put_s32 (
00147   LLRP_tSEncoderStream *        pBaseEncoderStream,
00148   const llrp_s32_t              Value,
00149   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00150 
00151 static void
00152 put_u32v (
00153   LLRP_tSEncoderStream *        pBaseEncoderStream,
00154   const llrp_u32v_t             Value,
00155   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00156 
00157 static void
00158 put_s32v (
00159   LLRP_tSEncoderStream *        pBaseEncoderStream,
00160   const llrp_s32v_t             Value,
00161   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00162 
00163 static void
00164 put_u64 (
00165   LLRP_tSEncoderStream *        pBaseEncoderStream,
00166   const llrp_u64_t              Value,
00167   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00168 
00169 static void
00170 put_s64 (
00171   LLRP_tSEncoderStream *        pBaseEncoderStream,
00172   const llrp_s64_t              Value,
00173   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00174 
00175 static void
00176 put_u64v (
00177   LLRP_tSEncoderStream *        pBaseEncoderStream,
00178   const llrp_u64v_t             Value,
00179   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00180 
00181 static void
00182 put_s64v (
00183   LLRP_tSEncoderStream *        pBaseEncoderStream,
00184   const llrp_s64v_t             Value,
00185   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00186 
00187 static void
00188 put_u1 (
00189   LLRP_tSEncoderStream *        pBaseEncoderStream,
00190   const llrp_u1_t               Value,
00191   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00192 
00193 static void
00194 put_u1v (
00195   LLRP_tSEncoderStream *        pBaseEncoderStream,
00196   const llrp_u1v_t              Value,
00197   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00198 
00199 static void
00200 put_u2 (
00201   LLRP_tSEncoderStream *        pBaseEncoderStream,
00202   const llrp_u2_t               Value,
00203   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00204 
00205 static void
00206 put_u96 (
00207   LLRP_tSEncoderStream *        pBaseEncoderStream,
00208   const llrp_u96_t              Value,
00209   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00210 
00211 static void
00212 put_utf8v (
00213   LLRP_tSEncoderStream *        pBaseEncoderStream,
00214   const llrp_utf8v_t            Value,
00215   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00216 
00217 static void
00218 put_bytesToEnd (
00219   LLRP_tSEncoderStream *        pBaseEncoderStream,
00220   const llrp_bytesToEnd_t       Value,
00221   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00222 
00223 static void
00224 put_e1 (
00225   LLRP_tSEncoderStream *        pBaseEncoderStream,
00226   const int                     Value,
00227   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00228 
00229 static void
00230 put_e2 (
00231   LLRP_tSEncoderStream *        pBaseEncoderStream,
00232   const int                     Value,
00233   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00234 
00235 static void
00236 put_e8 (
00237   LLRP_tSEncoderStream *        pBaseEncoderStream,
00238   const int                     Value,
00239   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00240 
00241 static void
00242 put_e16 (
00243   LLRP_tSEncoderStream *        pBaseEncoderStream,
00244   const int                     Value,
00245   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00246 
00247 static void
00248 put_e32 (
00249   LLRP_tSEncoderStream *        pBaseEncoderStream,
00250   const int                     Value,
00251   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00252 
00253 static void
00254 put_e8v (
00255   LLRP_tSEncoderStream *        pBaseEncoderStream,
00256   const llrp_u8v_t              Value,
00257   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00258 
00259 static void
00260 put_reserved (
00261   LLRP_tSEncoderStream *        pBaseEncoderStream,
00262   unsigned int                  nBits);
00263 
00264 static void
00265 streamConstruct_outermost (
00266   LLRP_tSFrameEncoderStream *   pEncoderStream,
00267   LLRP_tSFrameEncoder *         pEncoder);
00268 
00269 static void
00270 streamConstruct_nested (
00271   LLRP_tSFrameEncoderStream *   pEncoderStream,
00272   LLRP_tSFrameEncoderStream *   pEnclosingEncoderStream);
00273 
00274 static void
00275 putElement (
00276   LLRP_tSFrameEncoderStream *   pEncoderStream,
00277   const LLRP_tSElement *        pElement);
00278 
00279 static void
00280 nestSubParameter (
00281   LLRP_tSFrameEncoderStream *   pEncoderStream,
00282   const LLRP_tSParameter *      pParameter);
00283 
00284 static llrp_bool_t
00285 checkAvailable (
00286   LLRP_tSFrameEncoderStream *   pEncoderStream,
00287   unsigned int                  nByte,
00288   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00289 
00290 static llrp_bool_t
00291 putBitField (
00292   LLRP_tSFrameEncoderStream *   pEncoderStream,
00293   unsigned int                  nBit,
00294   unsigned int                  Value,  
00295   const LLRP_tSFieldDescriptor *pFieldDescriptor);
00296 
00297 /*
00298  * END forward declarations
00299  */
00300 
00301 
00302 static LLRP_tSEncoderOps
00303 s_FrameEncoderOps =
00304 {
00305     .pfDestruct                 = encoderDestruct,
00306     .pfEncodeElement            = encodeElement,
00307 };
00308 
00309 static LLRP_tSEncoderStreamOps
00310 s_FrameEncoderStreamOps =
00311 {
00312     .pfPutRequiredSubParameter      = putRequiredSubParameter,
00313     .pfPutOptionalSubParameter      = putOptionalSubParameter,
00314     .pfPutRequiredSubParameterList  = putRequiredSubParameterList,
00315     .pfPutOptionalSubParameterList  = putOptionalSubParameterList,
00316 
00317     .pfPut_u8                       = put_u8,
00318     .pfPut_s8                       = put_s8,
00319     .pfPut_u8v                      = put_u8v,
00320     .pfPut_s8v                      = put_s8v,
00321 
00322     .pfPut_u16                      = put_u16,
00323     .pfPut_s16                      = put_s16,
00324     .pfPut_u16v                     = put_u16v,
00325     .pfPut_s16v                     = put_s16v,
00326 
00327     .pfPut_u32                      = put_u32,
00328     .pfPut_s32                      = put_s32,
00329     .pfPut_u32v                     = put_u32v,
00330     .pfPut_s32v                     = put_s32v,
00331 
00332     .pfPut_u64                      = put_u64,
00333     .pfPut_s64                      = put_s64,
00334     .pfPut_u64v                     = put_u64v,
00335     .pfPut_s64v                     = put_s64v,
00336 
00337     .pfPut_u1                       = put_u1,
00338     .pfPut_u1v                      = put_u1v,
00339     .pfPut_u2                       = put_u2,
00340     .pfPut_u96                      = put_u96,
00341     .pfPut_utf8v                    = put_utf8v,
00342 
00343     .pfPut_bytesToEnd               = put_bytesToEnd,
00344 
00345     .pfPut_e1                       = put_e1,
00346     .pfPut_e2                       = put_e2,
00347     .pfPut_e8                       = put_e8,
00348     .pfPut_e16                      = put_e16,
00349     .pfPut_e32                      = put_e32,
00350     .pfPut_e8v                      = put_e8v,
00351 
00352     .pfPut_reserved                 = put_reserved,
00353 };
00354 
00355 
00356 LLRP_tSFrameEncoder *
00357 LLRP_FrameEncoder_construct (
00358   unsigned char *               pBuffer,
00359   unsigned int                  nBuffer)
00360 {
00361     LLRP_tSFrameEncoder *       pEncoder;
00362 
00363     pEncoder = malloc(sizeof *pEncoder);
00364     if(NULL == pEncoder)
00365     {
00366         return pEncoder;
00367     }
00368 
00369     memset(pEncoder, 0, sizeof *pEncoder);
00370 
00371     pEncoder->encoderHdr.pEncoderOps = &s_FrameEncoderOps;
00372 
00373     pEncoder->pBuffer        = pBuffer;
00374     pEncoder->nBuffer        = nBuffer;
00375 
00376     pEncoder->iNext          = 0;
00377     pEncoder->BitFieldBuffer = 0;
00378     pEncoder->nBitFieldResid = 0;
00379 
00380     return pEncoder;
00381 }
00382 
00383 static void
00384 encoderDestruct (
00385   LLRP_tSEncoder *              pBaseEncoder)
00386 {
00387     LLRP_tSFrameEncoder *       pEncoder = (LLRP_tSFrameEncoder*)pBaseEncoder;
00388 
00389     free(pEncoder);
00390 }
00391 
00392 static void
00393 encodeElement (
00394   LLRP_tSEncoder *              pBaseEncoder,
00395   const LLRP_tSElement *        pElement)
00396 {
00397     LLRP_tSFrameEncoder *       pEncoder = (LLRP_tSFrameEncoder*)pBaseEncoder;
00398     LLRP_tSFrameEncoderStream   EncoderStream;
00399 
00400     if(NULL == pElement)
00401     {
00402         return;
00403     }
00404 
00405     streamConstruct_outermost(&EncoderStream, pEncoder);
00406 
00407     putElement(&EncoderStream, pElement);
00408 }
00409 
00410 static void
00411 next_u8 (
00412   LLRP_tSFrameEncoder *         pEncoder,
00413   llrp_u8_t                     Value)
00414 {
00415     assert(pEncoder->iNext + 1u <= pEncoder->nBuffer);
00416 
00417     pEncoder->pBuffer[pEncoder->iNext++] = Value;
00418 }
00419 
00420 static void
00421 next_u16 (
00422   LLRP_tSFrameEncoder *         pEncoder,
00423   llrp_u16_t                    Value)
00424 {
00425     assert(pEncoder->iNext + 2u <= pEncoder->nBuffer);
00426 
00427     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 8u;
00428     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 0u;
00429 }
00430 
00431 static void
00432 next_u32 (
00433   LLRP_tSFrameEncoder *         pEncoder,
00434   llrp_u32_t                    Value)
00435 {
00436     assert(pEncoder->iNext + 4u <= pEncoder->nBuffer);
00437 
00438     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 24u;
00439     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 16u;
00440     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 8u;
00441     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 0u;
00442 }
00443 
00444 static void
00445 next_u64 (
00446   LLRP_tSFrameEncoder *         pEncoder,
00447   llrp_u64_t                    Value)
00448 {
00449     assert(pEncoder->iNext + 8u <= pEncoder->nBuffer);
00450 
00451     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 56u;
00452     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 48u;
00453     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 40u;
00454     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 32u;
00455 
00456     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 24u;
00457     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 16u;
00458     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 8u;
00459     pEncoder->pBuffer[pEncoder->iNext++] = Value >> 0u;
00460 }
00461 
00462 
00463 static void
00464 putRequiredSubParameter (
00465   LLRP_tSEncoderStream *        pBaseEncoderStream,
00466   const LLRP_tSParameter *      pParameter,
00467   const LLRP_tSTypeDescriptor * pRefType)
00468 {
00469     LLRP_tSFrameEncoderStream * pEncoderStream =
00470                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00471 
00472     if(NULL == pParameter)
00473     {
00474         LLRP_tSFrameEncoder *   pEncoder = pEncoderStream->pEncoder;
00475         LLRP_tSErrorDetails *   pError = &pEncoder->encoderHdr.ErrorDetails;
00476 
00477         LLRP_Error_missingParameter(pError, pRefType);
00478         return;
00479     }
00480 
00481     nestSubParameter(pEncoderStream, pParameter);
00482 }
00483 
00484 static void
00485 putOptionalSubParameter (
00486   LLRP_tSEncoderStream *        pBaseEncoderStream,
00487   const LLRP_tSParameter *      pParameter,
00488   const LLRP_tSTypeDescriptor * pRefType)
00489 {
00490     LLRP_tSFrameEncoderStream * pEncoderStream =
00491                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00492 
00493     nestSubParameter(pEncoderStream, pParameter);
00494 }
00495 
00496 static void
00497 putRequiredSubParameterList (
00498   LLRP_tSEncoderStream *        pBaseEncoderStream,
00499   const LLRP_tSParameter *      pParameterList,
00500   const LLRP_tSTypeDescriptor * pRefType)
00501 {
00502     LLRP_tSFrameEncoderStream * pEncoderStream =
00503                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00504     const LLRP_tSParameter *    pParameter;
00505 
00506     if(NULL == pParameterList)
00507     {
00508         LLRP_tSFrameEncoder *   pEncoder = pEncoderStream->pEncoder;
00509         LLRP_tSErrorDetails *   pError = &pEncoder->encoderHdr.ErrorDetails;
00510 
00511         LLRP_Error_missingParameter(pError, pRefType);
00512         return;
00513     }
00514 
00515     for(
00516         pParameter = pParameterList;
00517         NULL != pParameter;
00518         pParameter = pParameter->pNextSubParameter)
00519     {
00520         nestSubParameter(pEncoderStream, pParameter);
00521     }
00522 }
00523 
00524 static void
00525 putOptionalSubParameterList (
00526   LLRP_tSEncoderStream *        pBaseEncoderStream,
00527   const LLRP_tSParameter *      pParameterList,
00528   const LLRP_tSTypeDescriptor * pRefType)
00529 {
00530     LLRP_tSFrameEncoderStream * pEncoderStream =
00531                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00532     const LLRP_tSParameter *    pParameter;
00533 
00534     for(
00535         pParameter = pParameterList;
00536         NULL != pParameter;
00537         pParameter = pParameter->pNextSubParameter)
00538     {
00539         nestSubParameter(pEncoderStream, pParameter);
00540     }
00541 }
00542 
00543 static void
00544 put_u8 (
00545   LLRP_tSEncoderStream *        pBaseEncoderStream,
00546   const llrp_u8_t               Value,
00547   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00548 {
00549     LLRP_tSFrameEncoderStream * pEncoderStream =
00550                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00551     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00552 
00553     if(checkAvailable(pEncoderStream, 1u, pFieldDescriptor))
00554     {
00555         next_u8(pEncoder, Value);
00556     }
00557 }
00558 
00559 static void
00560 put_s8 (
00561   LLRP_tSEncoderStream *        pBaseEncoderStream,
00562   const llrp_s8_t               Value,
00563   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00564 {
00565     LLRP_tSFrameEncoderStream * pEncoderStream =
00566                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00567     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00568 
00569     if(checkAvailable(pEncoderStream, 1u, pFieldDescriptor))
00570     {
00571         next_u8(pEncoder, Value);
00572     }
00573 }
00574 
00575 static void
00576 put_u8v (
00577   LLRP_tSEncoderStream *        pBaseEncoderStream,
00578   const llrp_u8v_t              Value,
00579   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00580 {
00581     LLRP_tSFrameEncoderStream * pEncoderStream =
00582                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00583     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00584     unsigned int                nByte;
00585 
00586     nByte = 2u + Value.nValue * 1u;
00587 
00588     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00589     {
00590         unsigned int            i;
00591 
00592         next_u16(pEncoder, Value.nValue);
00593         for(i = 0; i < Value.nValue; i++)
00594         {
00595             next_u8(pEncoder, Value.pValue[i]);
00596         }
00597     }
00598 }
00599 
00600 static void
00601 put_s8v (
00602   LLRP_tSEncoderStream *        pBaseEncoderStream,
00603   const llrp_s8v_t              Value,
00604   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00605 {
00606     LLRP_tSFrameEncoderStream * pEncoderStream =
00607                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00608     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00609     unsigned int                nByte;
00610 
00611     nByte = 2u + Value.nValue * 1u;
00612 
00613     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00614     {
00615         unsigned int            i;
00616 
00617         next_u16(pEncoder, Value.nValue);
00618         for(i = 0; i < Value.nValue; i++)
00619         {
00620             next_u8(pEncoder, Value.pValue[i]);
00621         }
00622     }
00623 }
00624 
00625 static void
00626 put_u16 (
00627   LLRP_tSEncoderStream *        pBaseEncoderStream,
00628   const llrp_u16_t              Value,
00629   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00630 {
00631     LLRP_tSFrameEncoderStream * pEncoderStream =
00632                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00633     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00634 
00635     if(checkAvailable(pEncoderStream, 2u, pFieldDescriptor))
00636     {
00637         next_u16(pEncoder, Value);
00638     }
00639 }
00640 
00641 static void
00642 put_s16 (
00643   LLRP_tSEncoderStream *        pBaseEncoderStream,
00644   const llrp_s16_t              Value,
00645   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00646 {
00647     LLRP_tSFrameEncoderStream * pEncoderStream =
00648                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00649     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00650 
00651     if(checkAvailable(pEncoderStream, 2u, pFieldDescriptor))
00652     {
00653         next_u16(pEncoder, Value);
00654     }
00655 }
00656 
00657 static void
00658 put_u16v (
00659   LLRP_tSEncoderStream *        pBaseEncoderStream,
00660   const llrp_u16v_t             Value,
00661   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00662 {
00663     LLRP_tSFrameEncoderStream * pEncoderStream =
00664                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00665     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00666     unsigned int                nByte;
00667 
00668     nByte = 2u + Value.nValue * 2u;
00669 
00670     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00671     {
00672         unsigned int            i;
00673 
00674         next_u16(pEncoder, Value.nValue);
00675         for(i = 0; i < Value.nValue; i++)
00676         {
00677             next_u16(pEncoder, Value.pValue[i]);
00678         }
00679     }
00680 }
00681 
00682 static void
00683 put_s16v (
00684   LLRP_tSEncoderStream *        pBaseEncoderStream,
00685   const llrp_s16v_t             Value,
00686   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00687 {
00688     LLRP_tSFrameEncoderStream * pEncoderStream =
00689                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00690     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00691     unsigned int                nByte;
00692 
00693     nByte = 2u + Value.nValue * 2u;
00694 
00695     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00696     {
00697         unsigned int            i;
00698 
00699         next_u16(pEncoder, Value.nValue);
00700         for(i = 0; i < Value.nValue; i++)
00701         {
00702             next_u16(pEncoder, Value.pValue[i]);
00703         }
00704     }
00705 }
00706 
00707 static void
00708 put_u32 (
00709   LLRP_tSEncoderStream *        pBaseEncoderStream,
00710   const llrp_u32_t              Value,
00711   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00712 {
00713     LLRP_tSFrameEncoderStream * pEncoderStream =
00714                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00715     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00716 
00717     if(checkAvailable(pEncoderStream, 4u, pFieldDescriptor))
00718     {
00719         next_u32(pEncoder, Value);
00720     }
00721 }
00722 
00723 static void
00724 put_s32 (
00725   LLRP_tSEncoderStream *        pBaseEncoderStream,
00726   const llrp_s32_t              Value,
00727   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00728 {
00729     LLRP_tSFrameEncoderStream * pEncoderStream =
00730                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00731     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00732 
00733     if(checkAvailable(pEncoderStream, 4u, pFieldDescriptor))
00734     {
00735         next_u32(pEncoder, Value);
00736     }
00737 }
00738 
00739 static void
00740 put_u32v (
00741   LLRP_tSEncoderStream *        pBaseEncoderStream,
00742   const llrp_u32v_t             Value,
00743   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00744 {
00745     LLRP_tSFrameEncoderStream * pEncoderStream =
00746                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00747     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00748     unsigned int                nByte;
00749 
00750     nByte = 2u + Value.nValue * 4u;
00751 
00752     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00753     {
00754         unsigned int            i;
00755 
00756         next_u16(pEncoder, Value.nValue);
00757         for(i = 0; i < Value.nValue; i++)
00758         {
00759             next_u32(pEncoder, Value.pValue[i]);
00760         }
00761     }
00762 }
00763 
00764 static void
00765 put_s32v (
00766   LLRP_tSEncoderStream *        pBaseEncoderStream,
00767   const llrp_s32v_t             Value,
00768   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00769 {
00770     LLRP_tSFrameEncoderStream * pEncoderStream =
00771                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00772     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00773     unsigned int                nByte;
00774 
00775     nByte = 2u + Value.nValue * 4u;
00776 
00777     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00778     {
00779         unsigned int            i;
00780 
00781         next_u16(pEncoder, Value.nValue);
00782         for(i = 0; i < Value.nValue; i++)
00783         {
00784             next_u32(pEncoder, Value.pValue[i]);
00785         }
00786     }
00787 }
00788 
00789 static void
00790 put_u64 (
00791   LLRP_tSEncoderStream *        pBaseEncoderStream,
00792   const llrp_u64_t              Value,
00793   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00794 {
00795     LLRP_tSFrameEncoderStream * pEncoderStream =
00796                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00797     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00798 
00799     if(checkAvailable(pEncoderStream, 8u, pFieldDescriptor))
00800     {
00801         next_u64(pEncoder, Value);
00802     }
00803 }
00804 
00805 static void
00806 put_s64 (
00807   LLRP_tSEncoderStream *        pBaseEncoderStream,
00808   const llrp_s64_t              Value,
00809   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00810 {
00811     LLRP_tSFrameEncoderStream * pEncoderStream =
00812                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00813     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00814 
00815     if(checkAvailable(pEncoderStream, 8u, pFieldDescriptor))
00816     {
00817         next_u64(pEncoder, Value);
00818     }
00819 }
00820 
00821 static void
00822 put_u64v (
00823   LLRP_tSEncoderStream *        pBaseEncoderStream,
00824   const llrp_u64v_t             Value,
00825   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00826 {
00827     LLRP_tSFrameEncoderStream * pEncoderStream =
00828                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00829     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00830     unsigned int                nByte;
00831 
00832     nByte = 2u + Value.nValue * 8u;
00833 
00834     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00835     {
00836         unsigned int            i;
00837 
00838         next_u16(pEncoder, Value.nValue);
00839         for(i = 0; i < Value.nValue; i++)
00840         {
00841             next_u64(pEncoder, Value.pValue[i]);
00842         }
00843     }
00844 }
00845 
00846 static void
00847 put_s64v (
00848   LLRP_tSEncoderStream *        pBaseEncoderStream,
00849   const llrp_s64v_t             Value,
00850   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00851 {
00852     LLRP_tSFrameEncoderStream * pEncoderStream =
00853                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00854     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00855     unsigned int                nByte;
00856 
00857     nByte = 2u + Value.nValue * 8u;
00858 
00859     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00860     {
00861         unsigned int            i;
00862 
00863         next_u16(pEncoder, Value.nValue);
00864         for(i = 0; i < Value.nValue; i++)
00865         {
00866             next_u64(pEncoder, Value.pValue[i]);
00867         }
00868     }
00869 }
00870 
00871 static void
00872 put_u1 (
00873   LLRP_tSEncoderStream *        pBaseEncoderStream,
00874   const llrp_u1_t               Value,
00875   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00876 {
00877     LLRP_tSFrameEncoderStream * pEncoderStream =
00878                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00879 
00880     putBitField(pEncoderStream, 1u, Value, pFieldDescriptor);
00881 }
00882 
00883 static void
00884 put_u1v (
00885   LLRP_tSEncoderStream *        pBaseEncoderStream,
00886   const llrp_u1v_t              Value,
00887   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00888 {
00889     LLRP_tSFrameEncoderStream * pEncoderStream =
00890                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00891     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00892     unsigned int                nByte = (Value.nBit + 7u) / 8u;
00893 
00894     if(checkAvailable(pEncoderStream, 2u + nByte, pFieldDescriptor))
00895     {
00896         unsigned int            i;
00897 
00898         next_u16(pEncoder, Value.nBit);
00899         for(i = 0; i < nByte; i++)
00900         {
00901             next_u8(pEncoder, Value.pValue[i]);
00902         }
00903     }
00904 }
00905 
00906 static void
00907 put_u2 (
00908   LLRP_tSEncoderStream *        pBaseEncoderStream,
00909   const llrp_u2_t               Value,
00910   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00911 {
00912     LLRP_tSFrameEncoderStream * pEncoderStream =
00913                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00914 
00915     putBitField(pEncoderStream, 2u, Value, pFieldDescriptor);
00916 
00917 }
00918 
00919 static void
00920 put_u96 (
00921   LLRP_tSEncoderStream *        pBaseEncoderStream,
00922   const llrp_u96_t              Value,
00923   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00924 {
00925     LLRP_tSFrameEncoderStream * pEncoderStream =
00926                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00927     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00928 
00929     if(checkAvailable(pEncoderStream, 12u, pFieldDescriptor))
00930     {
00931         unsigned int            i;
00932 
00933         for(i = 0; i < 12; i++)
00934         {
00935             next_u8(pEncoder, Value.aValue[i]);
00936         }
00937     }
00938 }
00939 
00940 static void
00941 put_utf8v (
00942   LLRP_tSEncoderStream *        pBaseEncoderStream,
00943   const llrp_utf8v_t            Value,
00944   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00945 {
00946     LLRP_tSFrameEncoderStream * pEncoderStream =
00947                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00948     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00949     unsigned int                nByte;
00950 
00951     nByte = 2u + Value.nValue * 1u;
00952 
00953     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00954     {
00955         unsigned int            i;
00956 
00957         next_u16(pEncoder, Value.nValue);
00958         for(i = 0; i < Value.nValue; i++)
00959         {
00960             next_u8(pEncoder, Value.pValue[i]);
00961         }
00962     }
00963 }
00964 
00965 static void
00966 put_bytesToEnd (
00967   LLRP_tSEncoderStream *        pBaseEncoderStream,
00968   const llrp_bytesToEnd_t       Value,
00969   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00970 {
00971     LLRP_tSFrameEncoderStream * pEncoderStream =
00972                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
00973     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
00974     unsigned int                nByte;
00975 
00976     nByte = Value.nValue * 1u;
00977 
00978     if(checkAvailable(pEncoderStream, nByte, pFieldDescriptor))
00979     {
00980         unsigned int            i;
00981 
00982         for(i = 0; i < Value.nValue; i++)
00983         {
00984             next_u8(pEncoder, Value.pValue[i]);
00985         }
00986     }
00987 }
00988 
00989 
00990 static void
00991 put_e1 (
00992   LLRP_tSEncoderStream *        pBaseEncoderStream,
00993   const int                     Value,
00994   const LLRP_tSFieldDescriptor *pFieldDescriptor)
00995 {
00996     put_u1(pBaseEncoderStream, (llrp_u1_t)Value, pFieldDescriptor);
00997 }
00998 
00999 static void
01000 put_e2 (
01001   LLRP_tSEncoderStream *        pBaseEncoderStream,
01002   const int                     Value,
01003   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01004 {
01005     put_u2(pBaseEncoderStream, (llrp_u2_t)Value, pFieldDescriptor);
01006 }
01007 
01008 static void
01009 put_e8 (
01010   LLRP_tSEncoderStream *        pBaseEncoderStream,
01011   const int                     Value,
01012   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01013 {
01014     put_u8(pBaseEncoderStream, (llrp_u8_t)Value, pFieldDescriptor);
01015 }
01016 
01017 static void
01018 put_e16 (
01019   LLRP_tSEncoderStream *        pBaseEncoderStream,
01020   const int                     Value,
01021   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01022 {
01023     put_u16(pBaseEncoderStream, (llrp_u16_t)Value, pFieldDescriptor);
01024 }
01025 
01026 static void
01027 put_e32 (
01028   LLRP_tSEncoderStream *        pBaseEncoderStream,
01029   const int                     Value,
01030   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01031 {
01032     put_u32(pBaseEncoderStream, (llrp_u32_t)Value, pFieldDescriptor);
01033 }
01034 
01035 static void
01036 put_e8v (
01037   LLRP_tSEncoderStream *        pBaseEncoderStream,
01038   const llrp_u8v_t              Value,
01039   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01040 {
01041     put_u8v(pBaseEncoderStream, Value, pFieldDescriptor);
01042 }
01043 
01044 static void
01045 put_reserved (
01046   LLRP_tSEncoderStream *        pBaseEncoderStream,
01047   unsigned int                  nBits)
01048 {
01049     LLRP_tSFrameEncoderStream * pEncoderStream =
01050                             (LLRP_tSFrameEncoderStream *) pBaseEncoderStream;
01051     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
01052     LLRP_tSErrorDetails *       pError = &pEncoder->encoderHdr.ErrorDetails;
01053 
01054     if(LLRP_RC_OK != pError->eResultCode)
01055     {
01056         return;
01057     }
01058 
01059     while(nBits > 0)
01060     {
01061         unsigned int            Step = 7u & nBits;
01062 
01063         if(0 != pEncoder->nBitFieldResid)
01064         {
01065             if(Step != pEncoder->nBitFieldResid)
01066             {
01067                 pError->eResultCode = LLRP_RC_UnalignedReservedBits;
01068                 pError->pWhatStr    = "unaligned reserved bits";
01069                 pError->pRefType    = pEncoderStream->pRefType;
01070                 pError->pRefField   = NULL;
01071                 pError->OtherDetail = pEncoder->iNext;
01072                 return;
01073             }
01074 
01075             next_u8(pEncoder, pEncoder->BitFieldBuffer);
01076             nBits -= Step;
01077             pEncoder->BitFieldBuffer = 0;
01078             pEncoder->nBitFieldResid = 0;
01079         }
01080         else
01081         {
01082             if(0 != Step)
01083             {
01084                 pError->eResultCode = LLRP_RC_UnalignedReservedBits;
01085                 pError->pWhatStr    = "unaligned reserved bits";
01086                 pError->pRefType    = pEncoderStream->pRefType;
01087                 pError->pRefField   = NULL;
01088                 pError->OtherDetail = pEncoder->iNext;
01089                 return;
01090             }
01091 
01092             if(pEncoder->iNext >= pEncoder->nBuffer)
01093             {
01094                 pError->eResultCode = LLRP_RC_ReservedBitsOverrun;
01095                 pError->pWhatStr    = "overrun at reserved bits";
01096                 pError->pRefType    = pEncoderStream->pRefType;
01097                 pError->pRefField   = NULL;
01098                 pError->OtherDetail = pEncoder->iNext;
01099                 return;
01100             }
01101 
01102             next_u8(pEncoder, 0);
01103             nBits -= 8;
01104         }
01105     }
01106 }
01107 
01108 static void
01109 streamConstruct_outermost (
01110   LLRP_tSFrameEncoderStream *   pEncoderStream,
01111   LLRP_tSFrameEncoder *         pEncoder)
01112 {
01113     memset(pEncoderStream, 0, sizeof *pEncoderStream);
01114     pEncoderStream->encoderStreamHdr.pEncoderStreamOps =
01115                                 &s_FrameEncoderStreamOps;
01116 
01117     pEncoderStream->pEncoder                = pEncoder;
01118     pEncoderStream->pEnclosingEncoderStream = NULL;
01119     pEncoderStream->pRefType                = NULL;
01120     pEncoderStream->iBegin                  = pEncoder->iNext;
01121 }
01122 
01123 
01124 static void
01125 streamConstruct_nested (
01126   LLRP_tSFrameEncoderStream *   pEncoderStream,
01127   LLRP_tSFrameEncoderStream *   pEnclosingEncoderStream)
01128 {
01129     LLRP_tSFrameEncoder *       pEncoder;
01130 
01131     pEncoder = pEnclosingEncoderStream->pEncoder;
01132 
01133     memset(pEncoderStream, 0, sizeof *pEncoderStream);
01134     pEncoderStream->encoderStreamHdr.pEncoderStreamOps =
01135                                 &s_FrameEncoderStreamOps;
01136 
01137     pEncoderStream->pEncoder                = pEncoder;
01138     pEncoderStream->pEnclosingEncoderStream = pEnclosingEncoderStream;
01139     pEncoderStream->pRefType                = NULL;
01140     pEncoderStream->iBegin                  = pEncoder->iNext;
01141 }
01142 
01143 static void
01144 putElement (
01145   LLRP_tSFrameEncoderStream *   pEncoderStream,
01146   const LLRP_tSElement *        pElement)
01147 {
01148     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
01149     LLRP_tSErrorDetails *       pError = &pEncoder->encoderHdr.ErrorDetails;
01150     LLRP_tSEncoderStream *      pBaseEncoderStream =
01151                                         &pEncoderStream->encoderStreamHdr;
01152     const LLRP_tSTypeDescriptor *pRefType = pElement->pType;
01153     enum { MSG, TLV, TV, CUST_MSG, CUST_TLV } eFormat;
01154     unsigned int                nLength;
01155     unsigned char *             pLen;
01156 
01157     if(LLRP_RC_OK != pError->eResultCode)
01158     {
01159         return;
01160     }
01161 
01162     pEncoderStream->pRefType = pRefType;
01163 
01164     if(pRefType->bIsMessage)
01165     {
01166         eFormat = (NULL == pRefType->pVendorDescriptor) ? MSG : CUST_MSG;
01167     }
01168     else if(NULL == pRefType->pVendorDescriptor && 128 > pRefType->TypeNum)
01169     {
01170         /* TV parameter, never custom, no length */
01171         eFormat = TV;
01172     }
01173     else
01174     {
01175         /* TLV parameter */
01176         eFormat = (NULL == pRefType->pVendorDescriptor) ? TLV : CUST_TLV;
01177     }
01178 
01179     /*
01180      * Format the element header. The length part, if one,
01181      * is a place holder and back-patched later.
01182      */
01183     switch(eFormat)
01184     {
01185     default:
01186         assert(0);
01187         break;
01188 
01189     case MSG:
01190         {
01191             llrp_u16_t          VersType;
01192 
01193             VersType = (1u << 10u) | pRefType->TypeNum;
01194             put_u16(pBaseEncoderStream, VersType,
01195                 &LLRP_g_fdMessageHeader_Type);
01196             put_u32(pBaseEncoderStream, 0,
01197                 &LLRP_g_fdMessageHeader_Length);
01198             put_u32(pBaseEncoderStream,
01199                 ((const LLRP_tSMessage *)pElement)->MessageID,
01200                 &LLRP_g_fdMessageHeader_MessageID);
01201         }
01202         break;
01203 
01204     case CUST_MSG:
01205         {
01206             llrp_u16_t          VersType;
01207 
01208             /* Custom message */
01209             VersType = (1u << 10u) | 1023u;
01210             put_u16(pBaseEncoderStream, VersType,
01211                 &LLRP_g_fdMessageHeader_Type);
01212             /* length is a placeholder */
01213             put_u32(pBaseEncoderStream, 0,
01214                 &LLRP_g_fdMessageHeader_Length);
01215             put_u32(pBaseEncoderStream,
01216                 ((const LLRP_tSMessage *)pElement)->MessageID,
01217                 &LLRP_g_fdMessageHeader_MessageID);
01218             put_u32(pBaseEncoderStream,
01219                 pRefType->pVendorDescriptor->VendorID,
01220                 &LLRP_g_fdMessageHeader_VendorPEN);
01221             put_u8(pBaseEncoderStream, pRefType->TypeNum,
01222                 &LLRP_g_fdMessageHeader_Subtype);
01223         }
01224         break;
01225 
01226     case TV:
01227         put_u8(pBaseEncoderStream, pRefType->TypeNum | 0x80u,
01228             &LLRP_g_fdParameterHeader_TVType);
01229         break;
01230 
01231     case TLV:
01232         /* Standard parameter */
01233         put_u16(pBaseEncoderStream, pRefType->TypeNum,
01234             &LLRP_g_fdParameterHeader_TLVType);
01235         put_u16(pBaseEncoderStream, 0,
01236             &LLRP_g_fdParameterHeader_TLVLength);
01237         break;
01238 
01239     case CUST_TLV:
01240         /* Custom parameter */
01241         put_u16(pBaseEncoderStream, 1023u,
01242             &LLRP_g_fdParameterHeader_TLVType);
01243         put_u16(pBaseEncoderStream, 0,
01244             &LLRP_g_fdParameterHeader_TLVLength);
01245         put_u32(pBaseEncoderStream,
01246                 pRefType->pVendorDescriptor->VendorID,
01247             &LLRP_g_fdParameterHeader_VendorPEN);
01248         put_u32(pBaseEncoderStream, pRefType->TypeNum,
01249             &LLRP_g_fdParameterHeader_Subtype);
01250         break;
01251     }
01252 
01253     /*
01254      * If something went wrong preparing the element header,
01255      * just give up now.
01256      */
01257     if(LLRP_RC_OK != pError->eResultCode)
01258     {
01259         return;
01260     }
01261 
01262     pRefType->pfEncode(pElement, pBaseEncoderStream);
01263 
01264     nLength = pEncoder->iNext - pEncoderStream->iBegin;
01265     pLen = &pEncoder->pBuffer[pEncoderStream->iBegin];
01266 
01267     switch(eFormat)
01268     {
01269     default:
01270         assert(0);
01271         break;
01272 
01273     case MSG:
01274     case CUST_MSG:
01275         assert(nLength >= 10);
01276         pLen += 2;
01277         pLen[0] = nLength >> 24u;
01278         pLen[1] = nLength >> 16u;
01279         pLen[2] = nLength >> 8u;
01280         pLen[3] = nLength >> 0u;
01281         break;
01282 
01283     case TV:
01284         break;
01285 
01286     case TLV:
01287     case CUST_TLV:
01288         assert(nLength >= 4);
01289         pLen += 2;
01290         pLen[0] = nLength >> 8u;
01291         pLen[1] = nLength >> 0u;
01292         break;
01293     }
01294 }
01295 
01296 static void
01297 nestSubParameter (
01298   LLRP_tSFrameEncoderStream *   pEncoderStream,
01299   const LLRP_tSParameter *      pParameter)
01300 {
01301     LLRP_tSFrameEncoderStream   NestEncoderStream;
01302 
01303     if(NULL == pParameter)
01304     {
01305         return;
01306     }
01307 
01308     streamConstruct_nested(&NestEncoderStream, pEncoderStream);
01309 
01310     putElement(&NestEncoderStream, (LLRP_tSElement *)pParameter);
01311 }
01312 
01313 static llrp_bool_t
01314 checkAvailable (
01315   LLRP_tSFrameEncoderStream *   pEncoderStream,
01316   unsigned int                  nByte,
01317   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01318 {
01319     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
01320     LLRP_tSErrorDetails *       pError = &pEncoder->encoderHdr.ErrorDetails;
01321 
01322     if(LLRP_RC_OK != pError->eResultCode)
01323     {
01324         return FALSE;
01325     }
01326 
01327     if(pEncoder->iNext + nByte > pEncoder->nBuffer)
01328     {
01329         pError->eResultCode = LLRP_RC_FieldOverrun;
01330         pError->pWhatStr    = "overrun at field";
01331         pError->pRefType    = pEncoderStream->pRefType;
01332         pError->pRefField   = pFieldDescriptor;
01333         pError->OtherDetail = pEncoder->iNext;
01334         return FALSE;
01335     }
01336 
01337     if(0 != pEncoder->nBitFieldResid)
01338     {
01339         pError->eResultCode = LLRP_RC_UnalignedBitField;
01340         pError->pWhatStr    = "unalign/incomplete bit field";
01341         pError->pRefType    = pEncoderStream->pRefType;
01342         pError->pRefField   = pFieldDescriptor;
01343         pError->OtherDetail = pEncoder->iNext;
01344         return FALSE;
01345     }
01346 
01347     return TRUE;
01348 }
01349 
01350 static llrp_bool_t
01351 putBitField (
01352   LLRP_tSFrameEncoderStream *   pEncoderStream,
01353   unsigned int                  nBit,
01354   unsigned int                  Value,  
01355   const LLRP_tSFieldDescriptor *pFieldDescriptor)
01356 {
01357     LLRP_tSFrameEncoder *       pEncoder = pEncoderStream->pEncoder;
01358     LLRP_tSErrorDetails *       pError = &pEncoder->encoderHdr.ErrorDetails;
01359 
01360     if(0 == pEncoder->nBitFieldResid)
01361     {
01362         if(!checkAvailable(pEncoderStream, 1u, pFieldDescriptor))
01363         {
01364             return FALSE;
01365         }
01366         pEncoder->BitFieldBuffer = 0;
01367         pEncoder->nBitFieldResid = 8u;
01368     }
01369 
01370     if(pEncoder->nBitFieldResid < nBit)
01371     {
01372         pError->eResultCode = LLRP_RC_UnalignedBitField;
01373         pError->pWhatStr    = "unalign/incomplete bit field";
01374         pError->pRefType    = pEncoderStream->pRefType;
01375         pError->pRefField   = pFieldDescriptor;
01376         pError->OtherDetail = pEncoder->iNext;
01377         return FALSE;
01378     }
01379 
01380     pEncoder->nBitFieldResid -= nBit;
01381 
01382     Value &= (1u << nBit) - 1u;
01383 
01384     pEncoder->BitFieldBuffer |= Value << pEncoder->nBitFieldResid;
01385 
01386     if(0 == pEncoder->nBitFieldResid)
01387     {
01388         next_u8(pEncoder, pEncoder->BitFieldBuffer);
01389         pEncoder->BitFieldBuffer = 0;
01390         pEncoder->nBitFieldResid = 0;
01391 
01392     }
01393 
01394     return TRUE;
01395 }


thingmagic_rfid
Author(s): Brian Bingham
autogenerated on Thu May 16 2019 03:01:23