libiio  1.0
Library for interfacing with IIO devices
iiopp-enum.cpp

This example demonstrates usage of the C++ API to enumerate devices, channels and attributes.

// SPDX-License-Identifier: GPL-2.0-or-later
/*
* libiio - C++ API usage
*
* This example libiio program shows the usage of the C++ API for enumerating
* devices, channels and attributes.
*
* Copyright (c) 2023, DIFITEC GmbH
* Author: Tilman Blumhagen <tilman.blumhagen@difitec.de>
*/
#include <iiopp.h>
#include <iostream>
#include <iomanip>
using namespace iiopp;
using namespace std;
string get(Attr const & att)
{
char value[1024] = {0}; // Flawfinder: ignore
att.read_raw(value, sizeof(value));
return value;
}
void enumerateIioEntities()
{
cout << boolalpha;
ContextPtr context = create_context(nullptr, nullptr);
for (Device device : *context)
{
cout << "Device:" << endl;
cout << " id: " << quoted(string(device.id())) << endl;
cout << " name: ";
if (auto name = device.name())
cout << quoted(string(*name));
cout << endl;
cout << " is trigger: " << device.is_trigger() << endl;
for (auto att : device.attrs)
cout << " attribute " << att.name() << " = " << quoted(get(att)) << endl;
for (auto att : device.debug_attrs)
cout << " debug attribute " << att.name() << " = " << quoted(get(att)) << endl;
for (Channel channel : device)
{
cout << " Channel: " << channel.id() << endl;
cout << " name: ";
if (auto name = channel.name())
cout << quoted(string(*name));
cout << endl;
cout << " is output: " << channel.is_output() << endl;
for (auto att : channel.attrs)
cout << " attribute " << quoted(att.name().c_str()) << " = " << quoted(get(att)) << endl;
}
}
ScanPtr s = scan(nullptr, nullptr);
cout << "scan returned " << s->size() << " results" << endl;
for (ScanResult r : *s)
{
cout << " uri: " << quoted(r.uri().c_str()) << endl;
cout << " description: " << quoted(r.description().c_str()) << endl;
}
}
int main(int argc, char ** argv)
{
try
{
enumerateIioEntities();
}
catch (error & e)
{
cerr << "ERROR " << e.code().value() << ": " << e.what() << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
iiopp::Attr
C++ wrapper for the Attributes C-API.
Definition: iiopp.h:209
iiopp::error
Thrown to report errors.
Definition: iiopp.h:92
iiopp::Channel
C++ wrapper for the Channel C-API.
Definition: iiopp.h:384
iiopp::Device
C++ wrapper for the Device C-API.
Definition: iiopp.h:512
iiopp::value
double value(Channel ch)
Reads the value of a channel by using "input" or "raw" attribute and applying "scale" and "offset" if...
Definition: iiopp.h:707
iiopp::create_context
ContextPtr create_context(iio_context_params *params, const char *uri)
C++ wrapper for iio_create_context.
Definition: iiopp.h:652
iiopp
Public C++ API.
Definition: iiopp.h:59
iiopp::Ptr
Special unique pointer for instances that must be destroyed.
Definition: iiopp.h:310
iiopp.h
Public C++ interface.