info/info_example.c

Reference Documentation

Platform
Intel® PAC
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.10
Napatech Software Suite: info/info_example.c
info/info_example.c

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.

The following NTAPI functions are used:


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.

/*
*
* Copyright 2023 Napatech A/S. All Rights Reserved.
*
* 1. Copying, modification, and distribution of this file, or executable
* versions of this file, is governed by the terms of the Napatech Software
* license agreement under which this file was made available. If you do not
* agree to the terms of the license do not install, copy, access or
* otherwise use this file.
*
* 2. Under the Napatech Software license agreement you are granted a
* limited, non-exclusive, non-assignable, copyright license to copy, modify
* and distribute this file in conjunction with Napatech SmartNIC's and
* similar hardware manufactured or supplied by Napatech A/S.
*
* 3. The full Napatech Software license agreement is included in this
* distribution, please see "NP-0405 Napatech Software license
* agreement.pdf"
*
* 4. Redistributions of source code must retain this copyright notice,
* list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
* IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
* IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
* FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
* APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
* OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
* INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
* LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
* BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
* OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
* SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
* THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
* OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
* THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
* INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
*
*
*/
/**
* @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 this in order to access the Napatech API
#include <nt.h>
#include <assert.h>
static int showProductInfo(struct NtInfoStream_s *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 */
status = NT_InfoRead(hInfoStream, &hProdInfo);
if (status != NT_SUCCESS)
return status;
(void)printf("\nAdapter %u Product Information", (unsigned)adapterNo);
(void)printf("\n-------------------------------------------\n");
(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
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",
// 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;
}
}
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",
}
// 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
printf("\nDone.\n");
return 0;
}