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