evil_portal_scene_console_output.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  56. app->command_queue[0] = SET_AP_CMD;
  57. app->has_command_queue = true;
  58. app->command_index = 0;
  59. if(app->show_stopscan_tip) {
  60. const char* msg = "Starting portal\nIf no response press\nBACK to return\n";
  61. furi_string_cat_str(app->text_box_store, msg);
  62. app->text_box_store_strlen += strlen(msg);
  63. }
  64. }
  65. if(0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  66. app->sent_reset = true;
  67. if(app->show_stopscan_tip) {
  68. const char* msg = "Reseting portal\nPress BACK to return\n\n\n\n";
  69. furi_string_cat_str(app->text_box_store, msg);
  70. app->text_box_store_strlen += strlen(msg);
  71. }
  72. }
  73. }
  74. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  75. scene_manager_set_scene_state(app->scene_manager, Evil_PortalSceneConsoleOutput, 0);
  76. view_dispatcher_switch_to_view(app->view_dispatcher, Evil_PortalAppViewConsoleOutput);
  77. // Register callback to receive data
  78. evil_portal_uart_set_handle_rx_data_cb(
  79. app->uart, evil_portal_console_output_handle_rx_data_cb);
  80. if(app->is_command && app->selected_tx_string) {
  81. if(0 == strncmp(SET_HTML_CMD, app->selected_tx_string, strlen(SET_HTML_CMD))) {
  82. evil_portal_read_index_html(context);
  83. FuriString* data = furi_string_alloc();
  84. furi_string_cat(data, "sethtml=");
  85. furi_string_cat(data, (char*)app->index_html);
  86. evil_portal_uart_tx(
  87. (uint8_t*)(furi_string_get_cstr(data)), strlen(furi_string_get_cstr(data)));
  88. evil_portal_uart_tx((uint8_t*)("\n"), 1);
  89. app->sent_html = true;
  90. free(data);
  91. free(app->index_html);
  92. evil_portal_read_ap_name(context);
  93. } else if(0 == strncmp(RESET_CMD, app->selected_tx_string, strlen(RESET_CMD))) {
  94. app->sent_html = false;
  95. app->sent_ap = false;
  96. evil_portal_uart_tx(
  97. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  98. evil_portal_uart_tx((uint8_t*)("\n"), 1);
  99. } else if(1 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  100. evil_portal_uart_tx(
  101. (uint8_t*)(app->selected_tx_string), strlen(app->selected_tx_string));
  102. evil_portal_uart_tx((uint8_t*)("\n"), 1);
  103. }
  104. }
  105. }
  106. bool evil_portal_scene_console_output_on_event(void* context, SceneManagerEvent event) {
  107. Evil_PortalApp* app = context;
  108. bool consumed = false;
  109. if(event.type == SceneManagerEventTypeCustom) {
  110. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  111. consumed = true;
  112. } else if(event.type == SceneManagerEventTypeTick) {
  113. consumed = true;
  114. }
  115. return consumed;
  116. }
  117. void evil_portal_scene_console_output_on_exit(void* context) {
  118. Evil_PortalApp* app = context;
  119. // Unregister rx callback
  120. evil_portal_uart_set_handle_rx_data_cb(app->uart, NULL);
  121. }