uhf_worker.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "uhf_worker.h"
  2. #include "uhf_data.h"
  3. int32_t uhf_worker_task(void* ctx) {
  4. UHFWorker* uhf_worker = ctx;
  5. FURI_LOG_E("uhf_worker", "worker callback has been called");
  6. if(uhf_worker->callback) {
  7. uhf_worker->callback((UHFWorkerEvent)NULL, uhf_worker->ctx);
  8. }
  9. return 0;
  10. }
  11. UHFWorker* uhf_worker_alloc() {
  12. UHFWorker* uhf_worker = (UHFWorker*)malloc(sizeof(UHFWorker));
  13. uhf_worker->thread = furi_thread_alloc_ex("UHFWorker", 8 * 1024, uhf_worker_task, uhf_worker);
  14. uhf_worker->data = uhf_response_data_alloc();
  15. uhf_worker->callback = NULL;
  16. uhf_worker->ctx = NULL;
  17. return uhf_worker;
  18. }
  19. void uhf_worker_change_state(UHFWorker* worker, UHFWorkerState state) {
  20. worker->state = state;
  21. }
  22. void uhf_worker_start(
  23. UHFWorker* uhf_worker,
  24. UHFWorkerState state,
  25. UHFWorkerCallback callback,
  26. void* ctx) {
  27. uhf_worker->state = state;
  28. uhf_worker->callback = callback;
  29. uhf_worker->ctx = ctx;
  30. furi_thread_start(uhf_worker->thread);
  31. }
  32. void uhf_worker_stop(UHFWorker* uhf_worker) {
  33. furi_assert(uhf_worker);
  34. furi_assert(uhf_worker->thread);
  35. if(furi_thread_get_state(uhf_worker->thread) != FuriThreadStateStopped) {
  36. uhf_worker_change_state(uhf_worker, UHFWorkerStateStop);
  37. furi_thread_join(uhf_worker->thread);
  38. }
  39. }
  40. void uhf_worker_free(UHFWorker* uhf_worker) {
  41. furi_assert(uhf_worker);
  42. furi_thread_free(uhf_worker->thread);
  43. uhf_data_free((UHFData*)uhf_worker->data);
  44. free(uhf_worker);
  45. }