evil_portal_scene_console_output.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "../evil_portal_app_i.h"
  2. #include "../helpers/evil_portal_storage.h"
  3. void evil_portal_console_output_handle_rx_data_cb(uint8_t* buf, size_t len, void* context) {
  4. furi_assert(context);
  5. Evil_PortalApp* app = context;
  6. // If text box store gets too big, then truncate it
  7. app->text_box_store_strlen += len;
  8. if(app->text_box_store_strlen >= EVIL_PORTAL_TEXT_BOX_STORE_SIZE - 1) {
  9. furi_string_right(app->text_box_store, app->text_box_store_strlen / 2);
  10. app->text_box_store_strlen = furi_string_size(app->text_box_store) + len;
  11. }
  12. // Null-terminate buf and append to text box store
  13. buf[len] = '\0';
  14. furi_string_cat_printf(app->text_box_store, "%s", buf);
  15. view_dispatcher_send_custom_event(app->view_dispatcher, Evil_PortalEventRefreshConsoleOutput);
  16. }
  17. void evil_portal_scene_console_output_on_enter(void* context) {
  18. Evil_PortalApp* app = context;
  19. TextBox* text_box = app->text_box;
  20. text_box_reset(app->text_box);
  21. text_box_set_font(text_box, TextBoxFontText);
  22. if(app->focus_console_start) {
  23. text_box_set_focus(text_box, TextBoxFocusStart);
  24. } else {
  25. text_box_set_focus(text_box, TextBoxFocusEnd);
  26. }
  27. if(app->is_command) {
  28. furi_string_reset(app->text_box_store);
  29. app->text_box_store_strlen = 0;
  30. app->sent_reset = false;
  31. if(0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  32. const char* help_msg = "BLUE = Waiting\nGREEN = Good\nRED = Bad\n\nThis project is a "
  33. "WIP.\ngithub.com/bigbrodude6119/flipper-zero-evil-portal\n\n"
  34. "Version 0.0.2\n\n";
  35. furi_string_cat_str(app->text_box_store, help_msg);
  36. app->text_box_store_strlen += strlen(help_msg);
  37. if(app->show_stopscan_tip) {
  38. const char* msg = "Press BACK to return\n";
  39. furi_string_cat_str(app->text_box_store, msg);
  40. app->text_box_store_strlen += strlen(msg);
  41. }
  42. }
  43. if(0 == strncmp("savelogs", app->selected_tx_string, strlen("savelogs"))) {
  44. const char* help_msg = "Logs saved.\n\n";
  45. furi_string_cat_str(app->text_box_store, help_msg);
  46. app->text_box_store_strlen += strlen(help_msg);
  47. write_logs(app->portal_logs);
  48. furi_string_reset(app->portal_logs);
  49. if(app->show_stopscan_tip) {
  50. const char* msg = "Press BACK to return\n";
  51. furi_string_cat_str(app->text_box_store, msg);
  52. app->text_box_store_strlen += strlen(msg);
  53. }
  54. }
  55. if(0 == strncmp("setapname", app->selected_tx_string, strlen("setapname"))) {
  56. scene_manager_next_scene(app->scene_manager, Evil_PortalSceneRename);
  57. return;
  58. }
  59. if(0 == strncmp("selecthtml", app->selected_tx_string, strlen("selecthtml"))) {
  60. scene_manager_next_scene(app->scene_manager, Evil_PortalSceneSelectHtml);
  61. return;
  62. }
  63. if(0 == strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  64. app->command_queue[0] = SET_AP_CMD;
  65. app->has_command_queue = true;
  66. app->command_index = 0;
  67. if(app->show_stopscan_tip) {
  68. const char* msg = "Starting portal\nIf no response press\nBACK to return\n";
  69. furi_string_cat_str(app->text_box_store, msg);
  70. app->text_box_store_strlen += strlen(msg);
  71. }
  72. }
  73. if(0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  74. app->sent_reset = true;
  75. if(app->show_stopscan_tip) {
  76. const char* msg = "Reseting portal\nPress BACK to return\n\n\n\n";
  77. furi_string_cat_str(app->text_box_store, msg);
  78. app->text_box_store_strlen += strlen(msg);
  79. }
  80. }
  81. }
  82. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  83. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneConsoleOutput, 0);
  84. view_dispatcher_switch_to_view(app->view_dispatcher, Evil_PortalAppViewConsoleOutput);
  85. // Register callback to receive data
  86. evil_portal_uart_set_handle_rx_data_cb(
  87. app->uart, evil_portal_console_output_handle_rx_data_cb);
  88. if(app->is_command && app->selected_tx_string) {
  89. if(0 == strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  90. evil_portal_read_index_html(context);
  91. FuriString* data = furi_string_alloc();
  92. furi_string_cat(data, "sethtml=");
  93. furi_string_cat(data, (char*)app->index_html);
  94. evil_portal_uart_tx(
  95. (uint8_t*)(furi_string_get_cstr(data)), strlen(furi_string_get_cstr(data)));
  96. evil_portal_uart_tx((uint8_t*)("\n"), 1);
  97. app->sent_html = true;
  98. free(data);
  99. free(app->index_html);
  100. evil_portal_read_ap_name(context);
  101. } else if(0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  102. app->sent_html = false;
  103. app->sent_ap = false;
  104. evil_portal_uart_tx(
  105. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  106. evil_portal_uart_tx((uint8_t*)("\n"), 1);
  107. } else if(1 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  108. evil_portal_uart_tx(
  109. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  110. evil_portal_uart_tx((uint8_t*)("\n"), 1);
  111. }
  112. }
  113. }
  114. bool evil_portal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  115. Evil_PortalApp* app = context;
  116. bool consumed = false;
  117. if(event.type == SceneManagerEventTypeCustom) {
  118. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  119. consumed = true;
  120. } else if(event.type == SceneManagerEventTypeTick) {
  121. consumed = true;
  122. }
  123. return consumed;
  124. }
  125. void evil_portal_scene_console_output_on_exit(void* context) {
  126. Evil_PortalApp* app = context;
  127. // Unregister rx callback
  128. evil_portal_uart_set_handle_rx_data_cb(app->uart, NULL);
  129. }