#include #include #include #include "nfcommander.h" #include "config.h" #include "libyhy.h" static int reader_fd = -1; int init(){ char reader_version[100]; char* port = config_get("nfc", "device"); if(!port){ printf("Please configure nfc.device to the reader serial port\n"); return 1; } //TODO set cloexec, nonblock printf("Opening YHY reader on %s\n", port); reader_fd = yhy_open(port); if(reader_fd < 0){ printf("Failed to open reader connection at %s\n", port); return 1; } if(yhy_sync_read_version(reader_fd, reader_version, sizeof(reader_version)) > 0){ printf("Connected YHY reader reports firmware: %s\n", reader_version); } core_manage_fd(reader_fd, 1, system_reader); return 0; } int handle(int fd){ if(fd == reader_fd){ printf("Unhandled data on YHY fd\n"); } return 0; } int scan(){ nfc_tag_info_t card = { 0 }; uint16_t atqa = 0; uint8_t sak = 0; for(yhy_sync_request(reader_fd, 1, &atqa); atqa; yhy_sync_request(reader_fd, 0, &atqa)){ card.uid_length = yhy_sync_anticoll(reader_fd, card.uid, sizeof(card.uid)); if(!card.uid_length){ return 0; } yhy_sync_select(reader_fd, card.uid, card.uid_length, &sak); //TODO parse ATQA and SAK to detect tag type if(reader_tag_present(card) == TAG_READ_REQUESTED){ //TODO read card data } yhy_sync_hlta(reader_fd); } return 0; } static void __attribute__((destructor)) cleanup(){ core_manage_fd(reader_fd, 0, system_reader); close(reader_fd); reader_fd = -1; }