Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cbor.h Source File

cbor.h

00001 #ifndef CBOR_PROTOCOL_H__
00002 #define CBOR_PROTOCOL_H__
00003 
00004 #ifdef  __cplusplus
00005 extern "C" {
00006 #endif
00007 
00008 /* The 8 major types */
00009 #define MT_UNSIGNED 0
00010 #define MT_NEGATIVE 1
00011 #define MT_BYTES    2
00012 #define MT_TEXT     3
00013 #define MT_ARRAY    4
00014 #define MT_MAP      5
00015 #define MT_TAG      6
00016 #define MT_PRIM     7
00017 
00018 /* The initial bytes resulting from those */
00019 #define IB_UNSIGNED (MT_UNSIGNED << 5)
00020 #define IB_NEGATIVE (MT_NEGATIVE << 5)
00021 #define IB_BYTES    (MT_BYTES << 5)
00022 #define IB_TEXT     (MT_TEXT << 5)
00023 #define IB_ARRAY    (MT_ARRAY << 5)
00024 #define IB_MAP      (MT_MAP << 5)
00025 #define IB_TAG      (MT_TAG << 5)
00026 #define IB_PRIM     (MT_PRIM << 5)
00027 
00028 #define IB_NEGFLAG (IB_NEGATIVE - IB_UNSIGNED)
00029 #define IB_NEGFLAG_AS_BIT(ib) ((ib) >> 5)
00030 #define IB_TEXTFLAG (IB_TEXT - IB_BYTES)
00031 
00032 #define IB_AI(ib) ((ib) & 0x1F)
00033 #define IB_MT(ib) ((ib) >> 5)
00034 
00035 /* Tag numbers handled by this implementation */
00036 #define TAG_TIME_EPOCH 1
00037 #define TAG_BIGNUM     2
00038 #define TAG_BIGNUM_NEG 3
00039 #define TAG_URI        32
00040 #define TAG_RE         35
00041 
00042 /* Initial bytes of those tag numbers */
00043 #define IB_TIME_EPOCH (IB_TAG | TAG_TIME_EPOCH)
00044 #define IB_BIGNUM     (IB_TAG | TAG_BIGNUM)
00045 #define IB_BIGNUM_NEG (IB_TAG | TAG_BIGNUM_NEG)
00046 /* TAG_URI and TAG_RE are non-immediate tags */
00047 
00048 /* Simple values handled by this implementation */
00049 #define VAL_FALSE 20
00050 #define VAL_TRUE  21
00051 #define VAL_NIL   22
00052 #define VAL_UNDEF 23
00053 
00054 /* Initial bytes of those simple values */
00055 #define IB_FALSE (IB_PRIM | VAL_FALSE)
00056 #define IB_TRUE  (IB_PRIM | VAL_TRUE)
00057 #define IB_NIL   (IB_PRIM | VAL_NIL)
00058 #define IB_UNDEF (IB_PRIM | VAL_UNDEF)
00059 
00060 /* AI values with more data in head */
00061 #define AI_1 24
00062 #define AI_2 25
00063 #define AI_4 26
00064 #define AI_8 27
00065 #define AI_INDEF 31
00066 #define IB_BREAK (IB_PRIM | AI_INDEF)
00067 /* For  */
00068 #define IB_UNUSED (IB_TAG | AI_INDEF)
00069 
00070 /* Floating point initial bytes */
00071 #define IB_FLOAT2 (IB_PRIM | AI_2)
00072 #define IB_FLOAT4 (IB_PRIM | AI_4)
00073 #define IB_FLOAT8 (IB_PRIM | AI_8)
00074 
00075 // These definitions are here because they aren't required for the public
00076 // interface, and they were quite confusing in cn-cbor.h
00077 
00078 #ifdef USE_CBOR_CONTEXT
00079 
00080 
00081 /**
00082  * Allocate enough space for 1 `cn_cbor` structure.
00083  *
00084  * @param[in]  ctx  The allocation context, or NULL for calloc.
00085  * @return          A pointer to a `cn_cbor` or NULL on failure
00086  */
00087 #define CN_CALLOC(ctx) ((ctx) && (ctx)->calloc_func) ? \
00088     (ctx)->calloc_func(1, sizeof(cn_cbor), (ctx)->context) : \
00089     calloc(1, sizeof(cn_cbor));
00090 
00091 /**
00092  * Free a
00093  * @param  free_func [description]
00094  * @return           [description]
00095  */
00096 #define CN_FREE(ptr, ctx) ((ctx) && (ctx)->free_func) ? \
00097     (ctx)->free_func((ptr), (ctx)->context) : \
00098     free((ptr));
00099 
00100 #define CN_CALLOC_CONTEXT() CN_CALLOC(cbor_context)
00101 #define CN_CBOR_FREE_CONTEXT(p) CN_FREE(p, cbor_context)
00102 
00103 // This is in fact supposed to be sizeof(bitmap[0]). since sizeof is evaluated by the compiler and not the preprocessor, using it here will mess up the BITMAP_ARRAY_SIZE define.
00104 #define BITMAP_ENTRY_IN_BITS 32 
00105 
00106  // The number of entries in the bitmap array
00107 #define BITMAP_ARRAY_SIZE (( MAX_SIZE_POOL_IN_CBORS % BITMAP_ENTRY_IN_BITS == 0 ) ?  ( MAX_SIZE_POOL_IN_CBORS / BITMAP_ENTRY_IN_BITS ) : ( ( MAX_SIZE_POOL_IN_CBORS / BITMAP_ENTRY_IN_BITS ) + 1 ))
00108 
00109  /**
00110  * The memory pool structure.
00111  * The context field of a cn_cbor_context object should point to an object of this type.
00112  */
00113 typedef struct cbor_mem_pool_ {
00114     int32_t bitmap[BITMAP_ARRAY_SIZE];
00115     uint8_t pool_size_in_cbors;
00116     cn_cbor *pool;
00117 } cbor_mem_pool;
00118 
00119 
00120 #else
00121 
00122 #define CN_CALLOC_CONTEXT() CN_CALLOC
00123 #define CN_CBOR_FREE_CONTEXT(p) CN_FREE(p)
00124 
00125 #ifndef CN_CALLOC
00126 #define CN_CALLOC calloc(1, sizeof(cn_cbor))
00127 #endif
00128 
00129 #ifndef CN_FREE
00130 #define CN_FREE free
00131 #endif
00132 
00133 #endif // USE_CBOR_CONTEXT
00134 
00135 #ifndef UNUSED_PARAM
00136 #define UNUSED_PARAM(p) ((void)&(p))
00137 #endif
00138 
00139 #ifdef  __cplusplus
00140 }
00141 #endif
00142 
00143 #endif // CBOR_PROTOCOL_H__