subghz_file_encoder_worker.c 7.6 KB

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