]>
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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include <libsigrok/libsigrok.h> | |
22 | #include <libsigrokdecode/libsigrokdecode.h> | |
23 | ||
24 | #include "decoder.h" | |
25 | ||
26 | #include <pv/view/logicsignal.h> | |
27 | ||
28 | using boost::shared_ptr; | |
29 | using std::set; | |
30 | using std::map; | |
31 | using std::string; | |
32 | ||
33 | namespace pv { | |
34 | namespace data { | |
35 | namespace decode { | |
36 | ||
37 | Decoder::Decoder(const srd_decoder *const dec) : | |
38 | _decoder(dec), | |
39 | _shown(true) | |
40 | { | |
41 | } | |
42 | ||
43 | Decoder::~Decoder() | |
44 | { | |
45 | for (auto i = _options.begin(); i != _options.end(); i++) | |
46 | g_variant_unref((*i).second); | |
47 | } | |
48 | ||
49 | const srd_decoder* Decoder::decoder() const | |
50 | { | |
51 | return _decoder; | |
52 | } | |
53 | ||
54 | bool Decoder::shown() const | |
55 | { | |
56 | return _shown; | |
57 | } | |
58 | ||
59 | void Decoder::show(bool show) | |
60 | { | |
61 | _shown = show; | |
62 | } | |
63 | ||
64 | const map<const srd_channel*, shared_ptr<view::LogicSignal> >& | |
65 | Decoder::channels() const | |
66 | { | |
67 | return _probes; | |
68 | } | |
69 | ||
70 | void Decoder::set_probes(std::map<const srd_channel*, | |
71 | boost::shared_ptr<view::LogicSignal> > probes) | |
72 | { | |
73 | _probes = probes; | |
74 | } | |
75 | ||
76 | const std::map<std::string, GVariant*>& Decoder::options() const | |
77 | { | |
78 | return _options; | |
79 | } | |
80 | ||
81 | void Decoder::set_option(const char *id, GVariant *value) | |
82 | { | |
83 | assert(value); | |
84 | g_variant_ref(value); | |
85 | _options[id] = value; | |
86 | } | |
87 | ||
88 | bool Decoder::have_required_probes() const | |
89 | { | |
90 | for (GSList *l = _decoder->channels; l; l = l->next) { | |
91 | const srd_channel *const pdch = (const srd_channel*)l->data; | |
92 | assert(pdch); | |
93 | if (_probes.find(pdch) == _probes.end()) | |
94 | return false; | |
95 | } | |
96 | ||
97 | return true; | |
98 | } | |
99 | ||
100 | set< shared_ptr<pv::data::Logic> > Decoder::get_data() | |
101 | { | |
102 | set< shared_ptr<pv::data::Logic> > data; | |
103 | for(auto i = _probes.cbegin(); i != _probes.cend(); i++) { | |
104 | shared_ptr<view::LogicSignal> signal((*i).second); | |
105 | assert(signal); | |
106 | data.insert(signal->logic_data()); | |
107 | } | |
108 | ||
109 | return data; | |
110 | } | |
111 | ||
112 | srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session, int unit_size) const | |
113 | { | |
114 | GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash, | |
115 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
116 | ||
117 | for (auto i = _options.cbegin(); i != _options.cend(); i++) | |
118 | { | |
119 | GVariant *const value = (*i).second; | |
120 | g_variant_ref(value); | |
121 | g_hash_table_replace(opt_hash, (void*)g_strdup( | |
122 | (*i).first.c_str()), value); | |
123 | } | |
124 | ||
125 | srd_decoder_inst *const decoder_inst = srd_inst_new( | |
126 | session, _decoder->id, opt_hash); | |
127 | g_hash_table_destroy(opt_hash); | |
128 | ||
129 | if(!decoder_inst) | |
130 | return NULL; | |
131 | ||
132 | // Setup the probes | |
133 | GHashTable *const probes = g_hash_table_new_full(g_str_hash, | |
134 | g_str_equal, g_free, (GDestroyNotify)g_variant_unref); | |
135 | ||
136 | for(auto i = _probes.cbegin(); i != _probes.cend(); i++) | |
137 | { | |
138 | shared_ptr<view::LogicSignal> signal((*i).second); | |
139 | GVariant *const gvar = g_variant_new_int32( | |
140 | signal->probe()->index); | |
141 | g_variant_ref_sink(gvar); | |
142 | g_hash_table_insert(probes, (*i).first->id, gvar); | |
143 | } | |
144 | ||
145 | srd_inst_channel_set_all(decoder_inst, probes, unit_size); | |
146 | ||
147 | return decoder_inst; | |
148 | } | |
149 | ||
150 | } // decode | |
151 | } // data | |
152 | } // pv |