From 9a2a07ea537b2043de5e8e61bdb81d799e3794bc Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Tue, 28 Sep 2010 19:01:28 +0000 Subject: [PATCH] Enhance error reporting. Now that the libnfc has error handling, report errors triggered in the libnfc and add support for errors triggered in libfreefare. --- NEWS | 3 ++ examples/mifare-desfire-info.c | 4 +- libfreefare/Makefile.am | 6 ++- libfreefare/freefare.c | 24 ++++++++++ libfreefare/freefare.h | 7 +-- ...ifare_desfire_error.3 => freefare_error.3} | 46 ++++++++++++++----- libfreefare/freefare_internal.h | 1 + libfreefare/mifare_desfire.3 | 2 - libfreefare/mifare_desfire.c | 8 ---- 9 files changed, 73 insertions(+), 28 deletions(-) rename libfreefare/{mifare_desfire_error.3 => freefare_error.3} (76%) diff --git a/NEWS b/NEWS index efcee0b..59f944a 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ Changes between 0.2.0 and 0.2.1 [xx XXX xxxx] + *) The mifare_desfire_error_lookup() and mifare_desfire_get_last_error() + functions were removed and replaced by the freefare_strerror(), + freefare_strerror_r() and freefare_perror() functions. *) Blok numbers in ISO14443-4 blocks are incremented according to the specification. diff --git a/examples/mifare-desfire-info.c b/examples/mifare-desfire-info.c index 88b622b..91220e4 100644 --- a/examples/mifare-desfire-info.c +++ b/examples/mifare-desfire-info.c @@ -76,7 +76,7 @@ main(int argc, char *argv[]) res = mifare_desfire_get_version (tags[i], &info); if (res < 0) { - warnx ("Can't get Mifare DESFire version information."); + freefare_perror (tags[i], "mifare_desfire_get_version"); error = 1; break; } @@ -104,7 +104,7 @@ main(int argc, char *argv[]) uint8_t max_keys; res = mifare_desfire_get_key_settings (tags[i], &settings, &max_keys); if (res < 0) { - warnx ("Cant' get Mifare DESFire Master Key settings: %s", mifare_desfire_error_lookup (mifare_desfire_get_last_error (tags[i]))); + freefare_perror (tags[i], "mifare_desfire_get_key_settings"); error = 1; break; } diff --git a/libfreefare/Makefile.am b/libfreefare/Makefile.am index 09f0694..9b3276c 100644 --- a/libfreefare/Makefile.am +++ b/libfreefare/Makefile.am @@ -32,12 +32,12 @@ libfreefare_la_HEADERS = freefare.h libfreefare_ladir = $(includedir) man_MANS = freefare.3 \ + freefare_error.3 \ mad.3 \ mifare_application.3 \ mifare_classic.3 \ mifare_desfire.3 \ mifare_desfire_aid.3 \ - mifare_desfire_error.3 \ mifare_desfire_key.3 \ mifare_ultralight.3 \ tlv.3 @@ -48,6 +48,9 @@ linkedman = \ freefare.3 freefare_get_tag_type.3 \ freefare.3 freefare_get_tag_uid.3 \ freefare.3 freefare_get_tags.3 \ + freefare_error.3 freefare_strerror.3 \ + freefare_error.3 freefare_strerror_r.3 \ + freefare_error.3 freefare_perror.3 \ mad.3 mad_free.3 \ mad.3 mad_get_aid.3 \ mad.3 mad_get_card_publisher_sector.3 \ @@ -123,7 +126,6 @@ linkedman = \ mifare_desfire.3 mifare_desfire_write_record_ex.3 \ mifare_desfire_aid.3 mifare_desfire_aid_new.3 \ mifare_desfire_aid.3 mifare_desfire_aid_new_with_mad_aid.3 \ - mifare_desfire_error.3 mifare_desfire_error_lookup.3 \ mifare_desfire_key.3 mifare_desfire_3des_key_new.3 \ mifare_desfire_key.3 mifare_desfire_3des_key_new_with_version.3 \ mifare_desfire_key.3 mifare_desfire_des_key_new.3 \ diff --git a/libfreefare/freefare.c b/libfreefare/freefare.c index 5b56759..bd1b380 100644 --- a/libfreefare/freefare.c +++ b/libfreefare/freefare.c @@ -203,6 +203,30 @@ freefare_free_tag (MifareTag tag) } } +const char * +freefare_strerror (MifareTag tag) +{ + const char *p = "Unkown error"; + if (tag->device->iLastError > 0) + p = nfc_strerror (tag->device); + else if (tag->tag_info->type == DESFIRE) + p = mifare_desfire_error_lookup (MIFARE_DESFIRE (tag)->last_picc_error); + + return p; +} + +int +freefare_strerror_r (MifareTag tag, char *buffer, size_t len) +{ + return (snprintf (buffer, len, "%s", freefare_strerror (tag)) < 0) ? -1 : 0; +} + +void +freefare_perror (MifareTag tag, const char *string) +{ + fprintf (stderr, "%s: %s\n", string, freefare_strerror (tag)); +} + /* * Free the provided tag list. */ diff --git a/libfreefare/freefare.h b/libfreefare/freefare.h index e0b0bbb..f93d4a5 100644 --- a/libfreefare/freefare.h +++ b/libfreefare/freefare.h @@ -56,6 +56,10 @@ char *freefare_get_tag_uid (MifareTag tag); void freefare_free_tag (MifareTag tag); void freefare_free_tags (MifareTag *tags); +const char *freefare_strerror (MifareTag tag); +int freefare_strerror_r (MifareTag tag, char *buffer, size_t len); +void freefare_perror (MifareTag tag, const char *string); + int mifare_ultralight_connect (MifareTag tag); int mifare_ultralight_disconnect (MifareTag tag); @@ -295,7 +299,6 @@ struct mifare_desfire_file_settings { int mifare_desfire_connect (MifareTag tag); int mifare_desfire_disconnect (MifareTag tag); -uint8_t mifare_desfire_get_last_error (MifareTag tag); int mifare_desfire_authenticate (MifareTag tag, uint8_t key_no, MifareDESFireKey key); int mifare_desfire_change_key_settings (MifareTag tag, uint8_t settings); @@ -347,8 +350,6 @@ uint8_t mifare_desfire_key_get_version (MifareDESFireKey key); void mifare_desfire_key_set_version (MifareDESFireKey key, uint8_t version); void mifare_desfire_key_free (MifareDESFireKey key); -const char *mifare_desfire_error_lookup (uint8_t error); - uint8_t *tlv_encode (const uint8_t type, const uint8_t *istream, uint16_t isize, size_t *osize); uint8_t *tlv_decode (const uint8_t *istream, uint8_t *type, uint16_t *size); uint8_t *tlv_append (uint8_t *a, uint8_t *b); diff --git a/libfreefare/mifare_desfire_error.3 b/libfreefare/freefare_error.3 similarity index 76% rename from libfreefare/mifare_desfire_error.3 rename to libfreefare/freefare_error.3 index bf88fd0..d71858a 100644 --- a/libfreefare/mifare_desfire_error.3 +++ b/libfreefare/freefare_error.3 @@ -15,8 +15,8 @@ .\" .\" $Id: mifare_desfire.3 495 2010-07-26 21:48:18Z rtartiere@il4p.fr $ .\" -.Dd September 3, 2010 -.Dt MIFARE_DESFIRE_ERROR 3 +.Dd September 28, 2010 +.Dt FREEFARE_ERROR 3 .Os .\" _ _ .\" | \ | | __ _ _ __ ___ ___ @@ -25,8 +25,10 @@ .\" |_| \_|\__,_|_| |_| |_|\___| .\" .Sh NAME -.Nm mifare_desfire_error_lookup -.Nd Mifare DESFire Error Management Functions. +.Nm freefare_strerror , +.Nm freefare_strerror_r , +.Nm freefare_perror +.Nd Error Reporting Functions. .\" _ _ _ .\" | | (_) |__ _ __ __ _ _ __ _ _ .\" | | | | '_ \| '__/ _` | '__| | | | @@ -44,7 +46,11 @@ Mifare card manipulation library (libfreefare, \-lfreefare) .Sh SYNOPSIS .In freefare.h .Ft "const char *" -.Fn mifare_desfire_error_lookup "uint8_t code" +.Fn freefare_strerror "MifareTag tag" +.Ft "int" +.Fn freefare_strerror_r "MifareTag tag" "char *buffer" "size_t len" +.Ft "void" +.Fn freefare_strerror "MifareTag tag" "char *string" .\" ____ _ _ _ .\" | _ \ ___ ___ ___ _ __(_)_ __ | |_(_) ___ _ __ .\" | | | |/ _ \/ __|/ __| '__| | '_ \| __| |/ _ \| '_ \ @@ -53,9 +59,26 @@ Mifare card manipulation library (libfreefare, \-lfreefare) .\" |_| .Sh DESCRIPTION The -.Fn mifare_desfire_error_lookup -functions returns the error string corresponding to the provided error -.Vt code . +.Fn freefare_strerror +functions returns the error string corresponding to the last PCD or PICC error +encounred using +.Vt tag . +.Pp +The +.Fn freefare_strerror_r +functions does the same as +.Fn freefare_strerror +but the error message is copied into +.Vt buffer +for a maximum size of +.Vt len +chars. +.Pp +The +.Fn freefare_strerror +function displays the last PCD or PICC error encounred using +.Vt tag +to stderr. .\" ____ _ _ .\" | _ \ ___| |_ _ _ _ __ _ __ __ ____ _| |_ _ ___ ___ .\" | |_) / _ \ __| | | | '__| '_ \ \ \ / / _` | | | | |/ _ \/ __| @@ -63,8 +86,10 @@ functions returns the error string corresponding to the provided error .\" |_| \_\___|\__|\__,_|_| |_| |_| \_/ \__,_|_|\__,_|\___||___/ .\" .Sh RETURN VALUES -The functions always succeeds. If an invalid error code is provided, a -corresponding message is returned. +.Fn freefare_strerror +returns the error message, while +.Fn freefare_strerror_r +returns 0 on success, -1 on failure. .\" ____ _ .\" / ___| ___ ___ __ _| |___ ___ .\" \___ \ / _ \/ _ \ / _` | / __|/ _ \ @@ -73,7 +98,6 @@ corresponding message is returned. .\" .Sh SEE ALSO .Xr freefare 3 , -.Xr mifare_desfire 3 .\" _ _ _ .\" / \ _ _| |_| |__ ___ _ __ ___ .\" / _ \| | | | __| '_ \ / _ \| '__/ __| diff --git a/libfreefare/freefare_internal.h b/libfreefare/freefare_internal.h index 2ad5123..4559685 100644 --- a/libfreefare/freefare_internal.h +++ b/libfreefare/freefare_internal.h @@ -189,6 +189,7 @@ struct mifare_desfire_tag { }; MifareDESFireKey mifare_desfire_session_key_new (uint8_t rnda[8], uint8_t rndb[8], MifareDESFireKey authentication_key); +const char *mifare_desfire_error_lookup (uint8_t error); struct mifare_ultralight_tag { struct mifare_tag __tag; diff --git a/libfreefare/mifare_desfire.3 b/libfreefare/mifare_desfire.3 index e618789..01034a3 100644 --- a/libfreefare/mifare_desfire.3 +++ b/libfreefare/mifare_desfire.3 @@ -94,8 +94,6 @@ Mifare card manipulation library (libfreefare, \-lfreefare) .Fn mifare_desfire_connect "MifareTag tag" .Ft int .Fn mifare_desfire_disconnect "MifareTag tag" -.\".Ft uint8_t -.\".Fn mifare_desfire_get_last_error "MifareTag tag" .Ft int .Fn mifare_desfire_authenticate "MifareTag tag" "uint8_t key_no" "MifareDESFireKey key" .Ft int diff --git a/libfreefare/mifare_desfire.c b/libfreefare/mifare_desfire.c index b0a3dad..c5ac568 100644 --- a/libfreefare/mifare_desfire.c +++ b/libfreefare/mifare_desfire.c @@ -338,14 +338,6 @@ mifare_desfire_disconnect (MifareTag tag) -uint8_t -mifare_desfire_get_last_error (MifareTag tag) -{ - return MIFARE_DESFIRE (tag)->last_picc_error; -} - - - int mifare_desfire_authenticate (MifareTag tag, uint8_t key_no, MifareDESFireKey key) {