ble_serialization.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /* Copyright (c) 2013 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "ble_serialization.h"
  13. #include "nrf_error.h"
  14. #include "app_util.h"
  15. #include <stddef.h>
  16. #include <string.h>
  17. uint32_t ser_ble_cmd_rsp_status_code_enc(uint8_t op_code,
  18. uint32_t command_status,
  19. uint8_t * const p_buf,
  20. uint32_t * const p_buf_len)
  21. {
  22. SER_ASSERT_NOT_NULL(p_buf);
  23. SER_ASSERT_NOT_NULL(p_buf_len);
  24. uint32_t index = 0;
  25. SER_ASSERT_LENGTH_LEQ(SER_CMD_RSP_HEADER_SIZE, *p_buf_len);
  26. //Encode Op Code.
  27. p_buf[index++] = op_code;
  28. //Encode Status.
  29. index += uint32_encode(command_status, &(p_buf[index]));
  30. *p_buf_len = index;
  31. return NRF_SUCCESS;
  32. }
  33. uint32_t ser_ble_cmd_rsp_result_code_dec(uint8_t const * const p_buf,
  34. uint32_t * const p_pos,
  35. uint32_t packet_len,
  36. uint8_t op_code,
  37. uint32_t * const p_result_code)
  38. {
  39. SER_ASSERT_NOT_NULL(p_buf);
  40. SER_ASSERT_NOT_NULL(p_pos);
  41. SER_ASSERT_NOT_NULL(p_result_code);
  42. if (packet_len < SER_CMD_RSP_HEADER_SIZE)
  43. {
  44. return NRF_ERROR_DATA_SIZE;
  45. }
  46. if (p_buf[(*p_pos)] != op_code)
  47. {
  48. return NRF_ERROR_INVALID_DATA;
  49. }
  50. *p_result_code = uint32_decode(&(p_buf[(*p_pos) + SER_CMD_RSP_STATUS_CODE_POS]));
  51. *p_pos += SER_CMD_RSP_HEADER_SIZE;
  52. return NRF_SUCCESS;
  53. }
  54. uint32_t ser_ble_cmd_rsp_dec(uint8_t const * const p_buf,
  55. uint32_t packet_len,
  56. uint8_t op_code,
  57. uint32_t * const p_result_code)
  58. {
  59. uint32_t index = 0;
  60. uint32_t result_code = ser_ble_cmd_rsp_result_code_dec(p_buf, &index, packet_len, op_code,
  61. p_result_code);
  62. if (result_code != NRF_SUCCESS)
  63. {
  64. return result_code;
  65. }
  66. if (index != packet_len)
  67. {
  68. return NRF_ERROR_DATA_SIZE;
  69. }
  70. return NRF_SUCCESS;
  71. }
  72. uint32_t uint32_t_enc(void const * const p_field,
  73. uint8_t * const p_buf,
  74. uint32_t buf_len,
  75. uint32_t * const p_index)
  76. {
  77. SER_ASSERT_NOT_NULL(p_buf);
  78. SER_ASSERT_NOT_NULL(p_field);
  79. SER_ASSERT_NOT_NULL(p_index);
  80. uint32_t * p_uint32 = (uint32_t *)p_field;
  81. SER_ASSERT_LENGTH_LEQ(4, buf_len - *p_index);
  82. *p_index += uint32_encode(*p_uint32, &p_buf[*p_index]);
  83. return NRF_SUCCESS;
  84. }
  85. uint32_t uint32_t_dec(uint8_t const * const p_buf,
  86. uint32_t buf_len,
  87. uint32_t * const p_index,
  88. void * p_field)
  89. {
  90. SER_ASSERT_NOT_NULL(p_buf);
  91. SER_ASSERT_NOT_NULL(p_index);
  92. SER_ASSERT_NOT_NULL(p_field);
  93. uint32_t * p_uint32 = (uint32_t *)p_field;
  94. SER_ASSERT_LENGTH_LEQ(4, ((int32_t)buf_len - *p_index));
  95. *p_uint32 = uint32_decode(&p_buf[*p_index]);
  96. *p_index += 4;
  97. return NRF_SUCCESS;
  98. }
  99. uint32_t uint16_t_enc(const void * const p_field,
  100. uint8_t * const p_buf,
  101. uint32_t buf_len,
  102. uint32_t * const p_index)
  103. {
  104. uint16_t * p_u16 = (uint16_t *)p_field;
  105. SER_ASSERT_LENGTH_LEQ(2, buf_len - *p_index);
  106. *p_index += uint16_encode(*p_u16, &p_buf[*p_index]);
  107. return NRF_SUCCESS;
  108. }
  109. uint32_t uint16_t_dec(uint8_t const * const p_buf,
  110. uint32_t buf_len,
  111. uint32_t * const p_index,
  112. void * p_field)
  113. {
  114. uint16_t * p_u16 = (uint16_t *)p_field;
  115. SER_ASSERT_LENGTH_LEQ(2, ((int32_t)buf_len - *p_index));
  116. *p_u16 = uint16_decode(&p_buf[*p_index]);
  117. *p_index += 2;
  118. return NRF_SUCCESS;
  119. }
  120. void uint16_dec(uint8_t const * const p_buf,
  121. uint32_t buf_len,
  122. uint32_t * const index,
  123. uint16_t * const value)
  124. {
  125. SER_ASSERT_VOID_RETURN(*index + 2 <= buf_len);
  126. *value = uint16_decode(&p_buf[*index]);
  127. *index += 2;
  128. }
  129. uint32_t uint8_t_enc(const void * const p_field,
  130. uint8_t * const p_buf,
  131. uint32_t buf_len,
  132. uint32_t * const p_index)
  133. {
  134. SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index);
  135. uint8_t * p_u8 = (uint8_t *)p_field;
  136. p_buf[*p_index] = *p_u8;
  137. *p_index += 1;
  138. return NRF_SUCCESS;
  139. }
  140. uint32_t uint8_t_dec(uint8_t const * const p_buf,
  141. uint32_t buf_len,
  142. uint32_t * const p_index,
  143. void * p_field)
  144. {
  145. uint8_t * p_u8 = (uint8_t *)p_field;
  146. SER_ASSERT_LENGTH_LEQ(1, ((int32_t)buf_len - *p_index));
  147. *p_u8 = p_buf[*p_index];
  148. *p_index += 1;
  149. return NRF_SUCCESS;
  150. }
  151. void uint8_dec(uint8_t const * const p_buf,
  152. uint32_t buf_len,
  153. uint32_t * const index,
  154. uint8_t * const value)
  155. {
  156. SER_ASSERT_VOID_RETURN(*index + 1 <= buf_len);
  157. *value = p_buf[*index];
  158. *index += 1;
  159. }
  160. void int8_dec(uint8_t const * const p_buf,
  161. uint32_t buf_len,
  162. uint32_t * const index,
  163. int8_t * const value)
  164. {
  165. SER_ASSERT_VOID_RETURN(*index + 1 <= buf_len);
  166. *value = p_buf[*index];
  167. *index += 1;
  168. }
  169. uint32_t len8data_enc(uint8_t const * const p_data,
  170. uint8_t const dlen,
  171. uint8_t * const p_buf,
  172. uint32_t buf_len,
  173. uint32_t * const p_index)
  174. {
  175. uint32_t err_code = NRF_SUCCESS;
  176. err_code = uint8_t_enc(&dlen, p_buf, buf_len, p_index);
  177. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  178. err_code = buf_enc(p_data, dlen, p_buf, buf_len, p_index);
  179. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  180. return err_code;
  181. }
  182. uint32_t len8data_dec(uint8_t const * const p_buf,
  183. uint32_t buf_len,
  184. uint32_t * const p_index,
  185. uint8_t * * const pp_data,
  186. uint8_t * const p_len)
  187. {
  188. uint32_t err_code = NRF_SUCCESS;
  189. uint16_t out_buf_len = *p_len;
  190. err_code = uint8_t_dec(p_buf, buf_len, p_index, p_len);
  191. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  192. err_code = buf_dec(p_buf, buf_len, p_index, pp_data, out_buf_len, *p_len);
  193. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  194. return err_code;
  195. }
  196. uint32_t len16data_enc(uint8_t const * const p_data,
  197. uint16_t const dlen,
  198. uint8_t * const p_buf,
  199. uint32_t buf_len,
  200. uint32_t * const p_index)
  201. {
  202. uint32_t err_code = NRF_SUCCESS;
  203. err_code = uint16_t_enc(&dlen, p_buf, buf_len, p_index);
  204. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  205. err_code = buf_enc(p_data, dlen, p_buf, buf_len, p_index);
  206. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  207. return err_code;
  208. }
  209. uint32_t len16data_dec(uint8_t const * const p_buf,
  210. uint32_t buf_len,
  211. uint32_t * const p_index,
  212. uint8_t * * const pp_data,
  213. uint16_t * const p_dlen)
  214. {
  215. uint32_t err_code = NRF_SUCCESS;
  216. uint16_t out_buf_len = *p_dlen;
  217. err_code = uint16_t_dec(p_buf, buf_len, p_index, p_dlen);
  218. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  219. err_code = buf_dec(p_buf, buf_len, p_index, pp_data, out_buf_len, *p_dlen);
  220. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  221. return err_code;
  222. }
  223. uint32_t count16_cond_data16_enc(uint16_t const * const p_data,
  224. uint16_t const count,
  225. uint8_t * const p_buf,
  226. uint32_t buf_len,
  227. uint32_t * const p_index)
  228. {
  229. uint32_t i = 0;
  230. SER_ASSERT_LENGTH_LEQ(3, ((int32_t)buf_len - *p_index));
  231. *p_index += uint16_encode(count, &p_buf[*p_index]);
  232. if (p_data)
  233. {
  234. SER_ASSERT_LENGTH_LEQ((int32_t)(2 * count + 1), ((int32_t)buf_len - (int32_t)*p_index));
  235. p_buf[*p_index] = SER_FIELD_PRESENT;
  236. *p_index += 1;
  237. //memcpy may fail in case of Endianness difference between application and connectivity processor
  238. for (i = 0; i < count; i++)
  239. {
  240. *p_index += uint16_encode(p_data[i], &p_buf[*p_index]);
  241. }
  242. }
  243. else
  244. {
  245. SER_ASSERT_LENGTH_LEQ((1), ((int32_t)buf_len - *p_index));
  246. p_buf[*p_index] = SER_FIELD_NOT_PRESENT;
  247. *p_index += 1;
  248. }
  249. return NRF_SUCCESS;
  250. }
  251. uint32_t count16_cond_data16_dec(uint8_t const * const p_buf,
  252. uint32_t buf_len,
  253. uint32_t * const p_index,
  254. uint16_t * * const pp_data,
  255. uint16_t * const p_count)
  256. {
  257. uint16_t count = 0;
  258. uint8_t is_present = 0;
  259. uint16_t i;
  260. SER_ASSERT_NOT_NULL(p_count);
  261. SER_ASSERT_NOT_NULL(pp_data);
  262. SER_ASSERT_NOT_NULL(*pp_data);
  263. SER_ASSERT_LENGTH_LEQ(3, ((int32_t)buf_len - (*p_index)));
  264. uint16_dec(p_buf, buf_len, p_index, &count);
  265. if (count > *p_count)
  266. {
  267. return NRF_ERROR_DATA_SIZE;
  268. }
  269. SER_ASSERT_LENGTH_LEQ(count, *p_count);
  270. uint8_dec(p_buf, buf_len, p_index, &is_present);
  271. if (!is_present)
  272. {
  273. *pp_data = NULL;
  274. return NRF_SUCCESS;
  275. }
  276. else
  277. {
  278. for (i = 0; i < count; i++ )
  279. {
  280. uint16_dec(p_buf, buf_len, p_index, &((&(**pp_data))[i]) );
  281. }
  282. *p_count = i;
  283. }
  284. return NRF_SUCCESS;
  285. }
  286. uint32_t cond_len16_cond_data_dec(uint8_t const * const p_buf,
  287. uint32_t buf_len,
  288. uint32_t * const p_index,
  289. uint8_t * * const pp_data,
  290. uint16_t * * const pp_len)
  291. {
  292. SER_ASSERT_NOT_NULL(pp_len);
  293. SER_ASSERT_NOT_NULL(*pp_len);
  294. SER_ASSERT_NOT_NULL(pp_data);
  295. SER_ASSERT_NOT_NULL(*pp_data);
  296. SER_ASSERT_LENGTH_LEQ(2, ((int32_t)buf_len - (*p_index)));
  297. uint8_t is_present = 0;
  298. uint8_dec(p_buf, buf_len, p_index, &is_present);
  299. if (!is_present)
  300. {
  301. *pp_len = NULL; //if length field is not present
  302. (*p_index)++; //then data can not be present
  303. *pp_data = NULL;
  304. return NRF_SUCCESS;
  305. }
  306. else
  307. {
  308. return len16data_dec(p_buf, buf_len, p_index, pp_data, *pp_len);
  309. }
  310. }
  311. uint32_t op_status_enc(uint8_t op_code,
  312. uint32_t return_code,
  313. uint8_t * const p_buff,
  314. uint32_t * const p_buff_len,
  315. uint32_t * const p_index)
  316. {
  317. SER_ASSERT_NOT_NULL(p_buff);
  318. SER_ASSERT_NOT_NULL(p_buff_len);
  319. SER_ASSERT_NOT_NULL(p_index);
  320. SER_ASSERT_LENGTH_LEQ(SER_CMD_RSP_HEADER_SIZE, *p_buff_len - *p_index);
  321. //Encode Op Code.
  322. p_buff[(*p_index)++] = op_code;
  323. //Encode Status.
  324. *p_index += uint32_encode(return_code, &(p_buff[*p_index]));
  325. //update size of used buffer
  326. *p_buff_len = *p_index;
  327. return NRF_SUCCESS;
  328. }
  329. uint32_t op_status_cond_uint16_enc(uint8_t op_code,
  330. uint32_t return_code,
  331. uint16_t value,
  332. uint8_t * const p_buff,
  333. uint32_t * const p_buff_len,
  334. uint32_t * const p_index)
  335. {
  336. uint32_t status_code;
  337. uint32_t init_buff_len = *p_buff_len;
  338. status_code = op_status_enc(op_code, return_code, p_buff, p_buff_len, p_index);
  339. SER_ASSERT(status_code == NRF_SUCCESS, status_code);
  340. if (return_code == NRF_SUCCESS) //Add 16bit value when return_code is a success
  341. {
  342. *p_buff_len = init_buff_len; //restore original value - it has been modified by op_status_enc
  343. status_code = uint16_t_enc(&value, p_buff, *p_buff_len, p_index);
  344. *p_buff_len = *p_index;
  345. SER_ASSERT(status_code == NRF_SUCCESS, status_code);
  346. }
  347. return status_code;
  348. }
  349. uint32_t buf_enc(uint8_t const * const p_data,
  350. uint16_t const dlen,
  351. uint8_t * const p_buf,
  352. uint32_t buf_len,
  353. uint32_t * const p_index)
  354. {
  355. uint32_t err_code = NRF_SUCCESS;
  356. uint8_t is_present = (p_data == NULL) ? SER_FIELD_NOT_PRESENT : SER_FIELD_PRESENT;
  357. err_code = uint8_t_enc(&is_present, p_buf, buf_len, p_index);
  358. SER_ASSERT(err_code == NRF_SUCCESS, err_code);
  359. if (p_data)
  360. {
  361. SER_ASSERT_LENGTH_LEQ(dlen, ((int32_t)buf_len - *p_index));
  362. memcpy(&p_buf[*p_index], p_data, dlen);
  363. *p_index += dlen;
  364. }
  365. return err_code;
  366. }
  367. uint32_t buf_dec(uint8_t const * const p_buf,
  368. uint32_t buf_len,
  369. uint32_t * const p_index,
  370. uint8_t * * const pp_data,
  371. uint16_t data_len,
  372. uint16_t dlen)
  373. {
  374. uint8_t is_present = 0;
  375. SER_ASSERT_LENGTH_LEQ(1, ((int32_t)buf_len - *p_index));
  376. uint8_dec(p_buf, buf_len, p_index, &is_present);
  377. if (is_present == SER_FIELD_PRESENT)
  378. {
  379. SER_ASSERT_NOT_NULL(pp_data);
  380. SER_ASSERT_NOT_NULL(*pp_data);
  381. SER_ASSERT_LENGTH_LEQ(dlen, data_len);
  382. SER_ASSERT_LENGTH_LEQ(dlen, ((int32_t)buf_len - *p_index));
  383. memcpy(*pp_data, &p_buf[*p_index], dlen);
  384. *p_index += dlen;
  385. }
  386. else
  387. {
  388. if (pp_data)
  389. {
  390. *pp_data = NULL;
  391. }
  392. }
  393. return NRF_SUCCESS;
  394. }
  395. uint32_t uint8_vector_enc(uint8_t const * const p_data,
  396. uint16_t const dlen,
  397. uint8_t * const p_buf,
  398. uint32_t buf_len,
  399. uint32_t * const p_index)
  400. {
  401. SER_ASSERT_NOT_NULL(p_data);
  402. SER_ASSERT_NOT_NULL(p_buf);
  403. SER_ASSERT_NOT_NULL(p_index);
  404. SER_ASSERT_LENGTH_LEQ(dlen, ((int32_t)buf_len - *p_index));
  405. memcpy(&p_buf[*p_index], p_data, dlen);
  406. *p_index += dlen;
  407. return NRF_SUCCESS;
  408. }
  409. uint32_t uint8_vector_dec(uint8_t const * const p_buf,
  410. uint32_t buf_len,
  411. uint32_t * const p_index,
  412. uint8_t * const p_data,
  413. uint16_t dlen)
  414. {
  415. SER_ASSERT_NOT_NULL(p_data);
  416. SER_ASSERT_LENGTH_LEQ(dlen, ((int32_t)buf_len - *p_index));
  417. memcpy(p_data, &p_buf[*p_index], dlen);
  418. *p_index += dlen;
  419. return NRF_SUCCESS;
  420. }