2022-03-03 12:48:56 +03:00
|
|
|
#include "generic.h"
|
|
|
|
#include <lib/toolbox/stream/stream.h>
|
|
|
|
#include <lib/flipper_format/flipper_format_i.h>
|
|
|
|
|
|
|
|
#define TAG "SubGhzBlockGeneric"
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
void subghz_block_generic_get_preset_name(const char* preset_name, FuriString* preset_str) {
|
2022-07-26 17:16:59 +03:00
|
|
|
const char* preset_name_temp;
|
|
|
|
if(!strcmp(preset_name, "AM270")) {
|
|
|
|
preset_name_temp = "FuriHalSubGhzPresetOok270Async";
|
|
|
|
} else if(!strcmp(preset_name, "AM650")) {
|
|
|
|
preset_name_temp = "FuriHalSubGhzPresetOok650Async";
|
|
|
|
} else if(!strcmp(preset_name, "FM238")) {
|
|
|
|
preset_name_temp = "FuriHalSubGhzPreset2FSKDev238Async";
|
|
|
|
} else if(!strcmp(preset_name, "FM476")) {
|
|
|
|
preset_name_temp = "FuriHalSubGhzPreset2FSKDev476Async";
|
|
|
|
} else {
|
|
|
|
preset_name_temp = "FuriHalSubGhzPresetCustom";
|
2022-03-03 12:48:56 +03:00
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_set(preset_str, preset_name_temp);
|
2022-03-03 12:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_block_generic_serialize(
|
|
|
|
SubGhzBlockGeneric* instance,
|
|
|
|
FlipperFormat* flipper_format,
|
2022-10-19 20:27:26 +03:00
|
|
|
SubGhzRadioPreset* preset) {
|
2022-03-03 12:48:56 +03:00
|
|
|
furi_assert(instance);
|
|
|
|
bool res = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2022-03-03 12:48:56 +03:00
|
|
|
do {
|
|
|
|
stream_clean(flipper_format_get_raw_stream(flipper_format));
|
|
|
|
if(!flipper_format_write_header_cstr(
|
|
|
|
flipper_format, SUBGHZ_KEY_FILE_TYPE, SUBGHZ_KEY_FILE_VERSION)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to add header");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:16:59 +03:00
|
|
|
if(!flipper_format_write_uint32(flipper_format, "Frequency", &preset->frequency, 1)) {
|
2022-03-03 12:48:56 +03:00
|
|
|
FURI_LOG_E(TAG, "Unable to add Frequency");
|
|
|
|
break;
|
|
|
|
}
|
2022-07-26 17:16:59 +03:00
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
subghz_block_generic_get_preset_name(furi_string_get_cstr(preset->name), temp_str);
|
|
|
|
if(!flipper_format_write_string_cstr(
|
|
|
|
flipper_format, "Preset", furi_string_get_cstr(temp_str))) {
|
2022-03-03 12:48:56 +03:00
|
|
|
FURI_LOG_E(TAG, "Unable to add Preset");
|
|
|
|
break;
|
|
|
|
}
|
2022-10-05 18:15:23 +03:00
|
|
|
if(!strcmp(furi_string_get_cstr(temp_str), "FuriHalSubGhzPresetCustom")) {
|
2022-07-26 17:16:59 +03:00
|
|
|
if(!flipper_format_write_string_cstr(
|
|
|
|
flipper_format, "Custom_preset_module", "CC1101")) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to add Custom_preset_module");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_write_hex(
|
|
|
|
flipper_format, "Custom_preset_data", preset->data, preset->data_size)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to add Custom_preset_data");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-03 12:48:56 +03:00
|
|
|
if(!flipper_format_write_string_cstr(flipper_format, "Protocol", instance->protocol_name)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to add Protocol");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
uint32_t temp = instance->data_count_bit;
|
|
|
|
if(!flipper_format_write_uint32(flipper_format, "Bit", &temp, 1)) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to add Bit");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t key_data[sizeof(uint64_t)] = {0};
|
|
|
|
for(size_t i = 0; i < sizeof(uint64_t); i++) {
|
2022-12-26 15:13:30 +03:00
|
|
|
key_data[sizeof(uint64_t) - i - 1] = (instance->data >> (i * 8)) & 0xFF;
|
2022-03-03 12:48:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!flipper_format_write_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
|
|
|
FURI_LOG_E(TAG, "Unable to add Key");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
res = true;
|
|
|
|
} while(false);
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-03-03 12:48:56 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool subghz_block_generic_deserialize(SubGhzBlockGeneric* instance, FlipperFormat* flipper_format) {
|
|
|
|
furi_assert(instance);
|
|
|
|
bool res = false;
|
2022-10-05 18:15:23 +03:00
|
|
|
FuriString* temp_str;
|
|
|
|
temp_str = furi_string_alloc();
|
2022-03-03 12:48:56 +03:00
|
|
|
uint32_t temp_data = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if(!flipper_format_rewind(flipper_format)) {
|
|
|
|
FURI_LOG_E(TAG, "Rewind error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(!flipper_format_read_uint32(flipper_format, "Bit", (uint32_t*)&temp_data, 1)) {
|
|
|
|
FURI_LOG_E(TAG, "Missing Bit");
|
|
|
|
break;
|
|
|
|
}
|
2023-02-09 07:48:06 +03:00
|
|
|
instance->data_count_bit = (uint16_t)temp_data;
|
2022-03-03 12:48:56 +03:00
|
|
|
|
|
|
|
uint8_t key_data[sizeof(uint64_t)] = {0};
|
|
|
|
if(!flipper_format_read_hex(flipper_format, "Key", key_data, sizeof(uint64_t))) {
|
|
|
|
FURI_LOG_E(TAG, "Missing Key");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(uint8_t i = 0; i < sizeof(uint64_t); i++) {
|
|
|
|
instance->data = instance->data << 8 | key_data[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
res = true;
|
|
|
|
} while(0);
|
|
|
|
|
2022-10-05 18:15:23 +03:00
|
|
|
furi_string_free(temp_str);
|
2022-03-03 12:48:56 +03:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|