subghz_chat.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. #include "subghz_chat.h"
  2. #include <lib/subghz/subghz_tx_rx_worker.h>
  3. #define TAG "SubGhzChat"
  4. #define SUBGHZ_CHAT_WORKER_TIMEOUT_BETWEEN_MESSAGES 500
  5. struct SubGhzChatWorker {
  6. FuriThread* thread;
  7. SubGhzTxRxWorker* subghz_txrx;
  8. volatile bool worker_running;
  9. volatile bool worker_stoping;
  10. FuriMessageQueue* event_queue;
  11. uint32_t last_time_rx_data;
  12. Cli* cli;
  13. };
  14. /** Worker thread
  15. *
  16. * @param context
  17. * @return exit code
  18. */
  19. static int32_t subghz_chat_worker_thread(void* context) {
  20. SubGhzChatWorker* instance = context;
  21. FURI_LOG_I(TAG, "Worker start");
  22. char c;
  23. SubGhzChatEvent event;
  24. event.event = SubGhzChatEventUserEntrance;
  25. furi_message_queue_put(instance->event_queue, &event, 0);
  26. while(instance->worker_running) {
  27. if(cli_read_timeout(instance->cli, (uint8_t*)&c, 1, 1000) == 1) {
  28. event.event = SubGhzChatEventInputData;
  29. event.c = c;
  30. furi_message_queue_put(instance->event_queue, &event, FuriWaitForever);
  31. }
  32. }
  33. FURI_LOG_I(TAG, "Worker stop");
  34. return 0;
  35. }
  36. static void subghz_chat_worker_update_rx_event_chat(void* context) {
  37. furi_assert(context);
  38. SubGhzChatWorker* instance = context;
  39. SubGhzChatEvent event;
  40. if((furi_get_tick() - instance->last_time_rx_data) >
  41. SUBGHZ_CHAT_WORKER_TIMEOUT_BETWEEN_MESSAGES) {
  42. event.event = SubGhzChatEventNewMessage;
  43. furi_message_queue_put(instance->event_queue, &event, FuriWaitForever);
  44. }
  45. instance->last_time_rx_data = furi_get_tick();
  46. event.event = SubGhzChatEventRXData;
  47. furi_message_queue_put(instance->event_queue, &event, FuriWaitForever);
  48. }
  49. SubGhzChatWorker* subghz_chat_worker_alloc(Cli* cli) {
  50. SubGhzChatWorker* instance = malloc(sizeof(SubGhzChatWorker));
  51. instance->cli = cli;
  52. instance->thread =
  53. furi_thread_alloc_ex("SubGhzChat", 2048, subghz_chat_worker_thread, instance);
  54. instance->subghz_txrx = subghz_tx_rx_worker_alloc();
  55. instance->event_queue = furi_message_queue_alloc(80, sizeof(SubGhzChatEvent));
  56. return instance;
  57. }
  58. void subghz_chat_worker_free(SubGhzChatWorker* instance) {
  59. furi_assert(instance);
  60. furi_assert(!instance->worker_running);
  61. furi_message_queue_free(instance->event_queue);
  62. subghz_tx_rx_worker_free(instance->subghz_txrx);
  63. furi_thread_free(instance->thread);
  64. free(instance);
  65. }
  66. bool subghz_chat_worker_start(SubGhzChatWorker* instance, uint32_t frequency) {
  67. furi_assert(instance);
  68. furi_assert(!instance->worker_running);
  69. bool res = false;
  70. if(subghz_tx_rx_worker_start(instance->subghz_txrx, frequency)) {
  71. furi_message_queue_reset(instance->event_queue);
  72. subghz_tx_rx_worker_set_callback_have_read(
  73. instance->subghz_txrx, subghz_chat_worker_update_rx_event_chat, instance);
  74. instance->worker_running = true;
  75. instance->last_time_rx_data = 0;
  76. furi_thread_start(instance->thread);
  77. res = true;
  78. }
  79. return res;
  80. }
  81. void subghz_chat_worker_stop(SubGhzChatWorker* instance) {
  82. furi_assert(instance);
  83. furi_assert(instance->worker_running);
  84. if(subghz_tx_rx_worker_is_running(instance->subghz_txrx)) {
  85. subghz_tx_rx_worker_stop(instance->subghz_txrx);
  86. }
  87. instance->worker_running = false;
  88. furi_thread_join(instance->thread);
  89. }
  90. bool subghz_chat_worker_is_running(SubGhzChatWorker* instance) {
  91. furi_assert(instance);
  92. return instance->worker_running;
  93. }
  94. SubGhzChatEvent subghz_chat_worker_get_event_chat(SubGhzChatWorker* instance) {
  95. furi_assert(instance);
  96. SubGhzChatEvent event;
  97. if(furi_message_queue_get(instance->event_queue, &event, FuriWaitForever) == FuriStatusOk) {
  98. return event;
  99. } else {
  100. event.event = SubGhzChatEventNoEvent;
  101. return event;
  102. }
  103. }
  104. void subghz_chat_worker_put_event_chat(SubGhzChatWorker* instance, SubGhzChatEvent* event) {
  105. furi_assert(instance);
  106. furi_message_queue_put(instance->event_queue, event, FuriWaitForever);
  107. }
  108. size_t subghz_chat_worker_available(SubGhzChatWorker* instance) {
  109. furi_assert(instance);
  110. return subghz_tx_rx_worker_available(instance->subghz_txrx);
  111. }
  112. size_t subghz_chat_worker_read(SubGhzChatWorker* instance, uint8_t* data, size_t size) {
  113. furi_assert(instance);
  114. return subghz_tx_rx_worker_read(instance->subghz_txrx, data, size);
  115. }
  116. bool subghz_chat_worker_write(SubGhzChatWorker* instance, uint8_t* data, size_t size) {
  117. furi_assert(instance);
  118. return subghz_tx_rx_worker_write(instance->subghz_txrx, data, size);
  119. }