mailbox_update_demo.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. return rx_frame;
  28. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FNOSPACE) {
  29. printf("%s, The file is too large, not enough space to save.\n", __func__);
  30. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FNOSUP) {
  31. printf("%s, This file does not support mailbox upgrade.\n", __func__);
  32. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FCHECKE) {
  33. printf("%s, file crc check fail, don't update.\n", __func__);
  34. } else if (rx_frame->FrameRspStatus == MUFRS_ACK_FBURNE) {
  35. printf("%s, file burn fail, don't update.\n", __func__);
  36. } else {
  37. printf("%s, unknown error.\n", __func__);
  38. }
  39. return NULL;
  40. }
  41. int MailboxUpdateSendFrame(mb_msg_type_t msg_type, MailboxUpdateFrame *frame)
  42. {
  43. int retry = 3;
  44. while (retry--) {
  45. if (!mailbox_msg_send(msg_type, (uint8_t *)frame, sizeof(MailboxUpdateFrame), pdMS_TO_TICKS(50))) {
  46. return 0;
  47. }
  48. printf("%s, mailbox send frame fail, retry %d.\n", __func__, 3 - retry);
  49. }
  50. return -1;
  51. }
  52. MailboxUpdateFrame * MailboxUpdateReceiveFrame(mb_msg_type_t type, mb_rxmsg_t *msg, uint32_t timeout)
  53. {
  54. memset(msg, 0, sizeof(mb_rxmsg_t));
  55. if (mailbox_msg_receive(type, msg, timeout) != pdTRUE) {
  56. printf("%s, mailbox rx timeout.\n", __func__);
  57. return NULL;
  58. }
  59. return MailboxUpdateHandleMsg(msg);
  60. }
  61. #endif /* MAILBOX_UPDATE_SUPPORT */