flipbip.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "flipbip.h"
  2. #include "crypto/memzero.h"
  3. bool flipbip_custom_event_callback(void* context, uint32_t event) {
  4. furi_assert(context);
  5. FlipBip* app = context;
  6. return scene_manager_handle_custom_event(app->scene_manager, event);
  7. }
  8. void flipbip_tick_event_callback(void* context) {
  9. furi_assert(context);
  10. FlipBip* app = context;
  11. scene_manager_handle_tick_event(app->scene_manager);
  12. }
  13. //leave app if back button pressed
  14. bool flipbip_navigation_event_callback(void* context) {
  15. furi_assert(context);
  16. FlipBip* app = context;
  17. return scene_manager_handle_back_event(app->scene_manager);
  18. }
  19. static void text_input_callback(void* context) {
  20. furi_assert(context);
  21. FlipBip* app = context;
  22. if(app->passphrase == FlipBipPassphraseOn && strlen(app->input) > 0) {
  23. strcpy(app->passphrase_text, app->input);
  24. } else {
  25. memzero(app->passphrase_text, TEXT_BUFFER_SIZE);
  26. }
  27. memzero(app->input, TEXT_BUFFER_SIZE);
  28. view_dispatcher_switch_to_view(app->view_dispatcher, FlipBipViewIdSettings);
  29. }
  30. FlipBip* flipbip_app_alloc() {
  31. FlipBip* app = malloc(sizeof(FlipBip));
  32. app->gui = furi_record_open(RECORD_GUI);
  33. app->notification = furi_record_open(RECORD_NOTIFICATION);
  34. //Turn backlight on, believe me this makes testing your app easier
  35. notification_message(app->notification, &sequence_display_backlight_on);
  36. //Scene additions
  37. app->view_dispatcher = view_dispatcher_alloc();
  38. view_dispatcher_enable_queue(app->view_dispatcher);
  39. app->scene_manager = scene_manager_alloc(&flipbip_scene_handlers, app);
  40. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  41. view_dispatcher_set_navigation_event_callback(
  42. app->view_dispatcher, flipbip_navigation_event_callback);
  43. view_dispatcher_set_tick_event_callback(
  44. app->view_dispatcher, flipbip_tick_event_callback, 100);
  45. view_dispatcher_set_custom_event_callback(app->view_dispatcher, flipbip_custom_event_callback);
  46. app->submenu = submenu_alloc();
  47. // Settings
  48. app->haptic = FlipBipHapticOn;
  49. app->led = FlipBipLedOn;
  50. app->passphrase = FlipBipPassphraseOff;
  51. app->bip39_strength = FlipBipStrength256; // 256 bits (24 words)
  52. app->bip44_coin = FlipBipCoinBTC0; // 0 (BTC)
  53. app->overwrite_saved_seed = 0;
  54. view_dispatcher_add_view(
  55. app->view_dispatcher, FlipBipViewIdMenu, submenu_get_view(app->submenu));
  56. app->flipbip_startscreen = flipbip_startscreen_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher,
  59. FlipBipViewIdStartscreen,
  60. flipbip_startscreen_get_view(app->flipbip_startscreen));
  61. app->flipbip_scene_1 = flipbip_scene_1_alloc();
  62. view_dispatcher_add_view(
  63. app->view_dispatcher, FlipBipViewIdScene1, flipbip_scene_1_get_view(app->flipbip_scene_1));
  64. app->variable_item_list = variable_item_list_alloc();
  65. view_dispatcher_add_view(
  66. app->view_dispatcher,
  67. FlipBipViewIdSettings,
  68. variable_item_list_get_view(app->variable_item_list));
  69. app->text_input = text_input_alloc();
  70. text_input_set_result_callback(
  71. app->text_input,
  72. text_input_callback,
  73. (void*)app,
  74. app->input,
  75. TEXT_BUFFER_SIZE,
  76. //clear default text
  77. true);
  78. text_input_set_header_text(app->text_input, "Input");
  79. view_dispatcher_add_view(
  80. app->view_dispatcher, FlipBipViewIdTextInput, text_input_get_view(app->text_input));
  81. //End Scene Additions
  82. return app;
  83. }
  84. void flipbip_app_free(FlipBip* app) {
  85. furi_assert(app);
  86. // Scene manager
  87. scene_manager_free(app->scene_manager);
  88. text_input_free(app->text_input);
  89. // View Dispatcher
  90. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdMenu);
  91. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdScene1);
  92. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdSettings);
  93. view_dispatcher_remove_view(app->view_dispatcher, FlipBipViewIdTextInput);
  94. submenu_free(app->submenu);
  95. view_dispatcher_free(app->view_dispatcher);
  96. furi_record_close(RECORD_GUI);
  97. app->gui = NULL;
  98. app->notification = NULL;
  99. //Remove whatever is left
  100. memzero(app, sizeof(FlipBip));
  101. free(app);
  102. }
  103. int32_t flipbip_app(void* p) {
  104. UNUSED(p);
  105. FlipBip* app = flipbip_app_alloc();
  106. // Disabled because causes exit on customer firmwares such as RM
  107. /*if(!furi_hal_region_is_provisioned()) {
  108. flipbip_app_free(app);
  109. return 1;
  110. }*/
  111. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  112. scene_manager_next_scene(
  113. app->scene_manager, FlipBipSceneStartscreen); //Start with start screen
  114. //scene_manager_next_scene(app->scene_manager, FlipBipSceneMenu); //if you want to directly start with Menu
  115. furi_hal_power_suppress_charge_enter();
  116. view_dispatcher_run(app->view_dispatcher);
  117. furi_hal_power_suppress_charge_exit();
  118. flipbip_app_free(app);
  119. return 0;
  120. }