EC800_FTP_OTA(2268).c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. // AT指令响应超时时间定义
  49. #define REC_TIMEOUT (10000) // 1ms
  50. /* macro -------------------------------------------------------------*/
  51. /* variables ---------------------------------------------------------*/
  52. /* function prototypes -----------------------------------------------*/
  53. int findSubstring(const char* haystack, const char* needle) {
  54. // 如果needle是空字符串,我们定义它总是在haystack中找到,位置为0
  55. if (!needle[0]) {
  56. return 0;
  57. }
  58. for (int i = 0; haystack[i] != '\0'; ++i) {
  59. // 当发现第一个字符匹配时,开始查找整个needle
  60. if (haystack[i] == needle[0]) {
  61. int j = 0;
  62. // 检查整个needle是否匹配
  63. while (needle[j] != '\0' && haystack[i + j] == needle[j]) {
  64. j++;
  65. }
  66. // 如果needle到达了字符串的末尾,我们找到了完整的匹配
  67. if (needle[j] == '\0') {
  68. return i; // 返回needle在haystack中开始的位置
  69. }
  70. // 否则,继续从haystack的下一个字符开始查找
  71. }
  72. }
  73. // 如果未找到匹配,返回-1
  74. return -1;
  75. }
  76. uint8_t Accept_and_Compare_Str(const char* needle, uint8_t recNum){
  77. uint8_t temp = 0;
  78. uint8_t cnt = 0;
  79. static uint16_t timeOutCnt = 0; // 超时计数
  80. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN); //设置接收缓冲区
  81. __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
  82. while(!temp) {
  83. if((cnt < recNum) && (cnt == g_usart3_rx_sta)){
  84. cnt++;
  85. }
  86. osDelay(1);
  87. if(strstr(g_usart3_rx_buf, needle)){
  88. temp = 1;
  89. }
  90. if(strstr(g_usart3_rx_buf, "ERROR")){
  91. temp = 2;
  92. }
  93. if(timeOutCnt < REC_TIMEOUT){
  94. timeOutCnt++;
  95. }else{
  96. timeOutCnt = 0;
  97. // temp = 3;
  98. }
  99. }
  100. g_usart3_rx_sta = 0;
  101. return temp;
  102. }
  103. /* 1.登陆到FTP服务器--------------------------------------------------------*/
  104. static uint8_t logIn_step = 0;
  105. /**
  106. * @brief 通过EC800登录FTP
  107. * @param NONE
  108. * @note NONE
  109. * @retval 无
  110. */
  111. void EC800_LogIn_FTP(void){
  112. char* found = NULL;
  113. uint8_t errorCnt = 0; // 错误计数
  114. ftpInfo *p_ftpInfo = &s_ftpInfo;
  115. char atCmd[100] = {0}; // 静态缓冲区用于存放AT命令
  116. p_ftpInfo->fileType = p_ftpInfo->fileType; // 避免报警告
  117. switch(logIn_step){
  118. case 0: // 配置和激活 PDP 上下文
  119. // EC800M_SendCommand(AT_QIACT_1);
  120. //
  121. // found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  122. //
  123. // if (found != NULL) {
  124. // printf("Activation successful\r\n");
  125. // }else{
  126. // errorCnt++;
  127. // printf("Activation unsuccessful\r\n");
  128. // }
  129. EC800M_SendCommand(AT_QIACT_query);
  130. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  131. if (found != NULL) {
  132. printf("PDP Context Status OK\r\n");
  133. }else{
  134. errorCnt++;
  135. printf("PDP Context Status error\r\n");
  136. }
  137. EC800M_SendCommand(AT_QFTPCFG_ID_1);
  138. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  139. if (found != NULL) {
  140. printf("Set PDP context to 1.\r\n");
  141. }else{
  142. errorCnt++;
  143. printf("ERROR! Set PDP context to 1.\r\n");
  144. }
  145. if(errorCnt == 0){
  146. logIn_step = 1;
  147. }
  148. break;
  149. case 1: // 配置用户账号和传输设置
  150. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"account\",\"%s\",\"%s\"\r\n", p_ftpInfo->account, p_ftpInfo->passWord);
  151. EC800M_SendCommand(atCmd);
  152. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  153. if (found != NULL) {
  154. printf("FINISH! Set username and password.\r\n");
  155. }else{
  156. errorCnt++;
  157. printf("ERROR! Set username and password.\r\n");
  158. }
  159. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"filetype\",%d\r\n", p_ftpInfo->fileType);
  160. EC800M_SendCommand(atCmd);
  161. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  162. if (found != NULL) {
  163. printf("FINISH! Set username and password.\r\n");
  164. }else{
  165. errorCnt++;
  166. printf("ERROR! Set username and password.\r\n");
  167. }
  168. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"transmode\",%d\r\n", p_ftpInfo->transmode);
  169. EC800M_SendCommand(atCmd);
  170. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  171. if (found != NULL) {
  172. printf("FINISH! Set to passive transfer mode.\r\n");
  173. }else{
  174. errorCnt++;
  175. printf("ERROR! Set to passive transfer mode.\r\n");
  176. }
  177. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCFG=\"rsptimeout\",%d\r\n", p_ftpInfo->rsptimeout);
  178. EC800M_SendCommand(atCmd);
  179. found = EC800M_RecRespond(g_usart3_rx_buf, AT_RESP_OK);
  180. if (found != NULL) {
  181. printf("FINISH! Set username and password.\r\n");
  182. }else{
  183. errorCnt++;
  184. printf("ERROR! Set username and password.\r\n");
  185. }
  186. if(errorCnt == 0){
  187. logIn_step = 2;
  188. }
  189. break;
  190. case 2: // 登录FTP服务器。
  191. snprintf(atCmd, sizeof(atCmd), "AT+QFTPOPEN=\"%s\",%d\r\n", p_ftpInfo->ftpAddr, p_ftpInfo->ftpPort);
  192. EC800M_SendCommand(atCmd);
  193. errorCnt = Accept_and_Compare_Str("+QFTPOPEN: 0,0", 2);
  194. if(errorCnt == 1){
  195. errorCnt = 0;
  196. printf("FINISH! Log in to the FTP server.\r\n");
  197. }else{
  198. printf("ERROR! Log in to the FTP server.\r\n");
  199. }
  200. if(errorCnt == 0){
  201. logIn_step = 3;
  202. }
  203. break;
  204. default :
  205. break;
  206. }
  207. }
  208. /* 2.从FTP服务器上下载文件--------------------------------------------------------*/
  209. //int extract_data(const char* input, char* output, size_t output_size) {
  210. // const char* start_marker = "CONNECT\r\n";
  211. // const char* end_marker = "\r\nOK";
  212. // const char* start;
  213. // const char* end;
  214. //
  215. // // 寻找开始标记
  216. // start = strstr(input, start_marker);
  217. // if (start == NULL) {
  218. // return HAL_ERROR;
  219. // }
  220. //
  221. // // 移动指针越过开始标记
  222. // start += strlen(start_marker);
  223. //
  224. // // 寻找结束标记
  225. // end = strstr(start, end_marker);
  226. // if (end == NULL) {
  227. // return HAL_ERROR;
  228. // }
  229. //
  230. // // 计算要提取数据的长度
  231. // size_t data_length = end - start;
  232. // if (data_length >= output_size) { // 确保输出缓冲区能够存放提取的数据
  233. // return HAL_ERROR;
  234. // }
  235. //
  236. // // 提取数据
  237. // memcpy(output, start, data_length);
  238. //
  239. // // 在输出字符串最后添加一个null终止符
  240. // output[data_length] = '\0';
  241. //
  242. // return HAL_OK;
  243. //}
  244. /**
  245. * @brief 剔除除去固件的其他的数据
  246. * @param NONE
  247. * @note NONE
  248. * @retval 无
  249. */
  250. int extract_data_as_uint32(const char* input, uint32_t* output, size_t output_size) {
  251. const char* start_marker = "CONNECT\r\n";
  252. const char* end_marker = "\r\nOK";
  253. const char* start;
  254. const char* end;
  255. // 寻找开始标记
  256. // start = strstr(input, start_marker);
  257. start = input;
  258. if (start == NULL) {
  259. return HAL_ERROR;
  260. }
  261. // 移动指针越过开始标记
  262. // start += strlen(start_marker);
  263. // 寻找结束标记
  264. end = strstr(start, end_marker);
  265. if (end == NULL) {
  266. return HAL_ERROR;
  267. }
  268. // 计算要提取数据的长度
  269. size_t data_length = end - start;
  270. // 确保提取的数据长度为4的倍数,适用于 uint32_t
  271. if (data_length % sizeof(uint32_t) != 0) {
  272. return HAL_ERROR;
  273. }
  274. // 计算 uint32_t 的数量
  275. size_t data_length_uint32 = data_length / sizeof(uint32_t);
  276. if (data_length_uint32 > output_size) { // 确保输出缓冲区能够存放提取的数据
  277. return HAL_ERROR;
  278. }
  279. // 提取数据并转换为 uint32_t 数组
  280. memcpy(output, start, data_length_uint32 * sizeof(uint32_t));
  281. return HAL_OK;
  282. }
  283. uint32_t totalBytesReceived = 0;
  284. uint32_t currentFlashAddress = APP1_ADDRESS;
  285. /**
  286. * @brief 更新固件
  287. * @param NONE
  288. * @note NONE
  289. * @retval 无
  290. */
  291. void UpdateFirmware(void) {
  292. ftpInfo *p_ftpInfo = &s_ftpInfo;
  293. uint32_t buffer[FLASH_PAGE_SIZE / sizeof(uint32_t)]; // 2KB缓冲区
  294. uint8_t returnTemp = 0; // 用于接收返回值的临时变量
  295. char atCmd[100] = {0}; // 静态缓冲区用于存放AT命令
  296. if (totalBytesReceived < (p_ftpInfo->filesize)) {
  297. // 计算剩余要接收的字节数
  298. uint32_t remaining = (p_ftpInfo->filesize) - totalBytesReceived;
  299. uint32_t bytesToRead = remaining < 100 ? remaining : 100;
  300. p_ftpInfo->startAddr = totalBytesReceived;
  301. p_ftpInfo->byteNum = bytesToRead; //
  302. // 发送AT指令以开始接收
  303. snprintf(atCmd, sizeof(atCmd), "AT+QFTPGET=\"%s\",\"COM:\",%u,%u\r\n", p_ftpInfo->textName, p_ftpInfo->startAddr, p_ftpInfo->byteNum);
  304. EC800M_SendCommand(atCmd);
  305. returnTemp = Accept_and_Compare_Str("+QFTPGET: 0,", 2); // 开始接收数据
  306. if((returnTemp == 1) || (returnTemp == 3)){ // 接收成功
  307. // 接收数据处理到buffer 剔除除去固件的其他的数据
  308. returnTemp = extract_data_as_uint32(g_usart3_rx_buf, buffer, FLASH_PAGE_SIZE / sizeof(uint32_t));
  309. if(returnTemp != HAL_OK){
  310. printf("ERROR! UpdateFirmware_extract_data\n");
  311. return;
  312. }
  313. }else{
  314. return;
  315. }
  316. // 将接收到的数据写入Flash
  317. // if (FLASH_Write(currentFlashAddress, buffer, FLASH_PAGE_SIZE / sizeof(uint32_t)) != HAL_OK) {
  318. // // 错误处理
  319. // printf("ERROR! UpdateFirmware_FLASH_Write\n");
  320. // return;
  321. // }
  322. // 更新地址和接收统计
  323. currentFlashAddress += FLASH_PAGE_SIZE;
  324. totalBytesReceived += bytesToRead;
  325. }
  326. // 根据实际情况实现接收完成后的操作
  327. }
  328. uint8_t downloadStep = 0;
  329. /**
  330. * @brief FTP服务器上下载文件
  331. * @param NONE
  332. * @note 每一个任务周期过来下载2k的数据,以保证其他任务的正常运行
  333. * @retval 无
  334. */
  335. void EC800_FTP_DownloadText(void){
  336. uint8_t errorCnt = 0; // 错误计数
  337. ftpInfo *p_ftpInfo = &s_ftpInfo;
  338. char atCmd[100] = {0}; // 静态缓冲区用于存放AT命令
  339. switch(downloadStep){
  340. case 0: // 设置文件目录
  341. snprintf(atCmd, sizeof(atCmd), "AT+QFTPCWD=\"%s\"\r\n", p_ftpInfo->textDirectory);
  342. EC800M_SendCommand(atCmd);
  343. errorCnt = Accept_and_Compare_Str("+QFTPCWD: 0,0", 2);
  344. if (errorCnt == 1) {
  345. errorCnt = 0;
  346. printf("FINISH! Set the file directory.\r\n");
  347. }else{
  348. printf("ERROR! Set the file directory.\r\n");
  349. }
  350. if(errorCnt == 0){
  351. downloadStep = 1;
  352. }
  353. break;
  354. case 1: // 获取文件长度
  355. snprintf(atCmd, sizeof(atCmd), "AT+QFTPSIZE=\"%s\"\r\n", p_ftpInfo->textName);
  356. EC800M_SendCommand(atCmd);
  357. errorCnt = Accept_and_Compare_Str("+QFTPSIZE: 0,", 1);
  358. if (errorCnt == 1) {
  359. errorCnt = 0;
  360. printf("FINISH! Get the length of the file.\r\n");
  361. // 使用sscanf从字符串中解析整数
  362. if(sscanf(g_usart3_rx_buf, "\r\n+QFTPSIZE: %*d,%u", &(p_ftpInfo->filesize)) == 1) {
  363. // 解析成功,filesize变量现在包含值1000
  364. printf("The filesize is: %u\n", p_ftpInfo->filesize);
  365. } else {
  366. // 解析失败
  367. errorCnt++;
  368. printf("Failed to parse the filesize.\n");
  369. }
  370. }else{
  371. printf("ERROR! Get the length of the file.\r\n");
  372. }
  373. if(errorCnt == 0){
  374. downloadStep = 2;
  375. }
  376. break;
  377. case 2:
  378. // UpdateFirmware();
  379. // 发送AT指令以开始接收
  380. snprintf(atCmd, sizeof(atCmd), "AT+QFTPGET=\"%s\",\"COM:\",%u,%u\r\n", p_ftpInfo->textName, 0, 100);
  381. EC800M_SendCommand(atCmd);
  382. errorCnt = Accept_and_Compare_Str("+QFTPGET: 0,", 2); // 开始接收数据
  383. if (errorCnt == 1) {
  384. printf("finsh,receive");
  385. }
  386. break;
  387. default :
  388. break;
  389. }
  390. }
  391. /* 3. OTA升级从FTP流程--------------------------------------------------------*/
  392. uint8_t upgradeStep = 0;
  393. /**
  394. * @brief OTA升级从FTP
  395. * @param NONE
  396. * @note NONE
  397. * @retval 无
  398. */
  399. void EC800_FTP_OTA_Upgrade(void){
  400. switch (upgradeStep){
  401. case 0: // 登陆到FTP服务器
  402. EC800_LogIn_FTP();
  403. if(logIn_step == 3){
  404. upgradeStep = 1;
  405. }
  406. break;
  407. case 1: // 从FTP服务器上下载文件
  408. EC800_FTP_DownloadText();
  409. break;
  410. case 2: // 复位升级
  411. HAL_NVIC_SystemReset();
  412. break;
  413. default:
  414. break;
  415. }
  416. }