ota_message.c 4.6 KB

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