5.7.19. Write APDU to buffer

Normally, when we create an APDU, we also perform transceive operation and return the result to the application. Optionally, the user may want to just create the APDU and not perform the transceive operation.

This can be done by creating your own transaction function. All SE05x APIs call a common transaction function in which the complete APDU is created and then transmitted.

The following function can be used as reference.

smStatus_t fLogTransmitBuffer(Se05xSession_t *pwrite_apdubufferctx,
    const tlvHeader_t *hdr,
    uint8_t *cmdBuf,
    size_t cmdBufLen,
    uint8_t *rsp,
    size_t *rspLen,
    uint8_t hasle)
{
    memset(gTxBuffer, 0, sizeof(gTxBuffer));
    size_t i = 0;
    memcpy(&gTxBuffer[i], hdr, sizeof(*hdr));
    smStatus_t ret = SM_OK;
    i += sizeof(*hdr);
    if (cmdBufLen > 0) {
        // The Lc field must be extended in case the length does not fit
        // into a single byte (Note, while the standard would allow to
        // encode 0x100 as 0x00 in the Lc field, nobody who is sane in his mind
        // would actually do that).
        if ((cmdBufLen < 0xFF) && !hasle) {
            gTxBuffer[i++] = (uint8_t)cmdBufLen;
        }
        else {
            gTxBuffer[i++] = 0x00;
            gTxBuffer[i++] = 0xFFu & (cmdBufLen >> 8);
            gTxBuffer[i++] = 0xFFu & (cmdBufLen);
        }
        memcpy(&gTxBuffer[i], cmdBuf, cmdBufLen);
        i += cmdBufLen;
    }
    if (hasle) {
        gTxBuffer[i++] = 0x00;
        gTxBuffer[i++] = 0x00;
    }
    ret          = SM_OK;
    gTxBufferLen = i;

    return ret;
}

Assign this function to session context and use as following

Se05xSession_t write_apdubufferctx = {0};
write_apdubufferctx.fp_TXn         = &fLogTransmitBuffer;

Se05x_API_WriteBinary(&write_apdubufferctx, &policy, objectID, offset, length, inputData, inputDataLen);

After this, whichever SE05x API you call with write_apdubufferctx, it will just create the APDU and write it to the buffer. No transceive operation will be performed.

5.7.19.1. Building

Build the project with the following configurations.

se05x_GetAPDUBuffer

  • SCP = None

  • Project = se05x_GetAPDUBuffer

5.7.19.2. Running

On successful execution you can see the APDU buffer printed out:

App   :INFO :Policy for ReadOnly File:
App   :INFO :Locked = READ
App   :INFO :Excluded = DELETE, WRITE.
App   :INFO :pPolicyBuffer (Len=9)
      08 00 00 00    00 00 20 00    00
App   :INFO :gTxBuffer (Len=38)
      80 01 06 00    21 11 09 08    00 00 00 00    00 20 00 00
      41 04 11 22    33 44 43 02    00 0A 44 0A    01 02 03 04
      05 06 07 08    09 0A