[Pcsclite-muscle] sample code

Allen Curtis allen at criticalsoftwaresolutions.com
Fri Apr 7 08:35:02 PDT 2023


Below is the attached code distilled down to the relevant logic. The
actual source files are attached as well for your review.

typedef struct
{
    const RfidAtr atr;
    const AesKey keys[NUM_KEYS];  ///< list of key values used for
authentication
    const TagFile files[NUM_NTAG424_FILES];

    bool connected; ///< indicate that we are current connected to the reader
    size_t command_counter; ///< counter used for ADPU construction

    AesKey encrypt_key; ///< authentication derived AES encryption key
    AesKey cmac_key;    ///< authentication derived AES-MAC key
    TI ti;  ///< authentication derived transaction identifier

    pthread_t tid; ///< Monitor thread ID

    char* reader_name; ///< name of the reader returned by the driver to use
    SCARDCONTEXT context; ///< device driver context
    SCARDHANDLE handle; ///< handle to the connected card
    SCARD_READERSTATE reader_state; ///< current state of the reader,
updated by monitor
    DWORD card_state; ///< current state of the card in the reader
    SCARD_IO_REQUEST card_protocol; ///< card protocol to be used


} Ntag424;

Ntag424 RfidTag;

int main()
{
    LONG rv;
    DWORD dwReaders;
    char *ptr;
    char *found_readers = NULL;
    char *acs_picc_reader = NULL;
    int e_code = NO_ERROR;
    Ntag424 *ntag = &RfidTag;

    /* Get Context to the driver */
    rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &ntag->context);
    CHECK_ERROR(rv, "SCardEstablishContext");

    /* Retrieve the available readers list. */
    dwReaders = SCARD_AUTOALLOCATE;
    rv = SCardListReaders(ntag->context, NULL, (LPSTR)&found_readers,
&dwReaders);
    CHECK_ERROR(rv, "SCardListReaders");

    /* Extract readers from the null separated string and get the total
     * number of readers */
    unsigned nbReaders = 0;
    ptr = found_readers;
    while (*ptr != '\0')
    {
        printf("Reader %d: %s\n", nbReaders, ptr);
        char *p_acr1252 = strstr(ptr, "ACR1252");
        if (p_acr1252)
        {
            char *p_picc = strstr(ptr, "PICC");
            if (p_picc)
            {
                acs_picc_reader = ptr;
            }
        }

        ptr += strlen(ptr) + 1;
        nbReaders++;
    }

    if (nbReaders == 0)
    {
        printf("No card readers were found!\n");
        e_code = ERROR_INTERNAL_ERROR;
    }

    if (acs_picc_reader == NULL)
    {
        printf("Error: ACR1252 Dual Reader PICC not found!\n");
        e_code = ERROR_INTERNAL_ERROR;
    }
    else
    {
        printf("found: %s\n", acs_picc_reader);

        ntag->reader_name = malloc(strlen(acs_picc_reader)+1);
        strcpy(ntag->reader_name, acs_picc_reader);
    }

    SCardFreeMemory(ntag->context, found_readers);

        // Get current state
        LONG rv = SCardGetStatusChange(ntag->context, INFINITE,
&ntag->reader_state, 1);

        if (rv == SCARD_S_SUCCESS)
        {
            show_reader_state(ntag->reader_state.dwEventState);
            ntag->reader_state.dwCurrentState = ntag->reader_state.dwEventState;

            printf("calling SCardConnect\n");
            DWORD dwActiveProtocol = SCARD_PROTOCOL_UNSET;
            LONG rv = SCardConnect(
                RfidTag.context,
                RfidTag.reader_name,
                SCARD_SHARE_SHARED,
                (SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1),
                &RfidTag.handle,
                &dwActiveProtocol);
        }
        else
        {
            printf("Error: %s (0x%lX)\n", pcsc_stringify_error(rv), rv);
        }\

    return rv;
}


-- 
Allen Curtis
Medical Device Architect
Critical Software Solutions, LLC
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ntag424_daemon.c
Type: text/x-csrc
Size: 10927 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/pcsclite-muscle/attachments/20230407/30e8c95d/attachment-0002.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ntag424.h
Type: text/x-chdr
Size: 2908 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/pcsclite-muscle/attachments/20230407/30e8c95d/attachment-0003.bin>


More information about the pcsclite-muscle mailing list