]> sigrok.org Git - pulseview.git/blame - sigsession.cpp
Fixes to LargeData test checks
[pulseview.git] / sigsession.cpp
CommitLineData
2953961c
JH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2012 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 "sigsession.h"
22
28a4c9c5
JH
23#include "logicdata.h"
24#include "logicdatasnapshot.h"
e3f65ace 25#include "logicsignal.h"
28a4c9c5 26
009e1503
JH
27#include <QDebug>
28
2953961c
JH
29#include <assert.h>
30
28a4c9c5 31using namespace boost;
e3f65ace 32using namespace std;
28a4c9c5 33
2953961c 34// TODO: This should not be necessary
04abfae9 35SigSession* SigSession::_session = NULL;
2953961c 36
28a4c9c5 37SigSession::SigSession()
2953961c
JH
38{
39 // TODO: This should not be necessary
04abfae9 40 _session = this;
2953961c
JH
41}
42
43SigSession::~SigSession()
44{
45 // TODO: This should not be necessary
04abfae9 46 _session = NULL;
2953961c
JH
47}
48
04abfae9 49void SigSession::load_file(const std::string &name)
2953961c
JH
50{
51 if (sr_session_load(name.c_str()) == SR_OK) {
52 /* sigrok session file */
04abfae9 53 sr_session_datafeed_callback_add(data_feed_in_proc);
2953961c
JH
54 sr_session_start();
55 sr_session_run();
56 sr_session_stop();
57 }
58}
59
274d4f13 60void SigSession::start_capture(struct sr_dev_inst *sdi,
215f9499 61 uint64_t record_length, uint64_t sample_rate)
274d4f13
JH
62{
63 sr_session_new();
04abfae9 64 sr_session_datafeed_callback_add(data_feed_in_proc);
274d4f13
JH
65
66 if (sr_session_dev_add(sdi) != SR_OK) {
67 qDebug() << "Failed to use device.";
68 sr_session_destroy();
69 return;
70 }
71
274d4f13 72 if (sr_dev_config_set(sdi, SR_HWCAP_LIMIT_SAMPLES,
215f9499 73 &record_length) != SR_OK) {
274d4f13
JH
74 qDebug() << "Failed to configure time-based sample limit.";
75 sr_session_destroy();
76 return;
77 }
78
79 if (sr_dev_config_set(sdi, SR_HWCAP_SAMPLERATE,
80 &sample_rate) != SR_OK) {
81 qDebug() << "Failed to configure samplerate.";
82 sr_session_destroy();
83 return;
84 }
85
86 if (sr_session_start() != SR_OK) {
87 qDebug() << "Failed to start session.";
88 return;
89 }
90
91 sr_session_run();
92 sr_session_destroy();
93}
94
e3f65ace
JH
95vector< shared_ptr<Signal> >& SigSession::get_signals()
96{
97 return _signals;
98}
99
adb4b10c
JH
100boost::shared_ptr<LogicData> SigSession::get_data()
101{
102 return _logic_data;
103}
104
04abfae9 105void SigSession::data_feed_in(const struct sr_dev_inst *sdi,
2953961c
JH
106 struct sr_datafeed_packet *packet)
107{
108 assert(sdi);
109 assert(packet);
110
111 switch (packet->type) {
28a4c9c5 112 case SR_DF_HEADER:
e3f65ace 113 _signals.clear();
009e1503
JH
114 break;
115
28a4c9c5 116 case SR_DF_META_LOGIC:
009e1503 117 {
28a4c9c5 118 assert(packet->payload);
009e1503 119
e3f65ace
JH
120 const sr_datafeed_meta_logic &meta_logic =
121 *(sr_datafeed_meta_logic*)packet->payload;
009e1503 122
e3f65ace
JH
123 // Create an empty LogiData for coming data snapshots
124 _logic_data.reset(new LogicData(meta_logic));
28a4c9c5
JH
125 assert(_logic_data);
126 if(!_logic_data)
127 break;
009e1503 128
e3f65ace
JH
129 // Add the signals
130 for (int i = 0; i < meta_logic.num_probes; i++)
131 {
132 const sr_probe *const probe =
133 (const sr_probe*)g_slist_nth_data(
134 sdi->probes, i);
135 if(probe->enabled)
136 {
137 boost::shared_ptr<LogicSignal> signal(
138 new LogicSignal(probe->name,
139 _logic_data,
140 probe->index));
141 _signals.push_back(signal);
142 }
143 }
144
28a4c9c5 145 break;
2953961c 146 }
28a4c9c5
JH
147
148 case SR_DF_LOGIC:
f556bc6a 149
28a4c9c5 150 assert(packet->payload);
f556bc6a
JH
151 if(!_cur_logic_snapshot)
152 {
153 // Create a new data snapshot
154 _cur_logic_snapshot = shared_ptr<LogicDataSnapshot>(
155 new LogicDataSnapshot(
156 *(sr_datafeed_logic*)packet->payload));
157 _logic_data->push_snapshot(_cur_logic_snapshot);
158 }
159 else
160 {
161 // Append to the existing data snapshot
28a4c9c5
JH
162 _cur_logic_snapshot->append_payload(
163 *(sr_datafeed_logic*)packet->payload);
f556bc6a
JH
164 }
165
2953961c
JH
166 break;
167
168 case SR_DF_END:
28a4c9c5 169 _cur_logic_snapshot.reset();
04abfae9 170 data_updated();
2953961c
JH
171 break;
172 }
173}
174
04abfae9 175void SigSession::data_feed_in_proc(const struct sr_dev_inst *sdi,
2953961c
JH
176 struct sr_datafeed_packet *packet)
177{
04abfae9
JH
178 assert(_session);
179 _session->data_feed_in(sdi, packet);
2953961c 180}