config_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/config/config_example.c Source File
config_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 config/config_example.c
46  * @section config_example_description Description
47  *
48  * This source file is an example of how to use the @ref ConfigStream
49  * "Configuration stream" interface in NTAPI.
50  *
51  * The following NTAPI functions are used:
52  * - @ref NT_Init()
53  * - @ref NT_ConfigOpen()
54  * - @ref NT_ConfigWrite()
55  * - @ref NT_ConfigRead()
56  * - @ref NT_ConfigClose()
57  * - @ref NT_Done()
58  * - @ref NT_ExplainError()
59  *
60  * <hr>
61  * @section config_example_prerequisites Prerequisites
62  * A working system is needed with a Napatech accelerator as adapter 0.
63  *
64  * @section config_example_flow Program flow
65  * @{
66  * The following is required to perform read and write operations on
67  * the @ref ConfigStream "configuration stream":
68  * - \#include/nt.h - Applications/Tools only need to include @ref
69  * nt.h to obtain prototypes, macros etc. from NTAPI.
70  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
71  * library. @ref NTAPI_VERSION is a define that describes the version
72  * of the API described in the header files included by @ref
73  * nt.h. NT_Init() will ask the NTAPI library to convert return data
74  * to the @ref NTAPI_VERSION if possible. This will ensure that
75  * applications can run on NTAPI libraries of newer versions.
76  * - @ref NT_ConfigOpen() - Open a configuration stream.
77  * - @ref NT_ConfigRead() - Read configuration.
78  * - @ref NT_ConfigWrite() - Write configuration.
79  * - @ref NT_ConfigClose() - Close the stream when terminating.
80  * - @ref NT_Done() - Close down the NTAPI library.
81  * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions.
82  *
83  *<hr>
84  * @}
85  */
86 
87 // Include this in order to access the Napatech API
88 #include <nt.h>
89 
90 #if defined(__linux__) || defined(__FreeBSD__)
91  #include <unistd.h> // sleep()
92 #elif defined(WIN32) || defined (WIN64)
93  #include <winsock2.h>
94 #endif
95 
96 
97 int main(void)
98 {
99  NtConfigStream_t hConfigStream; // Configuration stream handle
100  NtConfig_t hConfig; // Configuration handle
101  char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer
102  int status; // Status variable
103 
104  // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library
105  if ((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) {
106  // Get the status code as text
107  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
108  fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer);
109  return -1;
110  }
111 
112  // Open the configuration stream
113  if ((status = NT_ConfigOpen(&hConfigStream, "ExampleConfig")) != NT_SUCCESS) {
114  // Get the status code as text
115  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
116  fprintf(stderr, "NT_ConfigOpen() failed: %s\n", errorBuffer);
117  return -1;
118  }
119 
120  // Read the current timestamp
122  hConfig.u.timestampRead.adapter=0;
123  if ((status = NT_ConfigRead(hConfigStream, &hConfig)) != NT_SUCCESS) {
124  // Get the status code as text
125  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
126  fprintf(stderr, "NT_ConfigRead() failed: %s\n", errorBuffer);
127  return -1;
128  }
129 
130  // Print the current timestamp
131  printf("Adapter 0 time is: %llX type %s.\n",
132  (unsigned long long)hConfig.u.timestampRead.data.ts,
136  hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_UNIX_NANOTIME?"UNIX Nanosecond":"Unknown"));
137 
138  // Set the timestamp to 0
140  hConfig.u.timestampWrite.adapter=0;
141  hConfig.u.timestampWrite.data.bCurrent=0;
142  hConfig.u.timestampWrite.data.ts=0;
143  printf("Setting time to 0.\n");
144  if ((status = NT_ConfigWrite(hConfigStream, &hConfig)) != NT_SUCCESS) {
145  // Get the status code as text
146  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
147  fprintf(stderr, "NT_ConfigWrite() failed: %s\n", errorBuffer);
148  return -1;
149  }
150 
151  // Sleep 1 sec
152 #if defined(__linux__) || defined(__FreeBSD__)
153  sleep(1);
154 #elif defined(WIN32) || defined (WIN64)
155  Sleep(1000); // sleep 1000 milliseconds = 1 second
156 #endif
157 
158  // Read the timestamp again
160  hConfig.u.timestampRead.adapter=0;
161  if ((status = NT_ConfigRead(hConfigStream, &hConfig)) != NT_SUCCESS) {
162  // Get the status code as text
163  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
164  fprintf(stderr, "NT_ConfigRead() failed: %s\n", errorBuffer);
165  return -1;
166  }
167 
168  printf("Sleep 1 sec.\n");
169  printf("Adapter 0 time is: %llX type %s.\n",
170  (unsigned long long)hConfig.u.timestampRead.data.ts,
174  hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_UNIX_NANOTIME?"UNIX Nanosecond":"Unknown"));
175 
176  // Close the configuration stream
177  if ((status = NT_ConfigClose(hConfigStream)) != NT_SUCCESS) {
178  // Get the status code as text
179  NT_ExplainError(status, errorBuffer, sizeof(errorBuffer));
180  fprintf(stderr, "NT_ConfigClose() failed: %s\n", errorBuffer);
181  return -1;
182  }
183 
184  // Close down the NTAPI library
185  NT_Done();
186 
187  printf("Done.\n");
188  return 0;
189 }