Skip to content

Commit

Permalink
[FL-2812] RFID: write fix for some protocols flipperdevices#1828
Browse files Browse the repository at this point in the history
Co-authored-by: あく <[email protected]>
  • Loading branch information
nminaylov and skotopes authored Oct 6, 2022
1 parent f16cdd1 commit a69e150
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/lfrfid/protocols/protocol_awid.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,15 @@ bool protocol_awid_write_data(ProtocolAwid* protocol, void* data) {
LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
bool result = false;

// Fix incorrect length byte
if(protocol->data[0] != 26 && protocol->data[0] != 50 && protocol->data[0] != 37 &&
protocol->data[0] != 34) {
protocol->data[0] = 26;
}

// Correct protocol data by redecoding
protocol_awid_encode(protocol->data, (uint8_t*)protocol->encoded_data);
bit_lib_remove_bit_every_nth((uint8_t*)protocol->encoded_data, 8, 88, 4);
protocol_awid_decode(protocol->encoded_data, protocol->data);

protocol_awid_encode(protocol->data, (uint8_t*)protocol->encoded_data);
Expand Down
9 changes: 9 additions & 0 deletions lib/lfrfid/protocols/protocol_fdx_a.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ static bool protocol_fdx_a_decode(const uint8_t* from, uint8_t* to) {
return true;
}

static void protocol_fdx_a_fix_parity(ProtocolFDXA* protocol) {
for(size_t i = 0; i < FDXA_DECODED_DATA_SIZE; i++) {
if(bit_lib_test_parity_32(protocol->data[i], BitLibParityOdd)) {
protocol->data[i] ^= (1 << 7);
}
}
}

static bool protocol_fdx_a_can_be_decoded(const uint8_t* data) {
// check preamble
if(data[0] != FDXA_PREAMBLE_0 || data[1] != FDXA_PREAMBLE_1 || data[12] != FDXA_PREAMBLE_0 ||
Expand Down Expand Up @@ -179,6 +187,7 @@ bool protocol_fdx_a_write_data(ProtocolFDXA* protocol, void* data) {
bool result = false;

// Correct protocol data by redecoding
protocol_fdx_a_fix_parity(protocol);
protocol_fdx_a_encoder_start(protocol);
protocol_fdx_a_decode(protocol->encoded_data, protocol->data);

Expand Down
3 changes: 3 additions & 0 deletions lib/lfrfid/protocols/protocol_keri.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ bool protocol_keri_encoder_start(ProtocolKeri* protocol) {
memset(protocol->encoded_data, 0, KERI_ENCODED_DATA_SIZE);
*(uint32_t*)&protocol->encoded_data[0] = 0b00000000000000000000000011100000;
bit_lib_copy_bits(protocol->encoded_data, 32, 32, protocol->data, 0);
bit_lib_set_bits(protocol->encoded_data, 32, 1, 1);

protocol->encoder.last_bit =
bit_lib_get_bit(protocol->encoded_data, KERI_ENCODED_BIT_SIZE - 1);
Expand Down Expand Up @@ -224,6 +225,8 @@ bool protocol_keri_write_data(ProtocolKeri* protocol, void* data) {
LFRFIDWriteRequest* request = (LFRFIDWriteRequest*)data;
bool result = false;

// Start bit should be always set
protocol->data[0] |= (1 << 7);
protocol_keri_encoder_start(protocol);

if(request->write_type == LFRFIDWriteTypeT5577) {
Expand Down
1 change: 1 addition & 0 deletions lib/lfrfid/protocols/protocol_pyramid.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ bool protocol_pyramid_write_data(ProtocolPyramid* protocol, void* data) {

// Correct protocol data by redecoding
protocol_pyramid_encode(protocol);
bit_lib_remove_bit_every_nth(protocol->encoded_data, 8, 15 * 8, 8);
protocol_pyramid_decode(protocol);

protocol_pyramid_encoder_start(protocol);
Expand Down

0 comments on commit a69e150

Please sign in to comment.