]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
C++: Fix segfault where input/output options are NULL.
[libsigrok.git] / bindings / cxx / classes.cpp
index 7becfb65e4ae472b44c0843e96d3f26731ea8089..bb709fe2613092a2a78c2ff7af7810ec4204b95c 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>
@@ -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),
@@ -1138,7 +1152,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 +1165,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),
@@ -1365,12 +1388,15 @@ string InputFormat::description()
 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;
 }
 
@@ -1502,12 +1528,15 @@ string OutputFormat::description()
 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;
 }