Committer:
jinu
Date:
Thu Feb 09 06:08:17 2017 +0000
Revision:
0:6ba9b94b8997
NRF51 serialization libraries for mDot

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jinu 0:6ba9b94b8997 1 /* Copyright (c) Nordic Semiconductor ASA
jinu 0:6ba9b94b8997 2 * All rights reserved.
jinu 0:6ba9b94b8997 3 *
jinu 0:6ba9b94b8997 4 * Redistribution and use in source and binary forms, with or without modification,
jinu 0:6ba9b94b8997 5 * are permitted provided that the following conditions are met:
jinu 0:6ba9b94b8997 6 *
jinu 0:6ba9b94b8997 7 * 1. Redistributions of source code must retain the above copyright notice, this
jinu 0:6ba9b94b8997 8 * list of conditions and the following disclaimer.
jinu 0:6ba9b94b8997 9 *
jinu 0:6ba9b94b8997 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this
jinu 0:6ba9b94b8997 11 * list of conditions and the following disclaimer in the documentation and/or
jinu 0:6ba9b94b8997 12 * other materials provided with the distribution.
jinu 0:6ba9b94b8997 13 *
jinu 0:6ba9b94b8997 14 * 3. Neither the name of Nordic Semiconductor ASA nor the names of other
jinu 0:6ba9b94b8997 15 * contributors to this software may be used to endorse or promote products
jinu 0:6ba9b94b8997 16 * derived from this software without specific prior written permission.
jinu 0:6ba9b94b8997 17 *
jinu 0:6ba9b94b8997 18 * 4. This software must only be used in a processor manufactured by Nordic
jinu 0:6ba9b94b8997 19 * Semiconductor ASA, or in a processor manufactured by a third party that
jinu 0:6ba9b94b8997 20 * is used in combination with a processor manufactured by Nordic Semiconductor.
jinu 0:6ba9b94b8997 21 *
jinu 0:6ba9b94b8997 22 *
jinu 0:6ba9b94b8997 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
jinu 0:6ba9b94b8997 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
jinu 0:6ba9b94b8997 25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
jinu 0:6ba9b94b8997 26 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
jinu 0:6ba9b94b8997 27 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
jinu 0:6ba9b94b8997 28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
jinu 0:6ba9b94b8997 29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
jinu 0:6ba9b94b8997 30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
jinu 0:6ba9b94b8997 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
jinu 0:6ba9b94b8997 32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
jinu 0:6ba9b94b8997 33 */
jinu 0:6ba9b94b8997 34
jinu 0:6ba9b94b8997 35 #ifndef BLE_SERIALIZATION_H__
jinu 0:6ba9b94b8997 36 #define BLE_SERIALIZATION_H__
jinu 0:6ba9b94b8997 37
jinu 0:6ba9b94b8997 38 #include "nordic_common.h"
jinu 0:6ba9b94b8997 39 #include "ble_rpc_defines.h"
jinu 0:6ba9b94b8997 40 #include "nrf_error.h"
jinu 0:6ba9b94b8997 41 #include <stdint.h>
jinu 0:6ba9b94b8997 42 #include <stddef.h>
jinu 0:6ba9b94b8997 43
jinu 0:6ba9b94b8997 44 #define LOW16(a) ((uint16_t)((a & 0x0000FFFF) >> 0))
jinu 0:6ba9b94b8997 45 #define HIGH16(a) ((uint16_t)((a & 0xFFFF0000) >> 16))
jinu 0:6ba9b94b8997 46
jinu 0:6ba9b94b8997 47 //lint -esym(516,__INTADDR__) Symbol '__INTADDR__()' has arg. type conflict
jinu 0:6ba9b94b8997 48 //lint -esym(628,__INTADDR__) no argument information provided for function '__INTADDR__()'
jinu 0:6ba9b94b8997 49
jinu 0:6ba9b94b8997 50
jinu 0:6ba9b94b8997 51 #define SER_CMD_REQ_HEADER_SIZE (BLE_OP_CODE_SIZE) /**< Size of command request header. */
jinu 0:6ba9b94b8997 52 #define SER_CMD_RSP_HEADER_SIZE (BLE_OP_CODE_SIZE + RPC_ERR_CODE_SIZE) /**< Size of command response header. */
jinu 0:6ba9b94b8997 53
jinu 0:6ba9b94b8997 54 #define SER_EVT_ID_SIZE 2 /**< Size of event ID field. */
jinu 0:6ba9b94b8997 55 #define SER_EVT_ID_POS 0 /**< Position of event ID field. */
jinu 0:6ba9b94b8997 56 #define SER_EVT_HEADER_SIZE (SER_EVT_ID_SIZE) /**< Size of event header. */
jinu 0:6ba9b94b8997 57 #define SER_EVT_CONN_HANDLE_SIZE 2 /**< Size of event connection handler. */
jinu 0:6ba9b94b8997 58
jinu 0:6ba9b94b8997 59 #define SER_POS_CMD_DATA (SER_CMD_REQ_HEADER_SIZE) /**< Position of command data. */
jinu 0:6ba9b94b8997 60 #define SER_POS_RSP_STATUS_CODE (BLE_OP_CODE_SIZE) /**< Position of command response code. */
jinu 0:6ba9b94b8997 61
jinu 0:6ba9b94b8997 62 /**< Enable SER_ASSERT<*> assserts */
jinu 0:6ba9b94b8997 63 #define SER_ASSERTS_ENABLED 1
jinu 0:6ba9b94b8997 64
jinu 0:6ba9b94b8997 65 /**< Returns with error code if expr is not true. It is used for checking error which should be
jinu 0:6ba9b94b8997 66 * checked even when SER_ASSERTS_ENABLED is not set. */
jinu 0:6ba9b94b8997 67 #define SER_ERROR_CHECK(expr, error_code) do { if (!(expr)) return (error_code); } while (0)
jinu 0:6ba9b94b8997 68
jinu 0:6ba9b94b8997 69 #ifdef SER_ASSERTS_ENABLED
jinu 0:6ba9b94b8997 70 /**< Returns with error code if expr is not true. */
jinu 0:6ba9b94b8997 71 #define SER_ASSERT(expr, error_code) SER_ERROR_CHECK(expr, error_code)
jinu 0:6ba9b94b8997 72 /**< Returns with if expr is not true. */
jinu 0:6ba9b94b8997 73 #define SER_ASSERT_VOID_RETURN(expr) do { if (!(expr)) return; } while (0)
jinu 0:6ba9b94b8997 74 /**< Returns with \ref NRF_ERROR_INVALID_LENGTH if len is not less or equal to maxlen. */
jinu 0:6ba9b94b8997 75 #define SER_ASSERT_LENGTH_LEQ(len, maxlen) \
jinu 0:6ba9b94b8997 76 SER_ASSERT((len) <= (maxlen), NRF_ERROR_INVALID_LENGTH)
jinu 0:6ba9b94b8997 77 /**< Returns with \ref NRF_ERROR_INVALID_LENGTH if actual_len is not equal to expected_len. */
jinu 0:6ba9b94b8997 78 #define SER_ASSERT_LENGTH_EQ(actual_len, expected_len) \
jinu 0:6ba9b94b8997 79 SER_ASSERT((actual_len) == (expected_len), NRF_ERROR_INVALID_LENGTH)
jinu 0:6ba9b94b8997 80 /**< Returns with \ref NRF_ERROR_NULL if pointer is null. */
jinu 0:6ba9b94b8997 81 #define SER_ASSERT_NOT_NULL(ptr) SER_ASSERT((ptr) != NULL, NRF_ERROR_NULL)
jinu 0:6ba9b94b8997 82 #else
jinu 0:6ba9b94b8997 83 #define SER_ASSERT(expr, error_code)
jinu 0:6ba9b94b8997 84 #define SER_ASSERT_VOID_RETURN(expr)
jinu 0:6ba9b94b8997 85 #define SER_ASSERT_LENGTH_LEQ(len, maxlen) UNUSED_VARIABLE(maxlen)
jinu 0:6ba9b94b8997 86 #define SER_ASSERT_LENGTH_EQ(actual_len, expected_len)
jinu 0:6ba9b94b8997 87 #define SER_ASSERT_NOT_NULL(ptr)
jinu 0:6ba9b94b8997 88 #endif
jinu 0:6ba9b94b8997 89
jinu 0:6ba9b94b8997 90 /**< Maximum length of p_value in \ref ble_gattc_write_params_t. See Bluetooth 4.0 spec: 3.4.5.1 and 3.4.5.3. */
jinu 0:6ba9b94b8997 91 #define BLE_GATTC_WRITE_P_VALUE_LEN_MAX (GATT_MTU_SIZE_DEFAULT - 3)
jinu 0:6ba9b94b8997 92
jinu 0:6ba9b94b8997 93 /* See Bluetooth 4.0 spec: 3.4.4.7. */
jinu 0:6ba9b94b8997 94 #define BLE_GATTC_HANDLE_COUNT_LEN_MAX (GATT_MTU_SIZE_DEFAULT - 1) / 2
jinu 0:6ba9b94b8997 95
jinu 0:6ba9b94b8997 96 /**< Generic command response status code encoder. */
jinu 0:6ba9b94b8997 97 uint32_t ser_ble_cmd_rsp_status_code_enc(uint8_t op_code,
jinu 0:6ba9b94b8997 98 uint32_t command_status,
jinu 0:6ba9b94b8997 99 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 100 uint32_t * const p_buf_len);
jinu 0:6ba9b94b8997 101
jinu 0:6ba9b94b8997 102 /**< Generic command response resykt code decoder. */
jinu 0:6ba9b94b8997 103 uint32_t ser_ble_cmd_rsp_result_code_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 104 uint32_t * const p_pos,
jinu 0:6ba9b94b8997 105 uint32_t packet_len,
jinu 0:6ba9b94b8997 106 uint8_t op_code,
jinu 0:6ba9b94b8997 107 uint32_t * const p_result_code);
jinu 0:6ba9b94b8997 108
jinu 0:6ba9b94b8997 109 /**< Generic command response resykt code decoder. */
jinu 0:6ba9b94b8997 110 uint32_t ser_ble_cmd_rsp_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 111 uint32_t packet_len,
jinu 0:6ba9b94b8997 112 uint8_t op_code,
jinu 0:6ba9b94b8997 113 uint32_t * const p_result_code);
jinu 0:6ba9b94b8997 114
jinu 0:6ba9b94b8997 115 /**@brief Function for safe encoding an uint16 value.
jinu 0:6ba9b94b8997 116 *
jinu 0:6ba9b94b8997 117 * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 118 *
jinu 0:6ba9b94b8997 119 * @param[in] p_field Uint16 value to be encoded.
jinu 0:6ba9b94b8997 120 * @param[out] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 121 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 122 * @param[in,out] p_index \c in: Index to start of uint16 value in buffer.
jinu 0:6ba9b94b8997 123 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 124 *
jinu 0:6ba9b94b8997 125 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 126 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 127 */
jinu 0:6ba9b94b8997 128 uint32_t uint16_t_enc(const void * const p_field,
jinu 0:6ba9b94b8997 129 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 130 uint32_t buf_len,
jinu 0:6ba9b94b8997 131 uint32_t * const p_index);
jinu 0:6ba9b94b8997 132
jinu 0:6ba9b94b8997 133 /**@brief Function for safe decoding an uint16 value.
jinu 0:6ba9b94b8997 134 *
jinu 0:6ba9b94b8997 135 * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 136 *
jinu 0:6ba9b94b8997 137 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 138 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 139 * @param[in,out] p_index \c in: Index to start of uint16 value in buffer.
jinu 0:6ba9b94b8997 140 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 141 * @param[out] p_field Pointer to the location where uint16 value will be decoded.
jinu 0:6ba9b94b8997 142 */
jinu 0:6ba9b94b8997 143 uint32_t uint16_t_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 144 uint32_t buf_len,
jinu 0:6ba9b94b8997 145 uint32_t * const p_index,
jinu 0:6ba9b94b8997 146 void * p_field);
jinu 0:6ba9b94b8997 147
jinu 0:6ba9b94b8997 148 /**@brief Function for safe decoding an uint16 value.
jinu 0:6ba9b94b8997 149 *
jinu 0:6ba9b94b8997 150 * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 151 *
jinu 0:6ba9b94b8997 152 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 153 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 154 * @param[in,out] index \c in: Index to start of uint16 value in buffer.
jinu 0:6ba9b94b8997 155 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 156 * @param[out] value Decoded uint16 value.
jinu 0:6ba9b94b8997 157 */
jinu 0:6ba9b94b8997 158 void uint16_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 159 uint32_t packet_len,
jinu 0:6ba9b94b8997 160 uint32_t * const index,
jinu 0:6ba9b94b8997 161 uint16_t * const value);
jinu 0:6ba9b94b8997 162
jinu 0:6ba9b94b8997 163 /**@brief Function for safe encoding an uint18 value.
jinu 0:6ba9b94b8997 164 *
jinu 0:6ba9b94b8997 165 * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 166 *
jinu 0:6ba9b94b8997 167 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 168 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 169 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 170 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 171 * @param[out] p_field Pointer to uint8 value to be encoded.
jinu 0:6ba9b94b8997 172 *
jinu 0:6ba9b94b8997 173 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 174 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 175 */
jinu 0:6ba9b94b8997 176 uint32_t uint8_t_enc(const void * const p_field,
jinu 0:6ba9b94b8997 177 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 178 uint32_t buf_len,
jinu 0:6ba9b94b8997 179 uint32_t * const p_index);
jinu 0:6ba9b94b8997 180
jinu 0:6ba9b94b8997 181 /**@brief Function for safe decoding an uint8 value.
jinu 0:6ba9b94b8997 182 *
jinu 0:6ba9b94b8997 183 * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 184 *
jinu 0:6ba9b94b8997 185 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 186 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 187 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 188 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 189 * @param[out] p_field Pointer to the location for decoded uint8 value.
jinu 0:6ba9b94b8997 190 *
jinu 0:6ba9b94b8997 191 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 192 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 193 */
jinu 0:6ba9b94b8997 194 uint32_t uint8_t_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 195 uint32_t buf_len,
jinu 0:6ba9b94b8997 196 uint32_t * const p_index,
jinu 0:6ba9b94b8997 197 void * p_field);
jinu 0:6ba9b94b8997 198
jinu 0:6ba9b94b8997 199 /**@brief Function for safe decoding an uint8 value.
jinu 0:6ba9b94b8997 200 *
jinu 0:6ba9b94b8997 201 * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 202 *
jinu 0:6ba9b94b8997 203 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 204 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 205 * @param[in,out] index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 206 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 207 * @param[out] value Decoded uint8 value.
jinu 0:6ba9b94b8997 208 */
jinu 0:6ba9b94b8997 209 void uint8_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 210 uint32_t packet_len,
jinu 0:6ba9b94b8997 211 uint32_t * const index,
jinu 0:6ba9b94b8997 212 uint8_t * const value);
jinu 0:6ba9b94b8997 213
jinu 0:6ba9b94b8997 214 /**@brief Function for safe decoding an uint18 value.
jinu 0:6ba9b94b8997 215 *
jinu 0:6ba9b94b8997 216 * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 217 *
jinu 0:6ba9b94b8997 218 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 219 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 220 * @param[in,out] index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 221 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 222 * @param[out] value Decoded uint8 value.
jinu 0:6ba9b94b8997 223 */
jinu 0:6ba9b94b8997 224 void int8_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 225 uint32_t packet_len,
jinu 0:6ba9b94b8997 226 uint32_t * const index,
jinu 0:6ba9b94b8997 227 int8_t * const value);
jinu 0:6ba9b94b8997 228
jinu 0:6ba9b94b8997 229 /**@brief Function for safe encoding variable length field encoded as length(8bit)+data.
jinu 0:6ba9b94b8997 230 *
jinu 0:6ba9b94b8997 231 * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 232 *
jinu 0:6ba9b94b8997 233 * @param[out] p_data Pointer to data to encode.
jinu 0:6ba9b94b8997 234 * @param[in] dlen Length of data to encode (0-255).
jinu 0:6ba9b94b8997 235 * @param[out] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 236 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 237 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 238 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 239 *
jinu 0:6ba9b94b8997 240 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 241 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 242 */
jinu 0:6ba9b94b8997 243 uint32_t len8data_enc(uint8_t const * const p_data,
jinu 0:6ba9b94b8997 244 uint8_t const dlen,
jinu 0:6ba9b94b8997 245 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 246 uint32_t buf_len,
jinu 0:6ba9b94b8997 247 uint32_t * const p_index);
jinu 0:6ba9b94b8997 248
jinu 0:6ba9b94b8997 249 /**@brief Function for safe decoding variable length field encoded as length(8bit)+data.
jinu 0:6ba9b94b8997 250 *
jinu 0:6ba9b94b8997 251 * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 252 *
jinu 0:6ba9b94b8997 253 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 254 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 255 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 256 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 257 * @param[out] pp_data Pointer to pointer to decoded data (p_data is set to NULL in
jinu 0:6ba9b94b8997 258 * case data is not present in the buffer).
jinu 0:6ba9b94b8997 259 * @param[out] p_len Decoded length (0-255).
jinu 0:6ba9b94b8997 260 *
jinu 0:6ba9b94b8997 261 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 262 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 263 */
jinu 0:6ba9b94b8997 264 uint32_t len8data_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 265 uint32_t buf_len,
jinu 0:6ba9b94b8997 266 uint32_t * const p_index,
jinu 0:6ba9b94b8997 267 uint8_t * * const pp_data,
jinu 0:6ba9b94b8997 268 uint8_t * const p_len);
jinu 0:6ba9b94b8997 269
jinu 0:6ba9b94b8997 270 /**@brief Function for safe encoding variable length field encoded as length(16bit)+data.
jinu 0:6ba9b94b8997 271 *
jinu 0:6ba9b94b8997 272 * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 273 * It is possible that provided p_data is NULL in that case length is encoded and it's followed by
jinu 0:6ba9b94b8997 274 * RPC_BLE_FIELD_NOT_PRESENT flag. RPC_BLE_FIELD_PRESENT flag preceeds data otherwise.
jinu 0:6ba9b94b8997 275 *
jinu 0:6ba9b94b8997 276 * @param[in] p_data Data to encode.
jinu 0:6ba9b94b8997 277 * @param[in] dlen Input data length (16bit).
jinu 0:6ba9b94b8997 278 * @param[in] p_buf Pointer to the beginning of the output buffer.
jinu 0:6ba9b94b8997 279 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 280 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 281 * \c out: Index in buffer to first byte after the encoded data.
jinu 0:6ba9b94b8997 282 *
jinu 0:6ba9b94b8997 283 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 284 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 285 */
jinu 0:6ba9b94b8997 286 uint32_t len16data_enc(uint8_t const * const p_data,
jinu 0:6ba9b94b8997 287 uint16_t const dlen,
jinu 0:6ba9b94b8997 288 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 289 uint32_t buf_len,
jinu 0:6ba9b94b8997 290 uint32_t * const p_index);
jinu 0:6ba9b94b8997 291
jinu 0:6ba9b94b8997 292 /**@brief Function for safe decoding variable length field encoded as length(16bit)+data.
jinu 0:6ba9b94b8997 293 *
jinu 0:6ba9b94b8997 294 * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 295 * Encoded data consist of length field, presence flag and conditional data (present only is presence flag
jinu 0:6ba9b94b8997 296 * is set). p_data pointer is required to be not NULL only is presence flag is set.
jinu 0:6ba9b94b8997 297 *
jinu 0:6ba9b94b8997 298 * @param[in] p_buf Pointer to the beginning of the input buffer.
jinu 0:6ba9b94b8997 299 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 300 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 301 * \c out: Index in buffer to first byte after the decoded data.
jinu 0:6ba9b94b8997 302 * @param[in] pp_data Pointer to pointer to decoded data.
jinu 0:6ba9b94b8997 303 * @param[in] p_dlen data length (16bit).
jinu 0:6ba9b94b8997 304 *
jinu 0:6ba9b94b8997 305 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 306 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 307 */
jinu 0:6ba9b94b8997 308 uint32_t len16data_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 309 uint32_t buf_len,
jinu 0:6ba9b94b8997 310 uint32_t * const p_index,
jinu 0:6ba9b94b8997 311 uint8_t * * const pp_data,
jinu 0:6ba9b94b8997 312 uint16_t * const p_dlen);
jinu 0:6ba9b94b8997 313
jinu 0:6ba9b94b8997 314
jinu 0:6ba9b94b8997 315 /**@brief Function for safe encoding of uint16 table with a given element count.
jinu 0:6ba9b94b8997 316 *
jinu 0:6ba9b94b8997 317 * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 318 * It is possible that provided p_data is NULL in that case length is encoded and it's followed by
jinu 0:6ba9b94b8997 319 * RPC_BLE_FIELD_NOT_PRESENT flag. RPC_BLE_FIELD_PRESENT flag precedes data otherwise.
jinu 0:6ba9b94b8997 320 *
jinu 0:6ba9b94b8997 321 * @param[in] p_data Data table to encode.
jinu 0:6ba9b94b8997 322 * @param[in] count Table element count.
jinu 0:6ba9b94b8997 323 * @param[in] p_buf Pointer to the beginning of the output buffer.
jinu 0:6ba9b94b8997 324 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 325 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 326 * \c out: Index in buffer to first byte after the encoded data.
jinu 0:6ba9b94b8997 327 *
jinu 0:6ba9b94b8997 328 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 329 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 330 */
jinu 0:6ba9b94b8997 331
jinu 0:6ba9b94b8997 332 uint32_t count16_cond_data16_enc(uint16_t const * const p_data,
jinu 0:6ba9b94b8997 333 uint16_t const count,
jinu 0:6ba9b94b8997 334 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 335 uint32_t buf_len,
jinu 0:6ba9b94b8997 336 uint32_t * const p_index);
jinu 0:6ba9b94b8997 337
jinu 0:6ba9b94b8997 338 /**@brief Function for safe decoding of uint16 table with a given element count.
jinu 0:6ba9b94b8997 339 *
jinu 0:6ba9b94b8997 340 * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 341 * It is possible that provided p_data is NULL in that case length is encoded and it's followed by
jinu 0:6ba9b94b8997 342 * RPC_BLE_FIELD_NOT_PRESENT flag. RPC_BLE_FIELD_PRESENT flag precedes data otherwise.
jinu 0:6ba9b94b8997 343 *
jinu 0:6ba9b94b8997 344 * @param[in] p_buf Pointer to the beginning of the output buffer.
jinu 0:6ba9b94b8997 345 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 346 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 347 * \c out: Index in buffer to first byte after the encoded data.
jinu 0:6ba9b94b8997 348 * @param[in] pp_data Pointer to pointer to the table to encode.
jinu 0:6ba9b94b8997 349 * @param[in,out] p_count Pointer to table element count - initialised with max count
jinu 0:6ba9b94b8997 350 *
jinu 0:6ba9b94b8997 351 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 352 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 353 * @retval NRF_ERROR_DATA_SIZE Decoding failure. Initial count is smaller than actual.
jinu 0:6ba9b94b8997 354 */
jinu 0:6ba9b94b8997 355
jinu 0:6ba9b94b8997 356 uint32_t count16_cond_data16_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 357 uint32_t buf_len,
jinu 0:6ba9b94b8997 358 uint32_t * const p_index,
jinu 0:6ba9b94b8997 359 uint16_t * * const pp_data,
jinu 0:6ba9b94b8997 360 uint16_t * const p_count);
jinu 0:6ba9b94b8997 361
jinu 0:6ba9b94b8997 362
jinu 0:6ba9b94b8997 363 /**@brief Function for safe decoding of variable length field encoded as length(16bit)+data.
jinu 0:6ba9b94b8997 364 *
jinu 0:6ba9b94b8997 365 * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 366 * Encoded data consist of presence flag, optional length field, second presence flag and optional data.
jinu 0:6ba9b94b8997 367 *
jinu 0:6ba9b94b8997 368 *
jinu 0:6ba9b94b8997 369 * @param[in] p_buf Pointer to the beginning of the input buffer.
jinu 0:6ba9b94b8997 370 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 371 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 372 * \c out: Index in buffer to first byte after the decoded data.
jinu 0:6ba9b94b8997 373 * @param[out] pp_data Pointer to decoded data.
jinu 0:6ba9b94b8997 374 * @param[out] pp_len data length (16bit).
jinu 0:6ba9b94b8997 375 *
jinu 0:6ba9b94b8997 376 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 377 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 378 */
jinu 0:6ba9b94b8997 379
jinu 0:6ba9b94b8997 380 uint32_t cond_len16_cond_data_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 381 uint32_t buf_len,
jinu 0:6ba9b94b8997 382 uint32_t * const p_index,
jinu 0:6ba9b94b8997 383 uint8_t * * const pp_data,
jinu 0:6ba9b94b8997 384 uint16_t * * const pp_len);
jinu 0:6ba9b94b8997 385
jinu 0:6ba9b94b8997 386
jinu 0:6ba9b94b8997 387 /**@brief Command response encoder - replacement of - ser_ble_cmd_rsp_status_code_enc
jinu 0:6ba9b94b8997 388 * with layout aligned to the rest of encoder functions
jinu 0:6ba9b94b8997 389 *
jinu 0:6ba9b94b8997 390 * @param[in] op_code Operation code - see BLE_GAP_SVCS
jinu 0:6ba9b94b8997 391 * @param[in] return_code nRF Error Code.
jinu 0:6ba9b94b8997 392 * @param[in] p_buff pointer to the start of pointer to decoded data.
jinu 0:6ba9b94b8997 393 * @param[in,out] p_buff_len \c in: size of buffer
jinu 0:6ba9b94b8997 394 * \c out: used bytes in buffer
jinu 0:6ba9b94b8997 395 * @param[in,out] p_buff_len \c in: initial offset in buffer
jinu 0:6ba9b94b8997 396 * \c out: final offset in buffer
jinu 0:6ba9b94b8997 397 *
jinu 0:6ba9b94b8997 398 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 399 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 400 * @retval NRF_ERROR_NULL Invalid pointer
jinu 0:6ba9b94b8997 401 */
jinu 0:6ba9b94b8997 402 uint32_t op_status_enc(uint8_t op_code,
jinu 0:6ba9b94b8997 403 uint32_t return_code,
jinu 0:6ba9b94b8997 404 uint8_t * const p_buff,
jinu 0:6ba9b94b8997 405 uint32_t * const p_buff_len,
jinu 0:6ba9b94b8997 406 uint32_t * const p_index);
jinu 0:6ba9b94b8997 407
jinu 0:6ba9b94b8997 408 /**@brief command response encoder with conditional 16bit field
jinu 0:6ba9b94b8997 409 *
jinu 0:6ba9b94b8997 410 * @param[in] op_code Operation code - see BLE_GAP_SVCS
jinu 0:6ba9b94b8997 411 * @param[in] return_code nRF Error Code.
jinu 0:6ba9b94b8997 412 * @param[in] value optional 16bit field encoded for return code == NRF_SUCCESS
jinu 0:6ba9b94b8997 413 * @param[in] p_buff pointer to the start of pointer to decoded data.
jinu 0:6ba9b94b8997 414 * @param[in,out] p_buff_len \c in: size of buffer
jinu 0:6ba9b94b8997 415 * \c out: used bytes in buffer
jinu 0:6ba9b94b8997 416 * @param[in,out] p_buff_len \c in: initial offset in buffer
jinu 0:6ba9b94b8997 417 * \c out: final offset in buffer
jinu 0:6ba9b94b8997 418 *
jinu 0:6ba9b94b8997 419 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 420 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 421 * @retval NRF_ERROR_NULL Invalid pointer
jinu 0:6ba9b94b8997 422 */
jinu 0:6ba9b94b8997 423
jinu 0:6ba9b94b8997 424 uint32_t op_status_cond_uint16_enc(uint8_t op_code,
jinu 0:6ba9b94b8997 425 uint32_t return_code,
jinu 0:6ba9b94b8997 426 uint16_t value,
jinu 0:6ba9b94b8997 427 uint8_t * const p_buff,
jinu 0:6ba9b94b8997 428 uint32_t * const p_buff_len,
jinu 0:6ba9b94b8997 429 uint32_t * const p_index);
jinu 0:6ba9b94b8997 430
jinu 0:6ba9b94b8997 431 /**@brief Function for safe encoding a buffer of known size.
jinu 0:6ba9b94b8997 432 *
jinu 0:6ba9b94b8997 433 * Safe encoding of a buffer. Encoder assumes that size is known to the decoder and it is not
jinu 0:6ba9b94b8997 434 * encoded here. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 435 *
jinu 0:6ba9b94b8997 436 * @param[in] p_data Data to encode.
jinu 0:6ba9b94b8997 437 * @param[in] dlen Input data length (16bit).
jinu 0:6ba9b94b8997 438 * @param[in] p_buf Pointer to the beginning of the output buffer.
jinu 0:6ba9b94b8997 439 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 440 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 441 * \c out: Index in buffer to first byte after the encoded data.
jinu 0:6ba9b94b8997 442 *
jinu 0:6ba9b94b8997 443 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 444 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 445 */
jinu 0:6ba9b94b8997 446 uint32_t buf_enc(uint8_t const * const p_data,
jinu 0:6ba9b94b8997 447 uint16_t const dlen,
jinu 0:6ba9b94b8997 448 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 449 uint32_t buf_len,
jinu 0:6ba9b94b8997 450 uint32_t * const p_index);
jinu 0:6ba9b94b8997 451
jinu 0:6ba9b94b8997 452 /**@brief Function for safe decoding a buffer of known size.
jinu 0:6ba9b94b8997 453 *
jinu 0:6ba9b94b8997 454 * Safe decoding of buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 455 * Encoded data consist of presence flag and conditional data (present only is presence flag
jinu 0:6ba9b94b8997 456 * is set). p_data pointer is required to be not NULL only is presence flag is set. Length is provided
jinu 0:6ba9b94b8997 457 * as an input to the function.
jinu 0:6ba9b94b8997 458 *
jinu 0:6ba9b94b8997 459 * @param[in] p_buf Pointer to the beginning of the input buffer.
jinu 0:6ba9b94b8997 460 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 461 * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
jinu 0:6ba9b94b8997 462 * \c out: Index in buffer to first byte after the decoded data.
jinu 0:6ba9b94b8997 463 * @param[in] pp_data Pointer to pointer to decoded data.
jinu 0:6ba9b94b8997 464 * @param[in] data_len Length of buffer for decoded data (16bit).
jinu 0:6ba9b94b8997 465 * @param[in] dlen Length of data to decode (16bit).
jinu 0:6ba9b94b8997 466 *
jinu 0:6ba9b94b8997 467 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 468 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 469 */
jinu 0:6ba9b94b8997 470 uint32_t buf_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 471 uint32_t buf_len,
jinu 0:6ba9b94b8997 472 uint32_t * const p_index,
jinu 0:6ba9b94b8997 473 uint8_t * * const pp_data,
jinu 0:6ba9b94b8997 474 uint16_t data_len,
jinu 0:6ba9b94b8997 475 uint16_t dlen);
jinu 0:6ba9b94b8997 476
jinu 0:6ba9b94b8997 477 /**@brief Function for safe encoding an uint32 value.
jinu 0:6ba9b94b8997 478 *
jinu 0:6ba9b94b8997 479 * Safe decoding of an uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 480 *
jinu 0:6ba9b94b8997 481 * @param[in] p_field uint32 value to be encoded.
jinu 0:6ba9b94b8997 482 * @param[out] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 483 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 484 * @param[in,out] p_index \c in: Index to start of uint32 value in buffer.
jinu 0:6ba9b94b8997 485 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 486 *
jinu 0:6ba9b94b8997 487 * @return NRF_SUCCESS Fields decoded successfully.
jinu 0:6ba9b94b8997 488 * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
jinu 0:6ba9b94b8997 489 */
jinu 0:6ba9b94b8997 490 uint32_t uint32_t_enc(void const * const p_field,
jinu 0:6ba9b94b8997 491 uint8_t * const p_buf,
jinu 0:6ba9b94b8997 492 uint32_t buf_len,
jinu 0:6ba9b94b8997 493 uint32_t * const p_index);
jinu 0:6ba9b94b8997 494
jinu 0:6ba9b94b8997 495 /**@brief Function for safe decoding an uint32 value.
jinu 0:6ba9b94b8997 496 *
jinu 0:6ba9b94b8997 497 * Safe decoding of an uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
jinu 0:6ba9b94b8997 498 *
jinu 0:6ba9b94b8997 499 * @param[in] p_buf Buffer containing the value.
jinu 0:6ba9b94b8997 500 * @param[in] buf_len Size of buffer.
jinu 0:6ba9b94b8997 501 * @param[in,out] p_index \c in: Index to start of uint32 value in buffer.
jinu 0:6ba9b94b8997 502 * \c out: Index in buffer to first byte after the decoded value.
jinu 0:6ba9b94b8997 503 * @param[out] value Decoded uint32 value.
jinu 0:6ba9b94b8997 504 */
jinu 0:6ba9b94b8997 505 uint32_t uint32_t_dec(uint8_t const * const p_buf,
jinu 0:6ba9b94b8997 506 uint32_t buf_len,
jinu 0:6ba9b94b8997 507 uint32_t * const p_index,
jinu 0:6ba9b94b8997 508 void * p_field);
jinu 0:6ba9b94b8997 509
jinu 0:6ba9b94b8997 510 #endif
jinu 0:6ba9b94b8997 511
jinu 0:6ba9b94b8997 512
jinu 0:6ba9b94b8997 513