|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * libiio - C++ API usage |
| 4 | + * |
| 5 | + * This example libiio program shows the usage of the C++ API for enumerating |
| 6 | + * devices, channels and attributes. |
| 7 | + * |
| 8 | + * Copyright (c) 2023, DIFITEC GmbH |
| 9 | + * Author: Tilman Blumhagen <[email protected]> |
| 10 | + */ |
| 11 | + |
| 12 | +#include <iiopp.h> |
| 13 | + |
| 14 | +#include <iostream> |
| 15 | +#include <iomanip> |
| 16 | + |
| 17 | +using namespace iiopp; |
| 18 | +using namespace std; |
| 19 | + |
| 20 | +string get(IAttr const & att) |
| 21 | +{ |
| 22 | + char value[1024] = {0}; // Flawfinder: ignore |
| 23 | + att.read(value, sizeof(value)); // Flawfinder: ignore |
| 24 | + return value; |
| 25 | +} |
| 26 | + |
| 27 | +void enumerateIioEntities() |
| 28 | +{ |
| 29 | + cout << boolalpha; |
| 30 | + |
| 31 | + shared_ptr<Context> context = create_default_context(); |
| 32 | + |
| 33 | + for (Device device : *context) |
| 34 | + { |
| 35 | + cout << "Device:" << endl; |
| 36 | + cout << " id: " << quoted(string(device.id())) << endl; |
| 37 | + |
| 38 | + cout << " name: "; |
| 39 | + if (auto name = device.name()) |
| 40 | + cout << quoted(string(*name)); |
| 41 | + cout << endl; |
| 42 | + |
| 43 | + cout << " is trigger: " << device.is_trigger() << endl; |
| 44 | + |
| 45 | + for (auto att : device.attrs) |
| 46 | + cout << " attribute " << att.name() << " = " << quoted(get(att)) << endl; |
| 47 | + |
| 48 | + for (auto att : device.debug_attrs) |
| 49 | + cout << " debug attribute " << att.name() << " = " << quoted(get(att)) << endl; |
| 50 | + |
| 51 | + for (auto att : device.buffer_attrs) |
| 52 | + cout << " buffer attribute " << att.name() << " = " << quoted(get(att)) << endl; |
| 53 | + |
| 54 | + for (Channel channel : device) |
| 55 | + { |
| 56 | + cout << " Channel: " << channel.id() << endl; |
| 57 | + |
| 58 | + cout << " name: "; |
| 59 | + if (auto name = channel.name()) |
| 60 | + cout << quoted(string(*name)); |
| 61 | + cout << endl; |
| 62 | + |
| 63 | + cout << " is output: " << channel.is_output() << endl; |
| 64 | + cout << " is enabled: " << channel.is_enabled() << endl; |
| 65 | + |
| 66 | + for (auto att : channel.attrs) |
| 67 | + cout << " attribute " << quoted(att.name().c_str()) << " = " << quoted(get(att)) << endl; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + shared_ptr<ScanContext> scanCtx = create_scan_context({}, 0); |
| 72 | + shared_ptr<ScanContext::InfoList> infoList = scanCtx->info_list(); |
| 73 | + |
| 74 | + cout << "scan context info list:" << endl; |
| 75 | + for (ContextInfo info : *infoList) |
| 76 | + { |
| 77 | + cout << " uri: " << quoted(info.uri().c_str()) << endl; |
| 78 | + cout << " description: " << quoted(info.desciption().c_str()) << endl; |
| 79 | + } |
| 80 | + |
| 81 | + cout << "scan block:" << endl; |
| 82 | + shared_ptr<ScanBlock> blk = create_scan_block({}, 0); |
| 83 | + for (ContextInfo info : *blk) |
| 84 | + { |
| 85 | + cout << " uri: " << quoted(info.uri().c_str()) << endl; |
| 86 | + cout << " description: " << quoted(info.desciption().c_str()) << endl; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +int main(int argc, char ** argv) |
| 91 | +{ |
| 92 | + try |
| 93 | + { |
| 94 | + enumerateIioEntities(); |
| 95 | + } |
| 96 | + catch (std::exception & e) |
| 97 | + { |
| 98 | + cerr << "ERROR: " << e.what() << endl; |
| 99 | + return EXIT_FAILURE; |
| 100 | + } |
| 101 | + |
| 102 | + return EXIT_SUCCESS; |
| 103 | +} |
0 commit comments