raw.c 12 KB

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