ble_serialization.c 17 KB

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