#include <pv/binding/decoder.hpp>
#include <pv/data/decode/decoder.hpp>
#include <pv/data/decode/row.hpp>
+#include <pv/globalsettings.hpp>
#include <pv/session.hpp>
using std::lock_guard;
const double DecodeSignal::DecodeThreshold = 0.2;
const int64_t DecodeSignal::DecodeChunkLength = 256 * 1024;
-mutex DecodeSignal::global_srd_mutex_;
-
DecodeSignal::DecodeSignal(pv::Session &session) :
SignalBase(nullptr, SignalBase::DecodeChannel),
{
connect(&session_, SIGNAL(capture_state_changed(int)),
this, SLOT(on_capture_state_changed(int)));
-
- set_name(tr("Empty decoder signal"));
}
DecodeSignal::~DecodeSignal()
return stack_;
}
-void DecodeSignal::stack_decoder(srd_decoder *decoder)
+void DecodeSignal::stack_decoder(const srd_decoder *decoder)
{
assert(decoder);
- stack_.push_back(make_shared<decode::Decoder>(decoder));
+ const shared_ptr<Decoder> dec = make_shared<decode::Decoder>(decoder);
+
+ stack_.push_back(dec);
// Set name if this decoder is the first in the list
if (stack_.size() == 1)
// Include the newly created decode channels in the channel lists
update_channel_list();
- auto_assign_signals();
+ auto_assign_signals(dec);
commit_decoder_channels();
begin_decode();
}
return channels_;
}
-void DecodeSignal::auto_assign_signals()
+void DecodeSignal::auto_assign_signals(const shared_ptr<Decoder> dec)
{
bool new_assignment = false;
// Try to auto-select channels that don't have signals assigned yet
for (data::DecodeChannel &ch : channels_) {
+ // If a decoder is given, auto-assign only its channels
+ if (dec && (ch.decoder_ != dec))
+ continue;
+
if (ch.assigned_signal)
continue;
{
SignalBase::save_settings(settings);
- // TODO Save decoder stack, channel mapping and decoder options
+ settings.setValue("decoders", (int)(stack_.size()));
+
+ // Save decoder stack
+ int decoder_idx = 0;
+ for (shared_ptr<decode::Decoder> decoder : stack_) {
+ settings.beginGroup("decoder" + QString::number(decoder_idx++));
+
+ settings.setValue("id", decoder->decoder()->id);
+
+ // Save decoder options
+ const map<string, GVariant*>& options = decoder->options();
+
+ settings.setValue("options", (int)options.size());
+
+ // Note: decode::Decoder::options() returns only the options
+ // that differ from the default. See binding::Decoder::getter()
+ int i = 0;
+ for (auto option : options) {
+ settings.beginGroup("option" + QString::number(i));
+ settings.setValue("name", QString::fromStdString(option.first));
+ GlobalSettings::store_gvariant(settings, option.second);
+ settings.endGroup();
+ i++;
+ }
+
+ settings.endGroup();
+ }
+
+ // Save channel mapping
+ settings.setValue("channels", (int)channels_.size());
+
+ for (unsigned int channel_id = 0; channel_id < channels_.size(); channel_id++) {
+ auto channel = find_if(channels_.begin(), channels_.end(),
+ [&](data::DecodeChannel ch) { return ch.id == channel_id; });
+
+ if (channel == channels_.end()) {
+ qDebug() << "ERROR: Gap in channel index:" << channel_id;
+ continue;
+ }
+
+ settings.beginGroup("channel" + QString::number(channel_id));
+
+ settings.setValue("name", channel->name); // Useful for debugging
+ settings.setValue("initial_pin_state", channel->initial_pin_state);
+
+ if (channel->assigned_signal)
+ settings.setValue("assigned_signal_name", channel->assigned_signal->name());
+
+ settings.endGroup();
+ }
}
void DecodeSignal::restore_settings(QSettings &settings)
{
SignalBase::restore_settings(settings);
- // TODO Restore decoder stack, channel mapping and decoder options
+ // Restore decoder stack
+ GSList *dec_list = g_slist_copy((GSList*)srd_decoder_list());
+
+ int decoders = settings.value("decoders").toInt();
+
+ for (int decoder_idx = 0; decoder_idx < decoders; decoder_idx++) {
+ settings.beginGroup("decoder" + QString::number(decoder_idx));
+
+ QString id = settings.value("id").toString();
+
+ for (GSList *entry = dec_list; entry; entry = entry->next) {
+ const srd_decoder *dec = (srd_decoder*)entry->data;
+ if (!dec)
+ continue;
+
+ if (QString::fromUtf8(dec->id) == id) {
+ shared_ptr<decode::Decoder> decoder =
+ make_shared<decode::Decoder>(dec);
+
+ stack_.push_back(decoder);
+
+ // Restore decoder options that differ from their default
+ int options = settings.value("options").toInt();
+
+ for (int i = 0; i < options; i++) {
+ settings.beginGroup("option" + QString::number(i));
+ QString name = settings.value("name").toString();
+ GVariant *value = GlobalSettings::restore_gvariant(settings);
+ decoder->set_option(name.toUtf8(), value);
+ settings.endGroup();
+ }
+
+ // Include the newly created decode channels in the channel lists
+ update_channel_list();
+ commit_decoder_channels();
+ break;
+ }
+ }
+
+ settings.endGroup();
+ channels_updated();
+ }
+
+ // Restore channel mapping
+ unsigned int channels = settings.value("channels").toInt();
+
+ const unordered_set< shared_ptr<data::SignalBase> > signalbases =
+ session_.signalbases();
+
+ for (unsigned int channel_id = 0; channel_id < channels; channel_id++) {
+ auto channel = find_if(channels_.begin(), channels_.end(),
+ [&](data::DecodeChannel ch) { return ch.id == channel_id; });
+
+ if (channel == channels_.end()) {
+ qDebug() << "ERROR: Non-existant channel index:" << channel_id;
+ continue;
+ }
+
+ settings.beginGroup("channel" + QString::number(channel_id));
+
+ QString assigned_signal_name = settings.value("assigned_signal_name").toString();
+
+ for (shared_ptr<data::SignalBase> signal : signalbases)
+ if (signal->name() == assigned_signal_name)
+ channel->assigned_signal = signal.get();
+
+ channel->initial_pin_state = settings.value("initial_pin_state").toInt();
+
+ settings.endGroup();
+ }
+
+ begin_decode();
}
void DecodeSignal::update_channel_list()
const shared_ptr<Logic> logic_data = ch.assigned_signal->logic_data();
const shared_ptr<LogicSegment> segment = logic_data->logic_segments().front();
segments.push_back(segment);
- signal_data.push_back(segment->get_samples(start, end));
+
+ uint8_t* data = new uint8_t[(end - start) * segment->unit_size()];
+ segment->get_samples(start, end, data);
+ signal_data.push_back(data);
const int bitpos = ch.assigned_signal->logic_bit_index();
signal_in_bytepos.push_back(bitpos / 8);
// TODO Currently we assume all channels have the same sample rate
// and start time
bool samplerate_valid = false;
+ data::DecodeChannel *any_channel;
+ shared_ptr<Logic> logic_data;
- auto any_channel = find_if(channels_.begin(), channels_.end(),
- [](data::DecodeChannel ch) { return ch.assigned_signal; });
+ do {
+ any_channel = &(*find_if(channels_.begin(), channels_.end(),
+ [](data::DecodeChannel ch) { return ch.assigned_signal; }));
+
+ logic_data = any_channel->assigned_signal->logic_data();
+
+ if (!logic_data) {
+ // Wait until input data is available or an interrupt was requested
+ unique_lock<mutex> input_wait_lock(input_mutex_);
+ decode_input_cond_.wait(input_wait_lock);
+ }
+ } while (!logic_data && !decode_interrupt_);
- shared_ptr<Logic> logic_data =
- any_channel->assigned_signal->logic_data();
+ if (decode_interrupt_)
+ return;
do {
if (!logic_data->logic_segments().empty()) {
const int64_t chunk_end = min(i + chunk_sample_count,
abs_start_samplenum + sample_count);
- const uint8_t* chunk = segment_->get_samples(i, chunk_end);
+ int64_t data_size = (chunk_end - i) * unit_size;
+ uint8_t* chunk = new uint8_t[data_size];
+ segment_->get_samples(i, chunk_end, chunk);
if (srd_session_send(srd_session_, i, chunk_end, chunk,
- (chunk_end - i) * unit_size, unit_size) != SRD_OK) {
+ data_size, unit_size) != SRD_OK) {
error_message_ = tr("Decoder reported an error");
delete[] chunk;
break;
do {
// Keep processing new samples until we exhaust the input data
do {
- // Prevent any other decode threads from accessing libsigrokdecode
- lock_guard<mutex> srd_lock(global_srd_mutex_);
-
- {
- lock_guard<mutex> input_lock(input_mutex_);
- sample_count = segment_->get_sample_count() - abs_start_samplenum;
- }
+ lock_guard<mutex> input_lock(input_mutex_);
+ sample_count = segment_->get_sample_count() - abs_start_samplenum;
if (sample_count > 0) {
decode_data(abs_start_samplenum, sample_count);
void DecodeSignal::annotation_callback(srd_proto_data *pdata, void *decode_signal)
{
assert(pdata);
- assert(decoder);
+ assert(decode_signal);
DecodeSignal *const ds = (DecodeSignal*)decode_signal;
assert(ds);