cond_field_serialization.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include "nrf_error.h"
  2. #include "cond_field_serialization.h"
  3. #include "ble_serialization.h"
  4. #include <stddef.h>
  5. uint32_t cond_field_enc(void const * const p_field,
  6. uint8_t * const p_buf,
  7. uint32_t buf_len,
  8. uint32_t * const p_index,
  9. field_encoder_handler_t fp_field_encoder)
  10. {
  11. uint32_t err_code = NRF_SUCCESS;
  12. SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index);
  13. p_buf[*p_index] = (p_field == NULL) ? SER_FIELD_NOT_PRESENT : SER_FIELD_PRESENT;
  14. *p_index += 1;
  15. if (p_field && (fp_field_encoder != NULL))
  16. {
  17. err_code = fp_field_encoder(p_field, p_buf, buf_len, p_index);
  18. }
  19. return err_code;
  20. }
  21. uint32_t cond_field_dec(uint8_t const * const p_buf,
  22. uint32_t buf_len,
  23. uint32_t * const p_index,
  24. void * * const pp_field,
  25. field_decoder_handler_t fp_field_parser)
  26. {
  27. uint32_t err_code = NRF_SUCCESS;
  28. uint8_t is_present;
  29. SER_ASSERT_LENGTH_LEQ(1, buf_len - *p_index);
  30. uint8_dec(p_buf, buf_len, p_index, &is_present);
  31. if (is_present == SER_FIELD_PRESENT)
  32. {
  33. SER_ASSERT_NOT_NULL(pp_field);
  34. SER_ASSERT_NOT_NULL(*pp_field);
  35. if (fp_field_parser != NULL)
  36. {
  37. err_code = fp_field_parser(p_buf, buf_len, p_index, *pp_field);
  38. }
  39. }
  40. else if (is_present == SER_FIELD_NOT_PRESENT)
  41. {
  42. if (pp_field != NULL)
  43. {
  44. *pp_field = NULL;
  45. }
  46. }
  47. else
  48. {
  49. err_code = NRF_ERROR_INVALID_DATA;
  50. }
  51. return err_code;
  52. }