config/config_example.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Description

This source file is an example of how to use the Configuration stream interface in NTAPI.

Prerequisites

A working system is needed with a Napatech accelerator as adapter 0.

Program flow

The following is required to perform read and write operations on the configuration stream:
  • #include/nt.h - Applications/Tools only need to include nt.h to obtain prototypes, macros etc. from NTAPI.

  • NT_Init(NTAPI_VERSION) - Initialize the NTAPI library. NTAPI_VERSION is a define that describes the version of the API described in the header files included by nt.h. NT_Init() will ask the NTAPI library to convert return data to the NTAPI_VERSION if possible. This will ensure that applications can run on NTAPI libraries of newer versions.

  • NT_ConfigOpen() - Open a configuration stream.

  • NT_ConfigRead() - Read configuration.

  • NT_ConfigWrite() - Write configuration.

  • NT_ConfigClose() - Close the stream when terminating.

  • NT_Done() - Close down the NTAPI library.

  • NT_ExplainError() - Explain an error code returned by NTAPI functions.

/* * %NT_SOFTWARE_LICENSE% */ /** * @example config/config_example.c * @section config_example_description Description * * This source file is an example of how to use the @ref ConfigStream * "Configuration stream" interface in NTAPI. * * The following NTAPI functions are used: * - @ref NT_Init() * - @ref NT_ConfigOpen() * - @ref NT_ConfigWrite() * - @ref NT_ConfigRead() * - @ref NT_ConfigClose() * - @ref NT_Done() * - @ref NT_ExplainError() * * <hr> * @section config_example_prerequisites Prerequisites * A working system is needed with a Napatech accelerator as adapter 0. * * @section config_example_flow Program flow * @{ * The following is required to perform read and write operations on * the @ref ConfigStream "configuration stream": * - \#include/nt.h - Applications/Tools only need to include @ref * nt.h to obtain prototypes, macros etc. from NTAPI. * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI * library. @ref NTAPI_VERSION is a define that describes the version * of the API described in the header files included by @ref * nt.h. NT_Init() will ask the NTAPI library to convert return data * to the @ref NTAPI_VERSION if possible. This will ensure that * applications can run on NTAPI libraries of newer versions. * - @ref NT_ConfigOpen() - Open a configuration stream. * - @ref NT_ConfigRead() - Read configuration. * - @ref NT_ConfigWrite() - Write configuration. * - @ref NT_ConfigClose() - Close the stream when terminating. * - @ref NT_Done() - Close down the NTAPI library. * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions. * *<hr> * @} */ #include <nt.h> #if defined(__linux__) || defined(__FreeBSD__) #include <unistd.h> // sleep() #elif defined(WIN32) || defined (WIN64) #include <winsock2.h> #endif int main(void) { NtConfigStream_t hConfigStream; // Configuration stream handle NtConfig_t hConfig; // Configuration handle char errorBuffer[NT_ERRBUF_SIZE]; // Error buffer int status; // Status variable // Initialize the NTAPI library and thereby check if NTAPI_VERSION can be used together with this library if((status = NT_Init(NTAPI_VERSION)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_Init() failed: %s\n", errorBuffer); return -1; } // Open the configuration stream if((status = NT_ConfigOpen(&hConfigStream, "ExampleConfig")) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_ConfigOpen() failed: %s\n", errorBuffer); return -1; } // Read the current timestamp hConfig.parm=NT_CONFIG_PARM_ADAPTER_TIMESTAMP; hConfig.u.timestampRead.adapter=0; if((status = NT_ConfigRead(hConfigStream, &hConfig)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_ConfigRead() failed: %s\n", errorBuffer); return -1; } // Print the current timestamp printf("Adapter 0 time is: %llX type %s.\n", (unsigned long long)hConfig.u.timestampRead.data.ts, (hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_NATIVE?"Native": hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_NATIVE_NDIS?"Native NDIS": hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_NATIVE_UNIX?"Native UNIX": hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_UNIX_NANOTIME?"UNIX Nanosecond":"Unknown")); // Set the timestamp to 0 hConfig.parm=NT_CONFIG_PARM_ADAPTER_TIMESTAMP; hConfig.u.timestampWrite.adapter=0; hConfig.u.timestampWrite.data.bCurrent=0; hConfig.u.timestampWrite.data.ts=0; printf("Setting time to 0.\n"); if((status = NT_ConfigWrite(hConfigStream, &hConfig)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_ConfigWrite() failed: %s\n", errorBuffer); return -1; } // Sleep 1 sec #if defined(__linux__) || defined(__FreeBSD__) sleep(1); #elif defined(WIN32) || defined (WIN64) Sleep(1000); // sleep 1000 milliseconds = 1 second #endif // Read the timestamp again hConfig.parm=NT_CONFIG_PARM_ADAPTER_TIMESTAMP; hConfig.u.timestampRead.adapter=0; if((status = NT_ConfigRead(hConfigStream, &hConfig)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_ConfigRead() failed: %s\n", errorBuffer); return -1; } printf("Sleep 1 sec.\n"); printf("Adapter 0 time is: %llX type %s.\n", (unsigned long long)hConfig.u.timestampRead.data.ts, (hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_NATIVE?"Native": hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_NATIVE_NDIS?"Native NDIS": hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_NATIVE_UNIX?"Native UNIX": hConfig.u.timestampRead.data.tsType==NT_TIMESTAMP_TYPE_UNIX_NANOTIME?"UNIX Nanosecond":"Unknown")); // Close the configuration stream if((status = NT_ConfigClose(hConfigStream)) != NT_SUCCESS) { // Get the status code as text NT_ExplainError(status, errorBuffer, sizeof(errorBuffer)); fprintf(stderr, "NT_ConfigClose() failed: %s\n", errorBuffer); return -1; } // Close down the NTAPI library NT_Done(); printf("Done.\n"); return 0; }