Reformat using "make style"

This commit is contained in:
Ludovic Rousseau 2012-06-12 14:37:19 +00:00
parent dec0c7ef0d
commit 7629b729c4

View File

@ -1,21 +1,21 @@
/** /**
* NFC utils - lsnfc * NFC utils - lsnfc
* *
* Copyright (C) 2009, 2010, Romuald Conty * Copyright (C) 2009, 2010, Romuald Conty
* *
* This program is free software: you can redistribute it and/or modify it * This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free * under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) * Software Foundation, either version 3 of the License, or (at your option)
* any later version. * any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT * This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details. * more details.
* *
* You should have received a copy of the GNU General Public License along with * You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/> * this program. If not, see <http://www.gnu.org/licenses/>
*/ */
/* /*
* This implementation was written based on information provided by the * This implementation was written based on information provided by the
@ -47,7 +47,7 @@ static nfc_device *pnd;
#define MAX_TARGET_COUNT 16 #define MAX_TARGET_COUNT 16
#define MAX_ATS_LENGTH 32 #define MAX_ATS_LENGTH 32
typedef char* (*identication_hook)(const nfc_iso14443a_info nai); typedef char *(*identication_hook)(const nfc_iso14443a_info nai);
struct iso14443a_tag { struct iso14443a_tag {
uint8_t SAK; uint8_t SAK;
@ -63,15 +63,15 @@ struct felica_tag {
}; };
void void
print_hex (const uint8_t* pbtData, size_t szData) print_hex(const uint8_t *pbtData, size_t szData)
{ {
for (size_t i = 0; i < szData; i++) { for (size_t i = 0; i < szData; i++) {
printf ("%02x", pbtData[i]); printf("%02x", pbtData[i]);
} }
printf ("\n"); printf("\n");
} }
char* char *
mifare_ultralight_identification(const nfc_iso14443a_info nai) mifare_ultralight_identification(const nfc_iso14443a_info nai)
{ {
uint8_t abtCmd[2]; uint8_t abtCmd[2];
@ -86,16 +86,16 @@ mifare_ultralight_identification(const nfc_iso14443a_info nai)
.nmt = NMT_ISO14443A, .nmt = NMT_ISO14443A,
.nbr = NBR_106 .nbr = NBR_106
}; };
if((res = nfc_initiator_select_passive_target(pnd, nm, nai.abtUid, nai.szUidLen, NULL)) >= 0 ) { if ((res = nfc_initiator_select_passive_target(pnd, nm, nai.abtUid, nai.szUidLen, NULL)) >= 0) {
nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, false); nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, false);
if ((res = nfc_initiator_transceive_bytes(pnd, abtCmd,sizeof(abtCmd), abtRx, sizeof(abtRx), 0)) >= 0) { if ((res = nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 0)) >= 0) {
// AUTH step1 command success, so it's a Ultralight C // AUTH step1 command success, so it's a Ultralight C
nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true); nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true);
nfc_initiator_deselect_target(pnd); nfc_initiator_deselect_target(pnd);
return strdup(" C"); return strdup(" C");
} else { } else {
// When a Auth failed, the tag returns in HALT state, so we don't need to deselect tag // When a Auth failed, the tag returns in HALT state, so we don't need to deselect tag
nfc_device_set_property_bool (pnd, NP_EASY_FRAMING, true); nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true);
return NULL; return NULL;
} }
} else { } else {
@ -111,31 +111,31 @@ Document used to code this function:
AN094533 AN094533
DESFire EV1- Features and Hints DESFire EV1- Features and Hints
*/ */
char* char *
mifare_desfire_identification(const nfc_iso14443a_info nai) mifare_desfire_identification(const nfc_iso14443a_info nai)
{ {
uint8_t abtCmd[] = { 0x60 }; // MIFARE DESFire GetVersion command uint8_t abtCmd[] = { 0x60 }; // MIFARE DESFire GetVersion command
uint8_t abtRx[265]; uint8_t abtRx[265];
size_t szRxLen; size_t szRxLen;
uint8_t abtDESFireVersion[14]; uint8_t abtDESFireVersion[14];
char* res = NULL; char *res = NULL;
int nfcRes = 0; int nfcRes = 0;
nfc_modulation nm = { nfc_modulation nm = {
.nmt = NMT_ISO14443A, .nmt = NMT_ISO14443A,
.nbr = NBR_106 .nbr = NBR_106
}; };
if((nfcRes = nfc_initiator_select_passive_target(pnd, nm, nai.abtUid, nai.szUidLen, NULL)) >= 0 ) { if ((nfcRes = nfc_initiator_select_passive_target(pnd, nm, nai.abtUid, nai.szUidLen, NULL)) >= 0) {
if ((nfcRes = nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 0)) >= 0) { if ((nfcRes = nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 0)) >= 0) {
// MIFARE DESFire GetVersion command success, decoding... // MIFARE DESFire GetVersion command success, decoding...
if( nfcRes == 8 ) { // GetVersion should reply 8 bytes if (nfcRes == 8) { // GetVersion should reply 8 bytes
memcpy( abtDESFireVersion, abtRx + 1, 7 ); memcpy(abtDESFireVersion, abtRx + 1, 7);
abtCmd[0] = 0xAF; // ask for GetVersion next bytes abtCmd[0] = 0xAF; // ask for GetVersion next bytes
if ((nfcRes = nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 0)) >= 0) { if ((nfcRes = nfc_initiator_transceive_bytes(pnd, abtCmd, sizeof(abtCmd), abtRx, sizeof(abtRx), 0)) >= 0) {
if( nfcRes == 8 ) { // GetVersion should reply 8 bytes if (nfcRes == 8) { // GetVersion should reply 8 bytes
memcpy( abtDESFireVersion + 7, abtRx + 1, 7 ); memcpy(abtDESFireVersion + 7, abtRx + 1, 7);
res = malloc(16); // We can alloc res: we will be able to provide information res = malloc(16); // We can alloc res: we will be able to provide information
bool bEV1 = ( ( abtDESFireVersion[3] == 0x01 ) && ( abtDESFireVersion[10] == 0x01 ) ); // Hardware major version and software major version should be equals to 1 to be an DESFire EV1 bool bEV1 = ((abtDESFireVersion[3] == 0x01) && (abtDESFireVersion[10] == 0x01)); // Hardware major version and software major version should be equals to 1 to be an DESFire EV1
snprintf(res, 16, " %s%dk", bEV1 ? "EV1 " : "", (uint16_t)(1 << ((abtDESFireVersion[5] >> 1) - 10))); snprintf(res, 16, " %s%dk", bEV1 ? "EV1 " : "", (uint16_t)(1 << ((abtDESFireVersion[5] >> 1) - 10)));
} }
} }
@ -179,26 +179,26 @@ struct iso14443a_tag iso14443a_tags[] = {
void void
print_iso14443a_name(const nfc_iso14443a_info nai) print_iso14443a_name(const nfc_iso14443a_info nai)
{ {
const char *tag_name[sizeof (iso14443a_tags) / sizeof (struct iso14443a_tag)]; const char *tag_name[sizeof(iso14443a_tags) / sizeof(struct iso14443a_tag)];
int matches=0; int matches = 0;
char *additionnal_info[sizeof (iso14443a_tags) / sizeof (struct iso14443a_tag)]; char *additionnal_info[sizeof(iso14443a_tags) / sizeof(struct iso14443a_tag)];
for (size_t i = 0; i < sizeof (iso14443a_tags) / sizeof (struct iso14443a_tag); i++) { for (size_t i = 0; i < sizeof(iso14443a_tags) / sizeof(struct iso14443a_tag); i++) {
if ( (nai.btSak == iso14443a_tags[i].SAK) ) { if ((nai.btSak == iso14443a_tags[i].SAK)) {
// printf("DBG: iso14443a_tags[i].ATS_length = %d , nai.szAtsLen = %d", iso14443a_tags[i].ATS_length, nai.szAtsLen); // printf("DBG: iso14443a_tags[i].ATS_length = %d , nai.szAtsLen = %d", iso14443a_tags[i].ATS_length, nai.szAtsLen);
if ( iso14443a_tags[i].identication_fct != NULL ) { if (iso14443a_tags[i].identication_fct != NULL) {
additionnal_info[matches] = iso14443a_tags[i].identication_fct(nai); additionnal_info[matches] = iso14443a_tags[i].identication_fct(nai);
} else { } else {
additionnal_info[matches] = NULL; additionnal_info[matches] = NULL;
} }
if( iso14443a_tags[i].ATS_length == 0 ) { if (iso14443a_tags[i].ATS_length == 0) {
tag_name[matches++] = (iso14443a_tags[i].name); tag_name[matches++] = (iso14443a_tags[i].name);
continue; continue;
} }
if( iso14443a_tags[i].ATS_length == nai.szAtsLen ) { if (iso14443a_tags[i].ATS_length == nai.szAtsLen) {
if ( memcmp( nai.abtAts, iso14443a_tags[i].ATS, iso14443a_tags[i].ATS_length ) == 0 ) { if (memcmp(nai.abtAts, iso14443a_tags[i].ATS, iso14443a_tags[i].ATS_length) == 0) {
tag_name[matches++] = (iso14443a_tags[i].name); tag_name[matches++] = (iso14443a_tags[i].name);
continue; continue;
} }
@ -208,32 +208,32 @@ print_iso14443a_name(const nfc_iso14443a_info nai)
int i; int i;
if (matches != 0) { if (matches != 0) {
printf("UID="); printf("UID=");
print_hex (nai.abtUid, nai.szUidLen); print_hex(nai.abtUid, nai.szUidLen);
printf ("\n"); printf("\n");
if (matches > 1) { if (matches > 1) {
printf ("Several possible matches:\n"); printf("Several possible matches:\n");
} }
for (i = 0; i < matches; i++ ) { for (i = 0; i < matches; i++) {
printf("* %s", tag_name[i]); printf("* %s", tag_name[i]);
if( additionnal_info[i] != NULL ) { if (additionnal_info[i] != NULL) {
printf("%s", additionnal_info[i]); printf("%s", additionnal_info[i]);
free(additionnal_info[i]); free(additionnal_info[i]);
} }
printf ("\n"); printf("\n");
} }
} else { } else {
printf ("Unknown ISO14443A tag type: "); printf("Unknown ISO14443A tag type: ");
printf ("ATQA (SENS_RES): "); printf("ATQA (SENS_RES): ");
print_hex (nai.abtAtqa, 2); print_hex(nai.abtAtqa, 2);
printf (", UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1')); printf(", UID (NFCID%c): ", (nai.abtUid[0] == 0x08 ? '3' : '1'));
print_hex (nai.abtUid, nai.szUidLen); print_hex(nai.abtUid, nai.szUidLen);
printf (", SAK (SEL_RES): "); printf(", SAK (SEL_RES): ");
print_hex (&nai.btSak, 1); print_hex(&nai.btSak, 1);
if (nai.szAtsLen) { if (nai.szAtsLen) {
printf (", ATS (ATR): "); printf(", ATS (ATR): ");
print_hex (nai.abtAts, nai.szAtsLen); print_hex(nai.abtAts, nai.szAtsLen);
} }
printf ("\n"); printf("\n");
} }
} }
@ -244,24 +244,24 @@ struct felica_tag felica_tags[] = {
}; };
void void
print_nfc_felica_info (const nfc_felica_info nfi) print_nfc_felica_info(const nfc_felica_info nfi)
{ {
printf (" ID (NFCID2): "); printf(" ID (NFCID2): ");
print_hex (nfi.abtId, 8); print_hex(nfi.abtId, 8);
printf (" Parameter (PAD): "); printf(" Parameter (PAD): ");
print_hex (nfi.abtPad, 8); print_hex(nfi.abtPad, 8);
printf (" System Code (SC): "); printf(" System Code (SC): ");
print_hex (nfi.abtSysCode, 2); print_hex(nfi.abtSysCode, 2);
for (size_t i = 0; i < sizeof (felica_tags) / sizeof (struct felica_tag); i++) { for (size_t i = 0; i < sizeof(felica_tags) / sizeof(struct felica_tag); i++) {
if ( (nfi.abtSysCode[0] == felica_tags[i].abtSysCode[0]) && (nfi.abtSysCode[1] == felica_tags[i].abtSysCode[1]) ) { if ((nfi.abtSysCode[0] == felica_tags[i].abtSysCode[0]) && (nfi.abtSysCode[1] == felica_tags[i].abtSysCode[1])) {
printf (" %s\n", felica_tags[i].name); printf(" %s\n", felica_tags[i].name);
} }
} }
} }
int int
main (int argc, const char *argv[]) main(int argc, const char *argv[])
{ {
uint8_t device_count = 0; uint8_t device_count = 0;
uint8_t device_tag_count = 0; // per device uint8_t device_tag_count = 0; // per device
@ -278,98 +278,98 @@ main (int argc, const char *argv[])
// Try to open the NFC device // Try to open the NFC device
nfc_connstring connstrings[MAX_DEVICE_COUNT]; nfc_connstring connstrings[MAX_DEVICE_COUNT];
szDeviceFound = nfc_list_devices (NULL, connstrings, MAX_DEVICE_COUNT); szDeviceFound = nfc_list_devices(NULL, connstrings, MAX_DEVICE_COUNT);
if (szDeviceFound == 0) { if (szDeviceFound == 0) {
ERR ("%s", "No device found."); ERR("%s", "No device found.");
} }
for (size_t i = 0; i < szDeviceFound; i++) { for (size_t i = 0; i < szDeviceFound; i++) {
pnd = nfc_open (NULL, connstrings[i]); pnd = nfc_open(NULL, connstrings[i]);
nfc_target ant[MAX_TARGET_COUNT]; nfc_target ant[MAX_TARGET_COUNT];
device_count++; device_count++;
device_tag_count = 0; device_tag_count = 0;
if (pnd == NULL) { if (pnd == NULL) {
ERR ("%s", "Unable to connect to NFC device."); ERR("%s", "Unable to connect to NFC device.");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
nfc_initiator_init (pnd); nfc_initiator_init(pnd);
// Drop the field for a while // Drop the field for a while
nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false); nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false);
// Let the reader only try once to find a tag // Let the reader only try once to find a tag
nfc_device_set_property_bool (pnd, NP_INFINITE_SELECT, false); nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false);
// Enable field so more power consuming cards can power themselves up // Enable field so more power consuming cards can power themselves up
nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, true); nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true);
printf ("NFC device: %s \n", nfc_device_get_name (pnd)); printf("NFC device: %s \n", nfc_device_get_name(pnd));
nfc_modulation nm = { nfc_modulation nm = {
.nmt = NMT_ISO14443A, .nmt = NMT_ISO14443A,
.nbr = NBR_106 .nbr = NBR_106
}; };
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT )) >= 0) { if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
size_t n; size_t n;
for(n = 0; n < res; n++) { for (n = 0; n < res; n++) {
print_iso14443a_name (ant[n].nti.nai); print_iso14443a_name(ant[n].nti.nai);
} }
device_tag_count += res; device_tag_count += res;
} }
nm.nmt = NMT_ISO14443B; nm.nmt = NMT_ISO14443B;
if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT )) >= 0) { if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
size_t n; size_t n;
for(n=0; n<res; n++) { for (n = 0; n < res; n++) {
printf (" ISO14443B: "); printf(" ISO14443B: ");
printf ("PUPI: "); printf("PUPI: ");
print_hex (ant[n].nti.nbi.abtPupi, 4); print_hex(ant[n].nti.nbi.abtPupi, 4);
printf (" Application Data: "); printf(" Application Data: ");
print_hex (ant[n].nti.nbi.abtApplicationData, 4); print_hex(ant[n].nti.nbi.abtApplicationData, 4);
printf (" Protocol Info: "); printf(" Protocol Info: ");
print_hex (ant[n].nti.nbi.abtProtocolInfo, 3); print_hex(ant[n].nti.nbi.abtProtocolInfo, 3);
printf ("\n"); printf("\n");
} }
device_tag_count += res; device_tag_count += res;
} else { } else {
nfc_perror (pnd, "nfc_initiator_list_passive_targets"); nfc_perror(pnd, "nfc_initiator_list_passive_targets");
} }
nm.nmt = NMT_FELICA; nm.nmt = NMT_FELICA;
nm.nbr = NBR_212; nm.nbr = NBR_212;
// List Felica tags // List Felica tags
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
int n; int n;
for (n = 0; n < res; n++) { for (n = 0; n < res; n++) {
print_nfc_felica_info (ant[n].nti.nfi); print_nfc_felica_info(ant[n].nti.nfi);
printf ("\n"); printf("\n");
} }
device_tag_count += res; device_tag_count += res;
} }
nm.nbr = NBR_424; nm.nbr = NBR_424;
if ((res = nfc_initiator_list_passive_targets (pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) { if ((res = nfc_initiator_list_passive_targets(pnd, nm, ant, MAX_TARGET_COUNT)) >= 0) {
int n; int n;
for (n = 0; n < res; n++) { for (n = 0; n < res; n++) {
print_nfc_felica_info (ant[n].nti.nfi); print_nfc_felica_info(ant[n].nti.nfi);
printf ("\n"); printf("\n");
} }
device_tag_count += res; device_tag_count += res;
} }
printf ("%d tag(s) on device.\n", device_tag_count); printf("%d tag(s) on device.\n", device_tag_count);
tag_count += device_tag_count; tag_count += device_tag_count;
// Disable field // Disable field
nfc_device_set_property_bool (pnd, NP_ACTIVATE_FIELD, false); nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false);
nfc_close (pnd); nfc_close(pnd);
} }
if (device_count > 1) { if (device_count > 1) {
printf ("Total: %d tag(s) on %d device(s).\n", tag_count, device_count); printf("Total: %d tag(s) on %d device(s).\n", tag_count, device_count);
} }
nfc_exit(NULL); nfc_exit(NULL);