info_example.c Source File

Reference Documentation

Platform
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.15
Napatech Software Suite: examples/info/info_example.c Source File
info_example.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2025 Napatech A/S. All Rights Reserved.
4  *
5  * 1. Copying, modification, and distribution of this file, or executable
6  * versions of this file, is governed by the terms of the Napatech Software
7  * license agreement under which this file was made available. If you do not
8  * agree to the terms of the license do not install, copy, access or
9  * otherwise use this file.
10  *
11  * 2. Under the Napatech Software license agreement you are granted a
12  * limited, non-exclusive, non-assignable, copyright license to copy, modify
13  * and distribute this file in conjunction with Napatech SmartNIC's and
14  * similar hardware manufactured or supplied by Napatech A/S.
15  *
16  * 3. The full Napatech Software License Agreement is included in this
17  * distribution, please see "NA-0009 Software License Agreement.pdf"
18  *
19  * 4. Redistributions of source code must retain this copyright notice,
20  * list of conditions and the following disclaimer.
21  *
22  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
23  * IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
24  * IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
25  * FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
26  * APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
27  * OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
28  * INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
29  * LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
30  * BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
31  * OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
32  * SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
33  * THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
34  * OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
36  * THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
37  * INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
38  *
39  *
40 
41  */
42 
43 /**
44  * @example info/info_example.c
45  * @section info_example_description Description
46  *
47  * This source file is an example of how to use the @ref InfoStream
48  * "Info stream" interface in NTAPI.
49  *
50  * The code shows how to retrieve and display basic information about
51  * the installed adapters. The adapter information presented in the
52  * example includes the serial number, version information for the
53  * hardware and FPGA images, and the PCI identifier.
54  *
55  * The following NTAPI functions are used:
56  * - @ref NT_Init()
57  * - @ref NT_InfoOpen()
58  * - @ref NT_InfoRead()
59  * - @ref NT_InfoClose()
60  * - @ref NT_Done()
61  * - @ref NT_ExplainError()
62  *
63  * <hr>
64  * @section info_example_prerequisites Prerequisites
65  * A working system is needed.
66  *
67  * @section info_example_flow Program flow
68  * @{
69  * The following is required to use the @ref InfoStream "Info stream"
70  * interface in NTAPI:
71  * - \#include/nt.h - Applications/Tools only need to include @ref
72  * nt.h to obtain prototypes, macros etc. from NTAPI.
73  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
74  * library. @ref NTAPI_VERSION is a define that describes the version
75  * of the API described in the header files included by @ref
76  * nt.h. NT_Init() will ask the NTAPI library to convert return data
77  * to the @ref NTAPI_VERSION if possible. This will ensure that
78  * applications can run on NTAPI libraries of newer versions.
79  * - @ref NT_InfoOpen() - Open an info stream.
80  * - @ref NT_InfoRead() - Read information.
81  * - @ref NT_InfoClose() - Close the stream when terminating.
82  * - @ref NT_Done() - Close down the NTAPI library.
83  * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions.
84  *
85  *<hr>
86  * @}
87  */
88 
89 // Include this in order to access the Napatech API
90 #include <nt.h>
91 
92 #include <assert.h>
93 
94 static int showProductInfo(struct NtInfoStream_s *hInfoStream,
95  uint8_t adapterNo)
96 {
97  NtInfo_t hProdInfo;
98  /* The file stream_info.h defines 'struct NtInfoProductInfo_s'
99  that provides product information. */
100  const struct NtInfoProductInfo_v4_s *pi = &hProdInfo.u.productInfo_v4.data;
101  int status;
102  char imageStr[1024]; /* Ensure plenty of space */
103 
105  hProdInfo.u.productInfo_v4.adapterNo = adapterNo;
106 
107  status = NT_InfoRead(hInfoStream, &hProdInfo);
108  if (status != NT_SUCCESS)
109  return status;
110 
111  (void)printf("\nAdapter %u Product Information", (unsigned)adapterNo);
112  (void)printf("\n-------------------------------------------\n");
113  if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN1) {
114  (void)printf("%-24s : %s\n", "P/N", pi->ProductId);
115  (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]);
116  (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]);
117  (void)printf("%-24s : %s\n", "CPLD", pi->CpldVersion);
118  assert(sizeof(imageStr) >= sizeof(pi->fpgaId1));
119  (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1));
120  imageStr[14] = '\0';
121  (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr);
122  assert(sizeof(imageStr) >= sizeof(pi->fpgaId2));
123  (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2));
124  imageStr[14] = '\0';
125  (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr);
126  } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN2) {
127  (void)printf("%-24s : %s\n", "P/N", pi->ProductId);
128  (void)printf("%-24s : %s\n", "Main Board Serial No", pi->SerialNo[0]);
129  (void)printf("%-24s : %s\n", "Main Board PBA", pi->PbaId[0]);
130  assert(sizeof(imageStr) >= sizeof(pi->AvrId[0]));
131  (void)memcpy(imageStr, pi->AvrId[0], sizeof(pi->AvrId[0]));
132  imageStr[3] = '\0';
133  (void)printf("%-24s : %s\n", "Main Board AVR", imageStr);
134  assert(sizeof(imageStr) >= sizeof(pi->fpgaId1));
135  (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1));
136  imageStr[14] = '\0';
137  (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr);
138  assert(sizeof(imageStr) >= sizeof(pi->fpgaId2));
139  (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2));
140  imageStr[14] = '\0';
141  (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr);
142  (void)printf("%-24s : %s\n", "Front Board Serial No", pi->SerialNo[1]);
143  (void)printf("%-24s : %s\n", "Front Board PBA", pi->PbaId[1]);
144  assert(sizeof(imageStr) >= sizeof(pi->AvrId[1]));
145  (void)memcpy(imageStr, pi->AvrId[1], sizeof(pi->AvrId[1]));
146  imageStr[3] = '\0';
147  (void)printf("%-24s : %s\n", "Front Board AVR", imageStr);
148  } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_GEN3) {
149  (void)printf("%-24s : %s\n", "P/N", pi->ProductId);
150  (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]);
151  (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]);
152  assert(sizeof(imageStr) >= sizeof(pi->AvrId[0]));
153  (void)memcpy(imageStr, pi->AvrId[0], sizeof(pi->AvrId[0]));
154  imageStr[3] = '\0';
155  (void)printf("%-24s : %s\n", "AVR Version", imageStr);
156  assert(sizeof(imageStr) >= sizeof(pi->fpgaId1));
157  (void)memcpy(imageStr, pi->fpgaId1, sizeof(pi->fpgaId1));
158  imageStr[14] = '\0';
159  (void)printf("%-24s : %s\n", "FPGA flash image #0", imageStr);
160  assert(sizeof(imageStr) >= sizeof(pi->fpgaId2));
161  (void)memcpy(imageStr, pi->fpgaId2, sizeof(pi->fpgaId2));
162  imageStr[14] = '\0';
163  (void)printf("%-24s : %s\n", "FPGA flash image #1", imageStr);
164  (void)printf("%-24s : %u\n", "FPGA image running bank", pi->fpgaImageRunningBank);
165  (void)printf("%-24s : %u\n", "FPGA image primary bank", pi->fpgaImagePrimaryBank);
166  (void)printf("%-24s : %u\n", "FPGA image change state", pi->fpgaImageChangeState);
167  } else if (pi->infoType == NT_PRODUCT_INFO_TYPE_NTBPE) {
168  (void)printf("%-24s : %s\n", "P/N", pi->ProductId);
169  (void)printf("%-24s : %s\n", "Serial No", pi->SerialNo[0]);
170  (void)printf("%-24s : %s\n", "PBA", pi->PbaId[0]);
171  (void)printf("%-24s : %s\n", "Firmware id", pi->AvrId[0]);
172  } else {
173  /* Product/Info type is unknown */
174  assert(0);
175  return NT_ERROR_INTERNAL_ERROR;
176  }
177  return NT_SUCCESS;
178 }
179 
180 int main(void)
181 {
182  NtInfoStream_t hInfoStream; // Info stream handle
183  NtInfo_t hInfo; // Info handle
184  char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer
185  int status; // Status variable
186  uint8_t numAdapters; // Number of adapters
187  uint8_t i;
188 
189  // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library
190  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
191  // Get the status code as text
192  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
193  fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer);
194  return -1;
195  }
196 
197  // Open the info stream
198  if ((status = NT_InfoOpen(&hInfoStream, "ExampleInfo")) != NT_SUCCESS) {
199  // Get the status code as text
200  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
201  fprintf(stderr, "NT_InfoOpen() failed: %s\n", errorBuffer);
202  return -1;
203  }
204 
205  // Read the system info
207  if ((status = NT_InfoRead(hInfoStream, &hInfo)) != NT_SUCCESS) {
208  // Get the status code as text
209  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
210  fprintf(stderr, "NT_InfoRead() failed: %s\n", errorBuffer);
211  return -1;
212  }
213 
214  // Print the system info
215  printf("The system contains %d adapter(s), %d port(s), and %d NUMA node(s)\n",
217 
218  // Print adapter info
219  numAdapters = hInfo.u.system.data.numAdapters;
220 
221  for (i = 0; i < numAdapters; i++) {
222 
223  status = showProductInfo(hInfoStream, i);
224  if (status != NT_SUCCESS) {
225  if (status == NT_ERROR_VPD_FAILED) {
226  (void)printf("Product information does not apply to adapter %u.\n",
227  (unsigned)i);
228  } else {
229  // Get the status code as text
230  (void)NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
231  (void)fprintf(stderr, "Cannot read product info: %s\n", errorBuffer);
232  return -1;
233  }
234  }
235 
237  hInfo.u.adapter_v7.adapterNo = i;
238  if ((status = NT_InfoRead(hInfoStream, &hInfo)) != NT_SUCCESS) {
239  // Get the status code as text
240  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
241  fprintf(stderr, "NT_InfoRead() failed: %s\n", errorBuffer);
242  return -1;
243  }
244 
245  // Print the PCI id and BUS id
246  printf("\n%-24s : %04X:%04X : %04X:%02X:%02X.%X \n",
247  "PCI Identifier",
248  hInfo.u.adapter_v7.data.pciid.s.vendor,
249  hInfo.u.adapter_v7.data.pciid.s.device,
250  hInfo.u.adapter_v7.data.busid.s.domain,
251  hInfo.u.adapter_v7.data.busid.s.bus,
252  hInfo.u.adapter_v7.data.busid.s.device,
253  hInfo.u.adapter_v7.data.busid.s.function);
254  }
255 
256  // Close the info stream
257  if ((status = NT_InfoClose(hInfoStream)) != NT_SUCCESS) {
258  // Get the status code as text
259  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
260  fprintf(stderr, "NT_InfoClose() failed: %s\n", errorBuffer);
261  return -1;
262  }
263 
264  // Close down the NTAPI library
265  NT_Done();
266 
267  printf("\nDone.\n");
268  return 0;
269 }