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
|
2010-10-14 15:31:36 +02:00
|
|
|
* Copyright (C) 2010, Romuald Conty
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-10-08 22:36:13 +02:00
|
|
|
* @file nfc-dep-target.c
|
2010-10-08 20:15:00 +02:00
|
|
|
* @brief Turns the NFC device into a D.E.P. target (see NFCIP-1)
|
2009-10-12 16:52:26 +02:00
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
2009-09-03 15:47:26 +02:00
|
|
|
#include <stdio.h>
|
2010-10-08 20:15:00 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2010-10-08 23:54:59 +02:00
|
|
|
#include "nfc-utils.h"
|
|
|
|
|
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-10-08 20:15:00 +02:00
|
|
|
byte_t abtRx[MAX_FRAME_LEN];
|
|
|
|
size_t szRx;
|
2010-09-28 17:24:05 +02:00
|
|
|
size_t szDeviceFound;
|
2010-10-08 20:15:00 +02:00
|
|
|
byte_t abtTx[] = "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-10-08 22:36:13 +02:00
|
|
|
// Little hack to allow using nfc-dep-initiator & nfc-dep-target from
|
2010-10-08 20:15:00 +02:00
|
|
|
// the same machine: if there is more than one readers connected
|
2010-10-08 22:36:13 +02:00
|
|
|
// nfc-dep-target will connect to the second reader
|
2010-10-08 20:15:00 +02:00
|
|
|
// (we hope they're always detected in the same order)
|
2010-09-28 17:24:05 +02:00
|
|
|
if (szDeviceFound == 1) {
|
2010-10-08 20:15:00 +02:00
|
|
|
pnd = nfc_connect (&(pnddDevices[0]));
|
|
|
|
} else if (szDeviceFound > 1) {
|
|
|
|
pnd = nfc_connect (&(pnddDevices[1]));
|
|
|
|
} else {
|
|
|
|
printf("No device found.");
|
|
|
|
return EXIT_FAILURE;
|
2010-09-28 17:24:05 +02:00
|
|
|
}
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2010-04-07 17:08:04 +02:00
|
|
|
if (argc > 1) {
|
2010-10-08 20:15:00 +02:00
|
|
|
printf ("Usage: %s\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
2010-04-07 17:08:04 +02:00
|
|
|
}
|
|
|
|
|
2010-10-14 18:27:50 +02:00
|
|
|
nfc_target_t nt = {
|
2010-10-13 21:17:51 +02:00
|
|
|
.nm.nmt = NMT_DEP,
|
2010-10-14 18:27:50 +02:00
|
|
|
.nm.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init
|
2010-10-08 23:54:59 +02:00
|
|
|
.nti.ndi.abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
|
|
|
|
.nti.ndi.szGB = 4,
|
|
|
|
.nti.ndi.abtGB = { 0x12, 0x34, 0x56, 0x78 },
|
|
|
|
/* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
|
|
|
|
.nti.ndi.btDID = 0x00,
|
|
|
|
.nti.ndi.btBS = 0x00,
|
|
|
|
.nti.ndi.btBR = 0x00,
|
|
|
|
.nti.ndi.btTO = 0x00,
|
|
|
|
.nti.ndi.btPP = 0x01,
|
2010-10-04 14:46:03 +02:00
|
|
|
};
|
|
|
|
|
2010-10-08 20:15:00 +02:00
|
|
|
if (!pnd) {
|
|
|
|
printf("Unable to connect to NFC device.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
printf ("Connected to NFC device: %s\n", pnd->acName);
|
|
|
|
|
2010-10-14 15:31:36 +02:00
|
|
|
printf ("NFC device will now act as: ");
|
2010-10-19 14:50:52 +02:00
|
|
|
print_nfc_target (nt, false);
|
2010-10-14 15:31:36 +02:00
|
|
|
|
2010-10-08 20:15:00 +02:00
|
|
|
printf ("Waiting for initiator request...\n");
|
2010-10-15 16:32:10 +02:00
|
|
|
if(!nfc_target_init (pnd, &nt, abtRx, &szRx)) {
|
2010-10-08 20:15:00 +02:00
|
|
|
nfc_perror(pnd, "nfc_target_init");
|
|
|
|
return EXIT_FAILURE;
|
2009-09-03 15:47:26 +02:00
|
|
|
}
|
|
|
|
|
2010-10-08 20:15:00 +02:00
|
|
|
printf("Initiator request received. Waiting for data...\n");
|
|
|
|
if (!nfc_target_receive_bytes (pnd, abtRx, &szRx)) {
|
|
|
|
nfc_perror(pnd, "nfc_target_receive_bytes");
|
|
|
|
return EXIT_FAILURE;
|
2009-09-03 15:47:26 +02:00
|
|
|
}
|
2010-10-08 20:15:00 +02:00
|
|
|
abtRx[szRx] = '\0';
|
|
|
|
printf ("Received: %s\n", abtRx);
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2010-10-08 20:15:00 +02:00
|
|
|
printf ("Sending: %s\n", abtTx);
|
|
|
|
if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx))) {
|
|
|
|
nfc_perror(pnd, "nfc_target_send_bytes");
|
|
|
|
return EXIT_FAILURE;
|
2009-09-03 15:47:26 +02:00
|
|
|
}
|
2010-10-08 20:15:00 +02:00
|
|
|
printf("Data sent.\n");
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
nfc_disconnect (pnd);
|
2010-10-08 20:15:00 +02:00
|
|
|
return EXIT_SUCCESS;
|
2009-09-03 15:47:26 +02:00
|
|
|
}
|