libnfc  1.7.1
nfc-emulation.c
Go to the documentation of this file.
1 /*-
2  * Free/Libre Near Field Communication (NFC) library
3  *
4  * Libnfc historical contributors:
5  * Copyright (C) 2009 Roel Verdult
6  * Copyright (C) 2009-2013 Romuald Conty
7  * Copyright (C) 2010-2012 Romain Tartière
8  * Copyright (C) 2010-2013 Philippe Teuwen
9  * Copyright (C) 2012-2013 Ludovic Rousseau
10  * See AUTHORS file for a more comprehensive list of contributors.
11  * Additional contributors of this file:
12  *
13  * This program is free software: you can redistribute it and/or modify it
14  * under the terms of the GNU Lesser General Public License as published by the
15  * Free Software Foundation, either version 3 of the License, or (at your
16  * option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but WITHOUT
19  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21  * more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>
25  */
26 
32 #include <nfc/nfc.h>
33 #include <nfc/nfc-emulation.h>
34 
35 #include "iso7816.h"
36 
47 int
48 nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator, const int timeout)
49 {
50  uint8_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN];
51  uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN];
52 
53  int res;
54  if ((res = nfc_target_init(pnd, emulator->target, abtRx, sizeof(abtRx), timeout)) < 0) {
55  return res;
56  }
57 
58  size_t szRx = res;
59  int io_res = res;
60  while (io_res >= 0) {
61  io_res = emulator->state_machine->io(emulator, abtRx, szRx, abtTx, sizeof(abtTx));
62  if (io_res > 0) {
63  if ((res = nfc_target_send_bytes(pnd, abtTx, io_res, timeout)) < 0) {
64  return res;
65  }
66  }
67  if (io_res >= 0) {
68  if ((res = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), timeout)) < 0) {
69  return res;
70  }
71  szRx = res;
72  }
73  }
74  return io_res;
75 }
76 
nfc_device
NFC device information.
Definition: nfc-internal.h:190
nfc_target_init
int nfc_target_init(nfc_device *pnd, nfc_target *pnt, uint8_t *pbtRx, const size_t szRx, int timeout)
Initialize NFC device as an emulated tag.
Definition: nfc.c:933
nfc_emulator
NFC emulator structure.
Definition: nfc-emulation.h:49
nfc-emulation.h
Provide a small API to ease emulation in libnfc.
nfc_target_receive_bytes
int nfc_target_receive_bytes(nfc_device *pnd, uint8_t *pbtRx, const size_t szRx, int timeout)
Receive bytes and APDU frames.
Definition: nfc.c:1032
nfc_target_send_bytes
int nfc_target_send_bytes(nfc_device *pnd, const uint8_t *pbtTx, const size_t szTx, int timeout)
Send bytes and APDU frames.
Definition: nfc.c:1012
nfc_emulate_target
int nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator, const int timeout)
Emulate a target.
Definition: nfc-emulation.c:48
nfc.h
libnfc interface