2022-01-05 16:10:18 +00:00
|
|
|
#include "rfid_reader.h"
|
2021-05-04 13:21:16 +00:00
|
|
|
#include <furi.h>
|
2022-01-05 16:10:18 +00:00
|
|
|
#include <furi_hal.h>
|
2021-05-04 13:21:16 +00:00
|
|
|
#include <stm32wbxx_ll_cortex.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief private violation assistant for RfidReader
|
|
|
|
*/
|
|
|
|
struct RfidReaderAccessor {
|
|
|
|
static void decode(RfidReader& rfid_reader, bool polarity) {
|
|
|
|
rfid_reader.decode(polarity);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void RfidReader::decode(bool polarity) {
|
|
|
|
uint32_t current_dwt_value = DWT->CYCCNT;
|
2021-08-01 22:11:18 +00:00
|
|
|
uint32_t period = current_dwt_value - last_dwt_value;
|
|
|
|
last_dwt_value = current_dwt_value;
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-09 12:33:13 +00:00
|
|
|
#ifdef RFID_GPIO_DEBUG
|
|
|
|
decoder_gpio_out.process_front(polarity, period);
|
|
|
|
#endif
|
|
|
|
|
2021-05-04 13:21:16 +00:00
|
|
|
switch(type) {
|
|
|
|
case Type::Normal:
|
2021-08-01 22:11:18 +00:00
|
|
|
decoder_em.process_front(polarity, period);
|
|
|
|
decoder_hid26.process_front(polarity, period);
|
|
|
|
break;
|
|
|
|
case Type::Indala:
|
|
|
|
decoder_em.process_front(polarity, period);
|
|
|
|
decoder_hid26.process_front(polarity, period);
|
|
|
|
decoder_indala.process_front(polarity, period);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
detect_ticks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RfidReader::switch_timer_elapsed() {
|
|
|
|
const uint32_t seconds_to_switch = osKernelGetTickFreq() * 2.0f;
|
|
|
|
return (osKernelGetTickCount() - switch_os_tick_last) > seconds_to_switch;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RfidReader::switch_timer_reset() {
|
|
|
|
switch_os_tick_last = osKernelGetTickCount();
|
|
|
|
}
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
void RfidReader::switch_mode() {
|
|
|
|
switch(type) {
|
|
|
|
case Type::Normal:
|
|
|
|
type = Type::Indala;
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_rfid_change_read_config(62500.0f, 0.25f);
|
2021-05-04 13:21:16 +00:00
|
|
|
break;
|
|
|
|
case Type::Indala:
|
2021-08-01 22:11:18 +00:00
|
|
|
type = Type::Normal;
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_rfid_change_read_config(125000.0f, 0.5f);
|
2021-05-04 13:21:16 +00:00
|
|
|
break;
|
|
|
|
}
|
2021-08-01 22:11:18 +00:00
|
|
|
|
|
|
|
switch_timer_reset();
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 17:59:20 +00:00
|
|
|
static void comparator_trigger_callback(bool level, void* comp_ctx) {
|
2021-05-04 13:21:16 +00:00
|
|
|
RfidReader* _this = static_cast<RfidReader*>(comp_ctx);
|
|
|
|
|
2022-03-23 17:59:20 +00:00
|
|
|
RfidReaderAccessor::decode(*_this, !level);
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RfidReader::RfidReader() {
|
|
|
|
}
|
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
void RfidReader::start() {
|
|
|
|
type = Type::Normal;
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_rfid_pins_read();
|
|
|
|
furi_hal_rfid_tim_read(125000, 0.5);
|
|
|
|
furi_hal_rfid_tim_read_start();
|
2021-08-01 22:11:18 +00:00
|
|
|
start_comparator();
|
|
|
|
|
|
|
|
switch_timer_reset();
|
2022-04-14 12:03:47 +00:00
|
|
|
last_read_count = 0;
|
2021-08-01 22:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RfidReader::start_forced(RfidReader::Type _type) {
|
2021-08-09 12:33:13 +00:00
|
|
|
start();
|
|
|
|
if(_type == Type::Indala) {
|
|
|
|
switch_mode();
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RfidReader::stop() {
|
2021-08-08 18:03:25 +00:00
|
|
|
furi_hal_rfid_pins_reset();
|
|
|
|
furi_hal_rfid_tim_read_stop();
|
|
|
|
furi_hal_rfid_tim_reset();
|
2021-05-04 13:21:16 +00:00
|
|
|
stop_comparator();
|
|
|
|
}
|
|
|
|
|
2021-11-16 08:47:49 +00:00
|
|
|
bool RfidReader::read(LfrfidKeyType* _type, uint8_t* data, uint8_t data_size, bool switch_enable) {
|
2021-05-04 13:21:16 +00:00
|
|
|
bool result = false;
|
2022-04-14 12:03:47 +00:00
|
|
|
bool something_read = false;
|
2021-05-04 13:21:16 +00:00
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
// reading
|
2021-05-04 13:21:16 +00:00
|
|
|
if(decoder_em.read(data, data_size)) {
|
2021-08-01 22:11:18 +00:00
|
|
|
*_type = LfrfidKeyType::KeyEM4100;
|
2022-04-14 12:03:47 +00:00
|
|
|
something_read = true;
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(decoder_hid26.read(data, data_size)) {
|
2021-08-01 22:11:18 +00:00
|
|
|
*_type = LfrfidKeyType::KeyH10301;
|
2022-04-14 12:03:47 +00:00
|
|
|
something_read = true;
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
if(decoder_indala.read(data, data_size)) {
|
|
|
|
*_type = LfrfidKeyType::KeyI40134;
|
2022-04-14 12:03:47 +00:00
|
|
|
something_read = true;
|
2021-08-01 22:11:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// validation
|
2022-04-14 12:03:47 +00:00
|
|
|
if(something_read) {
|
2021-08-01 22:11:18 +00:00
|
|
|
switch_timer_reset();
|
|
|
|
|
2022-04-14 12:03:47 +00:00
|
|
|
if(last_read_type == *_type && memcmp(last_read_data, data, data_size) == 0) {
|
|
|
|
last_read_count = last_read_count + 1;
|
2021-08-01 22:11:18 +00:00
|
|
|
|
2022-04-14 12:03:47 +00:00
|
|
|
if(last_read_count > 2) {
|
2021-08-01 22:11:18 +00:00
|
|
|
result = true;
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-14 12:03:47 +00:00
|
|
|
last_read_type = *_type;
|
|
|
|
memcpy(last_read_data, data, data_size);
|
|
|
|
last_read_count = 0;
|
2021-08-01 22:11:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mode switching
|
2021-11-16 08:47:49 +00:00
|
|
|
if(switch_enable && switch_timer_elapsed()) {
|
2021-08-01 22:11:18 +00:00
|
|
|
switch_mode();
|
2022-04-14 12:03:47 +00:00
|
|
|
last_read_count = 0;
|
2021-08-01 22:11:18 +00:00
|
|
|
}
|
2021-05-04 13:21:16 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-01 22:11:18 +00:00
|
|
|
bool RfidReader::detect() {
|
|
|
|
bool detected = false;
|
|
|
|
if(detect_ticks > 10) {
|
|
|
|
detected = true;
|
|
|
|
}
|
|
|
|
detect_ticks = 0;
|
|
|
|
|
|
|
|
return detected;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RfidReader::any_read() {
|
2022-04-14 12:03:47 +00:00
|
|
|
return last_read_count > 0;
|
2021-08-01 22:11:18 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 13:21:16 +00:00
|
|
|
void RfidReader::start_comparator(void) {
|
2022-03-23 17:59:20 +00:00
|
|
|
furi_hal_rfid_comp_set_callback(comparator_trigger_callback, this);
|
2021-05-04 13:21:16 +00:00
|
|
|
last_dwt_value = DWT->CYCCNT;
|
|
|
|
|
2022-03-23 17:59:20 +00:00
|
|
|
furi_hal_rfid_comp_start();
|
2021-05-04 13:21:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RfidReader::stop_comparator(void) {
|
2022-03-23 17:59:20 +00:00
|
|
|
furi_hal_rfid_comp_stop();
|
|
|
|
furi_hal_rfid_comp_set_callback(NULL, NULL);
|
|
|
|
}
|