Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
metadata.hpp
Go to the documentation of this file.
1/*
2 __ __ _ _ _ _
3 | \/ | ___| |_ __ _ __| | __ _| |_ __ _ ___| | __ _ ___ ___
4 | |\/| |/ _ \ __/ _` |/ _` |/ _` | __/ _` | / __| |/ _` / __/ __|
5 | | | | __/ || (_| | (_| | (_| | || (_| | | (__| | (_| \__ \__ \
6 |_| |_|\___|\__\__,_|\__,_|\__,_|\__\__,_| \___|_|\__,_|___/___/
7
8This class is a pure frontend class: it is expected to stream metadata, as
9triggers, events, and any other field-related information.
10
11Author(s): Paolo Bosetti
12*/
13
14#ifndef METADATA_HPP
15#define METADATA_HPP
16
17
18#include "mads.hpp"
19#include "agent.hpp"
20#include <vector>
21#ifdef USE_GPIOD_2
22#include <gpiod.hpp>
23#else
24#include <gpiod.h>
25#endif
26
27namespace Mads {
28
36class Metadata : public Agent {
37public:
44 Metadata(std::string name, std::string settings_path)
45 : Agent(name, settings_path) {
46 load_settings();
47 setup_lines();
48 }
49
50
51#ifdef USE_GPIOD_2
52
53 ~Metadata() {
54 _lines.release();
55 _chip.reset();
56 }
57
64 bool read_lines() {
65 bool changed = false;
66 for (auto &line : _lines) {
67 auto off = line.offset();
68 auto value = line.get_value();
69 if (value != _values[to_string(off)]) {
70 changed = true;
71 _values[to_string(off)] = value;
72 }
73 }
74 return changed;
75 }
76
77
78#else // USE_GPIOD_2
79
81 for (auto &[off, line] : _lines) {
82 gpiod_line_release(line);
83 }
84 gpiod_chip_close(_chip);
85 }
86
87 bool read_lines() {
88 bool changed = false;
89 for (auto &[off, line] : _lines) {
90 int val = gpiod_line_get_value(line);
91 if (val != _values[to_string(off)]) {
92 changed = true;
93 }
94 _values[to_string(off)] = val;
95 }
96 return changed;
97 }
98
99#endif // USE_GPIOD_2
100
109 nlohmann::json values() { return _values; }
110
111 void info(ostream &out = cout) override {
112 Agent::info(out);
113 out << " GPIO chip: " << style::bold << _chip_path << style::reset
114 << endl;
115 out << " GPIO lines: " << style::bold;
116 for (auto &line : _offsets) {
117 out << line << " ";
118 }
119 out << style::reset << endl;
120 out << " GPIOD version: " << style::bold << gpiod_version_string()
121 << style::reset << endl;
122 }
123
124private:
131 void load_settings() override {
132 auto cfg = _config[_name];
133 _chip_number = cfg["gpio_chip"].value_or(0);
134 _chip_path = cfg["gpio_chip_path"].value_or("/dev/gpiochip0");
135 if (cfg["gpio_lines"].type() == toml::node_type::integer) {
136 _offsets.push_back(cfg["gpio_lines"].value_or(0));
137 } else if (cfg["gpio_lines"].type() == toml::node_type::array) {
138 toml::array *a = cfg["gpio_lines"].as_array();
139 a->for_each([&](auto &&el) { _offsets.push_back(el.value_or(0)); });
140 }
141 }
142
143
144#ifdef USE_GPIOD_2
145
146 void setup_lines() {
147 _chip = gpiod::chip(_chip_path);
148 _lines = _chip.get_lines(_offsets);
149 _lines.request({_name.c_str(), ::gpiod::line_request::DIRECTION_INPUT, 0});
150 }
151
152#else // USE_GPIOD_2
153
154 void setup_lines() {
155 int ret = 0;
156 _chip = gpiod_chip_open_by_number(_chip_number);
157 for (auto &off : _offsets) {
158 struct gpiod_line *line = gpiod_chip_get_line(_chip, off);
159 ret = gpiod_line_request_input(line, _name.c_str());
160 if (ret) {
161 cerr << "Error reserving GPIO line " << off << ": " << strerror(errno)
162 << endl;
163 continue;
164 }
165 _lines[off] = line;
166 }
167 }
168
169#endif // USE_GPIOD_2
170
171 int _chip_number = 0;
172 string _chip_path = "/dev/gpiochip0";
173 vector<unsigned int> _offsets;
174 nlohmann::json _values;
175#ifdef USE_GPIOD_2
176 gpiod::chip _chip;
177 gpiod::line_bulk _lines;
178#else
179 struct gpiod_chip *_chip = NULL;
180 map<int, struct gpiod_line *> _lines;
181#endif
182};
183
184} // namespace Mads
185
186#endif // METADATA_HPP
toml::table _config
Definition agent.hpp:1289
std::string name()
Returns the name of the agent.
std::string _name
Definition agent.hpp:1286
virtual void info(std::ostream &out=std::cout)
Prints information about the agent.
The Metadata class represents metadata for an agent.
Definition metadata.hpp:36
bool read_lines()
Definition metadata.hpp:87
void info(ostream &out=cout) override
Prints information about the agent.
Definition metadata.hpp:111
Metadata(std::string name, std::string settings_path)
Constructs a Metadata object with the given name and settings path.
Definition metadata.hpp:44
nlohmann::json values()
Returns the values read from the GPIO lines.
Definition metadata.hpp:109
Definition agent.hpp:67