info_example.c Source File

Reference Documentation

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