μHAL
decoders.h
1#ifndef DECODERS_H
2#define DECODERS_H
3
4#include <cstdint>
5#include <cstdio>
6#include <memory>
7#include <optional>
8#include <string_view>
9#include <unordered_map>
10#include <utility>
11#include <variant>
12#include <vector>
13
14#include <sys/types.h>
15
16#include "sdb-defs.h"
17
18#define LNLS_VENDORID 0x1000000000001215
19
21class Printer;
22
23#define CONSTRUCTOR_REGS(type) regs_storage(new type ()), regs(*regs_storage)
24
25namespace decoders {
29 using data_type = std::variant<std::int32_t, double>;
30
31 using data_key = std::pair<std::string_view, std::optional<unsigned>>;
32}
33
39 bool devinfo_is_set = false;
40
41 uint64_t vendor_id;
42 uint32_t device_id;
43 uint8_t major_version;
47 bool check_devinfo = false;
48
49 bool match_devinfo(const struct sdb_device_info &) const;
50
51 protected:
52 size_t read_size;
53 void *read_dest;
54
55 void set_read_dest(auto &dest)
56 {
57 read_dest = &dest;
58 read_size = sizeof dest;
59 }
60
61 struct pcie_bars &bars;
62 struct sdb_device_info devinfo;
63 size_t addr;
64
67 RegisterDecoderBase(struct pcie_bars &, const struct sdb_device_info &);
68
70 virtual void read();
71
72 public:
73 void check_devinfo_is_set() const;
74 virtual void set_devinfo(const struct sdb_device_info &);
75
76 const device_match_fn match_devinfo_lambda;
77};
78
80 decoders::data_type value;
81 size_t offset;
82 uint32_t mask;
83 unsigned fixed_point_pos = 0;
84 bool multibit;
85 bool is_signed;
86 bool is_fixed_point = false;
87};
88
98 bool is_boolean_value(const char *) const;
99 int32_t try_boolean_value(const char *, int32_t) const;
100
101 std::unique_ptr<RegisterDecoderPrivate> pvt;
102
103 template <class T>
104 void add_data_internal(const char *, decoders::data_key::second_type, T);
105
106 void rf_add_data_internal(const char *, decoders::data_key::second_type, RegisterField);
107
108 size_t register2offset(uint32_t *);
109 uint32_t *offset2register(size_t, void *);
110
111 void write_internal(const char *, std::optional<unsigned>, decoders::data_type, void *);
112
113 protected:
116 std::optional<unsigned> number_of_channels;
117
118 std::unordered_map<std::string_view, Printer> printers;
119
120 RegisterDecoder(struct pcie_bars &, const struct sdb_device_info &, std::unordered_map<std::string_view, Printer>);
121
123 void add_general(const char *, int32_t);
125 void add_general_double(const char *, double);
128 void add_channel(const char *, unsigned, int32_t);
131 void add_channel_double(const char *, unsigned, double);
132
136 RegisterField rf_get_bit(uint32_t &, uint32_t);
138 RegisterField rf_extract_value(uint32_t &, uint32_t, bool=false);
141 RegisterField rf_whole_register(uint32_t &value, bool is_signed=false)
142 {
143 return rf_extract_value(value, UINT32_MAX, is_signed);
144 }
147
149 inline void add_general(const char *name, RegisterField rf)
150 {
151 rf_add_data_internal(name, std::nullopt, rf);
152 }
154 inline void add_channel(const char *name, unsigned pos, RegisterField rf)
155 {
156 rf_add_data_internal(name, pos, rf);
157 }
158
161 virtual void read_monitors();
163 virtual void decode() = 0;
166 virtual void decode_monitors();
167
168 public:
169 virtual ~RegisterDecoder();
172 void get_data(bool=false);
173 void binary_dump(FILE *) const;
174 virtual void print(FILE *, bool) const;
175
176 template <class T>
177 T get_general_data(const char *name) const
178 {
179 return std::get<T>(get_generic_data(name));
180 }
181 template <class T>
182 T get_channel_data(const char *name, unsigned channel_index) const
183 {
184 return std::get<T>(get_generic_data(name, channel_index));
185 }
186
187 decoders::data_type get_generic_data(const char *, decoders::data_key::second_type=std::nullopt) const;
188
189 std::optional<unsigned> channel;
190
191 inline void write_general(const char *name, decoders::data_type value, void *dest)
192 {
193 write_internal(name, std::nullopt, value, dest);
194 }
195 inline void write_channel(const char *name, unsigned pos, decoders::data_type value, void *dest)
196 {
197 write_internal(name, pos, value, dest);
198 }
199};
200
201#endif
Definition: printer.h:24
Definition: decoders.h:36
bool check_devinfo
Definition: decoders.h:47
RegisterDecoderBase(struct pcie_bars &, const struct sdb_device_info &)
Definition: decoderbase.cc:6
bool devinfo_is_set
Definition: decoders.h:39
virtual void read()
Definition: decoderbase.cc:35
Definition: decoders.h:97
virtual void decode_monitors()
Definition: decoders.cc:155
RegisterField rf_whole_register(uint32_t &value, bool is_signed=false)
Definition: decoders.h:141
void add_general(const char *name, RegisterField rf)
Definition: decoders.h:149
void add_general(const char *, int32_t)
Definition: decoders.cc:80
virtual void read_monitors()
Definition: decoders.cc:150
virtual void decode()=0
void get_data(bool=false)
Definition: decoders.cc:160
RegisterField rf_fixed2float(RegisterField, unsigned)
Definition: decoders.cc:135
RegisterField rf_extract_value(uint32_t &, uint32_t, bool=false)
Definition: decoders.cc:124
RegisterField rf_get_bit(uint32_t &, uint32_t)
Definition: decoders.cc:113
std::optional< unsigned > number_of_channels
Definition: decoders.h:116
void add_general_double(const char *, double)
Definition: decoders.cc:84
void add_channel_double(const char *, unsigned, double)
Definition: decoders.cc:93
void add_channel(const char *name, unsigned pos, RegisterField rf)
Definition: decoders.h:154
void add_channel(const char *, unsigned, int32_t)
Definition: decoders.cc:89
Definition: decoders.cc:27
Definition: decoders.h:79
Definition: pcie-defs.h:19
Definition: sdb-defs.h:7