info/info_example.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Description

This source file is an example of how to use the Info stream interface in NTAPI.

The code shows how to retrieve and display basic information about the installed adapters. The adapter information presented in the example includes the serial number, version information for the hardware and FPGA images, and the PCI identifier.

Prerequisites

A working system is needed.

Program flow

The following is required to use the Info stream interface in NTAPI:
  • #include/nt.h - Applications/Tools only need to include nt.h to obtain prototypes, macros etc. from NTAPI.

  • NT_Init(NTAPI_VERSION) - Initialize the NTAPI library. NTAPI_VERSION is a define that describes the version of the API described in the header files included by nt.h. NT_Init() will ask the NTAPI library to convert return data to the NTAPI_VERSION if possible. This will ensure that applications can run on NTAPI libraries of newer versions.

  • NT_InfoOpen() - Open an info stream.

  • NT_InfoRead() - Read information.

  • NT_InfoClose() - Close the stream when terminating.

  • NT_Done() - Close down the NTAPI library.

  • NT_ExplainError() - Explain an error code returned by NTAPI functions.

/* * %NT_SOFTWARE_LICENSE% */ /** * @example info/info_example.c * @section info_example_description Description * * This source file is an example of how to use the @ref InfoStream * "Info stream" interface in NTAPI. * * The code shows how to retrieve and display basic information about * the installed adapters. The adapter information presented in the * example includes the serial number, version information for the * hardware and FPGA images, and the PCI identifier. * * The following NTAPI functions are used: * - @ref NT_Init() * - @ref NT_InfoOpen() * - @ref NT_InfoRead() * - @ref NT_InfoClose() * - @ref NT_Done() * - @ref NT_ExplainError() * * <hr> * @section info_example_prerequisites Prerequisites * A working system is needed. * * @section info_example_flow Program flow * @{ * The following is required to use the @ref InfoStream "Info stream" * interface in NTAPI: * - \#include/nt.h - Applications/Tools only need to include @ref * nt.h to obtain prototypes, macros etc. from NTAPI. * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI * library. @ref NTAPI_VERSION is a define that describes the version * of the API described in the header files included by @ref * nt.h. NT_Init() will ask the NTAPI library to convert return data * to the @ref NTAPI_VERSION if possible. This will ensure that * applications can run on NTAPI libraries of newer versions. * - @ref NT_InfoOpen() - Open an info stream. * - @ref NT_InfoRead() - Read information. * - @ref NT_InfoClose() - Close the stream when terminating. * - @ref NT_Done() - Close down the NTAPI library. * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions. * *<hr> * @} */ #include <nt.h> #include <assert.h> static int showProductInfo(const NtInfoStream_t hInfoStream, uint8_t adapterNo) { NtInfo_t hProdInfo; /* The file stream_info.h defines 'struct NtInfoProductInfo_s' that provides product information. */ const struct NtInfoProductInfo_v2_s *pi = &hProdInfo.u.productInfo_v2.data; int status; char imageStr[1024]; /* Ensure plenty of space */ hProdInfo.cmd = NT_INFO_CMD_READ_PRODUCT_INFO_V2; hProdInfo.u.productInfo_v2.adapterNo = adapterNo; status = NT_InfoRead(hInfoStream, &hProdInfo); if (status != NT_SUCCESS) return status; (void)printf("\nAdapter %u Product Information", (unsigned)adapterNo); (void)printf("\n-------------------------------------------\n"); if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN1) { (void)printf("%-24s : %s\n", "P/N", pi->ProductId); (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]); (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]); (void)printf("%-24s : %s\n", "CPLD", pi->CpldVersion); assert(sizeof(imageStr) >= sizeof(pi->fpgaId1)); (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1)); imageStr[14] = '\0'; (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr); assert(sizeof(imageStr) >= sizeof(pi->fpgaId2)); (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2)); imageStr[14] = '\0'; (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr); } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN2) { (void)printf("%-24s : %s\n", "P/N", pi->ProductId); (void)printf("%-24s : %s\n", "Main Board Serial No", pi->SerialNo[0]); (void)printf("%-24s : %s\n", "Main Board PBA", pi->PbaId[0]); assert(sizeof(imageStr) >= sizeof(pi->AvrId[0])); (void)memcpy(imageStr, pi->AvrId[0], sizeof(pi->AvrId[0])); imageStr[3] = '\0'; (void)printf("%-24s : %s\n", "Main Board AVR", imageStr); assert(sizeof(imageStr) >= sizeof(pi->fpgaId1)); (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1)); imageStr[14] = '\0'; (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr); assert(sizeof(imageStr) >= sizeof(pi->fpgaId2)); (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2)); imageStr[14] = '\0'; (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr); (void)printf("%-24s : %s\n", "Front Board Serial No", pi->SerialNo[1]); (void)printf("%-24s : %s\n", "Front Board PBA", pi->PbaId[1]); assert(sizeof(imageStr) >= sizeof(pi->AvrId[1])); (void)memcpy(imageStr, pi->AvrId[1], sizeof(pi->AvrId[1])); imageStr[3] = '\0'; (void)printf("%-24s : %s\n", "Front Board AVR", imageStr); } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN3) { (void)printf("%-24s : %s\n", "P/N", pi->ProductId); (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]); (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]); assert(sizeof(imageStr) >= sizeof(pi->AvrId[0])); (void)memcpy(imageStr, pi->AvrId[0], sizeof(pi->AvrId[0])); imageStr[3] = '\0'; (void)printf("%-24s : %s\n", "AVR Version", imageStr); assert(sizeof(imageStr) >= sizeof(pi->fpgaId1)); (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1)); imageStr[14] = '\0'; (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr); assert(sizeof(imageStr) >= sizeof(pi->fpgaId2)); (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2)); imageStr[14] = '\0'; (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr); } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_NTBPE) { (void)printf("%-24s : %s\n", "P/N", pi->ProductId); (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]); (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]); (void)printf("%-24s : %s\n", "Firmware id", pi->AvrId[0]); } else { /* Product/Info type is unknown */ assert(0); return NT_ERROR_INTERNAL_ERROR; } return NT_SUCCESS; } int main(void) { NtInfoStream_t hInfoStream; // Info stream handle NtInfo_t hInfo; // Info handle char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer int status; // Status variable uint8_t numAdapters; // Number of adapters uint8_t i; // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library if((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer); return -1; } // Open the info stream if((status = NT_InfoOpen(&hInfoStream, "ExampleInfo")) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_InfoOpen() failed: %s\n", errorBuffer); return -1; } // Read the system info hInfo.cmd=NT_INFO_CMD_READ_SYSTEM; if((status = NT_InfoRead(hInfoStream, &hInfo)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_InfoRead() failed: %s\n", errorBuffer); return -1; } // Print the system info printf("The system contains %d adapter(s), %d port(s), and %d NUMA node(s)\n", hInfo.u.system.data.numAdapters, hInfo.u.system.data.numPorts, hInfo.u.system.data.numNumaNodes); // Print adapter info numAdapters = hInfo.u.system.data.numAdapters; for(i=0; i< numAdapters; i++) { status = showProductInfo(hInfoStream, i); if (status != NT_SUCCESS) { if (status == NT_ERROR_VPD_FAILED) { (void)printf("Product information does not apply to adapter %u.\n", (unsigned)i); } else { // Get the status code as text (void)NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); (void)fprintf(stderr, "Cannot read product info: %s\n", errorBuffer); return -1; } } hInfo.cmd=NT_INFO_CMD_READ_ADAPTER_V6; hInfo.u.adapter_v6.adapterNo = i; if((status = NT_InfoRead(hInfoStream, &hInfo)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_InfoRead() failed: %s\n", errorBuffer); return -1; } // Print the PCI id and BUS id printf("\n%-24s : %04X:%04X : %04X:%02X:%02X.%X \n", "PCI Identifier", hInfo.u.adapter_v6.data.pciid.s.vendor, hInfo.u.adapter_v6.data.pciid.s.device, hInfo.u.adapter_v6.data.busid.s.domain, hInfo.u.adapter_v6.data.busid.s.bus, hInfo.u.adapter_v6.data.busid.s.device, hInfo.u.adapter_v6.data.busid.s.function); } // Close the info stream if((status = NT_InfoClose(hInfoStream)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_InfoClose() failed: %s\n", errorBuffer); return -1; } // Close down the NTAPI library NT_Done(); printf("\nDone.\n"); return 0; }