KPN IoT / senml

Fork of kpn_senml by KPN IoT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cbor.h Source File

cbor.h

00001 /*  _  __  ____    _   _ 
00002  * | |/ / |  _ \  | \ | |
00003  * | ' /  | |_) | |  \| |
00004  * | . \  |  __/  | |\  |
00005  * |_|\_\ |_|     |_| \_|
00006  * 
00007  * (c) 2018 KPN
00008  * License: MIT License.
00009  * Author: Jan Bogaerts
00010  * 
00011  * cbor parsing and rendering header
00012  */
00013 
00014 
00015 #ifndef SENMLCBOR
00016 #define SENMLCBOR
00017 
00018 #include <Stream.h>
00019 #include <stddef.h>
00020 
00021 #define SENML_BVER_LABEL    -1
00022 #define SENML_CBOR_BN_LABEL -2
00023 #define SENML_CBOR_BT_LABEL -3
00024 #define SENML_CBOR_BU_LABEL -4
00025 #define SENML_CBOR_BV_LABEL -5
00026 #define SENML_CBOR_BS_LABEL -16
00027 #define SENML_CBOR_N_LABEL   0
00028 #define SENML_CBOR_U_LABEL   1
00029 #define SENML_CBOR_V_LABEL   2
00030 #define SENML_CBOR_VS_LABEL  3
00031 #define SENML_CBOR_VB_LABEL  4
00032 #define SENML_CBOR_S_LABEL   5
00033 #define SENML_CBOR_T_LABEL   6
00034 #define SENML_CBOR_UT_LABEL  7
00035 #define SENML_CBOR_VD_LABEL  8
00036 
00037 #define CBOR_TYPE_MASK          0xE0    /* top 3 bits */
00038 #define CBOR_INFO_MASK          0x1F    /* low 5 bits */
00039 
00040 /* Major types (cf. section 2.1) */
00041 /* Major type 0: Unsigned integers */
00042 #define CBOR_UINT8_FOLLOWS      24      /* 0x18 */
00043 #define CBOR_UINT16_FOLLOWS     25      /* 0x19 */
00044 #define CBOR_UINT32_FOLLOWS     26      /* 0x1a */
00045 #define CBOR_UINT64_FOLLOWS     27      /* 0x1b */
00046 
00047 #define CBOR_BYTE_FOLLOWS       24      /* indicator that the next byte is part of this item */
00048 
00049 /* Jump Table for Initial Byte (cf. table 5) */
00050 #define CBOR_UINT       0x00            /* type 0 */
00051 #define CBOR_NEGINT     0x20            /* type 1 */
00052 #define CBOR_BYTES      0x40            /* type 2 */
00053 #define CBOR_TEXT       0x60            /* type 3 */
00054 #define CBOR_ARRAY      0x80            /* type 4 */
00055 #define CBOR_MAP        0xA0            /* type 5 */
00056 #define CBOR_TAG        0xC0            /* type 6 */
00057 #define CBOR_7          0xE0            /* type 7 (float and other types) */
00058 
00059 #define CBOR_VAR_FOLLOWS        31      /* 0x1f */
00060 
00061 /* Major type 6: Semantic tagging */
00062 #define CBOR_DATETIME_STRING_FOLLOWS        0
00063 #define CBOR_DATETIME_EPOCH_FOLLOWS         1
00064 
00065 /* Major type 7: Float and other types */
00066 #define CBOR_FALSE      (CBOR_7 | 20)
00067 #define CBOR_TRUE       (CBOR_7 | 21)
00068 #define CBOR_NULL       (CBOR_7 | 22)
00069 #define CBOR_UNDEFINED  (CBOR_7 | 23)
00070 /* CBOR_BYTE_FOLLOWS == 24 */
00071 #define CBOR_FLOAT16    (CBOR_7 | 25)
00072 #define CBOR_FLOAT32    (CBOR_7 | 26)
00073 #define CBOR_FLOAT64    (CBOR_7 | 27)
00074 #define CBOR_BREAK      (CBOR_7 | 31)
00075 
00076 #define CBOR_TYPE (peekChar() & CBOR_TYPE_MASK)
00077 
00078 
00079 /**
00080  * @brief Serialize array of length @p array_length
00081  *
00082  * Basic usage:
00083  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
00084  * cbor_serialize_array(2); // array of length 2 follows
00085  * cbor_serialize_int(1)); // write item 1
00086  * cbor_serialize_int(2)); // write item 2
00087  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00088  *
00089  * @note You have to make sure to serialize the correct amount of items.
00090  * If you exceed the length @p array_length, items will just be appened as normal
00091  *
00092  * @param[in]  array_length Length of the array of items which follows
00093  *
00094  * @return Number of bytes written to stream @p stream
00095  */
00096 size_t cbor_serialize_array(size_t array_length);
00097 
00098 
00099 /**
00100  * @brief Serialize map of length @p map_length
00101  *
00102  * Basic usage:
00103  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
00104  * cbor_serialize_map(2); // map of length 2 follows
00105  * cbor_serialize_int(1)); // write key 1
00106  * cbor_serialize_byte_string("1")); // write value 1
00107  * cbor_serialize_int(2)); // write key 2
00108  * cbor_serialize_byte_string("2")); // write value 2
00109  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
00110  *
00111  * @param map_length Length of the map of items which follows
00112  *
00113  * @return Number of bytes written to stream @p stream
00114  */
00115 size_t cbor_serialize_map(size_t map_length);
00116 
00117 
00118 /**
00119  * @brief Serializes an integer
00120  *
00121  * @param[in] val       The integer to serialize
00122  *
00123  * @return Number of bytes written to stream @p stream
00124  */
00125 size_t cbor_serialize_int(int val);
00126 
00127 
00128 /**
00129  * @brief Serializes a unicode string.
00130  *
00131  *                      string
00132  * @param[out] val      The zero-terminated unicode string to serialize.
00133  *
00134  * @return Number of bytes written to stream @p stream
00135  */
00136 size_t cbor_serialize_unicode_string(const char *val);
00137 
00138 
00139 /**
00140  * @brief Serializes a double precision floating point value
00141  *
00142  * @param[in] val       The double to serialize
00143  *
00144  * @return Number of bytes written to stream @p stream
00145  */
00146 size_t cbor_serialize_double(double val);
00147 
00148 
00149 /**
00150  * @brief Serializes a boolean value
00151  *
00152  * @param[in] val       The boolean value to serialize
00153  *
00154  * @return Number of bytes written to stream @p stream
00155  */
00156 size_t cbor_serialize_bool(bool val);
00157 
00158 /**
00159  * @brief Serializes a signed 64 bit value
00160  *
00161  * @param[in] val       The 64 bit integer to serialize
00162  *
00163  * @return Number of bytes written to stream @p stream
00164  */
00165 size_t cbor_serialize_byte_string(const char *val, int length);
00166 
00167 
00168 //read integer
00169 size_t decode_int(uint64_t *val);
00170 
00171 /**
00172  * @brief Deserialize signed 64 bit values from stream to @p val
00173  *
00174  * @param[out] val   Pointer to destination array
00175  *
00176  * @return Number of bytes read from @p stream
00177  */
00178 size_t cbor_deserialize_int64_t(int64_t *val);
00179 
00180 /**
00181  * @brief Deserialize unsigned 64 bit values from @p stream to @p val
00182 
00183  * @param[out] val   Pointer to destination array
00184  *
00185  * @return Number of bytes read from @p stream
00186  */
00187 size_t cbor_deserialize_uint64_t(uint64_t *val);
00188 
00189 
00190 /**
00191  *  @brief check that the char at the current position is a break char
00192  * 
00193  * @return 1 if all ok, 0 if no break char was found.
00194  */
00195 size_t cbor_at_break();
00196 
00197 size_t cbor_deserialize_float_half(float *val);
00198 size_t cbor_deserialize_float(float *val);
00199 size_t cbor_deserialize_double(double *val);
00200 
00201 #endif // SENMLCBOR
00202 
00203 
00204 
00205 
00206 
00207 
00208 
00209