ble_serialization.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #ifndef BLE_SERIALIZATION_H__
  13. #define BLE_SERIALIZATION_H__
  14. #include "nordic_common.h"
  15. #include "nrf_error.h"
  16. #include <stdint.h>
  17. #include <stddef.h>
  18. /**@brief The types of serialization packets. */
  19. typedef enum
  20. {
  21. SER_PKT_TYPE_CMD = 0, /**< Command packet type. */
  22. SER_PKT_TYPE_RESP, /**< Command Response packet type. */
  23. SER_PKT_TYPE_EVT, /**< Event packet type. */
  24. SER_PKT_TYPE_DTM_CMD, /**< DTM Command packet type. */
  25. SER_PKT_TYPE_DTM_RESP, /**< DTM Response packet type. */
  26. SER_PKT_TYPE_RESET_CMD, /**< System Reset Command packet type. */
  27. SER_PKT_TYPE_MAX /**< Upper bound. */
  28. } ser_pkt_type_t;
  29. #define LOW16(a) ((uint16_t)((a & 0x0000FFFF) >> 0))
  30. #define HIGH16(a) ((uint16_t)((a & 0xFFFF0000) >> 16))
  31. //lint -esym(516,__INTADDR__) Symbol '__INTADDR__()' has arg. type conflict
  32. //lint -esym(628,__INTADDR__) no argument information provided for function '__INTADDR__()'
  33. /** Size in bytes of the Error Code field in a Command Response packet. */
  34. #define SER_ERR_CODE_SIZE 4
  35. /** Size in bytes of the Packet Type field (@ref ser_pkt_type_t). */
  36. #define SER_PKT_TYPE_SIZE 1
  37. /** Size in bytes of the Operation Code field. */
  38. #define SER_OP_CODE_SIZE 1
  39. /** Position of the Packet Type field in a serialized packet buffer. */
  40. #define SER_PKT_TYPE_POS 0
  41. /** Position of the Operation Code field in a serialized packet buffer. */
  42. #define SER_PKT_OP_CODE_POS (SER_PKT_TYPE_SIZE)
  43. /** Position of the Data in a serialized packet buffer. */
  44. #define SER_PKT_DATA_POS (SER_PKT_TYPE_SIZE + SER_OP_CODE_SIZE)
  45. /** Position of the Operation Code field in a command buffer. */
  46. #define SER_CMD_OP_CODE_POS 0
  47. /** Position of the Data in a command buffer.*/
  48. #define SER_CMD_DATA_POS (SER_OP_CODE_SIZE)
  49. /** Size of the Command header. */
  50. #define SER_CMD_HEADER_SIZE (SER_OP_CODE_SIZE)
  51. /** Size of the Command Response header. */
  52. #define SER_CMD_RSP_HEADER_SIZE (SER_OP_CODE_SIZE + SER_ERR_CODE_SIZE)
  53. /** Position of the Command Response code. */
  54. #define SER_CMD_RSP_STATUS_CODE_POS (SER_OP_CODE_SIZE)
  55. /** Size of event ID field. */
  56. #define SER_EVT_ID_SIZE 2
  57. /** Position of event ID field. */
  58. #define SER_EVT_ID_POS 0
  59. /** Size of event header. */
  60. #define SER_EVT_HEADER_SIZE (SER_EVT_ID_SIZE)
  61. /** Size of event connection handler. */
  62. #define SER_EVT_CONN_HANDLE_SIZE 2
  63. /** Position of the Op Code in the DTM command buffer.*/
  64. #define SER_DTM_CMD_OP_CODE_POS 0
  65. /** Position of the data in the DTM command buffer.*/
  66. #define SER_DTM_CMD_DATA_POS 1
  67. /** Position of the Op Code in the DTM command response buffer.*/
  68. #define SER_DTM_RESP_OP_CODE_POS 1
  69. /** Position of the status field in the DTM command response buffer.*/
  70. #define SER_DTM_RESP_STATUS_POS 2
  71. /** Value to indicate that an optional field is encoded in the serialized packet, e.g. white list.*/
  72. #define SER_FIELD_PRESENT 0x01
  73. /** Value to indicate that an optional field is not encoded in the serialized packet. */
  74. #define SER_FIELD_NOT_PRESENT 0x00
  75. /** Enable SER_ASSERT<*> assserts */
  76. #define SER_ASSERTS_ENABLED 1
  77. /** Returns with error code if expr is not true. It is used for checking error which should be
  78. * checked even when SER_ASSERTS_ENABLED is not set. */
  79. #define SER_ERROR_CHECK(expr, error_code) do { if (!(expr)) return (error_code); } while (0)
  80. #ifdef SER_ASSERTS_ENABLED
  81. /** Returns with error code if expr is not true. */
  82. #define SER_ASSERT(expr, error_code) SER_ERROR_CHECK(expr, error_code)
  83. /** Returns with if expr is not true. */
  84. #define SER_ASSERT_VOID_RETURN(expr) do { if (!(expr)) return; } while (0)
  85. /** Returns with \ref NRF_ERROR_INVALID_LENGTH if len is not less or equal to maxlen. */
  86. #define SER_ASSERT_LENGTH_LEQ(len, maxlen) \
  87. SER_ASSERT((len) <= (maxlen), NRF_ERROR_INVALID_LENGTH)
  88. /** Returns with \ref NRF_ERROR_INVALID_LENGTH if actual_len is not equal to expected_len. */
  89. #define SER_ASSERT_LENGTH_EQ(actual_len, expected_len) \
  90. SER_ASSERT((actual_len) == (expected_len), NRF_ERROR_INVALID_LENGTH)
  91. /** Returns with \ref NRF_ERROR_NULL if pointer is null. */
  92. #define SER_ASSERT_NOT_NULL(ptr) SER_ASSERT((ptr) != NULL, NRF_ERROR_NULL)
  93. #else
  94. #define SER_ASSERT(expr, error_code)
  95. #define SER_ASSERT_VOID_RETURN(expr)
  96. #define SER_ASSERT_LENGTH_LEQ(len, maxlen) UNUSED_VARIABLE(maxlen)
  97. #define SER_ASSERT_LENGTH_EQ(actual_len, expected_len)
  98. #define SER_ASSERT_NOT_NULL(ptr)
  99. #endif
  100. /** Maximum length of p_value in \ref ble_gattc_write_params_t. See Bluetooth 4.0 spec: 3.4.5.1 and
  101. * 3.4.5.3. */
  102. #define BLE_GATTC_WRITE_P_VALUE_LEN_MAX (GATT_MTU_SIZE_DEFAULT - 3)
  103. /** See Bluetooth 4.0 spec: 3.4.4.7. */
  104. #define BLE_GATTC_HANDLE_COUNT_LEN_MAX ((GATT_MTU_SIZE_DEFAULT - 1) / 2)
  105. /** Generic command response status code encoder. */
  106. uint32_t ser_ble_cmd_rsp_status_code_enc(uint8_t op_code,
  107. uint32_t command_status,
  108. uint8_t * const p_buf,
  109. uint32_t * const p_buf_len);
  110. /** Generic command response result code decoder. */
  111. uint32_t ser_ble_cmd_rsp_result_code_dec(uint8_t const * const p_buf,
  112. uint32_t * const p_pos,
  113. uint32_t packet_len,
  114. uint8_t op_code,
  115. uint32_t * const p_result_code);
  116. /** Generic command response decoder. */
  117. uint32_t ser_ble_cmd_rsp_dec(uint8_t const * const p_buf,
  118. uint32_t packet_len,
  119. uint8_t op_code,
  120. uint32_t * const p_result_code);
  121. /**@brief Function for safe encoding an uint16 value.
  122. *
  123. * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  124. *
  125. * @param[in] p_field Uint16 value to be encoded.
  126. * @param[out] p_buf Buffer containing the value.
  127. * @param[in] buf_len Size of buffer.
  128. * @param[in,out] p_index \c in: Index to start of uint16 value in buffer.
  129. * \c out: Index in buffer to first byte after the decoded value.
  130. *
  131. * @return NRF_SUCCESS Fields decoded successfully.
  132. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  133. */
  134. uint32_t uint16_t_enc(const void * const p_field,
  135. uint8_t * const p_buf,
  136. uint32_t buf_len,
  137. uint32_t * const p_index);
  138. /**@brief Function for safe decoding an uint16 value.
  139. *
  140. * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  141. *
  142. * @param[in] p_buf Buffer containing the value.
  143. * @param[in] buf_len Size of buffer.
  144. * @param[in,out] p_index \c in: Index to start of uint16 value in buffer.
  145. * \c out: Index in buffer to first byte after the decoded value.
  146. * @param[out] p_field Pointer to the location where uint16 value will be decoded.
  147. */
  148. uint32_t uint16_t_dec(uint8_t const * const p_buf,
  149. uint32_t buf_len,
  150. uint32_t * const p_index,
  151. void * p_field);
  152. /**@brief Function for safe decoding an uint16 value.
  153. *
  154. * Safe decoding of an uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  155. *
  156. * @param[in] p_buf Buffer containing the value.
  157. * @param[in] buf_len Size of buffer.
  158. * @param[in,out] index \c in: Index to start of uint16 value in buffer.
  159. * \c out: Index in buffer to first byte after the decoded value.
  160. * @param[out] value Decoded uint16 value.
  161. */
  162. void uint16_dec(uint8_t const * const p_buf,
  163. uint32_t packet_len,
  164. uint32_t * const index,
  165. uint16_t * const value);
  166. /**@brief Function for safe encoding an uint18 value.
  167. *
  168. * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  169. *
  170. * @param[in] p_buf Buffer containing the value.
  171. * @param[in] buf_len Size of buffer.
  172. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  173. * \c out: Index in buffer to first byte after the decoded value.
  174. * @param[out] p_field Pointer to uint8 value to be encoded.
  175. *
  176. * @return NRF_SUCCESS Fields decoded successfully.
  177. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  178. */
  179. uint32_t uint8_t_enc(const void * const p_field,
  180. uint8_t * const p_buf,
  181. uint32_t buf_len,
  182. uint32_t * const p_index);
  183. /**@brief Function for safe decoding an uint8 value.
  184. *
  185. * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  186. *
  187. * @param[in] p_buf Buffer containing the value.
  188. * @param[in] buf_len Size of buffer.
  189. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  190. * \c out: Index in buffer to first byte after the decoded value.
  191. * @param[out] p_field Pointer to the location for decoded uint8 value.
  192. *
  193. * @return NRF_SUCCESS Fields decoded successfully.
  194. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  195. */
  196. uint32_t uint8_t_dec(uint8_t const * const p_buf,
  197. uint32_t buf_len,
  198. uint32_t * const p_index,
  199. void * p_field);
  200. /**@brief Function for safe decoding an uint8 value.
  201. *
  202. * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  203. *
  204. * @param[in] p_buf Buffer containing the value.
  205. * @param[in] buf_len Size of buffer.
  206. * @param[in,out] index \c in: Index to start of uint8 value in buffer.
  207. * \c out: Index in buffer to first byte after the decoded value.
  208. * @param[out] value Decoded uint8 value.
  209. */
  210. void uint8_dec(uint8_t const * const p_buf,
  211. uint32_t packet_len,
  212. uint32_t * const index,
  213. uint8_t * const value);
  214. /**@brief Function for safe decoding an uint18 value.
  215. *
  216. * Safe decoding of an uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  217. *
  218. * @param[in] p_buf Buffer containing the value.
  219. * @param[in] buf_len Size of buffer.
  220. * @param[in,out] index \c in: Index to start of uint8 value in buffer.
  221. * \c out: Index in buffer to first byte after the decoded value.
  222. * @param[out] value Decoded uint8 value.
  223. */
  224. void int8_dec(uint8_t const * const p_buf,
  225. uint32_t packet_len,
  226. uint32_t * const index,
  227. int8_t * const value);
  228. /**@brief Function for safe encoding variable length field encoded as length(8bit)+data.
  229. *
  230. * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  231. *
  232. * @param[out] p_data Pointer to data to encode.
  233. * @param[in] dlen Length of data to encode (0-255).
  234. * @param[out] p_buf Buffer containing the value.
  235. * @param[in] buf_len Size of buffer.
  236. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  237. * \c out: Index in buffer to first byte after the decoded value.
  238. *
  239. * @return NRF_SUCCESS Fields decoded successfully.
  240. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  241. */
  242. uint32_t len8data_enc(uint8_t const * const p_data,
  243. uint8_t const dlen,
  244. uint8_t * const p_buf,
  245. uint32_t buf_len,
  246. uint32_t * const p_index);
  247. /**@brief Function for safe decoding variable length field encoded as length(8bit)+data.
  248. *
  249. * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  250. *
  251. * @param[in] p_buf Buffer containing the value.
  252. * @param[in] buf_len Size of buffer.
  253. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  254. * \c out: Index in buffer to first byte after the decoded value.
  255. * @param[out] pp_data Pointer to pointer to decoded data (p_data is set to NULL in
  256. * case data is not present in the buffer).
  257. * @param[out] p_len Decoded length (0-255).
  258. *
  259. * @return NRF_SUCCESS Fields decoded successfully.
  260. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  261. */
  262. uint32_t len8data_dec(uint8_t const * const p_buf,
  263. uint32_t buf_len,
  264. uint32_t * const p_index,
  265. uint8_t * * const pp_data,
  266. uint8_t * const p_len);
  267. /**@brief Function for safe encoding variable length field encoded as length(16bit)+data.
  268. *
  269. * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  270. * It is possible that provided p_data is NULL in that case length is encoded and it's followed by
  271. * SER_FIELD_NOT_PRESENT flag. SER_FIELD_PRESENT flag preceeds data otherwise.
  272. *
  273. * @param[in] p_data Data to encode.
  274. * @param[in] dlen Input data length (16bit).
  275. * @param[in] p_buf Pointer to the beginning of the output buffer.
  276. * @param[in] buf_len Size of buffer.
  277. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  278. * \c out: Index in buffer to first byte after the encoded data.
  279. *
  280. * @return NRF_SUCCESS Fields decoded successfully.
  281. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  282. */
  283. uint32_t len16data_enc(uint8_t const * const p_data,
  284. uint16_t const dlen,
  285. uint8_t * const p_buf,
  286. uint32_t buf_len,
  287. uint32_t * const p_index);
  288. /**@brief Function for safe decoding variable length field encoded as length(16bit)+data.
  289. *
  290. * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  291. * Encoded data consist of length field, presence flag and conditional data (present only is presence flag
  292. * is set). p_data pointer is required to be not NULL only is presence flag is set.
  293. *
  294. * @param[in] p_buf Pointer to the beginning of the input buffer.
  295. * @param[in] buf_len Size of buffer.
  296. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  297. * \c out: Index in buffer to first byte after the decoded data.
  298. * @param[in] pp_data Pointer to pointer to decoded data.
  299. * @param[in] p_dlen data length (16bit).
  300. *
  301. * @return NRF_SUCCESS Fields decoded successfully.
  302. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  303. */
  304. uint32_t len16data_dec(uint8_t const * const p_buf,
  305. uint32_t buf_len,
  306. uint32_t * const p_index,
  307. uint8_t * * const pp_data,
  308. uint16_t * const p_dlen);
  309. /**@brief Function for safe encoding of uint16 table with a given element count.
  310. *
  311. * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  312. * It is possible that provided p_data is NULL in that case length is encoded and it's followed by
  313. * SER_FIELD_NOT_PRESENT flag. SER_FIELD_PRESENT flag precedes data otherwise.
  314. *
  315. * @param[in] p_data Data table to encode.
  316. * @param[in] count Table element count.
  317. * @param[in] p_buf Pointer to the beginning of the output buffer.
  318. * @param[in] buf_len Size of buffer.
  319. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  320. * \c out: Index in buffer to first byte after the encoded data.
  321. *
  322. * @return NRF_SUCCESS Fields decoded successfully.
  323. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  324. */
  325. uint32_t count16_cond_data16_enc(uint16_t const * const p_data,
  326. uint16_t const count,
  327. uint8_t * const p_buf,
  328. uint32_t buf_len,
  329. uint32_t * const p_index);
  330. /**@brief Function for safe decoding of uint16 table with a given element count.
  331. *
  332. * Safe encoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  333. * It is possible that provided p_data is NULL in that case length is encoded and it's followed by
  334. * SER_FIELD_NOT_PRESENT flag. SER_FIELD_PRESENT flag precedes data otherwise.
  335. *
  336. * @param[in] p_buf Pointer to the beginning of the output buffer.
  337. * @param[in] buf_len Size of buffer.
  338. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  339. * \c out: Index in buffer to first byte after the encoded data.
  340. * @param[in] pp_data Pointer to pointer to the table to encode.
  341. * @param[in,out] p_count Pointer to table element count - initialised with max count
  342. *
  343. * @return NRF_SUCCESS Fields decoded successfully.
  344. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  345. * @retval NRF_ERROR_DATA_SIZE Decoding failure. Initial count is smaller than actual.
  346. */
  347. uint32_t count16_cond_data16_dec(uint8_t const * const p_buf,
  348. uint32_t buf_len,
  349. uint32_t * const p_index,
  350. uint16_t * * const pp_data,
  351. uint16_t * const p_count);
  352. /**@brief Function for safe decoding of variable length field encoded as length(16bit)+data.
  353. *
  354. * Safe decoding of an variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  355. * Encoded data consist of presence flag, optional length field, second presence flag and optional data.
  356. *
  357. *
  358. * @param[in] p_buf Pointer to the beginning of the input buffer.
  359. * @param[in] buf_len Size of buffer.
  360. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  361. * \c out: Index in buffer to first byte after the decoded data.
  362. * @param[out] pp_data Pointer to decoded data.
  363. * @param[out] pp_len data length (16bit).
  364. *
  365. * @return NRF_SUCCESS Fields decoded successfully.
  366. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  367. */
  368. uint32_t cond_len16_cond_data_dec(uint8_t const * const p_buf,
  369. uint32_t buf_len,
  370. uint32_t * const p_index,
  371. uint8_t * * const pp_data,
  372. uint16_t * * const pp_len);
  373. /**@brief Command response encoder - replacement of - ser_ble_cmd_rsp_status_code_enc
  374. * with layout aligned to the rest of encoder functions
  375. *
  376. * @param[in] op_code Operation code - see BLE_GAP_SVCS
  377. * @param[in] return_code nRF Error Code.
  378. * @param[in] p_buff pointer to the start of pointer to decoded data.
  379. * @param[in,out] p_buff_len \c in: size of buffer
  380. * \c out: used bytes in buffer
  381. * @param[in,out] p_buff_len \c in: initial offset in buffer
  382. * \c out: final offset in buffer
  383. *
  384. * @return NRF_SUCCESS Fields decoded successfully.
  385. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  386. * @retval NRF_ERROR_NULL Invalid pointer
  387. */
  388. uint32_t op_status_enc(uint8_t op_code,
  389. uint32_t return_code,
  390. uint8_t * const p_buff,
  391. uint32_t * const p_buff_len,
  392. uint32_t * const p_index);
  393. /**@brief command response encoder with conditional 16bit field
  394. *
  395. * @param[in] op_code Operation code - see BLE_GAP_SVCS
  396. * @param[in] return_code nRF Error Code.
  397. * @param[in] value optional 16bit field encoded for return code == NRF_SUCCESS
  398. * @param[in] p_buff pointer to the start of pointer to decoded data.
  399. * @param[in,out] p_buff_len \c in: size of buffer
  400. * \c out: used bytes in buffer
  401. * @param[in,out] p_buff_len \c in: initial offset in buffer
  402. * \c out: final offset in buffer
  403. *
  404. * @return NRF_SUCCESS Fields decoded successfully.
  405. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  406. * @retval NRF_ERROR_NULL Invalid pointer
  407. */
  408. uint32_t op_status_cond_uint16_enc(uint8_t op_code,
  409. uint32_t return_code,
  410. uint16_t value,
  411. uint8_t * const p_buff,
  412. uint32_t * const p_buff_len,
  413. uint32_t * const p_index);
  414. /**@brief Function for safe encoding a buffer of known size.
  415. *
  416. * Safe encoding of a buffer. Encoder assumes that size is known to the decoder and it is not
  417. * encoded here. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  418. *
  419. * @param[in] p_data Data to encode.
  420. * @param[in] dlen Input data length (16bit).
  421. * @param[in] p_buf Pointer to the beginning of the output buffer.
  422. * @param[in] buf_len Size of buffer.
  423. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  424. * \c out: Index in buffer to first byte after the encoded data.
  425. *
  426. * @return NRF_SUCCESS Fields decoded successfully.
  427. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  428. */
  429. uint32_t buf_enc(uint8_t const * const p_data,
  430. uint16_t const dlen,
  431. uint8_t * const p_buf,
  432. uint32_t buf_len,
  433. uint32_t * const p_index);
  434. /**@brief Function for safe decoding a buffer of known size.
  435. *
  436. * Safe decoding of buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  437. * Encoded data consist of presence flag and conditional data (present only is presence flag
  438. * is set). p_data pointer is required to be not NULL only is presence flag is set. Length is provided
  439. * as an input to the function.
  440. *
  441. * @param[in] p_buf Pointer to the beginning of the input buffer.
  442. * @param[in] buf_len Size of buffer.
  443. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  444. * \c out: Index in buffer to first byte after the decoded data.
  445. * @param[in] pp_data Pointer to pointer to decoded data.
  446. * @param[in] data_len Length of buffer for decoded data (16bit).
  447. * @param[in] dlen Length of data to decode (16bit).
  448. *
  449. * @return NRF_SUCCESS Fields decoded successfully.
  450. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  451. */
  452. uint32_t buf_dec(uint8_t const * const p_buf,
  453. uint32_t buf_len,
  454. uint32_t * const p_index,
  455. uint8_t * * const pp_data,
  456. uint16_t data_len,
  457. uint16_t dlen);
  458. /**@brief Function for safe encoding an uint32 value.
  459. *
  460. * Safe decoding of an uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  461. *
  462. * @param[in] p_field uint32 value to be encoded.
  463. * @param[out] p_buf Buffer containing the value.
  464. * @param[in] buf_len Size of buffer.
  465. * @param[in,out] p_index \c in: Index to start of uint32 value in buffer.
  466. * \c out: Index in buffer to first byte after the decoded value.
  467. *
  468. * @return NRF_SUCCESS Fields decoded successfully.
  469. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  470. */
  471. uint32_t uint32_t_enc(void const * const p_field,
  472. uint8_t * const p_buf,
  473. uint32_t buf_len,
  474. uint32_t * const p_index);
  475. /**@brief Function for safe decoding an uint32 value.
  476. *
  477. * Safe decoding of an uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  478. *
  479. * @param[in] p_buf Buffer containing the value.
  480. * @param[in] buf_len Size of buffer.
  481. * @param[in,out] p_index \c in: Index to start of uint32 value in buffer.
  482. * \c out: Index in buffer to first byte after the decoded value.
  483. * @param[out] value Decoded uint32 value.
  484. */
  485. uint32_t uint32_t_dec(uint8_t const * const p_buf,
  486. uint32_t buf_len,
  487. uint32_t * const p_index,
  488. void * p_field);
  489. /**@brief Function for safe encoding of an uint8 vector.
  490. *
  491. * Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  492. * *
  493. * @param[in] p_data Data to encode.
  494. * @param[in] dlen Input data length (16bit).
  495. * @param[in] p_buf Pointer to the beginning of the output buffer.
  496. * @param[in] buf_len Size of buffer.
  497. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  498. * \c out: Index in buffer to first byte after the encoded data.
  499. *
  500. * @return NRF_SUCCESS Fields decoded successfully.
  501. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  502. */
  503. uint32_t uint8_vector_enc(uint8_t const * const p_data,
  504. uint16_t const dlen,
  505. uint8_t * const p_buf,
  506. uint32_t buf_len,
  507. uint32_t * const p_index);
  508. /**@brief Function for safe decoding a an uint8 vector.
  509. *
  510. * Safe decoding of buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  511. * Vector length is provided as an input to the function.
  512. *
  513. * @param[in] p_buf Pointer to the beginning of the input buffer.
  514. * @param[in] buf_len Size of buffer.
  515. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  516. * \c out: Index in buffer to first byte after the decoded data.
  517. * @param[in] p_data Pointer to decoded data.
  518. * @param[in] dlen Length of data to decode (16bit).
  519. *
  520. * @return NRF_SUCCESS Fields decoded successfully.
  521. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  522. */
  523. uint32_t uint8_vector_dec(uint8_t const * const p_buf,
  524. uint32_t buf_len,
  525. uint32_t * const p_index,
  526. uint8_t * const p_data,
  527. uint16_t dlen);
  528. #endif