2023-01-18 20:18:19 +03:00
|
|
|
/* Copyright (C) 2023 Salvatore Sanfilippo -- All Rights Reserved
|
|
|
|
* Copyright (C) 2023 Maciej Wojtasik -- All Rights Reserved
|
|
|
|
* See the LICENSE file for information about the license. */
|
|
|
|
|
|
|
|
#include "app.h"
|
|
|
|
#include <stream/stream.h>
|
|
|
|
#include <flipper_format/flipper_format_i.h>
|
|
|
|
|
|
|
|
/* ========================= Signal file operations ========================= */
|
|
|
|
|
|
|
|
/* This function saves the current logical signal on disk. What is saved here
|
|
|
|
* is not the signal as level and duration as we received it from CC1101,
|
|
|
|
* but it's logical representation stored in the app->msg_info bitmap, where
|
|
|
|
* each 1 or 0 means a puls or gap for the specified short pulse duration time
|
|
|
|
* (te). */
|
2023-01-18 22:25:39 +03:00
|
|
|
bool save_signal(ProtoViewApp* app, const char* filename) {
|
2023-01-18 20:18:19 +03:00
|
|
|
/* We have a message at all? */
|
2023-01-18 22:25:39 +03:00
|
|
|
if(app->msg_info == NULL || app->msg_info->pulses_count == 0) return false;
|
|
|
|
|
|
|
|
Storage* storage = furi_record_open(RECORD_STORAGE);
|
|
|
|
FlipperFormat* file = flipper_format_file_alloc(storage);
|
|
|
|
Stream* stream = flipper_format_get_raw_stream(file);
|
|
|
|
FuriString* file_content = NULL;
|
2023-01-18 20:18:19 +03:00
|
|
|
bool success = true;
|
|
|
|
|
2023-01-18 22:25:39 +03:00
|
|
|
if(flipper_format_file_open_always(file, filename)) {
|
2023-01-18 20:18:19 +03:00
|
|
|
/* Write the file header. */
|
2023-01-18 22:25:39 +03:00
|
|
|
FuriString* file_content = furi_string_alloc();
|
|
|
|
const char* preset_id = ProtoViewModulations[app->modulation].id;
|
|
|
|
|
|
|
|
furi_string_printf(
|
|
|
|
file_content,
|
|
|
|
"Filetype: Flipper SubGhz RAW File\n"
|
|
|
|
"Version: 1\n"
|
|
|
|
"Frequency: %ld\n"
|
|
|
|
"Preset: %s\n",
|
|
|
|
app->frequency,
|
|
|
|
preset_id ? preset_id : "FuriHalSubGhzPresetCustom");
|
2023-01-18 20:18:19 +03:00
|
|
|
|
|
|
|
/* For custom modulations, we need to emit a set of registers. */
|
2023-01-18 22:25:39 +03:00
|
|
|
if(preset_id == NULL) {
|
|
|
|
FuriString* custom = furi_string_alloc();
|
|
|
|
uint8_t* regs = ProtoViewModulations[app->modulation].custom;
|
|
|
|
furi_string_printf(
|
|
|
|
custom,
|
2023-01-18 20:18:19 +03:00
|
|
|
"Custom_preset_module: CC1101\n"
|
2023-01-18 22:25:39 +03:00
|
|
|
"Custom_preset_data: ");
|
|
|
|
for(int j = 0; regs[j]; j += 2) {
|
|
|
|
furi_string_cat_printf(custom, "%02X %02X ", (int)regs[j], (int)regs[j + 1]);
|
2023-01-18 20:18:19 +03:00
|
|
|
}
|
2023-06-12 23:20:56 +03:00
|
|
|
// Add patable
|
|
|
|
furi_string_cat(custom, "00 00 C0 00 00 00 00 00 00 00 ");
|
2023-06-07 23:39:52 +03:00
|
|
|
//size_t len = furi_string_size(file_content);
|
|
|
|
//furi_string_set_char(custom, len - 1, '\n');
|
|
|
|
furi_string_cat(custom, "\n");
|
2023-01-18 22:25:39 +03:00
|
|
|
furi_string_cat(file_content, custom);
|
2023-01-18 20:18:19 +03:00
|
|
|
furi_string_free(custom);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We always save raw files. */
|
2023-01-18 22:25:39 +03:00
|
|
|
furi_string_cat_printf(
|
|
|
|
file_content,
|
|
|
|
"Protocol: RAW\n"
|
|
|
|
"RAW_Data: -10000\n"); // Start with 10 ms of gap
|
2023-01-18 20:18:19 +03:00
|
|
|
|
|
|
|
/* Write header. */
|
|
|
|
size_t len = furi_string_size(file_content);
|
2023-01-18 22:25:39 +03:00
|
|
|
if(stream_write(stream, (uint8_t*)furi_string_get_cstr(file_content), len) != len) {
|
2023-01-18 20:18:19 +03:00
|
|
|
FURI_LOG_W(TAG, "Short write to file");
|
|
|
|
success = false;
|
|
|
|
goto write_err;
|
|
|
|
}
|
|
|
|
furi_string_reset(file_content);
|
|
|
|
|
|
|
|
/* Write raw data sections. The Flipper subghz parser can't handle
|
|
|
|
* too much data on a single line, so we generate a new one
|
|
|
|
* every few samples. */
|
|
|
|
uint32_t this_line_samples = 0;
|
|
|
|
uint32_t max_line_samples = 100;
|
|
|
|
uint32_t idx = 0; // Iindex in the signal bitmap.
|
2023-01-18 22:25:39 +03:00
|
|
|
ProtoViewMsgInfo* i = app->msg_info;
|
2023-01-18 20:18:19 +03:00
|
|
|
while(idx < i->pulses_count) {
|
2023-01-18 22:25:39 +03:00
|
|
|
bool level = bitmap_get(i->bits, i->bits_bytes, idx);
|
2023-01-18 20:18:19 +03:00
|
|
|
uint32_t te_times = 1;
|
|
|
|
idx++;
|
|
|
|
/* Count the duration of the current pulse/gap. */
|
2023-01-18 22:25:39 +03:00
|
|
|
while(idx < i->pulses_count && bitmap_get(i->bits, i->bits_bytes, idx) == level) {
|
2023-01-18 20:18:19 +03:00
|
|
|
te_times++;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
// Invariant: after the loop 'idx' is at the start of the
|
|
|
|
// next gap or pulse.
|
|
|
|
|
|
|
|
int32_t dur = (int32_t)i->short_pulse_dur * te_times;
|
2023-01-18 22:25:39 +03:00
|
|
|
if(level == 0) dur = -dur; /* Negative is gap in raw files. */
|
2023-01-18 20:18:19 +03:00
|
|
|
|
|
|
|
/* Emit the sample. If this is the first sample of the line,
|
|
|
|
* also emit the RAW_Data: field. */
|
2023-01-18 22:25:39 +03:00
|
|
|
if(this_line_samples == 0) furi_string_cat_printf(file_content, "RAW_Data: ");
|
|
|
|
furi_string_cat_printf(file_content, "%d ", (int)dur);
|
2023-01-18 20:18:19 +03:00
|
|
|
this_line_samples++;
|
|
|
|
|
|
|
|
/* Store the current set of samples on disk, when we reach a
|
|
|
|
* given number or the end of the signal. */
|
|
|
|
bool end_reached = (idx == i->pulses_count);
|
2023-01-18 22:25:39 +03:00
|
|
|
if(this_line_samples == max_line_samples || end_reached) {
|
2023-01-18 20:18:19 +03:00
|
|
|
/* If that's the end, terminate the signal with a long
|
|
|
|
* gap. */
|
2023-01-18 22:25:39 +03:00
|
|
|
if(end_reached) furi_string_cat_printf(file_content, "-10000 ");
|
2023-01-18 20:18:19 +03:00
|
|
|
|
|
|
|
/* We always have a trailing space in the last sample. Make it
|
|
|
|
* a newline. */
|
|
|
|
size_t len = furi_string_size(file_content);
|
2023-01-18 22:25:39 +03:00
|
|
|
furi_string_set_char(file_content, len - 1, '\n');
|
2023-01-18 20:18:19 +03:00
|
|
|
|
2023-01-18 22:25:39 +03:00
|
|
|
if(stream_write(stream, (uint8_t*)furi_string_get_cstr(file_content), len) !=
|
|
|
|
len) {
|
2023-01-18 20:18:19 +03:00
|
|
|
FURI_LOG_W(TAG, "Short write to file");
|
|
|
|
success = false;
|
|
|
|
goto write_err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare for next line. */
|
|
|
|
furi_string_reset(file_content);
|
|
|
|
this_line_samples = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
FURI_LOG_W(TAG, "Unable to open file");
|
|
|
|
}
|
|
|
|
|
|
|
|
write_err:
|
|
|
|
furi_record_close(RECORD_STORAGE);
|
|
|
|
flipper_format_free(file);
|
2023-01-18 22:25:39 +03:00
|
|
|
if(file_content != NULL) furi_string_free(file_content);
|
2023-01-18 20:18:19 +03:00
|
|
|
return success;
|
|
|
|
}
|