2010-01-15 10:18:11 +00:00
|
|
|
/*-
|
2010-11-17 14:27:11 +00:00
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2009-10-12 14:52:26 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2009, Roel Verdult
|
2010-10-14 13:31:36 +00:00
|
|
|
* Copyright (C) 2010, Romuald Conty
|
2009-10-12 14:52:26 +00:00
|
|
|
*
|
2010-11-17 14:27:11 +00: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.
|
2009-10-12 14:52:26 +00:00
|
|
|
*
|
2010-11-17 14:27:11 +00:00
|
|
|
* Note that this license only applies on the examples, NFC library itself is under LGPL
|
2009-10-12 14:52:26 +00:00
|
|
|
*
|
2010-01-15 10:18:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-10-08 20:36:13 +00:00
|
|
|
* @file nfc-dep-target.c
|
2010-10-08 18:15:00 +00:00
|
|
|
* @brief Turns the NFC device into a D.E.P. target (see NFCIP-1)
|
2009-10-12 14:52:26 +00:00
|
|
|
*/
|
|
|
|
|
2010-01-15 10:18:11 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2010-09-07 17:51:03 +00:00
|
|
|
# include "config.h"
|
2010-01-15 10:18:11 +00:00
|
|
|
#endif // HAVE_CONFIG_H
|
|
|
|
|
2009-09-03 13:47:26 +00:00
|
|
|
#include <stdio.h>
|
2010-10-08 18:15:00 +00:00
|
|
|
#include <stdlib.h>
|
2011-04-27 15:10:14 +00:00
|
|
|
#include <signal.h>
|
2010-10-08 18:15:00 +00:00
|
|
|
|
2009-12-01 14:23:00 +00:00
|
|
|
#include <nfc/nfc.h>
|
2009-09-03 13:47:26 +00:00
|
|
|
|
2011-09-30 11:33:31 +00:00
|
|
|
#include "utils/nfc-utils.h"
|
2010-10-08 21:54:59 +00:00
|
|
|
|
2009-11-10 09:47:59 +00:00
|
|
|
#define MAX_FRAME_LEN 264
|
|
|
|
|
2011-11-23 15:55:40 +00:00
|
|
|
static nfc_device *pnd;
|
2011-04-27 15:10:14 +00:00
|
|
|
|
|
|
|
void stop_dep_communication (int sig)
|
|
|
|
{
|
|
|
|
(void) sig;
|
|
|
|
if (pnd)
|
|
|
|
nfc_abort_command (pnd);
|
|
|
|
else
|
|
|
|
exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2010-09-07 17:51:03 +00:00
|
|
|
int
|
|
|
|
main (int argc, const char *argv[])
|
2009-09-03 13:47:26 +00:00
|
|
|
{
|
2010-10-08 18:15:00 +00:00
|
|
|
byte_t abtRx[MAX_FRAME_LEN];
|
2011-03-02 15:02:30 +00:00
|
|
|
size_t szRx = sizeof(abtRx);
|
2010-09-28 15:24:05 +00:00
|
|
|
size_t szDeviceFound;
|
2010-10-08 18:15:00 +00:00
|
|
|
byte_t abtTx[] = "Hello Mars!";
|
2010-09-28 15:24:05 +00:00
|
|
|
#define MAX_DEVICE_COUNT 2
|
2011-10-17 13:03:56 +00:00
|
|
|
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
|
|
|
nfc_list_devices (connstrings, MAX_DEVICE_COUNT, &szDeviceFound);
|
2010-10-08 20:36:13 +00:00
|
|
|
// Little hack to allow using nfc-dep-initiator & nfc-dep-target from
|
2010-10-08 18:15:00 +00:00
|
|
|
// the same machine: if there is more than one readers connected
|
2010-10-08 20:36:13 +00:00
|
|
|
// nfc-dep-target will connect to the second reader
|
2010-10-08 18:15:00 +00:00
|
|
|
// (we hope they're always detected in the same order)
|
2010-09-28 15:24:05 +00:00
|
|
|
if (szDeviceFound == 1) {
|
2011-10-17 13:03:56 +00:00
|
|
|
pnd = nfc_connect (connstrings[0]);
|
2010-10-08 18:15:00 +00:00
|
|
|
} else if (szDeviceFound > 1) {
|
2011-10-17 13:03:56 +00:00
|
|
|
pnd = nfc_connect (connstrings[1]);
|
2010-10-08 18:15:00 +00:00
|
|
|
} else {
|
|
|
|
printf("No device found.");
|
|
|
|
return EXIT_FAILURE;
|
2010-09-28 15:24:05 +00:00
|
|
|
}
|
2009-09-03 13:47:26 +00:00
|
|
|
|
2010-04-07 15:08:04 +00:00
|
|
|
if (argc > 1) {
|
2010-10-08 18:15:00 +00:00
|
|
|
printf ("Usage: %s\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
2010-04-07 15:08:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 15:55:40 +00:00
|
|
|
nfc_target nt = {
|
2011-02-28 09:47:31 +00:00
|
|
|
.nm = {
|
|
|
|
.nmt = NMT_DEP,
|
|
|
|
.nbr = NBR_UNDEFINED
|
|
|
|
},
|
|
|
|
.nti = {
|
|
|
|
.ndi = {
|
|
|
|
.abtNFCID3 = { 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xff, 0x00, 0x00 },
|
|
|
|
.szGB = 4,
|
|
|
|
.abtGB = { 0x12, 0x34, 0x56, 0x78 },
|
2011-06-26 14:43:11 +00:00
|
|
|
.ndm = NDM_UNDEFINED,
|
2011-02-28 09:47:31 +00:00
|
|
|
/* These bytes are not used by nfc_target_init: the chip will provide them automatically to the initiator */
|
|
|
|
.btDID = 0x00,
|
|
|
|
.btBS = 0x00,
|
|
|
|
.btBR = 0x00,
|
|
|
|
.btTO = 0x00,
|
|
|
|
.btPP = 0x01,
|
|
|
|
},
|
|
|
|
},
|
2010-10-04 12:46:03 +00:00
|
|
|
};
|
|
|
|
|
2010-10-08 18:15:00 +00:00
|
|
|
if (!pnd) {
|
|
|
|
printf("Unable to connect to NFC device.\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
printf ("Connected to NFC device: %s\n", pnd->acName);
|
|
|
|
|
2011-04-27 15:10:14 +00:00
|
|
|
signal (SIGINT, stop_dep_communication);
|
|
|
|
|
2010-10-14 13:31:36 +00:00
|
|
|
printf ("NFC device will now act as: ");
|
2010-10-19 12:50:52 +00:00
|
|
|
print_nfc_target (nt, false);
|
2010-10-14 13:31:36 +00:00
|
|
|
|
2010-10-08 18:15:00 +00:00
|
|
|
printf ("Waiting for initiator request...\n");
|
2010-10-15 14:32:10 +00:00
|
|
|
if(!nfc_target_init (pnd, &nt, abtRx, &szRx)) {
|
2010-10-08 18:15:00 +00:00
|
|
|
nfc_perror(pnd, "nfc_target_init");
|
2011-05-05 09:17:38 +00:00
|
|
|
goto error;
|
2009-09-03 13:47:26 +00:00
|
|
|
}
|
|
|
|
|
2010-10-08 18:15:00 +00:00
|
|
|
printf("Initiator request received. Waiting for data...\n");
|
2011-09-22 13:03:47 +00:00
|
|
|
if (!nfc_target_receive_bytes (pnd, abtRx, &szRx, NULL)) {
|
2010-10-08 18:15:00 +00:00
|
|
|
nfc_perror(pnd, "nfc_target_receive_bytes");
|
2011-05-05 09:17:38 +00:00
|
|
|
goto error;
|
2009-09-03 13:47:26 +00:00
|
|
|
}
|
2010-10-08 18:15:00 +00:00
|
|
|
abtRx[szRx] = '\0';
|
|
|
|
printf ("Received: %s\n", abtRx);
|
2009-09-03 13:47:26 +00:00
|
|
|
|
2010-10-08 18:15:00 +00:00
|
|
|
printf ("Sending: %s\n", abtTx);
|
2011-09-22 13:03:47 +00:00
|
|
|
if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx), NULL)) {
|
2010-10-08 18:15:00 +00:00
|
|
|
nfc_perror(pnd, "nfc_target_send_bytes");
|
2011-05-05 09:17:38 +00:00
|
|
|
goto error;
|
2009-09-03 13:47:26 +00:00
|
|
|
}
|
2010-10-08 18:15:00 +00:00
|
|
|
printf("Data sent.\n");
|
2009-09-03 13:47:26 +00:00
|
|
|
|
2011-05-05 09:17:38 +00:00
|
|
|
error:
|
2010-09-07 17:51:03 +00:00
|
|
|
nfc_disconnect (pnd);
|
2010-10-08 18:15:00 +00:00
|
|
|
return EXIT_SUCCESS;
|
2009-09-03 13:47:26 +00:00
|
|
|
}
|