subghz_file_encoder_worker.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #include "subghz_file_encoder_worker.h"
  2. #include <stream_buffer.h>
  3. #include <lib/flipper_file/flipper_file.h>
  4. #include <lib/flipper_file/file_helper.h>
  5. #define TAG "SubGhzFileEncoderWorker"
  6. #define SUBGHZ_FILE_ENCODER_LOAD 512
  7. struct SubGhzFileEncoderWorker {
  8. FuriThread* thread;
  9. StreamBufferHandle_t stream;
  10. Storage* storage;
  11. FlipperFile* flipper_file;
  12. volatile bool worker_running;
  13. volatile bool worker_stoping;
  14. bool level;
  15. int32_t duration;
  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_livel_duration(
  31. SubGhzFileEncoderWorker* instance,
  32. int32_t duration) {
  33. bool res = true;
  34. if(duration < 0 && !instance->level) {
  35. instance->duration += duration;
  36. res = false;
  37. } else if(duration > 0 && instance->level) {
  38. instance->duration += duration;
  39. res = false;
  40. } else if(duration == 0) {
  41. instance->duration = 0;
  42. }
  43. if(res) {
  44. instance->level = !instance->level;
  45. instance->duration += duration;
  46. xStreamBufferSend(instance->stream, &instance->duration, sizeof(int32_t), 10);
  47. instance->duration = 0;
  48. }
  49. }
  50. bool subghz_file_encoder_worker_data_parse(
  51. SubGhzFileEncoderWorker* instance,
  52. const char* strStart,
  53. size_t len) {
  54. char* str1;
  55. size_t ind_start = (size_t)strStart; //store the start address of the beginning of the line
  56. bool res = false;
  57. str1 = strstr(
  58. strStart, "RAW_Data: "); //looking for the beginning of the desired title in the line
  59. if(str1 != NULL) {
  60. str1 = strchr(
  61. str1,
  62. ' '); //if found, shift the pointer by 1 element per line "RAW_Data: -1, 2, -2..."
  63. while(
  64. strchr(str1, ' ') != NULL &&
  65. ((size_t)str1 <
  66. (len +
  67. ind_start))) { //check that there is still an element in the line and that it has not gone beyond the line
  68. str1 = strchr(str1, ' ');
  69. str1 += 1; //if found, shift the pointer by next element per line
  70. subghz_file_encoder_worker_add_livel_duration(instance, atoi(str1));
  71. }
  72. res = true;
  73. }
  74. return res;
  75. }
  76. LevelDuration subghz_file_encoder_worker_get_level_duration(void* context) {
  77. furi_assert(context);
  78. SubGhzFileEncoderWorker* instance = context;
  79. int32_t duration;
  80. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  81. int ret = xStreamBufferReceiveFromISR(
  82. instance->stream, &duration, sizeof(int32_t), &xHigherPriorityTaskWoken);
  83. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  84. if(ret == sizeof(int32_t)) {
  85. LevelDuration level_duration = {.level = LEVEL_DURATION_RESET};
  86. if(duration < 0) {
  87. level_duration = level_duration_make(false, duration * -1);
  88. } else if(duration > 0) {
  89. level_duration = level_duration_make(true, duration);
  90. } else if(duration == 0) {
  91. level_duration = level_duration_reset();
  92. FURI_LOG_I(TAG, "Stop transmission");
  93. instance->worker_stoping = true;
  94. }
  95. return level_duration;
  96. } else {
  97. FURI_LOG_E(TAG, "Slow flash read");
  98. return level_duration_wait();
  99. }
  100. }
  101. /** Worker thread
  102. *
  103. * @param context
  104. * @return exit code
  105. */
  106. static int32_t subghz_file_encoder_worker_thread(void* context) {
  107. SubGhzFileEncoderWorker* instance = context;
  108. FURI_LOG_I(TAG, "Worker start");
  109. bool res = false;
  110. File* file = flipper_file_get_file(instance->flipper_file);
  111. do {
  112. if(!flipper_file_open_existing(
  113. instance->flipper_file, string_get_cstr(instance->file_path))) {
  114. FURI_LOG_E(
  115. TAG, "Unable to open file for read: %s", string_get_cstr(instance->file_path));
  116. break;
  117. }
  118. if(!flipper_file_read_string(instance->flipper_file, "Protocol", instance->str_data)) {
  119. FURI_LOG_E(TAG, "Missing Protocol");
  120. break;
  121. }
  122. //skip the end of the previous line "\n"
  123. storage_file_seek(file, 1, false);
  124. res = true;
  125. instance->worker_stoping = false;
  126. FURI_LOG_I(TAG, "Start transmission");
  127. } while(0);
  128. while(res && instance->worker_running) {
  129. size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
  130. if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
  131. if(file_helper_read_line(file, instance->str_data)) {
  132. //skip the end of the previous line "\n"
  133. storage_file_seek(file, 1, false);
  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(instance->worker_running) {
  154. if(instance->worker_stoping) {
  155. if(instance->callback_end) instance->callback_end(instance->context_end);
  156. }
  157. osDelay(50);
  158. }
  159. flipper_file_close(instance->flipper_file);
  160. FURI_LOG_I(TAG, "Worker stop");
  161. return 0;
  162. }
  163. SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
  164. SubGhzFileEncoderWorker* instance = furi_alloc(sizeof(SubGhzFileEncoderWorker));
  165. instance->thread = furi_thread_alloc();
  166. furi_thread_set_name(instance->thread, "SubghzFEWorker");
  167. furi_thread_set_stack_size(instance->thread, 2048);
  168. furi_thread_set_context(instance->thread, instance);
  169. furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);
  170. instance->stream = xStreamBufferCreate(sizeof(int32_t) * 2048, sizeof(int32_t));
  171. instance->storage = furi_record_open("storage");
  172. instance->flipper_file = flipper_file_alloc(instance->storage);
  173. string_init(instance->str_data);
  174. string_init(instance->file_path);
  175. instance->level = false;
  176. instance->worker_stoping = true;
  177. return instance;
  178. }
  179. void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
  180. furi_assert(instance);
  181. vStreamBufferDelete(instance->stream);
  182. furi_thread_free(instance->thread);
  183. string_clear(instance->str_data);
  184. string_clear(instance->file_path);
  185. flipper_file_free(instance->flipper_file);
  186. furi_record_close("storage");
  187. free(instance);
  188. }
  189. bool subghz_file_encoder_worker_start(SubGhzFileEncoderWorker* instance, const char* file_path) {
  190. furi_assert(instance);
  191. furi_assert(!instance->worker_running);
  192. xStreamBufferReset(instance->stream);
  193. string_set(instance->file_path, file_path);
  194. instance->worker_running = true;
  195. bool res = furi_thread_start(instance->thread);
  196. return res;
  197. }
  198. void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
  199. furi_assert(instance);
  200. furi_assert(instance->worker_running);
  201. instance->worker_running = false;
  202. furi_thread_join(instance->thread);
  203. }
  204. bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
  205. furi_assert(instance);
  206. return instance->worker_running;
  207. }