]> sigrok.org Git - libsigrok.git/blobdiff - src/output/output.c
output: Finish output module API wrappers.
[libsigrok.git] / src / output / output.c
index 4cf78f60a8b201d2ba6c60a9d6e50702d750ba5e..6406fcaceb3e5c75b9763982f14084c7c78297d9 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <string.h>
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
+#define LOG_PREFIX "output"
+
 /**
  * @file
  *
- * Output file/data format handling.
+ * Output module handling.
  */
 
 /**
- * @defgroup grp_output Output formats
+ * @defgroup grp_output Output modules
  *
- * Output file/data format handling.
+ * Output module handling.
  *
- * libsigrok supports several output (file) formats, e.g. binary, VCD,
- * gnuplot, and so on. It provides an output API that frontends can use.
- * New output formats can be added/implemented in libsigrok without having
+ * libsigrok supports several output modules for file formats such as binary,
+ * VCD, gnuplot, and so on. It provides an output API that frontends can use.
+ * New output modules can be added/implemented in libsigrok without having
  * to change the frontends at all.
  *
  * All output modules are fed data in a stream. Devices that can stream data
  */
 
 /** @cond PRIVATE */
-extern SR_PRIV struct sr_output_format output_bits;
-extern SR_PRIV struct sr_output_format output_hex;
-extern SR_PRIV struct sr_output_format output_ascii;
-extern SR_PRIV struct sr_output_format output_binary;
-extern SR_PRIV struct sr_output_format output_vcd;
-extern SR_PRIV struct sr_output_format output_ols;
-extern SR_PRIV struct sr_output_format output_gnuplot;
-extern SR_PRIV struct sr_output_format output_chronovu_la8;
-extern SR_PRIV struct sr_output_format output_csv;
-extern SR_PRIV struct sr_output_format output_analog;
+extern SR_PRIV struct sr_output_module output_bits;
+extern SR_PRIV struct sr_output_module output_hex;
+extern SR_PRIV struct sr_output_module output_ascii;
+extern SR_PRIV struct sr_output_module output_binary;
+extern SR_PRIV struct sr_output_module output_vcd;
+extern SR_PRIV struct sr_output_module output_ols;
+extern SR_PRIV struct sr_output_module output_gnuplot;
+extern SR_PRIV struct sr_output_module output_chronovu_la8;
+extern SR_PRIV struct sr_output_module output_csv;
+extern SR_PRIV struct sr_output_module output_analog;
 /* @endcond */
 
-static struct sr_output_format *output_module_list[] = {
+static const struct sr_output_module *output_module_list[] = {
        &output_ascii,
        &output_binary,
        &output_bits,
@@ -73,46 +76,182 @@ static struct sr_output_format *output_module_list[] = {
        NULL,
 };
 
-/** @since 0.1.0 */
-SR_API struct sr_output_format **sr_output_list(void)
+/**
+ * Returns a NULL-terminated list of all the available output modules.
+ *
+ * @since 0.4.0
+ */
+SR_API const struct sr_output_module **sr_output_list(void)
 {
        return output_module_list;
 }
 
-/** @since 0.3.0 */
-SR_API struct sr_output *sr_output_new(struct sr_output_format *of,
-               GHashTable *params, const struct sr_dev_inst *sdi)
+/**
+ * Returns the specified output module's ID.
+ *
+ * @since 0.4.0
+ */
+SR_API const char *sr_output_id_get(const struct sr_output_module *o)
+{
+       if (!o) {
+               sr_err("Invalid output module NULL!");
+               return NULL;
+       }
+
+       return o->id;
+}
+
+/**
+ * Returns the specified output module's name.
+ *
+ * @since 0.4.0
+ */
+SR_API const char *sr_output_name_get(const struct sr_output_module *o)
+{
+       if (!o) {
+               sr_err("Invalid output module NULL!");
+               return NULL;
+       }
+
+       return o->name;
+}
+
+/**
+ * Returns the specified output module's description.
+ *
+ * @since 0.4.0
+ */
+SR_API const char *sr_output_description_get(const struct sr_output_module *o)
+{
+       if (!o) {
+               sr_err("Invalid output module NULL!");
+               return NULL;
+       }
+
+       return o->desc;
+}
+
+/**
+ * Return the output module with the specified ID, or NULL if no module
+ * with that id is found.
+ *
+ * @since 0.4.0
+ */
+SR_API const struct sr_output_module *sr_output_find(char *id)
+{
+       int i;
+
+       for (i = 0; output_module_list[i]; i++) {
+               if (!strcmp(output_module_list[i]->id, id))
+                       return output_module_list[i];
+       }
+
+       return NULL;
+}
+
+/**
+ * Returns a NULL-terminated array of struct sr_option, or NULL if the
+ * module takes no options.
+ *
+ * Each call to this function must be followed by a call to
+ * sr_output_options_free().
+ *
+ * @since 0.4.0
+ */
+SR_API const struct sr_option *sr_output_options_get(const struct sr_output_module *o)
+{
+
+       if (!o || !o->options)
+               return NULL;
+
+       return o->options(FALSE);
+}
+
+/**
+ * After a call to sr_output_options_get(), this function cleans up all
+ * the resources allocated by that call.
+ *
+ * @since 0.4.0
+ */
+SR_API void sr_output_options_free(const struct sr_output_module *o)
 {
-       struct sr_output *o;
-
-       o = g_malloc(sizeof(struct sr_output));
-       o->format = of;
-       o->sdi = sdi;
-       o->params = params;
-       if (o->format->init && o->format->init(o) != SR_OK) {
-               g_free(o);
-               o = NULL;
+       struct sr_option *opt;
+
+       if (!o || !o->options)
+               return;
+
+       for (opt = o->options(TRUE); opt->id; opt++) {
+               if (opt->def) {
+                       g_variant_unref(opt->def);
+                       opt->def = NULL;
+               }
+
+               if (opt->values) {
+                       g_slist_free_full(opt->values, (GDestroyNotify)g_variant_unref);
+                       opt->values = NULL;
+               }
        }
+}
 
-       return o;
+/**
+ * Create a new output instance using the specified output module.
+ *
+ * <code>options</code> is a *HashTable with the keys corresponding with
+ * the module options' <code>id</code> field. The values should be GVariant
+ * pointers with sunk * references, of the same GVariantType as the option's
+ * default value.
+ *
+ * The sr_dev_inst passed in can be used by the instance to determine
+ * channel names, samplerate, and so on.
+ *
+ * @since 0.4.0
+ */
+SR_API const struct sr_output *sr_output_new(const struct sr_output_module *o,
+               GHashTable *options, const struct sr_dev_inst *sdi)
+{
+       struct sr_output *op;
+
+       op = g_malloc(sizeof(struct sr_output));
+       op->module = o;
+       op->sdi = sdi;
+       if (op->module->init && op->module->init(op, options) != SR_OK) {
+               g_free(op);
+               op = NULL;
+       }
+
+       return op;
 }
 
-/** @since 0.3.0 */
-SR_API int sr_output_send(struct sr_output *o,
+/**
+ * Send a packet to the specified output instance.
+ *
+ * The instance's output is returned as a newly allocated GString,
+ * which must be freed by the caller.
+ *
+ * @since 0.4.0
+ */
+SR_API int sr_output_send(const struct sr_output *o,
                const struct sr_datafeed_packet *packet, GString **out)
 {
-       return o->format->receive(o, packet, out);
+       return o->module->receive(o, packet, out);
 }
 
-/** @since 0.3.0 */
-SR_API int sr_output_free(struct sr_output *o)
+/**
+ * Free the specified output instance and all associated resources.
+ *
+ * @since 0.4.0
+ */
+SR_API int sr_output_free(const struct sr_output *o)
 {
        int ret;
 
+       if (!o)
+               return SR_ERR_ARG;
+
        ret = SR_OK;
-       if (o->format->cleanup)
-               ret = o->format->cleanup(o);
-       g_free(o);
+       if (o->module->cleanup)
+               ret = o->module->cleanup((struct sr_output *)o);
+       g_free((gpointer)o);
 
        return ret;
 }