ota_message.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include "ota_message.h"
  2. #include "gd32f10x.h"
  3. #include "w25q32.h"
  4. #include "ec800m.h"
  5. #include "usart.h"
  6. #include "delay.h"
  7. #include <stdio.h>
  8. #define BufferSize 100 //最大获取的数据空间
  9. static OTA_MESSAGE ota_message = {0};
  10. void clear_ota_message_config_block(void);
  11. void write_ota_message_to_flash(uint32_t *data, int size){
  12. uint8_t *pdata = (uint8_t *)data;
  13. W25Q32_PageWrite(pdata,256*OTA_EVENT_BLOCK);//写在第二扇区的第一页中
  14. }
  15. /*
  16. * 函数名:save_ota_message_config_params(OTA_MESSAGE *params)
  17. * 输入参数:无
  18. * 输出参数:无
  19. * 返回值:无
  20. * 函数作用:保存ota事件的信息
  21. */
  22. int save_ota_message_config_params(OTA_MESSAGE *params)
  23. {
  24. if(params == NULL)
  25. return -1;
  26. memset(&ota_message, 0, sizeof(OTA_MESSAGE));
  27. memcpy(&ota_message, params, sizeof(OTA_MESSAGE));
  28. clear_ota_message_config_block();
  29. write_ota_message_to_flash((uint32_t *)&ota_message,sizeof(OTA_MESSAGE));
  30. return 0;
  31. }
  32. // 模块下载download校验值
  33. static uint16_t checksum(const char *str, uint16_t len)
  34. {
  35. uint16_t sum = 0;
  36. uint8_t odd = 0;
  37. // 如果字符串长度为奇数,则将最后一个字符设置为高8位,低8位设置为0
  38. if (len % 2 == 1)
  39. {
  40. odd = 1;
  41. len--;
  42. }
  43. // 将每两个字符作为一个16位的数值进行异或操作
  44. for (uint16_t i = 0; i < len; i += 2)
  45. {
  46. sum ^= ((uint16_t)str[i] << 8) | (uint16_t)str[i + 1];
  47. }
  48. // 如果字符串长度为奇数,则还需要将最后一个字符与0xFF00异或
  49. if (odd)
  50. {
  51. sum ^= (uint16_t)str[len] << 8;
  52. }
  53. // 返回校验和
  54. return sum;
  55. }
  56. //返回str在数组中的索引
  57. static char* find_string(char *strs, char *str, int len)
  58. {
  59. int i = 0;
  60. char *start = NULL;
  61. while(i < len){
  62. start = strstr(strs, str);
  63. if(start != NULL){
  64. break;
  65. }
  66. i++;
  67. strs++;
  68. }
  69. if(i == len + 1){
  70. return NULL;
  71. }
  72. return start;
  73. }
  74. /*
  75. * 函数名:static void extract_data_from_buffer(const char* buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  76. * 输入参数:buffer字符串
  77. * 输出参数:json有效字符串长度len_ptr,checkCode_ptr校验码指针
  78. * 返回值:无
  79. * 函数作用:eg. QFDWL: 621,3e23 从json信息最后端取出这段json的有效长度和校验码
  80. */
  81. static void extract_data_from_buffer(char* buffer, uint32_t *len_ptr, uint16_t *checkCode_ptr)
  82. {
  83. char *start = find_string(buffer, "+QFDWL:",BufferSize);
  84. if (start != NULL)
  85. {
  86. start += 8; // 跳过"+QFDWL:"
  87. uint32_t len = 0;
  88. sscanf(start, "%u,", &len); // 读取长度
  89. start = strchr(start, ',') + 1; // 跳过逗号
  90. uint16_t checkCode = 0;
  91. sscanf(start, "%hx", &checkCode); // 读取16进制数据
  92. // 将提取的数据存入形参
  93. *len_ptr = len;
  94. *checkCode_ptr = checkCode;
  95. }
  96. }
  97. bool WaitForUpData(char* dmaBuffer)
  98. {
  99. if (UART0_RX_STAT > 0)
  100. {
  101. UART0_RX_STAT = 0;
  102. uint32_t len;
  103. uint16_t checkCode;
  104. char *temp;
  105. extract_data_from_buffer(dmaBuffer, &len, &checkCode);
  106. uint16_t jsonCheck = checksum(dmaBuffer, len);
  107. if (checkCode == jsonCheck)
  108. {
  109. return true;
  110. }
  111. else
  112. {
  113. return false;
  114. }
  115. }
  116. }
  117. /*
  118. * 函数名:load_config_params(CONFIG_PARAMS *params)
  119. * 输入参数:无
  120. * 输出参数:无
  121. * 返回值:ota升级标志是否有效 0有效 -1无效
  122. * 函数作用:从w25q32中加载ota信息
  123. */
  124. int load_ota_message_config_params()
  125. {
  126. //检查校验码
  127. char *dmabuffer=malloc(BufferSize);
  128. __LOAD_CONFIG:
  129. // get data from UFS
  130. dma_config_change(dmabuffer,BufferSize);
  131. Delay_Ms(1000);
  132. EC800MSendCmd(LOAD_otaMSG_FILE, strlen(LOAD_otaMSG_FILE)); //"AT+QFDWL=otaMSG.txt\r\n"
  133. Delay_Ms(1000);
  134. dma_config();
  135. if(WaitForUpData(dmabuffer) == false) goto __LOAD_CONFIG;
  136. // W25Q32_Read((uint8_t *)&ota_message, OTA_EVENT_BLOCK*64*1024, sizeof(OTA_MESSAGE)); //从W25Q32中读取结构体数据
  137. // FLASH_Read(OTA_EVENT_START_ADDR,&ota_message,sizeof(OTA_MESSAGE));
  138. memset(&ota_message,0,sizeof(OTA_MESSAGE));
  139. memcpy((uint8_t*)&ota_message,dmabuffer,sizeof(OTA_MESSAGE));
  140. free(dmabuffer);
  141. if(ota_message.otaflag == 1U)
  142. {
  143. return 0;
  144. }
  145. else
  146. {
  147. return -1;
  148. }
  149. }
  150. OTA_MESSAGE *get_config_params()
  151. {
  152. return &ota_message;
  153. }
  154. /*
  155. * 函数名:void clear_ota_message_config_block(void)
  156. * 输入参数:无
  157. * 输出参数:无
  158. * 返回值:无
  159. * 函数作用:清除block内包含的ota事件信息
  160. */
  161. void clear_ota_message_config_block(void)
  162. {
  163. static OTA_MESSAGE ota_message = {0};
  164. ota_message.otaflag = 1;
  165. ota_message.XmodemByte = 200 * 1024;
  166. EC800MSendCmd(OPEN_otaMSG_FILE,strlen(OPEN_otaMSG_FILE));
  167. if(WaitResponse("QFOPEN:", 2000) == 0)
  168. return;
  169. EC800MSendCmd(WRITE_otaMSG_FILE,strlen(WRITE_otaMSG_FILE));
  170. if(WaitResponse("CONNECT", 2000))
  171. return;
  172. EC800MSendCmd((uint8_t *)&ota_message,sizeof(OTA_MESSAGE));
  173. if(WaitResponse("QFWRITE", 5000))
  174. return;
  175. EC800MSendCmd(CLOSE_otaMSG_FILE,strlen(CLOSE_otaMSG_FILE));
  176. if(WaitResponse("OK", 2000))
  177. return;
  178. // EC800MSendCmd("AT+QFDEL=\"UFS:otaMSG.txt\r\n\"",27); //删除文件
  179. }