app.c 6.4 KB

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