mailbox_update_demo.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include "FreeRTOS.h"
  5. #include "board.h"
  6. #include "mailbox_message.h"
  7. #include "mailbox_update/mailbox_update_demo.h"
  8. #ifdef MAILBOX_UPDATE_SUPPORT
  9. static MailboxUpdateFrame *MailboxUpdateHandleMsg(mb_rxmsg_t *msg)
  10. {
  11. MailboxUpdateFrame *rx_frame;
  12. if (!msg)
  13. return NULL;
  14. if (msg->dl != sizeof(MailboxUpdateFrame)) {
  15. printf("%s, mailbox rev msg length err.\n", __func__);
  16. return NULL;
  17. }
  18. rx_frame = (MailboxUpdateFrame *)msg->data;
  19. if (!rx_frame) {
  20. printf("%s, mailbox update data abnormal.\n", __func__);
  21. return NULL;
  22. }
  23. if (rx_frame->FrameRspStatus == MUFRS_ACK_OK) {
  24. return rx_frame;
  25. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FSAME) {
  26. printf("checksum is the same as now, don't update.\n");
  27. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FNOSPACE) {
  28. printf("%s, The file is too large, not enough space to save.\n", __func__);
  29. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FNOSUP) {
  30. printf("%s, This file does not support mailbox upgrade.\n", __func__);
  31. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FCHECKE) {
  32. printf("%s, file crc check fail, don't update.\n", __func__);
  33. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FBURNE) {
  34. printf("%s, file burn fail, don't update.\n", __func__);
  35. } else {
  36. printf("%s, unknown error.\n", __func__);
  37. }
  38. return NULL;
  39. }
  40. int MailboxUpdateSendFrame(mb_msg_type_t msg_type, MailboxUpdateFrame *frame)
  41. {
  42. int retry = 3;
  43. while (retry--) {
  44. if (!mailbox_msg_send(msg_type, (uint8_t *)frame, sizeof(MailboxUpdateFrame), pdMS_TO_TICKS(50))) {
  45. return 0;
  46. }
  47. printf("%s, mailbox send frame fail, retry %d.\n", __func__, 3 - retry);
  48. }
  49. return -1;
  50. }
  51. MailboxUpdateFrame * MailboxUpdateReceiveFrame(mb_msg_type_t type, mb_rxmsg_t *msg, uint32_t timeout)
  52. {
  53. memset(msg, 0, sizeof(mb_rxmsg_t));
  54. if (mailbox_msg_receive(type, msg, timeout) != pdTRUE) {
  55. printf("%s, mailbox rx timeout.\n", __func__);
  56. return NULL;
  57. }
  58. return MailboxUpdateHandleMsg(msg);
  59. }
  60. #endif /* MAILBOX_UPDATE_SUPPORT */