]> sigrok.org Git - pulseview.git/blame - pv/data/decode/decoder.cpp
DecodeSignal: Fix race condition
[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
JH
43{
44}
45
46Decoder::~Decoder()
47{
27c05210 48 for (auto& option : options_)
da50281d 49 g_variant_unref(option.second);
7491a29f
JH
50}
51
52const srd_decoder* Decoder::decoder() const
53{
8dbbc7f0 54 return decoder_;
7491a29f
JH
55}
56
dd048a7e
JH
57bool Decoder::shown() const
58{
8dbbc7f0 59 return shown_;
dd048a7e
JH
60}
61
62void Decoder::show(bool show)
63{
8dbbc7f0 64 shown_ = show;
dd048a7e
JH
65}
66
27a3f09b 67const vector<DecodeChannel*>& Decoder::channels() const
7491a29f 68{
8dbbc7f0 69 return channels_;
7491a29f
JH
70}
71
27a3f09b 72void Decoder::set_channels(vector<DecodeChannel*> channels)
7491a29f 73{
8dbbc7f0 74 channels_ = channels;
7491a29f
JH
75}
76
6f925ba9 77const map<string, GVariant*>& Decoder::options() const
7491a29f 78{
8dbbc7f0 79 return options_;
7491a29f
JH
80}
81
82void Decoder::set_option(const char *id, GVariant *value)
83{
615f6d25 84 assert(value);
7491a29f 85 g_variant_ref(value);
8dbbc7f0 86 options_[id] = value;
8ce0e732
SA
87
88 // If we have a decoder instance, apply option value immediately
72486b78
SA
89 apply_all_options();
90}
91
92void Decoder::apply_all_options()
93{
8ce0e732
SA
94 if (decoder_inst_) {
95 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
96 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
97
c5f47334
SA
98 for (const auto& option : options_) {
99 GVariant *const value = option.second;
100 g_variant_ref(value);
101 g_hash_table_replace(opt_hash, (void*)g_strdup(
102 option.first.c_str()), value);
103 }
8ce0e732
SA
104
105 srd_inst_option_set(decoder_inst_, opt_hash);
106 g_hash_table_destroy(opt_hash);
107 }
7491a29f
JH
108}
109
6ac6242b 110bool Decoder::have_required_channels() const
a2d4b551 111{
27a3f09b
SA
112 for (DecodeChannel *ch : channels_)
113 if (!ch->assigned_signal && !ch->is_optional)
a2d4b551 114 return false;
a2d4b551
JH
115
116 return true;
117}
118
8ce0e732 119srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session)
7491a29f 120{
615f6d25
JH
121 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
122 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
123
27c05210 124 for (const auto& option : options_) {
da50281d 125 GVariant *const value = option.second;
615f6d25
JH
126 g_variant_ref(value);
127 g_hash_table_replace(opt_hash, (void*)g_strdup(
da50281d 128 option.first.c_str()), value);
615f6d25
JH
129 }
130
8ce0e732
SA
131 if (decoder_inst_)
132 qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists";
133
134 decoder_inst_ = srd_inst_new(session, decoder_->id, opt_hash);
615f6d25
JH
135 g_hash_table_destroy(opt_hash);
136
8ce0e732 137 if (!decoder_inst_)
4c60462b 138 return nullptr;
7491a29f 139
6ac6242b 140 // Setup the channels
f5295834 141 GArray *const init_pin_states = g_array_sized_new(false, true,
9f97b357
SA
142 sizeof(uint8_t), channels_.size());
143
144 g_array_set_size(init_pin_states, channels_.size());
145
6ac6242b 146 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
7491a29f
JH
147 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
148
27a3f09b 149 for (DecodeChannel *ch : channels_) {
6e7a4a00
SA
150 if (!ch->assigned_signal)
151 continue;
152
27a3f09b 153 init_pin_states->data[ch->id] = ch->initial_pin_state;
9f97b357 154
6e7a4a00 155 GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position
7491a29f 156 g_variant_ref_sink(gvar);
6e7a4a00 157 // key is channel name (pdch->id), value is bit position in each sample (gvar)
27a3f09b 158 g_hash_table_insert(channels, ch->pdch_->id, gvar);
7491a29f
JH
159 }
160
8ce0e732 161 srd_inst_channel_set_all(decoder_inst_, channels);
7491a29f 162
8ce0e732 163 srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states);
f5295834 164 g_array_free(init_pin_states, true);
407c9ebe 165
8ce0e732
SA
166 return decoder_inst_;
167}
168
169void Decoder::invalidate_decoder_inst()
170{
171 decoder_inst_ = nullptr;
7491a29f
JH
172}
173
870ea3db
UH
174} // namespace decode
175} // namespace data
176} // namespace pv