subghz_protocol_raw.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "subghz_protocol_raw.h"
  2. #include "file-worker.h"
  3. #include "../subghz_file_encoder_worker.h"
  4. #define SUBGHZ_DOWNLOAD_MAX_SIZE 512
  5. struct SubGhzProtocolRAW {
  6. SubGhzProtocolCommon common;
  7. int16_t* upload_raw;
  8. uint16_t ind_write;
  9. FileWorker* file_worker;
  10. SubGhzFileEncoderWorker* file_worker_encoder;
  11. uint32_t file_is_open;
  12. string_t file_name;
  13. size_t sample_write;
  14. bool last_level;
  15. };
  16. typedef enum {
  17. RAWFileIsOpenClose = 0,
  18. RAWFileIsOpenWrite,
  19. RAWFileIsOpenRead,
  20. } RAWFilIsOpen;
  21. SubGhzProtocolRAW* subghz_protocol_raw_alloc(void) {
  22. SubGhzProtocolRAW* instance = furi_alloc(sizeof(SubGhzProtocolRAW));
  23. instance->upload_raw = NULL;
  24. instance->ind_write = 0;
  25. instance->last_level = false;
  26. instance->file_worker = file_worker_alloc(false);
  27. instance->file_is_open = RAWFileIsOpenClose;
  28. string_init(instance->file_name);
  29. instance->common.name = "RAW";
  30. instance->common.code_min_count_bit_for_found = 0;
  31. instance->common.te_short = 80;
  32. instance->common.te_long = 32700;
  33. instance->common.te_delta = 0;
  34. instance->common.type_protocol = SubGhzProtocolCommonTypeRAW;
  35. instance->common.to_load_protocol_from_file =
  36. (SubGhzProtocolCommonLoadFromFile)subghz_protocol_raw_to_load_protocol_from_file;
  37. instance->common.to_string = (SubGhzProtocolCommonToStr)subghz_protocol_raw_to_str;
  38. //instance->common.to_load_protocol =
  39. // (SubGhzProtocolCommonLoadFromRAW)subghz_decoder_raw_to_load_protocol;
  40. instance->common.get_upload_protocol =
  41. (SubGhzProtocolCommonEncoderGetUpLoad)subghz_protocol_raw_send_key;
  42. return instance;
  43. }
  44. void subghz_protocol_raw_free(SubGhzProtocolRAW* instance) {
  45. furi_assert(instance);
  46. string_clear(instance->file_name);
  47. file_worker_free(instance->file_worker);
  48. free(instance);
  49. }
  50. void subghz_protocol_raw_file_encoder_worker_stop(void* context) {
  51. furi_assert(context);
  52. SubGhzProtocolRAW* instance = context;
  53. if(subghz_file_encoder_worker_is_running(instance->file_worker_encoder)) {
  54. subghz_file_encoder_worker_stop(instance->file_worker_encoder);
  55. subghz_file_encoder_worker_free(instance->file_worker_encoder);
  56. instance->file_is_open = RAWFileIsOpenClose;
  57. }
  58. }
  59. bool subghz_protocol_raw_send_key(
  60. SubGhzProtocolRAW* instance,
  61. SubGhzProtocolCommonEncoder* encoder) {
  62. furi_assert(instance);
  63. furi_assert(encoder);
  64. bool loaded = false;
  65. instance->file_worker_encoder = subghz_file_encoder_worker_alloc();
  66. if(subghz_file_encoder_worker_start(
  67. instance->file_worker_encoder, string_get_cstr(instance->file_name))) {
  68. //the worker needs a file in order to open and read part of the file
  69. osDelay(100);
  70. instance->file_is_open = RAWFileIsOpenRead;
  71. subghz_protocol_encoder_common_set_callback(
  72. encoder, subghz_file_encoder_worker_get_level_duration, instance->file_worker_encoder);
  73. subghz_protocol_encoder_common_set_callback_end(
  74. encoder, subghz_protocol_raw_file_encoder_worker_stop, instance);
  75. loaded = true;
  76. } else {
  77. subghz_protocol_raw_file_encoder_worker_stop(instance);
  78. }
  79. return loaded;
  80. }
  81. void subghz_protocol_raw_reset(SubGhzProtocolRAW* instance) {
  82. instance->ind_write = 0;
  83. }
  84. void subghz_protocol_raw_parse(SubGhzProtocolRAW* instance, bool level, uint32_t duration) {
  85. if(instance->upload_raw != NULL) {
  86. if(duration > instance->common.te_short) {
  87. if(duration > instance->common.te_long) duration = instance->common.te_long;
  88. if(instance->last_level != level) {
  89. instance->last_level = (level ? true : false);
  90. instance->upload_raw[instance->ind_write++] = (level ? duration : -duration);
  91. }
  92. }
  93. if(instance->ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE) {
  94. subghz_protocol_raw_save_to_file_write(instance);
  95. }
  96. }
  97. }
  98. void subghz_protocol_raw_to_str(SubGhzProtocolRAW* instance, string_t output) {
  99. string_cat_printf(output, "RAW Date");
  100. }
  101. const char* subghz_protocol_get_last_file_name(SubGhzProtocolRAW* instance) {
  102. return string_get_cstr(instance->file_name);
  103. }
  104. void subghz_protocol_set_last_file_name(SubGhzProtocolRAW* instance, const char* name) {
  105. string_printf(instance->file_name, "%s", name);
  106. }
  107. bool subghz_protocol_raw_save_to_file_init(
  108. SubGhzProtocolRAW* instance,
  109. const char* dev_name,
  110. uint32_t frequency,
  111. FuriHalSubGhzPreset preset) {
  112. furi_assert(instance);
  113. string_t dev_file_name;
  114. string_init(dev_file_name);
  115. string_t temp_str;
  116. string_init(temp_str);
  117. bool init = false;
  118. do {
  119. // Create subghz folder directory if necessary
  120. if(!file_worker_mkdir(instance->file_worker, SUBGHZ_RAW_FOLDER)) {
  121. break;
  122. }
  123. // Create saved directory if necessary
  124. if(!file_worker_mkdir(instance->file_worker, SUBGHZ_RAW_PATH_FOLDER)) {
  125. break;
  126. }
  127. string_set(instance->file_name, dev_name);
  128. // First remove subghz device file if it was saved
  129. string_printf(
  130. dev_file_name, "%s/%s%s", SUBGHZ_APP_PATH_FOLDER, dev_name, SUBGHZ_APP_EXTENSION);
  131. if(!file_worker_remove(instance->file_worker, string_get_cstr(dev_file_name))) {
  132. break;
  133. }
  134. // Open file
  135. if(!file_worker_open(
  136. instance->file_worker, string_get_cstr(dev_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  137. break;
  138. }
  139. // //get the name of the next free file
  140. // file_worker_get_next_filename(
  141. // instance->file_worker,
  142. // SUBGHZ_RAW_PATH_FOLDER,
  143. // dev_name,
  144. // SUBGHZ_APP_EXTENSION,
  145. // temp_str);
  146. // string_set(instance->file_name, temp_str);
  147. // string_printf(
  148. // dev_file_name,
  149. // "%s/%s%s",
  150. // SUBGHZ_RAW_PATH_FOLDER,
  151. // string_get_cstr(temp_str),
  152. // SUBGHZ_APP_EXTENSION);
  153. // // Open file
  154. // if(!file_worker_open(
  155. // instance->file_worker,
  156. // string_get_cstr(dev_file_name),
  157. // FSAM_WRITE,
  158. // FSOM_CREATE_ALWAYS)) {
  159. // break;
  160. // }
  161. //Get string frequency preset protocol
  162. string_printf(
  163. temp_str,
  164. "Frequency: %d\n"
  165. "Preset: %d\n"
  166. "Protocol: RAW\n",
  167. (int)frequency,
  168. (int)preset);
  169. if(!file_worker_write(
  170. instance->file_worker, string_get_cstr(temp_str), string_size(temp_str))) {
  171. break;
  172. }
  173. instance->upload_raw = furi_alloc(SUBGHZ_DOWNLOAD_MAX_SIZE * sizeof(uint16_t));
  174. instance->file_is_open = RAWFileIsOpenWrite;
  175. instance->sample_write = 0;
  176. init = true;
  177. } while(0);
  178. string_clear(temp_str);
  179. string_clear(dev_file_name);
  180. return init;
  181. }
  182. void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolRAW* instance) {
  183. furi_assert(instance);
  184. if(instance->file_is_open == RAWFileIsOpenWrite && instance->ind_write)
  185. subghz_protocol_raw_save_to_file_write(instance);
  186. if(instance->file_is_open != RAWFileIsOpenClose) {
  187. free(instance->upload_raw);
  188. instance->upload_raw = NULL;
  189. }
  190. file_worker_close(instance->file_worker);
  191. instance->file_is_open = RAWFileIsOpenClose;
  192. }
  193. bool subghz_protocol_raw_save_to_file_write(SubGhzProtocolRAW* instance) {
  194. furi_assert(instance);
  195. string_t temp_str;
  196. string_init(temp_str);
  197. bool is_write = false;
  198. if(instance->file_is_open == RAWFileIsOpenWrite) {
  199. do {
  200. string_printf(temp_str, "RAW_Data: ");
  201. if(!file_worker_write(
  202. instance->file_worker, string_get_cstr(temp_str), string_size(temp_str))) {
  203. break;
  204. }
  205. for(size_t i = 0; i < instance->ind_write - 1; i++) {
  206. string_printf(temp_str, "%d, ", instance->upload_raw[i]);
  207. if(!file_worker_write(
  208. instance->file_worker, string_get_cstr(temp_str), string_size(temp_str))) {
  209. break;
  210. }
  211. }
  212. string_printf(temp_str, "%d\n", instance->upload_raw[instance->ind_write - 1]);
  213. if(!file_worker_write(
  214. instance->file_worker, string_get_cstr(temp_str), string_size(temp_str))) {
  215. break;
  216. }
  217. instance->sample_write += instance->ind_write;
  218. instance->ind_write = 0;
  219. is_write = true;
  220. } while(0);
  221. string_clear(temp_str);
  222. }
  223. return is_write;
  224. }
  225. size_t subghz_protocol_raw_get_sample_write(SubGhzProtocolRAW* instance) {
  226. return instance->sample_write + instance->ind_write;
  227. }
  228. bool subghz_protocol_raw_to_load_protocol_from_file(
  229. FileWorker* file_worker,
  230. SubGhzProtocolRAW* instance,
  231. const char* file_path) {
  232. subghz_protocol_set_last_file_name(instance, file_path);
  233. return true;
  234. }