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