flipbip.c 5.2 KB

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