sensor_example.c Source File

Reference Documentation

Platform
Intel® PAC
Napatech SmartNIC
Content Type
Reference Information
Capture Software Version
Link™ Capture Software 12.10
Napatech Software Suite: examples/sensor/sensor_example.c Source File
sensor_example.c
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2023 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 "NP-0405 Napatech Software license
18  * agreement.pdf"
19  *
20  * 4. Redistributions of source code must retain this copyright notice,
21  * list of conditions and the following disclaimer.
22  *
23  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTIES, EXPRESS OR
24  * IMPLIED, AND NAPATECH DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY
25  * IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF
26  * FITNESS FOR A PARTICULAR PURPOSE. TO THE EXTENT NOT PROHIBITED BY
27  * APPLICABLE LAW, IN NO EVENT SHALL NAPATECH BE LIABLE FOR PERSONAL INJURY,
28  * OR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES WHATSOEVER,
29  * INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF PROFITS, CORRUPTION OR
30  * LOSS OF DATA, FAILURE TO TRANSMIT OR RECEIVE ANY DATA OR INFORMATION,
31  * BUSINESS INTERRUPTION OR ANY OTHER COMMERCIAL DAMAGES OR LOSSES, ARISING
32  * OUT OF OR RELATED TO YOUR USE OR INABILITY TO USE NAPATECH SOFTWARE OR
33  * SERVICES OR ANY THIRD PARTY SOFTWARE OR APPLICATIONS IN CONJUNCTION WITH
34  * THE NAPATECH SOFTWARE OR SERVICES, HOWEVER CAUSED, REGARDLESS OF THE THEORY
35  * OF LIABILITY (CONTRACT, TORT OR OTHERWISE) AND EVEN IF NAPATECH HAS BEEN
36  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME JURISDICTIONS DO NOT ALLOW
37  * THE EXCLUSION OR LIMITATION OF LIABILITY FOR PERSONAL INJURY, OR OF
38  * INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THIS LIMITATION MAY NOT APPLY TO YOU.
39  *
40  *
41 
42  */
43 
44 /**
45  * @example sensor/sensor_example.c
46  * @section sensor_example_description Description
47  *
48  * This source file is an example of how to use the @ref InfoStream
49  * "Info stream" interface in NTAPI.
50  *
51  * The following NTAPI functions are used:
52  * - @ref NT_Init()
53  * - @ref NT_InfoOpen()
54  * - @ref NT_InfoRead()
55  * - @ref NT_InfoClose()
56  * - @ref NT_Done()
57  * - @ref NT_ExplainError()
58  *
59  * <hr>
60  * @section sensor_example_prerequisites Prerequisites
61  * A working system is needed.
62  *
63  * @section sensor_example_flow Program flow
64  * @{
65  * The following is required to use the @ref InfoStream "Info stream"
66  * interface in NTAPI:
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_InfoOpen() - Open an info stream.
76  * - @ref NT_InfoRead() - Read information.
77  * - @ref NT_InfoClose() - Close the stream when terminating.
78  * - @ref NT_Done() - Close down the NTAPI library.
79  * - @ref NT_ExplainError() - Explain an error code returned by NTAPI functions.
80  *
81  *<hr>
82  * @}
83  */
84 
85 // Include this in order to access the Napatech API
86 #include <nt.h>
87 
88 #if defined(WIN32) || defined(WIN64)
89  #define snprintf(dst, ...) _snprintf_s((dst), _countof(dst), __VA_ARGS__)
90 #endif
91 
92 //
93 //
94 //
95 static void DisplaySensorData(NtInfoSensor_t *pSensor)
96 {
97  float fdivFac;
98  char name[64];
99 
100  switch (pSensor->subType)
101  {
103  snprintf(name, sizeof(name), "%s(%s)", pSensor->name, "OMA");
104  printf("%-14s", name);
105  break;
107  snprintf(name, sizeof(name), "%s(%s)", pSensor->name, "Average");
108  printf("%-14s", name);
109  break;
110  default:
111  printf("%-14s", pSensor->name);
112  break;
113  }
114 
115  switch (pSensor->type)
116  {
118  fdivFac = 10.0f;
119  printf("%-11s", "Temp. [C]");
120  break;
122  fdivFac = 1000.0f;
123  printf("%-11s", "Volt. [V]");
124  break;
126  fdivFac = 1000.0f;
127  printf("%-11s", "Curr. [mA]");
128  break;
130  fdivFac = 10.0f;
131  printf("%-11s", "Power [uW]");
132  break;
134  fdivFac = 10.0f;
135  printf("%-11s", "Power [W]");
136  break;
137  case NT_SENSOR_TYPE_FAN:
138  fdivFac = 1.0f;
139  printf("%-11s", "RPM");
140  break;
141 
142  default:
143  printf("**** ERROR UNKNOWN SENSOR TYPE");
144  return;
145  }
146  if (pSensor->limitLow == NT_SENSOR_NAN) {
147  printf(" ");
148  }
149  else {
150  printf("%8.2f ", (float)pSensor->limitLow / fdivFac);
151  }
152  if (pSensor->limitHigh == NT_SENSOR_NAN) {
153  printf(" ");
154  }
155  else {
156  printf("%8.2f ", (float)pSensor->limitHigh/fdivFac);
157  }
158  if (pSensor->value == NT_SENSOR_NAN) {
159  printf(" ");
160  }
161  else {
162  printf("%8.2f ", (float)pSensor->value/fdivFac);
163  }
164  printf("%8s ", pSensor->state==NT_SENSOR_STATE_ALARM?"Alarm!":"");
165  if (pSensor->valueLowest == NT_SENSOR_NAN) {
166  printf(" ");
167  }
168  else {
169  printf("%8.2f ", (float)pSensor->valueLowest/fdivFac);
170  }
171  if (pSensor->valueHighest == NT_SENSOR_NAN) {
172  printf(" ");
173  }
174  else {
175  printf("%8.2f ", (float)pSensor->valueHighest/fdivFac);
176  }
177  return;
178 }
179 
180 
181 //
182 //
183 //
184 int main(void)
185 {
186  int32_t status = NT_SUCCESS;
187  NtInfoStream_t hInfo;
188  NtInfo_t infoSystem;
189  NtInfo_t infoAdapter;
190  NtInfo_t infoPort;
191  NtInfo_t infoSensor;
192  int adapterCounter;
193  uint32_t sensorCounter;
194  uint32_t port, sensor;
195  int prtPort;
196 
197  char errBuf[NT_ERRBUF_SIZE];
198 
199  // Initialize NTAPI library
200  if ((status = NT_Init(NTAPI_VERSION))) {
201  NT_ExplainError(status, errBuf, sizeof(errBuf));
202  fprintf(stderr, ">>> Error: NT_Init failed. Code %d = %s\n", status, errBuf);
203  return status;
204  }
205 
206  // Open the information stream
207  if ((status = NT_InfoOpen(&hInfo, "Sensor example"))) {
208  NT_ExplainError(status, errBuf, sizeof(errBuf));
209  fprintf(stderr, ">>> Error: NT_InfoOpen failed. Code %d = %s\n", status, errBuf);
210  return status;
211  }
212 
213  // Read system information from info stream
214  infoSystem.cmd = NT_INFO_CMD_READ_SYSTEM;
215  if ((status = NT_InfoRead(hInfo, &infoSystem)) != 0) {
216  NT_ExplainError(status, errBuf, sizeof(errBuf));
217  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
218  return status;
219  }
220 
221  printf("System: %d.%d.%d.%d\n", infoSystem.u.system.data.version.major,
222  infoSystem.u.system.data.version.minor,
223  infoSystem.u.system.data.version.patch,
224  infoSystem.u.system.data.version.tag);
225  printf("Adapters: %2d\n", infoSystem.u.system.data.numAdapters);
226  printf("Ports: %2d\n", infoSystem.u.system.data.numPorts);
227 
228  //
229  //
230  //
231 
232  for (adapterCounter = 0; adapterCounter < (int)infoSystem.u.system.data.numAdapters; adapterCounter++) {
233 
234  infoAdapter.cmd = NT_INFO_CMD_READ_ADAPTER_V6;
235  infoAdapter.u.adapter_v6.adapterNo = (uint8_t) adapterCounter;
236 
237  if ((status = NT_InfoRead(hInfo, &infoAdapter)) != NT_SUCCESS) {
238  NT_ExplainError(status, errBuf, sizeof(errBuf));
239  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
240  goto ErrorExit;
241  }
242 
243  printf("\nAdapter %d: %s\n", infoAdapter.u.adapter_v6.adapterNo, infoAdapter.u.adapter_v6.data.name);
244 
245  printf("%12s ", "");
246  printf(" %-11s %-8s %-8s %-8s %-8s %-8s %-8s\n", "Sensor", "Low", "High", "Current", "Alarm", "Lowest", "Highest");
247  printf("%12s ", "");
248  printf(" %-11s %-8s %-8s %-8s %-8s %-8s %-8s\n", "type", "limit", "limit", "value", "state", "value", "value");
249 
250  for (sensorCounter = 0; sensorCounter < infoAdapter.u.adapter_v6.data.numSensors; sensorCounter++)
251  {
252  infoSensor.cmd = NT_INFO_CMD_READ_SENSOR;
254  infoSensor.u.sensor.sourceIndex = infoAdapter.u.adapter_v6.adapterNo;
255  infoSensor.u.sensor.sensorIndex = sensorCounter;
256  status = NT_InfoRead(hInfo, &infoSensor);
257  if (status == NT_SUCCESS) {
258  if (infoSensor.u.sensor.data.state != NT_SENSOR_STATE_NOT_PRESENT) {
259  DisplaySensorData(&infoSensor.u.sensor.data);
260  printf("\n");
261  }
262  } else {
263  NT_ExplainError(status, errBuf, sizeof(errBuf));
264  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
265  goto ErrorExit;
266  }
267  }
268 
269  for (port = 0; port < infoAdapter.u.adapter_v6.data.numPorts; port++) {
270  infoPort.cmd = NT_INFO_CMD_READ_PORT_V9;
271  infoPort.u.port_v9.portNo = (uint8_t) (port + infoAdapter.u.adapter_v6.data.portOffset);
272  if ((status = NT_InfoRead(hInfo, &infoPort)) != NT_SUCCESS) {
273  NT_ExplainError(status, errBuf, sizeof(errBuf));
274  fprintf(stderr, ">>> Error: NT_InfoRead failed. Code %d = %s\n", status, errBuf);
275  goto ErrorExit;
276  }
277 
278  prtPort = 0;
279  for (sensor = 0; sensor < infoPort.u.port_v9.data.numLevel1Sensors; sensor++) {
280  infoSensor.cmd = NT_INFO_CMD_READ_SENSOR;
282  infoSensor.u.sensor.sourceIndex = port + infoAdapter.u.adapter_v6.data.portOffset;
283  infoSensor.u.sensor.sensorIndex = sensor;
284  status = NT_InfoRead(hInfo, &infoSensor);
285 
286  if (status == NT_SUCCESS) {
287  if (infoSensor.u.sensor.data.state == NT_SENSOR_STATE_NORMAL || infoSensor.u.sensor.data.state == NT_SENSOR_STATE_ALARM) {
288  if (!prtPort){
289  printf("Port %i:\n", infoPort.u.port_v9.portNo);
290  prtPort = 1;
291  }
292  DisplaySensorData(&infoSensor.u.sensor.data);
293  printf("\n");
294  }
295  } else {
296  NT_ExplainError(status, errBuf, sizeof(errBuf));
297  printf("sensor :%i, %s\n", sensor, errBuf);
298  }
299  }
300  }
301  }
302 
303 ErrorExit:
304  if (hInfo != 0) {
305  // Close the info stream
306  NT_InfoClose(hInfo);
307  }
308 
309  // Close down the NTAPI library
310  NT_Done();
311  return 0;
312 }
313 
314 //
315 // EOF
316 //