EC800_FTP_OTA(3890).c 14 KB

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