]> sigrok.org Git - pulseview.git/blame - pv/data/decode/decoder.cpp
Use libsigrok C++ bindings (patch version 7).
[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
943edd76
MC
21#include <cassert>
22
e8d00928 23#include <libsigrok/libsigrok.hpp>
7491a29f
JH
24#include <libsigrokdecode/libsigrokdecode.h>
25
26#include "decoder.h"
27
28#include <pv/view/logicsignal.h>
29
ddee4cf8 30using std::set;
819f4c25 31using std::map;
f9abf97e 32using std::shared_ptr;
819f4c25 33using std::string;
7491a29f
JH
34
35namespace pv {
36namespace data {
37namespace decode {
38
39Decoder::Decoder(const srd_decoder *const dec) :
dd048a7e
JH
40 _decoder(dec),
41 _shown(true)
7491a29f
JH
42{
43}
44
45Decoder::~Decoder()
46{
f46e495e 47 for (auto i = _options.begin(); i != _options.end(); i++)
615f6d25 48 g_variant_unref((*i).second);
7491a29f
JH
49}
50
51const srd_decoder* Decoder::decoder() const
52{
53 return _decoder;
54}
55
dd048a7e
JH
56bool Decoder::shown() const
57{
58 return _shown;
59}
60
61void Decoder::show(bool show)
62{
63 _shown = show;
64}
65
8bd26d8b
UH
66const map<const srd_channel*, shared_ptr<view::LogicSignal> >&
67Decoder::channels() const
7491a29f 68{
6ac6242b 69 return _channels;
7491a29f
JH
70}
71
6ac6242b
ML
72void Decoder::set_channels(std::map<const srd_channel*,
73 std::shared_ptr<view::LogicSignal> > channels)
7491a29f 74{
6ac6242b 75 _channels = channels;
7491a29f
JH
76}
77
615f6d25 78const std::map<std::string, GVariant*>& Decoder::options() const
7491a29f
JH
79{
80 return _options;
81}
82
83void Decoder::set_option(const char *id, GVariant *value)
84{
615f6d25 85 assert(value);
7491a29f 86 g_variant_ref(value);
615f6d25 87 _options[id] = value;
7491a29f
JH
88}
89
6ac6242b 90bool Decoder::have_required_channels() const
a2d4b551 91{
8bd26d8b
UH
92 for (GSList *l = _decoder->channels; l; l = l->next) {
93 const srd_channel *const pdch = (const srd_channel*)l->data;
94 assert(pdch);
6ac6242b 95 if (_channels.find(pdch) == _channels.end())
a2d4b551
JH
96 return false;
97 }
98
99 return true;
100}
101
ddee4cf8
JH
102set< shared_ptr<pv::data::Logic> > Decoder::get_data()
103{
104 set< shared_ptr<pv::data::Logic> > data;
6ac6242b 105 for(auto i = _channels.cbegin(); i != _channels.cend(); i++) {
ddee4cf8
JH
106 shared_ptr<view::LogicSignal> signal((*i).second);
107 assert(signal);
108 data.insert(signal->logic_data());
109 }
110
111 return data;
112}
113
13a3528c 114srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session, int unit_size) 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
f46e495e 119 for (auto i = _options.cbegin(); i != _options.cend(); i++)
615f6d25
JH
120 {
121 GVariant *const value = (*i).second;
122 g_variant_ref(value);
123 g_hash_table_replace(opt_hash, (void*)g_strdup(
124 (*i).first.c_str()), value);
125 }
126
7491a29f 127 srd_decoder_inst *const decoder_inst = srd_inst_new(
615f6d25
JH
128 session, _decoder->id, opt_hash);
129 g_hash_table_destroy(opt_hash);
130
7491a29f
JH
131 if(!decoder_inst)
132 return NULL;
133
6ac6242b
ML
134 // Setup the channels
135 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
7491a29f
JH
136 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
137
6ac6242b 138 for(auto i = _channels.cbegin(); i != _channels.cend(); i++)
7491a29f
JH
139 {
140 shared_ptr<view::LogicSignal> signal((*i).second);
141 GVariant *const gvar = g_variant_new_int32(
e8d00928 142 signal->channel()->index());
7491a29f 143 g_variant_ref_sink(gvar);
6ac6242b 144 g_hash_table_insert(channels, (*i).first->id, gvar);
7491a29f
JH
145 }
146
6ac6242b 147 srd_inst_channel_set_all(decoder_inst, channels, unit_size);
7491a29f
JH
148
149 return decoder_inst;
150}
151
152} // decode
153} // data
154} // pv