evil_portal_scene_console_output.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. TextBox *text_box = app->text_box;
  22. text_box_reset(app->text_box);
  23. text_box_set_font(text_box, TextBoxFontText);
  24. if (app->focus_console_start) {
  25. text_box_set_focus(text_box, TextBoxFocusStart);
  26. } else {
  27. text_box_set_focus(text_box, TextBoxFocusEnd);
  28. }
  29. if (app->is_command) {
  30. furi_string_reset(app->text_box_store);
  31. app->text_box_store_strlen = 0;
  32. app->sent_reset = false;
  33. if (0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  34. const char *help_msg =
  35. "BLUE = Waiting\nGREEN = Good\nRED = Bad\n\nThis project is a "
  36. "WIP.\ngithub.com/bigbrodude6119/flipper-zero-evil-portal\n\n"
  37. "Version 0.0.2\n\n";
  38. furi_string_cat_str(app->text_box_store, help_msg);
  39. app->text_box_store_strlen += strlen(help_msg);
  40. if (app->show_stopscan_tip) {
  41. const char *msg = "Press BACK to return\n";
  42. furi_string_cat_str(app->text_box_store, msg);
  43. app->text_box_store_strlen += strlen(msg);
  44. }
  45. }
  46. if (0 == strncmp("savelogs", app->selected_tx_string, strlen("savelogs"))) {
  47. const char *help_msg = "Logs saved.\n\n";
  48. furi_string_cat_str(app->text_box_store, help_msg);
  49. app->text_box_store_strlen += strlen(help_msg);
  50. write_logs(app->portal_logs);
  51. furi_string_reset(app->portal_logs);
  52. if (app->show_stopscan_tip) {
  53. const char *msg = "Press BACK to return\n";
  54. furi_string_cat_str(app->text_box_store, msg);
  55. app->text_box_store_strlen += strlen(msg);
  56. }
  57. }
  58. if (0 == strncmp("setapname", app->selected_tx_string, strlen("setapname"))) {
  59. scene_manager_next_scene(app->scene_manager,
  60. Evil_PortalSceneRename);
  61. return;
  62. }
  63. if (0 == strncmp("selecthtml", app->selected_tx_string, strlen("selecthtml"))) {
  64. scene_manager_next_scene(app->scene_manager,
  65. Evil_PortalSceneSelectHtml);
  66. return;
  67. }
  68. if (0 ==
  69. strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  70. app->command_queue[0] = SET_AP_CMD;
  71. app->has_command_queue = true;
  72. app->command_index = 0;
  73. if (app->show_stopscan_tip) {
  74. const char *msg =
  75. "Starting portal\nIf no response press\nBACK to return\n";
  76. furi_string_cat_str(app->text_box_store, msg);
  77. app->text_box_store_strlen += strlen(msg);
  78. }
  79. }
  80. if (0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  81. app->sent_reset = true;
  82. if (app->show_stopscan_tip) {
  83. const char *msg = "Reseting portal\nPress BACK to return\n\n\n\n";
  84. furi_string_cat_str(app->text_box_store, msg);
  85. app->text_box_store_strlen += strlen(msg);
  86. }
  87. }
  88. }
  89. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  90. scene_manager_set_scene_state(app->scene_manager,
  91. Evil_PortalSceneConsoleOutput, 0);
  92. view_dispatcher_switch_to_view(app->view_dispatcher,
  93. Evil_PortalAppViewConsoleOutput);
  94. // Register callback to receive data
  95. evil_portal_uart_set_handle_rx_data_cb(
  96. app->uart, evil_portal_console_output_handle_rx_data_cb);
  97. if (app->is_command && app->selected_tx_string) {
  98. if (0 ==
  99. strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  100. evil_portal_read_index_html(context);
  101. FuriString *data = furi_string_alloc();
  102. furi_string_cat(data, "sethtml=");
  103. furi_string_cat(data, (char *)app->index_html);
  104. evil_portal_uart_tx((uint8_t *)(furi_string_get_cstr(data)),
  105. strlen(furi_string_get_cstr(data)));
  106. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  107. app->sent_html = true;
  108. free(data);
  109. free(app->index_html);
  110. evil_portal_read_ap_name(context);
  111. } else if (0 ==
  112. strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  113. app->sent_html = false;
  114. app->sent_ap = false;
  115. evil_portal_uart_tx((uint8_t *)(app->selected_tx_string),
  116. strlen(app->selected_tx_string));
  117. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  118. } else if (1 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  119. evil_portal_uart_tx((uint8_t *)(app->selected_tx_string),
  120. strlen(app->selected_tx_string));
  121. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  122. }
  123. }
  124. }
  125. bool evil_portal_scene_console_output_on_event(void *context,
  126. SceneManagerEvent event) {
  127. Evil_PortalApp *app = context;
  128. bool consumed = false;
  129. if (event.type == SceneManagerEventTypeCustom) {
  130. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  131. consumed = true;
  132. } else if (event.type == SceneManagerEventTypeTick) {
  133. consumed = true;
  134. }
  135. return consumed;
  136. }
  137. void evil_portal_scene_console_output_on_exit(void *context) {
  138. Evil_PortalApp *app = context;
  139. // Unregister rx callback
  140. evil_portal_uart_set_handle_rx_data_cb(app->uart, NULL);
  141. }