ble_serialization.h 55 KB

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