2
0

NanoPBHelper.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 "HTTPClient.h"
  7. #include <string>
  8. std::vector<uint8_t> pbEncode(const pb_msgdesc_t *fields, const void *src_struct);
  9. pb_bytes_array_t* vectorToPbArray(const std::vector<uint8_t>& vectorToPack);
  10. void packString(char* &dst, std::string stringToPack);
  11. std::vector<uint8_t> pbArrayToVector(pb_bytes_array_t* pbArray);
  12. template <typename T>
  13. T pbDecode(const pb_msgdesc_t *fields, std::vector<uint8_t> &data)
  14. {
  15. T result = {};
  16. // Create stream
  17. pb_istream_t stream = pb_istream_from_buffer(&data[0], data.size());
  18. // Decode the message
  19. if (pb_decode(&stream, fields, &result) == false) {
  20. printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
  21. }
  22. return result;
  23. }
  24. template <typename T>
  25. void pbDecode(T &result, const pb_msgdesc_t *fields, std::vector<uint8_t> &data)
  26. {
  27. // Create stream
  28. pb_istream_t stream = pb_istream_from_buffer(&data[0], data.size());
  29. // Decode the message
  30. if (pb_decode(&stream, fields, &result) == false) {
  31. printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
  32. }
  33. }
  34. void pbPutString(const std::string &stringToPack, char* dst);
  35. void pbPutCharArray(const char * stringToPack, char* dst);
  36. void pbPutBytes(const std::vector<uint8_t> &data, pb_bytes_array_t &dst);
  37. const char* pb_encode_to_string(const pb_msgdesc_t *fields, const void *data);
  38. pb_istream_t pb_istream_from_http(bell::HTTPClient::HTTPResponse *response, size_t length = 0);
  39. #endif