]>
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 <libsigrokcxx/libsigrokcxx.hpp> | |
23 | #include <libsigrokdecode/libsigrokdecode.h> | |
24 | ||
25 | #include "decoder.hpp" | |
26 | ||
27 | #include <pv/data/signalbase.hpp> | |
28 | ||
29 | using std::set; | |
30 | using std::map; | |
31 | using std::shared_ptr; | |
32 | using std::string; | |
33 | ||
34 | namespace pv { | |
35 | namespace data { | |
36 | namespace decode { | |
37 | ||
38 | Decoder::Decoder(const srd_decoder *const dec) : | |
39 | decoder_(dec), | |
40 | shown_(true), | |
41 | initial_pins_(nullptr) | |
42 | { | |
43 | } | |
44 | ||
45 | Decoder::~Decoder() | |
46 | { | |
47 | for (auto& option : options_) | |
48 | g_variant_unref(option.second); | |
49 | } | |
50 | ||
51 | const srd_decoder* Decoder::decoder() const | |
52 | { | |
53 | return decoder_; | |
54 | } | |
55 | ||
56 | bool Decoder::shown() const | |
57 | { | |
58 | return shown_; | |
59 | } | |
60 | ||
61 | void Decoder::show(bool show) | |
62 | { | |
63 | shown_ = show; | |
64 | } | |
65 | ||
66 | const map<const srd_channel*, shared_ptr<data::SignalBase> >& | |
67 | Decoder::channels() const | |
68 | { | |
69 | return channels_; | |
70 | } | |
71 | ||
72 | void Decoder::set_channels(map<const srd_channel*, | |
73 | shared_ptr<data::SignalBase> > channels) | |
74 | { | |
75 | channels_ = channels; | |
76 | } | |
77 | ||
78 | void Decoder::set_initial_pins(GArray *initial_pins) | |
79 | { | |
80 | if (initial_pins_) | |
81 | g_array_free(initial_pins_, TRUE); | |
82 | initial_pins_ = initial_pins; | |
83 | } | |
84 | ||
85 | GArray *Decoder::initial_pins() const | |
86 | { | |
87 | return initial_pins_; | |
88 | } | |
89 | ||
90 | const map<string, GVariant*>& Decoder::options() const | |
91 | { | |
92 | return options_; | |
93 | } | |
94 | ||
95 | void Decoder::set_option(const char *id, GVariant *value) | |
96 | { | |
97 | assert(value); | |
98 | g_variant_ref(value); | |
99 | options_[id] = value; | |
100 | } | |
101 | ||
102 | bool Decoder::have_required_channels() const | |
103 | { | |
104 | for (GSList *l = decoder_->channels; l; l = l->next) { | |
105 | const srd_channel *const pdch = (const srd_channel*)l->data; | |
106 | assert(pdch); | |
107 | if (channels_.find(pdch) == channels_.end()) | |
108 | return false; | |
109 | } | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
114 | set< shared_ptr<pv::data::Logic> > Decoder::get_data() | |
115 | { | |
116 | set< shared_ptr<pv::data::Logic> > data; | |
117 | for (const auto& channel : channels_) { | |
118 | shared_ptr<data::SignalBase> b(channel.second); | |
119 | assert(b); | |
120 | data.insert(b->logic_data()); | |
121 | } | |
122 | ||
123 | return data; | |
124 | } | |
125 | ||
126 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) const | |
127 | { | |
128 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
129 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
130 | ||
131 | for (const auto& option : options_) { | |
132 | GVariant *const value = option.second; | |
133 | g_variant_ref(value); | |
134 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
135 | option.first.c_str()), value); | |
136 | } | |
137 | ||
138 | srd_decoder_inst *const decoder_inst = srd_inst_new( | |
139 | session, decoder_->id, opt_hash); | |
140 | g_hash_table_destroy(opt_hash); | |
141 | ||
142 | if (!decoder_inst) | |
143 | return nullptr; | |
144 | ||
145 | // Setup the channels | |
146 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, | |
147 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
148 | ||
149 | for (const auto& channel : channels_) { | |
150 | shared_ptr<data::SignalBase> b(channel.second); | |
151 | GVariant *const gvar = g_variant_new_int32(b->index()); | |
152 | g_variant_ref_sink(gvar); | |
153 | g_hash_table_insert(channels, channel.first->id, gvar); | |
154 | } | |
155 | ||
156 | srd_inst_channel_set_all(decoder_inst, channels); | |
157 | ||
158 | srd_inst_initial_pins_set_all(decoder_inst, initial_pins_); | |
159 | ||
160 | return decoder_inst; | |
161 | } | |
162 | ||
163 | } // namespace decode | |
164 | } // namespace data | |
165 | } // namespace pv |