]>
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 | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
943edd76 MC |
21 | #include <cassert> |
22 | ||
fe3a1c21 | 23 | #include <libsigrokcxx/libsigrokcxx.hpp> |
7491a29f JH |
24 | #include <libsigrokdecode/libsigrokdecode.h> |
25 | ||
2acdb232 | 26 | #include "decoder.hpp" |
7491a29f | 27 | |
04394ded | 28 | #include <pv/data/signalbase.hpp> |
7491a29f | 29 | |
ddee4cf8 | 30 | using std::set; |
819f4c25 | 31 | using std::map; |
f9abf97e | 32 | using std::shared_ptr; |
819f4c25 | 33 | using std::string; |
7491a29f JH |
34 | |
35 | namespace pv { | |
36 | namespace data { | |
37 | namespace decode { | |
38 | ||
39 | Decoder::Decoder(const srd_decoder *const dec) : | |
8dbbc7f0 JH |
40 | decoder_(dec), |
41 | shown_(true) | |
7491a29f JH |
42 | { |
43 | } | |
44 | ||
45 | Decoder::~Decoder() | |
46 | { | |
27c05210 | 47 | for (auto& option : options_) |
da50281d | 48 | g_variant_unref(option.second); |
7491a29f JH |
49 | } |
50 | ||
51 | const srd_decoder* Decoder::decoder() const | |
52 | { | |
8dbbc7f0 | 53 | return decoder_; |
7491a29f JH |
54 | } |
55 | ||
dd048a7e JH |
56 | bool Decoder::shown() const |
57 | { | |
8dbbc7f0 | 58 | return shown_; |
dd048a7e JH |
59 | } |
60 | ||
61 | void Decoder::show(bool show) | |
62 | { | |
8dbbc7f0 | 63 | shown_ = show; |
dd048a7e JH |
64 | } |
65 | ||
04394ded | 66 | const map<const srd_channel*, shared_ptr<data::SignalBase> >& |
8bd26d8b | 67 | Decoder::channels() const |
7491a29f | 68 | { |
8dbbc7f0 | 69 | return channels_; |
7491a29f JH |
70 | } |
71 | ||
6ac6242b | 72 | void Decoder::set_channels(std::map<const srd_channel*, |
04394ded | 73 | std::shared_ptr<data::SignalBase> > channels) |
7491a29f | 74 | { |
8dbbc7f0 | 75 | channels_ = channels; |
7491a29f JH |
76 | } |
77 | ||
615f6d25 | 78 | const std::map<std::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; |
7491a29f JH |
88 | } |
89 | ||
6ac6242b | 90 | bool Decoder::have_required_channels() const |
a2d4b551 | 91 | { |
8dbbc7f0 | 92 | for (GSList *l = decoder_->channels; l; l = l->next) { |
8bd26d8b UH |
93 | const srd_channel *const pdch = (const srd_channel*)l->data; |
94 | assert(pdch); | |
8dbbc7f0 | 95 | if (channels_.find(pdch) == channels_.end()) |
a2d4b551 JH |
96 | return false; |
97 | } | |
98 | ||
99 | return true; | |
100 | } | |
101 | ||
ddee4cf8 JH |
102 | set< shared_ptr<pv::data::Logic> > Decoder::get_data() |
103 | { | |
104 | set< shared_ptr<pv::data::Logic> > data; | |
27c05210 | 105 | for (const auto& channel : channels_) { |
04394ded SA |
106 | shared_ptr<data::SignalBase> b(channel.second); |
107 | assert(b); | |
108 | data.insert(b->logic_data()); | |
ddee4cf8 JH |
109 | } |
110 | ||
111 | return data; | |
112 | } | |
113 | ||
c4dd1e4e | 114 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) const |
7491a29f | 115 | { |
615f6d25 JH |
116 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, |
117 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
118 | ||
27c05210 | 119 | for (const auto& option : options_) { |
da50281d | 120 | GVariant *const value = option.second; |
615f6d25 JH |
121 | g_variant_ref(value); |
122 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
da50281d | 123 | option.first.c_str()), value); |
615f6d25 JH |
124 | } |
125 | ||
7491a29f | 126 | srd_decoder_inst *const decoder_inst = srd_inst_new( |
8dbbc7f0 | 127 | session, decoder_->id, opt_hash); |
615f6d25 JH |
128 | g_hash_table_destroy(opt_hash); |
129 | ||
f3290553 | 130 | if (!decoder_inst) |
4c60462b | 131 | return nullptr; |
7491a29f | 132 | |
6ac6242b ML |
133 | // Setup the channels |
134 | GHashTable *const channels = g_hash_table_new_full(g_str_hash, | |
7491a29f JH |
135 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); |
136 | ||
27c05210 | 137 | for (const auto& channel : channels_) { |
04394ded SA |
138 | shared_ptr<data::SignalBase> b(channel.second); |
139 | GVariant *const gvar = g_variant_new_int32(b->index()); | |
7491a29f | 140 | g_variant_ref_sink(gvar); |
da50281d | 141 | g_hash_table_insert(channels, channel.first->id, gvar); |
7491a29f JH |
142 | } |
143 | ||
c4dd1e4e | 144 | srd_inst_channel_set_all(decoder_inst, channels); |
7491a29f JH |
145 | |
146 | return decoder_inst; | |
147 | } | |
148 | ||
149 | } // decode | |
150 | } // data | |
151 | } // pv |