subghz_file_encoder_worker.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. bool is_storage_slow;
  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. furi_stream_buffer_send(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. int ret = furi_stream_buffer_receive(instance->stream, &duration, sizeof(int32_t), 0);
  71. if(ret == sizeof(int32_t)) {
  72. LevelDuration level_duration = {.level = LEVEL_DURATION_RESET};
  73. if(duration < 0) {
  74. level_duration = level_duration_make(false, -duration);
  75. } else if(duration > 0) {
  76. level_duration = level_duration_make(true, duration);
  77. } else if(duration == 0) {
  78. level_duration = level_duration_reset();
  79. FURI_LOG_I(TAG, "Stop transmission");
  80. instance->worker_stoping = true;
  81. }
  82. return level_duration;
  83. } else {
  84. instance->is_storage_slow = true;
  85. return level_duration_wait();
  86. }
  87. }
  88. /** Worker thread
  89. *
  90. * @param context
  91. * @return exit code
  92. */
  93. static int32_t subghz_file_encoder_worker_thread(void* context) {
  94. SubGhzFileEncoderWorker* instance = context;
  95. FURI_LOG_I(TAG, "Worker start");
  96. bool res = false;
  97. instance->is_storage_slow = false;
  98. Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
  99. do {
  100. if(!flipper_format_file_open_existing(
  101. instance->flipper_format, furi_string_get_cstr(instance->file_path))) {
  102. FURI_LOG_E(
  103. TAG,
  104. "Unable to open file for read: %s",
  105. furi_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 = furi_stream_buffer_spaces_available(instance->stream);
  120. if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
  121. if(stream_read_line(stream, instance->str_data)) {
  122. furi_string_trim(instance->str_data);
  123. if(!subghz_file_encoder_worker_data_parse(
  124. instance, furi_string_get_cstr(instance->str_data))) {
  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. break;
  131. }
  132. } else {
  133. furi_delay_ms(1);
  134. }
  135. }
  136. //waiting for the end of the transfer
  137. if(instance->is_storage_slow) {
  138. FURI_LOG_E(TAG, "Storage is slow");
  139. }
  140. FURI_LOG_I(TAG, "End read file");
  141. while(!furi_hal_subghz_is_async_tx_complete() && instance->worker_running) {
  142. furi_delay_ms(5);
  143. }
  144. FURI_LOG_I(TAG, "End transmission");
  145. while(instance->worker_running) {
  146. if(instance->worker_stoping) {
  147. if(instance->callback_end) instance->callback_end(instance->context_end);
  148. }
  149. furi_delay_ms(50);
  150. }
  151. flipper_format_file_close(instance->flipper_format);
  152. FURI_LOG_I(TAG, "Worker stop");
  153. return 0;
  154. }
  155. SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
  156. SubGhzFileEncoderWorker* instance = malloc(sizeof(SubGhzFileEncoderWorker));
  157. instance->thread =
  158. furi_thread_alloc_ex("SubGhzFEWorker", 2048, subghz_file_encoder_worker_thread, instance);
  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. }