raw.c 12 KB

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