sensor/sensor_example.c
Description
This source file is an example of how to use the Info stream interface in NTAPI.
The following NTAPI functions are used:
Prerequisites
A working system is needed.
Program flow
The following is required to use the Info stream interface in NTAPI:
- #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_InfoOpen() - Open an info stream.
- NT_InfoRead() - Read information.
- NT_InfoClose() - Close the stream when terminating.
- NT_Done() - Close down the NTAPI library.
- NT_ExplainError() - Explain an error code returned by NTAPI functions.
/*
*
* Copyright 2024 Napatech A/S. All Rights Reserved.
*
* 1. Copying, modification, and distribution of this file, or executable
* versions of this file, is governed by the terms of the Napatech Software
* license agreement under which this file was made available. If you do not
* agree to the terms of the license do not install, copy, access or
* otherwise use this file.
*
* 2. Under the Napatech Software license agreement you are granted a
* limited, non-exclusive, non-assignable, copyright license to copy, modify
* and distribute this file in conjunction with Napatech SmartNIC's and
* similar hardware manufactured or supplied by Napatech A/S.
*
* 3. The full Napatech Software License Agreement is included in this
* distribution, please see "NA-0009 Software License Agreement.pdf"
*
* 4. Redistributions of source code must retain this copyright notice,
* list of conditions and the following disclaimer.
*
* THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
* IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
* IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
* FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
* APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
* OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
* INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
* LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
* BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
* OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
* SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
* THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
* OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
* THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
* INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
*
*
*/
/**
* @example sensor/sensor_example.c
* @section sensor_example_description Description
*
* This source file is an example of how to use the @ref InfoStream
* "Info stream" interface in NTAPI.
*
* The following NTAPI functions are used:
* - @ref NT_Init()
* - @ref NT_InfoOpen()
* - @ref NT_InfoRead()
* - @ref NT_InfoClose()
* - @ref NT_Done()
* - @ref NT_ExplainError()
*
* <hr>
* @section sensor_example_prerequisites Prerequisites
* A working system is needed.
*
* @section sensor_example_flow Program flow
* @{
* The following is required to use the @ref InfoStream "Info stream"
* interface in NTAPI:
* - \#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_InfoOpen() - Open an info stream.
* - @ref NT_InfoRead() - Read information.
* - @ref NT_InfoClose() - 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 this in order to access the Napatech API
#include <nt.h>
#if defined(WIN32) || defined(WIN64)
#define snprintf(dst, ...) _snprintf_s((dst), _countof(dst), __VA_ARGS__)
#endif
//
//
//
{
float fdivFac;
char name[64];
{
printf("%-14s", name);
break;
printf("%-14s", name);
break;
default:
printf("%-14s", pSensor->name);
break;
}
{
fdivFac = 10.0f;
printf("%-11s", "Temp. [C]");
break;
fdivFac = 1000.0f;
printf("%-11s", "Volt. [V]");
break;
fdivFac = 1000.0f;
printf("%-11s", "Curr. [mA]");
break;
fdivFac = 10.0f;
printf("%-11s", "Power [uW]");
break;
fdivFac = 1.0f;
printf("%-11s", "Power [mW]");
break;
fdivFac = 1.0f;
printf("%-11s", "RPM");
break;
default:
printf("**** ERROR UNKNOWN SENSOR TYPE");
return;
}
printf(" ");
}
else {
}
printf(" ");
}
else {
}
printf(" ");
}
else {
}
printf(" ");
}
else {
}
printf(" ");
}
else {
}
return;
}
//
//
//
{
int32_t status = NT_SUCCESS;
NtInfoStream_t hInfo;
NtInfo_t infoSystem;
NtInfo_t infoAdapter;
NtInfo_t infoPort;
NtInfo_t infoSensor;
int adapterCounter;
uint32_t sensorCounter;
uint32_t port, sensor;
int prtPort;
// Initialize NTAPI library
fprintf(stderr, ">>> Error: NT_Init failed. Code %d = %s\n", status, errBuf);
return status;
}
// Open the information stream
NT_ExplainError(status, errBuf, sizeof(errBuf));
fprintf(stderr, ">>> Error: NT_InfoOpen failed. Code %d = %s\n", status, errBuf);
return status;
}
// Read system information from info stream
NT_ExplainError(status, errBuf, sizeof(errBuf));
fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
return status;
}
//
//
//
for (adapterCounter = 0; adapterCounter < (int)infoSystem.u.system.data.numAdapters; adapterCounter++) {
if ((status = NT_InfoRead(hInfo, &infoAdapter)) != NT_SUCCESS) {
NT_ExplainError(status, errBuf, sizeof(errBuf));
fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
goto ErrorExit;
}
printf("\nAdapter %d: %s\n", infoAdapter.u.adapter_v6.adapterNo, infoAdapter.u.adapter_v6.data.name);
printf("%12s ", "");
printf(" %-11s %-8s %-8s %-8s %-8s %-8s %-8s\n", "Sensor", "Low", "High", "Current", "Alarm", "Lowest", "Highest");
printf("%12s ", "");
printf(" %-11s %-8s %-8s %-8s %-8s %-8s %-8s\n", "type", "limit", "limit", "value", "state", "value", "value");
{
status = NT_InfoRead(hInfo, &infoSensor);
if (status == NT_SUCCESS) {
printf("\n");
}
} else {
NT_ExplainError(status, errBuf, sizeof(errBuf));
fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
goto ErrorExit;
}
}
if ((status = NT_InfoRead(hInfo, &infoPort)) != NT_SUCCESS) {
NT_ExplainError(status, errBuf, sizeof(errBuf));
fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
goto ErrorExit;
}
prtPort = 0;
infoSensor.cmd = NT_INFO_CMD_READ_SENSOR;
status = NT_InfoRead(hInfo, &infoSensor);
if (status == NT_SUCCESS) {
if (infoSensor.u.sensor.data.state == NT_SENSOR_STATE_NORMAL || infoSensor.u.sensor.data.state == NT_SENSOR_STATE_ALARM) {
if (!prtPort){
prtPort = 1;
}
printf("\n");
}
} else {
NT_ExplainError(status, errBuf, sizeof(errBuf));
printf("sensor :%i, %s\n", sensor, errBuf);
}
}
}
}
ErrorExit:
if (hInfo != 0) {
// Close the info stream
NT_InfoClose(hInfo);
}
// Close down the NTAPI library
NT_Done();
return 0;
}
//
// EOF
//