#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; uint8_t data[16]; uint8_t bytes; for(yhy_sync_request(reader_fd, 1, &atqa); atqa; yhy_sync_request(reader_fd, 0, &atqa)){ sak = 0; //ATQA should normally not be used as an indicator for UID length //However, the YHY UL_SELECT command does not seem to work after CL1 anticollision for some reason //extended length UIDs if(atqa & 0x00C0){ card.uid_length = yhy_sync_ulselect(reader_fd, card.uid, sizeof(card.uid)); //since we dont get either the CL2 SAK or any other method of identifying the particular type of PICC, just guess whether it's an NTAG card.type = tag_unknown; if(atqa == 0x0044){ card.type = tag_ntag; card.bytes_available = 140; card.granularity = 4; } else{ printf("PICC Parameters: ATQA %04X\n", atqa); } } else{ card.uid_length = yhy_sync_anticoll(reader_fd, card.uid, sizeof(card.uid)); yhy_sync_select(reader_fd, card.uid, card.uid_length, &sak); card.type = tag_unknown; if(atqa == 0x0004 && sak == 0x08){ card.type = tag_mifare1; card.bytes_available = 752; card.granularity = 16; } else{ printf("PICC Parameters: ATQA %04X SAK %02X\n", atqa, sak); } } if(reader_tag_present(0, card) == TAG_READ_REQUESTED){ //read card data if(card.type == tag_ntag){ //capability container in page 3, byte 2 // // //0x12 -> ntag213 // //0x3e -> ntag215 // //0x6d -> ntag216 //user data in pages 4 trough 39 bytes = yhy_sync_read(reader_fd, 3, data, sizeof(data)); } else if(card.type == tag_mifare1){ } reader_tag_present(FLAG_TAG_LOCKED, card); } 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; }