2010-01-15 11:18:11 +01:00
|
|
|
/*-
|
2010-11-17 15:27:11 +01:00
|
|
|
* Public platform independent Near Field Communication (NFC) library examples
|
2012-05-29 02:33:22 +02:00
|
|
|
*
|
2012-10-21 16:09:16 +02:00
|
|
|
* Copyright (C) 2009 Roel Verdult
|
|
|
|
* Copyright (C) 2010 Romuald Conty
|
2012-05-29 02:33:22 +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,
|
2012-05-29 02:33:22 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2010-11-17 15:27:11 +01:00
|
|
|
* 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.
|
2012-05-29 02:33:22 +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
|
2009-10-12 16:52:26 +02:00
|
|
|
*
|
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>
|
2011-04-27 17:10:14 +02:00
|
|
|
#include <signal.h>
|
2010-10-08 20:15:00 +02:00
|
|
|
|
2009-12-01 15:23:00 +01:00
|
|
|
#include <nfc/nfc.h>
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2011-09-30 13:33:31 +02:00
|
|
|
#include "utils/nfc-utils.h"
|
2010-10-08 23:54:59 +02:00
|
|
|
|
2009-11-10 10:47:59 +01:00
|
|
|
#define MAX_FRAME_LEN 264
|
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
static nfc_device *pnd;
|
2013-03-05 23:58:35 +01:00
|
|
|
static nfc_context *context;
|
2011-04-27 17:10:14 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
static void stop_dep_communication(int sig)
|
2011-04-27 17:10:14 +02:00
|
|
|
{
|
|
|
|
(void) sig;
|
2013-03-05 22:50:37 +01:00
|
|
|
if (pnd != NULL) {
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_abort_command(pnd);
|
2013-03-05 22:50:37 +01:00
|
|
|
} else {
|
|
|
|
nfc_exit(context);
|
2012-05-29 17:54:36 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2013-03-05 22:50:37 +01:00
|
|
|
}
|
2011-04-27 17:10:14 +02:00
|
|
|
}
|
|
|
|
|
2010-09-07 19:51:03 +02:00
|
|
|
int
|
2012-05-29 17:54:36 +02:00
|
|
|
main(int argc, const char *argv[])
|
2009-09-03 15:47:26 +02:00
|
|
|
{
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtRx[MAX_FRAME_LEN];
|
2012-01-09 12:26:57 +01:00
|
|
|
int szRx;
|
2011-11-24 11:54:42 +01:00
|
|
|
uint8_t abtTx[] = "Hello Mars!";
|
2012-12-04 19:29:57 +01:00
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
if (argc > 1) {
|
|
|
|
printf("Usage: %s\n", argv[0]);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_init(&context);
|
2013-03-05 22:24:59 +01:00
|
|
|
if (context == NULL) {
|
|
|
|
ERR("Unable to init libnfc (malloc)");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2012-05-29 17:52:51 +02:00
|
|
|
#define MAX_DEVICE_COUNT 2
|
2011-10-17 15:03:56 +02:00
|
|
|
nfc_connstring connstrings[MAX_DEVICE_COUNT];
|
2012-12-04 19:29:57 +01:00
|
|
|
size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT);
|
2010-10-08 22:36:13 +02:00
|
|
|
// Little hack to allow using nfc-dep-initiator & nfc-dep-target from
|
2012-01-17 16:21:56 +01:00
|
|
|
// the same machine: if there is more than one readers opened
|
|
|
|
// nfc-dep-target will open 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) {
|
2012-12-04 19:29:57 +01:00
|
|
|
pnd = nfc_open(context, connstrings[0]);
|
2010-10-08 20:15:00 +02:00
|
|
|
} else if (szDeviceFound > 1) {
|
2012-12-04 19:29:57 +01:00
|
|
|
pnd = nfc_open(context, connstrings[1]);
|
2010-10-08 20:15:00 +02:00
|
|
|
} else {
|
2011-12-07 15:59:40 +01:00
|
|
|
printf("No device found.\n");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2010-04-07 17:08:04 +02:00
|
|
|
}
|
|
|
|
|
2011-11-23 16:55:40 +01:00
|
|
|
nfc_target nt = {
|
2011-02-28 10:47:31 +01: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 },
|
2012-05-29 17:52:51 +02:00
|
|
|
.ndm = NDM_UNDEFINED,
|
2011-02-28 10:47:31 +01: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 14:46:03 +02:00
|
|
|
};
|
|
|
|
|
2013-03-05 19:44:59 +01:00
|
|
|
if (pnd == NULL) {
|
2012-01-17 16:21:56 +01:00
|
|
|
printf("Unable to open NFC device.\n");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2010-10-08 20:15:00 +02:00
|
|
|
}
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("NFC device: %s opened\n", nfc_device_get_name(pnd));
|
2010-10-08 20:15:00 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
signal(SIGINT, stop_dep_communication);
|
2011-04-27 17:10:14 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("NFC device will now act as: ");
|
2013-03-04 05:40:07 +01:00
|
|
|
print_nfc_target(&nt, false);
|
2010-10-14 15:31:36 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Waiting for initiator request...\n");
|
|
|
|
if ((szRx = nfc_target_init(pnd, &nt, abtRx, sizeof(abtRx), 0)) < 0) {
|
2010-10-08 20:15:00 +02:00
|
|
|
nfc_perror(pnd, "nfc_target_init");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(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");
|
2012-05-29 17:54:36 +02:00
|
|
|
if ((szRx = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), 0)) < 0) {
|
2010-10-08 20:15:00 +02:00
|
|
|
nfc_perror(pnd, "nfc_target_receive_bytes");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(EXIT_FAILURE);
|
2009-09-03 15:47:26 +02:00
|
|
|
}
|
2012-01-09 12:26:57 +01:00
|
|
|
abtRx[(size_t) szRx] = '\0';
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Received: %s\n", abtRx);
|
2009-09-03 15:47:26 +02:00
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
printf("Sending: %s\n", abtTx);
|
|
|
|
if (nfc_target_send_bytes(pnd, abtTx, sizeof(abtTx), 0) < 0) {
|
2010-10-08 20:15:00 +02:00
|
|
|
nfc_perror(pnd, "nfc_target_send_bytes");
|
2013-03-05 19:44:59 +01:00
|
|
|
nfc_close(pnd);
|
|
|
|
nfc_exit(context);
|
|
|
|
exit(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
|
|
|
|
2012-05-29 17:54:36 +02:00
|
|
|
nfc_close(pnd);
|
2012-12-04 19:29:57 +01:00
|
|
|
nfc_exit(context);
|
2013-03-05 19:44:59 +01:00
|
|
|
exit(EXIT_SUCCESS);
|
2009-09-03 15:47:26 +02:00
|
|
|
}
|