libnfc  1.7.1
nfc.c
Go to the documentation of this file.
00001 /*-
00002  * Free/Libre Near Field Communication (NFC) library
00003  *
00004  * Libnfc historical contributors:
00005  * Copyright (C) 2009      Roel Verdult
00006  * Copyright (C) 2009-2013 Romuald Conty
00007  * Copyright (C) 2010-2012 Romain Tartière
00008  * Copyright (C) 2010-2013 Philippe Teuwen
00009  * Copyright (C) 2012-2013 Ludovic Rousseau
00010  * See AUTHORS file for a more comprehensive list of contributors.
00011  * Additional contributors of this file:
00012  *
00013  * This program is free software: you can redistribute it and/or modify it
00014  * under the terms of the GNU Lesser General Public License as published by the
00015  * Free Software Foundation, either version 3 of the License, or (at your
00016  * option) any later version.
00017  *
00018  * This program is distributed in the hope that it will be useful, but WITHOUT
00019  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00020  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00021  * more details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>
00025  */
00026 
00073 #ifdef HAVE_CONFIG_H
00074 #  include "config.h"
00075 #endif // HAVE_CONFIG_H
00076 
00077 #include <fcntl.h>
00078 #include <stdio.h>
00079 #include <stdlib.h>
00080 #include <stddef.h>
00081 #include <string.h>
00082 
00083 #include <nfc/nfc.h>
00084 
00085 #include "nfc-internal.h"
00086 #include "target-subr.h"
00087 #include "drivers.h"
00088 
00089 #if defined (DRIVER_ACR122_PCSC_ENABLED)
00090 #  include "drivers/acr122_pcsc.h"
00091 #endif /* DRIVER_ACR122_PCSC_ENABLED */
00092 
00093 #if defined (DRIVER_ACR122_USB_ENABLED)
00094 #  include "drivers/acr122_usb.h"
00095 #endif /* DRIVER_ACR122_USB_ENABLED */
00096 
00097 #if defined (DRIVER_ACR122S_ENABLED)
00098 #  include "drivers/acr122s.h"
00099 #endif /* DRIVER_ACR122S_ENABLED */
00100 
00101 #if defined (DRIVER_PN53X_USB_ENABLED)
00102 #  include "drivers/pn53x_usb.h"
00103 #endif /* DRIVER_PN53X_USB_ENABLED */
00104 
00105 #if defined (DRIVER_ARYGON_ENABLED)
00106 #  include "drivers/arygon.h"
00107 #endif /* DRIVER_ARYGON_ENABLED */
00108 
00109 #if defined (DRIVER_PN532_UART_ENABLED)
00110 #  include "drivers/pn532_uart.h"
00111 #endif /* DRIVER_PN532_UART_ENABLED */
00112 
00113 #if defined (DRIVER_PN532_SPI_ENABLED)
00114 #  include "drivers/pn532_spi.h"
00115 #endif /* DRIVER_PN532_SPI_ENABLED */
00116 
00117 #if defined (DRIVER_PN532_I2C_ENABLED)
00118 #  include "drivers/pn532_i2c.h"
00119 #endif /* DRIVER_PN532_I2C_ENABLED */
00120 
00121 
00122 #define LOG_CATEGORY "libnfc.general"
00123 #define LOG_GROUP    NFC_LOG_GROUP_GENERAL
00124 
00125 struct nfc_driver_list {
00126   const struct nfc_driver_list *next;
00127   const struct nfc_driver *driver;
00128 };
00129 
00130 const struct nfc_driver_list *nfc_drivers = NULL;
00131 
00132 static void
00133 nfc_drivers_init(void)
00134 {
00135 #if defined (DRIVER_PN53X_USB_ENABLED)
00136   nfc_register_driver(&pn53x_usb_driver);
00137 #endif /* DRIVER_PN53X_USB_ENABLED */
00138 #if defined (DRIVER_ACR122_PCSC_ENABLED)
00139   nfc_register_driver(&acr122_pcsc_driver);
00140 #endif /* DRIVER_ACR122_PCSC_ENABLED */
00141 #if defined (DRIVER_ACR122_USB_ENABLED)
00142   nfc_register_driver(&acr122_usb_driver);
00143 #endif /* DRIVER_ACR122_USB_ENABLED */
00144 #if defined (DRIVER_ACR122S_ENABLED)
00145   nfc_register_driver(&acr122s_driver);
00146 #endif /* DRIVER_ACR122S_ENABLED */
00147 #if defined (DRIVER_PN532_UART_ENABLED)
00148   nfc_register_driver(&pn532_uart_driver);
00149 #endif /* DRIVER_PN532_UART_ENABLED */
00150 #if defined (DRIVER_PN532_SPI_ENABLED)
00151   nfc_register_driver(&pn532_spi_driver);
00152 #endif /* DRIVER_PN532_SPI_ENABLED */
00153 #if defined (DRIVER_PN532_I2C_ENABLED)
00154   nfc_register_driver(&pn532_i2c_driver);
00155 #endif /* DRIVER_PN532_I2C_ENABLED */
00156 #if defined (DRIVER_ARYGON_ENABLED)
00157   nfc_register_driver(&arygon_driver);
00158 #endif /* DRIVER_ARYGON_ENABLED */
00159 }
00160 
00161 
00169 int
00170 nfc_register_driver(const struct nfc_driver *ndr)
00171 {
00172   if (!ndr)
00173     return NFC_EINVARG;
00174 
00175   struct nfc_driver_list *pndl = (struct nfc_driver_list *)malloc(sizeof(struct nfc_driver_list));
00176   if (!pndl)
00177     return NFC_ESOFT;
00178 
00179   pndl->driver = ndr;
00180   pndl->next = nfc_drivers;
00181   nfc_drivers = pndl;
00182 
00183   return NFC_SUCCESS;
00184 }
00185 
00191 void
00192 nfc_init(nfc_context **context)
00193 {
00194   *context = nfc_context_new();
00195   if (!*context) {
00196     perror("malloc");
00197     return;
00198   }
00199   if (!nfc_drivers)
00200     nfc_drivers_init();
00201 }
00202 
00208 void
00209 nfc_exit(nfc_context *context)
00210 {
00211   while (nfc_drivers) {
00212     struct nfc_driver_list *pndl = (struct nfc_driver_list *) nfc_drivers;
00213     nfc_drivers = pndl->next;
00214     free(pndl);
00215   }
00216 
00217   nfc_context_free(context);
00218 }
00219 
00237 nfc_device *
00238 nfc_open(nfc_context *context, const nfc_connstring connstring)
00239 {
00240   nfc_device *pnd = NULL;
00241 
00242   nfc_connstring ncs;
00243   if (connstring == NULL) {
00244     if (!nfc_list_devices(context, &ncs, 1)) {
00245       return NULL;
00246     }
00247   } else {
00248     strncpy(ncs, connstring, sizeof(nfc_connstring));
00249     ncs[sizeof(nfc_connstring) - 1] = '\0';
00250   }
00251 
00252   // Search through the device list for an available device
00253   const struct nfc_driver_list *pndl = nfc_drivers;
00254   while (pndl) {
00255     const struct nfc_driver *ndr = pndl->driver;
00256 
00257     // Specific device is requested: using device description
00258     if (0 != strncmp(ndr->name, ncs, strlen(ndr->name))) {
00259       // Check if connstring driver is usb -> accept any driver *_usb
00260       if ((0 != strncmp("usb", ncs, strlen("usb"))) || 0 != strncmp("_usb", ndr->name + (strlen(ndr->name) - 4), 4)) {
00261         pndl = pndl->next;
00262         continue;
00263       }
00264     }
00265 
00266     pnd = ndr->open(context, ncs);
00267     // Test if the opening was successful
00268     if (pnd == NULL) {
00269       if (0 == strncmp("usb", ncs, strlen("usb"))) {
00270         // We've to test the other usb drivers before giving up
00271         pndl = pndl->next;
00272         continue;
00273       }
00274       log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "Unable to open \"%s\".", ncs);
00275       return NULL;
00276     }
00277     for (uint32_t i = 0; i > context->user_defined_device_count; i++) {
00278       if (strcmp(ncs, context->user_defined_devices[i].connstring) == 0) {
00279         // This is a device sets by user, we use the device name given by user
00280         strcpy(pnd->name, context->user_defined_devices[i].name);
00281         break;
00282       }
00283     }
00284     log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "\"%s\" (%s) has been claimed.", pnd->name, pnd->connstring);
00285     return pnd;
00286   }
00287 
00288   // Too bad, no driver can decode connstring
00289   log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "No driver available to handle \"%s\".", ncs);
00290   return NULL;
00291 }
00292 
00299 void
00300 nfc_close(nfc_device *pnd)
00301 {
00302   if (pnd) {
00303     // Close, clean up and release the device
00304     pnd->driver->close(pnd);
00305   }
00306 }
00307 
00316 size_t
00317 nfc_list_devices(nfc_context *context, nfc_connstring connstrings[], const size_t connstrings_len)
00318 {
00319   size_t device_found = 0;
00320 
00321 #ifdef CONFFILES
00322   // Load manually configured devices (from config file and env variables)
00323   // TODO From env var...
00324   for (uint32_t i = 0; i < context->user_defined_device_count; i++) {
00325     if (context->user_defined_devices[i].optional) {
00326       // let's make sure the device exists
00327       nfc_device *pnd = NULL;
00328 
00329 #ifdef ENVVARS
00330       char *env_log_level = getenv("LIBNFC_LOG_LEVEL");
00331       char *old_env_log_level = NULL;
00332       // do it silently
00333       if (env_log_level) {
00334         if ((old_env_log_level = malloc(strlen(env_log_level) + 1)) == NULL) {
00335           log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_ERROR, "%s", "Unable to malloc()");
00336           return 0;
00337         }
00338         strcpy(old_env_log_level, env_log_level);
00339       }
00340       setenv("LIBNFC_LOG_LEVEL", "0", 1);
00341 #endif // ENVVARS
00342 
00343       pnd = nfc_open(context, context->user_defined_devices[i].connstring);
00344 
00345 #ifdef ENVVARS
00346       if (old_env_log_level) {
00347         setenv("LIBNFC_LOG_LEVEL", old_env_log_level, 1);
00348         free(old_env_log_level);
00349       } else {
00350         unsetenv("LIBNFC_LOG_LEVEL");
00351       }
00352 #endif // ENVVARS
00353 
00354       if (pnd) {
00355         nfc_close(pnd);
00356         log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "User device %s found", context->user_defined_devices[i].name);
00357         strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
00358         device_found ++;
00359         if (device_found == connstrings_len)
00360           break;
00361       }
00362     } else {
00363       // manual choice is not marked as optional so let's take it blindly
00364       strcpy((char *)(connstrings + device_found), context->user_defined_devices[i].connstring);
00365       device_found++;
00366       if (device_found >= connstrings_len)
00367         return device_found;
00368     }
00369   }
00370 #endif // CONFFILES
00371 
00372   // Device auto-detection
00373   if (context->allow_autoscan) {
00374     const struct nfc_driver_list *pndl = nfc_drivers;
00375     while (pndl) {
00376       const struct nfc_driver *ndr = pndl->driver;
00377       if ((ndr->scan_type == NOT_INTRUSIVE) || ((context->allow_intrusive_scan) && (ndr->scan_type == INTRUSIVE))) {
00378         size_t _device_found = ndr->scan(context, connstrings + (device_found), connstrings_len - (device_found));
00379         log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_DEBUG, "%ld device(s) found using %s driver", (unsigned long) _device_found, ndr->name);
00380         if (_device_found > 0) {
00381           device_found += _device_found;
00382           if (device_found == connstrings_len)
00383             break;
00384         }
00385       } // scan_type is INTRUSIVE but not allowed or NOT_AVAILABLE
00386       pndl = pndl->next;
00387     }
00388   } else if (context->user_defined_device_count == 0) {
00389     log_put(LOG_GROUP, LOG_CATEGORY, NFC_LOG_PRIORITY_INFO, "Warning: %s" , "user must specify device(s) manually when autoscan is disabled");
00390   }
00391 
00392   return device_found;
00393 }
00394 
00406 int
00407 nfc_device_set_property_int(nfc_device *pnd, const nfc_property property, const int value)
00408 {
00409   HAL(device_set_property_int, pnd, property, value);
00410 }
00411 
00412 
00425 int
00426 nfc_device_set_property_bool(nfc_device *pnd, const nfc_property property, const bool bEnable)
00427 {
00428   HAL(device_set_property_bool, pnd, property, bEnable);
00429 }
00430 
00451 int
00452 nfc_initiator_init(nfc_device *pnd)
00453 {
00454   int res = 0;
00455   // Drop the field for a while
00456   if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
00457     return res;
00458   // Enable field so more power consuming cards can power themselves up
00459   if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, true)) < 0)
00460     return res;
00461   // Let the device try forever to find a target/tag
00462   if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
00463     return res;
00464   // Activate auto ISO14443-4 switching by default
00465   if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
00466     return res;
00467   // Force 14443-A mode
00468   if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_ISO14443_A, true)) < 0)
00469     return res;
00470   // Force speed at 106kbps
00471   if ((res = nfc_device_set_property_bool(pnd, NP_FORCE_SPEED_106, true)) < 0)
00472     return res;
00473   // Disallow invalid frame
00474   if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
00475     return res;
00476   // Disallow multiple frames
00477   if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
00478     return res;
00479   HAL(initiator_init, pnd);
00480 }
00481 
00491 int
00492 nfc_initiator_init_secure_element(nfc_device *pnd)
00493 {
00494   HAL(initiator_init_secure_element, pnd);
00495 }
00496 
00520 int
00521 nfc_initiator_select_passive_target(nfc_device *pnd,
00522                                     const nfc_modulation nm,
00523                                     const uint8_t *pbtInitData, const size_t szInitData,
00524                                     nfc_target *pnt)
00525 {
00526   uint8_t *abtInit = NULL;
00527   uint8_t abtTmpInit[MAX(12, szInitData)];
00528   size_t  szInit = 0;
00529   if (szInitData == 0) {
00530     // Provide default values, if any
00531     prepare_initiator_data(nm, &abtInit, &szInit);
00532   } else if (nm.nmt == NMT_ISO14443A) {
00533     abtInit = abtTmpInit;
00534     iso14443_cascade_uid(pbtInitData, szInitData, abtInit, &szInit);
00535   } else {
00536     abtInit = abtTmpInit;
00537     memcpy(abtInit, pbtInitData, szInitData);
00538     szInit = szInitData;
00539   }
00540 
00541   HAL(initiator_select_passive_target, pnd, nm, abtInit, szInit, pnt);
00542 }
00543 
00560 int
00561 nfc_initiator_list_passive_targets(nfc_device *pnd,
00562                                    const nfc_modulation nm,
00563                                    nfc_target ant[], const size_t szTargets)
00564 {
00565   nfc_target nt;
00566   size_t  szTargetFound = 0;
00567   uint8_t *pbtInitData = NULL;
00568   size_t  szInitDataLen = 0;
00569   int res = 0;
00570 
00571   pnd->last_error = 0;
00572 
00573   // Let the reader only try once to find a tag
00574   bool bInfiniteSelect = pnd->bInfiniteSelect;
00575   if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
00576     return res;
00577   }
00578 
00579   prepare_initiator_data(nm, &pbtInitData, &szInitDataLen);
00580 
00581   while (nfc_initiator_select_passive_target(pnd, nm, pbtInitData, szInitDataLen, &nt) > 0) {
00582     size_t i;
00583     bool seen = false;
00584     // Check if we've already seen this tag
00585     for (i = 0; i < szTargetFound; i++) {
00586       if (memcmp(&(ant[i]), &nt, sizeof(nfc_target)) == 0) {
00587         seen = true;
00588       }
00589     }
00590     if (seen) {
00591       break;
00592     }
00593     memcpy(&(ant[szTargetFound]), &nt, sizeof(nfc_target));
00594     szTargetFound++;
00595     if (szTargets == szTargetFound) {
00596       break;
00597     }
00598     nfc_initiator_deselect_target(pnd);
00599     // deselect has no effect on FeliCa and Jewel cards so we'll stop after one...
00600     // ISO/IEC 14443 B' cards are polled at 100% probability so it's not possible to detect correctly two cards at the same time
00601     if ((nm.nmt == NMT_FELICA) || (nm.nmt == NMT_JEWEL) || (nm.nmt == NMT_ISO14443BI) || (nm.nmt == NMT_ISO14443B2SR) || (nm.nmt == NMT_ISO14443B2CT)) {
00602       break;
00603     }
00604   }
00605   if (bInfiniteSelect) {
00606     if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0) {
00607       return res;
00608     }
00609   }
00610   return szTargetFound;
00611 }
00612 
00626 int
00627 nfc_initiator_poll_target(nfc_device *pnd,
00628                           const nfc_modulation *pnmModulations, const size_t szModulations,
00629                           const uint8_t uiPollNr, const uint8_t uiPeriod,
00630                           nfc_target *pnt)
00631 {
00632   HAL(initiator_poll_target, pnd, pnmModulations, szModulations, uiPollNr, uiPeriod, pnt);
00633 }
00634 
00635 
00656 int
00657 nfc_initiator_select_dep_target(nfc_device *pnd,
00658                                 const nfc_dep_mode ndm, const nfc_baud_rate nbr,
00659                                 const nfc_dep_info *pndiInitiator, nfc_target *pnt, const int timeout)
00660 {
00661   HAL(initiator_select_dep_target, pnd, ndm, nbr, pndiInitiator, pnt, timeout);
00662 }
00663 
00681 int
00682 nfc_initiator_poll_dep_target(struct nfc_device *pnd,
00683                               const nfc_dep_mode ndm, const nfc_baud_rate nbr,
00684                               const nfc_dep_info *pndiInitiator,
00685                               nfc_target *pnt,
00686                               const int timeout)
00687 {
00688   const int period = 300;
00689   int remaining_time = timeout;
00690   int res;
00691   int result = 0;
00692   bool bInfiniteSelect = pnd->bInfiniteSelect;
00693   if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, true)) < 0)
00694     return res;
00695   while (remaining_time > 0) {
00696     if ((res = nfc_initiator_select_dep_target(pnd, ndm, nbr, pndiInitiator, pnt, period)) < 0) {
00697       if (res != NFC_ETIMEOUT) {
00698         result = res;
00699         goto end;
00700       }
00701     }
00702     if (res == 1) {
00703       result = res;
00704       goto end;
00705     }
00706     remaining_time -= period;
00707   }
00708 end:
00709   if (! bInfiniteSelect) {
00710     if ((res = nfc_device_set_property_bool(pnd, NP_INFINITE_SELECT, false)) < 0) {
00711       return res;
00712     }
00713   }
00714   return result;
00715 }
00716 
00729 int
00730 nfc_initiator_deselect_target(nfc_device *pnd)
00731 {
00732   HAL(initiator_deselect_target, pnd);
00733 }
00734 
00763 int
00764 nfc_initiator_transceive_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, uint8_t *pbtRx,
00765                                const size_t szRx, int timeout)
00766 {
00767   HAL(initiator_transceive_bytes, pnd, pbtTx, szTx, pbtRx, szRx, timeout)
00768 }
00769 
00806 int
00807 nfc_initiator_transceive_bits(nfc_device *pnd,
00808                               const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
00809                               uint8_t *pbtRx, const size_t szRx,
00810                               uint8_t *pbtRxPar)
00811 {
00812   (void)szRx;
00813   HAL(initiator_transceive_bits, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar);
00814 }
00815 
00842 int
00843 nfc_initiator_transceive_bytes_timed(nfc_device *pnd,
00844                                      const uint8_t *pbtTx, const size_t szTx,
00845                                      uint8_t *pbtRx, const size_t szRx,
00846                                      uint32_t *cycles)
00847 {
00848   HAL(initiator_transceive_bytes_timed, pnd, pbtTx, szTx, pbtRx, szRx, cycles);
00849 }
00850 
00861 int
00862 nfc_initiator_target_is_present(nfc_device *pnd, const nfc_target *pnt)
00863 {
00864   HAL(initiator_target_is_present, pnd, pnt);
00865 }
00866 
00888 int
00889 nfc_initiator_transceive_bits_timed(nfc_device *pnd,
00890                                     const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar,
00891                                     uint8_t *pbtRx, const size_t szRx,
00892                                     uint8_t *pbtRxPar,
00893                                     uint32_t *cycles)
00894 {
00895   (void)szRx;
00896   HAL(initiator_transceive_bits_timed, pnd, pbtTx, szTxBits, pbtTxPar, pbtRx, pbtRxPar, cycles);
00897 }
00898 
00932 int
00933 nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
00934 {
00935   int res = 0;
00936   // Disallow invalid frame
00937   if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_INVALID_FRAMES, false)) < 0)
00938     return res;
00939   // Disallow multiple frames
00940   if ((res = nfc_device_set_property_bool(pnd, NP_ACCEPT_MULTIPLE_FRAMES, false)) < 0)
00941     return res;
00942   // Make sure we reset the CRC and parity to chip handling.
00943   if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, true)) < 0)
00944     return res;
00945   if ((res = nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true)) < 0)
00946     return res;
00947   // Activate auto ISO14443-4 switching by default
00948   if ((res = nfc_device_set_property_bool(pnd, NP_AUTO_ISO14443_4, true)) < 0)
00949     return res;
00950   // Activate "easy framing" feature by default
00951   if ((res = nfc_device_set_property_bool(pnd, NP_EASY_FRAMING, true)) < 0)
00952     return res;
00953   // Deactivate the CRYPTO1 cipher, it may could cause problems when still active
00954   if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_CRYPTO1, false)) < 0)
00955     return res;
00956   // Drop explicitely the field
00957   if ((res = nfc_device_set_property_bool(pnd, NP_ACTIVATE_FIELD, false)) < 0)
00958     return res;
00959 
00960   HAL(target_init, pnd, pnt, pbtRx, szRx, timeout);
00961 }
00962 
00973 int
00974 nfc_idle(nfc_device *pnd)
00975 {
00976   HAL(idle, pnd);
00977 }
00978 
00990 int
00991 nfc_abort_command(nfc_device *pnd)
00992 {
00993   HAL(abort_command, pnd);
00994 }
00995 
01011 int
01012 nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
01013 {
01014   HAL(target_send_bytes, pnd, pbtTx, szTx, timeout);
01015 }
01016 
01031 int
01032 nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
01033 {
01034   HAL(target_receive_bytes, pnd, pbtRx, szRx, timeout);
01035 }
01036 
01048 int
01049 nfc_target_send_bits(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTxBits, const uint8_t *pbtTxPar)
01050 {
01051   HAL(target_send_bits, pnd, pbtTx, szTxBits, pbtTxPar);
01052 }
01053 
01070 int
01071 nfc_target_receive_bits(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, uint8_t *pbtRxPar)
01072 {
01073   HAL(target_receive_bits, pnd, pbtRx, szRx, pbtRxPar);
01074 }
01075 
01076 static struct sErrorMessage {
01077   int     iErrorCode;
01078   const char *pcErrorMsg;
01079 } sErrorMessages[] = {
01080   /* Chip-level errors (internal errors, RF errors, etc.) */
01081   { NFC_SUCCESS, "Success" },
01082   { NFC_EIO, "Input / Output Error" },
01083   { NFC_EINVARG, "Invalid argument(s)" },
01084   { NFC_EDEVNOTSUPP, "Not Supported by Device" },
01085   { NFC_ENOTSUCHDEV, "No Such Device" },
01086   { NFC_EOVFLOW, "Buffer Overflow" },
01087   { NFC_ETIMEOUT, "Timeout" },
01088   { NFC_EOPABORTED, "Operation Aborted" },
01089   { NFC_ENOTIMPL, "Not (yet) Implemented" },
01090   { NFC_ETGRELEASED, "Target Released" },
01091   { NFC_EMFCAUTHFAIL, "Mifare Authentication Failed" },
01092   { NFC_ERFTRANS, "RF Transmission Error" },
01093   { NFC_ECHIP, "Device's Internal Chip Error" },
01094 };
01095 
01102 const char *
01103 nfc_strerror(const nfc_device *pnd)
01104 {
01105   const char *pcRes = "Unknown error";
01106   size_t  i;
01107   for (i = 0; i < (sizeof(sErrorMessages) / sizeof(struct sErrorMessage)); i++) {
01108     if (sErrorMessages[i].iErrorCode == pnd->last_error) {
01109       pcRes = sErrorMessages[i].pcErrorMsg;
01110       break;
01111     }
01112   }
01113 
01114   return pcRes;
01115 }
01116 
01125 int
01126 nfc_strerror_r(const nfc_device *pnd, char *pcStrErrBuf, size_t szBufLen)
01127 {
01128   return (snprintf(pcStrErrBuf, szBufLen, "%s", nfc_strerror(pnd)) < 0) ? -1 : 0;
01129 }
01130 
01137 void
01138 nfc_perror(const nfc_device *pnd, const char *pcString)
01139 {
01140   fprintf(stderr, "%s: %s\n", pcString, nfc_strerror(pnd));
01141 }
01142 
01149 int
01150 nfc_device_get_last_error(const nfc_device *pnd)
01151 {
01152   return pnd->last_error;
01153 }
01154 
01155 /* Special data accessors */
01156 
01163 const char *
01164 nfc_device_get_name(nfc_device *pnd)
01165 {
01166   return pnd->name;
01167 }
01168 
01175 const char *
01176 nfc_device_get_connstring(nfc_device *pnd)
01177 {
01178   return pnd->connstring;
01179 }
01180 
01189 int
01190 nfc_device_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, const nfc_modulation_type **const supported_mt)
01191 {
01192   HAL(get_supported_modulation, pnd, mode, supported_mt);
01193 }
01194 
01203 int
01204 nfc_device_get_supported_baud_rate(nfc_device *pnd, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br)
01205 {
01206   HAL(get_supported_baud_rate, pnd, nmt, supported_br);
01207 }
01208 
01209 /* Misc. functions */
01210 
01217 const char *
01218 nfc_version(void)
01219 {
01220 #ifdef GIT_REVISION
01221   return GIT_REVISION;
01222 #else
01223   return PACKAGE_VERSION;
01224 #endif // GIT_REVISION
01225 }
01226 
01232 void
01233 nfc_free(void *p)
01234 {
01235   free(p);
01236 }
01237 
01246 int
01247 nfc_device_get_information_about(nfc_device *pnd, char **buf)
01248 {
01249   HAL(device_get_information_about, pnd, buf);
01250 }
01251 
01257 const char *
01258 str_nfc_baud_rate(const nfc_baud_rate nbr)
01259 {
01260   switch (nbr) {
01261     case NBR_UNDEFINED:
01262       return "undefined baud rate";
01263       break;
01264     case NBR_106:
01265       return "106 kbps";
01266       break;
01267     case NBR_212:
01268       return "212 kbps";
01269       break;
01270     case NBR_424:
01271       return "424 kbps";
01272       break;
01273     case NBR_847:
01274       return "847 kbps";
01275       break;
01276   }
01277   // Should never go there..
01278   return "";
01279 }
01280 
01286 const char *
01287 str_nfc_modulation_type(const nfc_modulation_type nmt)
01288 {
01289   switch (nmt) {
01290     case NMT_ISO14443A:
01291       return "ISO/IEC 14443A";
01292       break;
01293     case NMT_ISO14443B:
01294       return "ISO/IEC 14443-4B";
01295       break;
01296     case NMT_ISO14443BI:
01297       return "ISO/IEC 14443-4B'";
01298       break;
01299     case NMT_ISO14443B2CT:
01300       return "ISO/IEC 14443-2B ASK CTx";
01301       break;
01302     case NMT_ISO14443B2SR:
01303       return "ISO/IEC 14443-2B ST SRx";
01304       break;
01305     case NMT_FELICA:
01306       return "FeliCa";
01307       break;
01308     case NMT_JEWEL:
01309       return "Innovision Jewel";
01310       break;
01311     case NMT_DEP:
01312       return "D.E.P.";
01313       break;
01314   }
01315   // Should never go there..
01316   return "";
01317 }
01318 
01327 int
01328 str_nfc_target(char **buf, const nfc_target *pnt, bool verbose)
01329 {
01330   *buf = malloc(4096);
01331   if (! *buf)
01332     return NFC_ESOFT;
01333   (*buf)[0] = '\0';
01334   snprint_nfc_target(*buf, 4096, pnt, verbose);
01335   return strlen(*buf);
01336 }