app.c 6.3 KB

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