libnfc  1.7.0-rc7
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  * Additional contributors of this file:
11  *
12  * This program is free software: you can redistribute it and/or modify it
13  * under the terms of the GNU Lesser General Public License as published by the
14  * Free Software Foundation, either version 3 of the License, or (at your
15  * option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20  * more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>
24  */
25 
31 #include <nfc/nfc.h>
32 #include <nfc/nfc-emulation.h>
33 
34 #include "iso7816.h"
35 
46 int
47 nfc_emulate_target(nfc_device *pnd, struct nfc_emulator *emulator, const int timeout)
48 {
49  uint8_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN];
50  uint8_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN];
51 
52  int res;
53  if ((res = nfc_target_init(pnd, emulator->target, abtRx, sizeof(abtRx), timeout)) < 0) {
54  return res;
55  }
56 
57  size_t szRx = res;
58  int io_res = res;
59  while (io_res >= 0) {
60  io_res = emulator->state_machine->io(emulator, abtRx, szRx, abtTx, sizeof(abtTx));
61  if (io_res > 0) {
62  if ((res = nfc_target_send_bytes(pnd, abtTx, io_res, timeout)) < 0) {
63  return res;
64  }
65  }
66  if (io_res >= 0) {
67  if ((res = nfc_target_receive_bytes(pnd, abtRx, sizeof(abtRx), timeout)) < 0) {
68  return res;
69  }
70  szRx = res;
71  }
72  }
73  return (io_res < 0) ? io_res : 0;
74 }
75