raw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "raw.h"
  2. #include <lib/flipper_format/flipper_format.h>
  3. #include "../subghz_file_encoder_worker.h"
  4. #include "../blocks/const.h"
  5. #include "../blocks/decoder.h"
  6. #include "../blocks/encoder.h"
  7. #include "../blocks/generic.h"
  8. #include "../blocks/math.h"
  9. #include <flipper_format/flipper_format_i.h>
  10. #include <lib/toolbox/stream/stream.h>
  11. #define TAG "SubGhzProtocolRAW"
  12. #define SUBGHZ_DOWNLOAD_MAX_SIZE 512
  13. static const SubGhzBlockConst subghz_protocol_raw_const = {
  14. .te_short = 50,
  15. .te_long = 32700,
  16. .te_delta = 0,
  17. .min_count_bit_for_found = 0,
  18. };
  19. struct SubGhzProtocolDecoderRAW {
  20. SubGhzProtocolDecoderBase base;
  21. int32_t* upload_raw;
  22. uint16_t ind_write;
  23. Storage* storage;
  24. FlipperFormat* flipper_file;
  25. uint32_t file_is_open;
  26. string_t file_name;
  27. size_t sample_write;
  28. bool last_level;
  29. };
  30. struct SubGhzProtocolEncoderRAW {
  31. SubGhzProtocolEncoderBase base;
  32. bool is_runing;
  33. string_t file_name;
  34. SubGhzFileEncoderWorker* file_worker_encoder;
  35. };
  36. typedef enum {
  37. RAWFileIsOpenClose = 0,
  38. RAWFileIsOpenWrite,
  39. RAWFileIsOpenRead,
  40. } RAWFilIsOpen;
  41. const SubGhzProtocolDecoder subghz_protocol_raw_decoder = {
  42. .alloc = subghz_protocol_decoder_raw_alloc,
  43. .free = subghz_protocol_decoder_raw_free,
  44. .feed = subghz_protocol_decoder_raw_feed,
  45. .reset = subghz_protocol_decoder_raw_reset,
  46. .get_hash_data = NULL,
  47. .serialize = NULL,
  48. .get_string = subghz_protocol_decoder_raw_get_string,
  49. };
  50. const SubGhzProtocolEncoder subghz_protocol_raw_encoder = {
  51. .alloc = subghz_protocol_encoder_raw_alloc,
  52. .free = subghz_protocol_encoder_raw_free,
  53. .deserialize = subghz_protocol_encoder_raw_deserialize,
  54. .stop = subghz_protocol_encoder_raw_stop,
  55. .yield = subghz_protocol_encoder_raw_yield,
  56. };
  57. const SubGhzProtocol subghz_protocol_raw = {
  58. .name = SUBGHZ_PROTOCOL_RAW_NAME,
  59. .type = SubGhzProtocolTypeRAW,
  60. .flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
  61. SubGhzProtocolFlag_AM | SubGhzProtocolFlag_FM | SubGhzProtocolFlag_RAW |
  62. SubGhzProtocolFlag_Load | SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
  63. .decoder = &subghz_protocol_raw_decoder,
  64. .encoder = &subghz_protocol_raw_encoder,
  65. };
  66. bool subghz_protocol_raw_save_to_file_init(
  67. SubGhzProtocolDecoderRAW* instance,
  68. const char* dev_name,
  69. uint32_t frequency,
  70. FuriHalSubGhzPreset preset) {
  71. furi_assert(instance);
  72. instance->storage = furi_record_open("storage");
  73. instance->flipper_file = flipper_format_file_alloc(instance->storage);
  74. string_t temp_str;
  75. string_init(temp_str);
  76. bool init = false;
  77. do {
  78. // Create subghz folder directory if necessary
  79. if(!storage_simply_mkdir(instance->storage, SUBGHZ_RAW_FOLDER)) {
  80. break;
  81. }
  82. // Create saved directory if necessary
  83. if(!storage_simply_mkdir(instance->storage, SUBGHZ_RAW_FOLDER)) {
  84. break;
  85. }
  86. string_set(instance->file_name, dev_name);
  87. // First remove subghz device file if it was saved
  88. string_printf(temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, dev_name, SUBGHZ_APP_EXTENSION);
  89. if(!storage_simply_remove(instance->storage, string_get_cstr(temp_str))) {
  90. break;
  91. }
  92. // Open file
  93. if(!flipper_format_file_open_always(instance->flipper_file, string_get_cstr(temp_str))) {
  94. FURI_LOG_E(TAG, "Unable to open file for write: %s", temp_str);
  95. break;
  96. }
  97. if(!flipper_format_write_header_cstr(
  98. instance->flipper_file, SUBGHZ_RAW_FILE_TYPE, SUBGHZ_RAW_FILE_VERSION)) {
  99. FURI_LOG_E(TAG, "Unable to add header");
  100. break;
  101. }
  102. if(!flipper_format_write_uint32(instance->flipper_file, "Frequency", &frequency, 1)) {
  103. FURI_LOG_E(TAG, "Unable to add Frequency");
  104. break;
  105. }
  106. if(!subghz_block_generic_get_preset_name(preset, temp_str)) {
  107. break;
  108. }
  109. if(!flipper_format_write_string_cstr(
  110. instance->flipper_file, "Preset", string_get_cstr(temp_str))) {
  111. FURI_LOG_E(TAG, "Unable to add Preset");
  112. break;
  113. }
  114. if(!flipper_format_write_string_cstr(
  115. instance->flipper_file, "Protocol", instance->base.protocol->name)) {
  116. FURI_LOG_E(TAG, "Unable to add Protocol");
  117. break;
  118. }
  119. instance->upload_raw = malloc(SUBGHZ_DOWNLOAD_MAX_SIZE * sizeof(int32_t));
  120. instance->file_is_open = RAWFileIsOpenWrite;
  121. instance->sample_write = 0;
  122. init = true;
  123. } while(0);
  124. string_clear(temp_str);
  125. return init;
  126. }
  127. static bool subghz_protocol_raw_save_to_file_write(SubGhzProtocolDecoderRAW* instance) {
  128. furi_assert(instance);
  129. bool is_write = false;
  130. if(instance->file_is_open == RAWFileIsOpenWrite) {
  131. if(!flipper_format_write_int32(
  132. instance->flipper_file, "RAW_Data", instance->upload_raw, instance->ind_write)) {
  133. FURI_LOG_E(TAG, "Unable to add RAW_Data");
  134. } else {
  135. instance->sample_write += instance->ind_write;
  136. instance->ind_write = 0;
  137. is_write = true;
  138. }
  139. }
  140. return is_write;
  141. }
  142. void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolDecoderRAW* instance) {
  143. furi_assert(instance);
  144. if(instance->file_is_open == RAWFileIsOpenWrite && instance->ind_write)
  145. subghz_protocol_raw_save_to_file_write(instance);
  146. if(instance->file_is_open != RAWFileIsOpenClose) {
  147. free(instance->upload_raw);
  148. instance->upload_raw = NULL;
  149. flipper_format_file_close(instance->flipper_file);
  150. flipper_format_free(instance->flipper_file);
  151. furi_record_close("storage");
  152. }
  153. instance->file_is_open = RAWFileIsOpenClose;
  154. }
  155. size_t subghz_protocol_raw_get_sample_write(SubGhzProtocolDecoderRAW* instance) {
  156. return instance->sample_write + instance->ind_write;
  157. }
  158. void* subghz_protocol_decoder_raw_alloc(SubGhzEnvironment* environment) {
  159. UNUSED(environment);
  160. SubGhzProtocolDecoderRAW* instance = malloc(sizeof(SubGhzProtocolDecoderRAW));
  161. instance->base.protocol = &subghz_protocol_raw;
  162. instance->upload_raw = NULL;
  163. instance->ind_write = 0;
  164. instance->last_level = false;
  165. instance->file_is_open = RAWFileIsOpenClose;
  166. string_init(instance->file_name);
  167. return instance;
  168. }
  169. void subghz_protocol_decoder_raw_free(void* context) {
  170. furi_assert(context);
  171. SubGhzProtocolDecoderRAW* instance = context;
  172. string_clear(instance->file_name);
  173. free(instance);
  174. }
  175. void subghz_protocol_decoder_raw_reset(void* context) {
  176. furi_assert(context);
  177. SubGhzProtocolDecoderRAW* instance = context;
  178. instance->ind_write = 0;
  179. instance->last_level = false;
  180. }
  181. void subghz_protocol_decoder_raw_feed(void* context, bool level, uint32_t duration) {
  182. furi_assert(context);
  183. SubGhzProtocolDecoderRAW* instance = context;
  184. if(instance->upload_raw != NULL) {
  185. if(duration > subghz_protocol_raw_const.te_short) {
  186. if(instance->last_level != level) {
  187. instance->last_level = (level ? true : false);
  188. instance->upload_raw[instance->ind_write++] = (level ? duration : -duration);
  189. }
  190. }
  191. if(instance->ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE) {
  192. subghz_protocol_raw_save_to_file_write(instance);
  193. }
  194. }
  195. }
  196. void subghz_protocol_decoder_raw_get_string(void* context, string_t output) {
  197. furi_assert(context);
  198. //SubGhzProtocolDecoderRAW* instance = context;
  199. //ToDo no use
  200. string_cat_printf(output, "RAW Date");
  201. }
  202. void* subghz_protocol_encoder_raw_alloc(SubGhzEnvironment* environment) {
  203. UNUSED(environment);
  204. SubGhzProtocolEncoderRAW* instance = malloc(sizeof(SubGhzProtocolEncoderRAW));
  205. instance->base.protocol = &subghz_protocol_raw;
  206. string_init(instance->file_name);
  207. instance->is_runing = false;
  208. return instance;
  209. }
  210. void subghz_protocol_encoder_raw_stop(void* context) {
  211. SubGhzProtocolEncoderRAW* instance = context;
  212. instance->is_runing = false;
  213. if(subghz_file_encoder_worker_is_running(instance->file_worker_encoder)) {
  214. subghz_file_encoder_worker_stop(instance->file_worker_encoder);
  215. subghz_file_encoder_worker_free(instance->file_worker_encoder);
  216. }
  217. }
  218. void subghz_protocol_encoder_raw_free(void* context) {
  219. furi_assert(context);
  220. SubGhzProtocolEncoderRAW* instance = context;
  221. subghz_protocol_encoder_raw_stop(instance);
  222. string_clear(instance->file_name);
  223. free(instance);
  224. }
  225. void subghz_protocol_raw_file_encoder_worker_set_callback_end(
  226. SubGhzProtocolEncoderRAW* instance,
  227. SubGhzProtocolEncoderRAWCallbackEnd callback_end,
  228. void* context_end) {
  229. furi_assert(instance);
  230. furi_assert(callback_end);
  231. subghz_file_encoder_worker_callback_end(
  232. instance->file_worker_encoder, callback_end, context_end);
  233. }
  234. static bool subghz_protocol_encoder_raw_worker_init(SubGhzProtocolEncoderRAW* instance) {
  235. furi_assert(instance);
  236. instance->file_worker_encoder = subghz_file_encoder_worker_alloc();
  237. if(subghz_file_encoder_worker_start(
  238. instance->file_worker_encoder, string_get_cstr(instance->file_name))) {
  239. //the worker needs a file in order to open and read part of the file
  240. osDelay(100);
  241. instance->is_runing = true;
  242. } else {
  243. subghz_protocol_encoder_raw_stop(instance);
  244. }
  245. return instance->is_runing;
  246. }
  247. void subghz_protocol_raw_gen_fff_data(FlipperFormat* flipper_format, const char* file_path) {
  248. do {
  249. stream_clean(flipper_format_get_raw_stream(flipper_format));
  250. if(!flipper_format_write_string_cstr(flipper_format, "Protocol", "RAW")) {
  251. FURI_LOG_E(TAG, "Unable to add Protocol");
  252. break;
  253. }
  254. if(!flipper_format_write_string_cstr(flipper_format, "File_name", file_path)) {
  255. FURI_LOG_E(TAG, "Unable to add File_name");
  256. break;
  257. }
  258. } while(false);
  259. }
  260. bool subghz_protocol_encoder_raw_deserialize(void* context, FlipperFormat* flipper_format) {
  261. furi_assert(context);
  262. SubGhzProtocolEncoderRAW* instance = context;
  263. bool res = false;
  264. string_t temp_str;
  265. string_init(temp_str);
  266. do {
  267. if(!flipper_format_rewind(flipper_format)) {
  268. FURI_LOG_E(TAG, "Rewind error");
  269. break;
  270. }
  271. if(!flipper_format_read_string(flipper_format, "File_name", temp_str)) {
  272. FURI_LOG_E(TAG, "Missing File_name");
  273. break;
  274. }
  275. string_set(instance->file_name, temp_str);
  276. res = subghz_protocol_encoder_raw_worker_init(instance);
  277. } while(false);
  278. string_clear(temp_str);
  279. return res;
  280. }
  281. LevelDuration subghz_protocol_encoder_raw_yield(void* context) {
  282. SubGhzProtocolEncoderRAW* instance = context;
  283. if(!instance->is_runing) return level_duration_reset();
  284. return subghz_file_encoder_worker_get_level_duration(instance->file_worker_encoder);
  285. }