NFC library implementation. More...
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <nfc/nfc.h>
#include "chips.h"
#include "drivers.h"
#include <nfc/nfc-messages.h>
Go to the source code of this file.
Functions | |
void | nfc_list_devices (nfc_device_desc_t pnddDevices[], size_t szDevices, size_t *pszDeviceFound) |
Probe for discoverable supported devices (ie. only available for some drivers). | |
nfc_device_t * | nfc_connect (nfc_device_desc_t *pndd) |
Connect to a NFC device. | |
void | nfc_disconnect (nfc_device_t *pnd) |
Disconnect from a NFC device. | |
bool | nfc_configure (nfc_device_t *pnd, const nfc_device_option_t dco, const bool bEnable) |
Configure advanced NFC device settings. | |
bool | nfc_initiator_init (const nfc_device_t *pnd) |
Initialize NFC device as initiator (reader). | |
bool | nfc_initiator_select_dep_target (const nfc_device_t *pnd, const nfc_modulation_t nmInitModulation, const byte_t *pbtPidData, const size_t szPidDataLen, const byte_t *pbtNFCID3i, const size_t szNFCID3iDataLen, const byte_t *pbtGbData, const size_t szGbDataLen, nfc_target_info_t *pnti) |
Select a target and request active or passive mode for DEP (Data Exchange Protocol). | |
bool | nfc_initiator_select_tag (const nfc_device_t *pnd, const nfc_modulation_t nmInitModulation, const byte_t *pbtInitData, const size_t szInitDataLen, nfc_target_info_t *pnti) |
Select a passive or emulated tag. | |
bool | nfc_initiator_deselect_tag (const nfc_device_t *pnd) |
Deselect a selected passive or emulated tag. | |
bool | nfc_initiator_transceive_bits (const nfc_device_t *pnd, const byte_t *pbtTx, const size_t szTxBits, const byte_t *pbtTxPar, byte_t *pbtRx, size_t *pszRxBits, byte_t *pbtRxPar) |
Transceive raw bit-frames. | |
bool | nfc_initiator_transceive_dep_bytes (const nfc_device_t *pnd, const byte_t *pbtTx, const size_t szTxLen, byte_t *pbtRx, size_t *pszRxLen) |
Transceive data. | |
bool | nfc_initiator_transceive_bytes (const nfc_device_t *pnd, const byte_t *pbtTx, const size_t szTxLen, byte_t *pbtRx, size_t *pszRxLen) |
Transceive byte and APDU frames. | |
bool | nfc_initiator_mifare_cmd (const nfc_device_t *pnd, const mifare_cmd mc, const uint8_t ui8Block, mifare_param *pmp) |
Execute a MIFARE Classic Command. | |
bool | nfc_target_init (const nfc_device_t *pnd, byte_t *pbtRx, size_t *pszRxBits) |
Initialize NFC device as an emulated tag. | |
bool | nfc_target_receive_bits (const nfc_device_t *pnd, byte_t *pbtRx, size_t *pszRxBits, byte_t *pbtRxPar) |
Receive bit-frames. | |
bool | nfc_target_receive_dep_bytes (const nfc_device_t *pnd, byte_t *pbtRx, size_t *pszRxLen) |
Receive data. | |
bool | nfc_target_receive_bytes (const nfc_device_t *pnd, byte_t *pbtRx, size_t *pszRxLen) |
Receive bytes and APDU frames. | |
bool | nfc_target_send_bits (const nfc_device_t *pnd, const byte_t *pbtTx, const size_t szTxBits, const byte_t *pbtTxPar) |
Send raw bit-frames. | |
bool | nfc_target_send_bytes (const nfc_device_t *pnd, const byte_t *pbtTx, const size_t szTxLen) |
Send bytes and APDU frames. | |
bool | nfc_target_send_dep_bytes (const nfc_device_t *pnd, const byte_t *pbtTx, const size_t szTxLen) |
Send data. | |
const char * | nfc_device_name (nfc_device_t *pnd) |
Returns the device name. | |
const char * | nfc_version (void) |
Returns the library version. |
NFC library implementation.
Definition in file nfc.c.
bool nfc_configure | ( | nfc_device_t * | pnd, | |
const nfc_device_option_t | dco, | |||
const bool | bEnable | |||
) |
Configure advanced NFC device settings.
pnd | nfc_device_t struct pointer that represent currently used device | |
ndo | nfc_device_option_t struct that contains options to set to device | |
bEnable | boolean |
Configures parameters and registers that control for example timing, modulation, frame and error handling. There are different categories for configuring the PN53X chip features (handle, activate, infinite and accept). These are defined to organize future settings that will become available when they are needed.
nfc_device_t* nfc_connect | ( | nfc_device_desc_t * | pndd | ) |
Connect to a NFC device.
pndd | Device description if specific device is wanted, NULL otherwise |
If pndd is NULL, the first available NFC device is claimed by libnfc. It will automatically search the system using all available drivers to determine a device is free.
If pndd is passed then libnfc will try to claim the right device using information provided by this struct.
When it has successfully claimed a NFC device, memory is allocated to save the device information. It will return a pointer to a nfc_device_t struct. This pointer should be supplied by every next function of libnfc that should perform an action with this device.
const char* nfc_device_name | ( | nfc_device_t * | pnd | ) |
void nfc_disconnect | ( | nfc_device_t * | pnd | ) |
Disconnect from a NFC device.
pnd | nfc_device_t struct pointer that represent currently used device |
Initiator is disconnected and the device, including allocated nfc_device_t struct, is released.
bool nfc_initiator_deselect_tag | ( | const nfc_device_t * | pnd | ) |
Deselect a selected passive or emulated tag.
pnd | nfc_device_t struct pointer that represent currently used device |
After selecting and communicating with a passive tag, this function could be used to deactivate and release the tag. This is very useful when there are multiple tags available in the field. It is possible to use the nfc_initiator_select_tag() function to select the first available tag, test it for the available features and support, deselect it and skip to the next tag until the correct tag is found.
bool nfc_initiator_init | ( | const nfc_device_t * | pnd | ) |
Initialize NFC device as initiator (reader).
pnd | nfc_device_t struct pointer that represent currently used device |
The NFC device is configured to function as RFID reader. After initialization it can be used to communicate to passive RFID tags and active NFC devices. The reader will act as initiator to communicate peer 2 peer (NFCIP) to other active NFC devices.
bool nfc_initiator_mifare_cmd | ( | const nfc_device_t * | pnd, | |
const mifare_cmd | mc, | |||
const uint8_t | ui8Block, | |||
mifare_param * | pmp | |||
) |
Execute a MIFARE Classic Command.
pmp | Some commands need additional information. This information should be supplied in the mifare_param union. |
The specified MIFARE command will be executed on the tag. There are different commands possible, they all require the destination block number.
First an authentication must take place using Key A or B. It requires a 48 bit Key (6 bytes) and the UID. They are both used to initialize the internal cipher-state of the PN53X chip (http://libnfc.org/hardware/pn53x-chip). After a successful authentication it will be possible to execute other commands (e.g. Read/Write). The MIFARE Classic Specification (http://www.nxp.com/acrobat/other/identification/M001053_MF1ICS50_rev5_3.pdf) explains more about this process.
bool nfc_initiator_select_dep_target | ( | const nfc_device_t * | pnd, | |
const nfc_modulation_t | nmInitModulation, | |||
const byte_t * | pbtPidData, | |||
const size_t | szPidDataLen, | |||
const byte_t * | pbtNFCID3i, | |||
const size_t | szNFCID3iDataLen, | |||
const byte_t * | pbtGbData, | |||
const size_t | szGbDataLen, | |||
nfc_target_info_t * | pnti | |||
) |
Select a target and request active or passive mode for DEP (Data Exchange Protocol).
pnd | nfc_device_t struct pointer that represent currently used device | |
im | Desired modulation (NM_ACTIVE_DEP or NM_PASSIVE_DEP for active, respectively passive mode) | |
pbtPidData | passive initiator data, 4 or 5 bytes long, (optional, only for NM_PASSIVE_DEP, can be NULL) | |
pbtNFCID3i | the NFCID3, 10 bytes long, of the initiator (optional, can be NULL) | |
pbtGbData | generic data of the initiator, max 48 bytes long, (optional, can be NULL) |
The NFC device will try to find the available target. The standards (ISO18092 and ECMA-340) describe the modulation that can be used for reader to passive communications.
bool nfc_initiator_select_tag | ( | const nfc_device_t * | pnd, | |
const nfc_modulation_t | nmInitModulation, | |||
const byte_t * | pbtInitData, | |||
const size_t | szInitDataLen, | |||
nfc_target_info_t * | pnti | |||
) |
Select a passive or emulated tag.
pnd | nfc_device_t struct pointer that represent currently used device | |
im | Desired modulation | |
pbtInitData | Optional initiator data used for Felica, ISO14443B, Topaz Polling or for ISO14443A selecting a specific UID. | |
szInitDataLen | Length of initiator data pbtInitData. |
The NFC device will try to find the available passive tags. Some NFC devices are capable to emulate passive tags. The standards (ISO18092 and ECMA-340) describe the modulation that can be used for reader to passive communications. The chip needs to know with what kind of tag it is dealing with, therefore the initial modulation and speed (106, 212 or 424 kbps) should be supplied.
bool nfc_initiator_transceive_bits | ( | const nfc_device_t * | pnd, | |
const byte_t * | pbtTx, | |||
const size_t | szTxBits, | |||
const byte_t * | pbtTxPar, | |||
byte_t * | pbtRx, | |||
size_t * | pszRxBits, | |||
byte_t * | pbtRxPar | |||
) |
Transceive raw bit-frames.
pbtTx | contains a byte array of the frame that needs to be transmitted. | |
szTxBits | contains the length in bits. |
pbtTxPar | parameter contains a byte array of the corresponding parity bits needed to send per byte. |
The NFC reader will transmit low-level messages where only the modulation is handled by the PN53X chip. Construction of the frame (data, CRC and parity) is completely done by libnfc. This can be very useful for testing purposes. Some protocols (e.g. MIFARE Classic) require to violate the ISO14443-A standard by sending incorrect parity and CRC bytes. Using this feature you are able to simulate these frames.
bool nfc_initiator_transceive_bytes | ( | const nfc_device_t * | pnd, | |
const byte_t * | pbtTx, | |||
const size_t | szTxLen, | |||
byte_t * | pbtRx, | |||
size_t * | pszRxLen | |||
) |
Transceive byte and APDU frames.
The reader will transmit the supplied bytes in pbtTx to the target (tag). It waits for the response and stores the received bytes in the pbtRx byte array. The parity bits are handled by the PN53X chip. The CRC can be generated automatically or handled manually. Using this function, frames can be communicated very fast via the NFC reader to the tag. Tests show that on average this way of communicating is much faster than using the regular driver/middle-ware (often supplied by manufacturers).
bool nfc_initiator_transceive_dep_bytes | ( | const nfc_device_t * | pnd, | |
const byte_t * | pbtTx, | |||
const size_t | szTxLen, | |||
byte_t * | pbtRx, | |||
size_t * | pszRxLen | |||
) |
Transceive data.
The reader will transmit the supplied (data) bytes in pbtTx to the target (tag). It waits for the response and stores the received bytes in the pbtRx byte array. The difference between this function and nfc_initiator_transceive_bytes is that here pbtTx and pbtRx contain *only* the data sent and received and not any additional commands, that is all handled internally by the PN53X.
void nfc_list_devices | ( | nfc_device_desc_t | pnddDevices[], | |
size_t | szDevices, | |||
size_t * | pszDeviceFound | |||
) |
Probe for discoverable supported devices (ie. only available for some drivers).
pnddDevices | Array of nfc_device_desc_t previously allocated by the caller. | |
szDevices | size of the pnddDevices array. | |
pszDeviceFound | number of devices found. |
bool nfc_target_init | ( | const nfc_device_t * | pnd, | |
byte_t * | pbtRx, | |||
size_t * | pszRxBits | |||
) |
Initialize NFC device as an emulated tag.
This functionality allows the NFC device to act as an emulated tag. There seems to be quite some options available for this feature. Not all of the PN53X modulations are tested and documented at the moment. At the moment it could best be seen as a preliminary functionality.
bool nfc_target_receive_bits | ( | const nfc_device_t * | pnd, | |
byte_t * | pbtRx, | |||
size_t * | pszRxBits, | |||
byte_t * | pbtRxPar | |||
) |
Receive bit-frames.
This function makes it possible to receive (raw) bit-frames. It returns all the messages that are stored in the FIFO buffer of the PN53X chip. It does not require to send any frame and thereby could be used to snoop frames that are transmitted by a nearby reader. Check out the NDO_ACCEPT_MULTIPLE_FRAMES configuration option to avoid losing transmitted frames.
bool nfc_target_receive_bytes | ( | const nfc_device_t * | pnd, | |
byte_t * | pbtRx, | |||
size_t * | pszRxLen | |||
) |
bool nfc_target_receive_dep_bytes | ( | const nfc_device_t * | pnd, | |
byte_t * | pbtRx, | |||
size_t * | pszRxLen | |||
) |
Receive data.
The main receive function that returns the received data from a nearby reader. The difference between this function and nfc_target_receive_bytes is that here pbtRx contains *only* the data received and not any additional commands, that is all handled internally by the PN53X.
bool nfc_target_send_bits | ( | const nfc_device_t * | pnd, | |
const byte_t * | pbtTx, | |||
const size_t | szTxBits, | |||
const byte_t * | pbtTxPar | |||
) |
bool nfc_target_send_bytes | ( | const nfc_device_t * | pnd, | |
const byte_t * | pbtTx, | |||
const size_t | szTxLen | |||
) |
bool nfc_target_send_dep_bytes | ( | const nfc_device_t * | pnd, | |
const byte_t * | pbtTx, | |||
const size_t | szTxLen | |||
) |
Send data.
To communicate data to the reader, this function could be used. The difference between this function and nfc_target_send_bytes is that here pbtTx contains *only* the data sent and not any additional commands, that is all handled internally by the PN53X.