[FL-2529][FL-1628] New LF-RFID subsystem (#1601)

* Makefile: unit tests pack
* RFID: pulse joiner and its unit test
* Move pulse protocol helpers to appropriate place
* Drop pulse_joiner tests
* Generic protocol, protocols dictionary, unit test
* Protocol dict unit test
* iButton: protocols dictionary
* Lib: varint
* Lib: profiler
* Unit test: varint
* rfid: worker mockup
* LFRFID: em4100 unit test
* Storage: file_exist function
* rfid: fsk osc
* rfid: generic fsk demodulator
* rfid: protocol em4100
* rfid: protocol h10301
* rfid: protocol io prox xsf
* Unit test: rfid protocols
* rfid: new hal
* rfid: raw worker
* Unit test: fix error output
* rfid: worker
* rfid: plain c cli
* fw: migrate to scons
* lfrfid: full io prox support
* unit test: io prox protocol
* SubGHZ: move bit defines to source
* FSK oscillator: level duration compability
* libs: bit manipulation library
* lfrfid: ioprox protocol, use bit library and new level duration method of FSK ocillator
* bit lib: unit tests
* Bit lib: parity tests, remove every nth bit, copy bits
* Lfrfid: awid protocol
* bit lib: uint16 and uint32 getters, unit tests
* lfrfid: FDX-B read, draft version
* Minunit: better memeq assert
* bit lib: reverse, print, print regions
* Protocol dict: get protocol features, get protocol validate count
* lfrfid worker: improved read
* lfrfid raw worker: psk support
* Cli: rfid plain C cli
* protocol AWID: render
* protocol em4100: render
* protocol h10301: render
* protocol indala26: support every indala 26 scramble
* Protocol IO Prox: render
* Protocol FDX-B: advanced read
* lfrfid: remove unused test function
* lfrfid: fix os primitives
* bit lib: crc16 and unit tests
* FDX-B: save data
* lfrfid worker: increase stream size. Alloc raw worker only when needed.
* lfrfid: indala26 emulation
* lfrfid: prepare to write
* lfrfid: fdx-b emulation
* lfrfid: awid, ioprox write
* lfrfid: write t55xx w\o validation
* lfrfid: better t55xx block0 handling
* lfrfid: use new t5577 functions in worker
* lfrfid: improve protocol description
* lfrfid: write and verify
* lfrfid: delete cpp cli
* lfrfid: improve worker usage
* lfrfid-app: step to new worker
* lfrfid: old indala (I40134) load fallback
* lfrfid: indala26, recover wrong synced data
* lfrfid: remove old worker
* lfrfid app: dummy read screen
* lfrfid app: less dummy read screen
* lfrfid: generic 96-bit HID protocol (covers up to HID 37-bit)
* rename
* lfrfid: improve indala26 read
* lfrfid: generic 192-bit HID protocol (covers all HID extended)
* lfrfid: TODO about HID render
* lfrfid: new protocol FDX-A
* lfrfid-app: correct worker stop on exit
* misc fixes
* lfrfid: FDX-A and HID distinguishability has been fixed.
* lfrfid: decode HID size header and render it (#1612)
* lfrfid: rename HID96 and HID192 to HIDProx and HIDExt
* lfrfid: extra actions scene
* lfrfid: decode generic HID Proximity size lazily (#1618)
* lib: stream of data buffers concept
* lfrfid: raw file helper
* lfrfid: changed raw worker api
* lfrfid: packed varint pair
* lfrfid: read stream speedup
* lfrfid app: show read mode
* Documentation
* lfrfid app: raw read gui
* lfrfid app: storage check for raw read
* memleak fix
* review fixes
* lfrfid app: read blink color
* lfrfid app: reset key name after read
* review fixes
* lfrfid app: fix copypasted text
* review fixes
* lfrfid: disable debug gpio
* lfrfid: card detection events
* lfrfid: change validation color from magenta to green
* Update core_defines.
* lfrfid: prefix fdx-b id by zeroes
* lfrfid: parse up to 43-bit HID Proximity keys (#1640)
* Fbt: downgrade toolchain and fix PS1
* lfrfid: fix unit tests
* lfrfid app: remove printf
* lfrfid: indala26, use bit 55 as data
* lfrfid: indala26, better brief format
* lfrfid: indala26, loading fallback
* lfrfid: read timing tuning

Co-authored-by: James Ide <ide@users.noreply.github.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2022-08-24 01:57:39 +10:00
committed by GitHub
parent f92127c0a7
commit 9bfb641d3e
179 changed files with 10234 additions and 4804 deletions

291
lib/lfrfid/tools/bit_lib.c Normal file
View File

@@ -0,0 +1,291 @@
#include "bit_lib.h"
#include <core/check.h>
#include <stdio.h>
void bit_lib_push_bit(uint8_t* data, size_t data_size, bool bit) {
size_t last_index = data_size - 1;
for(size_t i = 0; i < last_index; ++i) {
data[i] = (data[i] << 1) | ((data[i + 1] >> 7) & 1);
}
data[last_index] = (data[last_index] << 1) | bit;
}
void bit_lib_set_bit(uint8_t* data, size_t position, bool bit) {
if(bit) {
data[position / 8] |= 1UL << (7 - (position % 8));
} else {
data[position / 8] &= ~(1UL << (7 - (position % 8)));
}
}
void bit_lib_set_bits(uint8_t* data, size_t position, uint8_t byte, uint8_t length) {
furi_check(length <= 8);
furi_check(length > 0);
for(uint8_t i = 0; i < length; ++i) {
uint8_t shift = (length - 1) - i;
bit_lib_set_bit(data, position + i, (byte >> shift) & 1);
}
}
bool bit_lib_get_bit(const uint8_t* data, size_t position) {
return (data[position / 8] >> (7 - (position % 8))) & 1;
}
uint8_t bit_lib_get_bits(const uint8_t* data, size_t position, uint8_t length) {
uint8_t shift = position % 8;
if(shift == 0) {
return data[position / 8] >> (8 - length);
} else {
// TODO fix read out of bounds
uint8_t value = (data[position / 8] << (shift));
value |= data[position / 8 + 1] >> (8 - shift);
value = value >> (8 - length);
return value;
}
}
uint16_t bit_lib_get_bits_16(const uint8_t* data, size_t position, uint8_t length) {
uint16_t value = 0;
if(length <= 8) {
value = bit_lib_get_bits(data, position, length);
} else {
value = bit_lib_get_bits(data, position, 8) << (length - 8);
value |= bit_lib_get_bits(data, position + 8, length - 8);
}
return value;
}
uint32_t bit_lib_get_bits_32(const uint8_t* data, size_t position, uint8_t length) {
uint32_t value = 0;
if(length <= 8) {
value = bit_lib_get_bits(data, position, length);
} else if(length <= 16) {
value = bit_lib_get_bits(data, position, 8) << (length - 8);
value |= bit_lib_get_bits(data, position + 8, length - 8);
} else if(length <= 24) {
value = bit_lib_get_bits(data, position, 8) << (length - 8);
value |= bit_lib_get_bits(data, position + 8, 8) << (length - 16);
value |= bit_lib_get_bits(data, position + 16, length - 16);
} else {
value = bit_lib_get_bits(data, position, 8) << (length - 8);
value |= bit_lib_get_bits(data, position + 8, 8) << (length - 16);
value |= bit_lib_get_bits(data, position + 16, 8) << (length - 24);
value |= bit_lib_get_bits(data, position + 24, length - 24);
}
return value;
}
bool bit_lib_test_parity_32(uint32_t bits, BitLibParity parity) {
#if !defined __GNUC__
#error Please, implement parity test for non-GCC compilers
#else
switch(parity) {
case BitLibParityEven:
return __builtin_parity(bits);
case BitLibParityOdd:
return !__builtin_parity(bits);
default:
furi_crash("Unknown parity");
}
#endif
}
bool bit_lib_test_parity(
const uint8_t* bits,
size_t position,
uint8_t length,
BitLibParity parity,
uint8_t parity_length) {
uint32_t parity_block;
bool result = true;
const size_t parity_blocks_count = length / parity_length;
for(size_t i = 0; i < parity_blocks_count; ++i) {
switch(parity) {
case BitLibParityEven:
case BitLibParityOdd:
parity_block = bit_lib_get_bits_32(bits, position + i * parity_length, parity_length);
if(!bit_lib_test_parity_32(parity_block, parity)) {
result = false;
}
break;
case BitLibParityAlways0:
if(bit_lib_get_bit(bits, position + i * parity_length + parity_length - 1)) {
result = false;
}
break;
case BitLibParityAlways1:
if(!bit_lib_get_bit(bits, position + i * parity_length + parity_length - 1)) {
result = false;
}
break;
}
if(!result) break;
}
return result;
}
size_t bit_lib_remove_bit_every_nth(uint8_t* data, size_t position, uint8_t length, uint8_t n) {
size_t counter = 0;
size_t result_counter = 0;
uint8_t bit_buffer = 0;
uint8_t bit_counter = 0;
while(counter < length) {
if((counter + 1) % n != 0) {
bit_buffer = (bit_buffer << 1) | bit_lib_get_bit(data, position + counter);
bit_counter++;
}
if(bit_counter == 8) {
bit_lib_set_bits(data, position + result_counter, bit_buffer, 8);
bit_counter = 0;
bit_buffer = 0;
result_counter += 8;
}
counter++;
}
if(bit_counter != 0) {
bit_lib_set_bits(data, position + result_counter, bit_buffer, bit_counter);
result_counter += bit_counter;
}
return result_counter;
}
void bit_lib_copy_bits(
uint8_t* data,
size_t position,
size_t length,
const uint8_t* source,
size_t source_position) {
for(size_t i = 0; i < length; ++i) {
bit_lib_set_bit(data, position + i, bit_lib_get_bit(source, source_position + i));
}
}
void bit_lib_reverse_bits(uint8_t* data, size_t position, uint8_t length) {
size_t i = 0;
size_t j = length - 1;
while(i < j) {
bool tmp = bit_lib_get_bit(data, position + i);
bit_lib_set_bit(data, position + i, bit_lib_get_bit(data, position + j));
bit_lib_set_bit(data, position + j, tmp);
i++;
j--;
}
}
uint8_t bit_lib_get_bit_count(uint32_t data) {
#if defined __GNUC__
return __builtin_popcountl(data);
#else
#error Please, implement popcount for non-GCC compilers
#endif
}
void bit_lib_print_bits(const uint8_t* data, size_t length) {
for(size_t i = 0; i < length; ++i) {
printf("%u", bit_lib_get_bit(data, i));
}
}
void bit_lib_print_regions(
const BitLibRegion* regions,
size_t region_count,
const uint8_t* data,
size_t length) {
// print data
bit_lib_print_bits(data, length);
printf("\r\n");
// print regions
for(size_t c = 0; c < length; ++c) {
bool print = false;
for(size_t i = 0; i < region_count; i++) {
if(regions[i].start <= c && c < regions[i].start + regions[i].length) {
print = true;
printf("%c", regions[i].mark);
break;
}
}
if(!print) {
printf(" ");
}
}
printf("\r\n");
// print regions data
for(size_t c = 0; c < length; ++c) {
bool print = false;
for(size_t i = 0; i < region_count; i++) {
if(regions[i].start <= c && c < regions[i].start + regions[i].length) {
print = true;
printf("%u", bit_lib_get_bit(data, c));
break;
}
}
if(!print) {
printf(" ");
}
}
printf("\r\n");
}
uint16_t bit_lib_reverse_16_fast(uint16_t data) {
uint16_t result = 0;
result |= (data & 0x8000) >> 15;
result |= (data & 0x4000) >> 13;
result |= (data & 0x2000) >> 11;
result |= (data & 0x1000) >> 9;
result |= (data & 0x0800) >> 7;
result |= (data & 0x0400) >> 5;
result |= (data & 0x0200) >> 3;
result |= (data & 0x0100) >> 1;
result |= (data & 0x0080) << 1;
result |= (data & 0x0040) << 3;
result |= (data & 0x0020) << 5;
result |= (data & 0x0010) << 7;
result |= (data & 0x0008) << 9;
result |= (data & 0x0004) << 11;
result |= (data & 0x0002) << 13;
result |= (data & 0x0001) << 15;
return result;
}
uint16_t bit_lib_crc16(
uint8_t const* data,
size_t data_size,
uint16_t polynom,
uint16_t init,
bool ref_in,
bool ref_out,
uint16_t xor_out) {
uint16_t crc = init;
for(size_t i = 0; i < data_size; ++i) {
uint8_t byte = data[i];
if(ref_in) byte = bit_lib_reverse_16_fast(byte) >> 8;
for(size_t j = 0; j < 8; ++j) {
bool c15 = (crc >> 15 & 1);
bool bit = (byte >> (7 - j) & 1);
crc <<= 1;
if(c15 ^ bit) crc ^= polynom;
}
}
if(ref_out) crc = bit_lib_reverse_16_fast(crc);
crc ^= xor_out;
return crc;
}

220
lib/lfrfid/tools/bit_lib.h Normal file
View File

@@ -0,0 +1,220 @@
#pragma once
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
BitLibParityEven,
BitLibParityOdd,
BitLibParityAlways0,
BitLibParityAlways1,
} BitLibParity;
/** @brief Increment and wrap around a value.
* @param index value to increment
* @param length wrap-around range
*/
#define bit_lib_increment_index(index, length) (index = (((index) + 1) % (length)))
/** @brief Test if a bit is set.
* @param data value to test
* @param index bit index to test
*/
#define bit_lib_bit_is_set(data, index) ((data & (1 << (index))) != 0)
/** @brief Test if a bit is not set.
* @param data value to test
* @param index bit index to test
*/
#define bit_lib_bit_is_not_set(data, index) ((data & (1 << (index))) == 0)
/** @brief Push a bit into a byte array.
* @param data array to push bit into
* @param data_size array size
* @param bit bit to push
*/
void bit_lib_push_bit(uint8_t* data, size_t data_size, bool bit);
/** @brief Set a bit in a byte array.
* @param data array to set bit in
* @param position The position of the bit to set.
* @param bit bit value to set
*/
void bit_lib_set_bit(uint8_t* data, size_t position, bool bit);
/** @brief Set the bit at the given position to the given value.
* @param data The data to set the bit in.
* @param position The position of the bit to set.
* @param byte The data to set the bit to.
* @param length The length of the data.
*/
void bit_lib_set_bits(uint8_t* data, size_t position, uint8_t byte, uint8_t length);
/** @brief Get the bit of a byte.
* @param data The byte to get the bits from.
* @param position The position of the bit.
* @return The bit.
*/
bool bit_lib_get_bit(const uint8_t* data, size_t position);
/**
* @brief Get the bits of a data, as uint8_t.
* @param data The data to get the bits from.
* @param position The position of the first bit.
* @param length The length of the bits.
* @return The bits.
*/
uint8_t bit_lib_get_bits(const uint8_t* data, size_t position, uint8_t length);
/**
* @brief Get the bits of a data, as uint16_t.
* @param data The data to get the bits from.
* @param position The position of the first bit.
* @param length The length of the bits.
* @return The bits.
*/
uint16_t bit_lib_get_bits_16(const uint8_t* data, size_t position, uint8_t length);
/**
* @brief Get the bits of a data, as uint32_t.
* @param data The data to get the bits from.
* @param position The position of the first bit.
* @param length The length of the bits.
* @return The bits.
*/
uint32_t bit_lib_get_bits_32(const uint8_t* data, size_t position, uint8_t length);
/**
* @brief Test parity of given bits
* @param bits Bits to test parity of
* @param parity Parity to test against
* @return true if parity is correct, false otherwise
*/
bool bit_lib_test_parity_32(uint32_t bits, BitLibParity parity);
/**
* @brief Test parity of bit array, check parity for every parity_length block from start
*
* @param data Bit array
* @param position Start position
* @param length Bit count
* @param parity Parity to test against
* @param parity_length Parity block length
* @return true
* @return false
*/
bool bit_lib_test_parity(
const uint8_t* data,
size_t position,
uint8_t length,
BitLibParity parity,
uint8_t parity_length);
/**
* @brief Remove bit every n in array and shift array left. Useful to remove parity.
*
* @param data Bit array
* @param position Start position
* @param length Bit count
* @param n every n bit will be removed
* @return size_t
*/
size_t bit_lib_remove_bit_every_nth(uint8_t* data, size_t position, uint8_t length, uint8_t n);
/**
* @brief Copy bits from source to destination.
*
* @param data destination array
* @param position position in destination array
* @param length length of bits to copy
* @param source source array
* @param source_position position in source array
*/
void bit_lib_copy_bits(
uint8_t* data,
size_t position,
size_t length,
const uint8_t* source,
size_t source_position);
/**
* @brief Reverse bits in bit array
*
* @param data Bit array
* @param position start position
* @param length length of bits to reverse
*/
void bit_lib_reverse_bits(uint8_t* data, size_t position, uint8_t length);
/**
* @brief Count 1 bits in data
*
* @param data
* @return uint8_t set bit count
*/
uint8_t bit_lib_get_bit_count(uint32_t data);
/**
* @brief Print data as bit array
*
* @param data
* @param length
*/
void bit_lib_print_bits(const uint8_t* data, size_t length);
typedef struct {
const char mark;
const size_t start;
const size_t length;
} BitLibRegion;
/**
* @brief Print data as bit array and mark regions. Regions needs to be sorted by start position.
*
* @param regions
* @param region_count
* @param data
* @param length
*/
void bit_lib_print_regions(
const BitLibRegion* regions,
size_t region_count,
const uint8_t* data,
size_t length);
/**
* @brief Reverse bits in uint16_t, faster than generic bit_lib_reverse_bits.
*
* @param data
* @return uint16_t
*/
uint16_t bit_lib_reverse_16_fast(uint16_t data);
/**
* @brief Slow, but generic CRC16 implementation
*
* @param data
* @param data_size
* @param polynom CRC polynom
* @param init init value
* @param ref_in true if the right bit is older
* @param ref_out true to reverse output
* @param xor_out xor output with this value
* @return uint16_t
*/
uint16_t bit_lib_crc16(
uint8_t const* data,
size_t data_size,
uint16_t polynom,
uint16_t init,
bool ref_in,
bool ref_out,
uint16_t xor_out);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,93 @@
#include <furi.h>
#include "fsk_demod.h"
struct FSKDemod {
uint32_t low_time;
uint32_t low_pulses;
uint32_t hi_time;
uint32_t hi_pulses;
bool invert;
uint32_t mid_time;
uint32_t time;
uint32_t count;
bool last_pulse;
};
FSKDemod*
fsk_demod_alloc(uint32_t low_time, uint32_t low_pulses, uint32_t hi_time, uint32_t hi_pulses) {
FSKDemod* demod = malloc(sizeof(FSKDemod));
demod->invert = false;
if(low_time > hi_time) {
uint32_t tmp;
tmp = hi_time;
hi_time = low_time;
low_time = tmp;
tmp = hi_pulses;
hi_pulses = low_pulses;
low_pulses = tmp;
demod->invert = true;
}
demod->low_time = low_time;
demod->low_pulses = low_pulses;
demod->hi_time = hi_time;
demod->hi_pulses = hi_pulses;
demod->mid_time = (hi_time - low_time) / 2 + low_time;
demod->time = 0;
demod->count = 0;
demod->last_pulse = false;
return demod;
}
void fsk_demod_free(FSKDemod* demod) {
free(demod);
}
void fsk_demod_feed(FSKDemod* demod, bool polarity, uint32_t time, bool* value, uint32_t* count) {
*count = 0;
if(polarity) {
// accumulate time
demod->time = time;
} else {
demod->time += time;
// check for valid pulse
if(demod->time >= demod->low_time && demod->time < demod->hi_time) {
bool pulse;
if(demod->time < demod->mid_time) {
pulse = false;
} else {
pulse = true;
}
demod->count++;
// check for edge transition
if(demod->last_pulse != pulse) {
uint32_t data_count = demod->count + 1;
if(demod->last_pulse) {
data_count /= demod->hi_pulses;
*value = !demod->invert;
} else {
data_count /= demod->low_pulses;
*value = demod->invert;
}
*count = data_count;
demod->count = 0;
demod->last_pulse = pulse;
}
} else {
demod->count = 0;
}
}
}

View File

@@ -0,0 +1,44 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct FSKDemod FSKDemod;
/**
* @brief Allocate a new FSKDemod instance
* FSKDemod is a demodulator that can decode FSK encoded data
*
* @param low_time time between rising edges for the 0 bit
* @param low_pulses rising edges count for the 0 bit
* @param hi_time time between rising edges for the 1 bit
* @param hi_pulses rising edges count for the 1 bit
* @return FSKDemod*
*/
FSKDemod*
fsk_demod_alloc(uint32_t low_time, uint32_t low_pulses, uint32_t hi_time, uint32_t hi_pulses);
/**
* @brief Free a FSKDemod instance
*
* @param fsk_demod
*/
void fsk_demod_free(FSKDemod* fsk_demod);
/**
* @brief Feed sample to demodulator
*
* @param demod FSKDemod instance
* @param polarity sample polarity
* @param time sample time
* @param value demodulated bit value
* @param count demodulated bit count
*/
void fsk_demod_feed(FSKDemod* demod, bool polarity, uint32_t time, bool* value, uint32_t* count);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,62 @@
#include "fsk_osc.h"
#include <stdlib.h>
struct FSKOsc {
uint16_t freq[2];
uint16_t osc_phase_max;
int32_t osc_phase_current;
uint32_t pulse;
};
FSKOsc* fsk_osc_alloc(uint32_t freq_low, uint32_t freq_hi, uint32_t osc_phase_max) {
FSKOsc* osc = malloc(sizeof(FSKOsc));
osc->freq[0] = freq_low;
osc->freq[1] = freq_hi;
osc->osc_phase_max = osc_phase_max;
osc->osc_phase_current = 0;
osc->pulse = 0;
return osc;
}
void fsk_osc_free(FSKOsc* osc) {
free(osc);
}
void fsk_osc_reset(FSKOsc* osc) {
osc->osc_phase_current = 0;
osc->pulse = 0;
}
bool fsk_osc_next(FSKOsc* osc, bool bit, uint32_t* period) {
bool advance = false;
*period = osc->freq[bit];
osc->osc_phase_current += *period;
if(osc->osc_phase_current > osc->osc_phase_max) {
advance = true;
osc->osc_phase_current -= osc->osc_phase_max;
}
return advance;
}
bool fsk_osc_next_half(FSKOsc* osc, bool bit, bool* level, uint32_t* duration) {
bool advance = false;
// if pulse is zero, we need to output high, otherwise we need to output low
if(osc->pulse == 0) {
uint32_t length;
advance = fsk_osc_next(osc, bit, &length);
*duration = length / 2;
osc->pulse = *duration;
*level = true;
} else {
// output low half and reset pulse
*duration = osc->pulse;
osc->pulse = 0;
*level = false;
}
return advance;
}

View File

@@ -0,0 +1,60 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct FSKOsc FSKOsc;
/**
* @brief Allocate a new FSKOsc instance
* FSKOsc is a oscillator that can provide FSK encoding
*
* @param freq_low
* @param freq_hi
* @param osc_phase_max
* @return FSKOsc*
*/
FSKOsc* fsk_osc_alloc(uint32_t freq_low, uint32_t freq_hi, uint32_t osc_phase_max);
/**
* @brief Free a FSKOsc instance
*
* @param osc
*/
void fsk_osc_free(FSKOsc* osc);
/**
* @brief Reset ocillator
*
* @param osc
*/
void fsk_osc_reset(FSKOsc* osc);
/**
* @brief Get next duration sample from oscillator
*
* @param osc
* @param bit
* @param period
* @return bool
*/
bool fsk_osc_next(FSKOsc* osc, bool bit, uint32_t* period);
/**
* @brief Get next half of sample from oscillator
* Useful when encoding high and low levels separately.
*
* @param osc
* @param bit
* @param level
* @param duration
* @return bool
*/
bool fsk_osc_next_half(FSKOsc* osc, bool bit, bool* level, uint32_t* duration);
#ifdef __cplusplus
}
#endif

94
lib/lfrfid/tools/t5577.c Normal file
View File

@@ -0,0 +1,94 @@
#include "t5577.h"
#include <furi.h>
#include <furi_hal_rfid.h>
#define T5577_TIMING_WAIT_TIME 400
#define T5577_TIMING_START_GAP 30
#define T5577_TIMING_WRITE_GAP 18
#define T5577_TIMING_DATA_0 24
#define T5577_TIMING_DATA_1 56
#define T5577_TIMING_PROGRAM 700
#define T5577_OPCODE_PAGE_0 0b10
#define T5577_OPCODE_PAGE_1 0b11
#define T5577_OPCODE_RESET 0b00
static void t5577_start() {
furi_hal_rfid_tim_read(125000, 0.5);
furi_hal_rfid_pins_read();
furi_hal_rfid_tim_read_start();
// do not ground the antenna
furi_hal_rfid_pin_pull_release();
}
static void t5577_stop() {
furi_hal_rfid_tim_read_stop();
furi_hal_rfid_tim_reset();
furi_hal_rfid_pins_reset();
}
static void t5577_write_gap(uint32_t gap_time) {
furi_hal_rfid_tim_read_stop();
furi_delay_us(gap_time * 8);
furi_hal_rfid_tim_read_start();
}
static void t5577_write_bit(bool value) {
if(value) {
furi_delay_us(T5577_TIMING_DATA_1 * 8);
} else {
furi_delay_us(T5577_TIMING_DATA_0 * 8);
}
t5577_write_gap(T5577_TIMING_WRITE_GAP);
}
static void t5577_write_opcode(uint8_t value) {
t5577_write_bit((value >> 1) & 1);
t5577_write_bit((value >> 0) & 1);
}
static void t5577_write_reset() {
t5577_write_gap(T5577_TIMING_START_GAP);
t5577_write_bit(1);
t5577_write_bit(0);
}
static void t5577_write_block(uint8_t block, bool lock_bit, uint32_t data) {
furi_delay_us(T5577_TIMING_WAIT_TIME * 8);
// start gap
t5577_write_gap(T5577_TIMING_START_GAP);
// opcode for page 0
t5577_write_opcode(T5577_OPCODE_PAGE_0);
// lock bit
t5577_write_bit(lock_bit);
// data
for(uint8_t i = 0; i < 32; i++) {
t5577_write_bit((data >> (31 - i)) & 1);
}
// block address
t5577_write_bit((block >> 2) & 1);
t5577_write_bit((block >> 1) & 1);
t5577_write_bit((block >> 0) & 1);
furi_delay_us(T5577_TIMING_PROGRAM * 8);
furi_delay_us(T5577_TIMING_WAIT_TIME * 8);
t5577_write_reset();
}
void t5577_write(LFRFIDT5577* data) {
t5577_start();
FURI_CRITICAL_ENTER();
for(size_t i = 0; i < data->blocks_to_write; i++) {
t5577_write_block(i, false, data->block[i]);
}
t5577_write_reset();
FURI_CRITICAL_EXIT();
t5577_stop();
}

56
lib/lfrfid/tools/t5577.h Normal file
View File

@@ -0,0 +1,56 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
#define LFRFID_T5577_BLOCK_COUNT 8
// T5577 block 0 definitions, thanks proxmark3!
#define LFRFID_T5577_POR_DELAY 0x00000001
#define LFRFID_T5577_ST_TERMINATOR 0x00000008
#define LFRFID_T5577_PWD 0x00000010
#define LFRFID_T5577_MAXBLOCK_SHIFT 5
#define LFRFID_T5577_AOR 0x00000200
#define LFRFID_T5577_PSKCF_RF_2 0
#define LFRFID_T5577_PSKCF_RF_4 0x00000400
#define LFRFID_T5577_PSKCF_RF_8 0x00000800
#define LFRFID_T5577_MODULATION_DIRECT 0
#define LFRFID_T5577_MODULATION_PSK1 0x00001000
#define LFRFID_T5577_MODULATION_PSK2 0x00002000
#define LFRFID_T5577_MODULATION_PSK3 0x00003000
#define LFRFID_T5577_MODULATION_FSK1 0x00004000
#define LFRFID_T5577_MODULATION_FSK2 0x00005000
#define LFRFID_T5577_MODULATION_FSK1a 0x00006000
#define LFRFID_T5577_MODULATION_FSK2a 0x00007000
#define LFRFID_T5577_MODULATION_MANCHESTER 0x00008000
#define LFRFID_T5577_MODULATION_BIPHASE 0x00010000
#define LFRFID_T5577_MODULATION_DIPHASE 0x00018000
#define LFRFID_T5577_X_MODE 0x00020000
#define LFRFID_T5577_BITRATE_RF_8 0
#define LFRFID_T5577_BITRATE_RF_16 0x00040000
#define LFRFID_T5577_BITRATE_RF_32 0x00080000
#define LFRFID_T5577_BITRATE_RF_40 0x000C0000
#define LFRFID_T5577_BITRATE_RF_50 0x00100000
#define LFRFID_T5577_BITRATE_RF_64 0x00140000
#define LFRFID_T5577_BITRATE_RF_100 0x00180000
#define LFRFID_T5577_BITRATE_RF_128 0x001C0000
#define LFRFID_T5577_TESTMODE_DISABLED 0x60000000
typedef struct {
uint32_t block[LFRFID_T5577_BLOCK_COUNT];
uint32_t blocks_to_write;
} LFRFIDT5577;
/**
* @brief Write T5577 tag data to tag
*
* @param data
*/
void t5577_write(LFRFIDT5577* data);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,77 @@
#include "varint_pair.h"
#include <toolbox/varint.h>
#define VARINT_PAIR_SIZE 10
struct VarintPair {
size_t data_length;
uint8_t data[VARINT_PAIR_SIZE];
};
VarintPair* varint_pair_alloc() {
VarintPair* pair = malloc(sizeof(VarintPair));
pair->data_length = 0;
return pair;
}
void varint_pair_free(VarintPair* pair) {
free(pair);
}
bool varint_pair_pack(VarintPair* pair, bool first, uint32_t value) {
bool result = false;
if(first) {
if(pair->data_length == 0) {
pair->data_length = varint_uint32_pack(value, pair->data);
} else {
pair->data_length = 0;
}
} else {
if(pair->data_length > 0) {
pair->data_length += varint_uint32_pack(value, pair->data + pair->data_length);
result = true;
} else {
pair->data_length = 0;
}
}
return result;
}
bool varint_pair_unpack(
uint8_t* data,
size_t data_length,
uint32_t* value_1,
uint32_t* value_2,
size_t* length) {
size_t size = 0;
uint32_t tmp_value_1;
uint32_t tmp_value_2;
size += varint_uint32_unpack(&tmp_value_1, &data[size], data_length);
if(size >= data_length) {
return false;
}
size += varint_uint32_unpack(&tmp_value_2, &data[size], (size_t)(data_length - size));
*value_1 = tmp_value_1;
*value_2 = tmp_value_2;
*length = size;
return true;
}
uint8_t* varint_pair_get_data(VarintPair* pair) {
return pair->data;
}
size_t varint_pair_get_size(VarintPair* pair) {
return pair->data_length;
}
void varint_pair_reset(VarintPair* pair) {
pair->data_length = 0;
}

View File

@@ -0,0 +1,79 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct VarintPair VarintPair;
/**
* @brief Allocate a new VarintPair instance
*
* VarintPair is a buffer that holds pair of varint values
* @return VarintPair*
*/
VarintPair* varint_pair_alloc();
/**
* @brief Free a VarintPair instance
*
* @param pair
*/
void varint_pair_free(VarintPair* pair);
/**
* @brief Write varint pair to buffer
*
* @param pair
* @param first
* @param value
* @return bool pair complete and needs to be written
*/
bool varint_pair_pack(VarintPair* pair, bool first, uint32_t value);
/**
* @brief Get pointer to varint pair buffer
*
* @param pair
* @return uint8_t*
*/
uint8_t* varint_pair_get_data(VarintPair* pair);
/**
* @brief Get size of varint pair buffer
*
* @param pair
* @return size_t
*/
size_t varint_pair_get_size(VarintPair* pair);
/**
* @brief Reset varint pair buffer
*
* @param pair
*/
void varint_pair_reset(VarintPair* pair);
/**
* @brief Unpack varint pair to uint32_t pair from buffer
*
* @param data
* @param data_length
* @param value_1
* @param value_2
* @param length
* @return bool
*/
bool varint_pair_unpack(
uint8_t* data,
size_t data_length,
uint32_t* value_1,
uint32_t* value_2,
size_t* length);
#ifdef __cplusplus
}
#endif