]>
Commit | Line | Data |
---|---|---|
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 | |
27a3f09b | 32 | using pv::data::DecodeChannel; |
819f4c25 JH |
33 | using std::map; |
34 | using std::string; | |
7491a29f JH |
35 | |
36 | namespace pv { | |
37 | namespace data { | |
38 | namespace decode { | |
39 | ||
40 | Decoder::Decoder(const srd_decoder *const dec) : | |
8dbbc7f0 | 41 | decoder_(dec), |
8ce0e732 SA |
42 | shown_(true), |
43 | decoder_inst_(nullptr) | |
7491a29f JH |
44 | { |
45 | } | |
46 | ||
47 | Decoder::~Decoder() | |
48 | { | |
27c05210 | 49 | for (auto& option : options_) |
da50281d | 50 | g_variant_unref(option.second); |
7491a29f JH |
51 | } |
52 | ||
53 | const srd_decoder* Decoder::decoder() const | |
54 | { | |
8dbbc7f0 | 55 | return decoder_; |
7491a29f JH |
56 | } |
57 | ||
dd048a7e JH |
58 | bool Decoder::shown() const |
59 | { | |
8dbbc7f0 | 60 | return shown_; |
dd048a7e JH |
61 | } |
62 | ||
63 | void Decoder::show(bool show) | |
64 | { | |
8dbbc7f0 | 65 | shown_ = show; |
dd048a7e JH |
66 | } |
67 | ||
27a3f09b | 68 | const vector<DecodeChannel*>& Decoder::channels() const |
7491a29f | 69 | { |
8dbbc7f0 | 70 | return channels_; |
7491a29f JH |
71 | } |
72 | ||
27a3f09b | 73 | void Decoder::set_channels(vector<DecodeChannel*> channels) |
7491a29f | 74 | { |
8dbbc7f0 | 75 | channels_ = channels; |
7491a29f JH |
76 | } |
77 | ||
6f925ba9 | 78 | const map<string, GVariant*>& Decoder::options() const |
7491a29f | 79 | { |
8dbbc7f0 | 80 | return options_; |
7491a29f JH |
81 | } |
82 | ||
83 | void Decoder::set_option(const char *id, GVariant *value) | |
84 | { | |
615f6d25 | 85 | assert(value); |
7491a29f | 86 | g_variant_ref(value); |
8dbbc7f0 | 87 | options_[id] = value; |
8ce0e732 SA |
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 | ||
c5f47334 SA |
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 | } | |
8ce0e732 SA |
100 | |
101 | srd_inst_option_set(decoder_inst_, opt_hash); | |
102 | g_hash_table_destroy(opt_hash); | |
103 | } | |
7491a29f JH |
104 | } |
105 | ||
6ac6242b | 106 | bool Decoder::have_required_channels() const |
a2d4b551 | 107 | { |
27a3f09b SA |
108 | for (DecodeChannel *ch : channels_) |
109 | if (!ch->assigned_signal && !ch->is_optional) | |
a2d4b551 | 110 | return false; |
a2d4b551 JH |
111 | |
112 | return true; | |
113 | } | |
114 | ||
8ce0e732 | 115 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) |
7491a29f | 116 | { |
615f6d25 JH |
117 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, |
118 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
119 | ||
27c05210 | 120 | for (const auto& option : options_) { |
da50281d | 121 | GVariant *const value = option.second; |
615f6d25 JH |
122 | g_variant_ref(value); |
123 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
da50281d | 124 | option.first.c_str()), value); |
615f6d25 JH |
125 | } |
126 | ||
8ce0e732 SA |
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); | |
615f6d25 JH |
131 | g_hash_table_destroy(opt_hash); |
132 | ||
8ce0e732 | 133 | if (!decoder_inst_) |
4c60462b | 134 | return nullptr; |
7491a29f | 135 | |
6ac6242b | 136 | // Setup the channels |
f5295834 | 137 | GArray *const init_pin_states = g_array_sized_new(false, true, |
9f97b357 SA |
138 | sizeof(uint8_t), channels_.size()); |
139 | ||
140 | g_array_set_size(init_pin_states, channels_.size()); | |
141 | ||
6ac6242b | 142 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, |
7491a29f JH |
143 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); |
144 | ||
27a3f09b | 145 | for (DecodeChannel *ch : channels_) { |
6e7a4a00 SA |
146 | if (!ch->assigned_signal) |
147 | continue; | |
148 | ||
27a3f09b | 149 | init_pin_states->data[ch->id] = ch->initial_pin_state; |
9f97b357 | 150 | |
6e7a4a00 | 151 | GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position |
7491a29f | 152 | g_variant_ref_sink(gvar); |
6e7a4a00 | 153 | // key is channel name (pdch->id), value is bit position in each sample (gvar) |
27a3f09b | 154 | g_hash_table_insert(channels, ch->pdch_->id, gvar); |
7491a29f JH |
155 | } |
156 | ||
8ce0e732 | 157 | srd_inst_channel_set_all(decoder_inst_, channels); |
7491a29f | 158 | |
8ce0e732 | 159 | srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states); |
f5295834 | 160 | g_array_free(init_pin_states, true); |
407c9ebe | 161 | |
8ce0e732 SA |
162 | return decoder_inst_; |
163 | } | |
164 | ||
165 | void Decoder::invalidate_decoder_inst() | |
166 | { | |
167 | decoder_inst_ = nullptr; | |
7491a29f JH |
168 | } |
169 | ||
870ea3db UH |
170 | } // namespace decode |
171 | } // namespace data | |
172 | } // namespace pv |