ble_serialization.h 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. /**
  2. * Copyright (c) 2013 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef BLE_SERIALIZATION_H__
  41. #define BLE_SERIALIZATION_H__
  42. #include "nordic_common.h"
  43. #include "nrf_error.h"
  44. #include <stdint.h>
  45. #include <stddef.h>
  46. #include "cond_field_serialization.h"
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. /**@brief Types of serialization packets. */
  51. typedef enum
  52. {
  53. SER_PKT_TYPE_CMD = 0, /**< Command packet type. */
  54. SER_PKT_TYPE_RESP, /**< Command Response packet type. */
  55. SER_PKT_TYPE_EVT, /**< Event packet type. */
  56. SER_PKT_TYPE_DTM_CMD, /**< DTM Command packet type. */
  57. SER_PKT_TYPE_DTM_RESP, /**< DTM Response packet type. */
  58. SER_PKT_TYPE_RESET_CMD, /**< System Reset Command packet type. */
  59. #if defined(ANT_STACK_SUPPORT_REQD)
  60. SER_PKT_TYPE_ANT_CMD, /**< ANT Command packet type. */
  61. SER_PKT_TYPE_ANT_RESP, /**< ANT Response packet type. */
  62. SER_PKT_TYPE_ANT_EVT, /**< ANT Event packet type. */
  63. #endif
  64. SER_PKT_TYPE_MAX /**< Upper bound. */
  65. } ser_pkt_type_t;
  66. #define LOW16(a) ((uint16_t)((a & 0x0000FFFF) >> 0))
  67. #define HIGH16(a) ((uint16_t)((a & 0xFFFF0000) >> 16))
  68. //lint -esym(516,__INTADDR__) Symbol '__INTADDR__()' has arg. type conflict
  69. //lint -esym(628,__INTADDR__) no argument information provided for function '__INTADDR__()'
  70. /** Size in bytes of the Error Code field in a Command Response packet. */
  71. #define SER_ERR_CODE_SIZE 4
  72. /** Size in bytes of the Packet Type field (@ref ser_pkt_type_t). */
  73. #define SER_PKT_TYPE_SIZE 1
  74. /** Size in bytes of the Operation Code field. */
  75. #define SER_OP_CODE_SIZE 1
  76. /** Position of the Packet Type field in a serialized packet buffer. */
  77. #define SER_PKT_TYPE_POS 0
  78. /** Position of the Operation Code field in a serialized packet buffer. */
  79. #define SER_PKT_OP_CODE_POS (SER_PKT_TYPE_SIZE)
  80. /** Position of the Data in a serialized packet buffer. */
  81. #define SER_PKT_DATA_POS (SER_PKT_TYPE_SIZE + SER_OP_CODE_SIZE)
  82. /** Position of the Operation Code field in a command buffer. */
  83. #define SER_CMD_OP_CODE_POS 0
  84. /** Position of the Data in a command buffer.*/
  85. #define SER_CMD_DATA_POS (SER_OP_CODE_SIZE)
  86. /** Size of the Command header. */
  87. #define SER_CMD_HEADER_SIZE (SER_OP_CODE_SIZE)
  88. /** Size of the Command Response header. */
  89. #define SER_CMD_RSP_HEADER_SIZE (SER_OP_CODE_SIZE + SER_ERR_CODE_SIZE)
  90. /** Position of the Command Response code. */
  91. #define SER_CMD_RSP_STATUS_CODE_POS (SER_OP_CODE_SIZE)
  92. /** Size of event ID field. */
  93. #define SER_EVT_ID_SIZE 2
  94. /** Position of event ID field. */
  95. #define SER_EVT_ID_POS 0
  96. /** Size of event header. */
  97. #define SER_EVT_HEADER_SIZE (SER_EVT_ID_SIZE)
  98. /** Size of event connection handler. */
  99. #define SER_EVT_CONN_HANDLE_SIZE 2
  100. #if defined(ANT_STACK_SUPPORT_REQD)
  101. /** Size of event ID field. */
  102. #define SER_ANT_EVT_ID_SIZE 2
  103. /** Position of event ID field. */
  104. #define SER_ANT_EVT_ID_POS 0
  105. /** Size of event header. */
  106. #define SER_ANT_EVT_HEADER_SIZE (SER_ANT_EVT_ID_SIZE)
  107. #endif
  108. /** Position of the Op Code in the DTM command buffer.*/
  109. #define SER_DTM_CMD_OP_CODE_POS 0
  110. /** Position of the data in the DTM command buffer.*/
  111. #define SER_DTM_CMD_DATA_POS 1
  112. /** Position of the Op Code in the DTM command response buffer.*/
  113. #define SER_DTM_RESP_OP_CODE_POS 1
  114. /** Position of the status field in the DTM command response buffer.*/
  115. #define SER_DTM_RESP_STATUS_POS 2
  116. /** Value to indicate that an optional field is encoded in the serialized packet, e.g. white list.*/
  117. #define SER_FIELD_PRESENT 0x01
  118. /** Value to indicate that an optional field is not encoded in the serialized packet. */
  119. #define SER_FIELD_NOT_PRESENT 0x00
  120. /** Enable SER_ASSERT<*> asserts */
  121. #define SER_ASSERTS_ENABLED 1
  122. /** Returns with error code if expr is not true. It is used for checking error which should be
  123. * checked even when SER_ASSERTS_ENABLED is not set. */
  124. #define SER_ERROR_CHECK(expr, error_code) do { if (!(expr)) return (error_code); } while (0)
  125. #ifdef SER_ASSERTS_ENABLED
  126. /** Returns with error code if expr is not true. */
  127. #define SER_ASSERT(expr, error_code) SER_ERROR_CHECK(expr, error_code)
  128. /** Returns if expression is not true. */
  129. #define SER_ASSERT_VOID_RETURN(expr) do { if (!(expr)) return; } while (0)
  130. /** Returns with \ref NRF_ERROR_INVALID_LENGTH if len is not less or equal to maxlen. */
  131. #define SER_ASSERT_LENGTH_LEQ(len, maxlen) \
  132. SER_ASSERT((len) <= (maxlen), NRF_ERROR_INVALID_LENGTH)
  133. /** Returns with \ref NRF_ERROR_INVALID_LENGTH if actual_len is not equal to expected_len. */
  134. #define SER_ASSERT_LENGTH_EQ(actual_len, expected_len) \
  135. SER_ASSERT((actual_len) == (expected_len), NRF_ERROR_INVALID_LENGTH)
  136. /** Returns with \ref NRF_ERROR_NULL if pointer is null. */
  137. #define SER_ASSERT_NOT_NULL(ptr) SER_ASSERT((ptr) != NULL, NRF_ERROR_NULL)
  138. #else
  139. #define SER_ASSERT(expr, error_code)
  140. #define SER_ASSERT_VOID_RETURN(expr)
  141. #define SER_ASSERT_LENGTH_LEQ(len, maxlen) UNUSED_VARIABLE(maxlen)
  142. #define SER_ASSERT_LENGTH_EQ(actual_len, expected_len)
  143. #define SER_ASSERT_NOT_NULL(ptr)
  144. #endif
  145. #if defined(BLE_GATT_MTU_SIZE_DEFAULT) && !defined(GATT_MTU_SIZE_DEFAULT)
  146. #define GATT_MTU_SIZE_DEFAULT BLE_GATT_MTU_SIZE_DEFAULT
  147. #endif
  148. /** See Bluetooth 4.0 spec: 3.4.4.7. */
  149. #define BLE_GATTC_HANDLE_COUNT_LEN_MAX ((GATT_MTU_SIZE_DEFAULT - 1) / 2)
  150. /** Subtract 1 from X if X is greater than 0. */
  151. #define SUB1(X) (((X)>0) ? ((X)-1) : (X))
  152. static inline void static_force_impl_castable_p_void(void const * const p) {}
  153. /** Force the argument to be a double pointer. */
  154. #define STATIC_FORCE_PP(PP) static_force_impl_castable_p_void(*(PP))
  155. /** Field decoder for special structures containing variable length data. */
  156. typedef uint32_t (*field_ext_decoder_handler_t)(uint8_t const * const p_buf,
  157. uint32_t buf_len,
  158. uint32_t * const p_index,
  159. uint32_t * const p_ext_length,
  160. void * p_field);
  161. /** Push uint8_t field into the output packet. */
  162. #define SER_PUSH_uint8(P_VAR) do { \
  163. err_code = uint8_t_enc((P_VAR), p_buf, buf_len, p_index); \
  164. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  165. } while (0)
  166. /** Push uint16_t field into the output packet. */
  167. #define SER_PUSH_uint16(P_VAR) do { \
  168. err_code = uint16_t_enc((P_VAR), p_buf, buf_len, p_index); \
  169. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  170. } while (0)
  171. /** Push uint32_t field into the output packet. */
  172. #define SER_PUSH_uint32(P_VAR) do { \
  173. err_code = uint32_t_enc((P_VAR), p_buf, buf_len, p_index); \
  174. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  175. } while (0)
  176. /** Push int8_t field into the output packet. */
  177. #define SER_PUSH_int8(P_VAR) SER_PUSH_uint8(P_VAR)
  178. /** Push uint16_t field into the output packet. */
  179. #define SER_PUSH_int16(P_VAR) SER_PUSH_uint16(P_VAR)
  180. /** Push uint32_t field into the output packet. */
  181. #define SER_PUSH_int32(P_VAR) SER_PUSH_uint32(P_VAR)
  182. /** Push a constant length array of bytes into the output packet. */
  183. #define SER_PUSH_uint8array(P_DATA, LEN) do { \
  184. err_code = uint8_vector_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
  185. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  186. } while (0)
  187. /** Push a variable length (8-bit) array of bytes into the output packet. */
  188. #define SER_PUSH_len8data(P_DATA, LEN) do { \
  189. err_code = len8data_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
  190. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  191. } while (0)
  192. /** Push a variable length (16-bit) array of bytes into the output packet. */
  193. #define SER_PUSH_len16data(P_DATA, LEN) do { \
  194. err_code = len16data_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
  195. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  196. } while (0)
  197. /** Push a variable length (16-bit) array of 16-bit words into the output packet. */
  198. #define SER_PUSH_len16data16(P_DATA, LEN) do { \
  199. err_code = count16_cond_data16_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
  200. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  201. } while (0)
  202. /** Push a buffer into the output packet. */
  203. #define SER_PUSH_buf(P_DATA, LEN) do { \
  204. err_code = buf_enc((P_DATA), (LEN), p_buf, buf_len, p_index); \
  205. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  206. } while (0)
  207. /** Push a structure into the output packet. */
  208. #define SER_PUSH_FIELD(P_VAR, P_ENCODER) do { \
  209. err_code = field_enc((P_VAR), p_buf, buf_len, p_index, (P_ENCODER)); \
  210. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  211. } while (0)
  212. /** Push an array of structures into the output packet. */
  213. #define SER_PUSH_FIELD_ARRAY(P_ARRAY, P_ENCODER, COUNT) do { \
  214. for (uint32_t _idx = 0; _idx < (COUNT); ++_idx) \
  215. { \
  216. SER_PUSH_FIELD(&((P_ARRAY)[_idx]),P_ENCODER);\
  217. } \
  218. } while (0)
  219. /** Conditionally push a field if the specified pointer is not null. */
  220. #define SER_PUSH_COND(P_VAR, P_ENCODER) do { \
  221. err_code = cond_field_enc((P_VAR), p_buf, buf_len, p_index, (P_ENCODER)); \
  222. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  223. } while (0)
  224. /** Pull a uint8_t field from the input packet. */
  225. #define SER_PULL_uint8(P_VAR) do { \
  226. err_code = uint8_t_dec(p_buf, packet_len, p_index, (P_VAR)); \
  227. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  228. } while (0)
  229. /** Pull a uint16_t field from the input packet. */
  230. #define SER_PULL_uint16(P_VAR) do { \
  231. err_code = uint16_t_dec(p_buf, packet_len, p_index, (P_VAR)); \
  232. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  233. } while (0)
  234. /** Pull a uint32_t field from the input packet. */
  235. #define SER_PULL_uint32(P_VAR) do { \
  236. err_code = uint32_t_dec(p_buf, packet_len, p_index, (P_VAR)); \
  237. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  238. } while (0)
  239. /** Pull an int8_t field from the input packet. */
  240. #define SER_PULL_int8(P_VAR) SER_PULL_uint8(P_VAR)
  241. /** Pull an int16_t field from the input packet. */
  242. #define SER_PULL_int16(P_VAR) SER_PULL_uint16(P_VAR)
  243. /** Pull an int32_t field from the input packet. */
  244. #define SER_PULL_int32(P_VAR) SER_PULL_uint32(P_VAR)
  245. /** Pull a constant length byte array from the input packet. */
  246. #define SER_PULL_uint8array(P_DATA, LEN) do { \
  247. err_code = uint8_vector_dec(p_buf, packet_len, p_index, (P_DATA), (LEN)); \
  248. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  249. } while (0)
  250. /** Pull a variable length (8-bit) byte array from the input packet. */
  251. #define SER_PULL_len8data(PP_DATA, LEN) do { \
  252. STATIC_FORCE_PP(PP_DATA); \
  253. err_code = len8data_dec(p_buf, packet_len, p_index, (PP_DATA), (LEN)); \
  254. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  255. } while (0)
  256. /** Pull a variable length (16-bit) byte array from the input packet. */
  257. #define SER_PULL_len16data(PP_DATA, P_LEN) do { \
  258. STATIC_FORCE_PP(PP_DATA); \
  259. err_code = len16data_dec(p_buf, packet_len, p_index, (PP_DATA), (P_LEN)); \
  260. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  261. } while (0)
  262. /** Pull a variable length (16-bit) word (16-bit) array from the input packet. */
  263. #define SER_PULL_len16data16(PP_DATA, P_LEN) do { \
  264. STATIC_FORCE_PP(PP_DATA); \
  265. err_code = count16_cond_data16_dec(p_buf, packet_len, p_index, (PP_DATA), (P_LEN)); \
  266. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  267. } while (0)
  268. /** Pull a buffer from the input packet. */
  269. #define SER_PULL_buf(PP_DATA, OUT_BUF_LEN, LEN) do { \
  270. STATIC_FORCE_PP(PP_DATA); \
  271. err_code = buf_dec(p_buf, packet_len, p_index, (PP_DATA), (OUT_BUF_LEN), (LEN)); \
  272. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  273. } while (0)
  274. /** Pull a structure from the input packet. */
  275. #define SER_PULL_FIELD(P_VAR, P_DECODER) do { \
  276. err_code = field_dec(p_buf, packet_len, p_index, (P_VAR), (P_DECODER)); \
  277. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  278. } while (0)
  279. /** Pull an array of structures from the input packet. */
  280. #define SER_PULL_FIELD_ARRAY(P_ARRAY, P_DECODER, COUNT) do { \
  281. for (uint32_t _idx = 0; _idx < (COUNT); ++_idx) \
  282. { \
  283. SER_PULL_FIELD(&((P_ARRAY)[_idx]),P_DECODER); \
  284. } \
  285. } while (0)
  286. /** Conditionally pull a structure from the input packet. */
  287. #define SER_PULL_COND(PP_VAR, P_DECODER) do { \
  288. STATIC_FORCE_PP(PP_VAR); \
  289. err_code = cond_field_dec(p_buf, packet_len, p_index, (void * *)(PP_VAR), (P_DECODER)); \
  290. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  291. } while (0)
  292. /** Start the encoding of a structure and prepare local variables for the usage of SER_PUSH_ macros. */
  293. #define SER_STRUCT_ENC_BEGIN(STRUCT_TYPE) \
  294. SER_ASSERT_NOT_NULL(p_buf); \
  295. SER_ASSERT_NOT_NULL(p_index); \
  296. SER_ASSERT_NOT_NULL(p_void_struct); \
  297. STRUCT_TYPE * p_struct = (STRUCT_TYPE *) p_void_struct; \
  298. uint32_t err_code = NRF_SUCCESS
  299. /** End the encoding of a structure. */
  300. #define SER_STRUCT_ENC_END return err_code
  301. /** Start the decoding of a structure and prepare local variables for the usage of SER_PULL_ macros. */
  302. #define SER_STRUCT_DEC_BEGIN(STRUCT_TYPE) \
  303. SER_ASSERT_NOT_NULL(p_buf); \
  304. SER_ASSERT_NOT_NULL(p_index); \
  305. SER_ASSERT_NOT_NULL(p_void_struct); \
  306. STRUCT_TYPE * p_struct = (STRUCT_TYPE *) p_void_struct; \
  307. uint32_t err_code = NRF_SUCCESS; \
  308. uint32_t packet_len = buf_len
  309. /** End the encoding of a structure. */
  310. #define SER_STRUCT_DEC_END return err_code
  311. /** Start the encoding of command request and prepare local variables for the usage of SER_PUSH_ macros. */
  312. #define SER_REQ_ENC_BEGIN(OPCODE) \
  313. SER_ASSERT_NOT_NULL(p_buf); \
  314. SER_ASSERT_NOT_NULL(p_buf_len); \
  315. \
  316. uint32_t index = 0; \
  317. uint32_t * const p_index = &index; \
  318. uint32_t err_code = NRF_SUCCESS; \
  319. uint32_t buf_len = *p_buf_len; \
  320. \
  321. uint8_t opcode = (OPCODE); \
  322. SER_PUSH_uint8(&opcode)
  323. /** End the encoding of command request. */
  324. #define SER_REQ_ENC_END \
  325. *p_buf_len = index; \
  326. return NRF_SUCCESS \
  327. /** Start the decoding of command response that does not contain any data except the result code. */
  328. #define SER_RSP_DEC_RESULT_ONLY(OPCODE) \
  329. return ser_ble_cmd_rsp_dec(p_buf, packet_len, (OPCODE), p_result_code)
  330. /** Start the decoding of command response and prepare local variables for the usage of SER_PULL_ macros. */
  331. #define SER_RSP_DEC_BEGIN(OPCODE) \
  332. SER_ASSERT_NOT_NULL(p_buf); \
  333. SER_ASSERT_NOT_NULL(p_result_code); \
  334. uint32_t err_code = NRF_SUCCESS; \
  335. uint32_t index = 0; \
  336. uint32_t * const p_index = &index; \
  337. /* Decode the result code and exit if decoding has failed or \
  338. the decoded result is not NRF_SUCCESS. */ \
  339. err_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, (OPCODE), p_result_code); \
  340. \
  341. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  342. if (*p_result_code != NRF_SUCCESS) \
  343. { \
  344. SER_ASSERT_LENGTH_EQ(index, packet_len); \
  345. return NRF_SUCCESS; \
  346. }
  347. /** End the decoding of command response. */
  348. #define SER_RSP_DEC_END \
  349. /* Require all data to be pulled. */ \
  350. SER_ASSERT_LENGTH_EQ(index, packet_len); \
  351. return err_code
  352. /** Start the decoding of command request and prepare local variables for the usage of SER_PULL_ macros. */
  353. #define SER_REQ_DEC_BEGIN(OPCODE) \
  354. SER_ASSERT_NOT_NULL(p_buf); \
  355. SER_ASSERT(packet_len>0, NRF_ERROR_INVALID_PARAM); \
  356. uint32_t index = 0; \
  357. uint32_t * const p_index = &index; \
  358. uint32_t err_code = NRF_SUCCESS; \
  359. SER_ASSERT(p_buf[index] == (OPCODE), NRF_ERROR_INVALID_PARAM); \
  360. (void)err_code; \
  361. (void)p_index; \
  362. ++index
  363. /** End the decoding of command request. */
  364. #define SER_REQ_DEC_END \
  365. SER_ASSERT_LENGTH_EQ(index, packet_len); \
  366. return NRF_SUCCESS
  367. /** Start the encoding of command response and prepare local variables for the usage of SER_PUSH_ macros. */
  368. #define SER_RSP_ENC_BEGIN(OPCODE) \
  369. SER_ASSERT_NOT_NULL(p_buf); \
  370. SER_ASSERT_NOT_NULL(p_buf_len); \
  371. uint32_t index = 0; \
  372. uint32_t * const p_index = &index; \
  373. uint32_t err_code = NRF_SUCCESS; \
  374. uint32_t buf_len = *p_buf_len; \
  375. /* Push the opcode + result and exit if result \
  376. is not NRF_SUCCESS. */ \
  377. uint8_t opcode = (OPCODE); \
  378. SER_PUSH_uint8(&opcode); \
  379. SER_PUSH_uint32(&return_code); \
  380. \
  381. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  382. if (return_code != NRF_SUCCESS) \
  383. { \
  384. SER_RSP_ENC_END; \
  385. }
  386. /** Start the encoding of command response that contains the result code only. */
  387. #define SER_RSP_ENC_RESULT_ONLY(OPCODE) \
  388. return ser_ble_cmd_rsp_status_code_enc((OPCODE), return_code, p_buf, p_buf_len)
  389. /** End the encoding of command response. */
  390. #define SER_RSP_ENC_END \
  391. *p_buf_len = index; \
  392. return NRF_SUCCESS
  393. /** Start the encoding of an event and prepare local variables for the usage of SER_PUSH_ macros. */
  394. #define SER_EVT_ENC_BEGIN(EVT_HEADER) \
  395. SER_ASSERT_NOT_NULL(p_event); \
  396. SER_ASSERT_NOT_NULL(p_buf); \
  397. SER_ASSERT_NOT_NULL(p_buf_len); \
  398. SER_ASSERT(p_event->header.evt_id == (EVT_HEADER), NRF_ERROR_INVALID_PARAM); \
  399. uint32_t index = 0; \
  400. uint32_t * p_index = &index; \
  401. uint32_t buf_len = *p_buf_len; \
  402. uint32_t err_code = NRF_SUCCESS; \
  403. uint16_t evt_header = (EVT_HEADER); \
  404. /* Push event header. */ \
  405. SER_PUSH_uint16(&evt_header)
  406. /** End the encoding of an event. */
  407. #define SER_EVT_ENC_END \
  408. *p_buf_len = index; \
  409. return err_code
  410. /** Start the decoding of an event that has an event-specific data structure
  411. and prepare local variables for the usage of SER_PULL_ macros. */
  412. #define SER_EVT_DEC_BEGIN(EVT_CODE, EVT_GROUP, EVT_NAME) \
  413. uint32_t err_code = NRF_SUCCESS; \
  414. uint32_t index = 0; \
  415. uint32_t * p_index = &index; \
  416. SER_ASSERT_NOT_NULL(p_buf); \
  417. SER_ASSERT_NOT_NULL(p_event_len); \
  418. /* Calculate the base event structure length */ \
  419. /* and make sure that there is enough free space */ \
  420. /* in the output buffer. */ \
  421. uint32_t evt_struct_len = \
  422. offsetof(ble_evt_t, evt.EVT_GROUP##_evt.params) \
  423. - offsetof(ble_evt_t, evt) \
  424. + sizeof(ble_##EVT_GROUP##_evt_##EVT_NAME##_t); \
  425. SER_ASSERT_LENGTH_LEQ(evt_struct_len, *p_event_len); \
  426. *p_event_len -= evt_struct_len; \
  427. /* Some structures contains variable length arrays */ \
  428. /* and the overall size may be greater. */ \
  429. uint32_t evt_extended_len = 0; \
  430. (void) evt_extended_len; \
  431. p_event->header.evt_id = EVT_CODE
  432. /** Start the decoding of an event that has no event-specific data structure.
  433. and prepare local variables for the usage of SER_PULL_ macros. */
  434. #define SER_EVT_DEC_BEGIN_NO_STRUCT(EVT_CODE, EVT_GROUP) \
  435. uint32_t err_code = NRF_SUCCESS; \
  436. uint32_t index = 0; \
  437. uint32_t * p_index = &index; \
  438. SER_ASSERT_NOT_NULL(p_buf); \
  439. SER_ASSERT_NOT_NULL(p_event_len); \
  440. /* Calculate the base event structure length */ \
  441. /* and make sure that there is enough free space */ \
  442. /* in the output buffer. */ \
  443. uint32_t evt_struct_len = \
  444. offsetof(ble_evt_t, evt.EVT_GROUP##_evt.params) \
  445. - offsetof(ble_evt_t, evt) ; \
  446. SER_ASSERT_LENGTH_LEQ(evt_struct_len, *p_event_len); \
  447. *p_event_len -= evt_struct_len; \
  448. /* Some structures contain variable length arrays */ \
  449. /* and the overall size may be greater. */ \
  450. uint32_t evt_extended_len = 0; \
  451. (void) evt_extended_len; \
  452. p_event->header.evt_id = EVT_CODE
  453. /** End the decoding of an event. */
  454. #define SER_EVT_DEC_END \
  455. SER_ASSERT_LENGTH_EQ(index, packet_len); \
  456. /*p_event_len = index; */ \
  457. /*p_event->header.evt_len = index; */ \
  458. *p_event_len = evt_struct_len + evt_extended_len; \
  459. return NRF_SUCCESS
  460. /** Push an event-specific special field that contains variable length fields and get the extended data size. */
  461. #define SER_PULL_FIELD_EXTENDED(P_VAR, P_DECODER) \
  462. do \
  463. { \
  464. uint32_t field_ext_len = *p_event_len; \
  465. err_code = field_ext_dec(p_buf, packet_len, p_index, &field_ext_len, (P_VAR), (P_DECODER)); \
  466. SER_ASSERT(err_code == NRF_SUCCESS, err_code); \
  467. *p_event_len -= field_ext_len; \
  468. evt_extended_len += field_ext_len; \
  469. } while (0) \
  470. /** Generic command response status code encoder. */
  471. uint32_t ser_ble_cmd_rsp_status_code_enc(uint8_t op_code,
  472. uint32_t command_status,
  473. uint8_t * const p_buf,
  474. uint32_t * const p_buf_len);
  475. /** Generic command response result code decoder. */
  476. uint32_t ser_ble_cmd_rsp_result_code_dec(uint8_t const * const p_buf,
  477. uint32_t * const p_pos,
  478. uint32_t packet_len,
  479. uint8_t op_code,
  480. uint32_t * const p_result_code);
  481. /** Generic command response decoder. */
  482. uint32_t ser_ble_cmd_rsp_dec(uint8_t const * const p_buf,
  483. uint32_t packet_len,
  484. uint8_t op_code,
  485. uint32_t * const p_result_code);
  486. /**@brief Function for safe field encoding field.
  487. *
  488. * @param[in] p_field Pointer to the input struct. Must not be a null.
  489. * @param[in] p_buf Pointer to the beginning of the output buffer.
  490. * @param[in] buf_len Size of the buffer.
  491. * @param[in,out] p_index \c in: Index to start of uint8 value in buffer.
  492. * \c out: Index in the buffer to the first byte after the encoded data.
  493. * @param[in] fp_field_encoder Pointer to the function that implements fields encoding.
  494. *
  495. * @return NRF_SUCCESS Field encoded successfully.
  496. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  497. * @retval NRF_ERROR_NULL p_field or fp_field_encoder is NULL.
  498. */
  499. static inline uint32_t field_enc(void const * const p_field,
  500. uint8_t * const p_buf,
  501. uint32_t buf_len,
  502. uint32_t * const p_index,
  503. field_encoder_handler_t fp_field_encoder)
  504. {
  505. SER_ASSERT_NOT_NULL(fp_field_encoder);
  506. SER_ASSERT_NOT_NULL(p_field);
  507. return fp_field_encoder(p_field, p_buf, buf_len, p_index);
  508. }
  509. /**@brief Function for safe field decoding.
  510. *
  511. * Function checks if conditional field is present in the input buffer and if it is set, it calls
  512. * the provided parser function that attempts to parse the buffer content to the known field.
  513. *
  514. * @param[in] p_buf Pointer to the beginning of the input buffer.
  515. * @param[in] buf_len Size of the buffer.
  516. * @param[in,out] p_index \c in: Index to the start of uint8 value in buffer.
  517. * \c out: Index in the buffer to the first byte after the decoded data.
  518. * @param[in] p_field Pointer to the output location.
  519. * @param[in] fp_field_decoder Pointer to the function that implements field decoding.
  520. *
  521. * @return NRF_SUCCESS Field decoded successfully.
  522. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  523. * @retval NRF_ERROR_NULL p_field or fp_field_decoder is NULL.
  524. */
  525. static inline uint32_t field_dec(uint8_t const * const p_buf,
  526. uint32_t buf_len,
  527. uint32_t * const p_index,
  528. void * const p_field,
  529. field_decoder_handler_t fp_field_decoder)
  530. {
  531. SER_ASSERT_NOT_NULL(fp_field_decoder);
  532. SER_ASSERT_NOT_NULL(p_field);
  533. return fp_field_decoder(p_buf, buf_len, p_index, p_field);
  534. }
  535. /**@brief Function for safe decoding of an event-specific field that contains extended data.
  536. *
  537. * Some event structures contain a variable length array (extended data),
  538. * that may be written next to the event structure.
  539. *
  540. * @param[in] p_buf Pointer to the beginning of the input buffer.
  541. * @param[in] buf_len Size of the buffer.
  542. * @param[in,out] p_index \c in: Index to the start of uint8 value in the buffer.
  543. * \c out: Index in the buffer to the first byte after the decoded data.
  544. * @param[in,out] p_ext_length \c in: Maximum size of extended data.
  545. * \c out: Extended data length in bytes.
  546. * @param[in] p_field Pointer to output location.
  547. * @param[in] fp_field_decoder Pointer to the function that implements field decoding.
  548. *
  549. * @return NRF_SUCCESS Field decoded successfully.
  550. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  551. * @retval NRF_ERROR_NULL p_field or fp_field_decoder is NULL.
  552. */
  553. static inline uint32_t field_ext_dec(uint8_t const * const p_buf,
  554. uint32_t buf_len,
  555. uint32_t * const p_index,
  556. uint32_t * const p_ext_length,
  557. void * const p_field,
  558. field_ext_decoder_handler_t fp_field_decoder)
  559. {
  560. SER_ASSERT_NOT_NULL(fp_field_decoder);
  561. SER_ASSERT_NOT_NULL(p_field);
  562. return fp_field_decoder(p_buf, buf_len, p_index, p_ext_length, p_field);
  563. }
  564. /**@brief Function for safe encoding an uint16 value.
  565. *
  566. * Safe decoding of a uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  567. *
  568. * @param[in] p_field A uint16 value to be encoded.
  569. * @param[out] p_buf Buffer containing the value.
  570. * @param[in] buf_len Size of the buffer.
  571. * @param[in,out] p_index \c in: Index to the start of the uint16 value in the buffer.
  572. * \c out: Index in the buffer to the first byte after the decoded value.
  573. *
  574. * @return NRF_SUCCESS Fields decoded successfully.
  575. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  576. */
  577. uint32_t uint16_t_enc(const void * const p_field,
  578. uint8_t * const p_buf,
  579. uint32_t buf_len,
  580. uint32_t * const p_index);
  581. /**@brief Function for safe decoding of a uint16 value.
  582. *
  583. * Safe decoding of a uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  584. *
  585. * @param[in] p_buf Buffer containing the value.
  586. * @param[in] buf_len Size of the buffer.
  587. * @param[in,out] p_index \c in: Index to the start of the uint16 value in the buffer.
  588. * \c out: Index in the buffer to the first byte after the decoded value.
  589. * @param[out] p_field Pointer to the location where the uint16 value will be decoded.
  590. */
  591. uint32_t uint16_t_dec(uint8_t const * const p_buf,
  592. uint32_t buf_len,
  593. uint32_t * const p_index,
  594. void * p_field);
  595. /**@brief Function for safe decoding of a uint16 value.
  596. *
  597. * Safe decoding of a uint16 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  598. *
  599. * @param[in] p_buf Buffer containing the value.
  600. * @param[in] buf_len Size of the buffer.
  601. * @param[in,out] index \c in: Index to the start of the uint16 value in buffer.
  602. * \c out: Index in the buffer to the first byte after the decoded value.
  603. * @param[out] value Decoded uint16 value.
  604. */
  605. void uint16_dec(uint8_t const * const p_buf,
  606. uint32_t packet_len,
  607. uint32_t * const index,
  608. uint16_t * const value);
  609. /**@brief Function for safe encoding of a uint18 value.
  610. *
  611. * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  612. *
  613. * @param[in] p_buf Buffer containing the value.
  614. * @param[in] buf_len Size of the buffer.
  615. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  616. * \c out: Index in the buffer to the first byte after the decoded value.
  617. * @param[out] p_field Pointer to uint8 value to be encoded.
  618. *
  619. * @return NRF_SUCCESS Fields decoded successfully.
  620. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  621. */
  622. uint32_t uint8_t_enc(const void * const p_field,
  623. uint8_t * const p_buf,
  624. uint32_t buf_len,
  625. uint32_t * const p_index);
  626. /**@brief Function for safe decoding of a uint8 value.
  627. *
  628. * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  629. *
  630. * @param[in] p_buf Buffer containing the value.
  631. * @param[in] buf_len Size of the buffer.
  632. * @param[in,out] p_index \c in: Index to the start of the uint8 value in buffer.
  633. * \c out: Index in the buffer to the first byte after the decoded value.
  634. * @param[out] p_field Pointer to the location for decoded uint8 value.
  635. *
  636. * @return NRF_SUCCESS Fields decoded successfully.
  637. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  638. */
  639. uint32_t uint8_t_dec(uint8_t const * const p_buf,
  640. uint32_t buf_len,
  641. uint32_t * const p_index,
  642. void * p_field);
  643. /**@brief Function for safe decoding of a uint8 value.
  644. *
  645. * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  646. *
  647. * @param[in] p_buf Buffer containing the value.
  648. * @param[in] buf_len Size of the buffer.
  649. * @param[in,out] index \c in: Index to the start of the uint8 value in the buffer.
  650. * \c out: Index in the buffer to the first byte after the decoded value.
  651. * @param[out] value Decoded uint8 value.
  652. */
  653. void uint8_dec(uint8_t const * const p_buf,
  654. uint32_t packet_len,
  655. uint32_t * const index,
  656. uint8_t * const value);
  657. /**@brief Function for safe decoding of a uint18 value.
  658. *
  659. * Safe decoding of a uint8 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  660. *
  661. * @param[in] p_buf Buffer containing the value.
  662. * @param[in] buf_len Size of the buffer.
  663. * @param[in,out] index \c in: Index to the start of the uint8 value in the buffer.
  664. * \c out: Index in the buffer to the first byte after the decoded value.
  665. * @param[out] value Decoded uint8 value.
  666. */
  667. void int8_dec(uint8_t const * const p_buf,
  668. uint32_t packet_len,
  669. uint32_t * const index,
  670. int8_t * const value);
  671. /**@brief Function for safe encoding of a variable length field encoded as length(8bit) + data.
  672. *
  673. * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  674. *
  675. * @param[out] p_data Pointer to data to encode.
  676. * @param[in] dlen Length of data to encode (0-255).
  677. * @param[out] p_buf Buffer containing the value.
  678. * @param[in] buf_len Size of the buffer.
  679. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  680. * \c out: Index in the buffer to the first byte after the decoded value.
  681. *
  682. * @return NRF_SUCCESS Fields decoded successfully.
  683. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  684. */
  685. uint32_t len8data_enc(uint8_t const * const p_data,
  686. uint8_t const dlen,
  687. uint8_t * const p_buf,
  688. uint32_t buf_len,
  689. uint32_t * const p_index);
  690. /**@brief Function for safe decoding of a variable length field encoded as length(8bit) + data.
  691. *
  692. * Safe decoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  693. *
  694. * @param[in] p_buf Buffer containing the value.
  695. * @param[in] buf_len Size of the buffer.
  696. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  697. * \c out: Index in the buffer to the first byte after the decoded value.
  698. * @param[out] pp_data Pointer to decoded data (p_data is set to NULL in
  699. * case data is not present in the buffer).
  700. * @param[out] p_len Decoded length (0-255).
  701. *
  702. * @return NRF_SUCCESS Fields decoded successfully.
  703. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  704. */
  705. uint32_t len8data_dec(uint8_t const * const p_buf,
  706. uint32_t buf_len,
  707. uint32_t * const p_index,
  708. uint8_t * * const pp_data,
  709. uint8_t * const p_len);
  710. /**@brief Function for safe encoding of a variable length field encoded as length(16 bit) + data.
  711. *
  712. * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  713. * It is possible that provided p_data is NULL. In that case, length is encoded and it is followed by
  714. * SER_FIELD_NOT_PRESENT flag. Otherwise, the SER_FIELD_PRESENT flag precedes the data.
  715. *
  716. * @param[in] p_data Data to encode.
  717. * @param[in] dlen Input data length (16 bit).
  718. * @param[in] p_buf Pointer to the beginning of the output buffer.
  719. * @param[in] buf_len Size of the buffer.
  720. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  721. * \c out: Index in the buffer to the first byte after the encoded data.
  722. *
  723. * @return NRF_SUCCESS Fields decoded successfully.
  724. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  725. */
  726. uint32_t len16data_enc(uint8_t const * const p_data,
  727. uint16_t const dlen,
  728. uint8_t * const p_buf,
  729. uint32_t buf_len,
  730. uint32_t * const p_index);
  731. /**@brief Function for safe decoding of a variable length field encoded as length(16 bit) + data.
  732. *
  733. * Safe decoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  734. * Encoded data consists of a length field, a presence flag, and conditional data (present only if the presence flag
  735. * is set). The p_data pointer cannot be NULL if the presence flag is set.
  736. *
  737. * @param[in] p_buf Pointer to the beginning of the input buffer.
  738. * @param[in] buf_len Size of the buffer.
  739. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  740. * \c out: Index in the buffer to the first byte after the decoded data.
  741. * @param[in] pp_data Pointer to decoded data.
  742. * @param[in] p_dlen Data length (16 bit).
  743. *
  744. * @return NRF_SUCCESS Fields decoded successfully.
  745. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  746. */
  747. uint32_t len16data_dec(uint8_t const * const p_buf,
  748. uint32_t buf_len,
  749. uint32_t * const p_index,
  750. uint8_t * * const pp_data,
  751. uint16_t * const p_dlen);
  752. /**@brief Function for safe encoding of a uint16 table with a given element count.
  753. *
  754. * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  755. * It is possible that the provided p_data is NULL. In that case, length is encoded and it is followed by a
  756. * SER_FIELD_NOT_PRESENT flag. Otherwise, the SER_FIELD_PRESENT flag precedes the data.
  757. *
  758. * @param[in] p_data Data table to encode.
  759. * @param[in] count Table element count.
  760. * @param[in] p_buf Pointer to the beginning of the output buffer.
  761. * @param[in] buf_len Size of the buffer.
  762. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  763. * \c out: Index in the buffer to the first byte after the encoded data.
  764. *
  765. * @return NRF_SUCCESS Fields decoded successfully.
  766. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  767. */
  768. uint32_t count16_cond_data16_enc(uint16_t const * const p_data,
  769. uint16_t const count,
  770. uint8_t * const p_buf,
  771. uint32_t buf_len,
  772. uint32_t * const p_index);
  773. /**@brief Function for safe decoding of a uint16 table with a given element count.
  774. *
  775. * Safe encoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  776. * It is possible that the provided p_data is NULL. In that case, length is encoded and it is followed by a
  777. * SER_FIELD_NOT_PRESENT flag. Otherwise, the SER_FIELD_PRESENT flag precedes the data.
  778. *
  779. * @param[in] p_buf Pointer to the beginning of the output buffer.
  780. * @param[in] buf_len Size of the buffer.
  781. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  782. * \c out: Index in the buffer to the first byte after the encoded data.
  783. * @param[in] pp_data Pointer to the table to encode.
  784. * @param[in,out] p_count Pointer to table element count - initialised with max count.
  785. *
  786. * @return NRF_SUCCESS Fields decoded successfully.
  787. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  788. * @retval NRF_ERROR_DATA_SIZE Decoding failure. Initial count is smaller than actual.
  789. */
  790. uint32_t count16_cond_data16_dec(uint8_t const * const p_buf,
  791. uint32_t buf_len,
  792. uint32_t * const p_index,
  793. uint16_t * * const pp_data,
  794. uint16_t * const p_count);
  795. /**@brief Function for safe decoding of a variable length field encoded as length(16 bit) + data.
  796. *
  797. * Safe decoding of a variable length field. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  798. * Encoded data consists of a presence flag, an optional length field, a second presence flag, and optional data.
  799. *
  800. *
  801. * @param[in] p_buf Pointer to the beginning of the input buffer.
  802. * @param[in] buf_len Size of the buffer.
  803. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  804. * \c out: Index in the buffer to the first byte after the decoded data.
  805. * @param[out] pp_data Pointer to decoded data.
  806. * @param[out] pp_len Data length (16 bit).
  807. *
  808. * @return NRF_SUCCESS Fields decoded successfully.
  809. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  810. */
  811. uint32_t cond_len16_cond_data_dec(uint8_t const * const p_buf,
  812. uint32_t buf_len,
  813. uint32_t * const p_index,
  814. uint8_t * * const pp_data,
  815. uint16_t * * const pp_len);
  816. /**@brief Command response encoder - replacement of ser_ble_cmd_rsp_status_code_enc
  817. * with layout aligned to the rest of encoder functions.
  818. *
  819. * @param[in] op_code Operation code - see BLE_GAP_SVCS.
  820. * @param[in] return_code nRF error code.
  821. * @param[in] p_buff Pointer to the start of pointer to decoded data.
  822. * @param[in,out] p_buff_len \c in: Size of the buffer.
  823. * \c out: Used bytes in the buffer.
  824. * @param[in,out] p_buff_len \c in: Initial offset in the buffer.
  825. * \c out: Final offset in the buffer.
  826. *
  827. * @return NRF_SUCCESS Fields decoded successfully.
  828. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  829. * @retval NRF_ERROR_NULL Invalid pointer.
  830. */
  831. uint32_t op_status_enc(uint8_t op_code,
  832. uint32_t return_code,
  833. uint8_t * const p_buff,
  834. uint32_t * const p_buff_len,
  835. uint32_t * const p_index);
  836. /**@brief Command response encoder with a conditional 16-bit field.
  837. *
  838. * @param[in] op_code Operation code - see BLE_GAP_SVCS.
  839. * @param[in] return_code nRF error code.
  840. * @param[in] value Optional 16-bit field encoded for return code == NRF_SUCCESS.
  841. * @param[in] p_buff Pointer to the start of pointer to decoded data.
  842. * @param[in,out] p_buff_len \c in: Size of the buffer.
  843. * \c out: Used bytes in the buffer.
  844. * @param[in,out] p_buff_len \c in: Initial offset in the buffer.
  845. * \c out: Final offset in the buffer.
  846. *
  847. * @return NRF_SUCCESS Fields decoded successfully.
  848. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  849. * @retval NRF_ERROR_NULL Invalid pointer.
  850. */
  851. uint32_t op_status_cond_uint16_enc(uint8_t op_code,
  852. uint32_t return_code,
  853. uint16_t value,
  854. uint8_t * const p_buff,
  855. uint32_t * const p_buff_len,
  856. uint32_t * const p_index);
  857. /**@brief Function for safe encoding of a buffer of known size.
  858. *
  859. * Safe encoding of a buffer. Encoder assumes that the size is known to the decoder and it is not
  860. * encoded here. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  861. *
  862. * @param[in] p_data Data to encode.
  863. * @param[in] dlen Input data length (16 bit).
  864. * @param[in] p_buf Pointer to the beginning of the output buffer.
  865. * @param[in] buf_len Size of the buffer.
  866. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  867. * \c out: Index in the buffer to the first byte after the encoded data.
  868. *
  869. * @return NRF_SUCCESS Fields decoded successfully.
  870. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  871. */
  872. uint32_t buf_enc(uint8_t const * const p_data,
  873. uint16_t const dlen,
  874. uint8_t * const p_buf,
  875. uint32_t buf_len,
  876. uint32_t * const p_index);
  877. /**@brief Function for safe decoding of a buffer of known size.
  878. *
  879. * Safe decoding of a buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  880. * Encoded data consists of a presence flag and conditional data (present only if the presence flag
  881. * is set). The p_data pointer cannot be NULL only if the presence flag is set. Length is provided
  882. * as input to the function.
  883. *
  884. * @param[in] p_buf Pointer to the beginning of the input buffer.
  885. * @param[in] buf_len Size of the buffer.
  886. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  887. * \c out: Index in the buffer to the first byte after the decoded data.
  888. * @param[in] pp_data Pointer to decoded data.
  889. * @param[in] data_len Length of the buffer for decoded data (16 bit).
  890. * @param[in] dlen Length of the data to decode (16 bit).
  891. *
  892. * @return NRF_SUCCESS Fields decoded successfully.
  893. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  894. */
  895. uint32_t buf_dec(uint8_t const * const p_buf,
  896. uint32_t buf_len,
  897. uint32_t * const p_index,
  898. uint8_t * * const pp_data,
  899. uint16_t data_len,
  900. uint16_t dlen);
  901. /**@brief Function for safe encoding of a uint32 value.
  902. *
  903. * Safe decoding of a uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  904. *
  905. * @param[in] p_field A uint32 value to be encoded.
  906. * @param[out] p_buf Buffer containing the value.
  907. * @param[in] buf_len Size of the buffer.
  908. * @param[in,out] p_index \c in: Index to the start of the uint32 value in the buffer.
  909. * \c out: Index in the buffer to the first byte after the decoded value.
  910. *
  911. * @return NRF_SUCCESS Fields decoded successfully.
  912. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  913. */
  914. uint32_t uint32_t_enc(void const * const p_field,
  915. uint8_t * const p_buf,
  916. uint32_t buf_len,
  917. uint32_t * const p_index);
  918. /**@brief Function for safe decoding of a uint32 value.
  919. *
  920. * Safe decoding of a uint32 value. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  921. *
  922. * @param[in] p_buf Buffer containing the value.
  923. * @param[in] buf_len Size of the buffer.
  924. * @param[in,out] p_index \c in: Index to the start of the uint32 value in the buffer.
  925. * \c out: Index in the buffer to the first byte after the decoded value.
  926. * @param[out] value Decoded uint32 value.
  927. */
  928. uint32_t uint32_t_dec(uint8_t const * const p_buf,
  929. uint32_t buf_len,
  930. uint32_t * const p_index,
  931. void * p_field);
  932. /**@brief Function for safe encoding of a uint8 vector.
  933. *
  934. * Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  935. * *
  936. * @param[in] p_data Data to encode.
  937. * @param[in] dlen Input data length (16 bit).
  938. * @param[in] p_buf Pointer to the beginning of the output buffer.
  939. * @param[in] buf_len Size of the buffer.
  940. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  941. * \c out: Index in the buffer to the first byte after the encoded data.
  942. *
  943. * @return NRF_SUCCESS Fields decoded successfully.
  944. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  945. */
  946. uint32_t uint8_vector_enc(uint8_t const * const p_data,
  947. uint16_t const dlen,
  948. uint8_t * const p_buf,
  949. uint32_t buf_len,
  950. uint32_t * const p_index);
  951. /**@brief Function for safe decoding a uint8 vector.
  952. *
  953. * Safe decoding of a buffer of known size. Range checks will be done if @ref SER_ASSERTS_ENABLED is set.
  954. * Vector length is provided as input to the function.
  955. *
  956. * @param[in] p_buf Pointer to the beginning of the input buffer.
  957. * @param[in] buf_len Size of the buffer.
  958. * @param[in,out] p_index \c in: Index to the start of the uint8 value in the buffer.
  959. * \c out: Index in the buffer to the first byte after the decoded data.
  960. * @param[in] p_data Pointer to decoded data.
  961. * @param[in] dlen Length of data to decode (16 bit).
  962. *
  963. * @return NRF_SUCCESS Fields decoded successfully.
  964. * @retval NRF_ERROR_INVALID_LENGTH Decoding failure. Incorrect buffer length.
  965. */
  966. uint32_t uint8_vector_dec(uint8_t const * const p_buf,
  967. uint32_t buf_len,
  968. uint32_t * const p_index,
  969. uint8_t * const p_data,
  970. uint16_t dlen);
  971. #ifdef __cplusplus
  972. }
  973. #endif
  974. #endif