2
0

NanoPBHelper.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef BELL_NANOPB_HELPER_H
  2. #define BELL_NANOPB_HELPER_H
  3. #include <vector>
  4. #include "pb_encode.h"
  5. #include "pb_decode.h"
  6. #include <string>
  7. std::vector<uint8_t> pbEncode(const pb_msgdesc_t *fields, const void *src_struct);
  8. pb_bytes_array_t* vectorToPbArray(const std::vector<uint8_t>& vectorToPack);
  9. void packString(char* &dst, std::string stringToPack);
  10. std::vector<uint8_t> pbArrayToVector(pb_bytes_array_t* pbArray);
  11. template <typename T>
  12. T pbDecode(const pb_msgdesc_t *fields, std::vector<uint8_t> &data)
  13. {
  14. T result = {};
  15. // Create stream
  16. pb_istream_t stream = pb_istream_from_buffer(&data[0], data.size());
  17. // Decode the message
  18. if (pb_decode(&stream, fields, &result) == false) {
  19. printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
  20. }
  21. return result;
  22. }
  23. template <typename T>
  24. void pbDecode(T &result, const pb_msgdesc_t *fields, std::vector<uint8_t> &data)
  25. {
  26. // Create stream
  27. pb_istream_t stream = pb_istream_from_buffer(&data[0], data.size());
  28. // Decode the message
  29. if (pb_decode(&stream, fields, &result) == false) {
  30. printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
  31. }
  32. }
  33. void pbFree(const pb_msgdesc_t *fields, void *src_struct);
  34. #endif