evil_portal_scene_console_output.c 6.2 KB

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