[FL-1449] Indala reading and writing (#616)
* Rfid: indala 40134 validation and decoding * Rfid: show indala info * Rfid: decoder to output comparator signal on gpio pins * Rfid: working indala 40134 decoder * HAL: added function to change rfid timer parameters on the fly * RFID: Indala reading, card detection, card verification * Rfid: indala writing
This commit is contained in:
@@ -12,6 +12,11 @@ static void set_bit(bool bit, uint8_t position, Indala40134CardData* card_data)
|
||||
}
|
||||
}
|
||||
|
||||
static bool get_bit(uint8_t position, const Indala40134CardData* card_data) {
|
||||
position = (sizeof(Indala40134CardData) * 8) - 1 - position;
|
||||
return (*card_data >> position) & 1;
|
||||
}
|
||||
|
||||
uint8_t ProtocolIndala40134::get_encoded_data_size() {
|
||||
return sizeof(Indala40134CardData);
|
||||
}
|
||||
@@ -110,6 +115,46 @@ void ProtocolIndala40134::encode(
|
||||
memcpy(encoded_data, &card_data, get_encoded_data_size());
|
||||
}
|
||||
|
||||
// factory code
|
||||
static uint8_t get_fc(const Indala40134CardData* card_data) {
|
||||
uint8_t fc = 0;
|
||||
|
||||
fc = fc << 1 | get_bit(57, card_data);
|
||||
fc = fc << 1 | get_bit(49, card_data);
|
||||
fc = fc << 1 | get_bit(44, card_data);
|
||||
fc = fc << 1 | get_bit(47, card_data);
|
||||
fc = fc << 1 | get_bit(48, card_data);
|
||||
fc = fc << 1 | get_bit(53, card_data);
|
||||
fc = fc << 1 | get_bit(39, card_data);
|
||||
fc = fc << 1 | get_bit(58, card_data);
|
||||
|
||||
return fc;
|
||||
}
|
||||
|
||||
// card number
|
||||
static uint16_t get_cn(const Indala40134CardData* card_data) {
|
||||
uint16_t cn = 0;
|
||||
|
||||
cn = cn << 1 | get_bit(42, card_data);
|
||||
cn = cn << 1 | get_bit(45, card_data);
|
||||
cn = cn << 1 | get_bit(43, card_data);
|
||||
cn = cn << 1 | get_bit(40, card_data);
|
||||
cn = cn << 1 | get_bit(52, card_data);
|
||||
cn = cn << 1 | get_bit(36, card_data);
|
||||
cn = cn << 1 | get_bit(35, card_data);
|
||||
cn = cn << 1 | get_bit(51, card_data);
|
||||
cn = cn << 1 | get_bit(46, card_data);
|
||||
cn = cn << 1 | get_bit(33, card_data);
|
||||
cn = cn << 1 | get_bit(37, card_data);
|
||||
cn = cn << 1 | get_bit(54, card_data);
|
||||
cn = cn << 1 | get_bit(56, card_data);
|
||||
cn = cn << 1 | get_bit(59, card_data);
|
||||
cn = cn << 1 | get_bit(50, card_data);
|
||||
cn = cn << 1 | get_bit(41, card_data);
|
||||
|
||||
return cn;
|
||||
}
|
||||
|
||||
void ProtocolIndala40134::decode(
|
||||
const uint8_t* encoded_data,
|
||||
const uint8_t encoded_data_size,
|
||||
@@ -117,15 +162,76 @@ void ProtocolIndala40134::decode(
|
||||
const uint8_t decoded_data_size) {
|
||||
furi_check(decoded_data_size >= get_decoded_data_size());
|
||||
furi_check(encoded_data_size >= get_encoded_data_size());
|
||||
// TODO implement decoding
|
||||
furi_check(0);
|
||||
|
||||
const Indala40134CardData* card_data =
|
||||
reinterpret_cast<const Indala40134CardData*>(encoded_data);
|
||||
|
||||
uint8_t fc = get_fc(card_data);
|
||||
uint16_t card = get_cn(card_data);
|
||||
|
||||
decoded_data[0] = fc;
|
||||
decoded_data[1] = card >> 8;
|
||||
decoded_data[2] = card;
|
||||
}
|
||||
|
||||
bool ProtocolIndala40134::can_be_decoded(
|
||||
const uint8_t* encoded_data,
|
||||
const uint8_t encoded_data_size) {
|
||||
furi_check(encoded_data_size >= get_encoded_data_size());
|
||||
// TODO implement decoding
|
||||
furi_check(0);
|
||||
return false;
|
||||
bool can_be_decoded = false;
|
||||
|
||||
const Indala40134CardData* card_data =
|
||||
reinterpret_cast<const Indala40134CardData*>(encoded_data);
|
||||
|
||||
do {
|
||||
// preambula
|
||||
if((*card_data >> 32) != 0xa0000000UL) break;
|
||||
|
||||
// data
|
||||
const uint32_t fc_and_card = get_fc(card_data) << 16 | get_cn(card_data);
|
||||
|
||||
// checksum
|
||||
const uint8_t checksum = get_bit(62, card_data) << 1 | get_bit(63, card_data);
|
||||
uint8_t checksum_sum = 0;
|
||||
checksum_sum += ((fc_and_card >> 14) & 1);
|
||||
checksum_sum += ((fc_and_card >> 12) & 1);
|
||||
checksum_sum += ((fc_and_card >> 9) & 1);
|
||||
checksum_sum += ((fc_and_card >> 8) & 1);
|
||||
checksum_sum += ((fc_and_card >> 6) & 1);
|
||||
checksum_sum += ((fc_and_card >> 5) & 1);
|
||||
checksum_sum += ((fc_and_card >> 2) & 1);
|
||||
checksum_sum += ((fc_and_card >> 0) & 1);
|
||||
checksum_sum = checksum_sum & 0b1;
|
||||
|
||||
if(checksum_sum == 1 && checksum == 0b01) {
|
||||
} else if(checksum_sum == 0 && checksum == 0b10) {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
|
||||
// wiegand parity bits
|
||||
// even parity sum calculation (high 12 bits of data)
|
||||
const bool even_parity = get_bit(34, card_data);
|
||||
uint8_t even_parity_sum = 0;
|
||||
for(int8_t i = 12; i < 24; i++) {
|
||||
if(((fc_and_card >> i) & 1) == 1) {
|
||||
even_parity_sum++;
|
||||
}
|
||||
}
|
||||
if(even_parity_sum % 2 != even_parity) break;
|
||||
|
||||
// odd parity sum calculation (low 12 bits of data)
|
||||
const bool odd_parity = get_bit(38, card_data);
|
||||
uint8_t odd_parity_sum = 1;
|
||||
for(int8_t i = 0; i < 12; i++) {
|
||||
if(((fc_and_card >> i) & 1) == 1) {
|
||||
odd_parity_sum++;
|
||||
}
|
||||
}
|
||||
if(odd_parity_sum % 2 != odd_parity) break;
|
||||
|
||||
can_be_decoded = true;
|
||||
} while(false);
|
||||
|
||||
return can_be_decoded;
|
||||
}
|
Reference in New Issue
Block a user