signal_file.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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(file_content,
  26. "Filetype: Flipper SubGhz RAW File\n"
  27. "Version: 1\n"
  28. "Frequency: %ld\n"
  29. "Preset: %s\n",
  30. app->frequency,
  31. preset_id ? preset_id : "FuriHalSubGhzPresetCustom");
  32. /* For custom modulations, we need to emit a set of registers. */
  33. if (preset_id == NULL) {
  34. FuriString *custom = furi_string_alloc();
  35. uint8_t *regs = ProtoViewModulations[app->modulation].custom;
  36. furi_string_printf(custom,
  37. "Custom_preset_module: CC1101\n"
  38. "Custom_preset_data: ");
  39. for (int j = 0; regs[j]; j += 2) {
  40. furi_string_cat_printf(custom, "%02X %02X ",
  41. (int)regs[j], (int)regs[j+1]);
  42. }
  43. size_t len = furi_string_size(file_content);
  44. furi_string_set_char(custom,len-1,'\n');
  45. furi_string_cat(file_content,custom);
  46. furi_string_free(custom);
  47. }
  48. /* We always save raw files. */
  49. furi_string_cat_printf(file_content,
  50. "Protocol: RAW\n"
  51. "RAW_Data: -10000\n"); // Start with 10 ms of gap
  52. /* Write header. */
  53. size_t len = furi_string_size(file_content);
  54. if (stream_write(stream,
  55. (uint8_t*) furi_string_get_cstr(file_content), len)
  56. != len)
  57. {
  58. FURI_LOG_W(TAG, "Short write to file");
  59. success = false;
  60. goto write_err;
  61. }
  62. furi_string_reset(file_content);
  63. /* Write raw data sections. The Flipper subghz parser can't handle
  64. * too much data on a single line, so we generate a new one
  65. * every few samples. */
  66. uint32_t this_line_samples = 0;
  67. uint32_t max_line_samples = 100;
  68. uint32_t idx = 0; // Iindex in the signal bitmap.
  69. ProtoViewMsgInfo *i = app->msg_info;
  70. while(idx < i->pulses_count) {
  71. bool level = bitmap_get(i->bits,i->bits_bytes,idx);
  72. uint32_t te_times = 1;
  73. idx++;
  74. /* Count the duration of the current pulse/gap. */
  75. while(idx < i->pulses_count &&
  76. bitmap_get(i->bits,i->bits_bytes,idx) == level)
  77. {
  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)
  88. furi_string_cat_printf(file_content,"RAW_Data: ");
  89. furi_string_cat_printf(file_content,"%d ",(int)dur);
  90. this_line_samples++;
  91. /* Store the current set of samples on disk, when we reach a
  92. * given number or the end of the signal. */
  93. bool end_reached = (idx == i->pulses_count);
  94. if (this_line_samples == max_line_samples || end_reached) {
  95. /* If that's the end, terminate the signal with a long
  96. * gap. */
  97. if (end_reached) furi_string_cat_printf(file_content,"-10000 ");
  98. /* We always have a trailing space in the last sample. Make it
  99. * a newline. */
  100. size_t len = furi_string_size(file_content);
  101. furi_string_set_char(file_content,len-1,'\n');
  102. if (stream_write(stream,
  103. (uint8_t*) furi_string_get_cstr(file_content),
  104. len) != len)
  105. {
  106. FURI_LOG_W(TAG, "Short write to file");
  107. success = false;
  108. goto write_err;
  109. }
  110. /* Prepare for next line. */
  111. furi_string_reset(file_content);
  112. this_line_samples = 0;
  113. }
  114. }
  115. } else {
  116. success = false;
  117. FURI_LOG_W(TAG, "Unable to open file");
  118. }
  119. write_err:
  120. furi_record_close(RECORD_STORAGE);
  121. flipper_format_free(file);
  122. if (file_content != NULL) furi_string_free(file_content);
  123. return success;
  124. }