wifi_deauther_uart.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #include "wifi_deauther_app_i.h"
  2. #include "wifi_deauther_uart.h"
  3. #include <stream_buffer.h>
  4. #define UART_CH (FuriHalUartIdUSART1)
  5. #define BAUDRATE (115200)
  6. struct WifideautherUart {
  7. WifideautherApp* app;
  8. FuriHalUartId channel;
  9. FuriThread* rx_thread;
  10. FuriStreamBuffer* rx_stream;
  11. uint8_t rx_buf[RX_BUF_SIZE + 1];
  12. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context);
  13. };
  14. typedef enum {
  15. WorkerEvtStop = (1 << 0),
  16. WorkerEvtRxDone = (1 << 1),
  17. } WorkerEvtFlags;
  18. void wifi_deauther_uart_set_handle_rx_data_cb(
  19. WifideautherUart* uart,
  20. void (*handle_rx_data_cb)(uint8_t* buf, size_t len, void* context)) {
  21. furi_assert(uart);
  22. uart->handle_rx_data_cb = handle_rx_data_cb;
  23. }
  24. #define WORKER_ALL_RX_EVENTS (WorkerEvtStop | WorkerEvtRxDone)
  25. void wifi_deauther_uart_on_irq_cb(UartIrqEvent ev, uint8_t data, void* context) {
  26. WifideautherUart* uart = (WifideautherUart*)context;
  27. if(ev == UartIrqEventRXNE) {
  28. furi_stream_buffer_send(uart->rx_stream, &data, 1, 0);
  29. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtRxDone);
  30. }
  31. }
  32. static int32_t uart_worker(void* context) {
  33. WifideautherUart* uart = (void*)context;
  34. while(1) {
  35. uint32_t events =
  36. furi_thread_flags_wait(WORKER_ALL_RX_EVENTS, FuriFlagWaitAny, FuriWaitForever);
  37. furi_check((events & FuriFlagError) == 0);
  38. if(events & WorkerEvtStop) break;
  39. if(events & WorkerEvtRxDone) {
  40. size_t len = furi_stream_buffer_receive(uart->rx_stream, uart->rx_buf, RX_BUF_SIZE, 0);
  41. if(len > 0) {
  42. if(uart->handle_rx_data_cb) uart->handle_rx_data_cb(uart->rx_buf, len, uart->app);
  43. }
  44. }
  45. }
  46. furi_stream_buffer_free(uart->rx_stream);
  47. return 0;
  48. }
  49. void wifi_deauther_uart_tx(uint8_t* data, size_t len) {
  50. furi_hal_uart_tx(UART_CH, data, len);
  51. }
  52. WifideautherUart* wifi_deauther_uart_init(WifideautherApp* app) {
  53. WifideautherUart* uart = malloc(sizeof(WifideautherUart));
  54. uart->app = app;
  55. uart->rx_stream = furi_stream_buffer_alloc(RX_BUF_SIZE, 1);
  56. uart->rx_thread = furi_thread_alloc();
  57. furi_thread_set_name(uart->rx_thread, "WifideautherUartRxThread");
  58. furi_thread_set_stack_size(uart->rx_thread, 1024);
  59. furi_thread_set_context(uart->rx_thread, uart);
  60. furi_thread_set_callback(uart->rx_thread, uart_worker);
  61. furi_thread_start(uart->rx_thread);
  62. furi_hal_console_disable();
  63. furi_hal_uart_set_br(UART_CH, BAUDRATE);
  64. furi_hal_uart_set_irq_cb(UART_CH, wifi_deauther_uart_on_irq_cb, uart);
  65. return uart;
  66. }
  67. void wifi_deauther_uart_free(WifideautherUart* uart) {
  68. furi_assert(uart);
  69. furi_thread_flags_set(furi_thread_get_id(uart->rx_thread), WorkerEvtStop);
  70. furi_thread_join(uart->rx_thread);
  71. furi_thread_free(uart->rx_thread);
  72. furi_hal_uart_set_irq_cb(UART_CH, NULL, NULL);
  73. furi_hal_console_enable();
  74. free(uart);
  75. }