Merge branch 'dev' into nfcf

This commit is contained in:
nullableVoidPtr
2022-12-25 18:03:54 +08:00
127 changed files with 4809 additions and 388 deletions
+2
View File
@@ -1,6 +1,7 @@
#include "lfrfid_protocols.h"
#include "protocol_em4100.h"
#include "protocol_h10301.h"
#include "protocol_idteck.h"
#include "protocol_indala26.h"
#include "protocol_io_prox_xsf.h"
#include "protocol_awid.h"
@@ -19,6 +20,7 @@
const ProtocolBase* lfrfid_protocols[] = {
[LFRFIDProtocolEM4100] = &protocol_em4100,
[LFRFIDProtocolH10301] = &protocol_h10301,
[LFRFIDProtocolIdteck] = &protocol_idteck,
[LFRFIDProtocolIndala26] = &protocol_indala26,
[LFRFIDProtocolIOProxXSF] = &protocol_io_prox_xsf,
[LFRFIDProtocolAwid] = &protocol_awid,
+1
View File
@@ -10,6 +10,7 @@ typedef enum {
typedef enum {
LFRFIDProtocolEM4100,
LFRFIDProtocolH10301,
LFRFIDProtocolIdteck,
LFRFIDProtocolIndala26,
LFRFIDProtocolIOProxXSF,
LFRFIDProtocolAwid,
+269
View File
@@ -0,0 +1,269 @@
#include <furi.h>
#include <toolbox/protocols/protocol.h>
#include <lfrfid/tools/bit_lib.h>
#include "lfrfid_protocols.h"
// Example: 4944544B 351FBE4B
// 01001001 01000100 01010100 01001011 00110101 00011111 10111110 01001011
// 4 9 4 4 5 4 4 B 3 5 1 F B E 4 B
// 0100 1001 0100 0100 0101 0100 0100 1011 0011 0101 0001 1111 1011 1110 0100 1011
#define IDTECK_PREAMBLE_BIT_SIZE (32)
#define IDTECK_PREAMBLE_DATA_SIZE (8)
#define IDTECK_ENCODED_BIT_SIZE (64)
#define IDTECK_ENCODED_DATA_SIZE (((IDTECK_ENCODED_BIT_SIZE) / 8) + IDTECK_PREAMBLE_DATA_SIZE)
#define IDTECK_ENCODED_DATA_LAST ((IDTECK_ENCODED_BIT_SIZE) / 8)
#define IDTECK_DECODED_BIT_SIZE (64)
#define IDTECK_DECODED_DATA_SIZE (8)
#define IDTECK_US_PER_BIT (255)
#define IDTECK_ENCODER_PULSES_PER_BIT (16)
typedef struct {
uint8_t data_index;
uint8_t bit_clock_index;
bool last_bit;
bool current_polarity;
bool pulse_phase;
} ProtocolIdteckEncoder;
typedef struct {
uint8_t encoded_data[IDTECK_ENCODED_DATA_SIZE];
uint8_t negative_encoded_data[IDTECK_ENCODED_DATA_SIZE];
uint8_t corrupted_encoded_data[IDTECK_ENCODED_DATA_SIZE];
uint8_t corrupted_negative_encoded_data[IDTECK_ENCODED_DATA_SIZE];
uint8_t data[IDTECK_DECODED_DATA_SIZE];
ProtocolIdteckEncoder encoder;
} ProtocolIdteck;
ProtocolIdteck* protocol_idteck_alloc(void) {
ProtocolIdteck* protocol = malloc(sizeof(ProtocolIdteck));
return protocol;
};
void protocol_idteck_free(ProtocolIdteck* protocol) {
free(protocol);
};
uint8_t* protocol_idteck_get_data(ProtocolIdteck* protocol) {
return protocol->data;
};
void protocol_idteck_decoder_start(ProtocolIdteck* protocol) {
memset(protocol->encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
memset(protocol->negative_encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
memset(protocol->corrupted_encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
memset(protocol->corrupted_negative_encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
};
static bool protocol_idteck_check_preamble(uint8_t* data, size_t bit_index) {
// Preamble 01001001 01000100 01010100 01001011
if(*(uint32_t*)&data[bit_index / 8] != 0b01001011010101000100010001001001) return false;
return true;
}
static bool protocol_idteck_can_be_decoded(uint8_t* data) {
if(!protocol_idteck_check_preamble(data, 0)) return false;
return true;
}
static bool protocol_idteck_decoder_feed_internal(bool polarity, uint32_t time, uint8_t* data) {
time += (IDTECK_US_PER_BIT / 2);
size_t bit_count = (time / IDTECK_US_PER_BIT);
bool result = false;
if(bit_count < IDTECK_ENCODED_BIT_SIZE) {
for(size_t i = 0; i < bit_count; i++) {
bit_lib_push_bit(data, IDTECK_ENCODED_DATA_SIZE, polarity);
if(protocol_idteck_can_be_decoded(data)) {
result = true;
break;
}
}
}
return result;
}
static void protocol_idteck_decoder_save(uint8_t* data_to, const uint8_t* data_from) {
bit_lib_copy_bits(data_to, 0, 64, data_from, 0);
}
bool protocol_idteck_decoder_feed(ProtocolIdteck* protocol, bool level, uint32_t duration) {
bool result = false;
if(duration > (IDTECK_US_PER_BIT / 2)) {
if(protocol_idteck_decoder_feed_internal(level, duration, protocol->encoded_data)) {
protocol_idteck_decoder_save(protocol->data, protocol->encoded_data);
FURI_LOG_D("Idteck", "Positive");
result = true;
return result;
}
if(protocol_idteck_decoder_feed_internal(
!level, duration, protocol->negative_encoded_data)) {
protocol_idteck_decoder_save(protocol->data, protocol->negative_encoded_data);
FURI_LOG_D("Idteck", "Negative");
result = true;
return result;
}
}
if(duration > (IDTECK_US_PER_BIT / 4)) {
// Try to decode wrong phase synced data
if(level) {
duration += 120;
} else {
if(duration > 120) {
duration -= 120;
}
}
if(protocol_idteck_decoder_feed_internal(
level, duration, protocol->corrupted_encoded_data)) {
protocol_idteck_decoder_save(protocol->data, protocol->corrupted_encoded_data);
FURI_LOG_D("Idteck", "Positive Corrupted");
result = true;
return result;
}
if(protocol_idteck_decoder_feed_internal(
!level, duration, protocol->corrupted_negative_encoded_data)) {
protocol_idteck_decoder_save(
protocol->data, protocol->corrupted_negative_encoded_data);
FURI_LOG_D("Idteck", "Negative Corrupted");
result = true;
return result;
}
}
return result;
};
bool protocol_idteck_encoder_start(ProtocolIdteck* protocol) {
memset(protocol->encoded_data, 0, IDTECK_ENCODED_DATA_SIZE);
*(uint32_t*)&protocol->encoded_data[0] = 0b01001011010101000100010001001001;
bit_lib_copy_bits(protocol->encoded_data, 32, 32, protocol->data, 32);
protocol->encoder.last_bit =
bit_lib_get_bit(protocol->encoded_data, IDTECK_ENCODED_BIT_SIZE - 1);
protocol->encoder.data_index = 0;
protocol->encoder.current_polarity = true;
protocol->encoder.pulse_phase = true;
protocol->encoder.bit_clock_index = 0;
return true;
};
LevelDuration protocol_idteck_encoder_yield(ProtocolIdteck* protocol) {
LevelDuration level_duration;
ProtocolIdteckEncoder* encoder = &protocol->encoder;
if(encoder->pulse_phase) {
level_duration = level_duration_make(encoder->current_polarity, 1);
encoder->pulse_phase = false;
} else {
level_duration = level_duration_make(!encoder->current_polarity, 1);
encoder->pulse_phase = true;
encoder->bit_clock_index++;
if(encoder->bit_clock_index >= IDTECK_ENCODER_PULSES_PER_BIT) {
encoder->bit_clock_index = 0;
bool current_bit = bit_lib_get_bit(protocol->encoded_data, encoder->data_index);
if(current_bit != encoder->last_bit) {
encoder->current_polarity = !encoder->current_polarity;
}
encoder->last_bit = current_bit;
bit_lib_increment_index(encoder->data_index, IDTECK_ENCODED_BIT_SIZE);
}
}
return level_duration;
};
// factory code
static uint32_t get_fc(const uint8_t* data) {
uint32_t fc = 0;
fc = bit_lib_get_bits_32(data, 0, 32);
return fc;
}
// card number
static uint32_t get_card(const uint8_t* data) {
uint32_t cn = 0;
cn = bit_lib_get_bits_32(data, 32, 32);
return cn;
}
void protocol_idteck_render_data_internal(ProtocolIdteck* protocol, FuriString* result, bool brief) {
const uint32_t fc = get_fc(protocol->data);
const uint32_t card = get_card(protocol->data);
if(brief) {
furi_string_printf(result, "FC: %08lX\r\nCard: %08lX", fc, card);
} else {
furi_string_printf(
result,
"FC: %08lX\r\n"
"Card: %08lX\r\n",
fc,
card);
}
}
void protocol_idteck_render_data(ProtocolIdteck* protocol, FuriString* result) {
protocol_idteck_render_data_internal(protocol, result, false);
}
void protocol_idteck_render_brief_data(ProtocolIdteck* protocol, FuriString* result) {
protocol_idteck_render_data_internal(protocol, result, true);
}
bool protocol_idteck_write_data(ProtocolIdteck* protocol, void* data) {
LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
bool result = false;
protocol_idteck_encoder_start(protocol);
if(request->write_type == LFRFIDWriteTypeT5577) {
request->t5577.block[0] = LFRFID_T5577_BITRATE_RF_32 | LFRFID_T5577_MODULATION_PSK1 |
(2 << LFRFID_T5577_MAXBLOCK_SHIFT);
request->t5577.block[1] = bit_lib_get_bits_32(protocol->encoded_data, 0, 32);
request->t5577.block[2] = bit_lib_get_bits_32(protocol->encoded_data, 32, 32);
request->t5577.blocks_to_write = 3;
result = true;
}
return result;
};
const ProtocolBase protocol_idteck = {
.name = "Idteck",
.manufacturer = "IDTECK",
.data_size = IDTECK_DECODED_DATA_SIZE,
.features = LFRFIDFeaturePSK,
.validate_count = 6,
.alloc = (ProtocolAlloc)protocol_idteck_alloc,
.free = (ProtocolFree)protocol_idteck_free,
.get_data = (ProtocolGetData)protocol_idteck_get_data,
.decoder =
{
.start = (ProtocolDecoderStart)protocol_idteck_decoder_start,
.feed = (ProtocolDecoderFeed)protocol_idteck_decoder_feed,
},
.encoder =
{
.start = (ProtocolEncoderStart)protocol_idteck_encoder_start,
.yield = (ProtocolEncoderYield)protocol_idteck_encoder_yield,
},
.render_data = (ProtocolRenderData)protocol_idteck_render_data,
.render_brief_data = (ProtocolRenderData)protocol_idteck_render_brief_data,
.write_data = (ProtocolWriteData)protocol_idteck_write_data,
};
+4
View File
@@ -0,0 +1,4 @@
#pragma once
#include <toolbox/protocols/protocol.h>
extern const ProtocolBase protocol_idteck;
+47 -11
View File
@@ -27,6 +27,7 @@ NfcDevice* nfc_device_alloc() {
nfc_dev->dialogs = furi_record_open(RECORD_DIALOGS);
nfc_dev->load_path = furi_string_alloc();
nfc_dev->dev_data.parsed_data = furi_string_alloc();
nfc_dev->folder = furi_string_alloc();
// Rename cache folder name for backward compatibility
if(storage_common_stat(nfc_dev->storage, "/ext/nfc/cache", NULL) == FSE_OK) {
@@ -42,6 +43,7 @@ void nfc_device_free(NfcDevice* nfc_dev) {
furi_record_close(RECORD_DIALOGS);
furi_string_free(nfc_dev->load_path);
furi_string_free(nfc_dev->dev_data.parsed_data);
furi_string_free(nfc_dev->folder);
free(nfc_dev);
}
@@ -1018,6 +1020,16 @@ static void nfc_device_get_shadow_path(FuriString* orig_path, FuriString* shadow
furi_string_cat_printf(shadow_path, "%s", NFC_APP_SHADOW_EXTENSION);
}
static void nfc_device_get_folder_from_path(FuriString* path, FuriString* folder) {
size_t last_slash = furi_string_search_rchar(path, '/');
if(last_slash == FURI_STRING_FAILURE) {
// No slashes in the path, treat the whole path as a folder
furi_string_set(folder, path);
} else {
furi_string_set_n(folder, path, 0, last_slash);
}
}
bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
furi_assert(dev);
@@ -1028,10 +1040,19 @@ bool nfc_device_save(NfcDevice* dev, const char* dev_name) {
temp_str = furi_string_alloc();
do {
// Create nfc directory if necessary
if(!storage_simply_mkdir(dev->storage, NFC_APP_FOLDER)) break;
// Create directory if necessary
FuriString* folder = furi_string_alloc();
// Get folder from filename (filename is in the form of "folder/filename.nfc", so the folder is "folder/")
furi_string_set(temp_str, dev_name);
// Get folder from filename
nfc_device_get_folder_from_path(temp_str, folder);
FURI_LOG_I("Nfc", "Saving to folder %s", furi_string_get_cstr(folder));
if(!storage_simply_mkdir(dev->storage, furi_string_get_cstr(folder))) {
FURI_LOG_E("Nfc", "Failed to create folder %s", furi_string_get_cstr(folder));
break;
}
furi_string_free(folder);
// First remove nfc device file if it was saved
furi_string_printf(temp_str, "%s", dev_name);
// Open file
if(!flipper_format_file_open_always(file, furi_string_get_cstr(temp_str))) break;
// Write header
@@ -1199,10 +1220,9 @@ bool nfc_device_load(NfcDevice* dev, const char* file_path, bool show_dialog) {
bool nfc_file_select(NfcDevice* dev) {
furi_assert(dev);
const char* folder = furi_string_get_cstr(dev->folder);
// Input events and views are managed by file_browser
FuriString* nfc_app_folder;
nfc_app_folder = furi_string_alloc_set(NFC_APP_FOLDER);
const DialogsFileBrowserOptions browser_options = {
.extension = NFC_APP_EXTENSION,
@@ -1212,13 +1232,12 @@ bool nfc_file_select(NfcDevice* dev) {
.hide_ext = true,
.item_loader_callback = NULL,
.item_loader_context = NULL,
.base_path = NFC_APP_FOLDER,
.base_path = folder,
};
bool res =
dialog_file_browser_show(dev->dialogs, dev->load_path, dev->load_path, &browser_options);
furi_string_free(nfc_app_folder);
if(res) {
FuriString* filename;
filename = furi_string_alloc();
@@ -1273,7 +1292,11 @@ bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
furi_string_set(file_path, dev->load_path);
} else {
furi_string_printf(
file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
file_path,
"%s/%s%s",
furi_string_get_cstr(dev->folder),
dev->dev_name,
NFC_APP_EXTENSION);
}
if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
// Delete shadow file if it exists
@@ -1282,7 +1305,11 @@ bool nfc_device_delete(NfcDevice* dev, bool use_load_path) {
nfc_device_get_shadow_path(dev->load_path, file_path);
} else {
furi_string_printf(
file_path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
file_path,
"%s/%s%s",
furi_string_get_cstr(dev->folder),
dev->dev_name,
NFC_APP_SHADOW_EXTENSION);
}
if(!storage_simply_remove(dev->storage, furi_string_get_cstr(file_path))) break;
}
@@ -1311,14 +1338,23 @@ bool nfc_device_restore(NfcDevice* dev, bool use_load_path) {
nfc_device_get_shadow_path(dev->load_path, path);
} else {
furi_string_printf(
path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_SHADOW_EXTENSION);
path,
"%s/%s%s",
furi_string_get_cstr(dev->folder),
dev->dev_name,
NFC_APP_SHADOW_EXTENSION);
}
if(!storage_simply_remove(dev->storage, furi_string_get_cstr(path))) break;
dev->shadow_file_exist = false;
if(use_load_path && !furi_string_empty(dev->load_path)) {
furi_string_set(path, dev->load_path);
} else {
furi_string_printf(path, "%s/%s%s", NFC_APP_FOLDER, dev->dev_name, NFC_APP_EXTENSION);
furi_string_printf(
path,
"%s/%s%s",
furi_string_get_cstr(dev->folder),
dev->dev_name,
NFC_APP_EXTENSION);
}
if(!nfc_device_load_data(dev, path, true)) break;
restored = true;
+1 -1
View File
@@ -22,7 +22,6 @@ extern "C" {
#define NFC_READER_DATA_MAX_SIZE 64
#define NFC_DICT_KEY_BATCH_SIZE 50
#define NFC_APP_FOLDER ANY_PATH("nfc")
#define NFC_APP_EXTENSION ".nfc"
#define NFC_APP_SHADOW_EXTENSION ".shd"
@@ -90,6 +89,7 @@ typedef struct {
NfcDeviceData dev_data;
char dev_name[NFC_DEV_NAME_MAX_LEN + 1];
FuriString* load_path;
FuriString* folder;
NfcDeviceSaveFormat format;
bool shadow_file_exist;
+9 -25
View File
@@ -8,6 +8,8 @@
#define TAG "SubGhzProtocolHormannHSM"
#define HORMANN_HSM_PATTERN 0xFF000000003
static const SubGhzBlockConst subghz_protocol_hormann_const = {
.te_short = 500,
.te_long = 1000,
@@ -101,20 +103,13 @@ static bool subghz_protocol_encoder_hormann_get_upload(SubGhzProtocolEncoderHorm
furi_assert(instance);
size_t index = 0;
size_t size_upload = 3 + (instance->generic.data_count_bit * 2 + 2) * 20 + 1;
size_t size_upload = (instance->generic.data_count_bit * 2 + 2) * 20 + 1;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send header
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)subghz_protocol_hormann_const.te_short * 64);
instance->encoder.repeat = 10; //original remote does 10 repeats
for(size_t repeat = 0; repeat < 20; repeat++) {
@@ -209,6 +204,10 @@ void subghz_protocol_decoder_hormann_free(void* context) {
free(instance);
}
static bool subghz_protocol_decoder_hormann_check_pattern(SubGhzProtocolDecoderHormann* instance) {
return (instance->decoder.decode_data & HORMANN_HSM_PATTERN) == HORMANN_HSM_PATTERN;
}
void subghz_protocol_decoder_hormann_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderHormann* instance = context;
@@ -221,25 +220,9 @@ void subghz_protocol_decoder_hormann_feed(void* context, bool level, uint32_t du
switch(instance->decoder.parser_step) {
case HormannDecoderStepReset:
if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 64) <
subghz_protocol_hormann_const.te_delta * 64)) {
instance->decoder.parser_step = HormannDecoderStepFoundStartHeader;
}
break;
case HormannDecoderStepFoundStartHeader:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 64) <
subghz_protocol_hormann_const.te_delta * 64)) {
instance->decoder.parser_step = HormannDecoderStepFoundHeader;
} else {
instance->decoder.parser_step = HormannDecoderStepReset;
}
break;
case HormannDecoderStepFoundHeader:
if((level) && (DURATION_DIFF(duration, subghz_protocol_hormann_const.te_short * 24) <
subghz_protocol_hormann_const.te_delta * 24)) {
instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
} else {
instance->decoder.parser_step = HormannDecoderStepReset;
}
break;
case HormannDecoderStepFoundStartBit:
@@ -254,7 +237,8 @@ void subghz_protocol_decoder_hormann_feed(void* context, bool level, uint32_t du
break;
case HormannDecoderStepSaveDuration:
if(level) { //save interval
if(duration >= (subghz_protocol_hormann_const.te_short * 5)) {
if(duration >= (subghz_protocol_hormann_const.te_short * 5) &&
subghz_protocol_decoder_hormann_check_pattern(instance)) {
instance->decoder.parser_step = HormannDecoderStepFoundStartBit;
if(instance->decoder.decode_count_bit >=
subghz_protocol_hormann_const.min_count_bit_for_found) {
+1 -1
View File
@@ -12,7 +12,7 @@ const SubGhzProtocol* subghz_protocol_registry_items[] = {
&subghz_protocol_chamb_code, &subghz_protocol_power_smart, &subghz_protocol_marantec,
&subghz_protocol_bett, &subghz_protocol_doitrand, &subghz_protocol_phoenix_v2,
&subghz_protocol_honeywell_wdb, &subghz_protocol_magellan, &subghz_protocol_intertechno_v3,
&subghz_protocol_clemsa, &subghz_protocol_ansonic,
&subghz_protocol_clemsa, &subghz_protocol_ansonic, &subghz_protocol_smc5326,
};
const SubGhzProtocolRegistry subghz_protocol_registry = {
+1
View File
@@ -36,5 +36,6 @@
#include "intertechno_v3.h"
#include "clemsa.h"
#include "ansonic.h"
#include "smc5326.h"
extern const SubGhzProtocolRegistry subghz_protocol_registry;
+387
View File
@@ -0,0 +1,387 @@
#include "smc5326.h"
#include "../blocks/const.h"
#include "../blocks/decoder.h"
#include "../blocks/encoder.h"
#include "../blocks/generic.h"
#include "../blocks/math.h"
/*
* Help
* https://datasheetspdf.com/pdf-file/532079/Aslic/AX5326-4/1
*
*/
#define TAG "SubGhzProtocolSMC5326"
#define DIP_P 0b11 //(+)
#define DIP_O 0b10 //(0)
#define DIP_N 0b00 //(-)
#define DIP_PATTERN "%c%c%c%c%c%c%c%c"
#define SHOW_DIP_P(dip, check_dip) \
((((dip >> 0xE) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xC) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0xA) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x8) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x6) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x4) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x2) & 0x3) == check_dip) ? '*' : '_'), \
((((dip >> 0x0) & 0x3) == check_dip) ? '*' : '_')
static const SubGhzBlockConst subghz_protocol_smc5326_const = {
.te_short = 300,
.te_long = 900,
.te_delta = 200,
.min_count_bit_for_found = 25,
};
struct SubGhzProtocolDecoderSMC5326 {
SubGhzProtocolDecoderBase base;
SubGhzBlockDecoder decoder;
SubGhzBlockGeneric generic;
uint32_t te;
uint32_t last_data;
};
struct SubGhzProtocolEncoderSMC5326 {
SubGhzProtocolEncoderBase base;
SubGhzProtocolBlockEncoder encoder;
SubGhzBlockGeneric generic;
uint32_t te;
};
typedef enum {
SMC5326DecoderStepReset = 0,
SMC5326DecoderStepSaveDuration,
SMC5326DecoderStepCheckDuration,
} SMC5326DecoderStep;
const SubGhzProtocolDecoder subghz_protocol_smc5326_decoder = {
.alloc = subghz_protocol_decoder_smc5326_alloc,
.free = subghz_protocol_decoder_smc5326_free,
.feed = subghz_protocol_decoder_smc5326_feed,
.reset = subghz_protocol_decoder_smc5326_reset,
.get_hash_data = subghz_protocol_decoder_smc5326_get_hash_data,
.serialize = subghz_protocol_decoder_smc5326_serialize,
.deserialize = subghz_protocol_decoder_smc5326_deserialize,
.get_string = subghz_protocol_decoder_smc5326_get_string,
};
const SubGhzProtocolEncoder subghz_protocol_smc5326_encoder = {
.alloc = subghz_protocol_encoder_smc5326_alloc,
.free = subghz_protocol_encoder_smc5326_free,
.deserialize = subghz_protocol_encoder_smc5326_deserialize,
.stop = subghz_protocol_encoder_smc5326_stop,
.yield = subghz_protocol_encoder_smc5326_yield,
};
const SubGhzProtocol subghz_protocol_smc5326 = {
.name = SUBGHZ_PROTOCOL_SMC5326_NAME,
.type = SubGhzProtocolTypeStatic,
.flag = SubGhzProtocolFlag_433 | SubGhzProtocolFlag_868 | SubGhzProtocolFlag_315 |
SubGhzProtocolFlag_AM | SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_Load |
SubGhzProtocolFlag_Save | SubGhzProtocolFlag_Send,
.decoder = &subghz_protocol_smc5326_decoder,
.encoder = &subghz_protocol_smc5326_encoder,
};
void* subghz_protocol_encoder_smc5326_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolEncoderSMC5326* instance = malloc(sizeof(SubGhzProtocolEncoderSMC5326));
instance->base.protocol = &subghz_protocol_smc5326;
instance->generic.protocol_name = instance->base.protocol->name;
instance->encoder.repeat = 10;
instance->encoder.size_upload = 128;
instance->encoder.upload = malloc(instance->encoder.size_upload * sizeof(LevelDuration));
instance->encoder.is_running = false;
return instance;
}
void subghz_protocol_encoder_smc5326_free(void* context) {
furi_assert(context);
SubGhzProtocolEncoderSMC5326* instance = context;
free(instance->encoder.upload);
free(instance);
}
/**
* Generating an upload from data.
* @param instance Pointer to a SubGhzProtocolEncoderSMC5326 instance
* @return true On success
*/
static bool subghz_protocol_encoder_smc5326_get_upload(SubGhzProtocolEncoderSMC5326* instance) {
furi_assert(instance);
size_t index = 0;
size_t size_upload = (instance->generic.data_count_bit * 2) + 2;
if(size_upload > instance->encoder.size_upload) {
FURI_LOG_E(TAG, "Size upload exceeds allocated encoder buffer.");
return false;
} else {
instance->encoder.size_upload = size_upload;
}
//Send key data
for(uint8_t i = instance->generic.data_count_bit; i > 0; i--) {
if(bit_read(instance->generic.data, i - 1)) {
//send bit 1
instance->encoder.upload[index++] =
level_duration_make(true, (uint32_t)instance->te * 3);
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)instance->te);
} else {
//send bit 0
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
instance->encoder.upload[index++] =
level_duration_make(false, (uint32_t)instance->te * 3);
}
}
//Send Stop bit
instance->encoder.upload[index++] = level_duration_make(true, (uint32_t)instance->te);
//Send PT_GUARD
instance->encoder.upload[index++] = level_duration_make(false, (uint32_t)instance->te * 25);
return true;
}
bool subghz_protocol_encoder_smc5326_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolEncoderSMC5326* instance = context;
bool res = false;
do {
if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
FURI_LOG_E(TAG, "Deserialize error");
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
FURI_LOG_E(TAG, "Missing TE");
break;
}
if(instance->generic.data_count_bit !=
subghz_protocol_smc5326_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
break;
}
//optional parameter parameter
flipper_format_read_uint32(
flipper_format, "Repeat", (uint32_t*)&instance->encoder.repeat, 1);
if(!subghz_protocol_encoder_smc5326_get_upload(instance)) break;
instance->encoder.is_running = true;
res = true;
} while(false);
return res;
}
void subghz_protocol_encoder_smc5326_stop(void* context) {
SubGhzProtocolEncoderSMC5326* instance = context;
instance->encoder.is_running = false;
}
LevelDuration subghz_protocol_encoder_smc5326_yield(void* context) {
SubGhzProtocolEncoderSMC5326* instance = context;
if(instance->encoder.repeat == 0 || !instance->encoder.is_running) {
instance->encoder.is_running = false;
return level_duration_reset();
}
LevelDuration ret = instance->encoder.upload[instance->encoder.front];
if(++instance->encoder.front == instance->encoder.size_upload) {
instance->encoder.repeat--;
instance->encoder.front = 0;
}
return ret;
}
void* subghz_protocol_decoder_smc5326_alloc(SubGhzEnvironment* environment) {
UNUSED(environment);
SubGhzProtocolDecoderSMC5326* instance = malloc(sizeof(SubGhzProtocolDecoderSMC5326));
instance->base.protocol = &subghz_protocol_smc5326;
instance->generic.protocol_name = instance->base.protocol->name;
return instance;
}
void subghz_protocol_decoder_smc5326_free(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSMC5326* instance = context;
free(instance);
}
void subghz_protocol_decoder_smc5326_reset(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSMC5326* instance = context;
instance->decoder.parser_step = SMC5326DecoderStepReset;
instance->last_data = 0;
}
void subghz_protocol_decoder_smc5326_feed(void* context, bool level, uint32_t duration) {
furi_assert(context);
SubGhzProtocolDecoderSMC5326* instance = context;
switch(instance->decoder.parser_step) {
case SMC5326DecoderStepReset:
if((!level) && (DURATION_DIFF(duration, subghz_protocol_smc5326_const.te_short * 24) <
subghz_protocol_smc5326_const.te_delta * 12)) {
//Found Preambula
instance->decoder.parser_step = SMC5326DecoderStepSaveDuration;
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->te = 0;
}
break;
case SMC5326DecoderStepSaveDuration:
//save duration
if(level) {
instance->decoder.te_last = duration;
instance->te += duration;
instance->decoder.parser_step = SMC5326DecoderStepCheckDuration;
}
break;
case SMC5326DecoderStepCheckDuration:
if(!level) {
if(duration >= ((uint32_t)subghz_protocol_smc5326_const.te_long * 2)) {
instance->decoder.parser_step = SMC5326DecoderStepSaveDuration;
if(instance->decoder.decode_count_bit ==
subghz_protocol_smc5326_const.min_count_bit_for_found) {
if((instance->last_data == instance->decoder.decode_data) &&
instance->last_data) {
instance->te /= (instance->decoder.decode_count_bit * 4 + 1);
instance->generic.data = instance->decoder.decode_data;
instance->generic.data_count_bit = instance->decoder.decode_count_bit;
if(instance->base.callback)
instance->base.callback(&instance->base, instance->base.context);
}
instance->last_data = instance->decoder.decode_data;
}
instance->decoder.decode_data = 0;
instance->decoder.decode_count_bit = 0;
instance->te = 0;
break;
}
instance->te += duration;
if((DURATION_DIFF(instance->decoder.te_last, subghz_protocol_smc5326_const.te_short) <
subghz_protocol_smc5326_const.te_delta) &&
(DURATION_DIFF(duration, subghz_protocol_smc5326_const.te_long) <
subghz_protocol_smc5326_const.te_delta * 3)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 0);
instance->decoder.parser_step = SMC5326DecoderStepSaveDuration;
} else if(
(DURATION_DIFF(instance->decoder.te_last, subghz_protocol_smc5326_const.te_long) <
subghz_protocol_smc5326_const.te_delta * 3) &&
(DURATION_DIFF(duration, subghz_protocol_smc5326_const.te_short) <
subghz_protocol_smc5326_const.te_delta)) {
subghz_protocol_blocks_add_bit(&instance->decoder, 1);
instance->decoder.parser_step = SMC5326DecoderStepSaveDuration;
} else {
instance->decoder.parser_step = SMC5326DecoderStepReset;
}
} else {
instance->decoder.parser_step = SMC5326DecoderStepReset;
}
break;
}
}
uint8_t subghz_protocol_decoder_smc5326_get_hash_data(void* context) {
furi_assert(context);
SubGhzProtocolDecoderSMC5326* instance = context;
return subghz_protocol_blocks_get_hash_data(
&instance->decoder, (instance->decoder.decode_count_bit / 8) + 1);
}
bool subghz_protocol_decoder_smc5326_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset) {
furi_assert(context);
SubGhzProtocolDecoderSMC5326* instance = context;
bool res = subghz_block_generic_serialize(&instance->generic, flipper_format, preset);
if(res && !flipper_format_write_uint32(flipper_format, "TE", &instance->te, 1)) {
FURI_LOG_E(TAG, "Unable to add TE");
res = false;
}
return res;
}
bool subghz_protocol_decoder_smc5326_deserialize(void* context, FlipperFormat* flipper_format) {
furi_assert(context);
SubGhzProtocolDecoderSMC5326* instance = context;
bool res = false;
do {
if(!subghz_block_generic_deserialize(&instance->generic, flipper_format)) {
FURI_LOG_E(TAG, "Deserialize error");
break;
}
if(instance->generic.data_count_bit !=
subghz_protocol_smc5326_const.min_count_bit_for_found) {
FURI_LOG_E(TAG, "Wrong number of bits in key");
break;
}
if(!flipper_format_rewind(flipper_format)) {
FURI_LOG_E(TAG, "Rewind error");
break;
}
if(!flipper_format_read_uint32(flipper_format, "TE", (uint32_t*)&instance->te, 1)) {
FURI_LOG_E(TAG, "Missing TE");
break;
}
res = true;
} while(false);
return res;
}
static void subghz_protocol_smc5326_get_event_serialize(uint8_t event, FuriString* output) {
furi_string_cat_printf(
output,
"%s%s%s%s\r\n",
(((event >> 6) & 0x3) == 0x3 ? "B1 " : ""),
(((event >> 4) & 0x3) == 0x3 ? "B2 " : ""),
(((event >> 2) & 0x3) == 0x3 ? "B3 " : ""),
(((event >> 0) & 0x3) == 0x3 ? "B4 " : ""));
}
void subghz_protocol_decoder_smc5326_get_string(void* context, FuriString* output) {
furi_assert(context);
SubGhzProtocolDecoderSMC5326* instance = context;
uint32_t data = (uint32_t)((instance->generic.data >> 9) & 0xFFFF);
furi_string_cat_printf(
output,
"%s %dbit\r\n"
"Key:%07lX Te:%ldus\r\n"
" +: " DIP_PATTERN "\r\n"
" o: " DIP_PATTERN " ",
instance->generic.protocol_name,
instance->generic.data_count_bit,
(uint32_t)(instance->generic.data & 0x1FFFFFF),
instance->te,
SHOW_DIP_P(data, DIP_P),
SHOW_DIP_P(data, DIP_O));
subghz_protocol_smc5326_get_event_serialize(instance->generic.data >> 1, output);
furi_string_cat_printf(output, " -: " DIP_PATTERN "\r\n", SHOW_DIP_P(data, DIP_N));
}
+107
View File
@@ -0,0 +1,107 @@
#pragma once
#include "base.h"
#define SUBGHZ_PROTOCOL_SMC5326_NAME "SMC5326"
typedef struct SubGhzProtocolDecoderSMC5326 SubGhzProtocolDecoderSMC5326;
typedef struct SubGhzProtocolEncoderSMC5326 SubGhzProtocolEncoderSMC5326;
extern const SubGhzProtocolDecoder subghz_protocol_smc5326_decoder;
extern const SubGhzProtocolEncoder subghz_protocol_smc5326_encoder;
extern const SubGhzProtocol subghz_protocol_smc5326;
/**
* Allocate SubGhzProtocolEncoderSMC5326.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolEncoderSMC5326* pointer to a SubGhzProtocolEncoderSMC5326 instance
*/
void* subghz_protocol_encoder_smc5326_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolEncoderSMC5326.
* @param context Pointer to a SubGhzProtocolEncoderSMC5326 instance
*/
void subghz_protocol_encoder_smc5326_free(void* context);
/**
* Deserialize and generating an upload to send.
* @param context Pointer to a SubGhzProtocolEncoderSMC5326 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
bool subghz_protocol_encoder_smc5326_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Forced transmission stop.
* @param context Pointer to a SubGhzProtocolEncoderSMC5326 instance
*/
void subghz_protocol_encoder_smc5326_stop(void* context);
/**
* Getting the level and duration of the upload to be loaded into DMA.
* @param context Pointer to a SubGhzProtocolEncoderSMC5326 instance
* @return LevelDuration
*/
LevelDuration subghz_protocol_encoder_smc5326_yield(void* context);
/**
* Allocate SubGhzProtocolDecoderSMC5326.
* @param environment Pointer to a SubGhzEnvironment instance
* @return SubGhzProtocolDecoderSMC5326* pointer to a SubGhzProtocolDecoderSMC5326 instance
*/
void* subghz_protocol_decoder_smc5326_alloc(SubGhzEnvironment* environment);
/**
* Free SubGhzProtocolDecoderSMC5326.
* @param context Pointer to a SubGhzProtocolDecoderSMC5326 instance
*/
void subghz_protocol_decoder_smc5326_free(void* context);
/**
* Reset decoder SubGhzProtocolDecoderSMC5326.
* @param context Pointer to a SubGhzProtocolDecoderSMC5326 instance
*/
void subghz_protocol_decoder_smc5326_reset(void* context);
/**
* Parse a raw sequence of levels and durations received from the air.
* @param context Pointer to a SubGhzProtocolDecoderSMC5326 instance
* @param level Signal level true-high false-low
* @param duration Duration of this level in, us
*/
void subghz_protocol_decoder_smc5326_feed(void* context, bool level, uint32_t duration);
/**
* Getting the hash sum of the last randomly received parcel.
* @param context Pointer to a SubGhzProtocolDecoderSMC5326 instance
* @return hash Hash sum
*/
uint8_t subghz_protocol_decoder_smc5326_get_hash_data(void* context);
/**
* Serialize data SubGhzProtocolDecoderSMC5326.
* @param context Pointer to a SubGhzProtocolDecoderSMC5326 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @param preset The modulation on which the signal was received, SubGhzRadioPreset
* @return true On success
*/
bool subghz_protocol_decoder_smc5326_serialize(
void* context,
FlipperFormat* flipper_format,
SubGhzRadioPreset* preset);
/**
* Deserialize data SubGhzProtocolDecoderSMC5326.
* @param context Pointer to a SubGhzProtocolDecoderSMC5326 instance
* @param flipper_format Pointer to a FlipperFormat instance
* @return true On success
*/
bool subghz_protocol_decoder_smc5326_deserialize(void* context, FlipperFormat* flipper_format);
/**
* Getting a textual representation of the received data.
* @param context Pointer to a SubGhzProtocolDecoderSMC5326 instance
* @param output Resulting text
*/
void subghz_protocol_decoder_smc5326_get_string(void* context, FuriString* output);
+51
View File
@@ -125,3 +125,54 @@ bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic,
return result;
}
bool saved_struct_get_payload_size(
const char* path,
uint8_t magic,
uint8_t version,
size_t* payload_size) {
furi_assert(path);
furi_assert(payload_size);
SavedStructHeader header;
Storage* storage = furi_record_open(RECORD_STORAGE);
File* file = storage_file_alloc(storage);
bool result = false;
do {
if(!storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
FURI_LOG_E(
TAG, "Failed to read \"%s\". Error: %s", path, storage_file_get_error_desc(file));
break;
}
uint16_t bytes_count = storage_file_read(file, &header, sizeof(SavedStructHeader));
if(bytes_count != sizeof(SavedStructHeader)) {
FURI_LOG_E(TAG, "Failed to read header");
break;
}
if((header.magic != magic) || (header.version != version)) {
FURI_LOG_E(
TAG,
"Magic(%d != %d) or Version(%d != %d) mismatch of file \"%s\"",
header.magic,
magic,
header.version,
version,
path);
break;
}
uint64_t file_size = storage_file_size(file);
*payload_size = file_size - sizeof(SavedStructHeader);
result = true;
} while(false);
storage_file_close(file);
storage_file_free(file);
furi_record_close(RECORD_STORAGE);
return result;
}
+6
View File
@@ -12,6 +12,12 @@ bool saved_struct_load(const char* path, void* data, size_t size, uint8_t magic,
bool saved_struct_save(const char* path, void* data, size_t size, uint8_t magic, uint8_t version);
bool saved_struct_get_payload_size(
const char* path,
uint8_t magic,
uint8_t version,
size_t* payload_size);
#ifdef __cplusplus
}
#endif