]>
Commit | Line | Data |
---|---|---|
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 | ||
32 | using pv::data::DecodeChannel; | |
33 | using std::map; | |
34 | using std::string; | |
35 | ||
36 | namespace pv { | |
37 | namespace data { | |
38 | namespace decode { | |
39 | ||
40 | Decoder::Decoder(const srd_decoder *const dec) : | |
41 | decoder_(dec), | |
42 | shown_(true), | |
43 | decoder_inst_(nullptr) | |
44 | { | |
45 | } | |
46 | ||
47 | Decoder::~Decoder() | |
48 | { | |
49 | for (auto& option : options_) | |
50 | g_variant_unref(option.second); | |
51 | } | |
52 | ||
53 | const srd_decoder* Decoder::decoder() const | |
54 | { | |
55 | return decoder_; | |
56 | } | |
57 | ||
58 | bool Decoder::shown() const | |
59 | { | |
60 | return shown_; | |
61 | } | |
62 | ||
63 | void Decoder::show(bool show) | |
64 | { | |
65 | shown_ = show; | |
66 | } | |
67 | ||
68 | const vector<DecodeChannel*>& Decoder::channels() const | |
69 | { | |
70 | return channels_; | |
71 | } | |
72 | ||
73 | void Decoder::set_channels(vector<DecodeChannel*> channels) | |
74 | { | |
75 | channels_ = channels; | |
76 | } | |
77 | ||
78 | const map<string, GVariant*>& Decoder::options() const | |
79 | { | |
80 | return options_; | |
81 | } | |
82 | ||
83 | void 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 | apply_all_options(); | |
91 | } | |
92 | ||
93 | void Decoder::apply_all_options() | |
94 | { | |
95 | if (decoder_inst_) { | |
96 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
97 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
98 | ||
99 | for (const auto& option : options_) { | |
100 | GVariant *const value = option.second; | |
101 | g_variant_ref(value); | |
102 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
103 | option.first.c_str()), value); | |
104 | } | |
105 | ||
106 | srd_inst_option_set(decoder_inst_, opt_hash); | |
107 | g_hash_table_destroy(opt_hash); | |
108 | } | |
109 | } | |
110 | ||
111 | bool Decoder::have_required_channels() const | |
112 | { | |
113 | for (DecodeChannel *ch : channels_) | |
114 | if (!ch->assigned_signal && !ch->is_optional) | |
115 | return false; | |
116 | ||
117 | return true; | |
118 | } | |
119 | ||
120 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) | |
121 | { | |
122 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
123 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
124 | ||
125 | for (const auto& option : options_) { | |
126 | GVariant *const value = option.second; | |
127 | g_variant_ref(value); | |
128 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
129 | option.first.c_str()), value); | |
130 | } | |
131 | ||
132 | if (decoder_inst_) | |
133 | qDebug() << "WARNING: previous decoder instance" << decoder_inst_ << "exists"; | |
134 | ||
135 | decoder_inst_ = srd_inst_new(session, decoder_->id, opt_hash); | |
136 | g_hash_table_destroy(opt_hash); | |
137 | ||
138 | if (!decoder_inst_) | |
139 | return nullptr; | |
140 | ||
141 | // Setup the channels | |
142 | GArray *const init_pin_states = g_array_sized_new(false, true, | |
143 | sizeof(uint8_t), channels_.size()); | |
144 | ||
145 | g_array_set_size(init_pin_states, channels_.size()); | |
146 | ||
147 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, | |
148 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
149 | ||
150 | for (DecodeChannel *ch : channels_) { | |
151 | if (!ch->assigned_signal) | |
152 | continue; | |
153 | ||
154 | init_pin_states->data[ch->id] = ch->initial_pin_state; | |
155 | ||
156 | GVariant *const gvar = g_variant_new_int32(ch->bit_id); // bit_id = bit position | |
157 | g_variant_ref_sink(gvar); | |
158 | // key is channel name (pdch->id), value is bit position in each sample (gvar) | |
159 | g_hash_table_insert(channels, ch->pdch_->id, gvar); | |
160 | } | |
161 | ||
162 | srd_inst_channel_set_all(decoder_inst_, channels); | |
163 | ||
164 | srd_inst_initial_pins_set_all(decoder_inst_, init_pin_states); | |
165 | g_array_free(init_pin_states, true); | |
166 | ||
167 | return decoder_inst_; | |
168 | } | |
169 | ||
170 | void Decoder::invalidate_decoder_inst() | |
171 | { | |
172 | decoder_inst_ = nullptr; | |
173 | } | |
174 | ||
175 | } // namespace decode | |
176 | } // namespace data | |
177 | } // namespace pv |