* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef _WIN32
#include "data/decode/decoder.hpp"
#include "devices/hardwaredevice.hpp"
+#include "devices/inputfile.hpp"
#include "devices/sessionfile.hpp"
#include "toolbars/mainbar.hpp"
using std::list;
using std::map;
using std::mutex;
+using std::pair;
using std::recursive_mutex;
using std::set;
using std::shared_ptr;
+using std::make_shared;
using std::string;
using std::unordered_set;
using std::vector;
using sigrok::DatafeedCallbackFunction;
using sigrok::Error;
using sigrok::Header;
+using sigrok::InputFormat;
using sigrok::Logic;
using sigrok::Meta;
+using sigrok::OutputFormat;
using sigrok::Packet;
using sigrok::PacketPayload;
using sigrok::Session;
default_name_(name),
name_(name),
capture_state_(Stopped),
- cur_samplerate_(0)
+ cur_samplerate_(0),
+ data_saved_(true)
{
}
return main_bar_;
}
+bool Session::data_saved() const
+{
+ return data_saved_;
+}
+
void Session::save_settings(QSettings &settings) const
{
map<string, string> dev_info;
settings.setValue("device_type", "hardware");
settings.beginGroup("device");
- key_list.push_back("vendor");
- key_list.push_back("model");
- key_list.push_back("version");
- key_list.push_back("serial_num");
- key_list.push_back("connection_id");
+ key_list.emplace_back("vendor");
+ key_list.emplace_back("model");
+ key_list.emplace_back("version");
+ key_list.emplace_back("serial_num");
+ key_list.emplace_back("connection_id");
dev_info = device_manager_.get_device_info(device_);
// Re-select last used device if possible but only if it's not demo
settings.beginGroup("device");
- key_list.push_back("vendor");
- key_list.push_back("model");
- key_list.push_back("version");
- key_list.push_back("serial_num");
- key_list.push_back("connection_id");
+ key_list.emplace_back("vendor");
+ key_list.emplace_back("model");
+ key_list.emplace_back("version");
+ key_list.emplace_back("serial_num");
+ key_list.emplace_back("connection_id");
for (string key : key_list) {
const QString k = QString::fromStdString(key);
settings.endGroup();
if (QFileInfo(filename).isReadable()) {
- device = std::make_shared<devices::SessionFile>(device_manager_.context(),
+ device = make_shared<devices::SessionFile>(device_manager_.context(),
filename.toStdString());
set_device(device);
}
}
+void Session::select_device(shared_ptr<devices::Device> device)
+{
+ try {
+ if (device)
+ set_device(device);
+ else
+ set_default_device();
+ } catch (const QString &e) {
+ main_bar_->session_error(tr("Failed to Select Device"),
+ tr("Failed to Select Device"));
+ }
+}
+
void Session::set_device(shared_ptr<devices::Device> device)
{
assert(device);
device_.reset();
- // Revert name back to default name (e.g. "Untitled-1") as the data is gone
+ // Revert name back to default name (e.g. "Session 1") as the data is gone
name_ = default_name_;
name_changed();
device_->open();
} catch (const QString &e) {
device_.reset();
- device_changed();
- throw;
}
- device_->session()->add_datafeed_callback([=]
- (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
- data_feed_in(device, packet);
- });
+ if (device_) {
+ device_->session()->add_datafeed_callback([=]
+ (shared_ptr<sigrok::Device> device, shared_ptr<Packet> packet) {
+ data_feed_in(device, packet);
+ });
+
+ update_signals();
+ }
- update_signals();
device_changed();
}
set_device((iter == devices.end()) ? devices.front() : *iter);
}
+void Session::load_init_file(const std::string &file_name,
+ const std::string &format)
+{
+ shared_ptr<InputFormat> input_format;
+
+ if (!format.empty()) {
+ const map<string, shared_ptr<InputFormat> > formats =
+ device_manager_.context()->input_formats();
+ const auto iter = find_if(formats.begin(), formats.end(),
+ [&](const pair<string, shared_ptr<InputFormat> > f) {
+ return f.first == format; });
+ if (iter == formats.end()) {
+ main_bar_->session_error(tr("Error"),
+ tr("Unexpected input format: %s").arg(QString::fromStdString(format)));
+ return;
+ }
+
+ input_format = (*iter).second;
+ }
+
+ load_file(QString::fromStdString(file_name), input_format);
+}
+
+void Session::load_file(QString file_name,
+ std::shared_ptr<sigrok::InputFormat> format,
+ const std::map<std::string, Glib::VariantBase> &options)
+{
+ const QString errorMessage(
+ QString("Failed to load file %1").arg(file_name));
+
+ try {
+ if (format)
+ set_device(shared_ptr<devices::Device>(
+ new devices::InputFile(
+ device_manager_.context(),
+ file_name.toStdString(),
+ format, options)));
+ else
+ set_device(shared_ptr<devices::Device>(
+ new devices::SessionFile(
+ device_manager_.context(),
+ file_name.toStdString())));
+ } catch (Error e) {
+ main_bar_->session_error(tr("Failed to load ") + file_name, e.what());
+ set_default_device();
+ main_bar_->update_device_list();
+ return;
+ }
+
+ main_bar_->update_device_list();
+
+ start_capture([&, errorMessage](QString infoMessage) {
+ main_bar_->session_error(errorMessage, infoMessage); });
+
+ set_name(QFileInfo(file_name).fileName());
+}
+
Session::capture_state Session::get_capture_state() const
{
lock_guard<mutex> lock(sampling_mutex_);
for (const shared_ptr<data::SignalData> d : all_signal_data_)
d->clear();
- // Revert name back to default name (e.g. "Untitled-1") as the data is gone
+ // Revert name back to default name (e.g. "Session 1") as the data is gone
name_ = default_name_;
name_changed();
try {
// Create the decoder
- decoder_stack = shared_ptr<data::DecoderStack>(
- new data::DecoderStack(*this, dec));
+ decoder_stack = make_shared<data::DecoderStack>(*this, dec);
// Make a list of all the channels
std::vector<const srd_channel*> all_channels;
// Create the decode signal
shared_ptr<data::SignalBase> signalbase =
- shared_ptr<data::SignalBase>(new data::SignalBase(nullptr));
+ make_shared<data::SignalBase>(nullptr);
signalbase->set_decoder_stack(decoder_stack);
signalbases_.insert(signalbase);
switch(channel->type()->id()) {
case SR_CHANNEL_LOGIC:
if (!signalbase) {
- signalbase = shared_ptr<data::SignalBase>(
- new data::SignalBase(channel));
+ signalbase = make_shared<data::SignalBase>(channel);
signalbases_.insert(signalbase);
all_signal_data_.insert(logic_data_);
case SR_CHANNEL_ANALOG:
{
if (!signalbase) {
- signalbase = shared_ptr<data::SignalBase>(
- new data::SignalBase(channel));
+ signalbase = make_shared<data::SignalBase>(channel);
signalbases_.insert(signalbase);
shared_ptr<data::Analog> data(new data::Analog());
assert(0);
}
+ // Optimize memory usage
+ free_unused_memory();
+
+ // We now have unsaved data unless we just "captured" from a file
+ shared_ptr<devices::File> file_device =
+ dynamic_pointer_cast<devices::File>(device_);
+
+ if (!file_device)
+ data_saved_ = false;
+
if (out_of_memory_)
error_handler(tr("Out of memory, acquisition stopped."));
}
+void Session::free_unused_memory()
+{
+ for (shared_ptr<data::SignalData> data : all_signal_data_) {
+ const vector< shared_ptr<data::Segment> > segments = data->segments();
+
+ for (shared_ptr<data::Segment> segment : segments) {
+ segment->free_unused_memory();
+ }
+ }
+}
+
void Session::feed_in_header()
{
cur_samplerate_ = device_->read_config<uint64_t>(ConfigKey::SAMPLERATE);
{
lock_guard<recursive_mutex> lock(data_mutex_);
- const size_t sample_count = logic->data_length() / logic->unit_size();
-
if (!logic_data_) {
// The only reason logic_data_ would not have been created is
// if it was not possible to determine the signals when the
set_capture_state(Running);
// Create a new data segment
- cur_logic_segment_ = shared_ptr<data::LogicSegment>(
- new data::LogicSegment(
- logic, cur_samplerate_, sample_count));
+ cur_logic_segment_ = make_shared<data::LogicSegment>(
+ *logic_data_, logic, cur_samplerate_);
logic_data_->push_segment(cur_logic_segment_);
// @todo Putting this here means that only listeners querying
// in the sweep containing this segment.
sweep_beginning = true;
- // Create a segment, keep it in the maps of channels
- segment = shared_ptr<data::AnalogSegment>(
- new data::AnalogSegment(
- cur_samplerate_, sample_count));
- cur_analog_segments_[channel] = segment;
-
// Find the analog data associated with the channel
shared_ptr<data::SignalBase> base = signalbase_from_channel(channel);
assert(base);
shared_ptr<data::Analog> data(base->analog_data());
assert(data);
+ // Create a segment, keep it in the maps of channels
+ segment = make_shared<data::AnalogSegment>(
+ *data, cur_samplerate_);
+ cur_analog_segments_[channel] = segment;
+
// Push the segment into the analog data.
data->push_segment(segment);
}
void Session::data_feed_in(shared_ptr<sigrok::Device> device,
shared_ptr<Packet> packet)
{
+ static bool frame_began=false;
+
(void)device;
assert(device);
case SR_DF_FRAME_BEGIN:
feed_in_frame_begin();
+ frame_began = true;
break;
case SR_DF_LOGIC:
}
break;
+ case SR_DF_FRAME_END:
case SR_DF_END:
{
{
cur_logic_segment_.reset();
cur_analog_segments_.clear();
}
- frame_ended();
+ if (frame_began) {
+ frame_began = false;
+ frame_ended();
+ }
break;
}
default:
}
}
+void Session::on_data_saved()
+{
+ data_saved_ = true;
+}
+
} // namespace pv