The KPN SenML library helps you create and parse senml documents in both json and cbor format. The library can be used for sending sensor data and receiving actuator commands.

Committer:
kpniot
Date:
Sat May 19 17:35:20 2018 +0000
Revision:
0:a9259748d982
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kpniot 0:a9259748d982 1 /* _ __ ____ _ _
kpniot 0:a9259748d982 2 * | |/ / | _ \ | \ | |
kpniot 0:a9259748d982 3 * | ' / | |_) | | \| |
kpniot 0:a9259748d982 4 * | . \ | __/ | |\ |
kpniot 0:a9259748d982 5 * |_|\_\ |_| |_| \_|
kpniot 0:a9259748d982 6 *
kpniot 0:a9259748d982 7 * (c) 2018 KPN
kpniot 0:a9259748d982 8 * License: MIT License.
kpniot 0:a9259748d982 9 * Author: Jan Bogaerts
kpniot 0:a9259748d982 10 *
kpniot 0:a9259748d982 11 * cbor parsing and rendering header
kpniot 0:a9259748d982 12 */
kpniot 0:a9259748d982 13
kpniot 0:a9259748d982 14
kpniot 0:a9259748d982 15 #ifndef SENMLCBOR
kpniot 0:a9259748d982 16 #define SENMLCBOR
kpniot 0:a9259748d982 17
kpniot 0:a9259748d982 18 #include <Stream.h>
kpniot 0:a9259748d982 19 #include <stddef.h>
kpniot 0:a9259748d982 20
kpniot 0:a9259748d982 21 #define SENML_BVER_LABEL -1
kpniot 0:a9259748d982 22 #define SENML_CBOR_BN_LABEL -2
kpniot 0:a9259748d982 23 #define SENML_CBOR_BT_LABEL -3
kpniot 0:a9259748d982 24 #define SENML_CBOR_BU_LABEL -4
kpniot 0:a9259748d982 25 #define SENML_CBOR_BV_LABEL -5
kpniot 0:a9259748d982 26 #define SENML_CBOR_BS_LABEL -16
kpniot 0:a9259748d982 27 #define SENML_CBOR_N_LABEL 0
kpniot 0:a9259748d982 28 #define SENML_CBOR_U_LABEL 1
kpniot 0:a9259748d982 29 #define SENML_CBOR_V_LABEL 2
kpniot 0:a9259748d982 30 #define SENML_CBOR_VS_LABEL 3
kpniot 0:a9259748d982 31 #define SENML_CBOR_VB_LABEL 4
kpniot 0:a9259748d982 32 #define SENML_CBOR_S_LABEL 5
kpniot 0:a9259748d982 33 #define SENML_CBOR_T_LABEL 6
kpniot 0:a9259748d982 34 #define SENML_CBOR_UT_LABEL 7
kpniot 0:a9259748d982 35 #define SENML_CBOR_VD_LABEL 8
kpniot 0:a9259748d982 36
kpniot 0:a9259748d982 37 #define CBOR_TYPE_MASK 0xE0 /* top 3 bits */
kpniot 0:a9259748d982 38 #define CBOR_INFO_MASK 0x1F /* low 5 bits */
kpniot 0:a9259748d982 39
kpniot 0:a9259748d982 40 /* Major types (cf. section 2.1) */
kpniot 0:a9259748d982 41 /* Major type 0: Unsigned integers */
kpniot 0:a9259748d982 42 #define CBOR_UINT8_FOLLOWS 24 /* 0x18 */
kpniot 0:a9259748d982 43 #define CBOR_UINT16_FOLLOWS 25 /* 0x19 */
kpniot 0:a9259748d982 44 #define CBOR_UINT32_FOLLOWS 26 /* 0x1a */
kpniot 0:a9259748d982 45 #define CBOR_UINT64_FOLLOWS 27 /* 0x1b */
kpniot 0:a9259748d982 46
kpniot 0:a9259748d982 47 #define CBOR_BYTE_FOLLOWS 24 /* indicator that the next byte is part of this item */
kpniot 0:a9259748d982 48
kpniot 0:a9259748d982 49 /* Jump Table for Initial Byte (cf. table 5) */
kpniot 0:a9259748d982 50 #define CBOR_UINT 0x00 /* type 0 */
kpniot 0:a9259748d982 51 #define CBOR_NEGINT 0x20 /* type 1 */
kpniot 0:a9259748d982 52 #define CBOR_BYTES 0x40 /* type 2 */
kpniot 0:a9259748d982 53 #define CBOR_TEXT 0x60 /* type 3 */
kpniot 0:a9259748d982 54 #define CBOR_ARRAY 0x80 /* type 4 */
kpniot 0:a9259748d982 55 #define CBOR_MAP 0xA0 /* type 5 */
kpniot 0:a9259748d982 56 #define CBOR_TAG 0xC0 /* type 6 */
kpniot 0:a9259748d982 57 #define CBOR_7 0xE0 /* type 7 (float and other types) */
kpniot 0:a9259748d982 58
kpniot 0:a9259748d982 59 #define CBOR_VAR_FOLLOWS 31 /* 0x1f */
kpniot 0:a9259748d982 60
kpniot 0:a9259748d982 61 /* Major type 6: Semantic tagging */
kpniot 0:a9259748d982 62 #define CBOR_DATETIME_STRING_FOLLOWS 0
kpniot 0:a9259748d982 63 #define CBOR_DATETIME_EPOCH_FOLLOWS 1
kpniot 0:a9259748d982 64
kpniot 0:a9259748d982 65 /* Major type 7: Float and other types */
kpniot 0:a9259748d982 66 #define CBOR_FALSE (CBOR_7 | 20)
kpniot 0:a9259748d982 67 #define CBOR_TRUE (CBOR_7 | 21)
kpniot 0:a9259748d982 68 #define CBOR_NULL (CBOR_7 | 22)
kpniot 0:a9259748d982 69 #define CBOR_UNDEFINED (CBOR_7 | 23)
kpniot 0:a9259748d982 70 /* CBOR_BYTE_FOLLOWS == 24 */
kpniot 0:a9259748d982 71 #define CBOR_FLOAT16 (CBOR_7 | 25)
kpniot 0:a9259748d982 72 #define CBOR_FLOAT32 (CBOR_7 | 26)
kpniot 0:a9259748d982 73 #define CBOR_FLOAT64 (CBOR_7 | 27)
kpniot 0:a9259748d982 74 #define CBOR_BREAK (CBOR_7 | 31)
kpniot 0:a9259748d982 75
kpniot 0:a9259748d982 76 #define CBOR_TYPE (peekChar() & CBOR_TYPE_MASK)
kpniot 0:a9259748d982 77
kpniot 0:a9259748d982 78
kpniot 0:a9259748d982 79 /**
kpniot 0:a9259748d982 80 * @brief Serialize array of length @p array_length
kpniot 0:a9259748d982 81 *
kpniot 0:a9259748d982 82 * Basic usage:
kpniot 0:a9259748d982 83 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
kpniot 0:a9259748d982 84 * cbor_serialize_array(2); // array of length 2 follows
kpniot 0:a9259748d982 85 * cbor_serialize_int(1)); // write item 1
kpniot 0:a9259748d982 86 * cbor_serialize_int(2)); // write item 2
kpniot 0:a9259748d982 87 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kpniot 0:a9259748d982 88 *
kpniot 0:a9259748d982 89 * @note You have to make sure to serialize the correct amount of items.
kpniot 0:a9259748d982 90 * If you exceed the length @p array_length, items will just be appened as normal
kpniot 0:a9259748d982 91 *
kpniot 0:a9259748d982 92 * @param[in] array_length Length of the array of items which follows
kpniot 0:a9259748d982 93 *
kpniot 0:a9259748d982 94 * @return Number of bytes written to stream @p stream
kpniot 0:a9259748d982 95 */
kpniot 0:a9259748d982 96 size_t cbor_serialize_array(size_t array_length);
kpniot 0:a9259748d982 97
kpniot 0:a9259748d982 98
kpniot 0:a9259748d982 99 /**
kpniot 0:a9259748d982 100 * @brief Serialize map of length @p map_length
kpniot 0:a9259748d982 101 *
kpniot 0:a9259748d982 102 * Basic usage:
kpniot 0:a9259748d982 103 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ {.c}
kpniot 0:a9259748d982 104 * cbor_serialize_map(2); // map of length 2 follows
kpniot 0:a9259748d982 105 * cbor_serialize_int(1)); // write key 1
kpniot 0:a9259748d982 106 * cbor_serialize_byte_string("1")); // write value 1
kpniot 0:a9259748d982 107 * cbor_serialize_int(2)); // write key 2
kpniot 0:a9259748d982 108 * cbor_serialize_byte_string("2")); // write value 2
kpniot 0:a9259748d982 109 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
kpniot 0:a9259748d982 110 *
kpniot 0:a9259748d982 111 * @param map_length Length of the map of items which follows
kpniot 0:a9259748d982 112 *
kpniot 0:a9259748d982 113 * @return Number of bytes written to stream @p stream
kpniot 0:a9259748d982 114 */
kpniot 0:a9259748d982 115 size_t cbor_serialize_map(size_t map_length);
kpniot 0:a9259748d982 116
kpniot 0:a9259748d982 117
kpniot 0:a9259748d982 118 /**
kpniot 0:a9259748d982 119 * @brief Serializes an integer
kpniot 0:a9259748d982 120 *
kpniot 0:a9259748d982 121 * @param[in] val The integer to serialize
kpniot 0:a9259748d982 122 *
kpniot 0:a9259748d982 123 * @return Number of bytes written to stream @p stream
kpniot 0:a9259748d982 124 */
kpniot 0:a9259748d982 125 size_t cbor_serialize_int(int val);
kpniot 0:a9259748d982 126
kpniot 0:a9259748d982 127
kpniot 0:a9259748d982 128 /**
kpniot 0:a9259748d982 129 * @brief Serializes a unicode string.
kpniot 0:a9259748d982 130 *
kpniot 0:a9259748d982 131 * string
kpniot 0:a9259748d982 132 * @param[out] val The zero-terminated unicode string to serialize.
kpniot 0:a9259748d982 133 *
kpniot 0:a9259748d982 134 * @return Number of bytes written to stream @p stream
kpniot 0:a9259748d982 135 */
kpniot 0:a9259748d982 136 size_t cbor_serialize_unicode_string(const char *val);
kpniot 0:a9259748d982 137
kpniot 0:a9259748d982 138
kpniot 0:a9259748d982 139 /**
kpniot 0:a9259748d982 140 * @brief Serializes a double precision floating point value
kpniot 0:a9259748d982 141 *
kpniot 0:a9259748d982 142 * @param[in] val The double to serialize
kpniot 0:a9259748d982 143 *
kpniot 0:a9259748d982 144 * @return Number of bytes written to stream @p stream
kpniot 0:a9259748d982 145 */
kpniot 0:a9259748d982 146 size_t cbor_serialize_double(double val);
kpniot 0:a9259748d982 147
kpniot 0:a9259748d982 148
kpniot 0:a9259748d982 149 /**
kpniot 0:a9259748d982 150 * @brief Serializes a boolean value
kpniot 0:a9259748d982 151 *
kpniot 0:a9259748d982 152 * @param[in] val The boolean value to serialize
kpniot 0:a9259748d982 153 *
kpniot 0:a9259748d982 154 * @return Number of bytes written to stream @p stream
kpniot 0:a9259748d982 155 */
kpniot 0:a9259748d982 156 size_t cbor_serialize_bool(bool val);
kpniot 0:a9259748d982 157
kpniot 0:a9259748d982 158 /**
kpniot 0:a9259748d982 159 * @brief Serializes a signed 64 bit value
kpniot 0:a9259748d982 160 *
kpniot 0:a9259748d982 161 * @param[in] val The 64 bit integer to serialize
kpniot 0:a9259748d982 162 *
kpniot 0:a9259748d982 163 * @return Number of bytes written to stream @p stream
kpniot 0:a9259748d982 164 */
kpniot 0:a9259748d982 165 size_t cbor_serialize_byte_string(const char *val, int length);
kpniot 0:a9259748d982 166
kpniot 0:a9259748d982 167
kpniot 0:a9259748d982 168 //read integer
kpniot 0:a9259748d982 169 size_t decode_int(uint64_t *val);
kpniot 0:a9259748d982 170
kpniot 0:a9259748d982 171 /**
kpniot 0:a9259748d982 172 * @brief Deserialize signed 64 bit values from stream to @p val
kpniot 0:a9259748d982 173 *
kpniot 0:a9259748d982 174 * @param[out] val Pointer to destination array
kpniot 0:a9259748d982 175 *
kpniot 0:a9259748d982 176 * @return Number of bytes read from @p stream
kpniot 0:a9259748d982 177 */
kpniot 0:a9259748d982 178 size_t cbor_deserialize_int64_t(int64_t *val);
kpniot 0:a9259748d982 179
kpniot 0:a9259748d982 180 /**
kpniot 0:a9259748d982 181 * @brief Deserialize unsigned 64 bit values from @p stream to @p val
kpniot 0:a9259748d982 182
kpniot 0:a9259748d982 183 * @param[out] val Pointer to destination array
kpniot 0:a9259748d982 184 *
kpniot 0:a9259748d982 185 * @return Number of bytes read from @p stream
kpniot 0:a9259748d982 186 */
kpniot 0:a9259748d982 187 size_t cbor_deserialize_uint64_t(uint64_t *val);
kpniot 0:a9259748d982 188
kpniot 0:a9259748d982 189
kpniot 0:a9259748d982 190 /**
kpniot 0:a9259748d982 191 * @brief check that the char at the current position is a break char
kpniot 0:a9259748d982 192 *
kpniot 0:a9259748d982 193 * @return 1 if all ok, 0 if no break char was found.
kpniot 0:a9259748d982 194 */
kpniot 0:a9259748d982 195 size_t cbor_at_break();
kpniot 0:a9259748d982 196
kpniot 0:a9259748d982 197 size_t cbor_deserialize_float_half(float *val);
kpniot 0:a9259748d982 198 size_t cbor_deserialize_float(float *val);
kpniot 0:a9259748d982 199 size_t cbor_deserialize_double(double *val);
kpniot 0:a9259748d982 200
kpniot 0:a9259748d982 201 #endif // SENMLCBOR
kpniot 0:a9259748d982 202
kpniot 0:a9259748d982 203
kpniot 0:a9259748d982 204
kpniot 0:a9259748d982 205
kpniot 0:a9259748d982 206
kpniot 0:a9259748d982 207
kpniot 0:a9259748d982 208
kpniot 0:a9259748d982 209