wifi_marauder_uart.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "wifi_marauder_app_i.h"
  2. #include "wifi_marauder_uart.h"
  3. #include <xtreme/xtreme.h>
  4. #define UART_CH \
  5. (xtreme_settings.uart_esp_channel == UARTDefault ? FuriHalUartIdUSART1 : FuriHalUartIdLPUART1)
  6. #define BAUDRATE (115200)
  7. struct WifiMarauderUart {
  8. WifiMarauderApp* app;
  9. FuriHalUartId channel;
  10. FuriThread* rx_thread;
  11. FuriStreamBuffer* rx_stream;
  12. FuriStreamBuffer* pcap_stream;
  13. bool pcap;
  14. uint8_t mark_test_buf[11];
  15. uint8_t mark_test_idx;
  16. uint8_t rx_buf[RX_BUF_SIZE + 1];
  17. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
  18. void (*handle_rx_pcap_cb)(uint8_t* buf, size_t len, void* context);
  19. };
  20. typedef enum {
  21. WorkerEvtStop = (1 << 0),
  22. WorkerEvtRxDone = (1 << 1),
  23. WorkerEvtPcapDone = (1 << 2),
  24. } WorkerEvtFlags;
  25. void wifi_marauder_uart_set_handle_rx_data_cb(
  26. WifiMarauderUart* uart,
  27. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)) {
  28. furi_assert(uart);
  29. uart->handle_rx_data_cb = handle_rx_data_cb;
  30. }
  31. void wifi_marauder_uart_set_handle_rx_pcap_cb(
  32. WifiMarauderUart* uart,
  33. void (*handle_rx_pcap_cb)(uint8_t* buf, size_t len, void* context)) {
  34. furi_assert(uart);
  35. uart->handle_rx_pcap_cb = handle_rx_pcap_cb;
  36. }
  37. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone | WorkerEvtPcapDone)
  38. void wifi_marauder_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  39. WifiMarauderUart* uart = (WifiMarauderUart*)context;
  40. if(ev == UartIrqEventRXNE) {
  41. const char* mark_begin = "[BUF/BEGIN]";
  42. const char* mark_close = "[BUF/CLOSE]";
  43. if(uart->mark_test_idx != 0) {
  44. // We are trying to match a marker
  45. if(data == mark_begin[uart->mark_test_idx] ||
  46. data == mark_close[uart->mark_test_idx]) {
  47. // Received char matches next char in a marker, append to test buffer
  48. uart->mark_test_buf[uart->mark_test_idx++] = data;
  49. if(uart->mark_test_idx == sizeof(uart->mark_test_buf)) {
  50. // Test buffer reached max length, parse what marker this is and discard buffer
  51. if(!memcmp(
  52. uart->mark_test_buf, (void*)mark_begin, sizeof(uart->mark_test_buf))) {
  53. uart->pcap = true;
  54. } else if(!memcmp(
  55. uart->mark_test_buf,
  56. (void*)mark_close,
  57. sizeof(uart->mark_test_buf))) {
  58. uart->pcap = false;
  59. }
  60. uart->mark_test_idx = 0;
  61. }
  62. // Don't pass to stream
  63. return;
  64. } else {
  65. // Received char doesn't match any expected next char, send current test buffer
  66. if(uart->pcap) {
  67. furi_stream_buffer_send(
  68. uart->pcap_stream, uart->mark_test_buf, uart->mark_test_idx, 0);
  69. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtPcapDone);
  70. } else {
  71. furi_stream_buffer_send(
  72. uart->rx_stream, uart->mark_test_buf, uart->mark_test_idx, 0);
  73. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
  74. }
  75. // Reset test buffer and try parsing this char from scratch
  76. uart->mark_test_idx = 0;
  77. }
  78. }
  79. // If we reach here the buffer is empty
  80. if(data == mark_begin[0]) {
  81. // Received marker start, append to test buffer
  82. uart->mark_test_buf[uart->mark_test_idx++] = data;
  83. } else {
  84. // Not a marker start and we aren't matching a marker, this is just data
  85. if(uart->pcap) {
  86. furi_stream_buffer_send(uart->pcap_stream, &data, 1, 0);
  87. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtPcapDone);
  88. } else {
  89. furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
  90. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
  91. }
  92. }
  93. }
  94. }
  95. static int32_t uart_worker(void* context) {
  96. WifiMarauderUart* uart = (void*)context;
  97. while(1) {
  98. uint32_t events =
  99. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  100. furi_check((events & FuriFlagError) == 0);
  101. if(events & WorkerEvtStop) break;
  102. if(events & WorkerEvtRxDone) {
  103. size_t len = furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0);
  104. if(len > 0) {
  105. if(uart->handle_rx_data_cb) uart->handle_rx_data_cb(uart->rx_buf, len, uart->app);
  106. }
  107. }
  108. if(events & WorkerEvtPcapDone) {
  109. size_t len =
  110. furi_stream_buffer_receive(uart->pcap_stream, uart->rx_buf, RX_BUF_SIZE, 0);
  111. if(len > 0) {
  112. if(uart->handle_rx_pcap_cb) uart->handle_rx_pcap_cb(uart->rx_buf, len, uart->app);
  113. }
  114. }
  115. }
  116. furi_stream_buffer_free(uart->rx_stream);
  117. furi_stream_buffer_free(uart->pcap_stream);
  118. return 0;
  119. }
  120. void wifi_marauder_uart_tx(uint8_t* data, size_t len) {
  121. furi_hal_uart_tx(UART_CH, data, len);
  122. }
  123. WifiMarauderUart*
  124. wifi_marauder_uart_init(WifiMarauderApp* app, FuriHalUartId channel, const char* thread_name) {
  125. WifiMarauderUart* uart = malloc(sizeof(WifiMarauderUart));
  126. uart->app = app;
  127. uart->channel = channel;
  128. uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  129. uart->pcap_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  130. uart->rx_thread = furi_thread_alloc();
  131. furi_thread_set_name(uart->rx_thread, thread_name);
  132. furi_thread_set_stack_size(uart->rx_thread, 1024);
  133. furi_thread_set_context(uart->rx_thread, uart);
  134. furi_thread_set_callback(uart->rx_thread, uart_worker);
  135. furi_thread_start(uart->rx_thread);
  136. if(channel == FuriHalUartIdUSART1) {
  137. furi_hal_console_disable();
  138. } else if(channel == FuriHalUartIdLPUART1) {
  139. furi_hal_uart_init(channel, BAUDRATE);
  140. }
  141. furi_hal_uart_set_br(channel, BAUDRATE);
  142. furi_hal_uart_set_irq_cb(channel, wifi_marauder_uart_on_irq_cb, uart);
  143. return uart;
  144. }
  145. WifiMarauderUart* wifi_marauder_usart_init(WifiMarauderApp* app) {
  146. return wifi_marauder_uart_init(app, UART_CH, "WifiMarauderUartRxThread");
  147. }
  148. void wifi_marauder_uart_free(WifiMarauderUart* uart) {
  149. furi_assert(uart);
  150. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop);
  151. furi_thread_join(uart->rx_thread);
  152. furi_thread_free(uart->rx_thread);
  153. furi_hal_uart_set_irq_cb(uart->channel, NULL, NULL);
  154. if(uart->channel == FuriHalUartIdLPUART1) {
  155. furi_hal_uart_deinit(uart->channel);
  156. } else {
  157. furi_hal_console_enable();
  158. }
  159. free(uart);
  160. }