forked from nfc-tools/libnfc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move nfc_emulate_target() from examples to the library.
- Loading branch information
Showing
6 changed files
with
148 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
|
||
nfcinclude_HEADERS = nfc.h nfc-types.h | ||
nfcinclude_HEADERS = \ | ||
nfc.h \ | ||
nfc-emulation.h \ | ||
nfc-types.h | ||
nfcincludedir = $(includedir)/nfc | ||
|
||
EXTRA_DIST = CMakeLists.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/*- | ||
* Public platform independent Near Field Communication (NFC) library | ||
* | ||
* Copyright (C) 2011, Romuald Conty, Romain Tartière | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or (at your | ||
* option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* 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/> | ||
*/ | ||
|
||
#ifndef __NFC_EMULATION_H__ | ||
#define __NFC_EMULATION_H__ | ||
|
||
#include <sys/types.h> | ||
|
||
struct nfc_emulator; | ||
struct nfc_emulation_state_machine; | ||
|
||
|
||
struct nfc_emulator { | ||
nfc_target_t *target; | ||
struct nfc_emulation_state_machine *state_machine; | ||
void *user_data; | ||
}; | ||
|
||
struct nfc_emulation_state_machine { | ||
int (*io)(struct nfc_emulator *emulator, const byte_t *data_in, const size_t data_in_len, byte_t *data_out, const size_t data_out_len); | ||
void *state_machine_data; | ||
}; | ||
|
||
int nfc_emulate_target (nfc_device_t* pnd, struct nfc_emulator *emulator); | ||
|
||
#endif /* __NFC_EMULATION_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/*- | ||
* Public platform independent Near Field Communication (NFC) library | ||
* | ||
* Copyright (C) 2011, Romuald Conty, Romain Tartière | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or (at your | ||
* option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* 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/> | ||
*/ | ||
|
||
#ifndef __ISO7816_H__ | ||
#define __ISO7816_H__ | ||
|
||
#define ISO7816_C_APDU_COMMAND_HEADER_LEN 4 | ||
#define ISO7816_SHORT_APDU_MAX_DATA_LEN 256 | ||
#define ISO7816_SHORT_C_APDU_MAX_OVERHEAD 2 | ||
#define ISO7816_SHORT_R_APDU_RESPONSE_TRAILER_LEN 2 | ||
|
||
#define ISO7816_SHORT_C_APDU_MAX_LEN (ISO7816_C_APDU_COMMAND_HEADER_LEN + ISO7816_SHORT_APDU_MAX_DATA_LEN + ISO7816_SHORT_C_APDU_MAX_OVERHEAD) | ||
#define ISO7816_SHORT_R_APDU_MAX_LEN (ISO7816_SHORT_APDU_MAX_DATA_LEN + ISO7816_SHORT_R_APDU_RESPONSE_TRAILER_LEN) | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/*- | ||
* Public platform independent Near Field Communication (NFC) library | ||
* | ||
* Copyright (C) 2011, Romuald Conty, Romain Tartière | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or (at your | ||
* option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* 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/> | ||
*/ | ||
|
||
#include <nfc/nfc.h> | ||
#include <nfc/nfc-emulation.h> | ||
|
||
#include "iso7816.h" | ||
|
||
int | ||
nfc_emulate_target (nfc_device_t* pnd, struct nfc_emulator *emulator) | ||
{ | ||
byte_t abtRx[ISO7816_SHORT_R_APDU_MAX_LEN]; | ||
size_t szRx = sizeof(abtRx); | ||
byte_t abtTx[ISO7816_SHORT_C_APDU_MAX_LEN]; | ||
int res = 0; | ||
|
||
if (!nfc_target_init (pnd, emulator->target, abtRx, &szRx)) { | ||
return -1; | ||
} | ||
|
||
while (res >= 0) { | ||
res = emulator->state_machine->io (emulator, abtRx, szRx, abtTx, sizeof (abtTx)); | ||
if (res > 0) { | ||
if (!nfc_target_send_bytes(pnd, abtTx, res)) { | ||
return -1; | ||
} | ||
} | ||
if (res >= 0) { | ||
if (!nfc_target_receive_bytes(pnd, abtRx, &szRx)) { | ||
return -1; | ||
} | ||
} | ||
} | ||
return 0; | ||
} |