subghz_file_encoder_worker.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #include "subghz_file_encoder_worker.h"
  2. #include <toolbox/stream/stream.h>
  3. #include <flipper_format/flipper_format.h>
  4. #include <flipper_format/flipper_format_i.h>
  5. #define TAG "SubGhzFileEncoderWorker"
  6. #define SUBGHZ_FILE_ENCODER_LOAD 512
  7. struct SubGhzFileEncoderWorker {
  8. FuriThread* thread;
  9. FuriStreamBuffer* stream;
  10. Storage* storage;
  11. FlipperFormat* flipper_format;
  12. volatile bool worker_running;
  13. volatile bool worker_stoping;
  14. bool level;
  15. FuriString* str_data;
  16. FuriString* file_path;
  17. SubGhzFileEncoderWorkerCallbackEnd callback_end;
  18. void* context_end;
  19. };
  20. void subghz_file_encoder_worker_callback_end(
  21. SubGhzFileEncoderWorker* instance,
  22. SubGhzFileEncoderWorkerCallbackEnd callback_end,
  23. void* context_end) {
  24. furi_assert(instance);
  25. furi_assert(callback_end);
  26. instance->callback_end = callback_end;
  27. instance->context_end = context_end;
  28. }
  29. void subghz_file_encoder_worker_add_level_duration(
  30. SubGhzFileEncoderWorker* instance,
  31. int32_t duration) {
  32. bool res = true;
  33. if(duration < 0 && !instance->level) {
  34. res = false;
  35. } else if(duration > 0 && instance->level) {
  36. res = false;
  37. }
  38. if(res) {
  39. instance->level = !instance->level;
  40. furi_stream_buffer_send(instance->stream, &duration, sizeof(int32_t), 100);
  41. } else {
  42. FURI_LOG_E(TAG, "Invalid level in the stream");
  43. }
  44. }
  45. bool subghz_file_encoder_worker_data_parse(SubGhzFileEncoderWorker* instance, const char* strStart) {
  46. char* str1;
  47. bool res = false;
  48. // Line sample: "RAW_Data: -1, 2, -2..."
  49. // Look for a key in the line
  50. str1 = strstr(strStart, "RAW_Data: ");
  51. if(str1 != NULL) {
  52. // Skip key
  53. str1 = strchr(str1, ' ');
  54. // Check that there is still an element in the line
  55. while(strchr(str1, ' ') != NULL) {
  56. str1 = strchr(str1, ' ');
  57. // Skip space
  58. str1 += 1;
  59. subghz_file_encoder_worker_add_level_duration(instance, atoi(str1));
  60. }
  61. res = true;
  62. }
  63. return res;
  64. }
  65. LevelDuration subghz_file_encoder_worker_get_level_duration(void* context) {
  66. furi_assert(context);
  67. SubGhzFileEncoderWorker* instance = context;
  68. int32_t duration;
  69. int ret = furi_stream_buffer_receive(instance->stream, &duration, sizeof(int32_t), 0);
  70. if(ret == sizeof(int32_t)) {
  71. LevelDuration level_duration = {.level = LEVEL_DURATION_RESET};
  72. if(duration < 0) {
  73. level_duration = level_duration_make(false, duration * -1);
  74. } else if(duration > 0) {
  75. level_duration = level_duration_make(true, duration);
  76. } else if(duration == 0) {
  77. level_duration = level_duration_reset();
  78. FURI_LOG_I(TAG, "Stop transmission");
  79. instance->worker_stoping = true;
  80. }
  81. return level_duration;
  82. } else {
  83. FURI_LOG_E(TAG, "Slow flash read");
  84. return level_duration_wait();
  85. }
  86. }
  87. /** Worker thread
  88. *
  89. * @param context
  90. * @return exit code
  91. */
  92. static int32_t subghz_file_encoder_worker_thread(void* context) {
  93. SubGhzFileEncoderWorker* instance = context;
  94. FURI_LOG_I(TAG, "Worker start");
  95. bool res = false;
  96. Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
  97. do {
  98. if(!flipper_format_file_open_existing(
  99. instance->flipper_format, furi_string_get_cstr(instance->file_path))) {
  100. FURI_LOG_E(
  101. TAG,
  102. "Unable to open file for read: %s",
  103. furi_string_get_cstr(instance->file_path));
  104. break;
  105. }
  106. if(!flipper_format_read_string(instance->flipper_format, "Protocol", instance->str_data)) {
  107. FURI_LOG_E(TAG, "Missing Protocol");
  108. break;
  109. }
  110. //skip the end of the previous line "\n"
  111. stream_seek(stream, 1, StreamOffsetFromCurrent);
  112. res = true;
  113. instance->worker_stoping = false;
  114. FURI_LOG_I(TAG, "Start transmission");
  115. } while(0);
  116. while(res && instance->worker_running) {
  117. size_t stream_free_byte = furi_stream_buffer_spaces_available(instance->stream);
  118. if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
  119. if(stream_read_line(stream, instance->str_data)) {
  120. furi_string_trim(instance->str_data);
  121. if(!subghz_file_encoder_worker_data_parse(
  122. instance, furi_string_get_cstr(instance->str_data))) {
  123. //to stop DMA correctly
  124. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  125. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  126. break;
  127. }
  128. } else {
  129. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  130. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  131. break;
  132. }
  133. }
  134. furi_delay_ms(5);
  135. }
  136. //waiting for the end of the transfer
  137. FURI_LOG_I(TAG, "End read file");
  138. while(!furi_hal_subghz_is_async_tx_complete() && instance->worker_running) {
  139. furi_delay_ms(5);
  140. }
  141. FURI_LOG_I(TAG, "End transmission");
  142. while(instance->worker_running) {
  143. if(instance->worker_stoping) {
  144. if(instance->callback_end) instance->callback_end(instance->context_end);
  145. }
  146. furi_delay_ms(50);
  147. }
  148. flipper_format_file_close(instance->flipper_format);
  149. FURI_LOG_I(TAG, "Worker stop");
  150. return 0;
  151. }
  152. SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
  153. SubGhzFileEncoderWorker* instance = malloc(sizeof(SubGhzFileEncoderWorker));
  154. instance->thread = furi_thread_alloc();
  155. furi_thread_set_name(instance->thread, "SubGhzFEWorker");
  156. furi_thread_set_stack_size(instance->thread, 2048);
  157. furi_thread_set_context(instance->thread, instance);
  158. furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);
  159. instance->stream = furi_stream_buffer_alloc(sizeof(int32_t) * 2048, sizeof(int32_t));
  160. instance->storage = furi_record_open(RECORD_STORAGE);
  161. instance->flipper_format = flipper_format_file_alloc(instance->storage);
  162. instance->str_data = furi_string_alloc();
  163. instance->file_path = furi_string_alloc();
  164. instance->level = false;
  165. instance->worker_stoping = true;
  166. return instance;
  167. }
  168. void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
  169. furi_assert(instance);
  170. furi_stream_buffer_free(instance->stream);
  171. furi_thread_free(instance->thread);
  172. furi_string_free(instance->str_data);
  173. furi_string_free(instance->file_path);
  174. flipper_format_free(instance->flipper_format);
  175. furi_record_close(RECORD_STORAGE);
  176. free(instance);
  177. }
  178. bool subghz_file_encoder_worker_start(SubGhzFileEncoderWorker* instance, const char* file_path) {
  179. furi_assert(instance);
  180. furi_assert(!instance->worker_running);
  181. furi_stream_buffer_reset(instance->stream);
  182. furi_string_set(instance->file_path, file_path);
  183. instance->worker_running = true;
  184. furi_thread_start(instance->thread);
  185. return true;
  186. }
  187. void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
  188. furi_assert(instance);
  189. furi_assert(instance->worker_running);
  190. instance->worker_running = false;
  191. furi_thread_join(instance->thread);
  192. }
  193. bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
  194. furi_assert(instance);
  195. return instance->worker_running;
  196. }