2010-04-09 18:21:25 +02:00
/*-
2010-11-17 15:27:11 +01:00
* Public platform independent Near Field Communication ( NFC ) library examples
2012-05-29 02:33:22 +02:00
*
2010-04-09 18:21:25 +02:00
* Copyright ( C ) 2010 , Romuald Conty
2011-03-04 20:59:49 +01:00
* Copyright ( C ) 2011 , Romain Tartiere , Romuald Conty
2012-05-29 02:33:22 +02:00
*
2010-11-17 15:27:11 +01:00
* Redistribution and use in source and binary forms , with or without
* modification , are permitted provided that the following conditions are met :
* 1 ) Redistributions of source code must retain the above copyright notice ,
2012-05-29 02:33:22 +02:00
* this list of conditions and the following disclaimer .
2010-11-17 15:27:11 +01:00
* 2 ) Redistributions in binary form must reproduce the above copyright
* notice , this list of conditions and the following disclaimer in the
* documentation and / or other materials provided with the distribution .
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS " AS IS "
* AND ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED . IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR
* CONSEQUENTIAL DAMAGES ( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES ; LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS
* INTERRUPTION ) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY , WHETHER IN
* CONTRACT , STRICT LIABILITY , OR TORT ( INCLUDING NEGLIGENCE OR OTHERWISE )
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE .
2012-05-29 02:33:22 +02:00
*
2010-11-17 15:27:11 +01:00
* Note that this license only applies on the examples , NFC library itself is under LGPL
2010-04-09 18:21:25 +02:00
*
*/
/**
2012-01-31 10:49:43 +01:00
* @ file nfc - poll . c
2010-04-09 18:21:25 +02:00
* @ brief Polling example
*/
# ifdef HAVE_CONFIG_H
2010-06-07 11:51:31 +02:00
# include "config.h"
2010-04-09 18:21:25 +02:00
# endif // HAVE_CONFIG_H
2010-04-10 00:03:52 +02:00
# include <err.h>
2011-03-04 20:59:49 +01:00
# include <signal.h>
2010-04-09 18:21:25 +02:00
# include <stdio.h>
# include <stddef.h>
# include <stdlib.h>
# include <string.h>
# include <nfc/nfc.h>
# include <nfc/nfc-types.h>
2011-03-05 09:53:46 +01:00
2011-09-30 13:33:31 +02:00
# include "utils/nfc-utils.h"
2010-04-09 18:21:25 +02:00
# define MAX_DEVICE_COUNT 16
2011-11-23 16:55:40 +01:00
static nfc_device * pnd = NULL ;
2011-03-04 20:59:49 +01:00
2012-05-29 17:54:36 +02:00
static void stop_polling ( int sig )
2011-03-04 20:59:49 +01:00
{
( void ) sig ;
if ( pnd )
2012-05-29 17:54:36 +02:00
nfc_abort_command ( pnd ) ;
2011-03-04 20:59:49 +01:00
else
2012-05-29 17:54:36 +02:00
exit ( EXIT_FAILURE ) ;
2011-03-04 20:59:49 +01:00
}
2010-04-09 18:21:25 +02:00
2012-05-13 14:39:17 +02:00
static void
2012-05-29 17:54:36 +02:00
print_usage ( const char * progname )
2012-01-04 22:29:43 +01:00
{
2012-05-29 17:54:36 +02:00
printf ( " usage: %s [-v] \n " , progname ) ;
printf ( " -v \t verbose display \n " ) ;
2012-01-04 22:29:43 +01:00
}
2010-06-07 11:51:31 +02:00
int
2012-05-29 17:54:36 +02:00
main ( int argc , const char * argv [ ] )
2010-04-09 18:21:25 +02:00
{
2010-10-19 14:50:52 +02:00
bool verbose = false ;
2010-04-09 18:21:25 +02:00
2012-05-29 17:54:36 +02:00
signal ( SIGINT , stop_polling ) ;
2011-03-04 20:59:49 +01:00
2010-04-09 18:21:25 +02:00
// Display libnfc version
2012-05-29 17:54:36 +02:00
const char * acLibnfcVersion = nfc_version ( ) ;
2010-04-10 00:03:52 +02:00
2012-05-29 17:54:36 +02:00
printf ( " %s uses libnfc %s \n " , argv [ 0 ] , acLibnfcVersion ) ;
2012-01-04 22:29:43 +01:00
if ( argc ! = 1 ) {
2012-05-29 17:54:36 +02:00
if ( ( argc = = 2 ) & & ( 0 = = strcmp ( " -v " , argv [ 1 ] ) ) ) {
2012-01-04 22:29:43 +01:00
verbose = true ;
} else {
2012-05-29 17:54:36 +02:00
print_usage ( argv [ 0 ] ) ;
exit ( EXIT_FAILURE ) ;
2012-01-04 22:29:43 +01:00
}
}
2010-04-09 18:21:25 +02:00
2011-10-17 15:03:56 +02:00
const uint8_t uiPollNr = 20 ;
const uint8_t uiPeriod = 2 ;
2011-11-23 16:55:40 +01:00
const nfc_modulation nmModulations [ 5 ] = {
2011-10-17 15:03:56 +02:00
{ . nmt = NMT_ISO14443A , . nbr = NBR_106 } ,
{ . nmt = NMT_ISO14443B , . nbr = NBR_106 } ,
{ . nmt = NMT_FELICA , . nbr = NBR_212 } ,
{ . nmt = NMT_FELICA , . nbr = NBR_424 } ,
{ . nmt = NMT_JEWEL , . nbr = NBR_106 } ,
} ;
const size_t szModulations = 5 ;
2011-11-23 16:55:40 +01:00
nfc_target nt ;
2011-12-20 16:46:35 +01:00
int res = 0 ;
2012-05-29 02:33:22 +02:00
2012-05-29 17:54:36 +02:00
nfc_init ( NULL ) ;
2010-04-09 18:21:25 +02:00
2012-05-29 17:54:36 +02:00
pnd = nfc_open ( NULL , NULL ) ;
2010-04-09 18:21:25 +02:00
2011-10-17 15:03:56 +02:00
if ( pnd = = NULL ) {
2012-05-29 17:54:36 +02:00
ERR ( " %s " , " Unable to open NFC device. " ) ;
exit ( EXIT_FAILURE ) ;
2010-04-09 18:21:25 +02:00
}
2012-05-29 17:54:36 +02:00
if ( nfc_initiator_init ( pnd ) < 0 ) {
nfc_perror ( pnd , " nfc_initiator_init " ) ;
exit ( EXIT_FAILURE ) ;
2012-01-05 11:33:50 +01:00
}
2011-10-17 15:03:56 +02:00
2012-05-29 17:54:36 +02:00
printf ( " NFC reader: %s opened \n " , nfc_device_get_name ( pnd ) ) ;
printf ( " NFC device will poll during %ld ms (%u pollings of %lu ms for %zd modulations) \n " , ( unsigned long ) uiPollNr * szModulations * uiPeriod * 150 , uiPollNr , ( unsigned long ) uiPeriod * 150 , szModulations ) ;
if ( ( res = nfc_initiator_poll_target ( pnd , nmModulations , szModulations , uiPollNr , uiPeriod , & nt ) ) < 0 ) {
nfc_perror ( pnd , " nfc_initiator_poll_target " ) ;
nfc_close ( pnd ) ;
nfc_exit ( NULL ) ;
exit ( EXIT_FAILURE ) ;
2011-12-19 01:23:21 +01:00
}
2011-12-22 12:16:27 +01:00
if ( res > 0 ) {
2012-05-29 17:54:36 +02:00
print_nfc_target ( nt , verbose ) ;
2011-10-17 15:03:56 +02:00
} else {
2012-05-29 17:54:36 +02:00
printf ( " No target found. \n " ) ;
2010-06-07 11:51:31 +02:00
}
2012-05-29 17:54:36 +02:00
nfc_close ( pnd ) ;
nfc_exit ( NULL ) ;
exit ( EXIT_SUCCESS ) ;
2010-04-09 18:21:25 +02:00
}