]> sigrok.org Git - pulseview.git/blame - pv/data/decode/decoder.cpp
Allow more than 256 binary output classes
[pulseview.git] / pv / data / decode / decoder.cpp
CommitLineData
7491a29f
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7491a29f
JH
18 */
19
943edd76
MC
20#include <cassert>
21
8ce0e732
SA
22#include <QDebug>
23
fe3a1c21 24#include <libsigrokcxx/libsigrokcxx.hpp>
7491a29f
JH
25#include <libsigrokdecode/libsigrokdecode.h>
26
2acdb232 27#include "decoder.hpp"
7491a29f 28
04394ded 29#include <pv/data/signalbase.hpp>
27a3f09b 30#include <pv/data/decodesignal.hpp>
7491a29f 31
819f4c25
JH
32using std::map;
33using std::string;
7491a29f
JH
34
35namespace pv {
36namespace data {
37namespace decode {
38
39Decoder::Decoder(const srd_decoder *const dec) :
8dbbc7f0 40 decoder_(dec),
8ce0e732
SA
41 shown_(true),
42 decoder_inst_(nullptr)
7491a29f 43{
e77de61f 44 // Query the decoder outputs
ac9494ef 45 uint32_t i = 0;
e77de61f
SA
46 for (GSList *l = dec->binary; l; l = l->next) {
47 char **bin_class = (char**)l->data;
48 char *name = bin_class[0];
49 char *desc = bin_class[1];
50 bin_classes_.push_back({i++, name, desc});
51 }
7491a29f
JH
52}
53
54Decoder::~Decoder()
55{
27c05210 56 for (auto& option : options_)
da50281d 57 g_variant_unref(option.second);
7491a29f
JH
58}
59
60const srd_decoder* Decoder::decoder() const
61{
8dbbc7f0 62 return decoder_;
7491a29f
JH
63}
64
e77de61f
SA
65const char* Decoder::name() const
66{
67 return decoder_->name;
68}
69
dd048a7e
JH
70bool Decoder::shown() const
71{
8dbbc7f0 72 return shown_;
dd048a7e
JH
73}
74
75void Decoder::show(bool show)
76{
8dbbc7f0 77 shown_ = show;
dd048a7e
JH
78}
79
27a3f09b 80const vector<DecodeChannel*>& Decoder::channels() const
7491a29f 81{
8dbbc7f0 82 return channels_;
7491a29f
JH
83}
84
27a3f09b 85void Decoder::set_channels(vector<DecodeChannel*> channels)
7491a29f 86{
8dbbc7f0 87 channels_ = channels;
7491a29f
JH
88}
89
6f925ba9 90const map<string, GVariant*>& Decoder::options() const
7491a29f 91{
8dbbc7f0 92 return options_;
7491a29f
JH
93}
94
95void Decoder::set_option(const char *id, GVariant *value)
96{
615f6d25 97 assert(value);
7491a29f 98 g_variant_ref(value);
8dbbc7f0 99 options_[id] = value;
8ce0e732
SA
100
101 // If we have a decoder instance, apply option value immediately
72486b78
SA
102 apply_all_options();
103}
104
105void Decoder::apply_all_options()
106{
8ce0e732
SA
107 if (decoder_inst_) {
108 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
109 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
110
c5f47334
SA
111 for (const auto& option : options_) {
112 GVariant *const value = option.second;
113 g_variant_ref(value);
114 g_hash_table_replace(opt_hash, (void*)g_strdup(
115 option.first.c_str()), value);
116 }
8ce0e732
SA
117
118 srd_inst_option_set(decoder_inst_, opt_hash);
119 g_hash_table_destroy(opt_hash);
120 }
7491a29f
JH
121}
122
6ac6242b 123bool Decoder::have_required_channels() const
a2d4b551 124{
27a3f09b
SA
125 for (DecodeChannel *ch : channels_)
126 if (!ch->assigned_signal && !ch->is_optional)
a2d4b551 127 return false;
a2d4b551
JH
128
129 return true;
130}
131
8ce0e732 132srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
7491a29f 133{
615f6d25
JH
134 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
135 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
136
27c05210 137 for (const auto& option : options_) {
da50281d 138 GVariant *const value = option.second;
615f6d25
JH
139 g_variant_ref(value);
140 g_hash_table_replace(opt_hash, (void*)g_strdup(
da50281d 141 option.first.c_str()), value);
615f6d25
JH
142 }
143
8ce0e732
SA
144 if (decoder_inst_)
145 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
146
147 decoder_inst_ = srd_inst_new(session, decoder_->id, opt_hash);
615f6d25
JH
148 g_hash_table_destroy(opt_hash);
149
8ce0e732 150 if (!decoder_inst_)
4c60462b 151 return nullptr;
7491a29f 152
6ac6242b 153 // Setup the channels
f5295834 154 GArray *const init_pin_states = g_array_sized_new(false, true,
9f97b357
SA
155 sizeof(uint8_t), channels_.size());
156
157 g_array_set_size(init_pin_states, channels_.size());
158
6ac6242b 159 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
7491a29f
JH
160 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
161
27a3f09b 162 for (DecodeChannel *ch : channels_) {
6e7a4a00
SA
163 if (!ch->assigned_signal)
164 continue;
165
27a3f09b 166 init_pin_states->data[ch->id] = ch->initial_pin_state;
9f97b357 167
6e7a4a00 168 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
7491a29f 169 g_variant_ref_sink(gvar);
6e7a4a00 170 // key is channel name (pdch->id), value is bit position in each sample (gvar)
27a3f09b 171 g_hash_table_insert(channels, ch->pdch_->id, gvar);
7491a29f
JH
172 }
173
8ce0e732 174 srd_inst_channel_set_all(decoder_inst_, channels);
7491a29f 175
8ce0e732 176 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
f5295834 177 g_array_free(init_pin_states, true);
407c9ebe 178
8ce0e732
SA
179 return decoder_inst_;
180}
181
182void Decoder::invalidate_decoder_inst()
183{
184 decoder_inst_ = nullptr;
7491a29f
JH
185}
186
ac9494ef 187uint32_t Decoder::get_binary_class_count() const
e77de61f
SA
188{
189 return bin_classes_.size();
190}
191
ac9494ef 192const DecodeBinaryClassInfo* Decoder::get_binary_class(uint32_t id) const
e77de61f
SA
193{
194 return &(bin_classes_.at(id));
195}
196
870ea3db
UH
197} // namespace decode
198} // namespace data
199} // namespace pv