mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-07-28 01:58:11 -07:00
Merge branch 'dev' into DigitalSequence_PulseReader
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -10,6 +10,7 @@ typedef enum {
|
||||
typedef enum {
|
||||
LFRFIDProtocolEM4100,
|
||||
LFRFIDProtocolH10301,
|
||||
LFRFIDProtocolIdteck,
|
||||
LFRFIDProtocolIndala26,
|
||||
LFRFIDProtocolIOProxXSF,
|
||||
LFRFIDProtocolAwid,
|
||||
|
||||
@@ -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,
|
||||
};
|
||||
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
#include <toolbox/protocols/protocol.h>
|
||||
|
||||
extern const ProtocolBase protocol_idteck;
|
||||
@@ -0,0 +1,480 @@
|
||||
#include <furi_hal_random.h>
|
||||
#include "nfc_generators.h"
|
||||
|
||||
#define NXP_MANUFACTURER_ID (0x04)
|
||||
|
||||
static const uint8_t version_bytes_mf0ulx1[] = {0x00, 0x04, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03};
|
||||
static const uint8_t version_bytes_ntag21x[] = {0x00, 0x04, 0x04, 0x02, 0x01, 0x00, 0x00, 0x03};
|
||||
static const uint8_t version_bytes_ntag_i2c[] = {0x00, 0x04, 0x04, 0x05, 0x02, 0x00, 0x00, 0x03};
|
||||
static const uint8_t default_data_ntag203[] =
|
||||
{0xE1, 0x10, 0x12, 0x00, 0x01, 0x03, 0xA0, 0x10, 0x44, 0x03, 0x00, 0xFE};
|
||||
static const uint8_t default_data_ntag213[] = {0x01, 0x03, 0xA0, 0x0C, 0x34, 0x03, 0x00, 0xFE};
|
||||
static const uint8_t default_data_ntag215_216[] = {0x03, 0x00, 0xFE};
|
||||
static const uint8_t default_data_ntag_i2c[] = {0xE1, 0x10, 0x00, 0x00, 0x03, 0x00, 0xFE};
|
||||
static const uint8_t default_config_ntag_i2c[] = {0x01, 0x00, 0xF8, 0x48, 0x08, 0x01, 0x00, 0x00};
|
||||
|
||||
static void nfc_generate_common_start(NfcDeviceData* data) {
|
||||
nfc_device_data_clear(data);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_uid(uint8_t* uid) {
|
||||
uid[0] = NXP_MANUFACTURER_ID;
|
||||
furi_hal_random_fill_buf(&uid[1], 6);
|
||||
// I'm not sure how this is generated, but the upper nybble always seems to be 8
|
||||
uid[6] &= 0x0F;
|
||||
uid[6] |= 0x80;
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_classic_uid(uint8_t* uid, uint8_t length) {
|
||||
uid[0] = NXP_MANUFACTURER_ID;
|
||||
furi_hal_random_fill_buf(&uid[1], length - 1);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_classic_block_0(uint8_t* block, uint8_t uid_len) {
|
||||
// Block length is always 16 bytes, and the UID can be either 4 or 7 bytes
|
||||
furi_assert(uid_len == 4 || uid_len == 7);
|
||||
furi_assert(block);
|
||||
nfc_generate_mf_classic_uid(block, uid_len);
|
||||
for(int i = uid_len; i < 16; i++) {
|
||||
block[i] = 0xFF;
|
||||
}
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_classic_sector_trailer(MfClassicData* data, uint8_t block) {
|
||||
// All keys are set to FFFF FFFF FFFFh at chip delivery and the bytes 6, 7 and 8 are set to FF0780h.
|
||||
MfClassicSectorTrailer* sec_tr = (MfClassicSectorTrailer*)data->block[block].value;
|
||||
sec_tr->access_bits[0] = 0xFF;
|
||||
sec_tr->access_bits[1] = 0x07;
|
||||
sec_tr->access_bits[2] = 0x80;
|
||||
sec_tr->access_bits[3] = 0x69; // Nice
|
||||
|
||||
memset(sec_tr->key_a, 0xff, sizeof(sec_tr->key_a));
|
||||
memset(sec_tr->key_b, 0xff, sizeof(sec_tr->key_b));
|
||||
|
||||
mf_classic_set_block_read(data, block, &data->block[block]);
|
||||
mf_classic_set_key_found(
|
||||
data, mf_classic_get_sector_by_block(block), MfClassicKeyA, 0xFFFFFFFFFFFF);
|
||||
mf_classic_set_key_found(
|
||||
data, mf_classic_get_sector_by_block(block), MfClassicKeyB, 0xFFFFFFFFFFFF);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_common(NfcDeviceData* data) {
|
||||
data->nfc_data.type = FuriHalNfcTypeA;
|
||||
data->nfc_data.interface = FuriHalNfcInterfaceRf;
|
||||
data->nfc_data.uid_len = 7;
|
||||
nfc_generate_mf_ul_uid(data->nfc_data.uid);
|
||||
data->nfc_data.atqa[0] = 0x44;
|
||||
data->nfc_data.atqa[1] = 0x00;
|
||||
data->nfc_data.sak = 0x00;
|
||||
data->protocol = NfcDeviceProtocolMifareUl;
|
||||
}
|
||||
|
||||
static void
|
||||
nfc_generate_mf_classic_common(NfcDeviceData* data, uint8_t uid_len, MfClassicType type) {
|
||||
data->nfc_data.type = FuriHalNfcTypeA;
|
||||
data->nfc_data.interface = FuriHalNfcInterfaceRf;
|
||||
data->nfc_data.uid_len = uid_len;
|
||||
nfc_generate_mf_classic_block_0(data->mf_classic_data.block[0].value, uid_len);
|
||||
data->nfc_data.atqa[0] = 0x44;
|
||||
data->nfc_data.atqa[1] = 0x00;
|
||||
data->nfc_data.sak = 0x08;
|
||||
data->protocol = NfcDeviceProtocolMifareClassic;
|
||||
data->mf_classic_data.type = type;
|
||||
}
|
||||
|
||||
static void nfc_generate_calc_bcc(uint8_t* uid, uint8_t* bcc0, uint8_t* bcc1) {
|
||||
*bcc0 = 0x88 ^ uid[0] ^ uid[1] ^ uid[2];
|
||||
*bcc1 = uid[3] ^ uid[4] ^ uid[5] ^ uid[6];
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_copy_uid_with_bcc(NfcDeviceData* data) {
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
memcpy(mful->data, data->nfc_data.uid, 3);
|
||||
memcpy(&mful->data[4], &data->nfc_data.uid[3], 4);
|
||||
nfc_generate_calc_bcc(data->nfc_data.uid, &mful->data[3], &mful->data[8]);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_orig(NfcDeviceData* data) {
|
||||
nfc_generate_common_start(data);
|
||||
nfc_generate_mf_ul_common(data);
|
||||
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeUnknown;
|
||||
mful->data_size = 16 * 4;
|
||||
mful->data_read = mful->data_size;
|
||||
nfc_generate_mf_ul_copy_uid_with_bcc(data);
|
||||
// TODO: what's internal byte on page 2?
|
||||
memset(&mful->data[4 * 4], 0xFF, 4);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_ntag203(NfcDeviceData* data) {
|
||||
nfc_generate_common_start(data);
|
||||
nfc_generate_mf_ul_common(data);
|
||||
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeNTAG203;
|
||||
mful->data_size = 42 * 4;
|
||||
mful->data_read = mful->data_size;
|
||||
nfc_generate_mf_ul_copy_uid_with_bcc(data);
|
||||
mful->data[9] = 0x48; // Internal byte
|
||||
memcpy(&mful->data[3 * 4], default_data_ntag203, sizeof(default_data_ntag203));
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_with_config_common(NfcDeviceData* data, uint8_t num_pages) {
|
||||
nfc_generate_common_start(data);
|
||||
nfc_generate_mf_ul_common(data);
|
||||
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->data_size = num_pages * 4;
|
||||
mful->data_read = mful->data_size;
|
||||
nfc_generate_mf_ul_copy_uid_with_bcc(data);
|
||||
uint16_t config_index = (num_pages - 4) * 4;
|
||||
mful->data[config_index] = 0x04; // STRG_MOD_EN
|
||||
mful->data[config_index + 3] = 0xFF; // AUTH0
|
||||
mful->data[config_index + 5] = 0x05; // VCTID
|
||||
memset(&mful->data[config_index + 8], 0xFF, 4); // Default PWD
|
||||
if(num_pages > 20) mful->data[config_index - 1] = MF_UL_TEARING_FLAG_DEFAULT;
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_ev1_common(NfcDeviceData* data, uint8_t num_pages) {
|
||||
nfc_generate_mf_ul_with_config_common(data, num_pages);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
memcpy(&mful->version, version_bytes_mf0ulx1, sizeof(version_bytes_mf0ulx1));
|
||||
for(size_t i = 0; i < 3; ++i) {
|
||||
mful->tearing[i] = MF_UL_TEARING_FLAG_DEFAULT;
|
||||
}
|
||||
// TODO: what's internal byte on page 2?
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_11(NfcDeviceData* data) {
|
||||
nfc_generate_mf_ul_ev1_common(data, 20);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeUL11;
|
||||
mful->version.prod_subtype = 0x01;
|
||||
mful->version.storage_size = 0x0B;
|
||||
mful->data[16 * 4] = 0x00; // Low capacitance version does not have STRG_MOD_EN
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_h11(NfcDeviceData* data) {
|
||||
nfc_generate_mf_ul_ev1_common(data, 20);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeUL11;
|
||||
mful->version.prod_subtype = 0x02;
|
||||
mful->version.storage_size = 0x0B;
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_21(NfcDeviceData* data) {
|
||||
nfc_generate_mf_ul_ev1_common(data, 41);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeUL21;
|
||||
mful->version.prod_subtype = 0x01;
|
||||
mful->version.storage_size = 0x0E;
|
||||
mful->data[37 * 4] = 0x00; // Low capacitance version does not have STRG_MOD_EN
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_ul_h21(NfcDeviceData* data) {
|
||||
nfc_generate_mf_ul_ev1_common(data, 41);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeUL21;
|
||||
mful->version.prod_subtype = 0x02;
|
||||
mful->version.storage_size = 0x0E;
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag21x_common(NfcDeviceData* data, uint8_t num_pages) {
|
||||
nfc_generate_mf_ul_with_config_common(data, num_pages);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
memcpy(&mful->version, version_bytes_ntag21x, sizeof(version_bytes_mf0ulx1));
|
||||
mful->data[9] = 0x48; // Internal byte
|
||||
// Capability container
|
||||
mful->data[12] = 0xE1;
|
||||
mful->data[13] = 0x10;
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag213(NfcDeviceData* data) {
|
||||
nfc_generate_ntag21x_common(data, 45);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeNTAG213;
|
||||
mful->version.storage_size = 0x0F;
|
||||
mful->data[14] = 0x12;
|
||||
// Default contents
|
||||
memcpy(&mful->data[16], default_data_ntag213, sizeof(default_data_ntag213));
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag215(NfcDeviceData* data) {
|
||||
nfc_generate_ntag21x_common(data, 135);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeNTAG215;
|
||||
mful->version.storage_size = 0x11;
|
||||
mful->data[14] = 0x3E;
|
||||
// Default contents
|
||||
memcpy(&mful->data[16], default_data_ntag215_216, sizeof(default_data_ntag215_216));
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag216(NfcDeviceData* data) {
|
||||
nfc_generate_ntag21x_common(data, 231);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = MfUltralightTypeNTAG216;
|
||||
mful->version.storage_size = 0x13;
|
||||
mful->data[14] = 0x6D;
|
||||
// Default contents
|
||||
memcpy(&mful->data[16], default_data_ntag215_216, sizeof(default_data_ntag215_216));
|
||||
}
|
||||
|
||||
static void
|
||||
nfc_generate_ntag_i2c_common(NfcDeviceData* data, MfUltralightType type, uint16_t num_pages) {
|
||||
nfc_generate_common_start(data);
|
||||
nfc_generate_mf_ul_common(data);
|
||||
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->type = type;
|
||||
memcpy(&mful->version, version_bytes_ntag_i2c, sizeof(version_bytes_ntag_i2c));
|
||||
mful->data_size = num_pages * 4;
|
||||
mful->data_read = mful->data_size;
|
||||
memcpy(mful->data, data->nfc_data.uid, data->nfc_data.uid_len);
|
||||
mful->data[7] = data->nfc_data.sak;
|
||||
mful->data[8] = data->nfc_data.atqa[0];
|
||||
mful->data[9] = data->nfc_data.atqa[1];
|
||||
|
||||
uint16_t config_register_page;
|
||||
uint16_t session_register_page;
|
||||
|
||||
// Sync with mifare_ultralight.c
|
||||
switch(type) {
|
||||
case MfUltralightTypeNTAGI2C1K:
|
||||
config_register_page = 227;
|
||||
session_register_page = 229;
|
||||
break;
|
||||
case MfUltralightTypeNTAGI2C2K:
|
||||
config_register_page = 481;
|
||||
session_register_page = 483;
|
||||
break;
|
||||
case MfUltralightTypeNTAGI2CPlus1K:
|
||||
case MfUltralightTypeNTAGI2CPlus2K:
|
||||
config_register_page = 232;
|
||||
session_register_page = 234;
|
||||
break;
|
||||
default:
|
||||
furi_crash("Unknown MFUL");
|
||||
break;
|
||||
}
|
||||
|
||||
memcpy(
|
||||
&mful->data[config_register_page * 4],
|
||||
default_config_ntag_i2c,
|
||||
sizeof(default_config_ntag_i2c));
|
||||
memcpy(
|
||||
&mful->data[session_register_page * 4],
|
||||
default_config_ntag_i2c,
|
||||
sizeof(default_config_ntag_i2c));
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag_i2c_1k(NfcDeviceData* data) {
|
||||
nfc_generate_ntag_i2c_common(data, MfUltralightTypeNTAGI2C1K, 231);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->version.prod_ver_minor = 0x01;
|
||||
mful->version.storage_size = 0x13;
|
||||
|
||||
memcpy(&mful->data[12], default_data_ntag_i2c, sizeof(default_data_ntag_i2c));
|
||||
mful->data[14] = 0x6D; // Size of tag in CC
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag_i2c_2k(NfcDeviceData* data) {
|
||||
nfc_generate_ntag_i2c_common(data, MfUltralightTypeNTAGI2C2K, 485);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->version.prod_ver_minor = 0x01;
|
||||
mful->version.storage_size = 0x15;
|
||||
|
||||
memcpy(&mful->data[12], default_data_ntag_i2c, sizeof(default_data_ntag_i2c));
|
||||
mful->data[14] = 0xEA; // Size of tag in CC
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag_i2c_plus_common(
|
||||
NfcDeviceData* data,
|
||||
MfUltralightType type,
|
||||
uint16_t num_pages) {
|
||||
nfc_generate_ntag_i2c_common(data, type, num_pages);
|
||||
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
uint16_t config_index = 227 * 4;
|
||||
mful->data[config_index + 3] = 0xFF; // AUTH0
|
||||
memset(&mful->data[config_index + 8], 0xFF, 4); // Default PWD
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag_i2c_plus_1k(NfcDeviceData* data) {
|
||||
nfc_generate_ntag_i2c_plus_common(data, MfUltralightTypeNTAGI2CPlus1K, 236);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->version.prod_ver_minor = 0x02;
|
||||
mful->version.storage_size = 0x13;
|
||||
}
|
||||
|
||||
static void nfc_generate_ntag_i2c_plus_2k(NfcDeviceData* data) {
|
||||
nfc_generate_ntag_i2c_plus_common(data, MfUltralightTypeNTAGI2CPlus2K, 492);
|
||||
MfUltralightData* mful = &data->mf_ul_data;
|
||||
mful->version.prod_ver_minor = 0x02;
|
||||
mful->version.storage_size = 0x15;
|
||||
}
|
||||
|
||||
void nfc_generate_mf_classic(NfcDeviceData* data, uint8_t uid_len, MfClassicType type) {
|
||||
nfc_generate_common_start(data);
|
||||
nfc_generate_mf_classic_common(data, uid_len, type);
|
||||
|
||||
// Set the UID
|
||||
data->nfc_data.uid[0] = NXP_MANUFACTURER_ID;
|
||||
for(int i = 1; i < uid_len; i++) {
|
||||
data->nfc_data.uid[i] = data->mf_classic_data.block[0].value[i];
|
||||
}
|
||||
|
||||
MfClassicData* mfc = &data->mf_classic_data;
|
||||
mf_classic_set_block_read(mfc, 0, &mfc->block[0]);
|
||||
|
||||
if(type == MfClassicType4k) {
|
||||
// Set every block to 0xFF
|
||||
for(uint16_t i = 1; i < 256; i += 1) {
|
||||
if(mf_classic_is_sector_trailer(i)) {
|
||||
nfc_generate_mf_classic_sector_trailer(mfc, i);
|
||||
} else {
|
||||
memset(&mfc->block[i].value, 0xFF, 16);
|
||||
}
|
||||
mf_classic_set_block_read(mfc, i, &mfc->block[i]);
|
||||
}
|
||||
// Set SAK to 18
|
||||
data->nfc_data.sak = 0x18;
|
||||
|
||||
} else if(type == MfClassicType1k) {
|
||||
// Set every block to 0xFF
|
||||
for(uint16_t i = 1; i < MF_CLASSIC_1K_TOTAL_SECTORS_NUM * 4; i += 1) {
|
||||
if(mf_classic_is_sector_trailer(i)) {
|
||||
nfc_generate_mf_classic_sector_trailer(mfc, i);
|
||||
} else {
|
||||
memset(&mfc->block[i].value, 0xFF, 16);
|
||||
}
|
||||
mf_classic_set_block_read(mfc, i, &mfc->block[i]);
|
||||
}
|
||||
// Set SAK to 08
|
||||
data->nfc_data.sak = 0x08;
|
||||
}
|
||||
|
||||
mfc->type = type;
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_classic_1k_4b_uid(NfcDeviceData* data) {
|
||||
nfc_generate_mf_classic(data, 4, MfClassicType1k);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_classic_1k_7b_uid(NfcDeviceData* data) {
|
||||
nfc_generate_mf_classic(data, 7, MfClassicType1k);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_classic_4k_4b_uid(NfcDeviceData* data) {
|
||||
nfc_generate_mf_classic(data, 4, MfClassicType4k);
|
||||
}
|
||||
|
||||
static void nfc_generate_mf_classic_4k_7b_uid(NfcDeviceData* data) {
|
||||
nfc_generate_mf_classic(data, 7, MfClassicType4k);
|
||||
}
|
||||
|
||||
static const NfcGenerator mf_ul_generator = {
|
||||
.name = "Mifare Ultralight",
|
||||
.generator_func = nfc_generate_mf_ul_orig,
|
||||
};
|
||||
|
||||
static const NfcGenerator mf_ul_11_generator = {
|
||||
.name = "Mifare Ultralight EV1 11",
|
||||
.generator_func = nfc_generate_mf_ul_11,
|
||||
};
|
||||
|
||||
static const NfcGenerator mf_ul_h11_generator = {
|
||||
.name = "Mifare Ultralight EV1 H11",
|
||||
.generator_func = nfc_generate_mf_ul_h11,
|
||||
};
|
||||
|
||||
static const NfcGenerator mf_ul_21_generator = {
|
||||
.name = "Mifare Ultralight EV1 21",
|
||||
.generator_func = nfc_generate_mf_ul_21,
|
||||
};
|
||||
|
||||
static const NfcGenerator mf_ul_h21_generator = {
|
||||
.name = "Mifare Ultralight EV1 H21",
|
||||
.generator_func = nfc_generate_mf_ul_h21,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag203_generator = {
|
||||
.name = "NTAG203",
|
||||
.generator_func = nfc_generate_mf_ul_ntag203,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag213_generator = {
|
||||
.name = "NTAG213",
|
||||
.generator_func = nfc_generate_ntag213,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag215_generator = {
|
||||
.name = "NTAG215",
|
||||
.generator_func = nfc_generate_ntag215,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag216_generator = {
|
||||
.name = "NTAG216",
|
||||
.generator_func = nfc_generate_ntag216,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag_i2c_1k_generator = {
|
||||
.name = "NTAG I2C 1k",
|
||||
.generator_func = nfc_generate_ntag_i2c_1k,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag_i2c_2k_generator = {
|
||||
.name = "NTAG I2C 2k",
|
||||
.generator_func = nfc_generate_ntag_i2c_2k,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag_i2c_plus_1k_generator = {
|
||||
.name = "NTAG I2C Plus 1k",
|
||||
.generator_func = nfc_generate_ntag_i2c_plus_1k,
|
||||
};
|
||||
|
||||
static const NfcGenerator ntag_i2c_plus_2k_generator = {
|
||||
.name = "NTAG I2C Plus 2k",
|
||||
.generator_func = nfc_generate_ntag_i2c_plus_2k,
|
||||
};
|
||||
|
||||
static const NfcGenerator mifare_classic_1k_4b_uid_generator = {
|
||||
.name = "Mifare Classic 1k 4byte UID",
|
||||
.generator_func = nfc_generate_mf_classic_1k_4b_uid,
|
||||
};
|
||||
|
||||
static const NfcGenerator mifare_classic_1k_7b_uid_generator = {
|
||||
.name = "Mifare Classic 1k 7byte UID",
|
||||
.generator_func = nfc_generate_mf_classic_1k_7b_uid,
|
||||
};
|
||||
|
||||
static const NfcGenerator mifare_classic_4k_4b_uid_generator = {
|
||||
.name = "Mifare Classic 4k 4byte UID",
|
||||
.generator_func = nfc_generate_mf_classic_4k_4b_uid,
|
||||
};
|
||||
|
||||
static const NfcGenerator mifare_classic_4k_7b_uid_generator = {
|
||||
.name = "Mifare Classic 4k 7byte UID",
|
||||
.generator_func = nfc_generate_mf_classic_4k_7b_uid,
|
||||
};
|
||||
|
||||
const NfcGenerator* const nfc_generators[] = {
|
||||
&mf_ul_generator,
|
||||
&mf_ul_11_generator,
|
||||
&mf_ul_h11_generator,
|
||||
&mf_ul_21_generator,
|
||||
&mf_ul_h21_generator,
|
||||
&ntag203_generator,
|
||||
&ntag213_generator,
|
||||
&ntag215_generator,
|
||||
&ntag216_generator,
|
||||
&ntag_i2c_1k_generator,
|
||||
&ntag_i2c_2k_generator,
|
||||
&ntag_i2c_plus_1k_generator,
|
||||
&ntag_i2c_plus_2k_generator,
|
||||
&mifare_classic_1k_4b_uid_generator,
|
||||
&mifare_classic_1k_7b_uid_generator,
|
||||
&mifare_classic_4k_4b_uid_generator,
|
||||
&mifare_classic_4k_7b_uid_generator,
|
||||
NULL,
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "../nfc_device.h"
|
||||
|
||||
typedef void (*NfcGeneratorFunc)(NfcDeviceData* data);
|
||||
|
||||
typedef struct {
|
||||
const char* name;
|
||||
NfcGeneratorFunc generator_func;
|
||||
} NfcGenerator;
|
||||
|
||||
extern const NfcGenerator* const nfc_generators[];
|
||||
|
||||
void nfc_generate_mf_classic(NfcDeviceData* data, uint8_t uid_len, MfClassicType type);
|
||||
@@ -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 = {
|
||||
|
||||
@@ -36,5 +36,6 @@
|
||||
#include "intertechno_v3.h"
|
||||
#include "clemsa.h"
|
||||
#include "ansonic.h"
|
||||
#include "smc5326.h"
|
||||
|
||||
extern const SubGhzProtocolRegistry subghz_protocol_registry;
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
@@ -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);
|
||||
Reference in New Issue
Block a user