subghz_file_encoder_worker.c 8.1 KB

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