C/C++ - NFC Bricklet

This is the description of the C/C++ API bindings for the NFC Bricklet. General information and technical specifications for the NFC Bricklet are summarized in its hardware description.

An installation guide for the C/C++ API bindings is part of their general description.

Examples

The example code below is Public Domain (CC0 1.0).

Scan For Tags

Download (example_scan_for_tags.c)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_nfc.h"

#define HOST "localhost"
#define PORT 4223
#define UID "XYZ" // Change XYZ to the UID of your NFC Bricklet

// Callback function for reader state changed callback
void cb_reader_state_changed(uint8_t state, bool idle, void *user_data) {
    NFC *nfc = (NFC *)user_data;

    if(state == NFC_READER_STATE_REQUEST_TAG_ID_READY) {
        uint8_t ret_tag_type;
        uint8_t ret_tag_id_length;
        uint8_t ret_tag_id[32];

        if(nfc_reader_get_tag_id(nfc, &ret_tag_type, ret_tag_id, &ret_tag_id_length) < 0) {
            fprintf(stderr, "Could not get tag ID\n");
            return;
        }

        printf("Found tag of type %d with ID [", ret_tag_type);

        for(uint8_t i = 0; i < ret_tag_id_length; i++) {
            printf("0x%02X", ret_tag_id[i]);

            if(i < ret_tag_id_length - 1) {
                printf(" ");
            }
        }

        printf("]\n");
    }

    if(idle) {
        nfc_reader_request_tag_id(nfc);
    }
}

int main(void) {
    // Create IP connection
    IPConnection ipcon;
    ipcon_create(&ipcon);

    // Create device object
    NFC nfc;
    nfc_create(&nfc, UID, &ipcon);

    // Connect to brickd
    if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
        fprintf(stderr, "Could not connect\n");
        return 1;
    }
    // Don't use device before ipcon is connected

    // Register reader state changed callback to function cb_reader_state_changed
    nfc_register_callback(&nfc,
                          NFC_CALLBACK_READER_STATE_CHANGED,
                          (void (*)(void))cb_reader_state_changed,
                          &nfc);

    // Enable reader mode
    nfc_set_mode(&nfc, NFC_MODE_READER);

    printf("Press key to exit\n");
    getchar();
    nfc_destroy(&nfc);
    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
    return 0;
}

Emulate Ndef

Download (example_emulate_ndef.c)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_nfc.h"

#define HOST "localhost"
#define PORT 4223
#define UID "XYZ" // Change XYZ to the UID of your NFC Bricklet
#define NDEF_URI "www.tinkerforge.com"

// Callback function for cardemu state changed callback
void cb_cardemu_state_changed(uint8_t state, bool idle, void *user_data) {
    NFC *nfc = (NFC *)user_data;

    (void)idle; // avoid unused parameter warning

    if(state == NFC_CARDEMU_STATE_IDLE) {
        uint8_t i = 0;
        uint8_t ndef_record_uri[512];
        const char *ptr_ndef_uri = NDEF_URI;

        memset(ndef_record_uri, 0, 512);

        // Only short records are supported
        ndef_record_uri[0] = 0xD1;
        ndef_record_uri[1] = 0x01;
        ndef_record_uri[2] = strlen(NDEF_URI) + 1;
        ndef_record_uri[3] = 'U';
        ndef_record_uri[4] = 0x04;

        while (*ptr_ndef_uri) {
            ndef_record_uri[5 + i] = *ptr_ndef_uri;
            i++;
            ptr_ndef_uri++;
        }

        nfc_cardemu_write_ndef(nfc, ndef_record_uri, strlen(NDEF_URI) + 5);
        nfc_cardemu_start_discovery(nfc);
    }
    else if(state == NFC_CARDEMU_STATE_DISCOVER_READY) {
        nfc_cardemu_start_transfer(nfc, NFC_CARDEMU_TRANSFER_WRITE);
    }
    else if(state == NFC_CARDEMU_STATE_DISCOVER_ERROR) {
        printf("Discover error\n");
    }
    else if(state == NFC_CARDEMU_STATE_TRANSFER_NDEF_ERROR) {
        printf("Transfer NDEF error\n");
    }
}

int main(void) {
    // Create IP connection
    IPConnection ipcon;
    ipcon_create(&ipcon);

    // Create device object
    NFC nfc;
    nfc_create(&nfc, UID, &ipcon);

    // Connect to brickd
    if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
        fprintf(stderr, "Could not connect\n");
        return 1;
    }
    // Don't use device before ipcon is connected

    // Register cardemu state changed callback to function cb_cardemu_state_changed
    nfc_register_callback(&nfc,
                          NFC_CALLBACK_CARDEMU_STATE_CHANGED,
                          (void (*)(void))cb_cardemu_state_changed,
                          &nfc);

    // Enable cardemu mode
    nfc_set_mode(&nfc, NFC_MODE_CARDEMU);

    printf("Press key to exit\n");
    getchar();
    nfc_destroy(&nfc);
    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
    return 0;
}

Write Read Type2

Download (example_write_read_type2.c)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_nfc.h"

#define HOST "localhost"
#define PORT 4223
#define UID "XYZ" // Change XYZ to the UID of your NFC Bricklet

// Callback function for reader state changed callback
void cb_reader_state_changed(uint8_t state, bool idle, void *user_data) {
    NFC *nfc = (NFC *)user_data;

    (void)idle; // avoid unused parameter warning

    if(state == NFC_READER_STATE_IDLE) {
        nfc_reader_request_tag_id(nfc);
    }
    else if(state == NFC_READER_STATE_REQUEST_TAG_ID_READY) {
        int ret = 0;
        uint8_t ret_tag_type = 0;
        uint8_t ret_tag_id_length = 0;
        uint8_t *ret_tag_id = (uint8_t *)malloc(32);

        ret = nfc_reader_get_tag_id(nfc, &ret_tag_type, ret_tag_id, &ret_tag_id_length);

        if(ret != E_OK) {
            free(ret_tag_id);

            return;
        }

        if(ret_tag_type != NFC_TAG_TYPE_TYPE2) {
            printf("Tag is not type-2\n");
            free(ret_tag_id);

            return;
        }

        printf("Found tag of type %d with ID [0x%X 0x%X 0x%X 0x%X]\n",
               ret_tag_type,
               ret_tag_id[0],
               ret_tag_id[1],
               ret_tag_id[2],
               ret_tag_id[3]);
        free(ret_tag_id);
        nfc_reader_request_page(nfc, 1, 4);
    }
    else if(state == NFC_READER_STATE_REQUEST_TAG_ID_ERROR) {
        printf("Request tag ID error\n");
    }
    else if(state == NFC_READER_STATE_REQUEST_PAGE_READY) {
        int ret = 0;
        uint16_t ret_data_length = 0;
        uint8_t *ret_data = (uint8_t *)malloc(4);

        ret = nfc_reader_read_page(nfc, ret_data, &ret_data_length);

        if(ret != E_OK) {
            free(ret_data);

            return;
        }
        printf("Page read: 0x%X 0x%X 0x%X 0x%X\n",
               ret_data[0],
               ret_data[1],
               ret_data[2],
               ret_data[3]);
        nfc_reader_write_page(nfc, 1, ret_data, ret_data_length);
        free(ret_data);
    }
    else if(state == NFC_READER_STATE_WRITE_PAGE_READY) {
        printf("Write page ready\n");
    }
    else if(state == NFC_READER_STATE_REQUEST_PAGE_ERROR) {
        printf("Request page error\n");
    }
    else if(state == NFC_READER_STATE_WRITE_PAGE_ERROR) {
        printf("Write page error\n");
    }
}

int main(void) {
    // Create IP connection
    IPConnection ipcon;
    ipcon_create(&ipcon);

    // Create device object
    NFC nfc;
    nfc_create(&nfc, UID, &ipcon);

    // Connect to brickd
    if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
        fprintf(stderr, "Could not connect\n");
        return 1;
    }
    // Don't use device before ipcon is connected

    // Register reader state changed callback to function cb_reader_state_changed
    nfc_register_callback(&nfc,
                          NFC_CALLBACK_READER_STATE_CHANGED,
                          (void (*)(void))cb_reader_state_changed,
                          &nfc);

    // Enable reader mode
    nfc_set_mode(&nfc, NFC_MODE_READER);

    printf("Press key to exit\n");
    getchar();
    nfc_destroy(&nfc);
    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
    return 0;
}

API

Most functions of the C/C++ bindings return an error code (e_code). Data returned from the device, when a getter is called, is handled via output parameters. These parameters are labeled with the ret_ prefix.

Possible error codes are:

  • E_OK = 0
  • E_TIMEOUT = -1
  • E_NO_STREAM_SOCKET = -2
  • E_HOSTNAME_INVALID = -3
  • E_NO_CONNECT = -4
  • E_NO_THREAD = -5
  • E_NOT_ADDED = -6 (unused since C/C++ bindings version 2.0.0)
  • E_ALREADY_CONNECTED = -7
  • E_NOT_CONNECTED = -8
  • E_INVALID_PARAMETER = -9
  • E_NOT_SUPPORTED = -10
  • E_UNKNOWN_ERROR_CODE = -11
  • E_STREAM_OUT_OF_SYNC = -12
  • E_INVALID_UID = -13
  • E_NON_ASCII_CHAR_IN_SECRET = -14
  • E_WRONG_DEVICE_TYPE = -15
  • E_DEVICE_REPLACED = -16
  • E_WRONG_RESPONSE_LENGTH = -17

as defined in ip_connection.h.

All functions listed below are thread-safe.

Basic Functions

void nfc_create(NFC *nfc, const char *uid, IPConnection *ipcon)
Parameters:
  • nfc – Type: NFC *
  • uid – Type: const char *
  • ipcon – Type: IPConnection *

Creates the device object nfc with the unique device ID uid and adds it to the IPConnection ipcon:

NFC nfc;
nfc_create(&nfc, "YOUR_DEVICE_UID", &ipcon);

This device object can be used after the IP connection has been connected.

void nfc_destroy(NFC *nfc)
Parameters:
  • nfc – Type: NFC *

Removes the device object nfc from its IPConnection and destroys it. The device object cannot be used anymore afterwards.

int nfc_set_mode(NFC *nfc, uint8_t mode)
Parameters:
  • nfc – Type: NFC *
  • mode – Type: uint8_t, Range: See constants, Default: 0
Returns:
  • e_code – Type: int

Sets the mode. The NFC Bricklet supports four modes:

  • Off
  • Card Emulation (Cardemu): Emulates a tag for other readers
  • Peer to Peer (P2P): Exchange data with other readers
  • Reader: Reads and writes tags
  • Simple: Automatically reads tag IDs

If you change a mode, the Bricklet will reconfigure the hardware for this mode. Therefore, you can only use functions corresponding to the current mode. For example, in Reader mode you can only use Reader functions.

The following constants are available for this function:

For mode:

  • NFC_MODE_OFF = 0
  • NFC_MODE_CARDEMU = 1
  • NFC_MODE_P2P = 2
  • NFC_MODE_READER = 3
  • NFC_MODE_SIMPLE = 4
int nfc_get_mode(NFC *nfc, uint8_t *ret_mode)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_mode – Type: uint8_t, Range: See constants, Default: 0
Returns:
  • e_code – Type: int

Returns the mode as set by nfc_set_mode().

The following constants are available for this function:

For ret_mode:

  • NFC_MODE_OFF = 0
  • NFC_MODE_CARDEMU = 1
  • NFC_MODE_P2P = 2
  • NFC_MODE_READER = 3
  • NFC_MODE_SIMPLE = 4
int nfc_reader_request_tag_id(NFC *nfc)
Parameters:
  • nfc – Type: NFC *
Returns:
  • e_code – Type: int

After you call nfc_reader_request_tag_id() the NFC Bricklet will try to read the tag ID from the tag. After this process is done the state will change. You can either register the NFC_CALLBACK_READER_STATE_CHANGED callback or you can poll nfc_reader_get_state() to find out about the state change.

If the state changes to ReaderRequestTagIDError it means that either there was no tag present or that the tag has an incompatible type. If the state changes to ReaderRequestTagIDReady it means that a compatible tag was found and that the tag ID has been saved. You can now read out the tag ID by calling nfc_reader_get_tag_id().

If two tags are in the proximity of the NFC Bricklet, this function will cycle through the tags. To select a specific tag you have to call nfc_reader_request_tag_id() until the correct tag ID is found.

In case of any ReaderError state the selection is lost and you have to start again by calling nfc_reader_request_tag_id().

int nfc_reader_get_tag_id(NFC *nfc, uint8_t *ret_tag_type, uint8_t *ret_tag_id, uint8_t *ret_tag_id_length)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_tag_type – Type: uint8_t, Range: See constants
  • ret_tag_id – Type: uint8_t *, Range: [0 to 255]
  • ret_tag_id_length – Type: uint8_t
Returns:
  • e_code – Type: int

Returns the tag type and the tag ID. This function can only be called if the NFC Bricklet is currently in one of the ReaderReady states. The returned tag ID is the tag ID that was saved through the last call of nfc_reader_request_tag_id().

To get the tag ID of a tag the approach is as follows:

  1. Call nfc_reader_request_tag_id()
  2. Wait for state to change to ReaderRequestTagIDReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  3. Call nfc_reader_get_tag_id()

The following constants are available for this function:

For ret_tag_type:

  • NFC_TAG_TYPE_MIFARE_CLASSIC = 0
  • NFC_TAG_TYPE_TYPE1 = 1
  • NFC_TAG_TYPE_TYPE2 = 2
  • NFC_TAG_TYPE_TYPE3 = 3
  • NFC_TAG_TYPE_TYPE4 = 4
  • NFC_TAG_TYPE_TYPE5 = 5
int nfc_reader_get_state(NFC *nfc, uint8_t *ret_state, bool *ret_idle)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_state – Type: uint8_t, Range: See constants
  • ret_idle – Type: bool
Returns:
  • e_code – Type: int

Returns the current reader state of the NFC Bricklet.

On startup the Bricklet will be in the ReaderInitialization state. The initialization will only take about 20ms. After that it changes to ReaderIdle.

The Bricklet is also reinitialized if the mode is changed, see nfc_set_mode().

The functions of this Bricklet can be called in the ReaderIdle state and all of the ReaderReady and ReaderError states.

Example: If you call nfc_reader_request_page(), the state will change to ReaderRequestPage until the reading of the page is finished. Then it will change to either ReaderRequestPageReady if it worked or to ReaderRequestPageError if it didn't. If the request worked you can get the page by calling nfc_reader_read_page().

The same approach is used analogously for the other API functions.

The following constants are available for this function:

For ret_state:

  • NFC_READER_STATE_INITIALIZATION = 0
  • NFC_READER_STATE_IDLE = 128
  • NFC_READER_STATE_ERROR = 192
  • NFC_READER_STATE_REQUEST_TAG_ID = 2
  • NFC_READER_STATE_REQUEST_TAG_ID_READY = 130
  • NFC_READER_STATE_REQUEST_TAG_ID_ERROR = 194
  • NFC_READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE = 3
  • NFC_READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE_READY = 131
  • NFC_READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE_ERROR = 195
  • NFC_READER_STATE_WRITE_PAGE = 4
  • NFC_READER_STATE_WRITE_PAGE_READY = 132
  • NFC_READER_STATE_WRITE_PAGE_ERROR = 196
  • NFC_READER_STATE_REQUEST_PAGE = 5
  • NFC_READER_STATE_REQUEST_PAGE_READY = 133
  • NFC_READER_STATE_REQUEST_PAGE_ERROR = 197
  • NFC_READER_STATE_WRITE_NDEF = 6
  • NFC_READER_STATE_WRITE_NDEF_READY = 134
  • NFC_READER_STATE_WRITE_NDEF_ERROR = 198
  • NFC_READER_STATE_REQUEST_NDEF = 7
  • NFC_READER_STATE_REQUEST_NDEF_READY = 135
  • NFC_READER_STATE_REQUEST_NDEF_ERROR = 199
int nfc_reader_write_ndef(NFC *nfc, uint8_t *ndef, uint16_t ndef_length)
Parameters:
  • nfc – Type: NFC *
  • ndef – Type: uint8_t *, Range: [0 to 255]
  • ndef_length – Type: uint16_t
Returns:
  • e_code – Type: int

Writes NDEF formated data.

This function currently supports NFC Forum Type 2, 4, 5 and Mifare Classic.

The general approach for writing a NDEF message is as follows:

  1. Call nfc_reader_request_tag_id()
  2. Wait for state to change to ReaderRequestTagIDReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  3. If looking for a specific tag then call nfc_reader_get_tag_id() and check if the expected tag was found, if it was not found got back to step 1
  4. Call nfc_reader_write_ndef() with the NDEF message that you want to write
  5. Wait for state to change to ReaderWriteNDEFReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
int nfc_reader_request_ndef(NFC *nfc)
Parameters:
  • nfc – Type: NFC *
Returns:
  • e_code – Type: int

Reads NDEF formated data from a tag.

This function currently supports NFC Forum Type 1, 2, 3, 4, 5 and Mifare Classic.

The general approach for reading a NDEF message is as follows:

  1. Call nfc_reader_request_tag_id()
  2. Wait for state to change to RequestTagIDReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  3. If looking for a specific tag then call nfc_reader_get_tag_id() and check if the expected tag was found, if it was not found got back to step 1
  4. Call nfc_reader_request_ndef()
  5. Wait for state to change to ReaderRequestNDEFReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  6. Call nfc_reader_read_ndef() to retrieve the NDEF message from the buffer
int nfc_reader_read_ndef(NFC *nfc, uint8_t *ret_ndef, uint16_t *ret_ndef_length)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_ndef – Type: uint8_t *, Range: [0 to 255]
  • ret_ndef_length – Type: uint16_t
Returns:
  • e_code – Type: int

Returns the NDEF data from an internal buffer. To fill the buffer with a NDEF message you have to call nfc_reader_request_ndef() beforehand.

int nfc_reader_authenticate_mifare_classic_page(NFC *nfc, uint16_t page, uint8_t key_number, uint8_t key[6])
Parameters:
  • nfc – Type: NFC *
  • page – Type: uint16_t, Range: [0 to 216 - 1]
  • key_number – Type: uint8_t, Range: See constants
  • key – Type: uint8_t[6], Range: [0 to 255]
Returns:
  • e_code – Type: int

Mifare Classic tags use authentication. If you want to read from or write to a Mifare Classic page you have to authenticate it beforehand. Each page can be authenticated with two keys: A (key_number = 0) and B (key_number = 1). A new Mifare Classic tag that has not yet been written to can be accessed with key A and the default key [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF].

The approach to read or write a Mifare Classic page is as follows:

  1. Call nfc_reader_request_tag_id()
  2. Wait for state to change to ReaderRequestTagIDReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  3. If looking for a specific tag then call nfc_reader_get_tag_id() and check if the expected tag was found, if it was not found got back to step 1
  4. Call nfc_reader_authenticate_mifare_classic_page() with page and key for the page
  5. Wait for state to change to ReaderAuthenticatingMifareClassicPageReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  6. Call nfc_reader_request_page() or nfc_reader_write_page() to read/write page

The authentication will always work for one whole sector (4 pages).

The following constants are available for this function:

For key_number:

  • NFC_KEY_A = 0
  • NFC_KEY_B = 1
int nfc_reader_write_page(NFC *nfc, uint16_t page, uint8_t *data, uint16_t data_length)
Parameters:
  • nfc – Type: NFC *
  • page – Type: uint16_t, Range: See constants
  • data – Type: uint8_t *, Range: [0 to 255]
  • data_length – Type: uint16_t
Returns:
  • e_code – Type: int

Writes a maximum of 8192 bytes starting from the given page. How many pages are written depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte
  • NFC Forum Type 1 page size: 8 byte
  • NFC Forum Type 2 page size: 4 byte
  • NFC Forum Type 3 page size: 16 byte
  • NFC Forum Type 4: No pages, page = file selection (CC or NDEF, see below)
  • NFC Forum Type 5 page size: 4 byte

The general approach for writing to a tag is as follows:

  1. Call nfc_reader_request_tag_id()
  2. Wait for state to change to ReaderRequestTagIDReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  3. If looking for a specific tag then call nfc_reader_get_tag_id() and check if the expected tag was found, if it was not found got back to step 1
  4. Call nfc_reader_write_page() with page number and data
  5. Wait for state to change to ReaderWritePageReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)

If you use a Mifare Classic tag you have to authenticate a page before you can write to it. See nfc_reader_authenticate_mifare_classic_page().

NFC Forum Type 4 tags are not organized into pages but different files. We currently support two files: Capability Container file (CC) and NDEF file.

Choose CC by setting page to 3 or NDEF by setting page to 4.

The following constants are available for this function:

For page:

  • NFC_READER_WRITE_TYPE4_CAPABILITY_CONTAINER = 3
  • NFC_READER_WRITE_TYPE4_NDEF = 4
int nfc_reader_request_page(NFC *nfc, uint16_t page, uint16_t length)
Parameters:
  • nfc – Type: NFC *
  • page – Type: uint16_t, Range: See constants
  • length – Type: uint16_t, Range: [0 to 213]
Returns:
  • e_code – Type: int

Reads a maximum of 8192 bytes starting from the given page and stores them into a buffer. The buffer can then be read out with nfc_reader_read_page(). How many pages are read depends on the tag type. The page sizes are as follows:

  • Mifare Classic page size: 16 byte
  • NFC Forum Type 1 page size: 8 byte
  • NFC Forum Type 2 page size: 4 byte
  • NFC Forum Type 3 page size: 16 byte
  • NFC Forum Type 4: No pages, page = file selection (CC or NDEF, see below)
  • NFC Forum Type 5 page size: 4 byte

The general approach for reading a tag is as follows:

  1. Call nfc_reader_request_tag_id()
  2. Wait for state to change to RequestTagIDReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  3. If looking for a specific tag then call nfc_reader_get_tag_id() and check if the expected tag was found, if it was not found got back to step 1
  4. Call nfc_reader_request_page() with page number
  5. Wait for state to change to ReaderRequestPageReady (see nfc_reader_get_state() or NFC_CALLBACK_READER_STATE_CHANGED callback)
  6. Call nfc_reader_read_page() to retrieve the page from the buffer

If you use a Mifare Classic tag you have to authenticate a page before you can read it. See nfc_reader_authenticate_mifare_classic_page().

NFC Forum Type 4 tags are not organized into pages but different files. We currently support two files: Capability Container file (CC) and NDEF file.

Choose CC by setting page to 3 or NDEF by setting page to 4.

The following constants are available for this function:

For page:

  • NFC_READER_REQUEST_TYPE4_CAPABILITY_CONTAINER = 3
  • NFC_READER_REQUEST_TYPE4_NDEF = 4
int nfc_reader_read_page(NFC *nfc, uint8_t *ret_data, uint16_t *ret_data_length)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_data – Type: uint8_t *, Range: [0 to 255]
  • ret_data_length – Type: uint16_t
Returns:
  • e_code – Type: int

Returns the page data from an internal buffer. To fill the buffer with specific pages you have to call nfc_reader_request_page() beforehand.

int nfc_cardemu_get_state(NFC *nfc, uint8_t *ret_state, bool *ret_idle)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_state – Type: uint8_t, Range: See constants
  • ret_idle – Type: bool
Returns:
  • e_code – Type: int

Returns the current cardemu state of the NFC Bricklet.

On startup the Bricklet will be in the CardemuInitialization state. The initialization will only take about 20ms. After that it changes to CardemuIdle.

The Bricklet is also reinitialized if the mode is changed, see nfc_set_mode().

The functions of this Bricklet can be called in the CardemuIdle state and all of the CardemuReady and CardemuError states.

Example: If you call nfc_cardemu_start_discovery(), the state will change to CardemuDiscover until the discovery is finished. Then it will change to either CardemuDiscoverReady if it worked or to CardemuDiscoverError if it didn't.

The same approach is used analogously for the other API functions.

The following constants are available for this function:

For ret_state:

  • NFC_CARDEMU_STATE_INITIALIZATION = 0
  • NFC_CARDEMU_STATE_IDLE = 128
  • NFC_CARDEMU_STATE_ERROR = 192
  • NFC_CARDEMU_STATE_DISCOVER = 2
  • NFC_CARDEMU_STATE_DISCOVER_READY = 130
  • NFC_CARDEMU_STATE_DISCOVER_ERROR = 194
  • NFC_CARDEMU_STATE_TRANSFER_NDEF = 3
  • NFC_CARDEMU_STATE_TRANSFER_NDEF_READY = 131
  • NFC_CARDEMU_STATE_TRANSFER_NDEF_ERROR = 195
int nfc_cardemu_start_discovery(NFC *nfc)
Parameters:
  • nfc – Type: NFC *
Returns:
  • e_code – Type: int

Starts the discovery process. If you call this function while a NFC reader device is near to the NFC Bricklet the state will change from CardemuDiscovery to CardemuDiscoveryReady.

If no NFC reader device can be found or if there is an error during discovery the cardemu state will change to CardemuDiscoveryError. In this case you have to restart the discovery process.

If the cardemu state changes to CardemuDiscoveryReady you can start the NDEF message transfer with nfc_cardemu_write_ndef() and nfc_cardemu_start_transfer().

int nfc_cardemu_write_ndef(NFC *nfc, uint8_t *ndef, uint16_t ndef_length)
Parameters:
  • nfc – Type: NFC *
  • ndef – Type: uint8_t *, Range: [0 to 255]
  • ndef_length – Type: uint16_t
Returns:
  • e_code – Type: int

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size in Cardemu mode is 255 byte.

You can call this function at any time in Cardemu mode. The internal buffer will not be overwritten until you call this function again or change the mode.

int nfc_cardemu_start_transfer(NFC *nfc, uint8_t transfer)
Parameters:
  • nfc – Type: NFC *
  • transfer – Type: uint8_t, Range: See constants
Returns:
  • e_code – Type: int

You can start the transfer of a NDEF message if the cardemu state is CardemuDiscoveryReady.

Before you call this function to start a write transfer, the NDEF message that is to be transferred has to be written via nfc_cardemu_write_ndef() first.

After you call this function the state will change to CardemuTransferNDEF. It will change to CardemuTransferNDEFReady if the transfer was successful or CardemuTransferNDEFError if it wasn't.

The following constants are available for this function:

For transfer:

  • NFC_CARDEMU_TRANSFER_ABORT = 0
  • NFC_CARDEMU_TRANSFER_WRITE = 1
int nfc_p2p_get_state(NFC *nfc, uint8_t *ret_state, bool *ret_idle)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_state – Type: uint8_t, Range: See constants
  • ret_idle – Type: bool
Returns:
  • e_code – Type: int

Returns the current P2P state of the NFC Bricklet.

On startup the Bricklet will be in the P2PInitialization state. The initialization will only take about 20ms. After that it changes to P2PIdle.

The Bricklet is also reinitialized if the mode is changed, see nfc_set_mode().

The functions of this Bricklet can be called in the P2PIdle state and all of the P2PReady and P2PError states.

Example: If you call nfc_p2p_start_discovery(), the state will change to P2PDiscover until the discovery is finished. Then it will change to either P2PDiscoverReady* if it worked or to P2PDiscoverError if it didn't.

The same approach is used analogously for the other API functions.

The following constants are available for this function:

For ret_state:

  • NFC_P2P_STATE_INITIALIZATION = 0
  • NFC_P2P_STATE_IDLE = 128
  • NFC_P2P_STATE_ERROR = 192
  • NFC_P2P_STATE_DISCOVER = 2
  • NFC_P2P_STATE_DISCOVER_READY = 130
  • NFC_P2P_STATE_DISCOVER_ERROR = 194
  • NFC_P2P_STATE_TRANSFER_NDEF = 3
  • NFC_P2P_STATE_TRANSFER_NDEF_READY = 131
  • NFC_P2P_STATE_TRANSFER_NDEF_ERROR = 195
int nfc_p2p_start_discovery(NFC *nfc)
Parameters:
  • nfc – Type: NFC *
Returns:
  • e_code – Type: int

Starts the discovery process. If you call this function while another NFC P2P enabled device is near to the NFC Bricklet the state will change from P2PDiscovery to P2PDiscoveryReady.

If no NFC P2P enabled device can be found or if there is an error during discovery the P2P state will change to P2PDiscoveryError. In this case you have to restart the discovery process.

If the P2P state changes to P2PDiscoveryReady you can start the NDEF message transfer with nfc_p2p_start_transfer().

int nfc_p2p_write_ndef(NFC *nfc, uint8_t *ndef, uint16_t ndef_length)
Parameters:
  • nfc – Type: NFC *
  • ndef – Type: uint8_t *, Range: [0 to 255]
  • ndef_length – Type: uint16_t
Returns:
  • e_code – Type: int

Writes the NDEF message that is to be transferred to the NFC peer.

The maximum supported NDEF message size for P2P transfer is 255 byte.

You can call this function at any time in P2P mode. The internal buffer will not be overwritten until you call this function again, change the mode or use P2P to read an NDEF messages.

int nfc_p2p_start_transfer(NFC *nfc, uint8_t transfer)
Parameters:
  • nfc – Type: NFC *
  • transfer – Type: uint8_t, Range: See constants
Returns:
  • e_code – Type: int

You can start the transfer of a NDEF message if the P2P state is P2PDiscoveryReady.

Before you call this function to start a write transfer, the NDEF message that is to be transferred has to be written via nfc_p2p_write_ndef() first.

After you call this function the P2P state will change to P2PTransferNDEF. It will change to P2PTransferNDEFReady if the transfer was successfull or P2PTransferNDEFError if it wasn't.

If you started a write transfer you are now done. If you started a read transfer you can now use nfc_p2p_read_ndef() to read the NDEF message that was written by the NFC peer.

The following constants are available for this function:

For transfer:

  • NFC_P2P_TRANSFER_ABORT = 0
  • NFC_P2P_TRANSFER_WRITE = 1
  • NFC_P2P_TRANSFER_READ = 2
int nfc_p2p_read_ndef(NFC *nfc, uint8_t *ret_ndef, uint16_t *ret_ndef_length)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_ndef – Type: uint8_t *, Range: [0 to 255]
  • ret_ndef_length – Type: uint16_t
Returns:
  • e_code – Type: int

Returns the NDEF message that was written by a NFC peer in NFC P2P mode.

The NDEF message is ready if you called nfc_p2p_start_transfer() with a read transfer and the P2P state changed to P2PTransferNDEFReady.

int nfc_simple_get_tag_id(NFC *nfc, uint8_t index, uint8_t *ret_tag_type, uint8_t *ret_tag_id, uint8_t *ret_tag_id_length, uint32_t *ret_last_seen)
Parameters:
  • nfc – Type: NFC *
  • index – Type: uint8_t, Range: [0 to 7]
Output Parameters:
  • ret_tag_type – Type: uint8_t, Range: See constants
  • ret_tag_id – Type: uint8_t *, Range: [0 to 255]
  • ret_tag_id_length – Type: uint8_t
  • ret_last_seen – Type: uint32_t, Unit: 1 ms, Range: [0 to 232 - 1]
Returns:
  • e_code – Type: int

Returns the tag type and tag ID from simple mode sorted by last seen time for a given index.

Up to eight tags are saved.

The following constants are available for this function:

For ret_tag_type:

  • NFC_TAG_TYPE_MIFARE_CLASSIC = 0
  • NFC_TAG_TYPE_TYPE1 = 1
  • NFC_TAG_TYPE_TYPE2 = 2
  • NFC_TAG_TYPE_TYPE3 = 3
  • NFC_TAG_TYPE_TYPE4 = 4
  • NFC_TAG_TYPE_TYPE5 = 5

New in version 2.0.6 (Plugin).

int nfc_cardemu_set_tag_id(NFC *nfc, uint8_t tag_id_length, uint8_t tag_id_data[7])
Parameters:
  • nfc – Type: NFC *
  • tag_id_length – Type: uint8_t, Range: [0 to 7]
  • tag_id_data – Type: uint8_t[7], Range: [0 to 255]
Returns:
  • e_code – Type: int

Sets the tag ID for cardemu mode. The tag ID can either have a length of 4 or 7.

Set a length of 0 for random tag ID (default)

New in version 2.1.0 (Plugin).

int nfc_cardemu_get_tag_id(NFC *nfc, uint8_t *ret_tag_id_length, uint8_t ret_tag_id_data[7])
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_tag_id_length – Type: uint8_t, Range: [0 to 7]
  • ret_tag_id_data – Type: uint8_t[7], Range: [0 to 255]
Returns:
  • e_code – Type: int

Returns the tag ID and length as set by nfc_cardemu_set_tag_id().

New in version 2.1.0 (Plugin).

Advanced Functions

int nfc_set_detection_led_config(NFC *nfc, uint8_t config)
Parameters:
  • nfc – Type: NFC *
  • config – Type: uint8_t, Range: See constants, Default: 3
Returns:
  • e_code – Type: int

Sets the detection LED configuration. By default the LED shows if a card/reader is detected.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is off.

The following constants are available for this function:

For config:

  • NFC_DETECTION_LED_CONFIG_OFF = 0
  • NFC_DETECTION_LED_CONFIG_ON = 1
  • NFC_DETECTION_LED_CONFIG_SHOW_HEARTBEAT = 2
  • NFC_DETECTION_LED_CONFIG_SHOW_DETECTION = 3
int nfc_get_detection_led_config(NFC *nfc, uint8_t *ret_config)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_config – Type: uint8_t, Range: See constants, Default: 3
Returns:
  • e_code – Type: int

Returns the configuration as set by nfc_set_detection_led_config()

The following constants are available for this function:

For ret_config:

  • NFC_DETECTION_LED_CONFIG_OFF = 0
  • NFC_DETECTION_LED_CONFIG_ON = 1
  • NFC_DETECTION_LED_CONFIG_SHOW_HEARTBEAT = 2
  • NFC_DETECTION_LED_CONFIG_SHOW_DETECTION = 3
int nfc_set_maximum_timeout(NFC *nfc, uint16_t timeout)
Parameters:
  • nfc – Type: NFC *
  • timeout – Type: uint16_t, Unit: 1 ms, Range: [0 to 216 - 1], Default: 2000
Returns:
  • e_code – Type: int

Sets the maximum timeout.

This is a global maximum used for all internal state timeouts. The timeouts depend heavily on the used tags etc. For example: If you use a Type 2 tag and you want to detect if it is present, you have to use nfc_reader_request_tag_id() and wait for the state to change to either the error state or the ready state.

With the default configuration this takes 2-3 seconds. By setting the maximum timeout to 100ms you can reduce this time to ~150-200ms. For Type 2 this would also still work with a 20ms timeout (a Type 2 tag answers usually within 10ms). A type 4 tag can take up to 500ms in our tests.

If you need a fast response time to discover if a tag is present or not you can find a good timeout value by trial and error for your specific tag.

By default we use a very conservative timeout, to be sure that any tag can always answer in time.

New in version 2.0.1 (Plugin).

int nfc_get_maximum_timeout(NFC *nfc, uint16_t *ret_timeout)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_timeout – Type: uint16_t, Unit: 1 ms, Range: [0 to 216 - 1], Default: 2000
Returns:
  • e_code – Type: int

Returns the timeout as set by nfc_set_maximum_timeout()

New in version 2.0.1 (Plugin).

int nfc_get_spitfp_error_count(NFC *nfc, uint32_t *ret_error_count_ack_checksum, uint32_t *ret_error_count_message_checksum, uint32_t *ret_error_count_frame, uint32_t *ret_error_count_overflow)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_error_count_ack_checksum – Type: uint32_t, Range: [0 to 232 - 1]
  • ret_error_count_message_checksum – Type: uint32_t, Range: [0 to 232 - 1]
  • ret_error_count_frame – Type: uint32_t, Range: [0 to 232 - 1]
  • ret_error_count_overflow – Type: uint32_t, Range: [0 to 232 - 1]
Returns:
  • e_code – Type: int

Returns the error count for the communication between Brick and Bricklet.

The errors are divided into

  • ACK checksum errors,
  • message checksum errors,
  • framing errors and
  • overflow errors.

The errors counts are for errors that occur on the Bricklet side. All Bricks have a similar function that returns the errors on the Brick side.

int nfc_set_status_led_config(NFC *nfc, uint8_t config)
Parameters:
  • nfc – Type: NFC *
  • config – Type: uint8_t, Range: See constants, Default: 3
Returns:
  • e_code – Type: int

Sets the status LED configuration. By default the LED shows communication traffic between Brick and Bricklet, it flickers once for every 10 received data packets.

You can also turn the LED permanently on/off or show a heartbeat.

If the Bricklet is in bootloader mode, the LED is will show heartbeat by default.

The following constants are available for this function:

For config:

  • NFC_STATUS_LED_CONFIG_OFF = 0
  • NFC_STATUS_LED_CONFIG_ON = 1
  • NFC_STATUS_LED_CONFIG_SHOW_HEARTBEAT = 2
  • NFC_STATUS_LED_CONFIG_SHOW_STATUS = 3
int nfc_get_status_led_config(NFC *nfc, uint8_t *ret_config)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_config – Type: uint8_t, Range: See constants, Default: 3
Returns:
  • e_code – Type: int

Returns the configuration as set by nfc_set_status_led_config()

The following constants are available for this function:

For ret_config:

  • NFC_STATUS_LED_CONFIG_OFF = 0
  • NFC_STATUS_LED_CONFIG_ON = 1
  • NFC_STATUS_LED_CONFIG_SHOW_HEARTBEAT = 2
  • NFC_STATUS_LED_CONFIG_SHOW_STATUS = 3
int nfc_get_chip_temperature(NFC *nfc, int16_t *ret_temperature)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_temperature – Type: int16_t, Unit: 1 °C, Range: [-215 to 215 - 1]
Returns:
  • e_code – Type: int

Returns the temperature as measured inside the microcontroller. The value returned is not the ambient temperature!

The temperature is only proportional to the real temperature and it has bad accuracy. Practically it is only useful as an indicator for temperature changes.

int nfc_reset(NFC *nfc)
Parameters:
  • nfc – Type: NFC *
Returns:
  • e_code – Type: int

Calling this function will reset the Bricklet. All configurations will be lost.

After a reset you have to create new device objects, calling functions on the existing ones will result in undefined behavior!

int nfc_get_identity(NFC *nfc, char ret_uid[8], char ret_connected_uid[8], char *ret_position, uint8_t ret_hardware_version[3], uint8_t ret_firmware_version[3], uint16_t *ret_device_identifier)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_uid – Type: char[8]
  • ret_connected_uid – Type: char[8]
  • ret_position – Type: char, Range: ['a' to 'h', 'z']
  • ret_hardware_version – Type: uint8_t[3]
    • 0: major – Type: uint8_t, Range: [0 to 255]
    • 1: minor – Type: uint8_t, Range: [0 to 255]
    • 2: revision – Type: uint8_t, Range: [0 to 255]
  • ret_firmware_version – Type: uint8_t[3]
    • 0: major – Type: uint8_t, Range: [0 to 255]
    • 1: minor – Type: uint8_t, Range: [0 to 255]
    • 2: revision – Type: uint8_t, Range: [0 to 255]
  • ret_device_identifier – Type: uint16_t, Range: [0 to 216 - 1]
Returns:
  • e_code – Type: int

Returns the UID, the UID where the Bricklet is connected to, the position, the hardware and firmware version as well as the device identifier.

The position can be 'a', 'b', 'c', 'd', 'e', 'f', 'g' or 'h' (Bricklet Port). A Bricklet connected to an Isolator Bricklet is always at position 'z'.

The device identifier numbers can be found here. There is also a constant for the device identifier of this Bricklet.

Callback Configuration Functions

void nfc_register_callback(NFC *nfc, int16_t callback_id, void (*function)(void), void *user_data)
Parameters:
  • nfc – Type: NFC *
  • callback_id – Type: int16_t
  • function – Type: void (*)(void)
  • user_data – Type: void *

Registers the given function with the given callback_id. The user_data will be passed as the last parameter to the function.

The available callback IDs with corresponding function signatures are listed below.

Callbacks

Callbacks can be registered to receive time critical or recurring data from the device. The registration is done with the nfc_register_callback() function:

void my_callback(int value, void *user_data) {
    printf("Value: %d\n", value);
}

nfc_register_callback(&nfc,
                      NFC_CALLBACK_EXAMPLE,
                      (void (*)(void))my_callback,
                      NULL);

The available constants with corresponding function signatures are described below.

Note

Using callbacks for recurring events is always preferred compared to using getters. It will use less USB bandwidth and the latency will be a lot better, since there is no round trip time.

NFC_CALLBACK_READER_STATE_CHANGED
void callback(uint8_t state, bool idle, void *user_data)
Callback Parameters:
  • state – Type: uint8_t, Range: See constants
  • idle – Type: bool
  • user_data – Type: void *

This callback is called if the reader state of the NFC Bricklet changes. See nfc_reader_get_state() for more information about the possible states.

The following constants are available for this function:

For state:

  • NFC_READER_STATE_INITIALIZATION = 0
  • NFC_READER_STATE_IDLE = 128
  • NFC_READER_STATE_ERROR = 192
  • NFC_READER_STATE_REQUEST_TAG_ID = 2
  • NFC_READER_STATE_REQUEST_TAG_ID_READY = 130
  • NFC_READER_STATE_REQUEST_TAG_ID_ERROR = 194
  • NFC_READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE = 3
  • NFC_READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE_READY = 131
  • NFC_READER_STATE_AUTHENTICATE_MIFARE_CLASSIC_PAGE_ERROR = 195
  • NFC_READER_STATE_WRITE_PAGE = 4
  • NFC_READER_STATE_WRITE_PAGE_READY = 132
  • NFC_READER_STATE_WRITE_PAGE_ERROR = 196
  • NFC_READER_STATE_REQUEST_PAGE = 5
  • NFC_READER_STATE_REQUEST_PAGE_READY = 133
  • NFC_READER_STATE_REQUEST_PAGE_ERROR = 197
  • NFC_READER_STATE_WRITE_NDEF = 6
  • NFC_READER_STATE_WRITE_NDEF_READY = 134
  • NFC_READER_STATE_WRITE_NDEF_ERROR = 198
  • NFC_READER_STATE_REQUEST_NDEF = 7
  • NFC_READER_STATE_REQUEST_NDEF_READY = 135
  • NFC_READER_STATE_REQUEST_NDEF_ERROR = 199
NFC_CALLBACK_CARDEMU_STATE_CHANGED
void callback(uint8_t state, bool idle, void *user_data)
Callback Parameters:
  • state – Type: uint8_t, Range: See constants
  • idle – Type: bool
  • user_data – Type: void *

This callback is called if the cardemu state of the NFC Bricklet changes. See nfc_cardemu_get_state() for more information about the possible states.

The following constants are available for this function:

For state:

  • NFC_CARDEMU_STATE_INITIALIZATION = 0
  • NFC_CARDEMU_STATE_IDLE = 128
  • NFC_CARDEMU_STATE_ERROR = 192
  • NFC_CARDEMU_STATE_DISCOVER = 2
  • NFC_CARDEMU_STATE_DISCOVER_READY = 130
  • NFC_CARDEMU_STATE_DISCOVER_ERROR = 194
  • NFC_CARDEMU_STATE_TRANSFER_NDEF = 3
  • NFC_CARDEMU_STATE_TRANSFER_NDEF_READY = 131
  • NFC_CARDEMU_STATE_TRANSFER_NDEF_ERROR = 195
NFC_CALLBACK_P2P_STATE_CHANGED
void callback(uint8_t state, bool idle, void *user_data)
Callback Parameters:
  • state – Type: uint8_t, Range: See constants
  • idle – Type: bool
  • user_data – Type: void *

This callback is called if the P2P state of the NFC Bricklet changes. See nfc_p2p_get_state() for more information about the possible states.

The following constants are available for this function:

For state:

  • NFC_P2P_STATE_INITIALIZATION = 0
  • NFC_P2P_STATE_IDLE = 128
  • NFC_P2P_STATE_ERROR = 192
  • NFC_P2P_STATE_DISCOVER = 2
  • NFC_P2P_STATE_DISCOVER_READY = 130
  • NFC_P2P_STATE_DISCOVER_ERROR = 194
  • NFC_P2P_STATE_TRANSFER_NDEF = 3
  • NFC_P2P_STATE_TRANSFER_NDEF_READY = 131
  • NFC_P2P_STATE_TRANSFER_NDEF_ERROR = 195

Virtual Functions

Virtual functions don't communicate with the device itself, but operate only on the API bindings device object. They can be called without the corresponding IP Connection object being connected.

int nfc_get_api_version(NFC *nfc, uint8_t ret_api_version[3])
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_api_version – Type: uint8_t[3]
    • 0: major – Type: uint8_t, Range: [0 to 255]
    • 1: minor – Type: uint8_t, Range: [0 to 255]
    • 2: revision – Type: uint8_t, Range: [0 to 255]
Returns:
  • e_code – Type: int

Returns the version of the API definition implemented by this API bindings. This is neither the release version of this API bindings nor does it tell you anything about the represented Brick or Bricklet.

int nfc_get_response_expected(NFC *nfc, uint8_t function_id, bool *ret_response_expected)
Parameters:
  • nfc – Type: NFC *
  • function_id – Type: uint8_t, Range: See constants
Output Parameters:
  • ret_response_expected – Type: bool
Returns:
  • e_code – Type: int

Returns the response expected flag for the function specified by the function ID parameter. It is true if the function is expected to send a response, false otherwise.

For getter functions this is enabled by default and cannot be disabled, because those functions will always send a response. For callback configuration functions it is enabled by default too, but can be disabled by nfc_set_response_expected(). For setter functions it is disabled by default and can be enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For function_id:

  • NFC_FUNCTION_SET_MODE = 1
  • NFC_FUNCTION_READER_REQUEST_TAG_ID = 3
  • NFC_FUNCTION_READER_WRITE_NDEF = 6
  • NFC_FUNCTION_READER_REQUEST_NDEF = 7
  • NFC_FUNCTION_READER_AUTHENTICATE_MIFARE_CLASSIC_PAGE = 9
  • NFC_FUNCTION_READER_WRITE_PAGE = 10
  • NFC_FUNCTION_READER_REQUEST_PAGE = 11
  • NFC_FUNCTION_CARDEMU_START_DISCOVERY = 15
  • NFC_FUNCTION_CARDEMU_WRITE_NDEF = 16
  • NFC_FUNCTION_CARDEMU_START_TRANSFER = 17
  • NFC_FUNCTION_P2P_START_DISCOVERY = 20
  • NFC_FUNCTION_P2P_WRITE_NDEF = 21
  • NFC_FUNCTION_P2P_START_TRANSFER = 22
  • NFC_FUNCTION_SET_DETECTION_LED_CONFIG = 25
  • NFC_FUNCTION_SET_MAXIMUM_TIMEOUT = 27
  • NFC_FUNCTION_CARDEMU_SET_TAG_ID = 30
  • NFC_FUNCTION_SET_WRITE_FIRMWARE_POINTER = 237
  • NFC_FUNCTION_SET_STATUS_LED_CONFIG = 239
  • NFC_FUNCTION_RESET = 243
  • NFC_FUNCTION_WRITE_UID = 248
int nfc_set_response_expected(NFC *nfc, uint8_t function_id, bool response_expected)
Parameters:
  • nfc – Type: NFC *
  • function_id – Type: uint8_t, Range: See constants
  • response_expected – Type: bool
Returns:
  • e_code – Type: int

Changes the response expected flag of the function specified by the function ID parameter. This flag can only be changed for setter (default value: false) and callback configuration functions (default value: true). For getter functions it is always enabled.

Enabling the response expected flag for a setter function allows to detect timeouts and other error conditions calls of this setter as well. The device will then send a response for this purpose. If this flag is disabled for a setter function then no response is sent and errors are silently ignored, because they cannot be detected.

The following constants are available for this function:

For function_id:

  • NFC_FUNCTION_SET_MODE = 1
  • NFC_FUNCTION_READER_REQUEST_TAG_ID = 3
  • NFC_FUNCTION_READER_WRITE_NDEF = 6
  • NFC_FUNCTION_READER_REQUEST_NDEF = 7
  • NFC_FUNCTION_READER_AUTHENTICATE_MIFARE_CLASSIC_PAGE = 9
  • NFC_FUNCTION_READER_WRITE_PAGE = 10
  • NFC_FUNCTION_READER_REQUEST_PAGE = 11
  • NFC_FUNCTION_CARDEMU_START_DISCOVERY = 15
  • NFC_FUNCTION_CARDEMU_WRITE_NDEF = 16
  • NFC_FUNCTION_CARDEMU_START_TRANSFER = 17
  • NFC_FUNCTION_P2P_START_DISCOVERY = 20
  • NFC_FUNCTION_P2P_WRITE_NDEF = 21
  • NFC_FUNCTION_P2P_START_TRANSFER = 22
  • NFC_FUNCTION_SET_DETECTION_LED_CONFIG = 25
  • NFC_FUNCTION_SET_MAXIMUM_TIMEOUT = 27
  • NFC_FUNCTION_CARDEMU_SET_TAG_ID = 30
  • NFC_FUNCTION_SET_WRITE_FIRMWARE_POINTER = 237
  • NFC_FUNCTION_SET_STATUS_LED_CONFIG = 239
  • NFC_FUNCTION_RESET = 243
  • NFC_FUNCTION_WRITE_UID = 248
int nfc_set_response_expected_all(NFC *nfc, bool response_expected)
Parameters:
  • nfc – Type: NFC *
  • response_expected – Type: bool
Returns:
  • e_code – Type: int

Changes the response expected flag for all setter and callback configuration functions of this device at once.

Internal Functions

Internal functions are used for maintenance tasks such as flashing a new firmware of changing the UID of a Bricklet. These task should be performed using Brick Viewer instead of using the internal functions directly.

int nfc_set_bootloader_mode(NFC *nfc, uint8_t mode, uint8_t *ret_status)
Parameters:
  • nfc – Type: NFC *
  • mode – Type: uint8_t, Range: See constants
Output Parameters:
  • ret_status – Type: uint8_t, Range: See constants
Returns:
  • e_code – Type: int

Sets the bootloader mode and returns the status after the requested mode change was instigated.

You can change from bootloader mode to firmware mode and vice versa. A change from bootloader mode to firmware mode will only take place if the entry function, device identifier and CRC are present and correct.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

The following constants are available for this function:

For mode:

  • NFC_BOOTLOADER_MODE_BOOTLOADER = 0
  • NFC_BOOTLOADER_MODE_FIRMWARE = 1
  • NFC_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT = 2
  • NFC_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT = 3
  • NFC_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT = 4

For ret_status:

  • NFC_BOOTLOADER_STATUS_OK = 0
  • NFC_BOOTLOADER_STATUS_INVALID_MODE = 1
  • NFC_BOOTLOADER_STATUS_NO_CHANGE = 2
  • NFC_BOOTLOADER_STATUS_ENTRY_FUNCTION_NOT_PRESENT = 3
  • NFC_BOOTLOADER_STATUS_DEVICE_IDENTIFIER_INCORRECT = 4
  • NFC_BOOTLOADER_STATUS_CRC_MISMATCH = 5
int nfc_get_bootloader_mode(NFC *nfc, uint8_t *ret_mode)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_mode – Type: uint8_t, Range: See constants
Returns:
  • e_code – Type: int

Returns the current bootloader mode, see nfc_set_bootloader_mode().

The following constants are available for this function:

For ret_mode:

  • NFC_BOOTLOADER_MODE_BOOTLOADER = 0
  • NFC_BOOTLOADER_MODE_FIRMWARE = 1
  • NFC_BOOTLOADER_MODE_BOOTLOADER_WAIT_FOR_REBOOT = 2
  • NFC_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_REBOOT = 3
  • NFC_BOOTLOADER_MODE_FIRMWARE_WAIT_FOR_ERASE_AND_REBOOT = 4
int nfc_set_write_firmware_pointer(NFC *nfc, uint32_t pointer)
Parameters:
  • nfc – Type: NFC *
  • pointer – Type: uint32_t, Unit: 1 B, Range: [0 to 232 - 1]
Returns:
  • e_code – Type: int

Sets the firmware pointer for nfc_write_firmware(). The pointer has to be increased by chunks of size 64. The data is written to flash every 4 chunks (which equals to one page of size 256).

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

int nfc_write_firmware(NFC *nfc, uint8_t data[64], uint8_t *ret_status)
Parameters:
  • nfc – Type: NFC *
  • data – Type: uint8_t[64], Range: [0 to 255]
Output Parameters:
  • ret_status – Type: uint8_t, Range: [0 to 255]
Returns:
  • e_code – Type: int

Writes 64 Bytes of firmware at the position as written by nfc_set_write_firmware_pointer() before. The firmware is written to flash every 4 chunks.

You can only write firmware in bootloader mode.

This function is used by Brick Viewer during flashing. It should not be necessary to call it in a normal user program.

int nfc_write_uid(NFC *nfc, uint32_t uid)
Parameters:
  • nfc – Type: NFC *
  • uid – Type: uint32_t, Range: [0 to 232 - 1]
Returns:
  • e_code – Type: int

Writes a new UID into flash. If you want to set a new UID you have to decode the Base58 encoded UID string into an integer first.

We recommend that you use Brick Viewer to change the UID.

int nfc_read_uid(NFC *nfc, uint32_t *ret_uid)
Parameters:
  • nfc – Type: NFC *
Output Parameters:
  • ret_uid – Type: uint32_t, Range: [0 to 232 - 1]
Returns:
  • e_code – Type: int

Returns the current UID as an integer. Encode as Base58 to get the usual string version.

Constants

NFC_DEVICE_IDENTIFIER

This constant is used to identify a NFC Bricklet.

The nfc_get_identity() function and the IPCON_CALLBACK_ENUMERATE callback of the IP Connection have a device_identifier parameter to specify the Brick's or Bricklet's type.

NFC_DEVICE_DISPLAY_NAME

This constant represents the human readable name of a NFC Bricklet.