6 #include <spdlog/spdlog.h>
11 #define REPEATING_FLAG_DELAY 500
12 #define RFID_READ_INTERVAL 100000
19 std::unique_ptr<SPI>
my_spi =
nullptr;
25 int init(
const char* device, uint8_t mode = 0, uint32_t speed = 1000000, uint16_t delay = 0, uint8_t bits_per_word = 8) {
26 spi_config = std::unique_ptr<spi_config_t>(
new spi_config_t());
32 spdlog::info(
"rfid_reader.cpp - Initializing SPI RFID Reader (RC522)");
37 spdlog::critical(
"rfid_reader.cpp - Failed to start SPI communication for RC522");
41 spdlog::info(
"rfid_reader.cpp - Successfully initialized SPI RFID Reader (RC522)");
48 spdlog::info(
"rfid_reader.cpp - Initializing SPI RFID Reader (RC522) with config (enable debug to print config)");
49 spdlog::debug(
"rfid_reader.cpp - dev: {0}" , config->
dev);
50 spdlog::debug(
"rfid_reader.cpp - mode: {0}" , config->
config->mode);
51 spdlog::debug(
"rfid_reader.cpp - bits_p_w: {0}", config->
config->bits_per_word);
52 spdlog::debug(
"rfid_reader.cpp - speed: {0}" , config->
config->speed);
53 spdlog::debug(
"rfid_reader.cpp - delay: {0}" , config->
config->delay);
55 my_spi = std::unique_ptr<SPI>(
new SPI(config->
dev.c_str(), config->
config.get()));
58 spdlog::critical(
"rfid_reader.cpp - Failed to start SPI communication for RC522");
62 spdlog::info(
"rfid_reader.cpp - Successfully initialized SPI RFID Reader (RC522)");
68 spdlog::debug(
"rfid_reader.cpp - Read RFID tag. Serial:");
71 printf(
"%d", tag->
serial[i]);
80 std::chrono::time_point<std::chrono::high_resolution_clock> last_read =
81 std::chrono::high_resolution_clock::now();
82 std::chrono::time_point<std::chrono::high_resolution_clock> current_read;
87 current_read = std::chrono::high_resolution_clock::now();
90 spdlog::error(
"rfid_reader.cpp - read_tag() finished but returned error state");
92 spdlog::info(
"rfid_reader.cpp - Successfully read RFID tag. Pushing into RFID event queue.");
94 std::chrono::duration<double, std::milli> diff = last_read - current_read;
102 last_read = current_read;
107 spdlog::info(
"rfid_reader.cpp - Cleaning SPI RFID Reader (RC522)");
117 *tag_type = buff[0] << 8 | buff[1];
123 char read_tag_serialn(uint8_t *serial, uint8_t *serial_size, uint8_t *ser_ack, uint8_t *buff) {
130 if (buff[0]== 0x88) {
133 memcpy(serial, &buff[1],3);
143 memcpy(serial + 3, &buff[1], 3);
152 memcpy(serial + 6, buff, 4);
156 memcpy(serial + 3, buff, 4);
164 memcpy(serial, &buff[0], 4);
189 spdlog::debug(
"rfid_reader.cpp - Issue reading tag, trying again...");
201 spdlog::debug(
"rfid_reader.cpp - Found card");
215 spdlog::debug(
"rfid_reader.cpp - Ultralight card");
216 spdlog::debug(
"rfid_reader.cpp - NOT supported by this program");
217 }
else if (tag->
ser_ack[0] == 0x8) {
218 spdlog::debug(
"rfid_reader.cpp - Mifare Classic(1K) or PLUS card (2K)");
219 spdlog::debug(
"rfid_reader.cpp - In case of PLUS card, we will only read the first 1K (64 blocks)");
222 }
else if (tag->
ser_ack[0] == 0x9) {
223 spdlog::debug(
"rfid_reader.cpp - Mifare MINI(0.3K)");
226 }
else if (tag->
ser_ack[0] == 0x18) {
227 spdlog::debug(
"rfid_reader.cpp - PLUS card (4K)");
228 spdlog::debug(
"rfid_reader.cpp - Can only access the first 128 blocks (NOT last 8 sectore with 16 blocks)");
236 spdlog::debug(
"rfid_reader.cpp - Mifare Classic(1K) or SmartMX with MIFARE 1K emulation");
239 }
else if (tag->
ser_ack[0] == 0x9) {
240 spdlog::debug(
"rfid_reader.cpp - Mifare MINI(0.3K)");
248 spdlog::debug(
"rfid_reader.cpp - Mifare Classic(4K) or SmartMX with MIFARE 4K emulation");
249 spdlog::debug(
"rfid_reader.cpp - Can only access the first 128 blocks (NOT last 8 sectore with 16 blocks)");
252 }
else if (tag->
ser_ack[0] == 0x9) {
253 spdlog::debug(
"rfid_reader.cpp - Mifare MINI(0.3K)");
261 spdlog::error(
"rfid_reader.cpp - Read card, BUT card type not known");
283 std::lock_guard<std::mutex> eq_guard(
eq_mutex);
static std::unique_ptr< app_workspace > & get_instance()
Get the instance app_workspace which is a singleton.
int init(const char *device, uint8_t mode=0, uint32_t speed=1000000, uint16_t delay=0, uint8_t bits_per_word=8)
RFID reader init through values.
int init_from_conf()
RFID reader init from loaded configuration.
std::unique_ptr< SPI > my_spi
std::queue< rfid_event > event_queue
char read_tag_serialn(uint8_t *serial, uint8_t *serial_size, uint8_t *ser_ack, uint8_t *buff)
This function reads the serial number of RIFD tag put near the reader.
std::unique_ptr< spi_config_t > spi_config
void clean()
Stops the reading thread.
void clear_event_queue()
Clears event queue, discarding all waiting events.
int read_tag(rfid_tag *tag)
This function is a loop handling the scanner.
rfid_event * poll_event()
Polls event from RFID event_queue.
char detect_tag(uint16_t *tag_type, uint8_t *buff)
This function detects if a RFID tag was put near the reader.
void print_card_serial(rfid_tag *tag)
void start_reading_cards()
Loop that is run in its own thread, reading the RFID reader.
void push_event(rfid_tag tag, uint8_t flags)
char PcdAnticoll(uint8_t cascade, uint8_t *pSnr)
void InitRc522(SPI *p_spi)
char PcdRequest(uint8_t req_code, uint8_t *pTagType)
char PcdSelect(uint8_t cascade, uint8_t *pSnr, uint8_t *SAK)
#define REPEATING_FLAG_DELAY
#define RFID_READ_INTERVAL
Structure holding SPI device and SPI config which is a library structure "spi_config_t".
std::unique_ptr< spi_config_t > config
Structure used for RFID event. This structure contains read tag and flags of the event.
rfid_reader::rfid_tag tag
RFID tag structure. Stores data read from SPI.