raw.c 12 KB

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