]> sigrok.org Git - libsigrok.git/blobdiff - bindings/cxx/classes.cpp
session-file: Remove old session save API
[libsigrok.git] / bindings / cxx / classes.cpp
index 3298ce34d0cb1549f7420e8c2b8de547f67e792a..25ac45b6235d166eeb735b50e7b49ac399969e45 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "libsigrok/libsigrokcxx.hpp"
+/* Needed for isascii(), as used in the GNU libstdc++ headers */
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 600
+#endif
+
+#include <config.h>
+#include <libsigrokcxx/libsigrokcxx.hpp>
 
 #include <sstream>
 #include <cmath>
@@ -77,7 +83,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] =
@@ -161,16 +167,6 @@ void Context::set_log_level(const LogLevel *level)
        check(sr_log_loglevel_set(level->id()));
 }
 
-string Context::log_domain()
-{
-       return valid_string(sr_log_logdomain_get());
-}
-
-void Context::set_log_domain(string value)
-{
-       check(sr_log_logdomain_set(value.c_str()));
-}
-
 static int call_log_callback(void *cb_data, int loglevel, const char *format, va_list args)
 {
        va_list args_copy;
@@ -652,8 +648,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()
@@ -668,7 +663,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()
@@ -886,20 +881,18 @@ shared_ptr<Device> SessionDevice::get_shared_from_this()
 
 Session::Session(shared_ptr<Context> context) :
        UserOwned(_structure),
-       _context(context),
-       _saving(false)
+       _context(context)
 {
-       check(sr_session_new(&_structure));
+       check(sr_session_new(context->_structure, &_structure));
        _context->_session = this;
 }
 
 Session::Session(shared_ptr<Context> context, string filename) :
        UserOwned(_structure),
        _context(context),
-       _filename(filename),
-       _saving(false)
+       _filename(filename)
 {
-       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)
@@ -975,93 +968,6 @@ void Session::stop()
        check(sr_session_stop(_structure));
 }
 
-void Session::begin_save(string filename)
-{
-       _saving = true;
-       _save_initialized = false;
-       _save_filename = filename;
-       _save_samplerate = 0;
-}
-
-void Session::append(shared_ptr<Packet> packet)
-{
-       if (!_saving)
-               throw Error(SR_ERR);
-
-       switch (packet->_structure->type)
-       {
-               case SR_DF_META:
-               {
-                       auto meta = (const struct sr_datafeed_meta *)
-                               packet->_structure->payload;
-
-                       for (auto l = meta->config; l; l = l->next)
-                       {
-                               auto config = (struct sr_config *) l->data;
-                               if (config->key == SR_CONF_SAMPLERATE)
-                                       _save_samplerate = g_variant_get_uint64(config->data);
-                       }
-
-                       break;
-               }
-               case SR_DF_LOGIC:
-               {
-                       if (_save_samplerate == 0)
-                       {
-                               GVariant *samplerate;
-
-                               check(sr_config_get(sr_dev_inst_driver_get(packet->_device->_structure),
-                                       packet->_device->_structure, NULL, SR_CONF_SAMPLERATE,
-                                       &samplerate));
-
-                               _save_samplerate = g_variant_get_uint64(samplerate);
-
-                               g_variant_unref(samplerate);
-                       }
-
-                       if (!_save_initialized)
-                       {
-                               vector<shared_ptr<Channel>> save_channels;
-
-                               for (auto channel : packet->_device->channels())
-                                       if (channel->_structure->enabled &&
-                                                       channel->_structure->type == SR_CHANNEL_LOGIC)
-                                               save_channels.push_back(channel);
-
-                               auto channels = g_new(char *, save_channels.size());
-
-                               int i = 0;
-                               for (auto channel : save_channels)
-                                               channels[i++] = channel->_structure->name;
-                               channels[i] = NULL;
-
-                               int ret = sr_session_save_init(_structure, _save_filename.c_str(),
-                                               _save_samplerate, channels);
-
-                               g_free(channels);
-
-                               if (ret != SR_OK)
-                                       throw Error(ret);
-
-                               _save_initialized = true;
-                       }
-
-                       auto logic = (const struct sr_datafeed_logic *)
-                               packet->_structure->payload;
-
-                       check(sr_session_append(_structure, _save_filename.c_str(),
-                               (uint8_t *) logic->data, logic->unitsize,
-                               logic->length / logic->unitsize));
-               }
-       }
-}
-
-void Session::append(void *data, size_t length, unsigned int unit_size)
-{
-       check(sr_session_append(_structure, _save_filename.c_str(),
-               (uint8_t *) data, unit_size, length));
-}
-
 static void datafeed_callback(const struct sr_dev_inst *sdi,
        const struct sr_datafeed_packet *pkt, void *cb_data)
 {
@@ -1385,15 +1291,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;
 }
 
@@ -1427,9 +1345,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);
@@ -1522,15 +1440,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;
 }
 
@@ -1542,10 +1472,33 @@ 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());
+}
+
+bool OutputFormat::test_flag(const OutputFlag *flag)
+{
+       return sr_output_test_flag(_structure, flag->id());
+}
+
 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)
@@ -1573,6 +1526,6 @@ string Output::receive(shared_ptr<Packet> packet)
        }
 }
 
-#include "enums.cpp"
+#include <enums.cpp>
 
 }