info_example.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Go to the documentation of this file.

1/* 2 * %NT_SOFTWARE_LICENSE% 3 */ 4 5/** 6 * @example info/info_example.c 7 * @section info_example_description Description 8 * 9 * This source file is an example of how to use the @ref InfoStream 10 * "Info stream" interface in NTAPI. 11 * 12 * The code shows how to retrieve and display basic information about 13 * the installed adapters. The adapter information presented in the 14 * example includes the serial number, version information for the 15 * hardware and FPGA images, and the PCI identifier. 16 * 17 * The following NTAPI functions are used: 18 * - @ref NT_Init() 19 * - @ref NT_InfoOpen() 20 * - @ref NT_InfoRead() 21 * - @ref NT_InfoClose() 22 * - @ref NT_Done() 23 * - @ref NT_ExplainError() 24 * 25 * <hr> 26 * @section info_example_prerequisites Prerequisites 27 * A working system is needed. 28 * 29 * @section info_example_flow Program flow 30 * @{ 31 * The following is required to use the @ref InfoStream "Info stream" 32 * interface in NTAPI: 33 * - \#include/nt.h - Applications/Tools only need to include @ref 34 * nt.h to obtain prototypes, macros etc. from NTAPI. 35 * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI 36 * library. @ref NTAPI_VERSION is a define that describes the version 37 * of the API described in the header files included by @ref 38 * nt.h. NT_Init() will ask the NTAPI library to convert return data 39 * to the @ref NTAPI_VERSION if possible. This will ensure that 40 * applications can run on NTAPI libraries of newer versions. 41 * - @ref NT_InfoOpen() - Open an info stream. 42 * - @ref NT_InfoRead() - Read information. 43 * - @ref NT_InfoClose() - Close the stream when terminating. 44 * - @ref NT_Done() - Close down the NTAPI library. 45 * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions. 46 * 47 *<hr> 48 * @} 49 */ 50 51#include <nt.h> 52#include <assert.h> 53 54static int showProductInfo(const NtInfoStream_t hInfoStream, uint8_t adapterNo) 55{ 56 NtInfo_t hProdInfo; 57 /* The file stream_info.h defines 'struct NtInfoProductInfo_s' 58 that provides product information. */ 59 const struct NtInfoProductInfo_v2_s *pi = &hProdInfo.u.productInfo_v2.data; 60 int status; 61 char imageStr[1024]; /* Ensure plenty of space */ 62 63 hProdInfo.cmd = NT_INFO_CMD_READ_PRODUCT_INFO_V2; 64 hProdInfo.u.productInfo_v2.adapterNo = adapterNo; 65 66 status = NT_InfoRead(hInfoStream, &hProdInfo); 67 if (status != NT_SUCCESS) 68 return status; 69 70 (void)printf("\nAdapter %u Product Information", (unsigned)adapterNo); 71 (void)printf("\n-------------------------------------------\n"); 72 if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN1) { 73 (void)printf("%-24s : %s\n", "P/N", pi->ProductId); 74 (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]); 75 (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]); 76 (void)printf("%-24s : %s\n", "CPLD", pi->CpldVersion); 77 assert(sizeof(imageStr) >= sizeof(pi->fpgaId1)); 78 (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1)); 79 imageStr[14] = '\0'; 80 (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr); 81 assert(sizeof(imageStr) >= sizeof(pi->fpgaId2)); 82 (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2)); 83 imageStr[14] = '\0'; 84 (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr); 85 } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN2) { 86 (void)printf("%-24s : %s\n", "P/N", pi->ProductId); 87 (void)printf("%-24s : %s\n", "Main Board Serial No", pi->SerialNo[0]); 88 (void)printf("%-24s : %s\n", "Main Board PBA", pi->PbaId[0]); 89 assert(sizeof(imageStr) >= sizeof(pi->AvrId[0])); 90 (void)memcpy(imageStr, pi->AvrId[0], sizeof(pi->AvrId[0])); 91 imageStr[3] = '\0'; 92 (void)printf("%-24s : %s\n", "Main Board AVR", imageStr); 93 assert(sizeof(imageStr) >= sizeof(pi->fpgaId1)); 94 (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1)); 95 imageStr[14] = '\0'; 96 (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr); 97 assert(sizeof(imageStr) >= sizeof(pi->fpgaId2)); 98 (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2)); 99 imageStr[14] = '\0'; 100 (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr); 101 (void)printf("%-24s : %s\n", "Front Board Serial No", pi->SerialNo[1]); 102 (void)printf("%-24s : %s\n", "Front Board PBA", pi->PbaId[1]); 103 assert(sizeof(imageStr) >= sizeof(pi->AvrId[1])); 104 (void)memcpy(imageStr, pi->AvrId[1], sizeof(pi->AvrId[1])); 105 imageStr[3] = '\0'; 106 (void)printf("%-24s : %s\n", "Front Board AVR", imageStr); 107 } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN3) { 108 (void)printf("%-24s : %s\n", "P/N", pi->ProductId); 109 (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]); 110 (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]); 111 assert(sizeof(imageStr) >= sizeof(pi->AvrId[0])); 112 (void)memcpy(imageStr, pi->AvrId[0], sizeof(pi->AvrId[0])); 113 imageStr[3] = '\0'; 114 (void)printf("%-24s : %s\n", "AVR Version", imageStr); 115 assert(sizeof(imageStr) >= sizeof(pi->fpgaId1)); 116 (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1)); 117 imageStr[14] = '\0'; 118 (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr); 119 assert(sizeof(imageStr) >= sizeof(pi->fpgaId2)); 120 (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2)); 121 imageStr[14] = '\0'; 122 (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr); 123 } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_NTBPE) { 124 (void)printf("%-24s : %s\n", "P/N", pi->ProductId); 125 (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]); 126 (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]); 127 (void)printf("%-24s : %s\n", "Firmware id", pi->AvrId[0]); 128 } else { 129 /* Product/Info type is unknown */ 130 assert(0); 131 return NT_ERROR_INTERNAL_ERROR; 132 } 133 return NT_SUCCESS; 134} 135 136int main(void) 137{ 138 NtInfoStream_t hInfoStream; // Info stream handle 139 NtInfo_t hInfo; // Info handle 140 char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer 141 int status; // Status variable 142 uint8_t numAdapters; // Number of adapters 143 uint8_t i; 144 145 // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library 146 if((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) { 147 // Get the status code as text 148 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 149 fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer); 150 return -1; 151 } 152 153 // Open the info stream 154 if((status = NT_InfoOpen(&hInfoStream, "ExampleInfo")) != NT_SUCCESS) { 155 // Get the status code as text 156 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 157 fprintf(stderr, "NT_InfoOpen() failed: %s\n", errorBuffer); 158 return -1; 159 } 160 161 // Read the system info 162 hInfo.cmd=NT_INFO_CMD_READ_SYSTEM; 163 if((status = NT_InfoRead(hInfoStream, &hInfo)) != NT_SUCCESS) { 164 // Get the status code as text 165 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 166 fprintf(stderr, "NT_InfoRead() failed: %s\n", errorBuffer); 167 return -1; 168 } 169 170 // Print the system info 171 printf("The system contains %d adapter(s), %d port(s), and %d NUMA node(s)\n", 172 hInfo.u.system.data.numAdapters, hInfo.u.system.data.numPorts, hInfo.u.system.data.numNumaNodes); 173 174 // Print adapter info 175 numAdapters = hInfo.u.system.data.numAdapters; 176 177 for(i=0; i< numAdapters; i++) { 178 179 status = showProductInfo(hInfoStream, i); 180 if (status != NT_SUCCESS) { 181 if (status == NT_ERROR_VPD_FAILED) { 182 (void)printf("Product information does not apply to adapter %u.\n", 183 (unsigned)i); 184 } else { 185 // Get the status code as text 186 (void)NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 187 (void)fprintf(stderr, "Cannot read product info: %s\n", errorBuffer); 188 return -1; 189 } 190 } 191 192 hInfo.cmd=NT_INFO_CMD_READ_ADAPTER_V6; 193 hInfo.u.adapter_v6.adapterNo = i; 194 if((status = NT_InfoRead(hInfoStream, &hInfo)) != NT_SUCCESS) { 195 // Get the status code as text 196 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 197 fprintf(stderr, "NT_InfoRead() failed: %s\n", errorBuffer); 198 return -1; 199 } 200 201 // Print the PCI id and BUS id 202 printf("\n%-24s : %04X:%04X : %04X:%02X:%02X.%X \n", 203 "PCI Identifier", 204 hInfo.u.adapter_v6.data.pciid.s.vendor, 205 hInfo.u.adapter_v6.data.pciid.s.device, 206 hInfo.u.adapter_v6.data.busid.s.domain, 207 hInfo.u.adapter_v6.data.busid.s.bus, 208 hInfo.u.adapter_v6.data.busid.s.device, 209 hInfo.u.adapter_v6.data.busid.s.function); 210 } 211 212 // Close the info stream 213 if((status = NT_InfoClose(hInfoStream)) != NT_SUCCESS) { 214 // Get the status code as text 215 NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); 216 fprintf(stderr, "NT_InfoClose() failed: %s\n", errorBuffer); 217 return -1; 218 } 219 220 // Close down the NTAPI library 221 NT_Done(); 222 223 printf("\nDone.\n"); 224 return 0; 225}