signal_file.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright (C) 2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * Copyright (C) 2023 Maciej Wojtasik -- All Rights Reserved
  3. * See the LICENSE file for information about the license. */
  4. #include "app.h"
  5. #include <stream/stream.h>
  6. #include <flipper_format/flipper_format_i.h>
  7. /* ========================= Signal file operations ========================= */
  8. /* This function saves the current logical signal on disk. What is saved here
  9. * is not the signal as level and duration as we received it from CC1101,
  10. * but it's logical representation stored in the app->msg_info bitmap, where
  11. * each 1 or 0 means a puls or gap for the specified short pulse duration time
  12. * (te). */
  13. bool save_signal(ProtoViewApp* app, const char* filename) {
  14. /* We have a message at all? */
  15. if(app->msg_info == NULL || app->msg_info->pulses_count == 0) return false;
  16. Storage* storage = furi_record_open(RECORD_STORAGE);
  17. FlipperFormat* file = flipper_format_file_alloc(storage);
  18. Stream* stream = flipper_format_get_raw_stream(file);
  19. FuriString* file_content = NULL;
  20. bool success = true;
  21. if(flipper_format_file_open_always(file, filename)) {
  22. /* Write the file header. */
  23. FuriString* file_content = furi_string_alloc();
  24. const char* preset_id = ProtoViewModulations[app->modulation].id;
  25. furi_string_printf(
  26. file_content,
  27. "Filetype: Flipper SubGhz RAW File\n"
  28. "Version: 1\n"
  29. "Frequency: %ld\n"
  30. "Preset: %s\n",
  31. app->frequency,
  32. preset_id ? preset_id : "FuriHalSubGhzPresetCustom");
  33. /* For custom modulations, we need to emit a set of registers. */
  34. if(preset_id == NULL) {
  35. FuriString* custom = furi_string_alloc();
  36. uint8_t* regs = ProtoViewModulations[app->modulation].custom;
  37. furi_string_printf(
  38. custom,
  39. "Custom_preset_module: CC1101\n"
  40. "Custom_preset_data: ");
  41. for(int j = 0; regs[j]; j += 2) {
  42. furi_string_cat_printf(custom, "%02X %02X ", (int)regs[j], (int)regs[j + 1]);
  43. }
  44. // Add patable
  45. furi_string_cat(custom, "00 00 C0 00 00 00 00 00 00 00 ");
  46. //size_t len = furi_string_size(file_content);
  47. //furi_string_set_char(custom, len - 1, '\n');
  48. furi_string_cat(custom, "\n");
  49. furi_string_cat(file_content, custom);
  50. furi_string_free(custom);
  51. }
  52. /* We always save raw files. */
  53. furi_string_cat_printf(
  54. file_content,
  55. "Protocol: RAW\n"
  56. "RAW_Data: -10000\n"); // Start with 10 ms of gap
  57. /* Write header. */
  58. size_t len = furi_string_size(file_content);
  59. if(stream_write(stream, (uint8_t*)furi_string_get_cstr(file_content), len) != len) {
  60. FURI_LOG_W(TAG, "Short write to file");
  61. success = false;
  62. goto write_err;
  63. }
  64. furi_string_reset(file_content);
  65. /* Write raw data sections. The Flipper subghz parser can't handle
  66. * too much data on a single line, so we generate a new one
  67. * every few samples. */
  68. uint32_t this_line_samples = 0;
  69. uint32_t max_line_samples = 100;
  70. uint32_t idx = 0; // Iindex in the signal bitmap.
  71. ProtoViewMsgInfo* i = app->msg_info;
  72. while(idx < i->pulses_count) {
  73. bool level = bitmap_get(i->bits, i->bits_bytes, idx);
  74. uint32_t te_times = 1;
  75. idx++;
  76. /* Count the duration of the current pulse/gap. */
  77. while(idx < i->pulses_count && bitmap_get(i->bits, i->bits_bytes, idx) == level) {
  78. te_times++;
  79. idx++;
  80. }
  81. // Invariant: after the loop 'idx' is at the start of the
  82. // next gap or pulse.
  83. int32_t dur = (int32_t)i->short_pulse_dur * te_times;
  84. if(level == 0) dur = -dur; /* Negative is gap in raw files. */
  85. /* Emit the sample. If this is the first sample of the line,
  86. * also emit the RAW_Data: field. */
  87. if(this_line_samples == 0) furi_string_cat_printf(file_content, "RAW_Data: ");
  88. furi_string_cat_printf(file_content, "%d ", (int)dur);
  89. this_line_samples++;
  90. /* Store the current set of samples on disk, when we reach a
  91. * given number or the end of the signal. */
  92. bool end_reached = (idx == i->pulses_count);
  93. if(this_line_samples == max_line_samples || end_reached) {
  94. /* If that's the end, terminate the signal with a long
  95. * gap. */
  96. if(end_reached) furi_string_cat_printf(file_content, "-10000 ");
  97. /* We always have a trailing space in the last sample. Make it
  98. * a newline. */
  99. size_t len = furi_string_size(file_content);
  100. furi_string_set_char(file_content, len - 1, '\n');
  101. if(stream_write(stream, (uint8_t*)furi_string_get_cstr(file_content), len) !=
  102. len) {
  103. FURI_LOG_W(TAG, "Short write to file");
  104. success = false;
  105. goto write_err;
  106. }
  107. /* Prepare for next line. */
  108. furi_string_reset(file_content);
  109. this_line_samples = 0;
  110. }
  111. }
  112. } else {
  113. success = false;
  114. FURI_LOG_W(TAG, "Unable to open file");
  115. }
  116. write_err:
  117. furi_record_close(RECORD_STORAGE);
  118. flipper_format_free(file);
  119. if(file_content != NULL) furi_string_free(file_content);
  120. return success;
  121. }