]> sigrok.org Git - libsigrokflow.git/blobdiff - src/main.cpp
Check for the (optional) libsigrokcxx dependency.
[libsigrokflow.git] / src / main.cpp
index b80d203c804e77853dbe0da7cb19a0cfda5d880a..080ef64376e5fe69d66dcbfc5402f909cb6debc1 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * This file is part of the libsigrokflow project.
  *
+ * Copyright (C) 2018 Martin Ling <martin-sigrok@earth.li>
  * Copyright (C) 2018 Uwe Hermann <uwe@hermann-uwe.de>
  *
  * This program is free software: you can redistribute it and/or modify
@@ -32,16 +33,23 @@ using namespace std::placeholders;
 
 void init()
 {
+#ifdef HAVE_LIBSIGROKCXX
        Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
                        "sigrok_legacy_capture_device",
                        "Wrapper for capture devices using legacy libsigrok APIs",
                        sigc::ptr_fun(&LegacyCaptureDevice::register_element),
                        "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
+       Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
+                       "sigrok_legacy_input",
+                       "Wrapper for inputs using legacy libsigrok APIs",
+                       sigc::ptr_fun(&LegacyInput::register_element),
+                       "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
        Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
                        "sigrok_legacy_output",
                        "Wrapper for outputs using legacy libsigrok APIs",
                        sigc::ptr_fun(&LegacyOutput::register_element),
                        "0.01", "GPL", "sigrok", "libsigrokflow", "http://sigrok.org");
+#endif
        Gst::Plugin::register_static(GST_VERSION_MAJOR, GST_VERSION_MINOR,
                        "sigrok_legacy_decoder",
                        "Wrapper for protocol decoders using legacy libsigrokdecode APIs",
@@ -64,6 +72,7 @@ CaptureDevice::CaptureDevice(GstElement *gobj) :
 {
 }
 
+#ifdef HAVE_LIBSIGROKCXX
 void LegacyCaptureDevice::class_init(Gst::ElementClass<LegacyCaptureDevice> *klass)
 {
        klass->set_metadata("sigrok legacy capture device",
@@ -164,6 +173,111 @@ void LegacyCaptureDevice::_run()
        _task->stop();
 }
 
+void LegacyInput::class_init(Gst::ElementClass<LegacyInput> *klass)
+{
+       klass->set_metadata("sigrok legacy input",
+                       "Transform", "Wrapper for inputs using legacy libsigrok APIs",
+                       "Martin Ling");
+
+       klass->add_pad_template(Gst::PadTemplate::create(
+                       "sink",
+                       Gst::PAD_SINK,
+                       Gst::PAD_ALWAYS,
+                       Gst::Caps::create_any()));
+
+       klass->add_pad_template(Gst::PadTemplate::create(
+                       "src",
+                       Gst::PAD_SRC,
+                       Gst::PAD_ALWAYS,
+                       Gst::Caps::create_any()));
+}
+
+bool LegacyInput::register_element(Glib::RefPtr<Gst::Plugin> plugin)
+{
+       Gst::ElementFactory::register_element(plugin, "sigrok_legacy_input",
+                       0, Gst::register_mm_type<LegacyInput>(
+                               "sigrok_legacy_input"));
+       return true;
+}
+
+LegacyInput::LegacyInput(GstElement *gobj) :
+       Glib::ObjectBase(typeid(LegacyInput)),
+       Gst::Element(gobj)
+{
+       add_pad(_sink_pad = Gst::Pad::create(get_pad_template("sink"), "sink"));
+       add_pad(_src_pad = Gst::Pad::create(get_pad_template("src"), "src"));
+       _sink_pad->set_chain_function(sigc::mem_fun(*this, &LegacyInput::chain));
+}
+
+Glib::RefPtr<LegacyInput> LegacyInput::create(
+       shared_ptr<sigrok::InputFormat> libsigrok_input_format,
+       map<string, Glib::VariantBase> options)
+{
+       auto element = Gst::ElementFactory::create_element("sigrok_legacy_input");
+       if (!element)
+               throw runtime_error("Failed to create element - plugin not registered?");
+       auto input = Glib::RefPtr<LegacyInput>::cast_static(element);
+       input->_libsigrok_input_format = libsigrok_input_format;
+       input->_options = options;
+       return input;
+}
+
+bool LegacyInput::start_vfunc()
+{
+       _libsigrok_input = _libsigrok_input_format->create_input(_options);
+       auto context = _libsigrok_input_format->parent();
+       _session = context->create_session();
+       _session->add_device(_libsigrok_input->device());
+       _session->add_datafeed_callback(bind(&LegacyInput::_datafeed_callback, this, _1, _2));
+       _session->start();
+       return true;
+}
+
+void LegacyInput::_datafeed_callback(
+       shared_ptr<sigrok::Device> device,
+       shared_ptr<sigrok::Packet> packet)
+{
+       (void) device;
+       switch (packet->type()->id()) {
+               case SR_DF_LOGIC:
+               {
+                       auto logic = static_pointer_cast<sigrok::Logic>(packet->payload());
+                       auto mem = Gst::Memory::create(
+                                       Gst::MEMORY_FLAG_READONLY,
+                                       logic->data_pointer(),
+                                       logic->data_length(),
+                                       0,
+                                       logic->data_length());
+                       auto buf = Gst::Buffer::create();
+                       buf->append_memory(move(mem));
+                       _src_pad->push(move(buf));
+                       break;
+               }
+               case SR_DF_END:
+                       _session->stop();
+                       _src_pad->push_event(Gst::EventEos::create());
+                       break;
+               default:
+                       break;
+       }
+}
+
+Gst::FlowReturn LegacyInput::chain(const Glib::RefPtr<Gst::Pad> &,
+                        const Glib::RefPtr<Gst::Buffer> &buf)
+{
+       Gst::MapInfo info;
+       buf->map(info, Gst::MAP_READ);
+       _libsigrok_input->send(info.get_data(), info.get_size());
+       buf->unmap(info);
+       return Gst::FLOW_OK;
+}
+
+bool LegacyInput::stop_vfunc()
+{
+       _libsigrok_input->end();
+       return true;
+}
+
 void LegacyOutput::class_init(Gst::ElementClass<LegacyOutput> *klass)
 {
        klass->set_metadata("sigrok legacy output",
@@ -234,6 +348,7 @@ bool LegacyOutput::stop_vfunc()
        cout << result;
        return true;
 }
+#endif
 
 void LegacyDecoder::class_init(Gst::ElementClass<LegacyDecoder> *klass)
 {