]>
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 | ||
8d3e0764 | 21 | #include <libsigrok/libsigrok.h> |
7491a29f JH |
22 | #include <libsigrokdecode/libsigrokdecode.h> |
23 | ||
24 | #include "decoder.h" | |
25 | ||
26 | #include <pv/view/logicsignal.h> | |
27 | ||
819f4c25 | 28 | using boost::shared_ptr; |
ddee4cf8 | 29 | using std::set; |
819f4c25 JH |
30 | using std::map; |
31 | using std::string; | |
7491a29f JH |
32 | |
33 | namespace pv { | |
34 | namespace data { | |
35 | namespace decode { | |
36 | ||
37 | Decoder::Decoder(const srd_decoder *const dec) : | |
dd048a7e JH |
38 | _decoder(dec), |
39 | _shown(true) | |
7491a29f JH |
40 | { |
41 | } | |
42 | ||
43 | Decoder::~Decoder() | |
44 | { | |
615f6d25 JH |
45 | for (map<string, GVariant*>::const_iterator i = _options.begin(); |
46 | i != _options.end(); i++) | |
47 | g_variant_unref((*i).second); | |
7491a29f JH |
48 | } |
49 | ||
50 | const srd_decoder* Decoder::decoder() const | |
51 | { | |
52 | return _decoder; | |
53 | } | |
54 | ||
dd048a7e JH |
55 | bool Decoder::shown() const |
56 | { | |
57 | return _shown; | |
58 | } | |
59 | ||
60 | void Decoder::show(bool show) | |
61 | { | |
62 | _shown = show; | |
63 | } | |
64 | ||
7491a29f JH |
65 | const map<const srd_probe*, shared_ptr<view::LogicSignal> >& |
66 | Decoder::probes() const | |
67 | { | |
68 | return _probes; | |
69 | } | |
70 | ||
71 | void Decoder::set_probes(std::map<const srd_probe*, | |
72 | boost::shared_ptr<view::LogicSignal> > probes) | |
73 | { | |
74 | _probes = probes; | |
75 | } | |
76 | ||
615f6d25 | 77 | const std::map<std::string, GVariant*>& Decoder::options() const |
7491a29f JH |
78 | { |
79 | return _options; | |
80 | } | |
81 | ||
82 | void Decoder::set_option(const char *id, GVariant *value) | |
83 | { | |
615f6d25 | 84 | assert(value); |
7491a29f | 85 | g_variant_ref(value); |
615f6d25 | 86 | _options[id] = value; |
7491a29f JH |
87 | } |
88 | ||
a2d4b551 JH |
89 | bool Decoder::have_required_probes() const |
90 | { | |
91 | for (GSList *p = _decoder->probes; p; p = p->next) { | |
92 | const srd_probe *const probe = (const srd_probe*)p->data; | |
93 | assert(probe); | |
94 | if (_probes.find(probe) == _probes.end()) | |
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; | |
104 | for(map<const srd_probe*, shared_ptr<view::LogicSignal> >:: | |
105 | const_iterator i = _probes.begin(); | |
106 | i != _probes.end(); i++) | |
107 | { | |
108 | shared_ptr<view::LogicSignal> signal((*i).second); | |
109 | assert(signal); | |
110 | data.insert(signal->logic_data()); | |
111 | } | |
112 | ||
113 | return data; | |
114 | } | |
115 | ||
13a3528c | 116 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session, int unit_size) const |
7491a29f | 117 | { |
615f6d25 JH |
118 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, |
119 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
120 | ||
121 | for (map<string, GVariant*>::const_iterator i = _options.begin(); | |
122 | i != _options.end(); i++) | |
123 | { | |
124 | GVariant *const value = (*i).second; | |
125 | g_variant_ref(value); | |
126 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
127 | (*i).first.c_str()), value); | |
128 | } | |
129 | ||
7491a29f | 130 | srd_decoder_inst *const decoder_inst = srd_inst_new( |
615f6d25 JH |
131 | session, _decoder->id, opt_hash); |
132 | g_hash_table_destroy(opt_hash); | |
133 | ||
7491a29f JH |
134 | if(!decoder_inst) |
135 | return NULL; | |
136 | ||
137 | // Setup the probes | |
138 | GHashTable *const probes = g_hash_table_new_full(g_str_hash, | |
139 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
140 | ||
141 | for(map<const srd_probe*, shared_ptr<view::LogicSignal> >:: | |
142 | const_iterator i = _probes.begin(); | |
143 | i != _probes.end(); i++) | |
144 | { | |
145 | shared_ptr<view::LogicSignal> signal((*i).second); | |
146 | GVariant *const gvar = g_variant_new_int32( | |
147 | signal->probe()->index); | |
148 | g_variant_ref_sink(gvar); | |
149 | g_hash_table_insert(probes, (*i).first->id, gvar); | |
150 | } | |
151 | ||
13a3528c | 152 | srd_inst_probe_set_all(decoder_inst, probes, unit_size); |
7491a29f JH |
153 | |
154 | return decoder_inst; | |
155 | } | |
156 | ||
157 | } // decode | |
158 | } // data | |
159 | } // pv |