amessage_decode_from_file.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <rtthread.h>
  2. #include <rtdevice.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <pb_encode.h>
  6. #include <pb_decode.h>
  7. #include "amessage.pb.h"
  8. #include <dfs_posix.h>
  9. static void nanopb_decode_from_file(int argc,char *argv[])
  10. {
  11. /* This is the buffer where we will store our message. */
  12. int ret;
  13. struct stat buf;
  14. ret = stat("/amessage.onnx", &buf);
  15. if(ret == 0)
  16. {
  17. rt_kprintf("amessage.onnx file size = %d\n", buf.st_size);
  18. }
  19. else
  20. {
  21. rt_kprintf("amessage.onnx file not fonud\n");
  22. }
  23. size_t message_length = buf.st_size;
  24. uint8_t buffer[128];
  25. bool status;
  26. int fd = open("/amessage.onnx", O_RDONLY);
  27. if (fd>= 0)
  28. {
  29. int size = read(fd, buffer, message_length);
  30. close(fd);
  31. rt_kprintf("Read from file amessage.onnx \n");
  32. if (size < 0)
  33. return ;
  34. }
  35. {
  36. /* Allocate space for the decoded message. */
  37. AMessage amessage = AMessage_init_zero;
  38. /* Create a stream that reads from the buffer. */
  39. pb_istream_t stream = pb_istream_from_buffer(buffer, message_length);
  40. /* Now we are ready to decode the message. */
  41. status = pb_decode(&stream, AMessage_fields, &amessage);
  42. /* Check for errors... */
  43. if (!status)
  44. {
  45. rt_kprintf("Decoding failed: %s\n", PB_GET_ERROR(&stream));
  46. return ;
  47. }
  48. /* Print the data contained in the message. */
  49. rt_kprintf("amessage.aa = %d\n", (int)amessage.aa);
  50. rt_kprintf("amessage.bb = %d\n", (int)amessage.bb);
  51. }
  52. return;
  53. }
  54. MSH_CMD_EXPORT(nanopb_decode_from_file, nanopb decode from file);