evil_portal_uart.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "evil_portal_app_i.h"
  2. #include "evil_portal_uart.h"
  3. #include "helpers/evil_portal_storage.h"
  4. struct Evil_PortalUart {
  5. Evil_PortalApp* app;
  6. FuriThread* rx_thread;
  7. FuriStreamBuffer* rx_stream;
  8. bool pcap;
  9. uint8_t mark_test_buf[11];
  10. uint8_t mark_test_idx;
  11. uint8_t rx_buf[RX_BUF_SIZE + 1];
  12. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
  13. FuriHalSerialHandle* serial_handle;
  14. };
  15. typedef enum {
  16. WorkerEvtStop = (1 << 0),
  17. WorkerEvtRxDone = (1 << 1),
  18. } WorkerEvtFlags;
  19. void evil_portal_uart_set_handle_rx_data_cb(
  20. Evil_PortalUart* uart,
  21. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)) {
  22. furi_assert(uart);
  23. uart->handle_rx_data_cb = handle_rx_data_cb;
  24. }
  25. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
  26. void evil_portal_uart_on_irq_cb(
  27. FuriHalSerialHandle* handle,
  28. FuriHalSerialRxEvent event,
  29. void* context) {
  30. Evil_PortalUart* uart = (Evil_PortalUart*)context;
  31. if(event == FuriHalSerialRxEventData) {
  32. uint8_t data = furi_hal_serial_async_rx(handle);
  33. const char* mark_begin = "[BUF/BEGIN]";
  34. const char* mark_close = "[BUF/CLOSE]";
  35. if(uart->mark_test_idx != 0) {
  36. // We are trying to match a marker
  37. if(data == mark_begin[uart->mark_test_idx] ||
  38. data == mark_close[uart->mark_test_idx]) {
  39. // Received char matches next char in a marker, append to test buffer
  40. uart->mark_test_buf[uart->mark_test_idx++] = data;
  41. if(uart->mark_test_idx == sizeof(uart->mark_test_buf)) {
  42. // Test buffer reached max length, parse what marker this is and discard buffer
  43. if(!memcmp(
  44. uart->mark_test_buf, (void*)mark_begin, sizeof(uart->mark_test_buf))) {
  45. uart->pcap = true;
  46. } else if(!memcmp(
  47. uart->mark_test_buf,
  48. (void*)mark_close,
  49. sizeof(uart->mark_test_buf))) {
  50. uart->pcap = false;
  51. }
  52. uart->mark_test_idx = 0;
  53. }
  54. // Don't pass to stream
  55. return;
  56. } else {
  57. // Received char doesn't match any expected next char, send current test buffer
  58. if(!uart->pcap) {
  59. furi_stream_buffer_send(
  60. uart->rx_stream, uart->mark_test_buf, uart->mark_test_idx, 0);
  61. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
  62. }
  63. // Reset test buffer and try parsing this char from scratch
  64. uart->mark_test_idx = 0;
  65. }
  66. }
  67. // If we reach here the buffer is empty
  68. if(data == mark_begin[0]) {
  69. // Received marker start, append to test buffer
  70. uart->mark_test_buf[uart->mark_test_idx++] = data;
  71. } else {
  72. // Not a marker start and we aren't matching a marker, this is just data
  73. if(!uart->pcap) {
  74. // We want to ignore pcap data from marauder
  75. furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
  76. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
  77. }
  78. }
  79. }
  80. }
  81. static int32_t uart_worker(void* context) {
  82. Evil_PortalUart* uart = (void*)context;
  83. while(1) {
  84. uint32_t events =
  85. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  86. furi_check((events & FuriFlagError) == 0);
  87. if(events & WorkerEvtStop) break;
  88. if(events & WorkerEvtRxDone) {
  89. size_t len = furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0);
  90. if(len > 0) {
  91. if(uart->handle_rx_data_cb) {
  92. uart->handle_rx_data_cb(uart->rx_buf, len, uart->app);
  93. furi_mutex_acquire(uart->app->portal_logs_mutex, FuriWaitForever);
  94. if(uart->app->sent_reset == false) {
  95. furi_string_cat(uart->app->portal_logs, (char*)uart->rx_buf);
  96. }
  97. if(furi_string_size(uart->app->portal_logs) > 4000) {
  98. write_logs(uart->app->portal_logs);
  99. furi_string_reset(uart->app->portal_logs);
  100. }
  101. furi_mutex_release(uart->app->portal_logs_mutex);
  102. } else {
  103. uart->rx_buf[len] = '\0';
  104. furi_mutex_acquire(uart->app->portal_logs_mutex, FuriWaitForever);
  105. if(uart->app->sent_reset == false) {
  106. furi_string_cat(uart->app->portal_logs, (char*)uart->rx_buf);
  107. }
  108. if(furi_string_size(uart->app->portal_logs) > 4000) {
  109. write_logs(uart->app->portal_logs);
  110. furi_string_reset(uart->app->portal_logs);
  111. }
  112. furi_mutex_release(uart->app->portal_logs_mutex);
  113. }
  114. }
  115. }
  116. }
  117. furi_stream_buffer_free(uart->rx_stream);
  118. return 0;
  119. }
  120. void evil_portal_uart_tx(Evil_PortalUart* uart, uint8_t* data, size_t len) {
  121. furi_hal_serial_tx(uart->serial_handle, data, len);
  122. }
  123. Evil_PortalUart* evil_portal_uart_init(Evil_PortalApp* app) {
  124. Evil_PortalUart* uart = malloc(sizeof(Evil_PortalUart));
  125. uart->app = app;
  126. // Init all rx stream and thread early to avoid crashes
  127. uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  128. uart->rx_thread = furi_thread_alloc();
  129. furi_thread_set_name(uart->rx_thread, "Evil_PortalUartRxThread");
  130. furi_thread_set_stack_size(uart->rx_thread, 1024);
  131. furi_thread_set_context(uart->rx_thread, uart);
  132. furi_thread_set_callback(uart->rx_thread, uart_worker);
  133. furi_thread_start(uart->rx_thread);
  134. if(app->BAUDRATE == 0) {
  135. app->BAUDRATE = 115200;
  136. }
  137. uart->serial_handle = furi_hal_serial_control_acquire(UART_CH);
  138. furi_check(uart->serial_handle);
  139. furi_hal_serial_init(uart->serial_handle, app->BAUDRATE);
  140. furi_hal_serial_async_rx_start(uart->serial_handle, evil_portal_uart_on_irq_cb, uart, false);
  141. return uart;
  142. }
  143. void evil_portal_uart_free(Evil_PortalUart* uart) {
  144. furi_assert(uart);
  145. furi_hal_serial_async_rx_stop(uart->serial_handle);
  146. furi_hal_serial_deinit(uart->serial_handle);
  147. furi_hal_serial_control_release(uart->serial_handle);
  148. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop);
  149. furi_thread_join(uart->rx_thread);
  150. furi_thread_free(uart->rx_thread);
  151. free(uart);
  152. }