subghz_file_encoder_worker.c 7.2 KB

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