amessage_encode_to_file.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <rtthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <dfs_posix.h>
  5. #include "amessage.pb-c.h"
  6. static void protobuf_encode_to_file(int argc, char *argv[])
  7. {
  8. void *buffer; // Buffer to store serialized data
  9. unsigned msg_len; // Length of serialized data
  10. if (argc != 3)
  11. {
  12. rt_kprintf("Usage: %s a b\n", argv[0]);
  13. return ;
  14. }
  15. // Encode message to buffer
  16. {
  17. rt_kprintf("---- Encoding ---\n");
  18. AMessage encode_msg = AMESSAGE__INIT; // AMessage
  19. encode_msg.has_a = 1;
  20. encode_msg.a = atoi(argv[1]);
  21. encode_msg.has_b = 1;
  22. encode_msg.b = atoi(argv[2]);
  23. msg_len = amessage__get_packed_size(&encode_msg);
  24. buffer = malloc(msg_len);
  25. rt_kprintf("Encoding %d serialized bytes\n", msg_len);
  26. amessage__pack(&encode_msg, buffer);
  27. rt_kprintf("---- Saving ---\n");
  28. int fd = open("/amessage.onnx", O_WRONLY | O_CREAT | O_TRUNC);
  29. if (fd>= 0)
  30. {
  31. write(fd, buffer, msg_len);
  32. close(fd);
  33. rt_kprintf("Written to file amessage.onnx.\n");
  34. }
  35. }
  36. free(buffer);
  37. return;
  38. }
  39. MSH_CMD_EXPORT(protobuf_encode_to_file, protobuf-c encode to file test);