nfcip-*: code clean up, enhance output to understand what happens.
This commit is contained in:
parent
bf1f9c68d3
commit
13d0bb7b0f
2 changed files with 66 additions and 44 deletions
|
@ -19,15 +19,15 @@
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file nfcip-initiator.c
|
* @file nfcip-initiator.c
|
||||||
* @brief
|
* @brief Turns the NFC device into a D.E.P. initiator (see NFCIP-1)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
# include "config.h"
|
# include "config.h"
|
||||||
#endif // HAVE_CONFIG_H
|
#endif // HAVE_CONFIG_H
|
||||||
|
|
||||||
#include <err.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <nfc/nfc.h>
|
#include <nfc/nfc.h>
|
||||||
|
|
||||||
|
@ -38,31 +38,41 @@ main (int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
nfc_device_t *pnd;
|
nfc_device_t *pnd;
|
||||||
nfc_target_info_t ti;
|
nfc_target_info_t ti;
|
||||||
byte_t abtRecv[MAX_FRAME_LEN];
|
byte_t abtRx[MAX_FRAME_LEN];
|
||||||
size_t szRecvBits;
|
size_t szRx;
|
||||||
byte_t send[] = "Hello World!";
|
byte_t send[] = "Hello World!";
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
errx (1, "usage: %s", argv[0]);
|
printf ("Usage: %s\n", argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
pnd = nfc_connect (NULL);
|
pnd = nfc_connect (NULL);
|
||||||
if (!pnd || !nfc_initiator_init (pnd)
|
if (!pnd) {
|
||||||
|| !nfc_initiator_select_dep_target (pnd, NM_PASSIVE_DEP, NULL, 0, NULL, 0, NULL, 0, &ti)) {
|
printf("Unable to connect to NFC device.\n");
|
||||||
printf ("unable to connect, initialize, or select the target\n");
|
return EXIT_FAILURE;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
printf ("Sending : %s\n", send);
|
if (!nfc_initiator_init (pnd)) {
|
||||||
if (!nfc_initiator_transceive_bytes (pnd, send, strlen ((char *) send), abtRecv, &szRecvBits)) {
|
nfc_perror(pnd, "nfc_initiator_init");
|
||||||
printf ("unable to send data\n");
|
return EXIT_FAILURE;
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
abtRecv[szRecvBits] = 0;
|
if(!nfc_initiator_select_dep_target (pnd, NM_PASSIVE_DEP, NULL, 0, NULL, 0, NULL, 0, &ti)) {
|
||||||
printf ("Received: %s\n", abtRecv);
|
nfc_perror(pnd, "nfc_initiator_select_dep_target");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf ("Sending: %s\n", send);
|
||||||
|
if (!nfc_initiator_transceive_bytes (pnd, send, strlen ((char *) send), abtRx, &szRx)) {
|
||||||
|
nfc_perror(pnd, "nfc_initiator_transceive_bytes");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
abtRx[szRx] = 0;
|
||||||
|
printf ("Received: %s\n", abtRx);
|
||||||
|
|
||||||
nfc_initiator_deselect_target (pnd);
|
nfc_initiator_deselect_target (pnd);
|
||||||
nfc_disconnect (pnd);
|
nfc_disconnect (pnd);
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,20 +15,20 @@
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU Lesser General Public License
|
* 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/>
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file nfcip-target.c
|
* @file nfcip-target.c
|
||||||
* @brief
|
* @brief Turns the NFC device into a D.E.P. target (see NFCIP-1)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
# include "config.h"
|
# include "config.h"
|
||||||
#endif // HAVE_CONFIG_H
|
#endif // HAVE_CONFIG_H
|
||||||
|
|
||||||
#include <err.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include <nfc/nfc.h>
|
#include <nfc/nfc.h>
|
||||||
|
|
||||||
#define MAX_FRAME_LEN 264
|
#define MAX_FRAME_LEN 264
|
||||||
|
@ -36,27 +36,30 @@
|
||||||
int
|
int
|
||||||
main (int argc, const char *argv[])
|
main (int argc, const char *argv[])
|
||||||
{
|
{
|
||||||
byte_t abtRecv[MAX_FRAME_LEN];
|
byte_t abtRx[MAX_FRAME_LEN];
|
||||||
size_t szRecvBits;
|
size_t szRx;
|
||||||
size_t szDeviceFound;
|
size_t szDeviceFound;
|
||||||
byte_t send[] = "Hello Mars!";
|
byte_t abtTx[] = "Hello Mars!";
|
||||||
nfc_device_t *pnd;
|
nfc_device_t *pnd;
|
||||||
#define MAX_DEVICE_COUNT 2
|
#define MAX_DEVICE_COUNT 2
|
||||||
nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT];
|
nfc_device_desc_t pnddDevices[MAX_DEVICE_COUNT];
|
||||||
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
|
nfc_list_devices (pnddDevices, MAX_DEVICE_COUNT, &szDeviceFound);
|
||||||
//# Little hack to allow using nfcip-initiator & nfcip-target from
|
// Little hack to allow using nfcip-initiator & nfcip-target from
|
||||||
//# the same machine: if there is more than one readers connected
|
// the same machine: if there is more than one readers connected
|
||||||
//# nfcip-target will connect to the second reader
|
// nfcip-target will connect to the second reader
|
||||||
//# (we hope they're always detected in the same order)
|
// (we hope they're always detected in the same order)
|
||||||
if (szDeviceFound == 1) {
|
if (szDeviceFound == 1) {
|
||||||
pnd = nfc_connect (&(pnddDevices[0]));
|
pnd = nfc_connect (&(pnddDevices[0]));
|
||||||
}
|
} else if (szDeviceFound > 1) {
|
||||||
else if (szDeviceFound > 1) {
|
pnd = nfc_connect (&(pnddDevices[1]));
|
||||||
pnd = nfc_connect (&(pnddDevices[1]));
|
} else {
|
||||||
|
printf("No device found.");
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
errx (1, "usage: %s", argv[0]);
|
printf ("Usage: %s\n", argv[0]);
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
// 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.
|
||||||
|
@ -69,24 +72,33 @@ main (int argc, const char *argv[])
|
||||||
.nti.nai.szAtsLen = 0,
|
.nti.nai.szAtsLen = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!pnd || !nfc_target_init (pnd, NTM_DEP, nt, abtRecv, &szRecvBits)) {
|
if (!pnd) {
|
||||||
printf ("unable to connect or initialize\n");
|
printf("Unable to connect to NFC device.\n");
|
||||||
return 1;
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
printf ("Connected to NFC device: %s\n", pnd->acName);
|
||||||
|
|
||||||
|
printf ("Waiting for initiator request...\n");
|
||||||
|
if(!nfc_target_init (pnd, NTM_DEP, nt, abtRx, &szRx)) {
|
||||||
|
nfc_perror(pnd, "nfc_target_init");
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!nfc_target_receive_bytes (pnd, abtRecv, &szRecvBits)) {
|
printf("Initiator request received. Waiting for data...\n");
|
||||||
printf ("unable to receive data\n");
|
if (!nfc_target_receive_bytes (pnd, abtRx, &szRx)) {
|
||||||
return 1;
|
nfc_perror(pnd, "nfc_target_receive_bytes");
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
abtRecv[szRecvBits] = 0;
|
abtRx[szRx] = '\0';
|
||||||
printf ("Received: %s\n", abtRecv);
|
printf ("Received: %s\n", abtRx);
|
||||||
printf ("Sending : %s\n", send);
|
|
||||||
|
|
||||||
if (!nfc_target_send_bytes (pnd, send, 11)) {
|
printf ("Sending: %s\n", abtTx);
|
||||||
printf ("unable to send data\n");
|
if (!nfc_target_send_bytes (pnd, abtTx, sizeof(abtTx))) {
|
||||||
return 1;
|
nfc_perror(pnd, "nfc_target_send_bytes");
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
printf("Data sent.\n");
|
||||||
|
|
||||||
nfc_disconnect (pnd);
|
nfc_disconnect (pnd);
|
||||||
return 0;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue