Skip to content

Commit

Permalink
update to use libnfc's trunk
Browse files Browse the repository at this point in the history
  • Loading branch information
Audrey Diacre committed Jan 26, 2012
1 parent f95ec4b commit 620e4ff
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/debug/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void debug_print(int level, const char *file, int line, const char *format, ...)
}
}

void _debug_print_tag(const nfc_target_t* tag)
void _debug_print_tag(const nfc_target* tag)
{
print_nfc_target(*tag, false);
}
3 changes: 1 addition & 2 deletions src/debug/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <config.h>
#endif

#define ERR(x,...) debug_print(-1, __FILE__, __LINE__, x, ## __VA_ARGS__ )
#define INFO(x,...) debug_print(0, __FILE__, __LINE__, x, ## __VA_ARGS__ )

#ifndef DEBUG
Expand Down Expand Up @@ -76,7 +75,7 @@ DEBUG_EXTERN void debug_print(int level, const char *file, int line, const char

#ifdef DEBUG
#include "../types.h"
void _debug_print_tag(const nfc_target_t* tag);
void _debug_print_tag(const nfc_target* tag);
#define debug_print_tag( X ) \
printf("%s:%s:%d: ", "\033[34mDEBUG", __FILE__, __LINE__); \
_debug_print_tag( X ); \
Expand Down
97 changes: 26 additions & 71 deletions src/debug/nfc-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include "nfc-utils.h"

static const byte_t OddParity[256] = {
static const uint8_t OddParity[256] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
Expand All @@ -52,14 +52,14 @@ static const byte_t OddParity[256] = {
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
};

byte_t
oddparity (const byte_t bt)
uint8_t
oddparity (const uint8_t bt)
{
return OddParity[bt];
}

void
oddparity_bytes_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar)
oddparity_bytes_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar)
{
size_t szByteNr;
// Calculate the parity bits for the command
Expand All @@ -69,7 +69,7 @@ oddparity_bytes_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar)
}

void
print_hex (const byte_t * pbtData, const size_t szBytes)
print_hex (const uint8_t *pbtData, const size_t szBytes)
{
size_t szPos;

Expand All @@ -80,7 +80,7 @@ print_hex (const byte_t * pbtData, const size_t szBytes)
}

void
print_hex_bits (const byte_t * pbtData, const size_t szBits)
print_hex_bits (const uint8_t *pbtData, const size_t szBits)
{
uint8_t uRemainder;
size_t szPos;
Expand All @@ -102,7 +102,7 @@ print_hex_bits (const byte_t * pbtData, const size_t szBits)
}

void
print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDataPar)
print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar)
{
uint8_t uRemainder;
size_t szPos;
Expand Down Expand Up @@ -133,7 +133,7 @@ print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDa
#define SAK_ISO18092_COMPLIANT 0x40

void
print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose)
{
printf (" ATQA (SENS_RES): ");
print_hex (nai.abtAtqa, 2);
Expand Down Expand Up @@ -202,7 +202,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)

size_t offset = 1;
if (nai.abtAts[0] & 0x10) { // TA(1) present
byte_t TA = nai.abtAts[offset];
uint8_t TA = nai.abtAts[offset];
offset++;
printf ("* Bit Rate Capability:\n");
if (TA == 0) {
Expand Down Expand Up @@ -234,7 +234,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if (nai.abtAts[0] & 0x20) { // TB(1) present
byte_t TB= nai.abtAts[offset];
uint8_t TB= nai.abtAts[offset];
offset++;
printf ("* Frame Waiting Time: %.4g ms\n",256.0*16.0*(1<<((TB & 0xf0) >> 4))/13560.0);
if ((TB & 0x0f) == 0) {
Expand All @@ -244,7 +244,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if (nai.abtAts[0] & 0x40) { // TC(1) present
byte_t TC = nai.abtAts[offset];
uint8_t TC = nai.abtAts[offset];
offset++;
if (TC & 0x1) {
printf("* Node ADdress supported\n");
Expand All @@ -260,20 +260,20 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
if (nai.szAtsLen > offset) {
printf ("* Historical bytes Tk: " );
print_hex (nai.abtAts + offset, (nai.szAtsLen - offset));
byte_t CIB = nai.abtAts[offset];
uint8_t CIB = nai.abtAts[offset];
offset++;
if (CIB != 0x00 && CIB != 0x10 && (CIB & 0xf0) != 0x80) {
printf(" * Proprietary format\n");
if (CIB == 0xc1) {
printf(" * Tag byte: Mifare or virtual cards of various types\n");
byte_t L = nai.abtAts[offset];
uint8_t L = nai.abtAts[offset];
offset++;
if (L != (nai.szAtsLen - offset)) {
printf(" * Warning: Type Identification Coding length (%i)", L);
printf(" not matching Tk length (%zi)\n", (nai.szAtsLen - offset));
}
if ((nai.szAtsLen - offset - 2) > 0) { // Omit 2 CRC bytes
byte_t CTC = nai.abtAts[offset];
uint8_t CTC = nai.abtAts[offset];
offset++;
printf(" * Chip Type: ");
switch (CTC & 0xf0) {
Expand Down Expand Up @@ -316,7 +316,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes
byte_t CVC = nai.abtAts[offset];
uint8_t CVC = nai.abtAts[offset];
offset++;
printf(" * Chip Status: ");
switch (CVC & 0xf0) {
Expand Down Expand Up @@ -350,7 +350,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}
}
if ((nai.szAtsLen - offset) > 0) { // Omit 2 CRC bytes
byte_t VCS = nai.abtAts[offset];
uint8_t VCS = nai.abtAts[offset];
offset++;
printf(" * Specifics (Virtual Card Selection):\n");
if ((VCS & 0x09) == 0x00) {
Expand Down Expand Up @@ -530,7 +530,7 @@ print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose)
}

void
print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose)
print_nfc_felica_info (const nfc_felica_info nfi, bool verbose)
{
(void) verbose;
printf (" ID (NFCID2): ");
Expand All @@ -542,7 +542,7 @@ print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose)
}

void
print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose)
print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose)
{
(void) verbose;
printf (" ATQA (SENS_RES): ");
Expand All @@ -555,7 +555,7 @@ print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose)
#define PI_NAD_SUPPORTED 0x01
#define PI_CID_SUPPORTED 0x02
void
print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose)
print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose)
{
const int iMaxFrameSizes[] = { 16, 24, 32, 40, 48, 64, 96, 128, 256 };
printf (" PUPI: ");
Expand Down Expand Up @@ -610,7 +610,7 @@ print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose)
}

void
print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose)
print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose)
{
printf (" DIV: ");
print_hex (nii.abtDIV, 4);
Expand All @@ -634,15 +634,15 @@ print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose)
}

void
print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose)
print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose)
{
(void) verbose;
printf (" UID: ");
print_hex (nsi.abtUID, 8);
}

void
print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose)
print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose)
{
(void) verbose;
uint32_t uid;
Expand All @@ -655,7 +655,7 @@ print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose)
}

void
print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose)
print_nfc_dep_info (const nfc_dep_info ndi, bool verbose)
{
(void) verbose;
printf (" NFCID3: ");
Expand All @@ -670,53 +670,8 @@ print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose)
}
}

/**
* @brief Tries to parse arguments to find device descriptions.
* @return Returns the list of found device descriptions.
*/
nfc_device_desc_t *
parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose)
{
nfc_device_desc_t *pndd = 0;
int arg;
*szFound = 0;

// Get commandline options
for (arg = 1; arg < argc; arg++) {

if (0 == strcmp (argv[arg], "--device")) {
// FIXME: this device selection by command line options is terrible & does not support USB/PCSC drivers
if (argc > arg + 1) {
char buffer[256];

pndd = malloc (sizeof (nfc_device_desc_t));

strncpy (buffer, argv[++arg], 256);

// Driver.
pndd->pcDriver = (char *) malloc (256);
strcpy (pndd->pcDriver, strtok (buffer, ":"));

// Port.
strcpy (pndd->acPort, strtok (NULL, ":"));

// Speed.
sscanf (strtok (NULL, ":"), "%u", &pndd->uiSpeed);

*szFound = 1;
} else {
errx (1, "usage: %s [--device driver:port:speed]", argv[0]);
}
}
if ((0 == strcmp (argv[arg], "-v")) || (0 == strcmp (argv[arg], "--verbose"))) {
*verbose = true;
}
}
return pndd;
}

const char *
str_nfc_baud_rate (const nfc_baud_rate_t nbr)
str_nfc_baud_rate (const nfc_baud_rate nbr)
{
switch(nbr) {
case NBR_UNDEFINED:
Expand All @@ -739,7 +694,7 @@ str_nfc_baud_rate (const nfc_baud_rate_t nbr)
}

void
print_nfc_target (const nfc_target_t nt, bool verbose)
print_nfc_target (const nfc_target nt, bool verbose)
{
switch(nt.nm.nmt) {
case NMT_ISO14443A:
Expand Down Expand Up @@ -771,7 +726,7 @@ print_nfc_target (const nfc_target_t nt, bool verbose)
print_nfc_iso14443b2ct_info (nt.nti.nci, verbose);
break;
case NMT_DEP:
printf ("D.E.P. (%s) target:\n", str_nfc_baud_rate(nt.nm.nbr));
printf ("D.E.P. (%s, %s) target:\n", str_nfc_baud_rate(nt.nm.nbr), (nt.nti.ndi.ndm == NDM_ACTIVE)? "active mode" : "passive mode");
print_nfc_dep_info (nt.nti.ndi, verbose);
break;
}
Expand Down
31 changes: 15 additions & 16 deletions src/debug/nfc-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,23 @@
# define ERR(...) warnx ("ERROR: " __VA_ARGS__ )
#endif

byte_t oddparity (const byte_t bt);
void oddparity_byte_ts (const byte_t * pbtData, const size_t szLen, byte_t * pbtPar);
uint8_t oddparity (const uint8_t bt);
void oddparity_uint8_ts (const uint8_t *pbtData, const size_t szLen, uint8_t *pbtPar);

void print_hex (const byte_t * pbtData, const size_t szLen);
void print_hex_bits (const byte_t * pbtData, const size_t szBits);
void print_hex_par (const byte_t * pbtData, const size_t szBits, const byte_t * pbtDataPar);
void print_hex (const uint8_t *pbtData, const size_t szLen);
void print_hex_bits (const uint8_t *pbtData, const size_t szBits);
void print_hex_par (const uint8_t *pbtData, const size_t szBits, const uint8_t *pbtDataPar);

void print_nfc_iso14443a_info (const nfc_iso14443a_info_t nai, bool verbose);
void print_nfc_iso14443b_info (const nfc_iso14443b_info_t nbi, bool verbose);
void print_nfc_iso14443bi_info (const nfc_iso14443bi_info_t nii, bool verbose);
void print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info_t nsi, bool verbose);
void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info_t nci, bool verbose);
void print_nfc_felica_info (const nfc_felica_info_t nfi, bool verbose);
void print_nfc_jewel_info (const nfc_jewel_info_t nji, bool verbose);
void print_nfc_dep_info (const nfc_dep_info_t ndi, bool verbose);
void print_nfc_iso14443a_info (const nfc_iso14443a_info nai, bool verbose);
void print_nfc_iso14443b_info (const nfc_iso14443b_info nbi, bool verbose);
void print_nfc_iso14443bi_info (const nfc_iso14443bi_info nii, bool verbose);
void print_nfc_iso14443b2sr_info (const nfc_iso14443b2sr_info nsi, bool verbose);
void print_nfc_iso14443b2ct_info (const nfc_iso14443b2ct_info nci, bool verbose);
void print_nfc_felica_info (const nfc_felica_info nfi, bool verbose);
void print_nfc_jewel_info (const nfc_jewel_info nji, bool verbose);
void print_nfc_dep_info (const nfc_dep_info ndi, bool verbose);
const char * str_nfc_baud_rate (const nfc_baud_rate nbr);

void print_nfc_target (const nfc_target_t nt, bool verbose);

nfc_device_desc_t *parse_args (int argc, const char *argv[], size_t * szFound, bool * verbose);
void print_nfc_target (const nfc_target nt, bool verbose);

#endif
5 changes: 3 additions & 2 deletions src/modules/nem_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
/* Nfc Event Module, aka NEM, common defines */
#include "../nfcconf/nfcconf.h"
#include "../debug/debug.h"
#include "../debug/nfc-utils.h"
#include "../types.h"

typedef void (*module_init_fct_t)(nfcconf_context*, nfcconf_block*);
typedef int (*module_event_handler_fct_t)( const nfc_device_t*, const nfc_target_t*, const nem_event_t );
typedef void (*module_init_fct)(nfcconf_context*, nfcconf_block*);
typedef int (*module_event_handler_fct)( const nfc_device*, const nfc_target*, const nem_event_t );

#endif /* __NEM_COMMON__ */

8 changes: 4 additions & 4 deletions src/modules/nem_dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ static nfcconf_block* _nem_dbus_config_block;
static char * _tag_uid = NULL;

void
tag_get_uid(nfc_device_t* nfc_device, const nfc_target_t* tag, char **dest) {
tag_get_uid(nfc_device* nfc_device, const nfc_target* tag, char **dest) {
DBG("tag_get_uid(%08x, %08x, %08x)", nfc_device, tag, dest);

nfc_target_t target;
nfc_target target;
debug_print_tag(tag);
/// @TODO We don't need to reselect tag to get his UID: nfc_target_t contains this data.
/// @TODO We don't need to reselect tag to get his UID: nfc_target contains this data.
// Poll for a ISO14443A (MIFARE) tag
if ( nfc_initiator_select_passive_target ( nfc_device, tag->nm, tag->nti.nai.abtUid, tag->nti.nai.szUidLen, &target ) ) {
*dest = malloc(target.nti.nai.szUidLen*sizeof(char));
Expand Down Expand Up @@ -213,7 +213,7 @@ nem_dbus_init( nfcconf_context *module_context, nfcconf_block* module_block ) {
}

int
nem_dbus_event_handler(const nfc_device_t* nfc_device, const nfc_target_t* tag, const nem_event_t event) {
nem_dbus_event_handler(const nfc_device* nfc_device, const nfc_target* tag, const nem_event_t event) {
switch (event) {
case EVENT_TAG_INSERTED:
// action = "tag_insert";
Expand Down
2 changes: 1 addition & 1 deletion src/modules/nem_dbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
#define NFC_DBUS_INTERFACE "org.freedevice.NFC"

void nem_dbus_init(nfcconf_context *module_context, nfcconf_block* module_block);
int nem_dbus_event_handler(const nfc_device_t* nfc_device, const nfc_target_t* tag, const nem_event_t event);
int nem_dbus_event_handler(const nfc_device* nfc_device, const nfc_target* tag, const nem_event_t event);

#endif /* __NEM_DBUS__ */
Loading

0 comments on commit 620e4ff

Please sign in to comment.