ibutton_worker.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <atomic.h>
  4. #include "ibutton_worker_i.h"
  5. typedef enum {
  6. iButtonMessageEnd,
  7. iButtonMessageStop,
  8. iButtonMessageRead,
  9. iButtonMessageWrite,
  10. iButtonMessageEmulate,
  11. iButtonMessageNotifyEmulate,
  12. } iButtonMessageType;
  13. typedef struct {
  14. iButtonMessageType type;
  15. union {
  16. iButtonKey* key;
  17. } data;
  18. } iButtonMessage;
  19. static int32_t ibutton_worker_thread(void* thread_context);
  20. iButtonWorker* ibutton_worker_alloc() {
  21. iButtonWorker* worker = malloc(sizeof(iButtonWorker));
  22. worker->key_p = NULL;
  23. worker->key_data = malloc(ibutton_key_get_max_size());
  24. worker->host = onewire_host_alloc();
  25. worker->slave = onewire_slave_alloc();
  26. worker->writer = ibutton_writer_alloc(worker->host);
  27. worker->device = onewire_device_alloc(0, 0, 0, 0, 0, 0, 0, 0);
  28. worker->pulse_decoder = pulse_decoder_alloc();
  29. worker->protocol_cyfral = protocol_cyfral_alloc();
  30. worker->protocol_metakom = protocol_metakom_alloc();
  31. worker->messages = furi_message_queue_alloc(1, sizeof(iButtonMessage));
  32. worker->mode_index = iButtonWorkerIdle;
  33. worker->last_dwt_value = 0;
  34. worker->read_cb = NULL;
  35. worker->write_cb = NULL;
  36. worker->emulate_cb = NULL;
  37. worker->cb_ctx = NULL;
  38. worker->encoder_cyfral = encoder_cyfral_alloc();
  39. worker->encoder_metakom = encoder_metakom_alloc();
  40. worker->thread = furi_thread_alloc();
  41. furi_thread_set_name(worker->thread, "ibutton_worker");
  42. furi_thread_set_callback(worker->thread, ibutton_worker_thread);
  43. furi_thread_set_context(worker->thread, worker);
  44. furi_thread_set_stack_size(worker->thread, 2048);
  45. pulse_decoder_add_protocol(
  46. worker->pulse_decoder,
  47. protocol_cyfral_get_protocol(worker->protocol_cyfral),
  48. PulseProtocolCyfral);
  49. pulse_decoder_add_protocol(
  50. worker->pulse_decoder,
  51. protocol_metakom_get_protocol(worker->protocol_metakom),
  52. PulseProtocolMetakom);
  53. return worker;
  54. }
  55. void ibutton_worker_read_set_callback(
  56. iButtonWorker* worker,
  57. iButtonWorkerReadCallback callback,
  58. void* context) {
  59. furi_check(worker->mode_index == iButtonWorkerIdle);
  60. worker->read_cb = callback;
  61. worker->cb_ctx = context;
  62. }
  63. void ibutton_worker_write_set_callback(
  64. iButtonWorker* worker,
  65. iButtonWorkerWriteCallback callback,
  66. void* context) {
  67. furi_check(worker->mode_index == iButtonWorkerIdle);
  68. worker->write_cb = callback;
  69. worker->cb_ctx = context;
  70. }
  71. void ibutton_worker_emulate_set_callback(
  72. iButtonWorker* worker,
  73. iButtonWorkerEmulateCallback callback,
  74. void* context) {
  75. furi_check(worker->mode_index == iButtonWorkerIdle);
  76. worker->emulate_cb = callback;
  77. worker->cb_ctx = context;
  78. }
  79. void ibutton_worker_read_start(iButtonWorker* worker, iButtonKey* key) {
  80. iButtonMessage message = {.type = iButtonMessageRead, .data.key = key};
  81. furi_check(
  82. furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
  83. }
  84. void ibutton_worker_write_start(iButtonWorker* worker, iButtonKey* key) {
  85. iButtonMessage message = {.type = iButtonMessageWrite, .data.key = key};
  86. furi_check(
  87. furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
  88. }
  89. void ibutton_worker_emulate_start(iButtonWorker* worker, iButtonKey* key) {
  90. iButtonMessage message = {.type = iButtonMessageEmulate, .data.key = key};
  91. furi_check(
  92. furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
  93. }
  94. void ibutton_worker_stop(iButtonWorker* worker) {
  95. iButtonMessage message = {.type = iButtonMessageStop};
  96. furi_check(
  97. furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
  98. }
  99. void ibutton_worker_free(iButtonWorker* worker) {
  100. pulse_decoder_free(worker->pulse_decoder);
  101. protocol_metakom_free(worker->protocol_metakom);
  102. protocol_cyfral_free(worker->protocol_cyfral);
  103. ibutton_writer_free(worker->writer);
  104. onewire_slave_free(worker->slave);
  105. onewire_host_free(worker->host);
  106. onewire_device_free(worker->device);
  107. encoder_cyfral_free(worker->encoder_cyfral);
  108. encoder_metakom_free(worker->encoder_metakom);
  109. furi_message_queue_free(worker->messages);
  110. furi_thread_free(worker->thread);
  111. free(worker->key_data);
  112. free(worker);
  113. }
  114. void ibutton_worker_start_thread(iButtonWorker* worker) {
  115. furi_thread_start(worker->thread);
  116. }
  117. void ibutton_worker_stop_thread(iButtonWorker* worker) {
  118. iButtonMessage message = {.type = iButtonMessageEnd};
  119. furi_check(
  120. furi_message_queue_put(worker->messages, &message, FuriWaitForever) == FuriStatusOk);
  121. furi_thread_join(worker->thread);
  122. }
  123. void ibutton_worker_switch_mode(iButtonWorker* worker, iButtonWorkerMode mode) {
  124. ibutton_worker_modes[worker->mode_index].stop(worker);
  125. worker->mode_index = mode;
  126. ibutton_worker_modes[worker->mode_index].start(worker);
  127. }
  128. void ibutton_worker_notify_emulate(iButtonWorker* worker) {
  129. iButtonMessage message = {.type = iButtonMessageNotifyEmulate};
  130. furi_check(furi_message_queue_put(worker->messages, &message, 0) == FuriStatusOk);
  131. }
  132. void ibutton_worker_set_key_p(iButtonWorker* worker, iButtonKey* key) {
  133. worker->key_p = key;
  134. }
  135. static int32_t ibutton_worker_thread(void* thread_context) {
  136. iButtonWorker* worker = thread_context;
  137. bool running = true;
  138. iButtonMessage message;
  139. FuriStatus status;
  140. ibutton_worker_modes[worker->mode_index].start(worker);
  141. while(running) {
  142. status = furi_message_queue_get(
  143. worker->messages, &message, ibutton_worker_modes[worker->mode_index].quant);
  144. if(status == FuriStatusOk) {
  145. switch(message.type) {
  146. case iButtonMessageEnd:
  147. ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
  148. ibutton_worker_set_key_p(worker, NULL);
  149. running = false;
  150. break;
  151. case iButtonMessageStop:
  152. ibutton_worker_switch_mode(worker, iButtonWorkerIdle);
  153. ibutton_worker_set_key_p(worker, NULL);
  154. break;
  155. case iButtonMessageRead:
  156. ibutton_worker_set_key_p(worker, message.data.key);
  157. ibutton_worker_switch_mode(worker, iButtonWorkerRead);
  158. break;
  159. case iButtonMessageWrite:
  160. ibutton_worker_set_key_p(worker, message.data.key);
  161. ibutton_worker_switch_mode(worker, iButtonWorkerWrite);
  162. break;
  163. case iButtonMessageEmulate:
  164. ibutton_worker_set_key_p(worker, message.data.key);
  165. ibutton_worker_switch_mode(worker, iButtonWorkerEmulate);
  166. break;
  167. case iButtonMessageNotifyEmulate:
  168. if(worker->emulate_cb) {
  169. worker->emulate_cb(worker->cb_ctx, true);
  170. }
  171. break;
  172. }
  173. } else if(status == FuriStatusErrorTimeout) {
  174. ibutton_worker_modes[worker->mode_index].tick(worker);
  175. } else {
  176. furi_crash("iButton worker error");
  177. }
  178. }
  179. ibutton_worker_modes[worker->mode_index].stop(worker);
  180. return 0;
  181. }