raw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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->last_level = false;
  136. instance->pause = false;
  137. init = true;
  138. } while(0);
  139. furi_string_free(temp_str);
  140. return init;
  141. }
  142. static bool subghz_protocol_raw_save_to_file_write(SubGhzProtocolDecoderRAW* instance) {
  143. furi_assert(instance);
  144. bool is_write = false;
  145. if(instance->file_is_open == RAWFileIsOpenWrite) {
  146. if(!flipper_format_write_int32(
  147. instance->flipper_file, "RAW_Data", instance->upload_raw, instance->ind_write)) {
  148. FURI_LOG_E(TAG, "Unable to add RAW_Data");
  149. } else {
  150. instance->sample_write += instance->ind_write;
  151. instance->ind_write = 0;
  152. is_write = true;
  153. }
  154. }
  155. return is_write;
  156. }
  157. void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolDecoderRAW* instance) {
  158. furi_assert(instance);
  159. if(instance->file_is_open == RAWFileIsOpenWrite && instance->ind_write)
  160. subghz_protocol_raw_save_to_file_write(instance);
  161. if(instance->file_is_open != RAWFileIsOpenClose) {
  162. free(instance->upload_raw);
  163. instance->upload_raw = NULL;
  164. flipper_format_file_close(instance->flipper_file);
  165. flipper_format_free(instance->flipper_file);
  166. furi_record_close(RECORD_STORAGE);
  167. }
  168. instance->file_is_open = RAWFileIsOpenClose;
  169. }
  170. void subghz_protocol_raw_save_to_file_pause(SubGhzProtocolDecoderRAW* instance, bool pause) {
  171. furi_assert(instance);
  172. if(instance->pause != pause) {
  173. instance->pause = pause;
  174. }
  175. }
  176. size_t subghz_protocol_raw_get_sample_write(SubGhzProtocolDecoderRAW* instance) {
  177. return instance->sample_write + instance->ind_write;
  178. }
  179. void* subghz_protocol_decoder_raw_alloc(SubGhzEnvironment* environment) {
  180. UNUSED(environment);
  181. SubGhzProtocolDecoderRAW* instance = malloc(sizeof(SubGhzProtocolDecoderRAW));
  182. instance->base.protocol = &subghz_protocol_raw;
  183. instance->upload_raw = NULL;
  184. instance->ind_write = 0;
  185. instance->last_level = false;
  186. instance->file_is_open = RAWFileIsOpenClose;
  187. instance->file_name = furi_string_alloc();
  188. return instance;
  189. }
  190. void subghz_protocol_decoder_raw_free(void* context) {
  191. furi_assert(context);
  192. SubGhzProtocolDecoderRAW* instance = context;
  193. furi_string_free(instance->file_name);
  194. free(instance);
  195. }
  196. void subghz_protocol_decoder_raw_reset(void* context) {
  197. furi_assert(context);
  198. SubGhzProtocolDecoderRAW* instance = context;
  199. instance->ind_write = 0;
  200. instance->last_level = false;
  201. }
  202. void subghz_protocol_decoder_raw_feed(void* context, bool level, uint32_t duration) {
  203. furi_assert(context);
  204. SubGhzProtocolDecoderRAW* instance = context;
  205. if(!instance->pause && (instance->upload_raw != NULL)) {
  206. if(duration > subghz_protocol_raw_const.te_short) {
  207. if(instance->last_level != level) {
  208. instance->last_level = (level ? true : false);
  209. instance->upload_raw[instance->ind_write++] = (level ? duration : -duration);
  210. }
  211. }
  212. if(instance->ind_write == SUBGHZ_DOWNLOAD_MAX_SIZE) {
  213. subghz_protocol_raw_save_to_file_write(instance);
  214. }
  215. }
  216. }
  217. SubGhzProtocolStatus
  218. subghz_protocol_decoder_raw_deserialize(void* context, FlipperFormat* flipper_format) {
  219. furi_assert(context);
  220. UNUSED(context);
  221. UNUSED(flipper_format);
  222. //ToDo stub, for backwards compatibility
  223. return SubGhzProtocolStatusOk;
  224. }
  225. void subghz_protocol_decoder_raw_get_string(void* context, FuriString* output) {
  226. furi_assert(context);
  227. //SubGhzProtocolDecoderRAW* instance = context;
  228. UNUSED(context);
  229. //ToDo no use
  230. furi_string_cat_printf(output, "RAW Date");
  231. }
  232. void* subghz_protocol_encoder_raw_alloc(SubGhzEnvironment* environment) {
  233. UNUSED(environment);
  234. SubGhzProtocolEncoderRAW* instance = malloc(sizeof(SubGhzProtocolEncoderRAW));
  235. instance->base.protocol = &subghz_protocol_raw;
  236. instance->file_name = furi_string_alloc();
  237. instance->is_running = false;
  238. return instance;
  239. }
  240. void subghz_protocol_encoder_raw_stop(void* context) {
  241. SubGhzProtocolEncoderRAW* instance = context;
  242. instance->is_running = false;
  243. if(subghz_file_encoder_worker_is_running(instance->file_worker_encoder)) {
  244. subghz_file_encoder_worker_stop(instance->file_worker_encoder);
  245. subghz_file_encoder_worker_free(instance->file_worker_encoder);
  246. }
  247. }
  248. void subghz_protocol_encoder_raw_free(void* context) {
  249. furi_assert(context);
  250. SubGhzProtocolEncoderRAW* instance = context;
  251. subghz_protocol_encoder_raw_stop(instance);
  252. furi_string_free(instance->file_name);
  253. free(instance);
  254. }
  255. void subghz_protocol_raw_file_encoder_worker_set_callback_end(
  256. SubGhzProtocolEncoderRAW* instance,
  257. SubGhzProtocolEncoderRAWCallbackEnd callback_end,
  258. void* context_end) {
  259. furi_assert(instance);
  260. furi_assert(callback_end);
  261. subghz_file_encoder_worker_callback_end(
  262. instance->file_worker_encoder, callback_end, context_end);
  263. }
  264. static bool subghz_protocol_encoder_raw_worker_init(SubGhzProtocolEncoderRAW* instance) {
  265. furi_assert(instance);
  266. instance->file_worker_encoder = subghz_file_encoder_worker_alloc();
  267. if(subghz_file_encoder_worker_start(
  268. instance->file_worker_encoder, furi_string_get_cstr(instance->file_name))) {
  269. //the worker needs a file in order to open and read part of the file
  270. furi_delay_ms(100);
  271. instance->is_running = true;
  272. } else {
  273. subghz_protocol_encoder_raw_stop(instance);
  274. }
  275. return instance->is_running;
  276. }
  277. void subghz_protocol_raw_gen_fff_data(FlipperFormat* flipper_format, const char* file_path) {
  278. do {
  279. stream_clean(flipper_format_get_raw_stream(flipper_format));
  280. if(!flipper_format_write_string_cstr(flipper_format, "Protocol", "RAW")) {
  281. FURI_LOG_E(TAG, "Unable to add Protocol");
  282. break;
  283. }
  284. if(!flipper_format_write_string_cstr(flipper_format, "File_name", file_path)) {
  285. FURI_LOG_E(TAG, "Unable to add File_name");
  286. break;
  287. }
  288. } while(false);
  289. }
  290. SubGhzProtocolStatus
  291. subghz_protocol_encoder_raw_deserialize(void* context, FlipperFormat* flipper_format) {
  292. furi_assert(context);
  293. SubGhzProtocolEncoderRAW* instance = context;
  294. SubGhzProtocolStatus res = SubGhzProtocolStatusError;
  295. FuriString* temp_str;
  296. temp_str = furi_string_alloc();
  297. do {
  298. if(!flipper_format_rewind(flipper_format)) {
  299. FURI_LOG_E(TAG, "Rewind error");
  300. res = SubGhzProtocolStatusErrorParserOthers;
  301. break;
  302. }
  303. if(!flipper_format_read_string(flipper_format, "File_name", temp_str)) {
  304. FURI_LOG_E(TAG, "Missing File_name");
  305. res = SubGhzProtocolStatusErrorParserOthers;
  306. break;
  307. }
  308. furi_string_set(instance->file_name, temp_str);
  309. if(!subghz_protocol_encoder_raw_worker_init(instance)) {
  310. res = SubGhzProtocolStatusErrorEncoderGetUpload;
  311. break;
  312. }
  313. res = SubGhzProtocolStatusOk;
  314. } while(false);
  315. furi_string_free(temp_str);
  316. return res;
  317. }
  318. LevelDuration subghz_protocol_encoder_raw_yield(void* context) {
  319. SubGhzProtocolEncoderRAW* instance = context;
  320. if(!instance->is_running) return level_duration_reset();
  321. return subghz_file_encoder_worker_get_level_duration(instance->file_worker_encoder);
  322. }