upgrade.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <pthread.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <sys/ipc.h>
  7. #include <sys/msg.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <unistd.h>
  11. #include <signal.h>
  12. #include <string.h>
  13. #include <stdarg.h>
  14. #include <dirent.h>
  15. #include "upgrade.h"
  16. #include "md5.h"
  17. #if 0
  18. #define LOGD log_print
  19. #define LOGE log_print
  20. #else
  21. #define LOGD printf
  22. #define LOGE printf
  23. #endif
  24. typedef struct {
  25. char *pbuf;
  26. int dlen;
  27. int blen;
  28. }node_t;
  29. typedef struct {
  30. uint32_t src; //
  31. uint8_t obj; //升级对象,解压后扫描文件,赋值到该变量
  32. uint8_t stat; //当前升级所处状态
  33. uint8_t step; //当前升级所处阶段
  34. pthread_t tid;
  35. FILE* log;
  36. }upg_handle_t;
  37. static upg_handle_t upgHandle;
  38. static int log_print(const char *fmt, ...);
  39. static int node_free(node_t *nd)
  40. {
  41. if(!nd) {
  42. return -1;
  43. }
  44. free(nd->pbuf);
  45. nd->pbuf = NULL;
  46. nd->dlen = 0;
  47. nd->blen = 0;
  48. return 0;
  49. }
  50. static int exe_cmd(char *cmd, node_t *nd)
  51. {
  52. int r=0;
  53. FILE *fp=NULL;
  54. char *pbuf=NULL;
  55. int onelen=1000;
  56. int dlen=0,blen=onelen;
  57. pbuf = malloc(onelen);
  58. if(!pbuf) {
  59. return -1;
  60. }
  61. fp = popen(cmd, "r");
  62. if (fp == NULL) {
  63. return -1;
  64. }
  65. while(1) {
  66. r = fread(pbuf+dlen, sizeof(char), onelen, fp);
  67. if(r<=0) {
  68. LOGE("upgrade relloac %d failed\n", dlen+onelen);
  69. break;
  70. }
  71. dlen += r;
  72. if(dlen+onelen>blen) {
  73. char* p = realloc(pbuf, dlen+onelen);
  74. if(!p) {
  75. LOGE("upgrade relloac %d failed\n", dlen+onelen);
  76. free(pbuf); pclose(fp);
  77. return -1;
  78. }
  79. pbuf = p;
  80. blen = dlen+onelen;
  81. }
  82. }
  83. pclose(fp);
  84. nd->pbuf = pbuf;
  85. nd->dlen = dlen;
  86. nd->blen = blen;
  87. return 0;
  88. }
  89. static int exe_cmd2(char *cmd)
  90. {
  91. return system(cmd);
  92. }
  93. //https://blog.csdn.net/qq_38650447/article/details/136284071
  94. static int log_init(void)
  95. {
  96. char *path=UPG_LOG_PATH;
  97. char tmp[200];
  98. if (upgHandle.log) {
  99. return -1;
  100. }
  101. snprintf(tmp, sizeof(tmp), "mkdir -p %s", path);
  102. system(tmp);
  103. upgHandle.log = fopen(path, "wt");
  104. if (!upgHandle.log) {
  105. LOGE("%s open failed\n", path);
  106. return -1;
  107. }
  108. return 0;
  109. }
  110. static int log_print(const char *fmt, ...)
  111. {
  112. int r;
  113. va_list va;
  114. char buf[1000];
  115. va_start(va, fmt);
  116. r = vsprintf(buf, fmt, va);
  117. va_end(va);
  118. if (upgHandle.log) {
  119. fprintf(upgHandle.log, "%s", buf);
  120. }
  121. else {
  122. printf("%s", buf);
  123. }
  124. return 0;
  125. }
  126. static int log_flush(void)
  127. {
  128. if (upgHandle.log) {
  129. return -1;
  130. }
  131. fflush(upgHandle.log);
  132. return 0;
  133. }
  134. static int file_exist(char* path)
  135. {
  136. int r;
  137. struct stat st;
  138. r = stat(path, &st);
  139. if (r == 0) {
  140. return 1;
  141. }
  142. else {
  143. return 0;
  144. }
  145. }
  146. static int file_length(char* path)
  147. {
  148. int r;
  149. struct stat st;
  150. r = stat(path, &st);
  151. if (r) {
  152. return -1;
  153. }
  154. return st.st_size;
  155. }
  156. static int file_touch(int id)
  157. {
  158. int r;
  159. char tmp[200];
  160. snprintf(tmp, sizeof(tmp), "touch %s/s%d", UPG_FOLDER, id);
  161. return system(tmp); // 使用写入模式创建一个新文件
  162. }
  163. static int file_untouch(int id)
  164. {
  165. int r;
  166. char tmp[200];
  167. snprintf(tmp, sizeof(tmp), "rm %s/s%d", UPG_FOLDER, id);
  168. return system(tmp); // 使用写入模式创建一个新文件
  169. }
  170. static int folder_init(void)
  171. {
  172. char tmp[200];
  173. snprintf(tmp, sizeof(tmp), "mkdir -p %s", RUN_FOLDER);
  174. system(tmp);
  175. snprintf(tmp, sizeof(tmp), "mkdir -p %s", BAK_FOLDER);
  176. system(tmp);
  177. snprintf(tmp, sizeof(tmp), "mkdir -p %s", UPG_FOLDER);
  178. system(tmp);
  179. snprintf(tmp, sizeof(tmp), "mkdir -p %s", DOWN_FOLDER);
  180. system(tmp);
  181. return 0;
  182. }
  183. static int file_save(char *path, void *buf, int len)
  184. {
  185. int r;
  186. FILE *fp;
  187. fp = fopen(path, "wb");
  188. if(!fp) {
  189. LOGE("file_save, fopen %s failed\n", path);
  190. return -1;
  191. }
  192. r = fwrite(buf, 1, len, fp);
  193. if(r!=len) {
  194. LOGE("file_save %s failed\n", path);
  195. r = -1;
  196. }
  197. else {
  198. r = 0;
  199. }
  200. fclose(fp);
  201. return r;
  202. }
  203. static int file_load(char* path, void* buf, int len)
  204. {
  205. int r;
  206. FILE* fp;
  207. fp = fopen(path, "rb");
  208. if (!fp) {
  209. LOGE("file_load, fopen %s failed\n", path);
  210. return -1;
  211. }
  212. r = fread(buf, 1, len, fp);
  213. if (r != len) {
  214. LOGE("file_load %s failed\n", path);
  215. r = -1;
  216. }
  217. else {
  218. r = 0;
  219. }
  220. fclose(fp);
  221. return r;
  222. }
  223. static int file_remove(char *path)
  224. {
  225. return remove(path);
  226. }
  227. /*
  228. tar pdutar to run/
  229. */
  230. static int file_cover(char *path)
  231. {
  232. int r;
  233. char cmd[200];
  234. snprintf(cmd, sizeof(cmd), "tar -xpf %s -C / > /dev/null 2>&1", path);
  235. r = system(cmd);
  236. if(r) {
  237. LOGE("%s failed\n", cmd);
  238. return -1;
  239. }
  240. return 0;
  241. }
  242. static int dir_is_empty(char *path)
  243. {
  244. struct dirent *entry;
  245. int is_empty = 1;
  246. DIR *dir = opendir(path);
  247. if (dir == NULL) {
  248. perror("opendir failed\n");
  249. return -1; // 错误处理
  250. }
  251. while ((entry = readdir(dir)) != NULL) {
  252. // 忽略'.'和'..'
  253. if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
  254. is_empty = 0;
  255. break;
  256. }
  257. }
  258. closedir(dir);
  259. return is_empty;
  260. }
  261. //when app run success, copy run/* to bak/
  262. static int file_backup(void)
  263. {
  264. int r;
  265. char tmp[100];
  266. char cmd[200];
  267. //dl目录有tar压缩包,则拷贝到bak目录
  268. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  269. if(file_exist(tmp)) {
  270. LOGD("%s exist\n", tmp);
  271. snprintf(cmd, sizeof(cmd), "cp -f %s %s/", tmp, BAK_FOLDER);
  272. r = system(cmd);
  273. if(r==0) {
  274. return 0;
  275. }
  276. LOGD("cp %s to %s failed\n", tmp, BAK_FOLDER);
  277. }
  278. else {
  279. LOGD("%s not exist\n", tmp);
  280. }
  281. //bak目录有tar压缩包,则退出
  282. snprintf(tmp, sizeof(tmp), "%s/%s", BAK_FOLDER, UPG_TAR_NAME);
  283. if(file_exist(tmp)) {
  284. LOGD("%s exist\n", tmp);
  285. return 0;
  286. }
  287. else {
  288. LOGD("%s not exist\n", tmp);
  289. }
  290. //bak目录没有tar压缩包,则将run目录打包存进bak目录
  291. LOGD("tar %s to %s/%s\n", RUN_FOLDER, BAK_FOLDER, UPG_TAR_NAME);
  292. snprintf(cmd, sizeof(cmd), "tar -czpPf %s/%s %s > /dev/null 2>&1", BAK_FOLDER, UPG_TAR_NAME, RUN_FOLDER);
  293. r = system(cmd);
  294. if(r) {
  295. LOGE("%s failed, %d\n", cmd, r);
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. /*
  301. when app run failed, copy to bak/ run/
  302. */
  303. static int file_restore(void)
  304. {
  305. int r;
  306. char cmd[200];
  307. snprintf(cmd, sizeof(cmd), "tar -xpf %s/%s -C / > /dev/null 2>&1", DOWN_FOLDER, UPG_TAR_NAME);
  308. r = system(cmd);
  309. if(r) {
  310. LOGE("%s failed, %d\n", cmd, r);
  311. return -1;
  312. }
  313. return 0;
  314. }
  315. #define CHAR(b) (((b)<=9)?((b)+0x30):((b)-10+0x61));
  316. static void byte2char(char *chr, uint8_t b)
  317. {
  318. chr[0] = CHAR(b>>4);
  319. chr[1] = CHAR(b&0x0f);
  320. }
  321. static void print_md5(char *s, md5_t *m)
  322. {
  323. int i;
  324. LOGD("__%s", s);
  325. for(i=0; i<32; i++) {
  326. LOGD("%c", m->data[i]);
  327. }
  328. LOGD("\n");
  329. }
  330. //md5相同返回0,否则返回1
  331. static int md5_check(upgrade_file_t *h)
  332. {
  333. md5_ctx_t ctx;
  334. uint8_t i,tmp[16];
  335. md5_t m1,m2;
  336. md5_init(&ctx);
  337. md5_update(&ctx, (uint8_t*)h, h->dlen+sizeof(upgrade_file_t));
  338. md5_final(&ctx, tmp);
  339. for (i=0; i<16; i++) {
  340. byte2char(&m2.data[i*2], tmp[i]);
  341. }
  342. memcpy(&m1, h->data+h->dlen, sizeof(md5_t));
  343. print_md5("ori md5: ", &m1);
  344. print_md5("cal md5: ", &m2);
  345. if(memcmp(&m1, &m2, sizeof(md5_t))) {
  346. return -1;
  347. }
  348. return 0;
  349. }
  350. static int file_split(void)
  351. {
  352. int r=0,flen;
  353. char *fbuf=NULL;
  354. char path[200];
  355. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  356. flen = file_length(path);
  357. if(flen<=0) {
  358. LOGE("file_length %s failed\n", path);
  359. return -1;
  360. }
  361. fbuf = (char*)malloc(flen);
  362. if(!fbuf) {
  363. LOGE("malloc %d failed\n", flen);
  364. return -1;
  365. }
  366. r = file_load(path, fbuf, flen);
  367. if(r) {
  368. LOGE("%s load failed\n", path);
  369. return -1;
  370. }
  371. upgrade_file_t *hfile = (upgrade_file_t*)fbuf;
  372. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
  373. r = file_save(path, hfile, sizeof(upgrade_file_t));
  374. if(r) {
  375. LOGE("file_save %s failed\n", path);
  376. return -1;
  377. }
  378. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  379. r = file_save(path, hfile->data, hfile->dlen);
  380. if(r) {
  381. LOGE("file_save %s failed\n", path);
  382. return -1;
  383. }
  384. snprintf(path, sizeof(path), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  385. r = file_remove(path);
  386. if(r) {
  387. LOGE("file_remove %s failed\n", path);
  388. return -1;
  389. }
  390. return 0;
  391. }
  392. static int file_check(char *path)
  393. {
  394. int r=0,flen;
  395. char *fbuf=NULL;
  396. upgrade_file_t *hfile=NULL;
  397. flen = file_length(path);
  398. if(flen<=0) {
  399. LOGE("file_length %s failed\n", path);
  400. return -1;
  401. }
  402. fbuf = (char*)malloc(flen);
  403. if(!fbuf) {
  404. LOGE("malloc %d failed\n", flen);
  405. return -1;
  406. }
  407. r = file_load(path, fbuf, flen);
  408. if(r) {
  409. LOGE("%s load failed\n", path);
  410. return -1;
  411. }
  412. hfile = (upgrade_file_t*)fbuf;
  413. if(hfile->magic!=UPG_FILE_MAGIC ||
  414. hfile->dlen!=flen-sizeof(upgrade_file_t)-sizeof(md5_t)) {
  415. r = -1;
  416. LOGE("magic: 0x%08x, should be: 0x%08x\n", hfile->magic, UPG_FILE_MAGIC);
  417. LOGE("num: %d, should < %d\n", hfile->obj.num, UPGRADE_NUM_MAX);
  418. LOGE("stra: %d, should < %d\n", hfile->obj.stra, UPGRADE_STRA_MAX);
  419. LOGE("file hdr check failed\n");
  420. goto fail;
  421. }
  422. r = md5_check(hfile);
  423. if(r) {
  424. LOGE("md5 check failed\n");
  425. goto fail;
  426. }
  427. fail:
  428. free(fbuf);
  429. return r;
  430. }
  431. static int file_scan(upg_handle_t *h)
  432. {
  433. //检查DOWN目录,如有升级文件
  434. //udisk检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
  435. //tcard检查,如有升级文件,拷贝到DOWN_FOLDER目录,并设置step为UPGRADE_STEP_DOWN
  436. return 0;
  437. }
  438. static int file_clear(void)
  439. {
  440. char tmp[200];
  441. snprintf(tmp, sizeof(tmp), "rm -r %s/* %s/*", DOWN_FOLDER, UPG_FOLDER);
  442. return system(tmp);
  443. }
  444. //////////////////////////////////////////////////
  445. static int is_running(char *prog)
  446. {
  447. FILE *fp;
  448. int pid=0;
  449. char buf[20]={0};
  450. char command[200];
  451. snprintf(command, sizeof(command), "ps -ef | grep -v grep | grep -w -c %s", prog);
  452. fp = popen(command, "r");
  453. if (fp == NULL) {
  454. LOGE("execute %s failed: %s", command, strerror(errno));
  455. return 0;
  456. }
  457. if (fgets(buf, sizeof(buf), fp)) {
  458. pid = atoi(buf);
  459. }
  460. pclose(fp);
  461. return (pid)?1:0;
  462. }
  463. static int app_start(void)
  464. {
  465. LOGD("____ app_start\n");
  466. if(is_running("smartMonitor.sh")) {
  467. return -1;
  468. }
  469. return system("/usr/sbin/smartMonitor.sh &");
  470. }
  471. static int app_stop(void)
  472. {
  473. LOGD("____ app_stop\n");
  474. return system("killall smartMonitor.sh; /etc/init.d/S90PDUApp stop");
  475. }
  476. static int sys_reboot(void)
  477. {
  478. return system("reboot");
  479. }
  480. //////////////////////////////////////////////////
  481. static void* upg_thread(void *arg)
  482. {
  483. int r=0;
  484. int msgid;
  485. key_t key;
  486. msgbuf_t mb;
  487. upg_handle_t *h=(upg_handle_t*)arg;
  488. LOGD("upg_thread running\n");
  489. key = ftok(UPG_KEY_PATH, UPG_KEY_SN);
  490. if(key<0) {
  491. LOGE("ftok %s failed, errno: %d\n", UPG_KEY_PATH, errno);
  492. pthread_exit(NULL);
  493. }
  494. msgid = msgget(key, 0666|IPC_CREAT);
  495. LOGD("___ key: %d, msgid: %d\n", key, msgid);
  496. //msgctl(msgid, IPC_RMID, NULL);
  497. while(1) {
  498. r = msgrcv(msgid, &mb, sizeof(mb.evt), 0, 0);
  499. if(r<0) {
  500. LOGE("___msgrcv failed, %d\n", errno);
  501. continue;
  502. }
  503. LOGD("___ msgrcv evt: 0x%02x\n", mb.evt);
  504. switch(mb.evt) {
  505. case UPGRADE_EVT_APP_RUNNING:
  506. {
  507. LOGD("___ smartPDU running!\n");
  508. h->step = UPGRADE_STEP_SUCCESS;
  509. }
  510. break;
  511. case UPGRADE_EVT_UPLOAD_FINISH:
  512. {
  513. LOGD("___ upgrade package upload finished!\n");
  514. h->step = UPGRADE_STEP_DOWN;
  515. file_touch(UPGRADE_STEP_DOWN);
  516. }
  517. break;
  518. }
  519. }
  520. pthread_exit(NULL);
  521. }
  522. static int upg_init(upg_handle_t *h)
  523. {
  524. int r;
  525. folder_init();
  526. h->src = UPGRADE_SRC_NONE;
  527. h->stat = UPGRADE_STAT_NONE;
  528. return pthread_create(&h->tid, NULL, upg_thread, h);
  529. }
  530. static int upg_check(upg_handle_t *h)
  531. {
  532. int i,r;
  533. char tmp[200];
  534. for(i=1; i<UPGRADE_STEP_MAX; i++) {
  535. snprintf(tmp, sizeof(tmp), "%s/s%d", UPG_FOLDER, i);
  536. r = file_exist(tmp);
  537. if(r) {
  538. LOGD("%s exist\n", tmp);
  539. //重启后如果是覆盖完成,则将进度置为SUCCESS
  540. h->step = i+1;
  541. break;
  542. }
  543. LOGD("%s not exist\n", tmp);
  544. }
  545. return r;
  546. }
  547. static int upg_handle(upg_handle_t *h)
  548. {
  549. int r=-1;
  550. char tmp[200];
  551. //LOGD("___h->step: %d\n", h->step);
  552. while(h->step!=UPGRADE_STEP_NONE) {
  553. switch(h->step) {
  554. case UPGRADE_STEP_DOWN:
  555. {
  556. LOGD("___ UPGRADE_STEP_DOWN\n");
  557. //先检查下载目录是否存在pdupkg文件
  558. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  559. if(file_exist(tmp)) {
  560. file_touch(UPGRADE_STEP_DOWN);
  561. h->step = UPGRADE_STEP_SPLIT;
  562. }
  563. else {
  564. LOGD("___ %s not exist, ", tmp);
  565. //下载目录不存在pdupkg文件,则继续检查是否存在pdutar文件
  566. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  567. LOGD("read %s\n", tmp);
  568. if(file_exist(tmp)) {
  569. file_touch(UPGRADE_STEP_DOWN);
  570. h->step = UPGRADE_STEP_COVER;
  571. }
  572. else {
  573. file_untouch(UPGRADE_STEP_DOWN);
  574. h->step = UPGRADE_STEP_NONE;
  575. }
  576. }
  577. }
  578. break;
  579. case UPGRADE_STEP_SPLIT:
  580. {
  581. LOGD("___ UPGRADE_STEP_SPLIT\n");
  582. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_PKG_NAME);
  583. r = file_check(tmp);
  584. if(r==0) {
  585. r = file_split();
  586. if(r==0) {
  587. file_touch(UPGRADE_STEP_SPLIT);
  588. h->step = UPGRADE_STEP_COVER;
  589. }
  590. else {
  591. file_untouch(UPGRADE_STEP_SPLIT);
  592. h->step = UPGRADE_STEP_NONE;
  593. }
  594. }
  595. else {
  596. file_remove(tmp);
  597. h->step = UPGRADE_STEP_NONE;
  598. }
  599. file_untouch(UPGRADE_STEP_DOWN);
  600. }
  601. break;
  602. case UPGRADE_STEP_COVER:
  603. {
  604. LOGD("___ UPGRADE_STEP_COVER\n");
  605. app_stop();
  606. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_TAR_NAME);
  607. r = file_cover(tmp);
  608. if(r==0) {
  609. file_touch(UPGRADE_STEP_COVER);
  610. h->step = UPGRADE_STEP_FINISH;
  611. sys_reboot();
  612. }
  613. else {
  614. LOGD("file_cover, delete %s!\n", tmp);
  615. file_remove(tmp);
  616. snprintf(tmp, sizeof(tmp), "%s/%s", DOWN_FOLDER, UPG_HDR_NAME);
  617. file_remove(tmp);
  618. h->step = UPGRADE_STEP_NONE;
  619. file_untouch(UPGRADE_STEP_COVER);
  620. file_restore();
  621. app_start();
  622. }
  623. file_untouch(UPGRADE_STEP_DOWN);
  624. file_untouch(UPGRADE_STEP_SPLIT);
  625. }
  626. break;
  627. case UPGRADE_STEP_FINISH:
  628. {
  629. LOGD("___ UPGRADE_STEP_FINISH\n");
  630. h->step = UPGRADE_STEP_NONE;
  631. }
  632. break;
  633. case UPGRADE_STEP_SUCCESS:
  634. {
  635. LOGD("___ UPGRADE_STEP_SUCCESS\n");
  636. h->step = UPGRADE_STEP_NONE;
  637. file_backup(); //启动成功才证明程序有效,可进行备份, 备份成功,删除下载目录中的文件以节省空间
  638. file_clear();
  639. //app_start();
  640. }
  641. break;
  642. default:
  643. LOGD("___ wrong step: %d\n", h->step);
  644. r = 1;
  645. break;
  646. }
  647. }
  648. return r;
  649. }
  650. /////////////////////////////////////////////////////////////////////////
  651. int main(void)
  652. {
  653. int id,r=0;
  654. upg_handle_t *h=&upgHandle;
  655. LOGD("___upgrade running ...\n");
  656. upg_init(h);
  657. upg_check(h);
  658. while(1) {
  659. upg_handle(h);
  660. sleep(1);
  661. }
  662. return 0;
  663. }