]> sigrok.org Git - pulseview.git/blame - pv/data/decode/decoder.cpp
Fix some random clang-tidy warnings.
[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
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>
27a3f09b 28#include <pv/data/decodesignal.hpp>
7491a29f 29
27a3f09b 30using pv::data::DecodeChannel;
819f4c25
JH
31using std::map;
32using std::string;
7491a29f
JH
33
34namespace pv {
35namespace data {
36namespace decode {
37
38Decoder::Decoder(const srd_decoder *const dec) :
8dbbc7f0 39 decoder_(dec),
9f97b357 40 shown_(true)
7491a29f
JH
41{
42}
43
44Decoder::~Decoder()
45{
27c05210 46 for (auto& option : options_)
da50281d 47 g_variant_unref(option.second);
7491a29f
JH
48}
49
50const srd_decoder* Decoder::decoder() const
51{
8dbbc7f0 52 return decoder_;
7491a29f
JH
53}
54
dd048a7e
JH
55bool Decoder::shown() const
56{
8dbbc7f0 57 return shown_;
dd048a7e
JH
58}
59
60void Decoder::show(bool show)
61{
8dbbc7f0 62 shown_ = show;
dd048a7e
JH
63}
64
27a3f09b 65const vector<DecodeChannel*>& Decoder::channels() const
7491a29f 66{
8dbbc7f0 67 return channels_;
7491a29f
JH
68}
69
27a3f09b 70void Decoder::set_channels(vector<DecodeChannel*> channels)
7491a29f 71{
8dbbc7f0 72 channels_ = channels;
7491a29f
JH
73}
74
6f925ba9 75const map<string, GVariant*>& Decoder::options() const
7491a29f 76{
8dbbc7f0 77 return options_;
7491a29f
JH
78}
79
80void Decoder::set_option(const char *id, GVariant *value)
81{
615f6d25 82 assert(value);
7491a29f 83 g_variant_ref(value);
8dbbc7f0 84 options_[id] = value;
7491a29f
JH
85}
86
6ac6242b 87bool Decoder::have_required_channels() const
a2d4b551 88{
27a3f09b
SA
89 for (DecodeChannel *ch : channels_)
90 if (!ch->assigned_signal && !ch->is_optional)
a2d4b551 91 return false;
a2d4b551
JH
92
93 return true;
94}
95
c4dd1e4e 96srd_decoder_inst* Decoder::create_decoder_inst(srd_session *session) const
7491a29f 97{
615f6d25
JH
98 GHashTable *const opt_hash = g_hash_table_new_full(g_str_hash,
99 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
100
27c05210 101 for (const auto& option : options_) {
da50281d 102 GVariant *const value = option.second;
615f6d25
JH
103 g_variant_ref(value);
104 g_hash_table_replace(opt_hash, (void*)g_strdup(
da50281d 105 option.first.c_str()), value);
615f6d25
JH
106 }
107
7491a29f 108 srd_decoder_inst *const decoder_inst = srd_inst_new(
8dbbc7f0 109 session, decoder_->id, opt_hash);
615f6d25
JH
110 g_hash_table_destroy(opt_hash);
111
f3290553 112 if (!decoder_inst)
4c60462b 113 return nullptr;
7491a29f 114
6ac6242b 115 // Setup the channels
f5295834 116 GArray *const init_pin_states = g_array_sized_new(false, true,
9f97b357
SA
117 sizeof(uint8_t), channels_.size());
118
119 g_array_set_size(init_pin_states, channels_.size());
120
6ac6242b 121 GHashTable *const channels = g_hash_table_new_full(g_str_hash,
7491a29f
JH
122 g_str_equal, g_free, (GDestroyNotify)g_variant_unref);
123
27a3f09b
SA
124 for (DecodeChannel *ch : channels_) {
125 init_pin_states->data[ch->id] = ch->initial_pin_state;
9f97b357 126
27a3f09b 127 GVariant *const gvar = g_variant_new_int32(ch->id); // id = bit position
7491a29f 128 g_variant_ref_sink(gvar);
27a3f09b
SA
129 // key is channel name, value is bit position in each sample
130 g_hash_table_insert(channels, ch->pdch_->id, gvar);
7491a29f
JH
131 }
132
c4dd1e4e 133 srd_inst_channel_set_all(decoder_inst, channels);
7491a29f 134
27a3f09b 135 srd_inst_initial_pins_set_all(decoder_inst, init_pin_states);
f5295834 136 g_array_free(init_pin_states, true);
407c9ebe 137
7491a29f
JH
138 return decoder_inst;
139}
140
870ea3db
UH
141} // namespace decode
142} // namespace data
143} // namespace pv