sensor_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/sensor/sensor_example.c Source File
sensor_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 sensor/sensor_example.c
45  * @section sensor_example_description Description
46  *
47  * This source file is an example of how to use the @ref InfoStream
48  * "Info stream" interface in NTAPI.
49  *
50  * The following NTAPI functions are used:
51  * - @ref NT_Init()
52  * - @ref NT_InfoOpen()
53  * - @ref NT_InfoRead()
54  * - @ref NT_InfoClose()
55  * - @ref NT_Done()
56  * - @ref NT_ExplainError()
57  *
58  * <hr>
59  * @section sensor_example_prerequisites Prerequisites
60  * A working system is needed.
61  *
62  * @section sensor_example_flow Program flow
63  * @{
64  * The following is required to use the @ref InfoStream "Info stream"
65  * interface in NTAPI:
66  * - \#include/nt.h - Applications/Tools only need to include @ref
67  * nt.h to obtain prototypes, macros etc. from NTAPI.
68  * - @ref NT_Init(@ref NTAPI_VERSION) - Initialize the NTAPI
69  * library. @ref NTAPI_VERSION is a define that describes the version
70  * of the API described in the header files included by @ref
71  * nt.h. NT_Init() will ask the NTAPI library to convert return data
72  * to the @ref NTAPI_VERSION if possible. This will ensure that
73  * applications can run on NTAPI libraries of newer versions.
74  * - @ref NT_InfoOpen() - Open an info stream.
75  * - @ref NT_InfoRead() - Read information.
76  * - @ref NT_InfoClose() - Close the stream when terminating.
77  * - @ref NT_Done() - Close down the NTAPI library.
78  * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions.
79  *
80  *<hr>
81  * @}
82  */
83 
84 // Include this in order to access the Napatech API
85 #include <nt.h>
86 
87 #if defined(WIN32) || defined(WIN64)
88  #define snprintf(dst, ...) _snprintf_s((dst), _countof(dst), __VA_ARGS__)
89 #endif
90 
91 //
92 //
93 //
94 static void DisplaySensorData(NtInfoSensor_t *pSensor)
95 {
96  float fdivFac;
97  char name[64];
98 
99  switch (pSensor->subType)
100  {
102  snprintf(name, sizeof(name), "%s(%s)", pSensor->name, "OMA");
103  printf("%-14s", name);
104  break;
106  snprintf(name, sizeof(name), "%s(%s)", pSensor->name, "Average");
107  printf("%-14s", name);
108  break;
109  default:
110  printf("%-14s", pSensor->name);
111  break;
112  }
113 
114  switch (pSensor->type)
115  {
117  fdivFac = 10.0f;
118  printf("%-11s", "Temp. [C]");
119  break;
121  fdivFac = 1000.0f;
122  printf("%-11s", "Volt. [V]");
123  break;
125  fdivFac = 1000.0f;
126  printf("%-11s", "Curr. [mA]");
127  break;
129  fdivFac = 10.0f;
130  printf("%-11s", "Power [uW]");
131  break;
133  fdivFac = 1.0f;
134  printf("%-11s", "Power [mW]");
135  break;
136  case NT_SENSOR_TYPE_FAN:
137  fdivFac = 1.0f;
138  printf("%-11s", "RPM");
139  break;
140 
141  default:
142  printf("**** ERROR UNKNOWN SENSOR TYPE");
143  return;
144  }
145  if (pSensor->limitLow == NT_SENSOR_NAN) {
146  printf(" ");
147  }
148  else {
149  printf("%8.2f ", (float)pSensor->limitLow / fdivFac);
150  }
151  if (pSensor->limitHigh == NT_SENSOR_NAN) {
152  printf(" ");
153  }
154  else {
155  printf("%8.2f ", (float)pSensor->limitHigh/fdivFac);
156  }
157  if (pSensor->value == NT_SENSOR_NAN) {
158  printf(" ");
159  }
160  else {
161  printf("%8.2f ", (float)pSensor->value/fdivFac);
162  }
163  printf("%8s ", pSensor->state==NT_SENSOR_STATE_ALARM?"Alarm!":"");
164  if (pSensor->valueLowest == NT_SENSOR_NAN) {
165  printf(" ");
166  }
167  else {
168  printf("%8.2f ", (float)pSensor->valueLowest/fdivFac);
169  }
170  if (pSensor->valueHighest == NT_SENSOR_NAN) {
171  printf(" ");
172  }
173  else {
174  printf("%8.2f ", (float)pSensor->valueHighest/fdivFac);
175  }
176  return;
177 }
178 
179 
180 //
181 //
182 //
183 int main(void)
184 {
185  int32_t status = NT_SUCCESS;
186  NtInfoStream_t hInfo;
187  NtInfo_t infoSystem;
188  NtInfo_t infoAdapter;
189  NtInfo_t infoPort;
190  NtInfo_t infoSensor;
191  int adapterCounter;
192  uint32_t sensorCounter;
193  uint32_t port, sensor;
194  int prtPort;
195 
196  char errBuf[NT_ERRBUF_SIZE];
197 
198  // Initialize NTAPI library
199  if ((status = NT_Init(NTAPI_VERSION))) {
200  NT_ExplainError(status, errBuf, sizeof(errBuf));
201  fprintf(stderr, ">>> Error: NT_Init failed. Code %d = %s\n", status, errBuf);
202  return status;
203  }
204 
205  // Open the information stream
206  if ((status = NT_InfoOpen(&hInfo, "Sensor example"))) {
207  NT_ExplainError(status, errBuf, sizeof(errBuf));
208  fprintf(stderr, ">>> Error: NT_InfoOpen failed. Code %d = %s\n", status, errBuf);
209  return status;
210  }
211 
212  // Read system information from info stream
213  infoSystem.cmd = NT_INFO_CMD_READ_SYSTEM;
214  if ((status = NT_InfoRead(hInfo, &infoSystem)) != 0) {
215  NT_ExplainError(status, errBuf, sizeof(errBuf));
216  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
217  return status;
218  }
219 
220  printf("System: %d.%d.%d.%d\n", infoSystem.u.system.data.version.major,
221  infoSystem.u.system.data.version.minor,
222  infoSystem.u.system.data.version.patch,
223  infoSystem.u.system.data.version.tag);
224  printf("Adapters: %2d\n", infoSystem.u.system.data.numAdapters);
225  printf("Ports: %2d\n", infoSystem.u.system.data.numPorts);
226 
227  //
228  //
229  //
230 
231  for (adapterCounter = 0; adapterCounter < (int)infoSystem.u.system.data.numAdapters; adapterCounter++) {
232 
233  infoAdapter.cmd = NT_INFO_CMD_READ_ADAPTER_V7;
234  infoAdapter.u.adapter_v7.adapterNo = (uint8_t) adapterCounter;
235 
236  if ((status = NT_InfoRead(hInfo, &infoAdapter)) != NT_SUCCESS) {
237  NT_ExplainError(status, errBuf, sizeof(errBuf));
238  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
239  goto ErrorExit;
240  }
241 
242  printf("\nAdapter %d: %s\n", infoAdapter.u.adapter_v7.adapterNo, infoAdapter.u.adapter_v7.data.name);
243 
244  printf("%12s ", "");
245  printf(" %-11s %-8s %-8s %-8s %-8s %-8s %-8s\n", "Sensor", "Low", "High", "Current", "Alarm", "Lowest", "Highest");
246  printf("%12s ", "");
247  printf(" %-11s %-8s %-8s %-8s %-8s %-8s %-8s\n", "type", "limit", "limit", "value", "state", "value", "value");
248 
249  for (sensorCounter = 0; sensorCounter < infoAdapter.u.adapter_v7.data.numSensors; sensorCounter++)
250  {
251  infoSensor.cmd = NT_INFO_CMD_READ_SENSOR;
253  infoSensor.u.sensor.sourceIndex = infoAdapter.u.adapter_v7.adapterNo;
254  infoSensor.u.sensor.sensorIndex = sensorCounter;
255  status = NT_InfoRead(hInfo, &infoSensor);
256  if (status == NT_SUCCESS) {
257  if (infoSensor.u.sensor.data.state != NT_SENSOR_STATE_NOT_PRESENT) {
258  DisplaySensorData(&infoSensor.u.sensor.data);
259  printf("\n");
260  }
261  } else {
262  NT_ExplainError(status, errBuf, sizeof(errBuf));
263  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
264  goto ErrorExit;
265  }
266  }
267 
268  for (port = 0; port < infoAdapter.u.adapter_v7.data.numPorts; port++) {
269  infoPort.cmd = NT_INFO_CMD_READ_PORT_V10;
270  infoPort.u.port_v10.portNo = (uint8_t) (port + infoAdapter.u.adapter_v7.data.portOffset);
271  if ((status = NT_InfoRead(hInfo, &infoPort)) != NT_SUCCESS) {
272  NT_ExplainError(status, errBuf, sizeof(errBuf));
273  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
274  goto ErrorExit;
275  }
276 
277  prtPort = 0;
278  for (sensor = 0; sensor < infoPort.u.port_v10.data.numLevel1Sensors; sensor++) {
279  infoSensor.cmd = NT_INFO_CMD_READ_SENSOR;
281  infoSensor.u.sensor.sourceIndex = port + infoAdapter.u.adapter_v7.data.portOffset;
282  infoSensor.u.sensor.sensorIndex = sensor;
283  status = NT_InfoRead(hInfo, &infoSensor);
284 
285  if (status == NT_SUCCESS) {
286  if (infoSensor.u.sensor.data.state == NT_SENSOR_STATE_NORMAL || infoSensor.u.sensor.data.state == NT_SENSOR_STATE_ALARM) {
287  if (!prtPort){
288  printf("Port %i:\n", infoPort.u.port_v10.portNo);
289  prtPort = 1;
290  }
291  DisplaySensorData(&infoSensor.u.sensor.data);
292  printf("\n");
293  }
294  } else {
295  NT_ExplainError(status, errBuf, sizeof(errBuf));
296  printf("sensor :%i, %s\n", sensor, errBuf);
297  }
298  }
299  }
300  }
301 
302 ErrorExit:
303  if (hInfo != 0) {
304  // Close the info stream
305  NT_InfoClose(hInfo);
306  }
307 
308  // Close down the NTAPI library
309  NT_Done();
310  return 0;
311 }
312 
313 //
314 // EOF
315 //