fota.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <aos/list.h>
  8. #include <aos/kernel.h>
  9. #include <aos/kv.h>
  10. #include <yoc/netio.h>
  11. #include <yoc/fota.h>
  12. #include <ulog/ulog.h>
  13. #define TAG "fota"
  14. typedef struct fota_netio_list {
  15. slist_t next;
  16. const fota_cls_t *cls;
  17. } fota_cls_node_t;
  18. static AOS_SLIST_HEAD(fota_cls_list);
  19. int fota_register(const fota_cls_t *cls)
  20. {
  21. fota_cls_node_t *node = aos_malloc(sizeof(fota_cls_node_t));
  22. if (node) {
  23. node->cls = cls;
  24. slist_add_tail(&node->next, &fota_cls_list);
  25. return 0;
  26. }
  27. return -1;
  28. }
  29. fota_t *fota_open(const char *fota_name, const char *dst, fota_event_cb_t event_cb)
  30. {
  31. fota_cls_node_t *node;
  32. fota_t *fota = NULL;
  33. fota_config_t config = { 3000, 3000, 0, 30000, 0 };
  34. if (!(fota_name && dst)) {
  35. LOGE(TAG, "fota open e.");
  36. return NULL;
  37. }
  38. slist_for_each_entry(&fota_cls_list, node, fota_cls_node_t, next) {
  39. if (strcmp(node->cls->name, fota_name) == 0) {
  40. fota = aos_zalloc(sizeof(fota_t));
  41. if (fota == NULL) {
  42. LOGE(TAG, "malloc fota failed.");
  43. return NULL;
  44. }
  45. fota->to_path = strdup(dst);
  46. fota->cls = node->cls;
  47. fota->event_cb = event_cb;
  48. memcpy(&fota->config, &config, sizeof(fota_config_t));
  49. if (fota->cls->init)
  50. fota->cls->init(&fota->info);
  51. LOGD(TAG, "fota: 0x%x path:%s", fota, fota->to_path);
  52. break;
  53. }
  54. }
  55. if (fota == NULL) {
  56. LOGE(TAG, "fota open e. %s cls not found", fota_name);
  57. return NULL;
  58. }
  59. aos_sem_new(&fota->sem, 0);
  60. aos_sem_new(&fota->do_check_event, 0);
  61. aos_sem_new(&fota->sem_download, 0);
  62. return fota;
  63. }
  64. static int fota_version_check(fota_t *fota, fota_info_t *info) {
  65. if (fota && fota->cls && fota->cls->version_check) {
  66. int ret = fota->cls->version_check(info);
  67. if (ret != 0) {
  68. return -1;
  69. }
  70. if (fota->from_path == NULL) {
  71. fota->from_path = strdup(info->fota_url);
  72. } else {
  73. aos_free(fota->from_path);
  74. fota->from_path = strdup(info->fota_url);
  75. }
  76. LOGD(TAG, "get image url: %s", fota->from_path);
  77. return ret;
  78. }
  79. return -1;
  80. }
  81. static void fota_finish(fota_t *fota, fota_info_t *info)
  82. {
  83. if (fota->cls->finish)
  84. fota->cls->finish(info);
  85. }
  86. static void fota_fail(fota_t *fota, fota_info_t *info)
  87. {
  88. if (fota->cls->fail)
  89. fota->cls->fail(info);
  90. }
  91. static int fota_prepare(fota_t *fota)
  92. {
  93. int seek;
  94. if (!(fota->from_path && fota->to_path)) {
  95. LOGE(TAG, "fota->from_path or fota->to_path is NULL");
  96. return -EINVAL;
  97. }
  98. LOGD(TAG, "###fota->from_path:%s, fota->to_path:%s", fota->from_path, fota->to_path);
  99. fota->buffer = aos_malloc(CONFIG_FOTA_BUFFER_SIZE);
  100. fota->from = netio_open(fota->from_path);
  101. fota->to = netio_open(fota->to_path);
  102. if (fota->buffer == NULL || fota->from == NULL || fota->to == NULL) {
  103. if (fota->buffer == NULL) {
  104. LOGD(TAG, "fota->buffer e");
  105. } else if (fota->from == NULL) {
  106. LOGD(TAG, "fota->from e");
  107. } else if (fota->to == NULL) {
  108. LOGD(TAG, "fota->to e");
  109. }
  110. goto error;
  111. }
  112. seek = 0;
  113. do_seek:
  114. if (aos_kv_getint(KV_FOTA_OFFSET, &fota->offset) < 0) {
  115. if (aos_kv_setint(KV_FOTA_OFFSET, 0) < 0) {
  116. goto error;
  117. }
  118. fota->offset = 0;
  119. }
  120. LOGI(TAG, "FOTA seek %d", fota->offset);
  121. if (netio_seek(fota->from, fota->offset, SEEK_SET) != 0) {
  122. LOGD(TAG, "from seek error");
  123. fota->offset = 0;
  124. if (aos_kv_setint(KV_FOTA_OFFSET, 0) < 0) {
  125. goto error;
  126. }
  127. if (seek) {
  128. goto error;
  129. }
  130. seek = 1;
  131. goto do_seek;
  132. }
  133. if (netio_seek(fota->to, fota->offset, SEEK_SET) != 0) {
  134. LOGD(TAG, "to seek error");
  135. fota->offset = 0;
  136. if (aos_kv_setint(KV_FOTA_OFFSET, 0) < 0) {
  137. goto error;
  138. }
  139. if (seek) {
  140. goto error;
  141. }
  142. seek = 1;
  143. goto do_seek;
  144. }
  145. fota->status = FOTA_DOWNLOAD;
  146. fota->total_size = fota->from->size;
  147. LOGD(TAG, "fota prepare ok.");
  148. return 0;
  149. error:
  150. if (fota->buffer) {
  151. aos_free(fota->buffer);
  152. fota->buffer = NULL;
  153. }
  154. if (fota->from) {
  155. netio_close(fota->from);
  156. fota->from = NULL;
  157. }
  158. if (fota->to) {
  159. netio_close(fota->to);
  160. fota->to = NULL;
  161. }
  162. return -1;
  163. }
  164. static void fota_release(fota_t *fota)
  165. {
  166. LOGD(TAG, "%s,%d", __func__, __LINE__);
  167. if (fota->buffer) {
  168. aos_free(fota->buffer);
  169. fota->buffer = NULL;
  170. }
  171. if (fota->from) {
  172. netio_close(fota->from);
  173. fota->from = NULL;
  174. }
  175. if (fota->to) {
  176. netio_close(fota->to);
  177. fota->to = NULL;
  178. }
  179. }
  180. static void fota_task(void *arg)
  181. {
  182. fota_t *fota = (fota_t *)arg;
  183. int retry = fota->config.retry_count;
  184. LOGD(TAG, "fota_task start: %s", fota->to_path);
  185. while (!fota->quit) {
  186. if (fota->status == FOTA_INIT) {
  187. LOGD(TAG, "fota_task FOTA_INIT! wait......");
  188. aos_sem_wait(&fota->do_check_event, -1);
  189. if (fota->quit) {
  190. break;
  191. }
  192. if (fota->status == FOTA_DOWNLOAD) {
  193. continue;
  194. }
  195. retry = fota->config.retry_count;
  196. if (fota->event_cb) {
  197. fota->error_code = FOTA_ERROR_NULL;
  198. fota->event_cb(arg, FOTA_EVENT_START);
  199. }
  200. if (fota_version_check(fota, &fota->info) == 0) {
  201. if (fota->event_cb) {
  202. fota->error_code = FOTA_ERROR_NULL;
  203. fota->event_cb(arg, FOTA_EVENT_VERSION);
  204. }
  205. if (fota->config.auto_check_en > 0) {
  206. if (fota_prepare(fota) < 0) {
  207. LOGE(TAG, "fota_prepare failed");
  208. if (fota->event_cb) {
  209. fota->error_code = FOTA_ERROR_PREPARE;
  210. fota->event_cb(arg, FOTA_EVENT_VERSION);
  211. }
  212. }
  213. }
  214. } else {
  215. if (fota->event_cb) {
  216. fota->error_code = FOTA_ERROR_VERSION_CHECK;
  217. fota->event_cb(arg, FOTA_EVENT_VERSION);
  218. }
  219. }
  220. if (fota->config.auto_check_en > 0) {
  221. if (fota->status == FOTA_DOWNLOAD) {
  222. aos_sem_signal(&fota->sem_download);
  223. continue;
  224. }
  225. aos_msleep(fota->config.sleep_time);
  226. aos_sem_signal(&fota->do_check_event);
  227. }
  228. } else if (fota->status == FOTA_DOWNLOAD) {
  229. LOGD(TAG, "fota_task download! wait......");
  230. aos_sem_wait(&fota->sem_download, -1);
  231. if (fota->quit) {
  232. break;
  233. }
  234. int size = netio_read(fota->from, fota->buffer, CONFIG_FOTA_BUFFER_SIZE, fota->config.read_timeoutms);
  235. fota->total_size = fota->from->size;
  236. LOGD(TAG, "fota_task FOTA_DOWNLOAD! total:%d offset:%d", fota->from->size, fota->to->offset);
  237. LOGD(TAG, "##read: %d", size);
  238. if (size < 0) {
  239. LOGD(TAG, "read size < 0 %d", size);
  240. if (size == -2) {
  241. LOGW(TAG, "reconnect again");
  242. aos_sem_signal(&fota->sem_download);
  243. continue;
  244. }
  245. if (fota->event_cb) {
  246. fota->error_code = FOTA_ERROR_NET_READ;
  247. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  248. }
  249. fota->status = FOTA_ABORT;
  250. LOGD(TAG, "fota abort");
  251. continue;
  252. } else if (size == 0) {
  253. // download finish
  254. LOGD(TAG, "read size 0.");
  255. if (fota->event_cb) {
  256. fota->error_code = FOTA_ERROR_NULL;
  257. fota->event_cb(arg, FOTA_EVENT_VERIFY);
  258. }
  259. int verify = fota_data_verify();
  260. fota_finish(fota, &fota->info);
  261. fota_release(fota);
  262. aos_kv_del(KV_FOTA_OFFSET);
  263. if (verify != 0) {
  264. LOGE(TAG, "fota data verify failed.");
  265. fota->error_code = FOTA_ERROR_VERIFY;
  266. if (fota->event_cb)
  267. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  268. // goto init status
  269. fota->status = FOTA_INIT;
  270. if (fota->config.auto_check_en > 0) {
  271. aos_msleep(fota->config.sleep_time);
  272. aos_sem_signal(&fota->do_check_event);
  273. }
  274. } else {
  275. LOGD(TAG, "fota data verify ok.");
  276. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  277. aos_kv_setint(KV_FOTA_FINISH, 1);
  278. #endif
  279. fota->status = FOTA_FINISH;
  280. fota->error_code = FOTA_ERROR_NULL;
  281. if (fota->event_cb)
  282. fota->event_cb(arg, FOTA_EVENT_FINISH);
  283. if (fota->config.auto_check_en > 0) {
  284. fota_restart(fota, 0);
  285. }
  286. }
  287. continue;
  288. }
  289. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  290. if (fota->offset == 0) {
  291. LOGD(TAG, "set fota_finish to 0");
  292. if (aos_kv_setint(KV_FOTA_FINISH, 0) < 0) {
  293. goto write_err;
  294. }
  295. }
  296. #endif
  297. size = netio_write(fota->to, fota->buffer, size, fota->config.write_timeoutms);
  298. LOGI(TAG, "write size: %d", size);
  299. if (size > 0) {
  300. if (aos_kv_setint(KV_FOTA_OFFSET, fota->offset + size) < 0) {
  301. goto write_err;
  302. }
  303. fota->offset += size;
  304. if (fota->event_cb) {
  305. fota->error_code = FOTA_ERROR_NULL;
  306. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  307. }
  308. aos_sem_signal(&fota->sem_download);
  309. } else {
  310. write_err:
  311. // flash write error
  312. LOGE(TAG, "flash write size error.");
  313. if (fota->event_cb) {
  314. fota->error_code = FOTA_ERROR_WRITE;
  315. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  316. }
  317. fota->status = FOTA_ABORT;
  318. }
  319. } else if (fota->status == FOTA_ABORT) {
  320. LOGD(TAG, "fota_task FOTA_ABORT!");
  321. if (retry != 0) {
  322. LOGW(TAG, "fota retry: %d!", retry);
  323. retry--;
  324. fota->status = FOTA_DOWNLOAD;
  325. aos_sem_signal(&fota->sem_download);
  326. } else {
  327. retry = fota->config.retry_count;
  328. fota_fail(fota, &fota->info);
  329. fota_release(fota);
  330. fota->status = FOTA_INIT;
  331. if (fota->config.auto_check_en > 0) {
  332. aos_msleep(fota->config.sleep_time);
  333. aos_sem_signal(&fota->do_check_event);
  334. }
  335. }
  336. } else {
  337. // finish state will come here.
  338. LOGD(TAG, "##fota status:%d", fota->status);
  339. aos_msleep(fota->config.sleep_time);
  340. }
  341. }
  342. LOGD(TAG, "force quit need release source");
  343. fota->status = 0; // need reset for next restart
  344. fota_release(fota);
  345. if (fota->event_cb) {
  346. fota->error_code = FOTA_ERROR_NULL;
  347. fota->event_cb(arg, FOTA_EVENT_QUIT);
  348. }
  349. /* will free fota */
  350. aos_sem_signal(&fota->sem);
  351. }
  352. void fota_do_check(fota_t *fota)
  353. {
  354. LOGD(TAG, "fota do check signal........");
  355. if (fota && (fota->status == FOTA_INIT || fota->status == FOTA_FINISH)) {
  356. fota->status = FOTA_INIT;
  357. aos_sem_signal(&fota->do_check_event);
  358. }
  359. }
  360. int fota_start(fota_t *fota)
  361. {
  362. if (fota == NULL) {
  363. return -EINVAL;
  364. }
  365. if (fota->status == 0) {
  366. fota->status = FOTA_INIT;
  367. fota->quit = 0;
  368. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  369. int isfinish;
  370. if (aos_kv_getint(KV_FOTA_FINISH, &isfinish) < 0) {
  371. isfinish = 0;
  372. }
  373. if (isfinish > 0) {
  374. LOGD(TAG, "come to fota finish state.");
  375. fota->status = FOTA_FINISH;
  376. }
  377. #endif
  378. if (aos_task_new_ext(&fota->task, "fota", fota_task, fota, CONFIG_FOTA_TASK_STACK_SIZE, 45) != 0) {
  379. fota->quit = 1;
  380. fota->status = 0;
  381. LOGE(TAG, "fota task create failed.");
  382. return -1;
  383. }
  384. if (fota->config.auto_check_en > 0) {
  385. fota_do_check(fota);
  386. }
  387. }
  388. return 0;
  389. }
  390. int fota_stop(fota_t *fota)
  391. {
  392. if (fota == NULL) {
  393. return -EINVAL;
  394. }
  395. LOGD(TAG, "%s,%d", __func__, __LINE__);
  396. fota->quit = 1;
  397. aos_sem_signal(&fota->do_check_event);
  398. aos_sem_signal(&fota->sem_download);
  399. return 0;
  400. }
  401. int fota_close(fota_t *fota)
  402. {
  403. if (fota == NULL) {
  404. return -EINVAL;
  405. }
  406. LOGD(TAG, "%s,%d", __func__, __LINE__);
  407. fota->quit = 1;
  408. aos_sem_signal(&fota->do_check_event);
  409. aos_sem_signal(&fota->sem_download);
  410. aos_sem_wait(&fota->sem, -1);
  411. aos_sem_free(&fota->sem);
  412. aos_sem_free(&fota->do_check_event);
  413. aos_sem_free(&fota->sem_download);
  414. if (fota->from_path) aos_free(fota->from_path);
  415. if (fota->to_path) aos_free(fota->to_path);
  416. if (fota->buffer) aos_free(fota->buffer);
  417. if (fota->from) netio_close(fota->from);
  418. if (fota->to) netio_close(fota->to);
  419. aos_free(fota);
  420. return 0;
  421. }
  422. int fota_download(fota_t *fota)
  423. {
  424. if (fota == NULL) {
  425. LOGE(TAG, "fota param null.");
  426. return -EINVAL;
  427. }
  428. LOGD(TAG, "fota download, status:%d", fota->status);
  429. if (fota->status == FOTA_ABORT || fota->status == FOTA_INIT || fota->status == FOTA_FINISH) {
  430. if (fota->status == FOTA_INIT || fota->status == FOTA_FINISH) {
  431. if (fota_prepare(fota)) {
  432. LOGE(TAG, "fota_prepare failed");
  433. if (fota->event_cb) {
  434. fota->error_code = FOTA_ERROR_PREPARE;
  435. fota->event_cb(fota, FOTA_EVENT_VERSION);
  436. }
  437. return -1;
  438. }
  439. aos_sem_signal(&fota->do_check_event);
  440. } else {
  441. fota->status = FOTA_DOWNLOAD;
  442. }
  443. LOGD(TAG, "signal download.");
  444. aos_sem_signal(&fota->sem_download);
  445. return 0;
  446. }
  447. LOGW(TAG, "the status is not allow to download.");
  448. return -1;
  449. }
  450. static void timer_thread(void *timer, void *args)
  451. {
  452. fota_t *fota = (fota_t *)args;
  453. LOGD(TAG, "timer_thread, fota:0x%x", fota);
  454. if (fota) {
  455. LOGD(TAG, "report restart event.");
  456. if (fota->event_cb) {
  457. fota->error_code = FOTA_ERROR_NULL;
  458. fota->event_cb(fota, FOTA_EVENT_RESTART);
  459. }
  460. aos_timer_stop(&fota->restart_timer);
  461. aos_timer_free(&fota->restart_timer);
  462. LOGD(TAG, "stop and delete timer ok.");
  463. return;
  464. }
  465. LOGE(TAG, "timer task params error.");
  466. }
  467. int fota_restart(fota_t *fota, int delay_ms)
  468. {
  469. if (fota == NULL) {
  470. LOGE(TAG, "fota param null.");
  471. return -EINVAL;
  472. }
  473. LOGD(TAG, "%s,%d, delay_ms:%d", __func__, __LINE__, delay_ms);
  474. if (fota->status != FOTA_FINISH) {
  475. LOGE(TAG, "fota status is not FOTA_FINISH, cant restart to upgrade.");
  476. return -1;
  477. }
  478. if (fota->cls && fota->cls->restart) {
  479. if (delay_ms > 0) {
  480. int ret = aos_timer_new_ext(&fota->restart_timer, timer_thread, fota, delay_ms, 0, 1);
  481. if (ret < 0) {
  482. LOGE(TAG, "timer_create error [%d]!\n", errno);
  483. return -1;
  484. }
  485. LOGD(TAG, "set timer ok.");
  486. return 0;
  487. }
  488. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  489. if (aos_kv_setint(KV_FOTA_FINISH, 0) < 0) {
  490. LOGE(TAG, "set finish 0 error.");
  491. return -1;
  492. }
  493. #endif
  494. fota->cls->restart();
  495. }
  496. return 0;
  497. }
  498. void fota_config(fota_t *fota, fota_config_t *config)
  499. {
  500. if (!(fota && config)) {
  501. LOGE(TAG, "fota or config param null.");
  502. return;
  503. }
  504. memcpy(&fota->config, config, sizeof(fota_config_t));
  505. }
  506. fota_config_t *fota_get_config(fota_t *fota)
  507. {
  508. if (fota == NULL) {
  509. LOGE(TAG, "fota param null.");
  510. return NULL;
  511. }
  512. return &fota->config;
  513. }
  514. void fota_set_auto_check(fota_t *fota, int enable)
  515. {
  516. if (fota)
  517. fota->config.auto_check_en = enable;
  518. }
  519. int fota_get_auto_check(fota_t *fota)
  520. {
  521. if (fota)
  522. return fota->config.auto_check_en;
  523. return 0;
  524. }
  525. fota_status_e fota_get_status(fota_t *fota)
  526. {
  527. if (fota == NULL) {
  528. LOGE(TAG, "fota param null.");
  529. return 0;
  530. }
  531. return fota->status;
  532. }
  533. int64_t fota_get_size(fota_t *fota, const char *name)
  534. {
  535. if (fota == NULL) {
  536. LOGE(TAG, "fota param null.");
  537. return -EINVAL;
  538. }
  539. if (fota->cls && fota->cls->get_size) {
  540. return fota->cls->get_size(name);
  541. }
  542. return 0;
  543. }