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
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
2010-04-09 18:21:25 +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 ,
* this list of conditions and the following disclaimer .
* 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 .
2010-04-09 18:21:25 +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
*
*/
/**
* @ file nfc - poll
* @ 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-03-04 20:59:49 +01:00
static nfc_device_t * pnd = NULL ;
void stop_polling ( int sig )
{
( void ) sig ;
if ( pnd )
nfc_abort_command ( pnd ) ;
else
exit ( EXIT_FAILURE ) ;
}
2010-04-09 18:21:25 +02:00
2010-06-07 11:51:31 +02:00
int
main ( int argc , const char * argv [ ] )
2010-04-09 18:21:25 +02:00
{
2010-09-07 19:51:03 +02:00
size_t szFound ;
size_t i ;
2010-10-19 14:50:52 +02:00
bool verbose = false ;
2010-04-09 18:21:25 +02:00
nfc_device_desc_t * pnddDevices ;
2011-03-04 20:59:49 +01:00
signal ( SIGINT , stop_polling ) ;
2010-10-19 14:50:52 +02:00
pnddDevices = parse_args ( argc , argv , & szFound , & verbose ) ;
2010-04-09 18:21:25 +02:00
// Display libnfc version
2010-06-07 11:51:31 +02:00
const char * acLibnfcVersion = nfc_version ( ) ;
2010-04-10 00:03:52 +02:00
if ( argc > 1 ) {
errx ( 1 , " usage: %s " , argv [ 0 ] ) ;
}
2011-05-25 21:53:45 +02:00
printf ( " %s uses libnfc %s \n " , argv [ 0 ] , acLibnfcVersion ) ;
2010-04-09 18:21:25 +02:00
2010-10-19 14:50:52 +02:00
if ( szFound = = 0 ) {
if ( ! ( pnddDevices = malloc ( MAX_DEVICE_COUNT * sizeof ( * pnddDevices ) ) ) ) {
fprintf ( stderr , " malloc() failed \n " ) ;
2011-03-05 02:45:49 +01:00
exit ( EXIT_FAILURE ) ;
2010-10-19 14:50:52 +02:00
}
2010-04-09 18:21:25 +02:00
}
nfc_list_devices ( pnddDevices , MAX_DEVICE_COUNT , & szFound ) ;
2010-06-07 11:51:31 +02:00
if ( szFound = = 0 ) {
2010-10-26 16:27:17 +02:00
printf ( " No NFC device found. \n " ) ;
2010-04-09 18:21:25 +02:00
}
2010-06-07 11:51:31 +02:00
for ( i = 0 ; i < szFound ; i + + ) {
2011-10-03 13:19:08 +02:00
const uint8_t uiPollNr = 20 ;
const uint8_t uiPeriod = 2 ;
2010-10-13 19:43:23 +02:00
const nfc_modulation_t nmModulations [ 5 ] = {
{ . 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 ;
2010-08-10 21:50:29 +02:00
2011-10-03 13:19:08 +02:00
nfc_target_t nt ;
2010-09-07 19:51:03 +02:00
bool res ;
2010-08-10 21:50:29 +02:00
pnd = nfc_connect ( & ( pnddDevices [ i ] ) ) ;
2010-04-09 18:21:25 +02:00
2010-06-07 11:51:31 +02:00
if ( pnd = = NULL ) {
ERR ( " %s " , " Unable to connect to NFC device. " ) ;
2011-03-05 02:45:49 +01:00
exit ( EXIT_FAILURE ) ;
2010-04-09 18:21:25 +02:00
}
2010-06-07 11:51:31 +02:00
nfc_initiator_init ( pnd ) ;
2010-04-09 18:21:25 +02:00
2010-06-07 11:51:31 +02:00
printf ( " Connected to NFC reader: %s \n " , pnd - > acName ) ;
2011-10-03 13:19:08 +02:00
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 ) ;
res = nfc_initiator_poll_target ( pnd , nmModulations , szModulations , uiPollNr , uiPeriod , & nt ) ;
2010-06-07 11:51:31 +02:00
if ( res ) {
2011-10-03 13:19:08 +02:00
print_nfc_target ( nt , verbose ) ;
2010-04-09 18:21:25 +02:00
} else {
2011-10-03 13:19:08 +02:00
if ( pnd - > iLastError ) {
nfc_perror ( pnd , " nfc_initiator_poll_targets " ) ;
nfc_disconnect ( pnd ) ;
exit ( EXIT_FAILURE ) ;
} else {
printf ( " No target found. \n " ) ;
}
2010-04-09 18:21:25 +02:00
}
2010-06-07 11:51:31 +02:00
nfc_disconnect ( pnd ) ;
}
2010-04-09 18:21:25 +02:00
free ( pnddDevices ) ;
2011-03-05 02:45:49 +01:00
exit ( EXIT_SUCCESS ) ;
2010-04-09 18:21:25 +02:00
}