]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/decode/decoder.cpp
Annotation: Use special type for the class, not plain int
[pulseview.git] / pv / data / decode / decoder.cpp
... / ...
CommitLineData
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
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <cassert>
21
22#include <QDebug>
23
24#include <libsigrokcxx/libsigrokcxx.hpp>
25#include <libsigrokdecode/libsigrokdecode.h>
26
27#include "decoder.hpp"
28
29#include <pv/data/signalbase.hpp>
30#include <pv/data/decodesignal.hpp>
31
32using pv::data::DecodeChannel;
33using std::map;
34using std::string;
35
36namespace pv {
37namespace data {
38namespace decode {
39
40Decoder::Decoder(const srd_decoder *const dec) :
41 decoder_(dec),
42 shown_(true),
43 decoder_inst_(nullptr)
44{
45}
46
47Decoder::~Decoder()
48{
49 for (auto& option : options_)
50 g_variant_unref(option.second);
51}
52
53const srd_decoder* Decoder::decoder() const
54{
55 return decoder_;
56}
57
58bool Decoder::shown() const
59{
60 return shown_;
61}
62
63void Decoder::show(bool show)
64{
65 shown_ = show;
66}
67
68const vector<DecodeChannel*>& Decoder::channels() const
69{
70 return channels_;
71}
72
73void Decoder::set_channels(vector<DecodeChannel*> channels)
74{
75 channels_ = channels;
76}
77
78const map<string, GVariant*>& Decoder::options() const
79{
80 return options_;
81}
82
83void Decoder::set_option(const char *id, GVariant *value)
84{
85 assert(value);
86 g_variant_ref(value);
87 options_[id] = value;
88
89 // If we have a decoder instance, apply option value immediately
90 if (decoder_inst_) {
91 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
92 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
93
94 for (const auto& option : options_) {
95 GVariant *const value = option.second;
96 g_variant_ref(value);
97 g_hash_table_replace(opt_hash, (void*)g_strdup(
98 option.first.c_str()), value);
99 }
100
101 srd_inst_option_set(decoder_inst_, opt_hash);
102 g_hash_table_destroy(opt_hash);
103 }
104}
105
106bool Decoder::have_required_channels() const
107{
108 for (DecodeChannel *ch : channels_)
109 if (!ch->assigned_signal && !ch->is_optional)
110 return false;
111
112 return true;
113}
114
115srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
116{
117 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
118 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
119
120 for (const auto& option : options_) {
121 GVariant *const value = option.second;
122 g_variant_ref(value);
123 g_hash_table_replace(opt_hash, (void*)g_strdup(
124 option.first.c_str()), value);
125 }
126
127 if (decoder_inst_)
128 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
129
130 decoder_inst_ = srd_inst_new(session, decoder_->id, opt_hash);
131 g_hash_table_destroy(opt_hash);
132
133 if (!decoder_inst_)
134 return nullptr;
135
136 // Setup the channels
137 GArray *const init_pin_states = g_array_sized_new(false, true,
138 sizeof(uint8_t), channels_.size());
139
140 g_array_set_size(init_pin_states, channels_.size());
141
142 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
143 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
144
145 for (DecodeChannel *ch : channels_) {
146 if (!ch->assigned_signal)
147 continue;
148
149 init_pin_states->data[ch->id] = ch->initial_pin_state;
150
151 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
152 g_variant_ref_sink(gvar);
153 // key is channel name (pdch->id), value is bit position in each sample (gvar)
154 g_hash_table_insert(channels, ch->pdch_->id, gvar);
155 }
156
157 srd_inst_channel_set_all(decoder_inst_, channels);
158
159 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
160 g_array_free(init_pin_states, true);
161
162 return decoder_inst_;
163}
164
165void Decoder::invalidate_decoder_inst()
166{
167 decoder_inst_ = nullptr;
168}
169
170} // namespace decode
171} // namespace data
172} // namespace pv