subghz_file_encoder_worker.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 SUBGHZ_FILE_ENCODER_LOAD 512
  6. struct SubGhzFileEncoderWorker {
  7. FuriThread* thread;
  8. StreamBufferHandle_t stream;
  9. Storage* storage;
  10. FlipperFile* flipper_file;
  11. volatile bool worker_running;
  12. bool level;
  13. int32_t duration;
  14. string_t str_data;
  15. string_t file_path;
  16. };
  17. void subghz_file_encoder_worker_add_livel_duration(
  18. SubGhzFileEncoderWorker* instance,
  19. int32_t duration) {
  20. bool res = true;
  21. if(duration < 0 && !instance->level) {
  22. instance->duration += duration;
  23. res = false;
  24. } else if(duration > 0 && instance->level) {
  25. instance->duration += duration;
  26. res = false;
  27. } else if(duration == 0) {
  28. instance->duration = 0;
  29. }
  30. if(res) {
  31. instance->level = !instance->level;
  32. instance->duration += duration;
  33. xStreamBufferSend(instance->stream, &instance->duration, sizeof(int32_t), 10);
  34. instance->duration = 0;
  35. }
  36. }
  37. bool subghz_file_encoder_worker_data_parse(
  38. SubGhzFileEncoderWorker* instance,
  39. const char* strStart,
  40. size_t len) {
  41. char* str1;
  42. size_t ind_start = (size_t)strStart; //store the start address of the beginning of the line
  43. bool res = false;
  44. str1 = strstr(
  45. strStart, "RAW_Data: "); //looking for the beginning of the desired title in the line
  46. if(str1 != NULL) {
  47. str1 = strchr(
  48. str1,
  49. ' '); //if found, shift the pointer by 1 element per line "RAW_Data: -1, 2, -2..."
  50. while(
  51. strchr(str1, ' ') != NULL &&
  52. ((size_t)str1 <
  53. (len +
  54. ind_start))) { //check that there is still an element in the line and that it has not gone beyond the line
  55. str1 = strchr(str1, ' ');
  56. str1 += 1; //if found, shift the pointer by next element per line
  57. subghz_file_encoder_worker_add_livel_duration(instance, atoi(str1));
  58. }
  59. res = true;
  60. }
  61. return res;
  62. }
  63. LevelDuration subghz_file_encoder_worker_get_level_duration(void* context) {
  64. furi_assert(context);
  65. SubGhzFileEncoderWorker* instance = context;
  66. int32_t duration;
  67. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  68. int ret = xStreamBufferReceiveFromISR(
  69. instance->stream, &duration, sizeof(int32_t), &xHigherPriorityTaskWoken);
  70. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  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 * -1);
  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("SubGhzFileEncoderWorker", "Stop transmission");
  80. }
  81. return level_duration;
  82. } else {
  83. FURI_LOG_E("SubGhzFileEncoderWorker", "Slow flash read");
  84. return level_duration_wait();
  85. }
  86. }
  87. /** Worker thread
  88. *
  89. * @param context
  90. * @return exit code
  91. */
  92. static int32_t subghz_file_encoder_worker_thread(void* context) {
  93. SubGhzFileEncoderWorker* instance = context;
  94. FURI_LOG_I("SubGhzFileEncoderWorker", "Worker start");
  95. bool res = false;
  96. File* file = flipper_file_get_file(instance->flipper_file);
  97. do {
  98. if(!flipper_file_open_existing(
  99. instance->flipper_file, string_get_cstr(instance->file_path))) {
  100. FURI_LOG_E(
  101. "SubGhzFileEncoderWorker",
  102. "Unable to open file for read: %s",
  103. string_get_cstr(instance->file_path));
  104. break;
  105. }
  106. if(!flipper_file_read_string(instance->flipper_file, "Protocol", instance->str_data)) {
  107. FURI_LOG_E("SubGhzFileEncoderWorker", "Missing Protocol");
  108. break;
  109. }
  110. //skip the end of the previous line "\n"
  111. storage_file_seek(file, 1, false);
  112. res = true;
  113. FURI_LOG_I("SubGhzFileEncoderWorker", "Start transmission");
  114. } while(0);
  115. while(res && instance->worker_running) {
  116. size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
  117. if((stream_free_byte / sizeof(int32_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
  118. if(file_helper_read_line(file, instance->str_data)) {
  119. //skip the end of the previous line "\n"
  120. storage_file_seek(file, 1, false);
  121. if(!subghz_file_encoder_worker_data_parse(
  122. instance,
  123. string_get_cstr(instance->str_data),
  124. strlen(string_get_cstr(instance->str_data)))) {
  125. //to stop DMA correctly
  126. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  127. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  128. break;
  129. }
  130. } else {
  131. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  132. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  133. break;
  134. }
  135. }
  136. }
  137. //waiting for the end of the transfer
  138. FURI_LOG_I("SubGhzFileEncoderWorker", "End read file");
  139. while(instance->worker_running) {
  140. osDelay(50);
  141. }
  142. flipper_file_close(instance->flipper_file);
  143. FURI_LOG_I("SubGhzFileEncoderWorker", "Worker stop");
  144. return 0;
  145. }
  146. SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
  147. SubGhzFileEncoderWorker* instance = furi_alloc(sizeof(SubGhzFileEncoderWorker));
  148. instance->thread = furi_thread_alloc();
  149. furi_thread_set_name(instance->thread, "subghz_file_encoder_worker");
  150. furi_thread_set_stack_size(instance->thread, 2048);
  151. furi_thread_set_context(instance->thread, instance);
  152. furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);
  153. instance->stream = xStreamBufferCreate(sizeof(int32_t) * 2048, sizeof(int32_t));
  154. instance->storage = furi_record_open("storage");
  155. instance->flipper_file = flipper_file_alloc(instance->storage);
  156. string_init(instance->str_data);
  157. string_init(instance->file_path);
  158. instance->level = false;
  159. return instance;
  160. }
  161. void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
  162. furi_assert(instance);
  163. vStreamBufferDelete(instance->stream);
  164. furi_thread_free(instance->thread);
  165. string_clear(instance->str_data);
  166. string_clear(instance->file_path);
  167. flipper_file_free(instance->flipper_file);
  168. furi_record_close("storage");
  169. free(instance);
  170. }
  171. bool subghz_file_encoder_worker_start(SubGhzFileEncoderWorker* instance, const char* file_path) {
  172. furi_assert(instance);
  173. furi_assert(!instance->worker_running);
  174. xStreamBufferReset(instance->stream);
  175. string_set(instance->file_path, file_path);
  176. instance->worker_running = true;
  177. bool res = furi_thread_start(instance->thread);
  178. return res;
  179. }
  180. void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
  181. furi_assert(instance);
  182. furi_assert(instance->worker_running);
  183. instance->worker_running = false;
  184. furi_thread_join(instance->thread);
  185. }
  186. bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
  187. furi_assert(instance);
  188. return instance->worker_running;
  189. }