evil_portal_scene_console_output.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. if (0 == strncmp("help", app->selected_tx_string, strlen("help"))) {
  33. const char *help_msg = "This project is a WIP.\ngithub.com/.\n\n";
  34. furi_string_cat_str(app->text_box_store, help_msg);
  35. app->text_box_store_strlen += strlen(help_msg);
  36. }
  37. if (0 == strncmp("savelogs", app->selected_tx_string, strlen("savelogs"))) {
  38. const char *help_msg = "Logs saved.\n\n";
  39. furi_string_cat_str(app->text_box_store, help_msg);
  40. app->text_box_store_strlen += strlen(help_msg);
  41. write_logs(app->portal_logs);
  42. }
  43. if (0 == strncmp("sethtml", app->selected_tx_string, strlen("sethtml"))) {
  44. // create a command queue
  45. // add sethtml
  46. // add setap
  47. // add start
  48. // if (!app->sent_html) {
  49. // const char *help_msg = "Set HTML before\nstarting portal.\n\n";
  50. // furi_string_cat_str(app->text_box_store, help_msg);
  51. // app->text_box_store_strlen += strlen(help_msg);
  52. // } else if (!app->sent_ap) {
  53. // const char *help_msg = "Set ap name before\nstarting portal.\n\n";
  54. // furi_string_cat_str(app->text_box_store, help_msg);
  55. // app->text_box_store_strlen += strlen(help_msg);
  56. // }
  57. // app->command_queue[0] = "sethtml";
  58. app->command_queue[0] = "setap";
  59. // app->command_queue[1] = "start";
  60. app->has_command_queue = true;
  61. app->command_index = 0;
  62. }
  63. if (app->show_stopscan_tip) {
  64. const char *help_msg = "Press BACK to return\n";
  65. furi_string_cat_str(app->text_box_store, help_msg);
  66. app->text_box_store_strlen += strlen(help_msg);
  67. }
  68. }
  69. // Set starting text - for "View Log", this will just be what was already in
  70. // the text box store
  71. text_box_set_text(app->text_box, furi_string_get_cstr(app->text_box_store));
  72. scene_manager_set_scene_state(app->scene_manager,
  73. Evil_PortalSceneConsoleOutput, 0);
  74. view_dispatcher_switch_to_view(app->view_dispatcher,
  75. Evil_PortalAppViewConsoleOutput);
  76. // Register callback to receive data
  77. evil_portal_uart_set_handle_rx_data_cb(
  78. app->uart,
  79. evil_portal_console_output_handle_rx_data_cb); // setup callback for rx
  80. // thread
  81. // Send command with CR+LF or newline '\n'
  82. // it is sent here
  83. if (app->is_command && app->selected_tx_string) {
  84. if (app->TERMINAL_MODE == 1) {
  85. evil_portal_uart_tx((uint8_t *)(app->selected_tx_string),
  86. strlen(app->selected_tx_string));
  87. evil_portal_uart_tx((uint8_t *)("\r\n"), 2);
  88. } else {
  89. // handle special commands here
  90. if (0 == strncmp("sethtml", app->selected_tx_string, strlen("sethtml"))) {
  91. evil_portal_read_index_html(context);
  92. char *data = malloc(
  93. (size_t)(strlen((char *)app->index_html) + strlen("sethtml=")));
  94. strcat(data, "sethtml=");
  95. strcat(data, (char *)app->index_html);
  96. evil_portal_uart_tx((uint8_t *)(data), strlen(data));
  97. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  98. app->sent_html = true;
  99. free(data);
  100. free(app->index_html);
  101. evil_portal_read_ap_name(context);
  102. } else if (0 ==
  103. strncmp("setap", app->selected_tx_string, strlen("setap"))) {
  104. evil_portal_read_ap_name(context);
  105. char *data =
  106. malloc((size_t)(strlen((char *)app->ap_name) + strlen("setap=")));
  107. strcat(data, "setap=");
  108. strcat(data, (char *)app->ap_name);
  109. evil_portal_uart_tx((uint8_t *)(data), strlen(data));
  110. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  111. app->sent_ap = true;
  112. free(data);
  113. free(app->ap_name);
  114. } else if (0 ==
  115. strncmp("reset", app->selected_tx_string, strlen("reset"))) {
  116. app->sent_html = false;
  117. app->sent_ap = false;
  118. evil_portal_uart_tx((uint8_t *)(app->selected_tx_string),
  119. strlen(app->selected_tx_string));
  120. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  121. } else if (0 ==
  122. strncmp("help", app->selected_tx_string, strlen("help"))) {
  123. // do nothing?
  124. } else {
  125. evil_portal_uart_tx((uint8_t *)(app->selected_tx_string),
  126. strlen(app->selected_tx_string));
  127. evil_portal_uart_tx((uint8_t *)("\n"), 1);
  128. }
  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. }