Reception sample

This small snippet of code shows how to retrieve the packets stored in a TC_PACKETS_BUFFER in two possible ways:

int main(int argc, char* argv[])
{
    TC_INSTANCE hInstance;
    TC_PACKETS_BUFFER hPacketsBuffer;
    TC_STATUS result;
    PCHAR portName;

    if (argc < 2)
    {
        printf("Please provide port name\n");
        return -1;
    }

    portName = argv[1];

    result = TcInstanceOpenByName(portName, &hInstance);

    if (result != TC_SUCCESS)
    {
        printf("Error, cannot open port, error %s (%08x)\n", TcStatusGetString(result), result);
        return -1;
    }

    //
    // Enable reception
    //
    result = TcInstanceSetFeature(hInstance, TC_INST_FT_RX_STATUS, 1);
    if (result != TC_SUCCESS)
    {
        printf("Error, cannot enable reception: %s (%08x)", TcStatusGetString(result), result);
        
        (VOID)TcInstanceClose(hInstance);
            return -1;
    }

    PrintInstanceInfo(hInstance);

    //
    // reception code
    //
    do
    {
        result = TcInstanceReceivePackets(hInstance, &hPacketsBuffer);
        if (result != TC_SUCCESS) break;

        //
        // check if we have received any packet
        //
        if (hPacketsBuffer == NULL) continue;

        //
        // Please enable only one of the two process functions.
        // If you want to scan the packets with both the functions
        // you need to call TcPacketsBufferResetIndex.
        //

        //
        // this is the first way to process packets, API based
        //
        ProcessPacketsBuffer(hPacketsBuffer);

        //
        // This the second way to process packets, raw buffers
        //
//      ProcessPacketsBufferLowLevel(hPacketsBuffer);

        //
        // the packets in the buffer, being them returned by TcPacketsBufferGetNextPacket
        // or thru the raw pointers, are valid until the user calls PacketsBufferDestroy
        // or closes the Adapter
        //
        TcPacketsBufferDestroy(hPacketsBuffer);

        if (_kbhit()) break;

    }while(TRUE);
    
    if (result != TC_SUCCESS)
    {
        printf("Error in the reception process: %s (%08x)\n", TcStatusGetString(result), result);
    }

    result = TcInstanceClose(hInstance);
    _ASSERT(result == TC_SUCCESS);
}
void 
PrintInstanceInfo(TC_INSTANCE hInstance)
{
    TC_PORT hPort;
    PCHAR name;
    PCHAR description;
    TC_PORT_TYPE type;
    TC_STATUS status;
    TC_BOARD hBoard;

    hPort = TcInstanceGetPort(hInstance);

    name = TcPortGetName(hPort);
    description = TcPortGetDescription(hPort);
    type = TcPortGetType(hPort);

    printf("Port name = %s\n", name);
    printf("Port description = %s\n", description);

    switch(type)
    {
        case TC_PORT_TYPE_PHYSICAL: printf("Port type = physical\n"); break;
        case TC_PORT_TYPE_BAP:          printf("Port type = BAP\n"); break;
        case TC_PORT_TYPE_TCAP:     printf("Port type = TCAP\n"); break;
        break;
    }

    if (type == TC_PORT_TYPE_PHYSICAL || type == TC_PORT_TYPE_BAP)
    {
        status = TcPortQueryBoard(hPort, &hBoard);
        
        if (status != TC_SUCCESS)
        {
            hBoard = NULL;
            printf("Cannot retrieve the board, %s (%08x)\n", TcStatusGetString(status), status);
        }
    }
    else
    {
        hBoard = NULL;
    }

    if (hBoard != NULL)
    {
        description = TcBoardGetDescription(hBoard);
        printf("Board description = %s\n", description);
    }
}
void
ProcessPacketsBuffer(TC_PACKETS_BUFFER hPacketsBuffer)
{
    PVOID pData;
    TC_PACKET_HEADER header;
    TC_STATUS status;

    do
    {
        status = TcPacketsBufferQueryNextPacket(hPacketsBuffer,
            &header,
            &pData);

        if (status != TC_SUCCESS) break;

        PrintPacket(pData, &header);
    }while(TRUE);
}
void
ProcessPacketsBufferLowLevel(TC_PACKETS_BUFFER hPacketsBuffer)
{
    PVOID pData;
    PTC_PACKET_HEADER pHeader;
    PBYTE pBuffer;
    ULONG effectiveLength;
    ULONG position = 0;

    pBuffer = (PBYTE)TcPacketsBufferGetBuffer(hPacketsBuffer);
    effectiveLength = TcPacketsBufferGetEffectiveLength(hPacketsBuffer);

    while(position < effectiveLength)
    {
        //
        // here we are not checking that the packet is properly formatted
        //

        pHeader = (PTC_PACKET_HEADER)(pBuffer + position);
        position += sizeof(TC_PACKET_HEADER);
        pData = (pBuffer + position);

        PrintPacket(pData, pHeader);

        position += TC_ALIGN_USHORT_TO_64BIT(pHeader->CapturedLength);
    }
}
void
PrintPacket(PVOID pData, PTC_PACKET_HEADER pHeader)
{
    printf("Received packet at %I64u, size = %u\n", pHeader->Timestamp, pHeader->Length);
}

TurboCap API documentation. Copyright (c) 2007-2008 CACE Technologies. All rights reserved.