subghz_file_encoder_worker.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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(
  47. SubGhzFileEncoderWorker* instance,
  48. const char* strStart,
  49. size_t len) {
  50. char* str1;
  51. size_t ind_start = (size_t)strStart; //store the start address of the beginning of the line
  52. bool res = false;
  53. str1 = strstr(
  54. strStart, "RAW_Data: "); //looking for the beginning of the desired title in the line
  55. if(str1 != NULL) {
  56. str1 = strchr(
  57. str1,
  58. ' '); //if found, shift the pointer by 1 element per line "RAW_Data: -1, 2, -2..."
  59. while(
  60. strchr(str1, ' ') != NULL &&
  61. ((size_t)str1 <
  62. (len +
  63. ind_start))) { //check that there is still an element in the line and that it has not gone beyond the line
  64. str1 = strchr(str1, ' ');
  65. str1 += 1; //if found, shift the pointer by next element per line
  66. subghz_file_encoder_worker_add_level_duration(instance, atoi(str1));
  67. }
  68. res = true;
  69. }
  70. return res;
  71. }
  72. LevelDuration subghz_file_encoder_worker_get_level_duration(void* context) {
  73. furi_assert(context);
  74. SubGhzFileEncoderWorker* instance = context;
  75. int32_t duration;
  76. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  77. int ret = xStreamBufferReceiveFromISR(
  78. instance->stream, &duration, sizeof(int32_t), &xHigherPriorityTaskWoken);
  79. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  80. if(ret == sizeof(int32_t)) {
  81. LevelDuration level_duration = {.level = LEVEL_DURATION_RESET};
  82. if(duration < 0) {
  83. level_duration = level_duration_make(false, duration * -1);
  84. } else if(duration > 0) {
  85. level_duration = level_duration_make(true, duration);
  86. } else if(duration == 0) {
  87. level_duration = level_duration_reset();
  88. FURI_LOG_I(TAG, "Stop transmission");
  89. instance->worker_stoping = true;
  90. }
  91. return level_duration;
  92. } else {
  93. FURI_LOG_E(TAG, "Slow flash read");
  94. return level_duration_wait();
  95. }
  96. }
  97. /** Worker thread
  98. *
  99. * @param context
  100. * @return exit code
  101. */
  102. static int32_t subghz_file_encoder_worker_thread(void* context) {
  103. SubGhzFileEncoderWorker* instance = context;
  104. FURI_LOG_I(TAG, "Worker start");
  105. bool res = false;
  106. Stream* stream = flipper_format_get_raw_stream(instance->flipper_format);
  107. do {
  108. if(!flipper_format_file_open_existing(
  109. instance->flipper_format, string_get_cstr(instance->file_path))) {
  110. FURI_LOG_E(
  111. TAG, "Unable to open file for read: %s", string_get_cstr(instance->file_path));
  112. break;
  113. }
  114. if(!flipper_format_read_string(instance->flipper_format, "Protocol", instance->str_data)) {
  115. FURI_LOG_E(TAG, "Missing Protocol");
  116. break;
  117. }
  118. //skip the end of the previous line "\n"
  119. stream_seek(stream, 1, StreamOffsetFromCurrent);
  120. res = true;
  121. instance->worker_stoping = false;
  122. FURI_LOG_I(TAG, "Start transmission");
  123. } while(0);
  124. while(res && instance->worker_running) {
  125. size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
  126. if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
  127. if(stream_read_line(stream, instance->str_data)) {
  128. string_strim(instance->str_data);
  129. if(!subghz_file_encoder_worker_data_parse(
  130. instance,
  131. string_get_cstr(instance->str_data),
  132. strlen(string_get_cstr(instance->str_data)))) {
  133. //to stop DMA correctly
  134. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  135. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  136. break;
  137. }
  138. } else {
  139. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  140. subghz_file_encoder_worker_add_level_duration(instance, LEVEL_DURATION_RESET);
  141. break;
  142. }
  143. }
  144. osDelay(5);
  145. }
  146. //waiting for the end of the transfer
  147. FURI_LOG_I(TAG, "End read file");
  148. while(!furi_hal_subghz_is_async_tx_complete() && instance->worker_running) {
  149. osDelay(5);
  150. }
  151. FURI_LOG_I(TAG, "End transmission");
  152. while(instance->worker_running) {
  153. if(instance->worker_stoping) {
  154. if(instance->callback_end) instance->callback_end(instance->context_end);
  155. }
  156. osDelay(50);
  157. }
  158. flipper_format_file_close(instance->flipper_format);
  159. FURI_LOG_I(TAG, "Worker stop");
  160. return 0;
  161. }
  162. SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
  163. SubGhzFileEncoderWorker* instance = malloc(sizeof(SubGhzFileEncoderWorker));
  164. instance->thread = furi_thread_alloc();
  165. furi_thread_set_name(instance->thread, "SubGhzFEWorker");
  166. furi_thread_set_stack_size(instance->thread, 2048);
  167. furi_thread_set_context(instance->thread, instance);
  168. furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);
  169. instance->stream = xStreamBufferCreate(sizeof(int32_t) * 2048, sizeof(int32_t));
  170. instance->storage = furi_record_open("storage");
  171. instance->flipper_format = flipper_format_file_alloc(instance->storage);
  172. string_init(instance->str_data);
  173. string_init(instance->file_path);
  174. instance->level = false;
  175. instance->worker_stoping = true;
  176. return instance;
  177. }
  178. void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
  179. furi_assert(instance);
  180. vStreamBufferDelete(instance->stream);
  181. furi_thread_free(instance->thread);
  182. string_clear(instance->str_data);
  183. string_clear(instance->file_path);
  184. flipper_format_free(instance->flipper_format);
  185. furi_record_close("storage");
  186. free(instance);
  187. }
  188. bool subghz_file_encoder_worker_start(SubGhzFileEncoderWorker* instance, const char* file_path) {
  189. furi_assert(instance);
  190. furi_assert(!instance->worker_running);
  191. xStreamBufferReset(instance->stream);
  192. string_set(instance->file_path, file_path);
  193. instance->worker_running = true;
  194. bool res = furi_thread_start(instance->thread);
  195. return res;
  196. }
  197. void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
  198. furi_assert(instance);
  199. furi_assert(instance->worker_running);
  200. instance->worker_running = false;
  201. furi_thread_join(instance->thread);
  202. }
  203. bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
  204. furi_assert(instance);
  205. return instance->worker_running;
  206. }