fota.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. if (!(fota->from_path && fota->to_path)) {
  94. LOGE(TAG, "fota->from_path or fota->to_path is NULL");
  95. return -EINVAL;
  96. }
  97. LOGD(TAG, "###fota->from_path:%s, fota->to_path:%s", fota->from_path, fota->to_path);
  98. fota->buffer = aos_malloc(CONFIG_FOTA_BUFFER_SIZE);
  99. fota->from = netio_open(fota->from_path);
  100. fota->to = netio_open(fota->to_path);
  101. if (fota->buffer == NULL || fota->from == NULL || fota->to == NULL) {
  102. if (fota->buffer == NULL) {
  103. LOGD(TAG, "fota->buffer e");
  104. } else if (fota->from == NULL) {
  105. LOGD(TAG, "fota->from e");
  106. } else if (fota->to == NULL) {
  107. LOGD(TAG, "fota->to e");
  108. }
  109. goto error;
  110. }
  111. if (aos_kv_getint(KV_FOTA_OFFSET, &fota->offset) < 0) {
  112. if (aos_kv_setint(KV_FOTA_OFFSET, 0) < 0) {
  113. goto error;
  114. }
  115. fota->offset = 0;
  116. }
  117. LOGI(TAG, "FOTA seek %d", fota->offset);
  118. if (netio_seek(fota->from, fota->offset, SEEK_SET) != 0) {
  119. LOGD(TAG, "from seek error");
  120. goto error;
  121. }
  122. if (netio_seek(fota->to, fota->offset, SEEK_SET) != 0) {
  123. LOGD(TAG, "to seek error");
  124. goto error;
  125. }
  126. fota->status = FOTA_DOWNLOAD;
  127. fota->total_size = fota->from->size;
  128. LOGD(TAG, "fota prepare ok.");
  129. return 0;
  130. error:
  131. if (fota->buffer) {
  132. aos_free(fota->buffer);
  133. fota->buffer = NULL;
  134. }
  135. if (fota->from) {
  136. netio_close(fota->from);
  137. fota->from = NULL;
  138. }
  139. if (fota->to) {
  140. netio_close(fota->to);
  141. fota->to = NULL;
  142. }
  143. return -1;
  144. }
  145. static void fota_release(fota_t *fota)
  146. {
  147. LOGD(TAG, "%s,%d", __func__, __LINE__);
  148. if (fota->buffer) {
  149. aos_free(fota->buffer);
  150. fota->buffer = NULL;
  151. }
  152. if (fota->from) {
  153. netio_close(fota->from);
  154. fota->from = NULL;
  155. }
  156. if (fota->to) {
  157. netio_close(fota->to);
  158. fota->to = NULL;
  159. }
  160. }
  161. static void fota_task(void *arg)
  162. {
  163. fota_t *fota = (fota_t *)arg;
  164. int retry = fota->config.retry_count;
  165. LOGD(TAG, "fota_task start: %s", fota->to_path);
  166. while (!fota->quit) {
  167. if (fota->status == FOTA_INIT) {
  168. LOGD(TAG, "fota_task FOTA_INIT! wait......");
  169. aos_sem_wait(&fota->do_check_event, -1);
  170. if (fota->quit) {
  171. break;
  172. }
  173. if (fota->status == FOTA_DOWNLOAD) {
  174. continue;
  175. }
  176. retry = fota->config.retry_count;
  177. if (fota->event_cb) {
  178. fota->error_code = FOTA_ERROR_NULL;
  179. fota->event_cb(arg, FOTA_EVENT_START);
  180. }
  181. if (fota_version_check(fota, &fota->info) == 0) {
  182. if (fota->event_cb) {
  183. fota->error_code = FOTA_ERROR_NULL;
  184. fota->event_cb(arg, FOTA_EVENT_VERSION);
  185. }
  186. if (fota->config.auto_check_en > 0) {
  187. if (fota_prepare(fota) < 0) {
  188. LOGE(TAG, "fota_prepare failed");
  189. if (fota->event_cb) {
  190. fota->error_code = FOTA_ERROR_PREPARE;
  191. fota->event_cb(arg, FOTA_EVENT_VERSION);
  192. }
  193. }
  194. }
  195. } else {
  196. if (fota->event_cb) {
  197. fota->error_code = FOTA_ERROR_VERSION_CHECK;
  198. fota->event_cb(arg, FOTA_EVENT_VERSION);
  199. }
  200. }
  201. if (fota->config.auto_check_en > 0) {
  202. if (fota->status == FOTA_DOWNLOAD) {
  203. aos_sem_signal(&fota->sem_download);
  204. continue;
  205. }
  206. aos_msleep(fota->config.sleep_time);
  207. aos_sem_signal(&fota->do_check_event);
  208. }
  209. } else if (fota->status == FOTA_DOWNLOAD) {
  210. LOGD(TAG, "fota_task download! wait......");
  211. aos_sem_wait(&fota->sem_download, -1);
  212. if (fota->quit) {
  213. break;
  214. }
  215. int size = netio_read(fota->from, fota->buffer, CONFIG_FOTA_BUFFER_SIZE, fota->config.read_timeoutms);
  216. fota->total_size = fota->from->size;
  217. LOGD(TAG, "fota_task FOTA_DOWNLOAD! total:%d offset:%d", fota->from->size, fota->to->offset);
  218. LOGD(TAG, "##read: %d", size);
  219. if (size < 0) {
  220. LOGD(TAG, "read size < 0 %d", size);
  221. if (size == -2) {
  222. LOGW(TAG, "reconnect again");
  223. aos_sem_signal(&fota->sem_download);
  224. continue;
  225. }
  226. if (fota->event_cb) {
  227. fota->error_code = FOTA_ERROR_NET_READ;
  228. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  229. }
  230. fota->status = FOTA_ABORT;
  231. LOGD(TAG, "fota abort");
  232. continue;
  233. } else if (size == 0) {
  234. // download finish
  235. LOGD(TAG, "read size 0.");
  236. if (fota->event_cb) {
  237. fota->error_code = FOTA_ERROR_NULL;
  238. fota->event_cb(arg, FOTA_EVENT_VERIFY);
  239. }
  240. int verify = fota_data_verify();
  241. fota_finish(fota, &fota->info);
  242. fota_release(fota);
  243. aos_kv_del(KV_FOTA_OFFSET);
  244. if (verify != 0) {
  245. LOGE(TAG, "fota data verify failed.");
  246. fota->error_code = FOTA_ERROR_VERIFY;
  247. if (fota->event_cb)
  248. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  249. // goto init status
  250. fota->status = FOTA_INIT;
  251. if (fota->config.auto_check_en > 0) {
  252. aos_msleep(fota->config.sleep_time);
  253. aos_sem_signal(&fota->do_check_event);
  254. }
  255. } else {
  256. LOGD(TAG, "fota data verify ok.");
  257. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  258. aos_kv_setint(KV_FOTA_FINISH, 1);
  259. #endif
  260. fota->status = FOTA_FINISH;
  261. fota->error_code = FOTA_ERROR_NULL;
  262. if (fota->event_cb)
  263. fota->event_cb(arg, FOTA_EVENT_FINISH);
  264. if (fota->config.auto_check_en > 0) {
  265. fota_restart(fota, 0);
  266. }
  267. }
  268. continue;
  269. }
  270. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  271. if (fota->offset == 0) {
  272. LOGD(TAG, "set fota_finish to 0");
  273. if (aos_kv_setint(KV_FOTA_FINISH, 0) < 0) {
  274. goto write_err;
  275. }
  276. }
  277. #endif
  278. size = netio_write(fota->to, fota->buffer, size, fota->config.write_timeoutms);
  279. LOGI(TAG, "write size: %d", size);
  280. if (size > 0) {
  281. if (aos_kv_setint(KV_FOTA_OFFSET, fota->offset + size) < 0) {
  282. goto write_err;
  283. }
  284. fota->offset += size;
  285. if (fota->event_cb) {
  286. fota->error_code = FOTA_ERROR_NULL;
  287. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  288. }
  289. aos_sem_signal(&fota->sem_download);
  290. } else {
  291. write_err:
  292. // flash write error
  293. LOGE(TAG, "flash write size error.");
  294. if (fota->event_cb) {
  295. fota->error_code = FOTA_ERROR_WRITE;
  296. fota->event_cb(arg, FOTA_EVENT_PROGRESS);
  297. }
  298. fota->status = FOTA_ABORT;
  299. }
  300. } else if (fota->status == FOTA_ABORT) {
  301. LOGD(TAG, "fota_task FOTA_ABORT!");
  302. if (retry != 0) {
  303. LOGW(TAG, "fota retry: %d!", retry);
  304. retry--;
  305. fota->status = FOTA_DOWNLOAD;
  306. aos_msleep(fota->config.sleep_time);
  307. aos_sem_signal(&fota->sem_download);
  308. } else {
  309. retry = fota->config.retry_count;
  310. fota_fail(fota, &fota->info);
  311. fota_release(fota);
  312. fota->status = FOTA_INIT;
  313. if (fota->config.auto_check_en > 0) {
  314. aos_msleep(fota->config.sleep_time);
  315. aos_sem_signal(&fota->do_check_event);
  316. }
  317. }
  318. } else {
  319. // finish state will come here.
  320. LOGD(TAG, "##fota status:%d", fota->status);
  321. aos_msleep(fota->config.sleep_time);
  322. }
  323. }
  324. LOGD(TAG, "force quit need release source");
  325. fota->status = 0; // need reset for next restart
  326. fota_release(fota);
  327. if (fota->event_cb) {
  328. fota->error_code = FOTA_ERROR_NULL;
  329. fota->event_cb(arg, FOTA_EVENT_QUIT);
  330. }
  331. /* will free fota */
  332. aos_sem_signal(&fota->sem);
  333. }
  334. void fota_do_check(fota_t *fota)
  335. {
  336. LOGD(TAG, "fota do check signal........");
  337. if (fota && (fota->status == FOTA_INIT || fota->status == FOTA_FINISH)) {
  338. fota->status = FOTA_INIT;
  339. aos_sem_signal(&fota->do_check_event);
  340. }
  341. }
  342. int fota_start(fota_t *fota)
  343. {
  344. if (fota == NULL) {
  345. return -EINVAL;
  346. }
  347. if (fota->status == 0) {
  348. fota->status = FOTA_INIT;
  349. fota->quit = 0;
  350. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  351. int isfinish;
  352. if (aos_kv_getint(KV_FOTA_FINISH, &isfinish) < 0) {
  353. isfinish = 0;
  354. }
  355. if (isfinish > 0) {
  356. LOGD(TAG, "come to fota finish state.");
  357. fota->status = FOTA_FINISH;
  358. }
  359. #endif
  360. if (aos_task_new_ext(&fota->task, "fota", fota_task, fota, CONFIG_FOTA_TASK_STACK_SIZE, 45) != 0) {
  361. fota->quit = 1;
  362. fota->status = 0;
  363. LOGE(TAG, "fota task create failed.");
  364. return -1;
  365. }
  366. if (fota->config.auto_check_en > 0) {
  367. fota_do_check(fota);
  368. }
  369. }
  370. return 0;
  371. }
  372. int fota_stop(fota_t *fota)
  373. {
  374. if (fota == NULL) {
  375. return -EINVAL;
  376. }
  377. LOGD(TAG, "%s,%d", __func__, __LINE__);
  378. fota->quit = 1;
  379. aos_sem_signal(&fota->do_check_event);
  380. aos_sem_signal(&fota->sem_download);
  381. return 0;
  382. }
  383. int fota_close(fota_t *fota)
  384. {
  385. if (fota == NULL) {
  386. return -EINVAL;
  387. }
  388. LOGD(TAG, "%s,%d", __func__, __LINE__);
  389. fota->quit = 1;
  390. aos_sem_signal(&fota->do_check_event);
  391. aos_sem_signal(&fota->sem_download);
  392. aos_sem_wait(&fota->sem, -1);
  393. aos_sem_free(&fota->sem);
  394. aos_sem_free(&fota->do_check_event);
  395. aos_sem_free(&fota->sem_download);
  396. if (fota->from_path) aos_free(fota->from_path);
  397. if (fota->to_path) aos_free(fota->to_path);
  398. if (fota->buffer) aos_free(fota->buffer);
  399. if (fota->from) netio_close(fota->from);
  400. if (fota->to) netio_close(fota->to);
  401. aos_free(fota);
  402. return 0;
  403. }
  404. int fota_download(fota_t *fota)
  405. {
  406. if (fota == NULL) {
  407. LOGE(TAG, "fota param null.");
  408. return -EINVAL;
  409. }
  410. LOGD(TAG, "fota download, status:%d", fota->status);
  411. if (fota->status == FOTA_ABORT || fota->status == FOTA_INIT || fota->status == FOTA_FINISH) {
  412. if (fota->status == FOTA_INIT || fota->status == FOTA_FINISH) {
  413. if (fota_prepare(fota)) {
  414. LOGE(TAG, "fota_prepare failed");
  415. if (fota->event_cb) {
  416. fota->error_code = FOTA_ERROR_PREPARE;
  417. fota->event_cb(fota, FOTA_EVENT_VERSION);
  418. }
  419. return -1;
  420. }
  421. aos_sem_signal(&fota->do_check_event);
  422. } else {
  423. fota->status = FOTA_DOWNLOAD;
  424. }
  425. LOGD(TAG, "signal download.");
  426. aos_sem_signal(&fota->sem_download);
  427. return 0;
  428. }
  429. LOGW(TAG, "the status is not allow to download.");
  430. return -1;
  431. }
  432. static void timer_thread(void *timer, void *args)
  433. {
  434. fota_t *fota = (fota_t *)args;
  435. LOGD(TAG, "timer_thread, fota:0x%x", fota);
  436. if (fota) {
  437. LOGD(TAG, "report restart event.");
  438. if (fota->event_cb) {
  439. fota->error_code = FOTA_ERROR_NULL;
  440. fota->event_cb(fota, FOTA_EVENT_RESTART);
  441. }
  442. aos_timer_stop(&fota->restart_timer);
  443. aos_timer_free(&fota->restart_timer);
  444. LOGD(TAG, "stop and delete timer ok.");
  445. return;
  446. }
  447. LOGE(TAG, "timer task params error.");
  448. }
  449. int fota_restart(fota_t *fota, int delay_ms)
  450. {
  451. if (fota == NULL) {
  452. LOGE(TAG, "fota param null.");
  453. return -EINVAL;
  454. }
  455. LOGD(TAG, "%s,%d, delay_ms:%d", __func__, __LINE__, delay_ms);
  456. if (fota->status != FOTA_FINISH) {
  457. LOGE(TAG, "fota status is not FOTA_FINISH, cant restart to upgrade.");
  458. return -1;
  459. }
  460. if (fota->cls && fota->cls->restart) {
  461. if (delay_ms > 0) {
  462. int ret = aos_timer_new_ext(&fota->restart_timer, timer_thread, fota, delay_ms, 0, 1);
  463. if (ret < 0) {
  464. LOGE(TAG, "timer_create error [%d]!\n", errno);
  465. return -1;
  466. }
  467. LOGD(TAG, "set timer ok.");
  468. return 0;
  469. }
  470. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  471. if (aos_kv_setint(KV_FOTA_FINISH, 0) < 0) {
  472. LOGE(TAG, "set finish 0 error.");
  473. return -1;
  474. }
  475. #endif
  476. fota->cls->restart();
  477. }
  478. return 0;
  479. }
  480. void fota_config(fota_t *fota, fota_config_t *config)
  481. {
  482. if (!(fota && config)) {
  483. LOGE(TAG, "fota or config param null.");
  484. return;
  485. }
  486. memcpy(&fota->config, config, sizeof(fota_config_t));
  487. }
  488. fota_config_t *fota_get_config(fota_t *fota)
  489. {
  490. if (fota == NULL) {
  491. LOGE(TAG, "fota param null.");
  492. return NULL;
  493. }
  494. return &fota->config;
  495. }
  496. void fota_set_auto_check(fota_t *fota, int enable)
  497. {
  498. if (fota)
  499. fota->config.auto_check_en = enable;
  500. }
  501. int fota_get_auto_check(fota_t *fota)
  502. {
  503. if (fota)
  504. return fota->config.auto_check_en;
  505. return 0;
  506. }
  507. fota_status_e fota_get_status(fota_t *fota)
  508. {
  509. if (fota == NULL) {
  510. LOGE(TAG, "fota param null.");
  511. return 0;
  512. }
  513. return fota->status;
  514. }
  515. int64_t fota_get_size(fota_t *fota, const char *name)
  516. {
  517. if (fota == NULL) {
  518. LOGE(TAG, "fota param null.");
  519. return -EINVAL;
  520. }
  521. if (fota->cls && fota->cls->get_size) {
  522. return fota->cls->get_size(name);
  523. }
  524. return 0;
  525. }