subghz_file_encoder_worker.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. string_t str_data;
  17. string_t 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, string_get_cstr(instance->file_path))) {
  104. FURI_LOG_E(
  105. TAG, "Unable to open file for read: %s", string_get_cstr(instance->file_path));
  106. break;
  107. }
  108. if(!flipper_format_read_string(instance->flipper_format, "Protocol", instance->str_data)) {
  109. FURI_LOG_E(TAG, "Missing Protocol");
  110. break;
  111. }
  112. //skip the end of the previous line "\n"
  113. stream_seek(stream, 1, StreamOffsetFromCurrent);
  114. res = true;
  115. instance->worker_stoping = false;
  116. FURI_LOG_I(TAG, "Start transmission");
  117. } while(0);
  118. while(res && instance->worker_running) {
  119. size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
  120. if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
  121. if(stream_read_line(stream, instance->str_data)) {
  122. string_strim(instance->str_data);
  123. if(!subghz_file_encoder_worker_data_parse(
  124. instance, string_get_cstr(instance->str_data))) {
  125. //to stop DMA correctly
  126. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  127. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  128. break;
  129. }
  130. } else {
  131. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  132. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  133. break;
  134. }
  135. }
  136. furi_delay_ms(5);
  137. }
  138. //waiting for the end of the transfer
  139. FURI_LOG_I(TAG, "End read file");
  140. while(!furi_hal_subghz_is_async_tx_complete() && instance->worker_running) {
  141. furi_delay_ms(5);
  142. }
  143. FURI_LOG_I(TAG, "End transmission");
  144. while(instance->worker_running) {
  145. if(instance->worker_stoping) {
  146. if(instance->callback_end) instance->callback_end(instance->context_end);
  147. }
  148. furi_delay_ms(50);
  149. }
  150. flipper_format_file_close(instance->flipper_format);
  151. FURI_LOG_I(TAG, "Worker stop");
  152. return 0;
  153. }
  154. SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
  155. SubGhzFileEncoderWorker* instance = malloc(sizeof(SubGhzFileEncoderWorker));
  156. instance->thread = furi_thread_alloc();
  157. furi_thread_set_name(instance->thread, "SubGhzFEWorker");
  158. furi_thread_set_stack_size(instance->thread, 2048);
  159. furi_thread_set_context(instance->thread, instance);
  160. furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);
  161. instance->stream = xStreamBufferCreate(sizeof(int32_t) * 2048, sizeof(int32_t));
  162. instance->storage = furi_record_open(RECORD_STORAGE);
  163. instance->flipper_format = flipper_format_file_alloc(instance->storage);
  164. string_init(instance->str_data);
  165. string_init(instance->file_path);
  166. instance->level = false;
  167. instance->worker_stoping = true;
  168. return instance;
  169. }
  170. void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
  171. furi_assert(instance);
  172. vStreamBufferDelete(instance->stream);
  173. furi_thread_free(instance->thread);
  174. string_clear(instance->str_data);
  175. string_clear(instance->file_path);
  176. flipper_format_free(instance->flipper_format);
  177. furi_record_close(RECORD_STORAGE);
  178. free(instance);
  179. }
  180. bool subghz_file_encoder_worker_start(SubGhzFileEncoderWorker* instance, const char* file_path) {
  181. furi_assert(instance);
  182. furi_assert(!instance->worker_running);
  183. xStreamBufferReset(instance->stream);
  184. string_set(instance->file_path, file_path);
  185. instance->worker_running = true;
  186. furi_thread_start(instance->thread);
  187. return true;
  188. }
  189. void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
  190. furi_assert(instance);
  191. furi_assert(instance->worker_running);
  192. instance->worker_running = false;
  193. furi_thread_join(instance->thread);
  194. }
  195. bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
  196. furi_assert(instance);
  197. return instance->worker_running;
  198. }