app.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include <alloc/alloc.h>
  2. // Entry point for the FlipWorld application
  3. int32_t flip_world_main(void *p)
  4. {
  5. // Suppress unused parameter warning
  6. UNUSED(p);
  7. // Initialize the FlipWorld application
  8. FlipWorldApp *app = flip_world_app_alloc();
  9. if (!app)
  10. {
  11. FURI_LOG_E(TAG, "Failed to allocate FlipWorldApp");
  12. return -1;
  13. }
  14. // check if board is connected (Derek Jamison)
  15. // initialize the http
  16. if (flipper_http_init(flipper_http_rx_callback, app))
  17. {
  18. if (!flipper_http_ping())
  19. {
  20. FURI_LOG_E(TAG, "Failed to ping the device");
  21. return -1;
  22. }
  23. // Try to wait for pong response.
  24. uint8_t counter = 10;
  25. while (fhttp.state == INACTIVE && --counter > 0)
  26. {
  27. FURI_LOG_D(TAG, "Waiting for PONG");
  28. furi_delay_ms(100);
  29. }
  30. if (counter == 0)
  31. easy_flipper_dialog("FlipperHTTP Error", "Ensure your WiFi Developer\nBoard or Pico W is connected\nand the latest FlipperHTTP\nfirmware is installed.");
  32. flipper_http_deinit();
  33. }
  34. else
  35. {
  36. easy_flipper_dialog("FlipperHTTP Error", "The UART is likely busy.\nEnsure you have the correct\nflash for your board then\nrestart your Flipper Zero.");
  37. }
  38. // this will be removed in version 0.3. we'll keep all our data in the data folder from now on
  39. // load app version
  40. char saved_app_version[16];
  41. if (load_char("app_version", saved_app_version, sizeof(saved_app_version)))
  42. {
  43. float saved_version = strtod(saved_app_version, NULL);
  44. if (saved_version == 0.1)
  45. {
  46. // transfer files over into the data folder (to bs used to load the player context)
  47. char directory_path[256];
  48. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  49. // Create the directory
  50. Storage *storage = furi_record_open(RECORD_STORAGE);
  51. storage_common_mkdir(storage, directory_path);
  52. // copy the whole folder
  53. char source_path[256];
  54. snprintf(source_path, sizeof(source_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  55. if (storage_common_migrate(storage, source_path, directory_path) != FSE_OK)
  56. {
  57. FURI_LOG_E(TAG, "Failed to migrate files");
  58. }
  59. else
  60. {
  61. void clean_up(char *file_path)
  62. {
  63. char updated_file_path[128];
  64. snprintf(updated_file_path, sizeof(updated_file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s", file_path);
  65. // check if the file exists
  66. if (storage_file_exists(storage, updated_file_path) &&
  67. storage_common_remove(storage, updated_file_path) != FSE_OK)
  68. {
  69. FURI_LOG_E(TAG, "Failed to delete %s", updated_file_path);
  70. }
  71. // check if the directory exists
  72. if (storage_dir_exists(storage, updated_file_path) &&
  73. storage_common_remove(storage, updated_file_path) != FSE_OK)
  74. {
  75. FURI_LOG_E(TAG, "Failed to delete %s", updated_file_path);
  76. }
  77. }
  78. // clean up
  79. clean_up("WiFi-SSID.txt");
  80. clean_up("WiFi-Password.txt");
  81. clean_up("Flip-Social-Username.txt");
  82. clean_up("Flip-Social-Password.txt");
  83. clean_up("Game-FPS.txt");
  84. clean_up("Game-Screen-Always-On.txt");
  85. clean_up("is_logged_in.txt");
  86. clean_up("data/worlds");
  87. clean_up("data/settings.bin");
  88. }
  89. }
  90. }
  91. else
  92. {
  93. // transfer files over into the data folder (to bs used to load the player context)
  94. char directory_path[256];
  95. snprintf(directory_path, sizeof(directory_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/data");
  96. // Create the directory
  97. Storage *storage = furi_record_open(RECORD_STORAGE);
  98. storage_common_mkdir(storage, directory_path);
  99. // copy the whole folder
  100. char source_path[256];
  101. snprintf(source_path, sizeof(source_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world");
  102. if (storage_common_migrate(storage, source_path, directory_path) != FSE_OK)
  103. {
  104. FURI_LOG_E(TAG, "Failed to migrate files");
  105. }
  106. else
  107. {
  108. void clean_up(char *file_path)
  109. {
  110. char updated_file_path[128];
  111. snprintf(updated_file_path, sizeof(updated_file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_world/%s", file_path);
  112. // check if the file exists
  113. if (storage_file_exists(storage, updated_file_path) &&
  114. storage_common_remove(storage, updated_file_path) != FSE_OK)
  115. {
  116. FURI_LOG_E(TAG, "Failed to delete %s", updated_file_path);
  117. }
  118. // check if the directory exists
  119. if (storage_dir_exists(storage, updated_file_path) &&
  120. storage_common_remove(storage, updated_file_path) != FSE_OK)
  121. {
  122. FURI_LOG_E(TAG, "Failed to delete %s", updated_file_path);
  123. }
  124. }
  125. // clean up
  126. clean_up("WiFi-SSID.txt");
  127. clean_up("WiFi-Password.txt");
  128. clean_up("Flip-Social-Username.txt");
  129. clean_up("Flip-Social-Password.txt");
  130. clean_up("Game-FPS.txt");
  131. clean_up("Game-Screen-Always-On.txt");
  132. clean_up("is_logged_in.txt");
  133. clean_up("data/worlds");
  134. clean_up("data/settings.bin");
  135. }
  136. }
  137. // svae app version
  138. char app_version[16];
  139. snprintf(app_version, sizeof(app_version), "%f", (double)VERSION);
  140. save_char("app_version", app_version);
  141. // Run the view dispatcher
  142. view_dispatcher_run(app->view_dispatcher);
  143. // Free the resources used by the FlipWorld application
  144. flip_world_app_free(app);
  145. // Return 0 to indicate success
  146. return 0;
  147. }