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