Re-ident examples/nfc-mfultralight.c using "indent -br -ce --line-length120 -nut -i2 -ppi 2" command line.
This commit is contained in:
parent
470dd8be05
commit
688ebe2324
1 changed files with 107 additions and 118 deletions
|
@ -47,14 +47,16 @@ static mifare_param mp;
|
|||
static mifareul_tag mtDump;
|
||||
static uint32_t uiBlocks = 0xF;
|
||||
|
||||
static void print_success_or_failure(bool bFailure, uint32_t* uiCounter)
|
||||
static void
|
||||
print_success_or_failure (bool bFailure, uint32_t * uiCounter)
|
||||
{
|
||||
printf ("%c", (bFailure) ? 'x' : '.');
|
||||
if (uiCounter)
|
||||
*uiCounter += (bFailure) ? 0 : 1;
|
||||
}
|
||||
|
||||
static bool read_card(void)
|
||||
static bool
|
||||
read_card (void)
|
||||
{
|
||||
uint32_t page;
|
||||
bool bFailure = false;
|
||||
|
@ -62,11 +64,9 @@ static bool read_card(void)
|
|||
|
||||
printf ("Reading out %d blocks |", uiBlocks + 1);
|
||||
|
||||
for (page = 0; page <= uiBlocks; page += 4)
|
||||
{
|
||||
for (page = 0; page <= uiBlocks; page += 4) {
|
||||
// Try to read out the data block
|
||||
if (nfc_initiator_mifare_cmd(pnd,MC_READ,page,&mp))
|
||||
{
|
||||
if (nfc_initiator_mifare_cmd (pnd, MC_READ, page, &mp)) {
|
||||
memcpy (mtDump.amb[page / 4].mbd.abtData, mp.mpd.abtData, 16);
|
||||
} else {
|
||||
bFailure = true;
|
||||
|
@ -85,7 +85,8 @@ static bool read_card(void)
|
|||
return (!bFailure);
|
||||
}
|
||||
|
||||
static bool write_card(void)
|
||||
static bool
|
||||
write_card (void)
|
||||
{
|
||||
uint32_t uiBlock = 0;
|
||||
int page;
|
||||
|
@ -98,12 +99,10 @@ static bool write_card(void)
|
|||
printf ("ssss");
|
||||
for (page = 0x4; page <= 0xF; page++) {
|
||||
// Show if the readout went well
|
||||
if (bFailure)
|
||||
{
|
||||
if (bFailure) {
|
||||
// printf("x");
|
||||
// When a failure occured we need to redo the anti-collision
|
||||
if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti))
|
||||
{
|
||||
if (!nfc_initiator_select_tag (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
|
||||
ERR ("!\nError: tag was removed\n");
|
||||
return false;
|
||||
}
|
||||
|
@ -115,7 +114,8 @@ static bool write_card(void)
|
|||
// writes one page at a time.
|
||||
uiBlock = page / 4;
|
||||
memcpy (mp.mpd.abtData, mtDump.amb[uiBlock].mbd.abtData + ((page % 4) * 4), 16);
|
||||
if (!nfc_initiator_mifare_cmd(pnd, MC_WRITE, page, &mp)) bFailure = true;
|
||||
if (!nfc_initiator_mifare_cmd (pnd, MC_WRITE, page, &mp))
|
||||
bFailure = true;
|
||||
}
|
||||
print_success_or_failure (bFailure, &uiWritenPages);
|
||||
}
|
||||
|
@ -126,14 +126,14 @@ static bool write_card(void)
|
|||
return true;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
int
|
||||
main (int argc, const char *argv[])
|
||||
{
|
||||
bool bReadAction;
|
||||
byte_t *pbtUID;
|
||||
FILE *pfDump;
|
||||
|
||||
if (argc < 3)
|
||||
{
|
||||
if (argc < 3) {
|
||||
printf ("\n");
|
||||
printf ("%s r|w <dump.mfd>\n", argv[0]);
|
||||
printf ("\n");
|
||||
|
@ -147,20 +147,17 @@ int main(int argc, const char* argv[])
|
|||
|
||||
bReadAction = tolower ((int) ((unsigned char) *(argv[1])) == 'r');
|
||||
|
||||
if (bReadAction)
|
||||
{
|
||||
if (bReadAction) {
|
||||
memset (&mtDump, 0x00, sizeof (mtDump));
|
||||
} else {
|
||||
pfDump = fopen (argv[2], "rb");
|
||||
|
||||
if (pfDump == NULL)
|
||||
{
|
||||
if (pfDump == NULL) {
|
||||
ERR ("Could not open dump file: %s\n", argv[2]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (fread(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump))
|
||||
{
|
||||
if (fread (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
||||
ERR ("Could not read from dump file: %s\n", argv[2]);
|
||||
fclose (pfDump);
|
||||
return 1;
|
||||
|
@ -171,8 +168,7 @@ int main(int argc, const char* argv[])
|
|||
|
||||
// Try to open the NFC reader
|
||||
pnd = nfc_connect (NULL);
|
||||
if (pnd == NULL)
|
||||
{
|
||||
if (pnd == NULL) {
|
||||
ERR ("Error connecting NFC reader\n");
|
||||
return 1;
|
||||
}
|
||||
|
@ -193,13 +189,11 @@ int main(int argc, const char* argv[])
|
|||
printf ("Connected to NFC reader: %s\n", pnd->acName);
|
||||
|
||||
// Try to find a MIFARE Ultralight tag
|
||||
if (!nfc_initiator_select_tag(pnd,NM_ISO14443A_106,NULL,0,&nti))
|
||||
{
|
||||
if (!nfc_initiator_select_tag (pnd, NM_ISO14443A_106, NULL, 0, &nti)) {
|
||||
ERR ("no tag was found\n");
|
||||
nfc_disconnect (pnd);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Test if we are dealing with a MIFARE compatible tag
|
||||
|
||||
if (nti.nai.abtAtqa[1] != 0x44) {
|
||||
|
@ -208,25 +202,20 @@ int main(int argc, const char* argv[])
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
// Get the info from the current tag (UID is stored little-endian)
|
||||
pbtUID = nti.nai.abtUid;
|
||||
printf ("Found MIFARE Ultralight card with UID: %02x%02x%02x%02x\n", pbtUID[3], pbtUID[2], pbtUID[1], pbtUID[0]);
|
||||
|
||||
if (bReadAction)
|
||||
{
|
||||
if (read_card())
|
||||
{
|
||||
if (bReadAction) {
|
||||
if (read_card ()) {
|
||||
printf ("Writing data to file: %s ... ", argv[2]);
|
||||
fflush (stdout);
|
||||
pfDump = fopen (argv[2], "wb");
|
||||
if (pfDump == NULL)
|
||||
{
|
||||
if (pfDump == NULL) {
|
||||
printf ("Could not open file: %s\n", argv[2]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if (fwrite(&mtDump,1,sizeof(mtDump),pfDump) != sizeof(mtDump))
|
||||
{
|
||||
if (fwrite (&mtDump, 1, sizeof (mtDump), pfDump) != sizeof (mtDump)) {
|
||||
printf ("Could not write to file: %s\n", argv[2]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue