raw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. SubGhzPesetDefinition* preset) {
  70. furi_assert(instance);
  71. instance->storage = furi_record_open(RECORD_STORAGE);
  72. instance->flipper_file = flipper_format_file_alloc(instance->storage);
  73. string_t temp_str;
  74. string_init(temp_str);
  75. bool init = false;
  76. do {
  77. // Create subghz folder directory if necessary
  78. if(!storage_simply_mkdir(instance->storage, SUBGHZ_RAW_FOLDER)) {
  79. break;
  80. }
  81. // Create saved directory if necessary
  82. if(!storage_simply_mkdir(instance->storage, SUBGHZ_RAW_FOLDER)) {
  83. break;
  84. }
  85. string_set(instance->file_name, dev_name);
  86. // First remove subghz device file if it was saved
  87. string_printf(temp_str, "%s/%s%s", SUBGHZ_RAW_FOLDER, dev_name, SUBGHZ_APP_EXTENSION);
  88. if(!storage_simply_remove(instance->storage, string_get_cstr(temp_str))) {
  89. break;
  90. }
  91. // Open file
  92. if(!flipper_format_file_open_always(instance->flipper_file, string_get_cstr(temp_str))) {
  93. FURI_LOG_E(TAG, "Unable to open file for write: %s", temp_str);
  94. break;
  95. }
  96. if(!flipper_format_write_header_cstr(
  97. instance->flipper_file, SUBGHZ_RAW_FILE_TYPE, SUBGHZ_RAW_FILE_VERSION)) {
  98. FURI_LOG_E(TAG, "Unable to add header");
  99. break;
  100. }
  101. if(!flipper_format_write_uint32(
  102. instance->flipper_file, "Frequency", &preset->frequency, 1)) {
  103. FURI_LOG_E(TAG, "Unable to add Frequency");
  104. break;
  105. }
  106. subghz_block_generic_get_preset_name(string_get_cstr(preset->name), temp_str);
  107. if(!flipper_format_write_string_cstr(
  108. instance->flipper_file, "Preset", string_get_cstr(temp_str))) {
  109. FURI_LOG_E(TAG, "Unable to add Preset");
  110. break;
  111. }
  112. if(!strcmp(string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
  113. if(!flipper_format_write_string_cstr(
  114. instance->flipper_file, "Custom_preset_module", "CC1101")) {
  115. FURI_LOG_E(TAG, "Unable to add Custom_preset_module");
  116. break;
  117. }
  118. if(!flipper_format_write_hex(
  119. instance->flipper_file, "Custom_preset_data", preset->data, preset->data_size)) {
  120. FURI_LOG_E(TAG, "Unable to add Custom_preset_data");
  121. break;
  122. }
  123. }
  124. if(!flipper_format_write_string_cstr(
  125. instance->flipper_file, "Protocol", instance->base.protocol->name)) {
  126. FURI_LOG_E(TAG, "Unable to add Protocol");
  127. break;
  128. }
  129. instance->upload_raw = malloc(SUBGHZ_DOWNLOAD_MAX_SIZE * sizeof(int32_t));
  130. instance->file_is_open = RAWFileIsOpenWrite;
  131. instance->sample_write = 0;
  132. init = true;
  133. } while(0);
  134. string_clear(temp_str);
  135. return init;
  136. }
  137. static bool subghz_protocol_raw_save_to_file_write(SubGhzProtocolDecoderRAW* instance) {
  138. furi_assert(instance);
  139. bool is_write = false;
  140. if(instance->file_is_open == RAWFileIsOpenWrite) {
  141. if(!flipper_format_write_int32(
  142. instance->flipper_file, "RAW_Data", instance->upload_raw, instance->ind_write)) {
  143. FURI_LOG_E(TAG, "Unable to add RAW_Data");
  144. } else {
  145. instance->sample_write += instance->ind_write;
  146. instance->ind_write = 0;
  147. is_write = true;
  148. }
  149. }
  150. return is_write;
  151. }
  152. void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolDecoderRAW* instance) {
  153. furi_assert(instance);
  154. if(instance->file_is_open == RAWFileIsOpenWrite && instance->ind_write)
  155. subghz_protocol_raw_save_to_file_write(instance);
  156. if(instance->file_is_open != RAWFileIsOpenClose) {
  157. free(instance->upload_raw);
  158. instance->upload_raw = NULL;
  159. flipper_format_file_close(instance->flipper_file);
  160. flipper_format_free(instance->flipper_file);
  161. furi_record_close(RECORD_STORAGE);
  162. }
  163. instance->file_is_open = RAWFileIsOpenClose;
  164. }
  165. size_t subghz_protocol_raw_get_sample_write(SubGhzProtocolDecoderRAW* instance) {
  166. return instance->sample_write + instance->ind_write;
  167. }
  168. void* subghz_protocol_decoder_raw_alloc(SubGhzEnvironment* environment) {
  169. UNUSED(environment);
  170. SubGhzProtocolDecoderRAW* instance = malloc(sizeof(SubGhzProtocolDecoderRAW));
  171. instance->base.protocol = &subghz_protocol_raw;
  172. instance->upload_raw = NULL;
  173. instance->ind_write = 0;
  174. instance->last_level = false;
  175. instance->file_is_open = RAWFileIsOpenClose;
  176. string_init(instance->file_name);
  177. return instance;
  178. }
  179. void subghz_protocol_decoder_raw_free(void* context) {
  180. furi_assert(context);
  181. SubGhzProtocolDecoderRAW* instance = context;
  182. string_clear(instance->file_name);
  183. free(instance);
  184. }
  185. void subghz_protocol_decoder_raw_reset(void* context) {
  186. furi_assert(context);
  187. SubGhzProtocolDecoderRAW* instance = context;
  188. instance->ind_write = 0;
  189. instance->last_level = false;
  190. }
  191. void subghz_protocol_decoder_raw_feed(void* context, bool level, uint32_t duration) {
  192. furi_assert(context);
  193. SubGhzProtocolDecoderRAW* instance = context;
  194. if(instance->upload_raw != NULL) {
  195. if(duration > subghz_protocol_raw_const.te_short) {
  196. if(instance->last_level != level) {
  197. instance->last_level = (level ? true : false);
  198. instance->upload_raw[instance->ind_write++] = (level ? duration : -duration);
  199. }
  200. }
  201. if(instance->ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE) {
  202. subghz_protocol_raw_save_to_file_write(instance);
  203. }
  204. }
  205. }
  206. void subghz_protocol_decoder_raw_get_string(void* context, string_t output) {
  207. furi_assert(context);
  208. //SubGhzProtocolDecoderRAW* instance = context;
  209. //ToDo no use
  210. string_cat_printf(output, "RAW Date");
  211. }
  212. void* subghz_protocol_encoder_raw_alloc(SubGhzEnvironment* environment) {
  213. UNUSED(environment);
  214. SubGhzProtocolEncoderRAW* instance = malloc(sizeof(SubGhzProtocolEncoderRAW));
  215. instance->base.protocol = &subghz_protocol_raw;
  216. string_init(instance->file_name);
  217. instance->is_runing = false;
  218. return instance;
  219. }
  220. void subghz_protocol_encoder_raw_stop(void* context) {
  221. SubGhzProtocolEncoderRAW* instance = context;
  222. instance->is_runing = false;
  223. if(subghz_file_encoder_worker_is_running(instance->file_worker_encoder)) {
  224. subghz_file_encoder_worker_stop(instance->file_worker_encoder);
  225. subghz_file_encoder_worker_free(instance->file_worker_encoder);
  226. }
  227. }
  228. void subghz_protocol_encoder_raw_free(void* context) {
  229. furi_assert(context);
  230. SubGhzProtocolEncoderRAW* instance = context;
  231. subghz_protocol_encoder_raw_stop(instance);
  232. string_clear(instance->file_name);
  233. free(instance);
  234. }
  235. void subghz_protocol_raw_file_encoder_worker_set_callback_end(
  236. SubGhzProtocolEncoderRAW* instance,
  237. SubGhzProtocolEncoderRAWCallbackEnd callback_end,
  238. void* context_end) {
  239. furi_assert(instance);
  240. furi_assert(callback_end);
  241. subghz_file_encoder_worker_callback_end(
  242. instance->file_worker_encoder, callback_end, context_end);
  243. }
  244. static bool subghz_protocol_encoder_raw_worker_init(SubGhzProtocolEncoderRAW* instance) {
  245. furi_assert(instance);
  246. instance->file_worker_encoder = subghz_file_encoder_worker_alloc();
  247. if(subghz_file_encoder_worker_start(
  248. instance->file_worker_encoder, string_get_cstr(instance->file_name))) {
  249. //the worker needs a file in order to open and read part of the file
  250. furi_delay_ms(100);
  251. instance->is_runing = true;
  252. } else {
  253. subghz_protocol_encoder_raw_stop(instance);
  254. }
  255. return instance->is_runing;
  256. }
  257. void subghz_protocol_raw_gen_fff_data(FlipperFormat* flipper_format, const char* file_path) {
  258. do {
  259. stream_clean(flipper_format_get_raw_stream(flipper_format));
  260. if(!flipper_format_write_string_cstr(flipper_format, "Protocol", "RAW")) {
  261. FURI_LOG_E(TAG, "Unable to add Protocol");
  262. break;
  263. }
  264. if(!flipper_format_write_string_cstr(flipper_format, "File_name", file_path)) {
  265. FURI_LOG_E(TAG, "Unable to add File_name");
  266. break;
  267. }
  268. } while(false);
  269. }
  270. bool subghz_protocol_encoder_raw_deserialize(void* context, FlipperFormat* flipper_format) {
  271. furi_assert(context);
  272. SubGhzProtocolEncoderRAW* instance = context;
  273. bool res = false;
  274. string_t temp_str;
  275. string_init(temp_str);
  276. do {
  277. if(!flipper_format_rewind(flipper_format)) {
  278. FURI_LOG_E(TAG, "Rewind error");
  279. break;
  280. }
  281. if(!flipper_format_read_string(flipper_format, "File_name", temp_str)) {
  282. FURI_LOG_E(TAG, "Missing File_name");
  283. break;
  284. }
  285. string_set(instance->file_name, temp_str);
  286. res = subghz_protocol_encoder_raw_worker_init(instance);
  287. } while(false);
  288. string_clear(temp_str);
  289. return res;
  290. }
  291. LevelDuration subghz_protocol_encoder_raw_yield(void* context) {
  292. SubGhzProtocolEncoderRAW* instance = context;
  293. if(!instance->is_runing) return level_duration_reset();
  294. return subghz_file_encoder_worker_get_level_duration(instance->file_worker_encoder);
  295. }