EC800_FTP_OTA.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* 连接到FTP服务器 ,从FTP服务器中下载文件,下载逻辑 */
  2. /* includes ----------------------------------------------------------*/
  3. #include "EC800_FTP_OTA.h"
  4. /* typedef -----------------------------------------------------------*/
  5. ftpInfo s_ftpInfo = {
  6. // .account = "cdzupdate",
  7. // .passWord = "cdz2021!Z",
  8. .fileType = 1,
  9. .transmode = 1,
  10. .rsptimeout = 90,
  11. // .ftpAddr = "39.98.211.244",
  12. // .ftpPort = 21,
  13. // .textDirectory = "/data/cdz",
  14. // .textName = "centralCtrSys.bin",
  15. };
  16. /* define ------------------------------------------------------------*/
  17. // 登陆到FTP服务器
  18. // 第一步:配置和激活 PDP 上下文
  19. #define AT_QIACT_1 "AT+QIACT=1\r\n" // 激活 PDP 上下文 1
  20. #define AT_QIACT_query "AT+QIACT?\r\n" // 查询 PDP 上下文状态
  21. #define AT_QFTPCFG_ID_1 "AT+QFTPCFG=\"contextid\",1\r\n" // 查询 PDP 上下文状态
  22. // 第二步: 配置用户账号和传输设置
  23. #define AT_QFTPCFG_ACCOUNT "AT+QFTPCFG=\"account\",\"%s\",\"%s\"\r\n" // 设置用户名和密码。
  24. #define AT_QFTPCFG_FILE(fileType) "AT+QFTPCFG=\"filetype\"," #fileType "\r\n" // 设置文件类型为ASCII AT+QFTPCFG="filetype",1
  25. #define AT_QFTPCFG_TRANS(mode) "AT+QFTPCFG=\"transmode\"," #mode "\r\n" // 设置为被动传输方式AT+QFTPCFG="transmode",1
  26. #define AT_QFTPCFG_TIMEOUT(time) "AT+QFTPCFG=\"rsptimeout\"," #time "\r\n" // 设置最大响应时间(默认为90秒)
  27. //第三步:登录FTP服务器。
  28. #define AT_QFTPOPEN(addr,port) "AT+QFTPOPEN=\"" #addr "\"," #port "\r\n" // 登录FTP服务器。
  29. // 从FTP服务器下载文件 本项目文件较小,选择直接通过COM口输出下载数据
  30. #define AT_QFTPCWD(directory) "AT+QFTPCWD=\"" #directory "\"\r\n" // 设置当前目录。
  31. #define AT_QFTPSIZE(textName) "AT+QFTPSIZE=\"" #textName "\"\r\n" // 查询 FTP(S)服务器 test_my1.txt 文件大小
  32. #define AT_QFTPGET(textName, startByte, downloadNum) \
  33. "AT+QFTPGET=\"" #textName "\",\"COM:\"," #startByte "," #downloadNum "\r\n" // 下载文件, 通过COM口输出特定字段的数据
  34. // 一次下载的字节个数
  35. #define DOWNLOAD_BYTE_LEN (512)
  36. /* macro -------------------------------------------------------------*/
  37. /* variables ---------------------------------------------------------*/
  38. /* function prototypes -----------------------------------------------*/
  39. /* 1.登陆到FTP服务器--------------------------------------------------------*/
  40. static uint8_t logIn_step = 0;
  41. /**
  42. * @brief 通过EC800登录FTP
  43. * @param NONE
  44. * @note NONE
  45. * @retval 无
  46. */
  47. void EC800_LogIn_FTP(void){
  48. uint8_t errorCnt = 0; // 错误计数
  49. ftpInfo *p_ftpInfo = &s_ftpInfo;
  50. char atCmd[100] = {0}; // 静态缓冲区用于存放AT命令
  51. p_ftpInfo->fileType = p_ftpInfo->fileType; // 避免报警告
  52. switch(logIn_step){
  53. case 0: // 配置和激活 PDP 上下文
  54. // EC800M_SendCommand(AT_QIACT_1);
  55. //
  56. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  57. //
  58. // if (found != NULL) {
  59. // printf("Activation successful\r\n");
  60. // }else{
  61. // errorCnt++;
  62. // printf("Activation unsuccessful\r\n");
  63. // }
  64. EC800M_SendCommand(AT_QIACT_query);
  65. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  66. // printf("PDP Context Status OK\r\n");
  67. }else{
  68. errorCnt++;
  69. // printf("PDP Context Status error\r\n");
  70. }
  71. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  72. EC800M_SendCommand(AT_QFTPCFG_ID_1);
  73. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  74. // printf("Set PDP context to 1.\r\n");
  75. }else{
  76. errorCnt++;
  77. // printf("ERROR! Set PDP context to 1.\r\n");
  78. }
  79. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  80. if(errorCnt == 0){
  81. logIn_step = 1;
  82. }
  83. break;
  84. case 1: // 配置用户账号和传输设置
  85. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"account\",\"%s\",\"%s\"\r\n", p_ftpInfo->account, p_ftpInfo->passWord);
  86. EC800M_SendCommand(atCmd);
  87. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  88. // printf("FINISH! Set username and password.\r\n");
  89. }else{
  90. errorCnt++;
  91. // printf("ERROR! Set username and password.\r\n");
  92. }
  93. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  94. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"filetype\",%d\r\n", p_ftpInfo->fileType);
  95. EC800M_SendCommand(atCmd);
  96. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  97. // printf("FINISH! Set username and password.\r\n");
  98. }else{
  99. errorCnt++;
  100. // printf("ERROR! Set username and password.\r\n");
  101. }
  102. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  103. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"transmode\",%d\r\n", p_ftpInfo->transmode);
  104. EC800M_SendCommand(atCmd);
  105. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  106. // printf("FINISH! Set to passive transfer mode.\r\n");
  107. }else{
  108. errorCnt++;
  109. // printf("ERROR! Set to passive transfer mode.\r\n");
  110. }
  111. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  112. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"rsptimeout\",%d\r\n", p_ftpInfo->rsptimeout);
  113. EC800M_SendCommand(atCmd);
  114. if (Accept_and_Compare_Str(AT_RESP_OK) == 1) {
  115. // printf("FINISH! Set username and password.\r\n");
  116. }else{
  117. errorCnt++;
  118. // printf("ERROR! Set username and password.\r\n");
  119. }
  120. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清除数据buff
  121. if(errorCnt == 0){
  122. logIn_step = 2;
  123. }
  124. break;
  125. case 2: // 登录FTP服务器。
  126. snprintf(atCmd, sizeof(atCmd), "AT+QFTPOPEN=\"%s\",%d\r\n", p_ftpInfo->ftpAddr, p_ftpInfo->ftpPort);
  127. EC800M_SendCommand(atCmd);
  128. errorCnt = Accept_and_Compare_Str("+QFTPOPEN: 0,0");
  129. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  130. if(errorCnt == 1){
  131. errorCnt = 0;
  132. // printf("FINISH! Log in to the FTP server.\r\n");
  133. }else{
  134. // printf("ERROR! Log in to the FTP server.\r\n");
  135. }
  136. if(errorCnt == 0){
  137. logIn_step = 3;
  138. }
  139. break;
  140. default :
  141. break;
  142. }
  143. }
  144. /* 2.从FTP服务器上下载文件--------------------------------------------------------*/
  145. /**
  146. * @brief 剔除除去固件的其他的数据
  147. * @param NONE
  148. * @note NONE
  149. * @retval 无
  150. */
  151. int extract_data_as_uint32(char* input, uint32_t* output, size_t output_size) {
  152. const char* start_marker = "\r\nCONNECT\r\n";
  153. const char* end_marker = "\r\nOK\r\n";
  154. const char* start;
  155. const char* end;
  156. // 寻找开始标记
  157. start = strstr(input, start_marker);
  158. if (start != NULL) {
  159. // 如果发现开始标记,移动指针越过开始标记
  160. start += strlen(start_marker);
  161. } else {
  162. // 如果没有发现开始标记,则从输入串的开始处理
  163. start = input;
  164. }
  165. // 寻找结束标记
  166. end = search_sequence(start, USART4_REC_LEN - (start - input), end_marker, strlen(end_marker));
  167. if (end == NULL) {
  168. return HAL_ERROR;
  169. }
  170. // 计算要提取数据的长度
  171. size_t data_length = end - start;
  172. // 确保提取的数据长度为4的倍数,适用于 uint32_t
  173. if (data_length % sizeof(uint32_t) != 0) {
  174. return HAL_ERROR;
  175. }
  176. // 计算 uint32_t 的数量
  177. size_t data_length_uint32 = data_length / sizeof(uint32_t);
  178. if (data_length_uint32 > output_size) { // 确保输出缓冲区能够存放提取的数据
  179. return HAL_ERROR;
  180. }
  181. // 提取数据并转换为 uint32_t 数组
  182. memcpy(output, start, data_length_uint32 * sizeof(uint32_t));
  183. memset(input, 0, USART4_REC_LEN); // 清空输入缓冲区
  184. return HAL_OK;
  185. }
  186. uint8_t writeFlashPage(uint32_t address, uint32_t *pData) {
  187. if (FLASH_Erase(address, 1) != HAL_OK) {
  188. return 0;
  189. }
  190. if (FLASH_Write(address, pData, (FLASH_PAGE_SIZE / sizeof(uint32_t))) != HAL_OK) {
  191. return 0;
  192. }
  193. return 1;
  194. }
  195. #define AT_CMD_SIZE 100
  196. uint32_t totalBytesReceived = 0; // 当前接收的固件字节数
  197. uint32_t currentFlashAddress = APP2_ADDRESS;
  198. uint32_t buffer[FLASH_PAGE_SIZE / sizeof(uint32_t)] = {0}; // 2KB缓冲区
  199. uint32_t bufferIndex = 0; // 缓冲区当前索引(以uint32_t为单位)
  200. /**
  201. * @brief 更新固件
  202. * @note 执行过程中不能使用printf(会触发hardfault),暂不清楚原因
  203. * @retval 0表示未完成更新,1表示更新完成
  204. */
  205. uint8_t UpdateFirmware(void) {
  206. ftpInfo *p_ftpInfo = &s_ftpInfo;
  207. char atCmd[AT_CMD_SIZE] = {0}; // 用于存放AT命令的静态缓冲区
  208. if (totalBytesReceived < p_ftpInfo->filesize) {
  209. // 计算剩余要接收的字节数
  210. uint32_t remaining = p_ftpInfo->filesize - totalBytesReceived;
  211. uint32_t bytesToRead = remaining < DOWNLOAD_BYTE_LEN ? remaining : DOWNLOAD_BYTE_LEN;
  212. // 设置FTP信息
  213. p_ftpInfo->startAddr = totalBytesReceived;
  214. p_ftpInfo->byteNum = bytesToRead;
  215. // 发送AT指令以开始接收
  216. snprintf(atCmd, AT_CMD_SIZE, "AT+QFTPGET=\"%s\",\"COM:\",%u,%u\r\n",
  217. p_ftpInfo->textName, p_ftpInfo->startAddr, bytesToRead);
  218. EC800M_SendCommand(atCmd);
  219. // 只有当还有数据需要接收时,才进行下载和写入操作
  220. snprintf(atCmd, AT_CMD_SIZE, "+QFTPGET: 0,");
  221. if (Accept_and_Compare_Str(atCmd) == 1) { // 接收成功
  222. // 临时缓冲区,用于存放这次接收的数据
  223. uint32_t tempBuffer[DOWNLOAD_BYTE_LEN / sizeof(uint32_t)] = {0};
  224. memset(tempBuffer, 0xff, DOWNLOAD_BYTE_LEN); // 保证内存数据为缺省值0xff
  225. if (extract_data_as_uint32(g_usart4_rx_buf, tempBuffer, sizeof(tempBuffer) / sizeof(uint32_t)) != HAL_OK) {
  226. // printf("ERROR! UpdateFirmware_extract_data\n");
  227. return 0;
  228. }
  229. // 将临时缓冲区中的数据追加到主缓冲区
  230. for (uint32_t i = 0; i < (DOWNLOAD_BYTE_LEN / sizeof(uint32_t)); i++) {
  231. buffer[bufferIndex++] = tempBuffer[i];
  232. // 如果主缓冲区满了,写入Flash
  233. if (bufferIndex == (FLASH_PAGE_SIZE / sizeof(uint32_t))) {
  234. s_param_boot.pageNumMove += 1;
  235. if (!writeFlashPage(currentFlashAddress, buffer)) {
  236. // printf("ERROR! Write to flash failed\n");
  237. return 0;
  238. }
  239. // 重置缓冲区索引,准备下一轮数据接收
  240. memset(buffer, 0xFF, FLASH_PAGE_SIZE); // 保证内存数据为缺省值0xff
  241. bufferIndex = 0;
  242. currentFlashAddress += FLASH_PAGE_SIZE;
  243. }
  244. }
  245. totalBytesReceived += bytesToRead;
  246. } else {
  247. // printf("ERROR! Receive fault!\n");
  248. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  249. return 0;
  250. }
  251. // 如果下载完成,则处理剩余数据并写入最后一部分
  252. if (totalBytesReceived >= p_ftpInfo->filesize) {
  253. // 检查buffer是否有未写入的数据
  254. if (bufferIndex > 0) {
  255. s_param_boot.pageNumMove += 1;
  256. // 写入最后一页数据
  257. if (!writeFlashPage(currentFlashAddress, buffer)) {
  258. // printf("ERROR! Write to flash failed\n");
  259. return 0;
  260. }
  261. }
  262. return 1; // 更新完成
  263. }
  264. }
  265. return 0; // 更新未完成
  266. }
  267. uint8_t downloadStep = 0;
  268. /**
  269. * @brief FTP服务器上下载文件
  270. * @param NONE
  271. * @note 每一个任务周期过来下载2k的数据,以保证其他任务的正常运行
  272. * @retval 无
  273. */
  274. void EC800_FTP_DownloadText(void){
  275. uint8_t errorCnt = 0; // 错误计数
  276. ftpInfo *p_ftpInfo = &s_ftpInfo;
  277. char atCmd[100] = {0}; // 静态缓冲区用于存放AT命令
  278. uint8_t backTemp = 0; // 返回值临时变量
  279. switch(downloadStep){
  280. case 0: // 设置文件目录
  281. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCWD=\"%s\"\r\n", p_ftpInfo->textDirectory);
  282. EC800M_SendCommand(atCmd);
  283. errorCnt = Accept_and_Compare_Str("+QFTPCWD: 0,0");
  284. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  285. if (errorCnt == 1) {
  286. errorCnt = 0;
  287. // printf("FINISH! Set the file directory.\r\n");
  288. }else{
  289. // printf("ERROR! Set the file directory.\r\n");
  290. }
  291. if(errorCnt == 0){
  292. downloadStep = 1;
  293. }
  294. break;
  295. case 1: // 获取文件长度
  296. snprintf(atCmd, sizeof(atCmd), "AT+QFTPSIZE=\"%s\"\r\n", p_ftpInfo->textName);
  297. EC800M_SendCommand(atCmd);
  298. errorCnt = Accept_and_Compare_Str("+QFTPSIZE: 0,");
  299. if (errorCnt == 1) {
  300. errorCnt = 0;
  301. // printf("FINISH! Get the length of the file.\r\n");
  302. // 使用sscanf从字符串中解析整数
  303. if(sscanf(g_usart4_rx_buf, "\r\nOK\r\n\r\n+QFTPSIZE: %*d,%u", &(p_ftpInfo->filesize)) == 1) {
  304. // 解析成功,filesize变量现在包含值1000
  305. // printf("The filesize is: %u\n", p_ftpInfo->filesize);
  306. } else {
  307. // 解析失败
  308. errorCnt++;
  309. // printf("Failed to parse the filesize.\n");
  310. }
  311. }else{
  312. // printf("ERROR! Get the length of the file.\r\n");
  313. }
  314. memset(g_usart4_rx_buf, 0, USART4_REC_LEN); // 清空输入缓冲区
  315. if(errorCnt == 0){
  316. downloadStep = 2;
  317. }
  318. break;
  319. case 2:
  320. // 判断当前所处APP地址 选取更新地址
  321. currentFlashAddress = APP2_ADDRESS;
  322. downloadStep = 3;
  323. break;
  324. case 3: // 更新固件
  325. backTemp = UpdateFirmware();
  326. if(backTemp == 1){
  327. downloadStep = 4;
  328. }
  329. break;
  330. case 4: // 固件更新完成,设置更新标志,并复位
  331. s_param_boot.updateFlag = 1; // 需要更新固件并搬移
  332. Write_paramArea();
  333. downloadStep = 5;
  334. break;
  335. default :
  336. break;
  337. }
  338. }
  339. /* 3. OTA升级从FTP流程--------------------------------------------------------*/
  340. uint8_t upgradeStep = 0;
  341. /**
  342. * @brief OTA升级从FTP
  343. * @param NONE
  344. * @note NONE
  345. * @retval 无
  346. */
  347. void EC800_FTP_OTA_Upgrade(void){
  348. ec800Date *p_ec800Date = &s_ec800Date;
  349. global_par *p_global_par = &s_global_par;
  350. comData *p_comData = &s_comData;
  351. // 等待ec800模块初始化完成 并满足更新条件
  352. if(!((p_ec800Date->ec800InitFlag) && (p_ec800Date->hardwareUpdate)
  353. && (BIT_CHECK(p_comData->Malfunction, OTA_fault) == 0))){
  354. return;
  355. }
  356. p_global_par->otaUpgradeStartFlag = 1; // 置位OTA升级开始标志
  357. switch (upgradeStep){
  358. case 0: // 登陆到FTP服务器
  359. EC800_LogIn_FTP();
  360. if(logIn_step == 3){
  361. upgradeStep = 1;
  362. }
  363. break;
  364. case 1: // 从FTP服务器上下载文件
  365. EC800_FTP_DownloadText();
  366. if(downloadStep == 5){
  367. upgradeStep = 2;
  368. }
  369. break;
  370. case 2: // 复位升级
  371. osDelay(1000);
  372. HAL_NVIC_SystemReset();
  373. break;
  374. default:
  375. break;
  376. }
  377. }
  378. /**
  379. * @brief OTA升级故障处理
  380. * @param NONE
  381. * @note NONE
  382. * @retval 无
  383. */
  384. void handleOTAUpgradeFault(void){
  385. comData *p_comData = &s_comData;
  386. ec800Date *p_ec800Date = &s_ec800Date;
  387. // 触发OTA升级故障
  388. if(BIT_CHECK(p_comData->Malfunction, OTA_fault)){
  389. p_ec800Date->hardwareUpdate = 0; // 取消升级,正常运行,本次上电不更新
  390. }
  391. }