sensor/sensor_example.c

Reference Documentation

product_line_custom
Napatech SmartNIC
category
Reference Information

Description

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

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.

/* * %NT_SOFTWARE_LICENSE% */ /** * @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 <nt.h> #if defined(WIN32) || defined (WIN64) //#define snprintf _snprintf #define snprintf(a, b, c, d, e) _snprintf_s((a), _countof(a), (b), (c), (d), (e)) #endif // // // static void DisplaySensorData(NtInfoSensor_t *pSensor) { float fdivFac; char name[64]; switch (pSensor->subType) { case NT_SENSOR_SUBTYPE_POWER_OMA: snprintf(name, 64, "%s(%s)", pSensor->name, "OMA"); printf("%-14s", name); break; case NT_SENSOR_SUBTYPE_POWER_AVERAGE: snprintf(name, 64, "%s(%s)", pSensor->name, "Average"); printf("%-14s", name); break; default: printf("%-14s", pSensor->name); break; } switch (pSensor->type) { case NT_SENSOR_TYPE_TEMPERATURE: fdivFac = 10.0f; printf("%-11s", "Temp. [C]"); break; case NT_SENSOR_TYPE_VOLTAGE: fdivFac = 100.0f; printf("%-11s", "Volt. [V]"); break; case NT_SENSOR_TYPE_CURRENT: fdivFac = 1000.0f; printf("%-11s", "Curr. [mA]"); break; case NT_SENSOR_TYPE_POWER: fdivFac = 10.0f; printf("%-11s", "Power [uW]"); break; case NT_SENSOR_TYPE_HIGH_POWER: fdivFac = 10.0f; printf("%-11s", "Power [W]"); break; case NT_SENSOR_TYPE_FAN: fdivFac = 1.0f; printf("%-11s", "RPM"); break; default: printf("**** ERROR UNKNOWN SENSOR TYPE"); return; } if (pSensor->limitLow == NT_SENSOR_NAN) { printf(" "); } else { printf("%8.2f ", (float)pSensor->limitLow / fdivFac); } if (pSensor->limitHigh == NT_SENSOR_NAN) { printf(" "); } else { printf("%8.2f ", (float)pSensor->limitHigh/fdivFac); } if (pSensor->value == NT_SENSOR_NAN) { printf(" "); } else { printf("%8.2f ", (float)pSensor->value/fdivFac); } printf("%8s ", pSensor->state==NT_SENSOR_STATE_ALARM?"Alarm!":""); if (pSensor->valueLowest == NT_SENSOR_NAN) { printf(" "); } else { printf("%8.2f ", (float)pSensor->valueLowest/fdivFac); } if (pSensor->valueHighest == NT_SENSOR_NAN) { printf(" "); } else { printf("%8.2f ", (float)pSensor->valueHighest/fdivFac); } return; } // // // int main(void) { int32_t status = NT_SUCCESS; NtInfoStream_t hInfo; NtInfo_t infoSystem; NtInfo_t infoAdapter; NtInfo_t infoPort; NtInfo_t infoSensor; int adapterCounter; int sensorCounter; int port, sensor; int prtPort; char errBuf[NT_ERRBUF_SIZE]; // Initialize NTAPI library if ((status = NT_Init(NTAPI_VERSION))) { NT_ExplainError(status, errBuf, sizeof(errBuf)); fprintf(stderr, ">>> Error: NT_Init failed. Code %d = %s\n", status, errBuf); return status; } // Open the information stream if((status = NT_InfoOpen(&hInfo, "Sensor example"))) { 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 infoSystem.cmd = NT_INFO_CMD_READ_SYSTEM; if ((status = NT_InfoRead(hInfo, &infoSystem)) != 0) { NT_ExplainError(status, errBuf, sizeof(errBuf)); fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf); return status; } printf("System: %d.%d.%d.%d\n", infoSystem.u.system.data.version.major, infoSystem.u.system.data.version.minor, infoSystem.u.system.data.version.patch, infoSystem.u.system.data.version.tag); printf("Adapters: %2d\n", infoSystem.u.system.data.numAdapters); printf("Ports: %2d\n", infoSystem.u.system.data.numPorts); // // // for(adapterCounter = 0; adapterCounter < (int)infoSystem.u.system.data.numAdapters; adapterCounter++) { infoAdapter.cmd = NT_INFO_CMD_READ_ADAPTER_V6; infoAdapter.u.adapter_v6.adapterNo = (uint8_t) 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"); for (sensorCounter = 0; sensorCounter < (int)infoAdapter.u.adapter_v6.data.numSensors; sensorCounter++) { infoSensor.cmd = NT_INFO_CMD_READ_SENSOR; infoSensor.u.sensor.source = NT_SENSOR_SOURCE_ADAPTER; infoSensor.u.sensor.sourceIndex = infoAdapter.u.adapter_v6.adapterNo; infoSensor.u.sensor.sensorIndex = sensorCounter; status = NT_InfoRead(hInfo, &infoSensor); if (status == NT_SUCCESS) { if (infoSensor.u.sensor.data.state != NT_SENSOR_STATE_NOT_PRESENT) { DisplaySensorData(&infoSensor.u.sensor.data); printf("\n"); } } else { NT_ExplainError(status, errBuf, sizeof(errBuf)); fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf); goto ErrorExit; } } for (port = 0; port < (int)infoAdapter.u.adapter_v6.data.numPorts; port++) { infoPort.cmd = NT_INFO_CMD_READ_PORT_V9; infoPort.u.port_v9.portNo = (uint8_t) (port + infoAdapter.u.adapter_v6.data.portOffset); 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; for (sensor = 0; sensor < (int)infoPort.u.port_v9.data.numLevel1Sensors; sensor++) { infoSensor.cmd = NT_INFO_CMD_READ_SENSOR; infoSensor.u.sensor.source = NT_SENSOR_SOURCE_LEVEL1_PORT; infoSensor.u.sensor.sourceIndex = port + infoAdapter.u.adapter_v6.data.portOffset; infoSensor.u.sensor.sensorIndex = 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){ printf("Port %i:\n", infoPort.u.port_v9.portNo); prtPort = 1; } DisplaySensorData(&infoSensor.u.sensor.data); 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 //