]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
Add filename field to sr_output and make it accessible
[libsigrok.git] / bindings / cxx / classes.cpp
index 7becfb65e4ae472b44c0843e96d3f26731ea8089..357c9d6f3cbccac46f24a7d1c74025c525b40324 100644 (file)
@@ -17,7 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "libsigrok/libsigrok.hpp"
+#include "libsigrokcxx/libsigrokcxx.hpp"
 
 #include <sstream>
 #include <cmath>
@@ -77,7 +77,7 @@ Context::Context() :
 {
        check(sr_init(&_structure));
 
-       struct sr_dev_driver **driver_list = sr_driver_list();
+       struct sr_dev_driver **driver_list = sr_driver_list(_structure);
        if (driver_list)
                for (int i = 0; driver_list[i]; i++)
                        _drivers[driver_list[i]->name] =
@@ -318,6 +318,20 @@ shared_ptr<Input> Context::open_stream(string header)
                new Input(shared_from_this(), input), Input::Deleter());
 }
 
+map<string, string> Context::serials(shared_ptr<Driver> driver)
+{
+       GSList *serial_list = sr_serial_list(driver ? driver->_structure : NULL);
+       map<string, string> serials;
+
+       for (GSList *serial = serial_list; serial; serial = serial->next) {
+               struct sr_serial_port *port = (sr_serial_port *) serial->data;
+               serials[string(port->name)] = string(port->description);
+       }
+
+       g_slist_free_full(serial_list, (GDestroyNotify)sr_serial_free);
+       return serials;
+}
+
 Driver::Driver(struct sr_dev_driver *structure) :
        ParentOwned(structure),
        Configurable(structure, NULL, NULL),
@@ -638,8 +652,7 @@ string Channel::name()
 
 void Channel::set_name(string name)
 {
-       check(sr_dev_channel_name_set(_parent->_structure,
-               _structure->index, name.c_str()));
+       check(sr_dev_channel_name_set(_structure, name.c_str()));
 }
 
 const ChannelType *Channel::type()
@@ -654,7 +667,7 @@ bool Channel::enabled()
 
 void Channel::set_enabled(bool value)
 {
-       check(sr_dev_channel_enable(_parent->_structure, _structure->index, value));
+       check(sr_dev_channel_enable(_structure, value));
 }
 
 unsigned int Channel::index()
@@ -875,7 +888,7 @@ Session::Session(shared_ptr<Context> context) :
        _context(context),
        _saving(false)
 {
-       check(sr_session_new(&_structure));
+       check(sr_session_new(context->_structure, &_structure));
        _context->_session = this;
 }
 
@@ -885,7 +898,7 @@ Session::Session(shared_ptr<Context> context, string filename) :
        _filename(filename),
        _saving(false)
 {
-       check(sr_session_load(filename.c_str(), &_structure));
+       check(sr_session_load(context->_structure, filename.c_str(), &_structure));
        GSList *dev_list;
        check(sr_session_dev_list(_structure, &dev_list));
        for (GSList *dev = dev_list; dev; dev = dev->next)
@@ -1138,7 +1151,11 @@ shared_ptr<Trigger> Session::trigger()
 
 void Session::set_trigger(shared_ptr<Trigger> trigger)
 {
-       check(sr_session_trigger_set(_structure, trigger->_structure));
+       if (!trigger)
+               // Set NULL trigger, i.e. remove any trigger from the session.
+               check(sr_session_trigger_set(_structure, NULL));
+       else
+               check(sr_session_trigger_set(_structure, trigger->_structure));
        _trigger = trigger;
 }
 
@@ -1147,6 +1164,11 @@ string Session::filename()
        return _filename;
 }
 
+shared_ptr<Context> Session::context()
+{
+       return _context;
+}
+
 Packet::Packet(shared_ptr<Device> device,
        const struct sr_datafeed_packet *structure) :
        UserOwned(structure),
@@ -1362,15 +1384,27 @@ string InputFormat::description()
        return valid_string(sr_input_description_get(_structure));
 }
 
+vector<string> InputFormat::extensions()
+{
+       vector<string> exts;
+       for (const char *const *e = sr_input_extensions_get(_structure);
+               e && *e; e++)
+               exts.push_back(*e);
+       return exts;
+}
+
 map<string, shared_ptr<Option>> InputFormat::options()
 {
        const struct sr_option **options = sr_input_options_get(_structure);
-       auto option_array = shared_ptr<const struct sr_option *>(
-               options, sr_input_options_free);
        map<string, shared_ptr<Option>> result;
-       for (int i = 0; options[i]; i++)
-               result[options[i]->id] = shared_ptr<Option>(
-                       new Option(options[i], option_array), Option::Deleter());
+       if (options)
+       {
+               auto option_array = shared_ptr<const struct sr_option *>(
+                       options, sr_input_options_free);
+               for (int i = 0; options[i]; i++)
+                       result[options[i]->id] = shared_ptr<Option>(
+                               new Option(options[i], option_array), Option::Deleter());
+       }
        return result;
 }
 
@@ -1404,9 +1438,9 @@ shared_ptr<InputDevice> Input::device()
        return _device->get_shared_pointer(shared_from_this());
 }
 
-void Input::send(string data)
+void Input::send(void *data, size_t length)
 {
-       auto gstr = g_string_new(data.c_str());
+       auto gstr = g_string_new_len((gchar *)data, length);
        auto ret = sr_input_send(_structure, gstr);
        g_string_free(gstr, false);
        check(ret);
@@ -1499,15 +1533,27 @@ string OutputFormat::description()
        return valid_string(sr_output_description_get(_structure));
 }
 
+vector<string> OutputFormat::extensions()
+{
+       vector<string> exts;
+       for (const char *const *e = sr_output_extensions_get(_structure);
+               e && *e; e++)
+               exts.push_back(*e);
+       return exts;
+}
+
 map<string, shared_ptr<Option>> OutputFormat::options()
 {
        const struct sr_option **options = sr_output_options_get(_structure);
-       auto option_array = shared_ptr<const struct sr_option *>(
-               options, sr_output_options_free);
        map<string, shared_ptr<Option>> result;
-       for (int i = 0; options[i]; i++)
-               result[options[i]->id] = shared_ptr<Option>(
-                       new Option(options[i], option_array), Option::Deleter());
+       if (options)
+       {
+               auto option_array = shared_ptr<const struct sr_option *>(
+                       options, sr_output_options_free);
+               for (int i = 0; options[i]; i++)
+                       result[options[i]->id] = shared_ptr<Option>(
+                               new Option(options[i], option_array), Option::Deleter());
+       }
        return result;
 }
 
@@ -1519,10 +1565,28 @@ shared_ptr<Output> OutputFormat::create_output(
                Output::Deleter());
 }
 
+shared_ptr<Output> OutputFormat::create_output(string filename,
+       shared_ptr<Device> device, map<string, Glib::VariantBase> options)
+{
+       return shared_ptr<Output>(
+               new Output(filename, shared_from_this(), device, options),
+               Output::Deleter());
+}
+
 Output::Output(shared_ptr<OutputFormat> format,
                shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
        UserOwned(sr_output_new(format->_structure,
-               map_to_hash_variant(options), device->_structure)),
+               map_to_hash_variant(options), device->_structure, NULL)),
+       _format(format),
+       _device(device),
+       _options(options)
+{
+}
+
+Output::Output(string filename, shared_ptr<OutputFormat> format,
+               shared_ptr<Device> device, map<string, Glib::VariantBase> options) :
+       UserOwned(sr_output_new(format->_structure,
+               map_to_hash_variant(options), device->_structure, filename.c_str())),
        _format(format),
        _device(device),
        _options(options)