signal_file.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /* We will know the size of the preset data once we reach the end
  42. * of the registers (null address). For now it's INT_MAX. */
  43. int preset_data_size = INT_MAX;
  44. bool patable_reached = false;
  45. for(int j = 0; j <= preset_data_size; j += 2) {
  46. // End reached, set the size to write the remaining 8 bytes (PATABLE)
  47. if(!patable_reached && regs[j] == 0) {
  48. preset_data_size = j + 8;
  49. patable_reached = true;
  50. }
  51. furi_string_cat_printf(custom, "%02X %02X ", (int)regs[j], (int)regs[j + 1]);
  52. }
  53. size_t len = furi_string_size(custom);
  54. furi_string_set_char(custom, len - 1, '\n');
  55. furi_string_cat(file_content, custom);
  56. furi_string_free(custom);
  57. }
  58. /* We always save raw files. */
  59. furi_string_cat_printf(
  60. file_content,
  61. "Protocol: RAW\n"
  62. "RAW_Data: -10000\n"); // Start with 10 ms of gap
  63. /* Write header. */
  64. size_t len = furi_string_size(file_content);
  65. if(stream_write(stream, (uint8_t*)furi_string_get_cstr(file_content), len) != len) {
  66. FURI_LOG_W(TAG, "Short write to file");
  67. success = false;
  68. goto write_err;
  69. }
  70. furi_string_reset(file_content);
  71. /* Write raw data sections. The Flipper subghz parser can't handle
  72. * too much data on a single line, so we generate a new one
  73. * every few samples. */
  74. uint32_t this_line_samples = 0;
  75. uint32_t max_line_samples = 100;
  76. uint32_t idx = 0; // Iindex in the signal bitmap.
  77. ProtoViewMsgInfo* i = app->msg_info;
  78. while(idx < i->pulses_count) {
  79. bool level = bitmap_get(i->bits, i->bits_bytes, idx);
  80. uint32_t te_times = 1;
  81. idx++;
  82. /* Count the duration of the current pulse/gap. */
  83. while(idx < i->pulses_count && bitmap_get(i->bits, i->bits_bytes, idx) == level) {
  84. te_times++;
  85. idx++;
  86. }
  87. // Invariant: after the loop 'idx' is at the start of the
  88. // next gap or pulse.
  89. int32_t dur = (int32_t)i->short_pulse_dur * te_times;
  90. if(level == 0) dur = -dur; /* Negative is gap in raw files. */
  91. /* Emit the sample. If this is the first sample of the line,
  92. * also emit the RAW_Data: field. */
  93. if(this_line_samples == 0) furi_string_cat_printf(file_content, "RAW_Data: ");
  94. furi_string_cat_printf(file_content, "%d ", (int)dur);
  95. this_line_samples++;
  96. /* Store the current set of samples on disk, when we reach a
  97. * given number or the end of the signal. */
  98. bool end_reached = (idx == i->pulses_count);
  99. if(this_line_samples == max_line_samples || end_reached) {
  100. /* If that's the end, terminate the signal with a long
  101. * gap. */
  102. if(end_reached) furi_string_cat_printf(file_content, "-10000 ");
  103. /* We always have a trailing space in the last sample. Make it
  104. * a newline. */
  105. size_t len = furi_string_size(file_content);
  106. furi_string_set_char(file_content, len - 1, '\n');
  107. if(stream_write(stream, (uint8_t*)furi_string_get_cstr(file_content), len) !=
  108. len) {
  109. FURI_LOG_W(TAG, "Short write to file");
  110. success = false;
  111. goto write_err;
  112. }
  113. /* Prepare for next line. */
  114. furi_string_reset(file_content);
  115. this_line_samples = 0;
  116. }
  117. }
  118. } else {
  119. success = false;
  120. FURI_LOG_W(TAG, "Unable to open file");
  121. }
  122. write_err:
  123. furi_record_close(RECORD_STORAGE);
  124. flipper_format_free(file);
  125. if(file_content != NULL) furi_string_free(file_content);
  126. return success;
  127. }