main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) 2018-2021 Alibaba Group Holding Limited
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <pthread.h>
  8. #include <signal.h>
  9. #include <unistd.h>
  10. #include <errno.h>
  11. #include <getopt.h>
  12. #include <aos/kv.h>
  13. #include <aos/nvram.h>
  14. #include <aos/version.h>
  15. #include <ulog/ulog.h>
  16. #include <fota.h>
  17. #include <fota_debug.h>
  18. #include <fota_server.h>
  19. #include "imagef.h"
  20. #define TAG "main"
  21. extern int daemonize;
  22. fota_server_t *fota_init(void)
  23. {
  24. fota_server_t *fota;
  25. fota = malloc(sizeof(fota_server_t));
  26. if (!fota) {
  27. return NULL;
  28. }
  29. memset(fota, 0, sizeof(fota_server_t));
  30. fota_dbus_init(fota);
  31. return fota;
  32. }
  33. void fota_deinit(fota_server_t *fota)
  34. {
  35. fota_dbus_deinit(fota);
  36. free(fota);
  37. }
  38. static const char short_options[] = "hvd";
  39. static const struct option long_options[] = {
  40. {"help", 0, 0, 'h'},
  41. {"version", 0, 0, 'v'},
  42. {"daemon", 0, 0, 'd'},
  43. {0, 0, 0, 0}
  44. };
  45. static void usage(char *cmd)
  46. {
  47. printf(
  48. "Usage: %s [OPTION]\n"
  49. "\n"
  50. "-h, --help help\n"
  51. "-v, --version print current version\n"
  52. "-d, --daemon run daemon in the background\n",
  53. cmd
  54. );
  55. }
  56. static void version(char *cmd)
  57. {
  58. printf("%s: version " "0.0.1" " by T-Head\n", cmd);
  59. }
  60. static int run_daemon(void)
  61. {
  62. #ifdef __linux__
  63. if (daemon(0, 0)) {
  64. perror("daemon");
  65. return -1;
  66. }
  67. #endif
  68. return 0;
  69. }
  70. extern int fota_register_cop2(void);
  71. extern int netio_register_httpc(const char *cert);
  72. extern int netio_register_flash2(void);
  73. extern int check_rootfs_partition(void);
  74. extern int set_fota_success_param(void);
  75. extern int set_fota_update_version_ok(void);
  76. const fota_register_ops_t register_ops = {
  77. .cert = NULL,
  78. .fota_register_cloud = fota_register_cop2,
  79. .netio_register_from = netio_register_httpc,
  80. .netio_register_to = netio_register_flash2
  81. };
  82. int main(int argc, char *argv[])
  83. {
  84. int c;
  85. int option_index;
  86. while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) {
  87. switch (c) {
  88. case 'h':
  89. usage(argv[0]);
  90. return 0;
  91. case 'v':
  92. version(argv[0]);
  93. return 0;
  94. case 'd':
  95. if (run_daemon()) {
  96. daemonize = 0;
  97. exit(1);
  98. } else {
  99. daemonize = 1;
  100. }
  101. break;
  102. default:
  103. printf("Try `%s --help' for more information.\n", argv[0]);
  104. return 1;
  105. }
  106. }
  107. printf("Service Init!\n");
  108. aos_kv_init(KV_PATH_DEFULT);
  109. nvram_init(NV_PATH_DEFUALT);
  110. int ret = set_fota_success_param();
  111. if (ret == 2) {
  112. set_fota_update_version_ok();
  113. } else if (ret == 1) {
  114. LOGD(TAG, "FOTA boot failed, rollback already...");
  115. }
  116. if (check_rootfs_partition() == 1) {
  117. LOGD(TAG, "############current_AB: A");
  118. } else {
  119. LOGD(TAG, "############current_AB: B");
  120. }
  121. LOGD(TAG, "############current_version: %s", aos_get_app_version());
  122. LOGD(TAG, "############current_changelog: %s", aos_get_changelog());
  123. #ifdef UBI_NOT_SUPPORT_INTERRUPTED_UPDATE
  124. if (FILE_SYSTEM_IS_UBI()) {
  125. // because of UBI volume features, see ubi-user.h in linux kernel
  126. aos_kv_setint("fota_offset", 0);
  127. }
  128. #endif
  129. fota_server_t *fota = fota_init();
  130. if (fota)
  131. fota->fotax.register_ops = (fota_register_ops_t *)&register_ops;
  132. int auto_check_en;
  133. if (aos_kv_getint("fota_autock", &auto_check_en) < 0) {
  134. auto_check_en = 0;
  135. }
  136. LOGD(TAG, "fota_autock: %d", auto_check_en);
  137. if (auto_check_en > 0) {
  138. extern void fotax_event(fotax_t *fotax, fotax_event_e event, const char *json);
  139. if (fotax_start(&fota->fotax) == 0) {
  140. LOGD(TAG, "fota auto start ok.");
  141. fota->fotax.fotax_event_cb = fotax_event;
  142. fota->fotax.private = fota;
  143. }
  144. }
  145. fota_loop_run(fota);
  146. return 0;
  147. }