2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2009-10-12 16:52:26 +02:00
|
|
|
* Public platform independent Near Field Communication (NFC) library
|
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Roel Verdult
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU Lesser General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2010-01-15 11:18:11 +01:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2009-10-12 16:52:26 +02:00
|
|
|
* @file nfcip-target.c
|
|
|
|
* @brief
|
|
|
|
*/
|
|
|
|
|
2010-01-15 11:18:11 +01:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-09-07 19:51:03 +02:00
|
|
|
# include "config.h"
|
2010-01-15 11:18:11 +01:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2010-04-07 17:08:04 +02:00
|
|
|
#include <err.h>
|
2009-09-03 15:47:26 +02:00
|
|
|
#include <stdio.h>
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2009-11-10 10:47:59 +01:00
|
|
|
#define MAX_FRAME_LEN 264
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
|
|
|
main (int argc, const char *argv[])
|
2009-09-03 15:47:26 +02:00
|
|
|
{
|
2010-09-07 19:51:03 +02:00
|
|
|
byte_t abtRecv[MAX_FRAME_LEN];
|
|
|
|
size_t szRecvBits;
|
2010-09-28 17:24:05 +02:00
|
|
|
size_t szDeviceFound;
|
2010-09-07 19:51:03 +02:00
|
|
|
byte_t send[] = "Hello Mars!";
|
2010-09-28 17:24:05 +02:00
|
|
|
nfc_device_t *pnd;
|
|
|
|
#define MAX_DEVICE_COUNT 2
|
|
|
|
nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT];
|
|
|
|
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
|
2010-09-28 18:18:05 +02:00
|
|
|
//# Little hack to allow using nfcip-initiator & nfcip-target from
|
|
|
|
//# the same machine: if there is more than one readers connected
|
|
|
|
//# nfcip-target will connect to the second reader
|
|
|
|
//# (we hope they're always detected in the same order)
|
2010-09-28 17:24:05 +02:00
|
|
|
if (szDeviceFound == 1) {
|
|
|
|
pnd = nfc_connect (&(pnddDevices[0]));
|
|
|
|
}
|
|
|
|
else if (szDeviceFound > 1) {
|
|
|
|
pnd = nfc_connect (&(pnddDevices[1]));
|
|
|
|
}
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2010-04-07 17:08:04 +02:00
|
|
|
if (argc > 1) {
|
|
|
|
errx (1, "usage: %s", argv[0]);
|
|
|
|
}
|
|
|
|
|
2010-10-04 14:46:03 +02:00
|
|
|
// Note: We have to build a "fake" nfc_target_t in order to do exactly the same that was done before the new nfc_target_init() was introduced.
|
|
|
|
nfc_target_t nt = {
|
|
|
|
.ntt = NTT_GENERIC_PASSIVE_106,
|
|
|
|
.nti.nai.abtAtqa = "\x04\x00",
|
|
|
|
.nti.nai.abtUid = "\xde\xad\xbe\xaf\x62",
|
|
|
|
.nti.nai.btSak = 0x20,
|
|
|
|
.nti.nai.szUidLen = 5,
|
|
|
|
.nti.nai.szAtsLen = 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!pnd || !nfc_target_init (pnd, NTM_DEP, nt, abtRecv, &szRecvBits)) {
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("unable to connect or initialize\n");
|
2009-09-03 15:47:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!nfc_target_receive_bytes (pnd, abtRecv, &szRecvBits)) {
|
|
|
|
printf ("unable to receive data\n");
|
2009-09-03 15:47:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
2009-10-02 11:52:02 +02:00
|
|
|
abtRecv[szRecvBits] = 0;
|
2010-09-07 19:51:03 +02:00
|
|
|
printf ("Received: %s\n", abtRecv);
|
|
|
|
printf ("Sending : %s\n", send);
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
if (!nfc_target_send_bytes (pnd, send, 11)) {
|
|
|
|
printf ("unable to send data\n");
|
2009-09-03 15:47:26 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_disconnect (pnd);
|
2009-09-03 15:47:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|