fota_ftp_demo.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*================================================================
  2. Copyright (c) 2021, Quectel Wireless Solutions Co., Ltd. All rights reserved.
  3. Quectel Wireless Solutions Proprietary and Confidential.
  4. =================================================================*/
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include "ql_type.h"
  9. #include "ql_api_osi.h"
  10. #include "ql_log.h"
  11. #include "ql_api_datacall.h"
  12. #include "ql_fs.h"
  13. #include "ql_power.h"
  14. #include "ql_api_dev.h"
  15. #include "ql_api_fota.h"
  16. #include "ql_ftp_client.h"
  17. #include "ql_app_feature_config.h"
  18. #define QL_FOTA_FTP_LOG_LEVEL QL_LOG_LEVEL_INFO
  19. #define QL_FOTA_FTP_LOG(msg, ...) QL_LOG(QL_FOTA_FTP_LOG_LEVEL, "ql_FOTA_ftp", msg, ##__VA_ARGS__)
  20. #define QL_FOTA_FTP_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_FOTA_ftp", msg, ##__VA_ARGS__)
  21. #ifdef QL_APP_FEATURE_FTP_FOTA
  22. #define QL_VERSION_MAX 256
  23. float approximate_zero = 1e-6; //近似0值,用于两个浮点型变量判相等
  24. /* 默认配置,用户可自行修改 */
  25. bool file_progress_show = true; //默认显示下载进度
  26. char local_filename[] = "UFS:fota.pac"; //本地默认升级包地址
  27. int profile_idx = 1; //cid
  28. uint8_t nSim = 0; //sid
  29. //FTP服务器相关信息
  30. char hostname[] = "220.180.239.212:8309"; //FTP服务器 IP:端口号
  31. char username[] = "test"; //FTP服务器用户名
  32. char password[] = "test"; //FTP服务器用户密码
  33. char file_full_path[] = "kruskal/output.pack"; //FTP服务器文件路径
  34. ql_task_t fota_ftp_task = NULL;
  35. typedef enum{
  36. QL_FTP_FOTA_SUCCESS = 0,
  37. QL_ERROR_INVALID_PARAM = -1,
  38. }ql_fota_ftp_error_code_e;
  39. typedef enum{
  40. DOWN_INIT, //初始阶段
  41. DOWN_DOWNING, //下载中
  42. DOWN_INTR, //下载被中断
  43. DOWN_DOWNED, //下载完成
  44. DOWN_NOSPACE, //没有空间
  45. DOWN_OTHER_ERROR, //执行下载时其他错误
  46. }fota_down_state;
  47. typedef struct{
  48. bool show; //是否显示文件下载进度
  49. unsigned long total_size; //从FTP服务器所要下载的文件总大小
  50. unsigned long downloaded_size; //已下载到本地的FOTA文件大小
  51. unsigned long last_downloaded_size; //上次下载完成后文件的大小
  52. }fota_file_progress;
  53. typedef struct{
  54. void *ftp_handle; //用于与FTP服务器进行文件交互的句柄
  55. bool poweroutage_resume; //0-不支持断点续传,1-支持断点续传
  56. int profile_idx; //cid
  57. uint8_t nSim; //sid
  58. fota_down_state down_state; //当前从FTP服务器下载文件的下载状态
  59. fota_file_progress file_progress; //当前从FTP服务器文件信息和已下载文件的具体进度信息
  60. char local_filename[QL_FOTA_PACK_NAME_MAX_LEN]; //下载到本地的升级文件的位置
  61. //FTP服务器相关
  62. char hostname[256]; //FTP服务器: IP地址+端口号 (IP地址:端口号)
  63. char username[256]; //FTP服务器用户名
  64. char password[256]; //FTP服务器用户密码
  65. char file_full_path[256]; //所要从FTP服务器上获取的文件的具体路径信息,包括文件名. (文件路径/文件名)
  66. }fota_ftp_client_t;
  67. fota_ftp_client_t fota_ftp_cli = {.down_state = DOWN_INIT};
  68. int fota_ftp_client_init(fota_ftp_client_t *fota_ftp_cli_p)
  69. {
  70. int ret = QL_FTP_FOTA_SUCCESS;
  71. QFILE local_file_fd = -1; //用来保存本地FTP FOTA文件的文件描述符
  72. fota_ftp_cli_p->poweroutage_resume = true;
  73. fota_ftp_cli_p->profile_idx = profile_idx;
  74. fota_ftp_cli_p->nSim = nSim;
  75. fota_ftp_cli_p->down_state = DOWN_INIT;
  76. fota_ftp_cli_p->file_progress.show = file_progress_show;
  77. fota_ftp_cli_p->file_progress.total_size = 0; //等连接到FTP服务器,获得服务器文件大小后才赋值
  78. fota_ftp_cli_p->file_progress.downloaded_size = 0;
  79. fota_ftp_cli_p->file_progress.last_downloaded_size = 0;
  80. memcpy(fota_ftp_cli_p->local_filename, local_filename, strlen(local_filename) ); //默认升级包地址
  81. //FTP服务器相关信息
  82. memcpy(fota_ftp_cli_p->hostname, hostname, strlen(hostname) );
  83. memcpy(fota_ftp_cli_p->username, username, strlen(username) );
  84. memcpy(fota_ftp_cli_p->password, password, strlen(password) );
  85. memcpy(fota_ftp_cli_p->file_full_path, file_full_path, strlen(file_full_path) );
  86. //本次对FTP服务器的文件进行操作时,文件句柄都需要重新创建
  87. fota_ftp_cli_p->ftp_handle = ql_ftp_client_new();
  88. if (NULL == fota_ftp_cli_p->ftp_handle)
  89. {
  90. //未成功创建句柄时,返回错误,并且退出线程
  91. ret = QL_ERROR_INVALID_PARAM;
  92. }
  93. local_file_fd = ql_fopen(fota_ftp_cli_p->local_filename,"wb+");
  94. if(local_file_fd <= 0)
  95. {
  96. QL_FOTA_FTP_LOG("ftp fota loc file open failed ret : %d!",local_file_fd);
  97. ret = QL_ERROR_INVALID_PARAM;
  98. }
  99. ql_fclose(local_file_fd);
  100. return ret;
  101. }
  102. int fota_ftp_net_register(uint8_t nSim,int profile_idx)
  103. {
  104. int ret = QL_FTP_FOTA_SUCCESS;
  105. ql_data_call_info_s info;
  106. memset(&info, 0x00, sizeof(ql_data_call_info_s));
  107. for (int i = 0; i < 10; i++)
  108. {
  109. ret = ql_network_register_wait(nSim, 120);
  110. if (QL_FTP_FOTA_SUCCESS == ret)
  111. {
  112. break;
  113. }
  114. ql_rtos_task_sleep_s(1);
  115. }
  116. if (QL_FTP_FOTA_SUCCESS != ret)
  117. {
  118. QL_FOTA_FTP_LOG("network register failure!");
  119. return QL_ERROR_INVALID_PARAM;
  120. }
  121. ret = ql_set_data_call_asyn_mode(nSim, profile_idx, 0);
  122. QL_FOTA_FTP_LOG("network datacall asyn mode ret = %d",ret);
  123. ql_start_data_call(nSim, profile_idx, QL_PDP_TYPE_IP, "uninet", NULL, NULL, 0);
  124. QL_FOTA_FTP_LOG("network datacall result ret = %d",ret);
  125. if (0 != ret)
  126. {
  127. QL_FOTA_FTP_LOG("data call failure!");
  128. }
  129. ret = ql_get_data_call_info(nSim, profile_idx, &info);
  130. if (0 != ret)
  131. {
  132. ql_stop_data_call(nSim, profile_idx);
  133. return QL_ERROR_INVALID_PARAM;
  134. }
  135. return ret;
  136. }
  137. int fota_ftp_server_connect(fota_ftp_client_t *fota_ftp_cli_p)
  138. {
  139. int ret = QL_FTP_FOTA_SUCCESS;
  140. uint16_t sim_cid;
  141. ret = ql_bind_sim_and_profile(fota_ftp_cli_p->nSim, fota_ftp_cli_p->profile_idx, &sim_cid);
  142. if (ret != QL_DATACALL_SUCCESS)
  143. {
  144. return QL_ERROR_INVALID_PARAM;
  145. }
  146. ql_ftp_client_setopt(fota_ftp_cli_p->ftp_handle, QL_FTP_CLIENT_SIM_CID, sim_cid);
  147. ql_ftp_client_setopt(fota_ftp_cli_p->ftp_handle, QL_FTP_CLIENT_OPT_PDP_CID, fota_ftp_cli_p->profile_idx);
  148. ret = ql_ftp_client_open(fota_ftp_cli_p->ftp_handle, fota_ftp_cli_p->hostname, fota_ftp_cli_p->username, fota_ftp_cli_p->password);
  149. QL_FOTA_FTP_LOG("********** ql_ftp_client_open ret:%d **********",ret);
  150. return ret;
  151. }
  152. int fota_ftp_get_server_file_size(fota_ftp_client_t *fota_ftp_cli_p)
  153. {
  154. QL_FOTA_FTP_LOG("********** fota_ftp_get_server_file_size ***********");
  155. int ret = QL_FTP_FOTA_SUCCESS;
  156. double file_size = 0;
  157. ret = ql_ftp_client_size(fota_ftp_cli_p->ftp_handle, fota_ftp_cli_p->file_full_path, &file_size);
  158. fota_ftp_cli_p->file_progress.total_size = file_size;
  159. QL_FOTA_FTP_LOG("********** fota_ftp_cli_p->file_progress.total_size : %d **********", fota_ftp_cli_p->file_progress.total_size);
  160. return ret;
  161. }
  162. int fota_ftp_net_connect_ready(fota_ftp_client_t *fota_ftp_cli_p)
  163. {
  164. int ret = QL_FTP_FOTA_SUCCESS;
  165. int residual_space_size = 0;
  166. //网络注册,连接
  167. ret = fota_ftp_net_register(fota_ftp_cli_p->nSim, fota_ftp_cli_p->profile_idx);
  168. if (QL_FTP_FOTA_SUCCESS != ret)
  169. {
  170. QL_FOTA_FTP_LOG("********** Network registration failed **********");
  171. ret = QL_ERROR_INVALID_PARAM;
  172. goto exit;
  173. }
  174. //向FTP服务器发起连接请求
  175. ret = fota_ftp_server_connect(fota_ftp_cli_p);
  176. if (QL_FTP_FOTA_SUCCESS != ret)
  177. {
  178. //未成功与FTP服务器建立连接
  179. QL_FOTA_FTP_LOG("********** The connection to the FTP server was unsuccessful **********");
  180. ret = QL_ERROR_INVALID_PARAM;
  181. goto exit;
  182. }
  183. //获取FTP服务器所要下载的文件大小
  184. if (0 != fota_ftp_get_server_file_size(fota_ftp_cli_p))
  185. {
  186. //文件不存在于FTP服务器上
  187. QL_FOTA_FTP_LOG("********** The file to be downloaded does not exist or failed on the FTP server **********");
  188. //断开与FTP服务器的连接
  189. ql_ftp_client_close(fota_ftp_cli_p->ftp_handle);
  190. ret = QL_ERROR_INVALID_PARAM;
  191. goto exit;
  192. }
  193. //获取模块剩余空间大小
  194. residual_space_size = ql_fs_free_size(fota_ftp_cli_p->local_filename);
  195. QL_FOTA_FTP_LOG("********** residual_space_size:%d ftp file total_size:%d**********", residual_space_size,fota_ftp_cli_p->file_progress.total_size);
  196. //判断文件空间是否足够
  197. //当 total_size - downloaded_size > residual_space_size 时 空间不足
  198. if (fota_ftp_cli_p->file_progress.total_size > residual_space_size)
  199. {
  200. //剩余空间不足,无法下载FOTA文件
  201. QL_FOTA_FTP_LOG("********** There is not enough free space to download FOTA files **********");
  202. fota_ftp_cli_p->down_state = DOWN_NOSPACE;
  203. ql_remove(fota_ftp_cli_p->local_filename);
  204. //断开与FTP服务器的连接
  205. ql_ftp_client_close(fota_ftp_cli_p->ftp_handle);
  206. ret = QL_ERROR_INVALID_PARAM;
  207. goto exit;
  208. }
  209. exit:
  210. return ret;
  211. }
  212. void fota_ftp_show_file_progress(fota_ftp_client_t *fota_ftp_cli_p)
  213. {
  214. float last_download_progress = 0; //上次下载完成后的进度信息
  215. float download_progress = 0; //本次下载完成后的进度信息
  216. //避免除0中断
  217. if (0 == fota_ftp_cli_p->file_progress.total_size)
  218. {
  219. return;
  220. }
  221. last_download_progress = (float)(fota_ftp_cli_p->file_progress.last_downloaded_size)/(fota_ftp_cli_p->file_progress.total_size);
  222. download_progress = (float)(fota_ftp_cli_p->file_progress.downloaded_size)/(fota_ftp_cli_p->file_progress.total_size);
  223. //相同的进度不予显示
  224. if ( (download_progress - last_download_progress) < approximate_zero )
  225. {
  226. return;
  227. }
  228. download_progress = download_progress * 100;
  229. QL_FOTA_FTP_LOG("*************************************************************");
  230. QL_FOTA_FTP_LOG("********** Current file download progress : %.1f%% **********", download_progress);
  231. QL_FOTA_FTP_LOG("*************************************************************");
  232. }
  233. size_t fota_ftp_write_cb(void *ptr, size_t size, size_t nmemb, void *stream)
  234. {
  235. QL_FOTA_FTP_LOG("********** fota_ftp_write_cb **********");
  236. int ret = 0;
  237. QFILE fd = -1;
  238. fota_ftp_client_t *user_data;
  239. user_data = (fota_ftp_client_t *)stream;
  240. //"ab" 打开或新建一个二进制文件,只允许在文件末尾追写
  241. fd = ql_fopen(user_data->local_filename, "ab");
  242. //回写数据前将状态置为中断,ql_fwrite出现异常无法继续执行下去时,将会一直保持此状态,再次调用FTP FOTA线程时,将使用本次的断点信息继续下载
  243. user_data->down_state = DOWN_INTR;
  244. ret = ql_fwrite(ptr, size, nmemb, fd);
  245. QL_FOTA_FTP_LOG("ql_fwrite ret = %d",ret);
  246. ql_fclose(fd);
  247. if (size*nmemb == ret)
  248. {
  249. user_data->file_progress.downloaded_size += size*nmemb;
  250. if (true == user_data->file_progress.show)
  251. {
  252. //显示本次调用回调fota_ftp_write_cb,回写文件当前下载进度
  253. fota_ftp_show_file_progress(user_data);
  254. }
  255. user_data->file_progress.last_downloaded_size = user_data->file_progress.downloaded_size;
  256. //当前状态还视为下载中,是否下载完成在 fota_ftp_get_file 通过本地文件与服务器文件大小进行判断
  257. user_data->down_state = DOWN_DOWNING;
  258. }
  259. else
  260. {
  261. if (QL_FILE_NO_SPACE == ret)
  262. {
  263. //空间不足,无法写入数据
  264. user_data->down_state = DOWN_NOSPACE;
  265. }
  266. else
  267. {
  268. user_data->down_state = DOWN_OTHER_ERROR;
  269. }
  270. return 0;
  271. }
  272. //ret的其余错误码均视为回写过程中的异常中断
  273. QL_FOTA_FTP_LOG();
  274. return size*nmemb;
  275. }
  276. void fota_ftp_get_file(fota_ftp_client_t *fota_ftp_cli_p)
  277. {
  278. QL_FOTA_FTP_LOG("********** fota_ftp_get_file **********");
  279. int ret;
  280. //设置文件下载起始偏移为上次文件下载之后的位置,初始偏移量为0
  281. QL_FOTA_FTP_LOG("********** downloaded_size : %ld**********", fota_ftp_cli_p->file_progress.downloaded_size);
  282. ql_ftp_client_setopt(fota_ftp_cli_p->ftp_handle, QL_FTP_CLIENT_OPT_START_POS, fota_ftp_cli_p->file_progress.downloaded_size);
  283. ret = ql_ftp_client_get_ex(fota_ftp_cli_p->ftp_handle, fota_ftp_cli_p->file_full_path, NULL, fota_ftp_write_cb, (void *)fota_ftp_cli_p);
  284. QL_FOTA_FTP_LOG("********** ql_ftp_client_get_ex : %d **********", ret);
  285. if ( (QL_FTP_FOTA_SUCCESS != ret) && (DOWN_DOWNING == fota_ftp_cli_p->down_state) )
  286. {
  287. //fota_ftp_cli_p->down_state 被置为 DOWN_DOWNING,说明已经执行了回调函数fota_ftp_write_cb,且最后一次执行回调时还在下载中
  288. //由于网络异常,导致回写无法继续进行,ql_ftp_client_get_ex返回错误,fota_ftp_cli_p->down_state为上次正常执行时的状态
  289. //下载状态设置为阻塞状态,并尝试下一次FTP文件获取请求
  290. //下载过程中网络异常
  291. fota_ftp_cli_p->down_state = DOWN_INTR;
  292. return;
  293. }
  294. if (DOWN_INTR == fota_ftp_cli_p->down_state)
  295. {
  296. //下载中断的状态,还未正确执行fota_ftp_write_cb而修改
  297. return;
  298. }
  299. if (DOWN_NOSPACE == fota_ftp_cli_p->down_state)
  300. {
  301. //由于空间不足导致ql_ftp_client_get_ex返回值异常,直接退出
  302. return;
  303. }
  304. if (QL_FTP_FOTA_SUCCESS != ret)
  305. {
  306. //ql_ftp_client_get_ex 配置参数错误,导致无法正确调用写回调函数
  307. fota_ftp_cli_p->down_state = DOWN_OTHER_ERROR;
  308. return;
  309. }
  310. if (fota_ftp_cli_p->file_progress.downloaded_size >= fota_ftp_cli_p->file_progress.total_size)
  311. {
  312. //文件已下载大小和FTP 服务器文件大小一致,完成文件下载
  313. fota_ftp_cli_p->down_state = DOWN_DOWNED;
  314. }
  315. }
  316. void fota_ftp_file_download(fota_ftp_client_t *fota_ftp_cli_p)
  317. {
  318. //向FTP服务器请求最多10次FTP FOTA文件下载
  319. for (int i = 0; i < 10; i++)
  320. {
  321. QL_FOTA_FTP_LOG("*************** down_state : %d ******************", fota_ftp_cli_p->down_state);
  322. if (DOWN_DOWNED == fota_ftp_cli_p->down_state)
  323. {
  324. //下载完成
  325. QL_FOTA_FTP_LOG("FTP FOTA file download complete");
  326. break;
  327. }
  328. else if (DOWN_NOSPACE == fota_ftp_cli_p->down_state)
  329. {
  330. //在当前file_download阶段发生了空间不足的情况,表明net_connect_ready阶段时候获取的目标文件大小 和 当前file_download阶段下载的文件不是同一内容的文件,仅仅同名
  331. //在net_connect_ready阶段获取了文件大小后 到 file_download阶段 开始真正文件下载前,这一时间间隔当中服务器的文件可能被同名替换,或者在下载文件过程中,文件被同名替换
  332. //这样的文件和初始目标文件不同,应该被删除
  333. //目标文件被替换,导致下载FOTA包的空间不足
  334. QL_FOTA_FTP_LOG("********** The object file on the server has been replaced and there is not enough space left in the FOTA package after downloading the replacement **********");
  335. //删除文件
  336. ql_remove(fota_ftp_cli_p->local_filename);
  337. //下载状态置为初始状态,等待下一次的FTP FOTA下载
  338. fota_ftp_cli_p->down_state = DOWN_INIT;
  339. break;
  340. }
  341. else if (DOWN_OTHER_ERROR == fota_ftp_cli_p->down_state)
  342. {
  343. //ql_ftp_client_get_ex 函数参数配置错误,导致下载FTP文件时发生错误
  344. QL_FOTA_FTP_LOG("********** The ql_ftp_client_get_ex function parameter configuration error, resulting in an error when downloading FTP files **********");
  345. break;
  346. }
  347. //FTP FOTA文件下载
  348. fota_ftp_get_file(fota_ftp_cli_p);
  349. //等待10s继续下载
  350. ql_rtos_task_sleep_s(10);
  351. }
  352. //断开与FTP服务器的连接
  353. ql_ftp_client_close(fota_ftp_cli_p->ftp_handle);
  354. }
  355. void fota_ftp_file_check(fota_ftp_client_t *fota_ftp_cli_p)
  356. {
  357. int ret = QL_FTP_FOTA_SUCCESS;
  358. //文件下载完成,FOTA文件本地校验
  359. if (DOWN_DOWNED == fota_ftp_cli_p->down_state)
  360. {
  361. ret = ql_fota_image_verify(fota_ftp_cli_p->local_filename);
  362. if (QL_FTP_FOTA_SUCCESS != ret)
  363. {
  364. QL_FOTA_FTP_LOG("********** [%s]package is invalid FOTA **********", fota_ftp_cli_p->local_filename);
  365. //删除无效的FOTA文件
  366. ret = ql_remove(fota_ftp_cli_p->local_filename);
  367. fota_ftp_cli_p->down_state = DOWN_INIT;
  368. if (QL_FTP_FOTA_SUCCESS == ret)
  369. {
  370. QL_FOTA_FTP_LOG("********** delete file success **********");
  371. }
  372. }
  373. else
  374. {
  375. //校验成功
  376. QL_FOTA_FTP_LOG("********** download is sucess ,system will reset power! **********");
  377. //设置升级
  378. ql_rtos_task_sleep_s(5);
  379. ql_power_reset(RESET_NORMAL);
  380. }
  381. }
  382. else
  383. {
  384. QL_FOTA_FTP_LOG("********** download is failed ,remove file! **********");
  385. ret = ql_remove(fota_ftp_cli_p->local_filename);
  386. fota_ftp_cli_p->down_state = DOWN_INIT;
  387. if (QL_FTP_FOTA_SUCCESS == ret)
  388. {
  389. QL_FOTA_FTP_LOG("********** delete file success **********");
  390. }
  391. }
  392. return;
  393. }
  394. ql_fota_result_e fota_ftp_result_process(void)
  395. {
  396. ql_fota_result_e p_fota_result = 0;
  397. //获取升级结果
  398. if ( ql_fota_get_result(&p_fota_result) != QL_FOTA_SUCCESS )
  399. {
  400. QL_FOTA_FTP_LOG("ql_fota_get_result failed ");
  401. return QL_FOTA_STATUS_INVALID;
  402. }
  403. if ( p_fota_result == QL_FOTA_FINISHED )
  404. {
  405. QL_FOTA_FTP_LOG("update finished");
  406. ql_fota_file_reset(TRUE);
  407. return QL_FOTA_FINISHED;
  408. }
  409. else if(p_fota_result == QL_FOTA_READY)
  410. {
  411. QL_FOTA_FTP_LOG("fota ready bigen power reset ");
  412. ql_rtos_task_sleep_s(5);
  413. ql_power_reset(RESET_NORMAL);
  414. }
  415. else if(p_fota_result == QL_FOTA_NOT_EXIST)
  416. {
  417. QL_FOTA_FTP_LOG("fota file not exist");
  418. ql_fota_file_reset(TRUE);
  419. return QL_FOTA_NOT_EXIST;
  420. }
  421. QL_FOTA_FTP_LOG("fota result status invalid");
  422. return QL_FOTA_STATUS_INVALID;
  423. }
  424. void fota_ftp_app_thread()
  425. {
  426. //延迟启动30S
  427. ql_rtos_task_sleep_s(30);
  428. int ret = QL_FTP_FOTA_SUCCESS;
  429. char version_buf[QL_VERSION_MAX] = {0};
  430. ql_dev_get_firmware_version(version_buf, sizeof(version_buf));
  431. QL_FOTA_FTP_LOG("current version: %s", version_buf);
  432. fota_ftp_client_t *fota_ftp_cli_p = &fota_ftp_cli;
  433. if(fota_ftp_result_process() == QL_FOTA_FINISHED)
  434. {
  435. goto init_error_exit;
  436. }
  437. ret = fota_ftp_client_init(fota_ftp_cli_p);
  438. if (QL_FTP_FOTA_SUCCESS != ret)
  439. {
  440. goto init_error_exit;
  441. }
  442. ret = fota_ftp_net_connect_ready(fota_ftp_cli_p);
  443. if (QL_FTP_FOTA_SUCCESS != ret)
  444. {
  445. goto exit;
  446. }
  447. //向FTP服务器进行FTP FOTA文件下载请求
  448. fota_ftp_file_download(fota_ftp_cli_p);
  449. //文件下载完成,FOTA文件本地校验
  450. fota_ftp_file_check(fota_ftp_cli_p);
  451. exit:
  452. ql_ftp_client_release(fota_ftp_cli_p->ftp_handle);
  453. init_error_exit:
  454. QL_FOTA_FTP_LOG("********** exit ql_ftp_fota_demo **********");
  455. ql_rtos_task_delete(fota_ftp_task);
  456. }
  457. #endif
  458. void ql_fota_ftp_app_init()
  459. {
  460. #ifdef QL_APP_FEATURE_FTP_FOTA
  461. QL_FOTA_FTP_LOG("ftp fota demo support!");
  462. QlOSStatus err = QL_OSI_SUCCESS;
  463. err = ql_rtos_task_create(&fota_ftp_task, 1024*16, APP_PRIORITY_HIGH, "QfotaFtp", fota_ftp_app_thread, NULL, 5);
  464. if (err != QL_OSI_SUCCESS)
  465. {
  466. QL_FOTA_FTP_LOG("created task failed");
  467. }
  468. #endif
  469. }