fota.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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_sem_signal(&fota->sem_download);
  307. } else {
  308. retry = fota->config.retry_count;
  309. fota_fail(fota, &fota->info);
  310. fota_release(fota);
  311. fota->status = FOTA_INIT;
  312. if (fota->config.auto_check_en > 0) {
  313. aos_msleep(fota->config.sleep_time);
  314. aos_sem_signal(&fota->do_check_event);
  315. }
  316. }
  317. } else {
  318. // finish state will come here.
  319. LOGD(TAG, "##fota status:%d", fota->status);
  320. aos_msleep(fota->config.sleep_time);
  321. }
  322. }
  323. LOGD(TAG, "force quit need release source");
  324. fota->status = 0; // need reset for next restart
  325. fota_release(fota);
  326. if (fota->event_cb) {
  327. fota->error_code = FOTA_ERROR_NULL;
  328. fota->event_cb(arg, FOTA_EVENT_QUIT);
  329. }
  330. /* will free fota */
  331. aos_sem_signal(&fota->sem);
  332. }
  333. void fota_do_check(fota_t *fota)
  334. {
  335. LOGD(TAG, "fota do check signal........");
  336. if (fota && (fota->status == FOTA_INIT || fota->status == FOTA_FINISH)) {
  337. fota->status = FOTA_INIT;
  338. aos_sem_signal(&fota->do_check_event);
  339. }
  340. }
  341. int fota_start(fota_t *fota)
  342. {
  343. if (fota == NULL) {
  344. return -EINVAL;
  345. }
  346. if (fota->status == 0) {
  347. fota->status = FOTA_INIT;
  348. fota->quit = 0;
  349. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  350. int isfinish;
  351. if (aos_kv_getint(KV_FOTA_FINISH, &isfinish) < 0) {
  352. isfinish = 0;
  353. }
  354. if (isfinish > 0) {
  355. LOGD(TAG, "come to fota finish state.");
  356. fota->status = FOTA_FINISH;
  357. }
  358. #endif
  359. if (aos_task_new_ext(&fota->task, "fota", fota_task, fota, CONFIG_FOTA_TASK_STACK_SIZE, 45) != 0) {
  360. fota->quit = 1;
  361. fota->status = 0;
  362. LOGE(TAG, "fota task create failed.");
  363. return -1;
  364. }
  365. if (fota->config.auto_check_en > 0) {
  366. fota_do_check(fota);
  367. }
  368. }
  369. return 0;
  370. }
  371. int fota_stop(fota_t *fota)
  372. {
  373. if (fota == NULL) {
  374. return -EINVAL;
  375. }
  376. LOGD(TAG, "%s,%d", __func__, __LINE__);
  377. fota->quit = 1;
  378. aos_sem_signal(&fota->do_check_event);
  379. aos_sem_signal(&fota->sem_download);
  380. return 0;
  381. }
  382. int fota_close(fota_t *fota)
  383. {
  384. if (fota == NULL) {
  385. return -EINVAL;
  386. }
  387. LOGD(TAG, "%s,%d", __func__, __LINE__);
  388. fota->quit = 1;
  389. aos_sem_signal(&fota->do_check_event);
  390. aos_sem_signal(&fota->sem_download);
  391. aos_sem_wait(&fota->sem, -1);
  392. aos_sem_free(&fota->sem);
  393. aos_sem_free(&fota->do_check_event);
  394. aos_sem_free(&fota->sem_download);
  395. if (fota->from_path) aos_free(fota->from_path);
  396. if (fota->to_path) aos_free(fota->to_path);
  397. if (fota->buffer) aos_free(fota->buffer);
  398. if (fota->from) netio_close(fota->from);
  399. if (fota->to) netio_close(fota->to);
  400. aos_free(fota);
  401. return 0;
  402. }
  403. int fota_download(fota_t *fota)
  404. {
  405. if (fota == NULL) {
  406. LOGE(TAG, "fota param null.");
  407. return -EINVAL;
  408. }
  409. LOGD(TAG, "fota download, status:%d", fota->status);
  410. if (fota->status == FOTA_ABORT || fota->status == FOTA_INIT || fota->status == FOTA_FINISH) {
  411. if (fota->status == FOTA_INIT || fota->status == FOTA_FINISH) {
  412. if (fota_prepare(fota)) {
  413. LOGE(TAG, "fota_prepare failed");
  414. if (fota->event_cb) {
  415. fota->error_code = FOTA_ERROR_PREPARE;
  416. fota->event_cb(fota, FOTA_EVENT_VERSION);
  417. }
  418. return -1;
  419. }
  420. aos_sem_signal(&fota->do_check_event);
  421. } else {
  422. fota->status = FOTA_DOWNLOAD;
  423. }
  424. LOGD(TAG, "signal download.");
  425. aos_sem_signal(&fota->sem_download);
  426. return 0;
  427. }
  428. LOGW(TAG, "the status is not allow to download.");
  429. return -1;
  430. }
  431. static void timer_thread(void *timer, void *args)
  432. {
  433. fota_t *fota = (fota_t *)args;
  434. LOGD(TAG, "timer_thread, fota:0x%x", fota);
  435. if (fota) {
  436. LOGD(TAG, "report restart event.");
  437. if (fota->event_cb) {
  438. fota->error_code = FOTA_ERROR_NULL;
  439. fota->event_cb(fota, FOTA_EVENT_RESTART);
  440. }
  441. aos_timer_stop(&fota->restart_timer);
  442. aos_timer_free(&fota->restart_timer);
  443. LOGD(TAG, "stop and delete timer ok.");
  444. return;
  445. }
  446. LOGE(TAG, "timer task params error.");
  447. }
  448. int fota_restart(fota_t *fota, int delay_ms)
  449. {
  450. if (fota == NULL) {
  451. LOGE(TAG, "fota param null.");
  452. return -EINVAL;
  453. }
  454. LOGD(TAG, "%s,%d, delay_ms:%d", __func__, __LINE__, delay_ms);
  455. if (fota->status != FOTA_FINISH) {
  456. LOGE(TAG, "fota status is not FOTA_FINISH, cant restart to upgrade.");
  457. return -1;
  458. }
  459. if (fota->cls && fota->cls->restart) {
  460. if (delay_ms > 0) {
  461. int ret = aos_timer_new_ext(&fota->restart_timer, timer_thread, fota, delay_ms, 0, 1);
  462. if (ret < 0) {
  463. LOGE(TAG, "timer_create error [%d]!\n", errno);
  464. return -1;
  465. }
  466. LOGD(TAG, "set timer ok.");
  467. return 0;
  468. }
  469. #ifdef CONFIG_DL_FINISH_FLAG_POWSAVE
  470. if (aos_kv_setint(KV_FOTA_FINISH, 0) < 0) {
  471. LOGE(TAG, "set finish 0 error.");
  472. return -1;
  473. }
  474. #endif
  475. fota->cls->restart();
  476. }
  477. return 0;
  478. }
  479. void fota_config(fota_t *fota, fota_config_t *config)
  480. {
  481. if (!(fota && config)) {
  482. LOGE(TAG, "fota or config param null.");
  483. return;
  484. }
  485. memcpy(&fota->config, config, sizeof(fota_config_t));
  486. }
  487. fota_config_t *fota_get_config(fota_t *fota)
  488. {
  489. if (fota == NULL) {
  490. LOGE(TAG, "fota param null.");
  491. return NULL;
  492. }
  493. return &fota->config;
  494. }
  495. void fota_set_auto_check(fota_t *fota, int enable)
  496. {
  497. if (fota)
  498. fota->config.auto_check_en = enable;
  499. }
  500. int fota_get_auto_check(fota_t *fota)
  501. {
  502. if (fota)
  503. return fota->config.auto_check_en;
  504. return 0;
  505. }
  506. fota_status_e fota_get_status(fota_t *fota)
  507. {
  508. if (fota == NULL) {
  509. LOGE(TAG, "fota param null.");
  510. return 0;
  511. }
  512. return fota->status;
  513. }
  514. int64_t fota_get_size(fota_t *fota, const char *name)
  515. {
  516. if (fota == NULL) {
  517. LOGE(TAG, "fota param null.");
  518. return -EINVAL;
  519. }
  520. if (fota->cls && fota->cls->get_size) {
  521. return fota->cls->get_size(name);
  522. }
  523. return 0;
  524. }