void NFCReaderUnit::refreshChipList() { nfc_safe_call(nfc_initiator_init, d_device); // Drop the field for a while nfc_safe_call(nfc_device_set_property_bool, d_device, NP_ACTIVATE_FIELD, false); // Configure the CRC and Parity settings nfc_safe_call(nfc_device_set_property_bool, d_device, NP_HANDLE_CRC, true); nfc_safe_call(nfc_device_set_property_bool, d_device, NP_HANDLE_PARITY, true); nfc_safe_call(nfc_device_set_property_bool, d_device, NP_AUTO_ISO14443_4, true); // Enable field so more power consuming cards can power themselves up nfc_safe_call(nfc_device_set_property_bool, d_device, NP_ACTIVATE_FIELD, true); // Poll for a ISO14443A (MIFARE) tag nfc_target candidates[MAX_CANDIDATES]; int candidates_count; nfc_modulation modulation; modulation.nmt = NMT_ISO14443A; modulation.nbr = NBR_106; if ((candidates_count = nfc_initiator_list_passive_targets(d_device, modulation, candidates, MAX_CANDIDATES)) < 0) throw LibLogicalAccessException("ISO14443A nfc_initiator_list_passive_targets error"); for (int c = 0; c < candidates_count; c++) { std::string ctype = getCardTypeFromTarget(candidates[c]); if (ctype != "") { std::shared_ptr<Chip> chip = createChip(ctype); if (chip) { d_chips[chip] = candidates[c]; } } } // Poll for a FELICA tag modulation.nmt = NMT_FELICA; modulation.nbr = NBR_424; // FIXME NBR_212 should also be supported if ((candidates_count = nfc_initiator_list_passive_targets(d_device, modulation, candidates, MAX_CANDIDATES)) < 0) throw LibLogicalAccessException("FELICA nfc_initiator_list_passive_targets error"); for (int c = 0; c < candidates_count; c++) { std::string ctype = getCardTypeFromTarget(candidates[c]); if (ctype != "") { std::shared_ptr<Chip> chip = createChip(ctype); if (chip) { d_chips[chip] = candidates[c]; } } } }
/* * Get a list of the MIFARE targets near to the provided NFC initiator. * * The list has to be freed using the freefare_free_tags() function. */ MifareTag * freefare_get_tags (nfc_device *device) { MifareTag *tags = NULL; int tag_count = 0; nfc_initiator_init(device); // Drop the field for a while nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,false); // Configure the CRC and Parity settings nfc_device_set_property_bool(device,NP_HANDLE_CRC,true); nfc_device_set_property_bool(device,NP_HANDLE_PARITY,true); nfc_device_set_property_bool(device,NP_AUTO_ISO14443_4,true); // Enable field so more power consuming cards can power themselves up nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,true); // Poll for a ISO14443A (MIFARE) tag nfc_target *candidates = pvPortMalloc(MAX_CANDIDATES*sizeof(nfc_target)); int candidates_count; nfc_modulation modulation = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; if ((candidates_count = nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES)) < 0) { vPortFree(candidates); return NULL; } tags = pvPortMalloc(MAX_CANDIDATES*sizeof (void *)); if(!tags) { vPortFree(candidates); return NULL; } memset (tags,0,MAX_CANDIDATES*sizeof (void *)); for (int c = 0; c < candidates_count; c++) { MifareTag t; if ((t = freefare_tag_new(device, candidates[c].nti.nai))) { /* (Re)Allocate memory for the found MIFARE targets array */ /* MifareTag *p = pvPortRealloc (tags, (tag_count + 2) * sizeof (MifareTag)); if (p) tags = p; else { vPortFree(candidates); return tags; // FAIL! Return what has been found so far. }*/ tags[tag_count++] = t; // tags[tag_count] = NULL; } } vPortFree(candidates); return tags; }
static int list_passive_targets(nfc_device *_pnd) { int res = 0; nfc_target ant[MAX_TARGET_COUNT]; if (nfc_initiator_init(_pnd) < 0) { return -EXIT_FAILURE; } if ((res = nfc_initiator_list_passive_targets(_pnd, nmMifare, ant, MAX_TARGET_COUNT)) >= 0) { int i; if (res > 0) printf("%d ISO14443A passive target(s) found:\n", res); for (i = 0; i < res; i++) { size_t szPos; printf("\t"); for (szPos = 0; szPos < ant[i].nti.nai.szUidLen; szPos++) { printf("%02x", ant[i].nti.nai.abtUid[szPos]); } printf("\n"); } } return 0; }
void main_loop(void *data) { int i,j,n; int32_t fseq = 0; //verbose = 2; // get the local IP adress for the TUIO2 source attribute char hostname[64]; struct hostent *hp = NULL; struct in_addr *addr = NULL; gethostname(hostname, 64); hp = gethostbyname(hostname); if (hp==NULL) { sprintf(hostname, "%s.local", hostname); hp = gethostbyname(hostname); } if (hp!=NULL) { for (i = 0; hp->h_addr_list[i] != 0; ++i) { addr = (struct in_addr *)(hp->h_addr_list[i]); } } else { //generate a random internet address srand ( (unsigned int)time(NULL) ); int32_t r = rand(); addr = (struct in_addr*)&r; } while (running) { uint8_t tag_count = 0; bool updated = false; lo_bundle osc_bundle = lo_bundle_new(LO_TT_IMMEDIATE); nfc_target ant[MAX_TARGET_COUNT]; // List ISO14443A targets if (verbose>1) printf("polling for a ISO14443A (MIFARE) tag:\n"); nfc_modulation nm = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; lo_timetag frame_time; lo_timetag_now (&frame_time); for (int dev=0;dev<no_devices;dev++) { if (pnd[dev] == NULL) continue; if (!running) return; lo_message frm_message = lo_message_new(); lo_message_add_int32(frm_message, fseq); lo_message_add_timetag(frm_message, frame_time); lo_message_add_int32(frm_message, 0); // sensor dim if (device_count>1) sprintf(source_string, "NFOSC:%d",dev); lo_message_add_string(frm_message, source_string); // source name lo_bundle_add_message(osc_bundle, "/tuio2/frm", frm_message); fseq++; int szTargetFound = nfc_initiator_list_passive_targets (pnd[dev], nm, ant, MAX_TARGET_COUNT); if (szTargetFound<0) { if (no_devices==1) printf("NFC reader disconnected ...\n"); else printf("NFC reader #%d disconnected ...\n",dev); pnd[dev] = NULL; device_count--; if (device_count==0) { running=false; return; } continue; } else if (szTargetFound>0) { for (n = 0; n < szTargetFound; n++) { if (!running) return; bool added = false; nfosc_t found_tag; found_tag.type_id = MIFARE_OTHER; found_tag.device_id = dev; decode_hex(ant[n].nti.nai.abtUid,ant[n].nti.nai.szUidLen); strcpy(found_tag.uid_str,uid_str); if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x09)) { if (verbose>1) printf("NXP MIFARE Mini - UID: %s\n",uid_str); found_tag.type_id = MIFARE_MINI; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x08)) { if (verbose>1) printf("NXP MIFARE Classic 1K - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_1K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x02) && (ant[n].nti.nai.btSak == 0x18)) { if (verbose>1) printf("NXP MIFARE Classic 4K - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_4K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x02) && (ant[n].nti.nai.btSak == 0x38)) { if (verbose>1) printf("Nokia MIFARE Classic 4K - emulated - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_4K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x44) && (ant[n].nti.nai.btSak == 0x00)) { if (verbose>1) printf("NXP MIFARE Ultralight - UID: %s\n",uid_str); found_tag.type_id = MIFARE_ULTRALIGHT; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x03) && (ant[n].nti.nai.abtAtqa[1] == 0x44) && (ant[n].nti.nai.btSak == 0x20)) { if (verbose>1) printf("NXP MIFARE DESFire - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x03) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x28)) { if (verbose>1) printf("NXP JCOP31 - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x48) && (ant[n].nti.nai.btSak == 0x20)) { /* @todo handle ATS to be able to know which one it is */ if (verbose>1) printf("NXP JCOP31 or JCOP41 - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x28)) { if (verbose>1) printf("NXP JCOP41 - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x04) && (ant[n].nti.nai.btSak == 0x88)) { if (verbose>1) printf("Infineon MIFARE Classic 1K - UID: %s\n",uid_str); found_tag.type_id = MIFARE_CLASSIC_1K; } else if ((ant[n].nti.nai.abtAtqa[0] == 0x00) && (ant[n].nti.nai.abtAtqa[1] == 0x02) && (ant[n].nti.nai.btSak == 0x98)) { if (verbose>1) printf("Gemplus MPCOS - UID: %s\n",uid_str); } else if ((ant[n].nti.nai.abtAtqa[0] == 0x0C) && (ant[n].nti.nai.abtAtqa[1] == 0x00)) { /* @note not sure if Jewel can be detected using this modulation */ if (verbose>1) printf("Innovision R&T Jewel - UID: %s\n",uid_str); } else { if (verbose>1) { printf("ATQA (SENS_RES): %s\n", decode_hex(ant[n].nti.nai.abtAtqa,2)); printf(" UID (NFCID%c): ",(ant[n].nti.nai.abtUid[0]==0x08?'3':'1')); printf("%s\n",decode_hex(ant[n].nti.nai.abtUid,ant[n].nti.nai.szUidLen)); printf(" SAK (SEL_RES): %s\n", decode_hex(&ant[n].nti.nai.btSak,1)); if (ant[n].nti.nai.szAtsLen) { printf(" ATS (ATR): %s\n",decode_hex(ant[n].nti.nai.abtAts,ant[n].nti.nai.szAtsLen)); } printf("\n"); } } int32_t symbol_id = max_symbol_id; int32_t i; for (i=0;i<max_symbol_id;i++) { if (strcmp(tag_database[i].uid_str,found_tag.uid_str)==0) { symbol_id = i; found_tag.symbol_id = symbol_id; break; } } if (symbol_id==max_symbol_id) { symbol_id = max_symbol_id; max_symbol_id++; found_tag.symbol_id = symbol_id; session_id++; found_tag.session_id = session_id; tag_database[symbol_id] = found_tag; tag_buffer[buffer_size] = found_tag; buffer_size++; if (verbose) printf("assigning ID %d to UID %s\n",found_tag.symbol_id,found_tag.uid_str); added = true; } else { int32_t b_pos = buffer_size; for (i=0;i<buffer_size;i++) { if ((strcmp(tag_buffer[i].uid_str,found_tag.uid_str)==0) && (tag_buffer[i].device_id==found_tag.device_id)) { found_tag.session_id = tag_buffer[i].session_id; b_pos=i; break; } } if (b_pos==buffer_size) { session_id++; found_tag.session_id = session_id; tag_buffer[buffer_size] = found_tag; buffer_size++; added=true; } } lo_message sym_message = lo_message_new(); lo_message_add_int32(sym_message, found_tag.session_id); lo_message_add_int32(sym_message, found_tag.type_id); lo_message_add_int32(sym_message, found_tag.symbol_id); switch (found_tag.type_id) { case MIFARE_ULTRALIGHT: lo_message_add_string(sym_message, "mifare/ul"); break; case MIFARE_CLASSIC_1K: lo_message_add_string(sym_message, "mifare/1k"); break; case MIFARE_CLASSIC_4K: lo_message_add_string(sym_message, "mifare/4k"); break; case MIFARE_MINI: lo_message_add_string(sym_message, "mifare/mini"); break; default: lo_message_add_string(sym_message, "mifare/other"); } lo_message_add_string(sym_message, found_tag.uid_str); lo_bundle_add_message(osc_bundle, "/tuio2/sym", sym_message); if (added) { lo_message add_message = lo_message_new(); lo_message_add_int32(add_message, found_tag.device_id); lo_message_add_int32(add_message, found_tag.symbol_id); lo_message_add_int32(add_message, found_tag.type_id); lo_message_add_string(add_message, found_tag.uid_str); lo_bundle_add_message(osc_bundle, "/nfosc/add", add_message); updated = true; if (verbose) printf("add %d %d %d %s\n",found_tag.device_id,found_tag.symbol_id,found_tag.type_id,found_tag.uid_str); } frame_buffer[tag_count] = found_tag; tag_count++; } } lo_message sid_message = lo_message_new(); for (i=0;i<tag_count;i++) if (frame_buffer[i].device_id==dev) lo_message_add_int32(sid_message, frame_buffer[i].session_id); lo_bundle_add_message(osc_bundle, "/tuio2/alv", sid_message); } for (i=0;i<buffer_size;i++) { bool removed = true; for (j=0;j<=tag_count;j++) { if ((strcmp(tag_buffer[i].uid_str,frame_buffer[j].uid_str)==0) && (tag_buffer[i].device_id == frame_buffer[j].device_id)) { removed=false; break; } } if (removed) { lo_message del_message = lo_message_new(); lo_message_add_int32(del_message, tag_buffer[i].device_id); lo_message_add_int32(del_message, tag_buffer[i].symbol_id); lo_message_add_int32(del_message, tag_buffer[i].type_id); lo_message_add_string(del_message, tag_buffer[i].uid_str); lo_bundle_add_message(osc_bundle, "/nfosc/del", del_message); updated = true; if (verbose) printf("del %d %d %d %s\n",tag_buffer[i].device_id,tag_buffer[i].symbol_id,tag_buffer[i].type_id,tag_buffer[i].uid_str); } } if (updated) { if(lo_send_bundle(target, osc_bundle) == -1) { fprintf(stderr, "an OSC error occured: %s\n", lo_address_errstr(target)); } } if (verbose>1) { if (tag_count==0) printf("no tag was found ...\n\n"); else if (tag_count==1) printf("1 tag was found ...\n\n"); else printf("%d tags were found ...\n\n",tag_count); } buffer_size = tag_count; for (i=0; i<tag_count; i++) { tag_buffer[i] = frame_buffer[i]; frame_buffer[i] = empty_tag; } for (int dev=0;dev<no_devices;dev++) { if (pnd[dev] == NULL) continue; if (nfc_device_set_property_bool(pnd[dev],NP_ACTIVATE_FIELD,false)<0) { if (running) { if (no_devices==1) printf("NFC reader disconnected ...\n"); else printf("NFC reader #%d disconnected ...\n",dev); } pnd[dev] = NULL; device_count--; if (device_count==0) { running=false; return; } } else { nfc_device_set_property_bool(pnd[dev],NP_ACTIVATE_FIELD,true); } } usleep(1000/no_devices); } }
int main(int argc, const char *argv[]) { nfc_device *pnd; nfc_target nt; const nfc_target ntbis; // Allocate only a pointer to nfc_context nfc_context *context; // Initialize libnfc and set the nfc_context nfc_init(&context); if (context == NULL) { printf("Unable to init libnfc (malloc)\n"); exit(EXIT_FAILURE); } // Display libnfc version const char *acLibnfcVersion = nfc_version(); (void)argc; printf("%s utilise libnfc v%s\n", argv[0], acLibnfcVersion); // Open, using the first available NFC device which can be in order of selection: // - default device specified using environment variable or // - first specified device in libnfc.conf (/etc/nfc) or // - first specified device in device-configuration directory (/etc/nfc/devices.d) or // - first auto-detected (if feature is not disabled in libnfc.conf) device pnd = nfc_open(context, NULL); if (pnd == NULL) { printf("ERROR: %s\n", "Unable to open NFC device."); exit(EXIT_FAILURE); } // Set opened NFC device to initiator mode if (nfc_initiator_init(pnd) < 0) { nfc_perror(pnd, "nfc_initiator_init"); exit(EXIT_FAILURE); } printf("Lecteur NFC: %s \n", nfc_device_get_name(pnd)); // Poll for a ISO14443A (MIFARE) tag const nfc_modulation nmMifare = { .nmt = NMT_ISO14443A, .nbr = NBR_106, }; int uinp_fd; if ((uinp_fd = setup_uinput_device()) < 0) { printf("Unable to find uinput device\n"); return -1; } sleep(1); long int uid = 0; int i = 0; int check = 1; while (1) { if (nfc_initiator_list_passive_targets(pnd, nmMifare, &nt, 1) > 0) { if (uid != parse_dex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen)) { //printf("%s\n", strtol(*nt.nti.nai.abtUid, 16)); i++; printf("\nmotherfucker %d\n", i); printf("Uid : "); print_hex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); printf("Parse dex : "); printf("%ld\n",parse_dex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen)); uid = parse_dex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen); //printf("Before x : "); //printf("%s\n", before_x(uid)); printf("Do x : "); do_x(uinp_fd, uid); } else { //printf("i: %d uid:%ld nt:%ld\n",i, uid, parse_dex(nt.nti.nai.abtUid, nt.nti.nai.szUidLen)); //i++; } } else { check = 0; uid = 0; } } /* Destroy the input device */ ioctl(uinp_fd, UI_DEV_DESTROY); /* Close the UINPUT device */ close(uinp_fd); // Close NFC device nfc_close(pnd); // Release the context nfc_exit(context); exit(EXIT_SUCCESS); }
/* * Get a list of the MIFARE targets near to the provided NFC initiator. * * The list has to be freed using the freefare_free_tags() function. */ FreefareTag * freefare_get_tags (nfc_device *device) { FreefareTag *tags = NULL; int tag_count = 0; nfc_initiator_init(device); // Drop the field for a while nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,false); // Configure the CRC and Parity settings nfc_device_set_property_bool(device,NP_HANDLE_CRC,true); nfc_device_set_property_bool(device,NP_HANDLE_PARITY,true); nfc_device_set_property_bool(device,NP_AUTO_ISO14443_4,true); // Enable field so more power consuming cards can power themselves up nfc_device_set_property_bool(device,NP_ACTIVATE_FIELD,true); // Poll for a ISO14443A (MIFARE) tag nfc_target candidates[MAX_CANDIDATES]; int candidates_count; nfc_modulation modulation = { .nmt = NMT_ISO14443A, .nbr = NBR_106 }; if ((candidates_count = nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES)) < 0) return NULL; tags = malloc(sizeof (void *)); if(!tags) return NULL; tags[0] = NULL; for (int c = 0; c < candidates_count; c++) { FreefareTag t; if ((t = freefare_tag_new(device, candidates[c]))) { /* (Re)Allocate memory for the found MIFARE targets array */ FreefareTag *p = realloc (tags, (tag_count + 2) * sizeof (FreefareTag)); if (p) tags = p; else return tags; // FAIL! Return what has been found so far. tags[tag_count++] = t; tags[tag_count] = NULL; } } // Poll for a FELICA tag modulation.nmt = NMT_FELICA; modulation.nbr = NBR_424; // FIXME NBR_212 should also be supported if ((candidates_count = nfc_initiator_list_passive_targets(device, modulation, candidates, MAX_CANDIDATES)) < 0) return NULL; for (int c = 0; c < candidates_count; c++) { FreefareTag t; if ((t = freefare_tag_new(device, candidates[c]))) { /* (Re)Allocate memory for the found FELICA targets array */ FreefareTag *p = realloc (tags, (tag_count + 2) * sizeof (FreefareTag)); if (p) tags = p; else return tags; // FAIL! Return what has been found so far. tags[tag_count++] = t; tags[tag_count] = NULL; } } return tags; }