]> sigrok.org Git - libsigrok.git/commitdiff
hwdriver: Introduce sr_config_commit() API call.
authorDaniel Elstner <redacted>
Sun, 19 Jan 2014 19:39:11 +0000 (20:39 +0100)
committerBert Vermeulen <redacted>
Mon, 20 Jan 2014 13:16:32 +0000 (14:16 +0100)
(sr_dev_driver.config_probe_set): New optional callback enabling
drivers to be notified upon changes to probe settings.
(sr_dev_probe_enable, sr_dev_trigger_set): Invoke new driver
callback on changes.
(sr_dev_driver.config_commit): New optional callback allowing
drivers to defer application of configuration settings until
an explicit call to config_commit().
(sr_config_commit): New public wrapper function.

device.c
hwdriver.c
libsigrok-internal.h
libsigrok.h
proto.h

index ce2ca6554a70776b81a5d45d88fa4c864014896e..c0df1ecc4b4ee48e7f6bd641ab6b4f107e5761eb 100644 (file)
--- a/device.c
+++ b/device.c
@@ -116,7 +116,9 @@ SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
  * @param probenum The probe number, starting from 0.
  * @param state TRUE to enable the probe, FALSE to disable.
  *
- * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
+ * @return SR_OK on success or SR_ERR on failure.  In case of invalid
+ *         arguments, SR_ERR_ARG is returned and the probe enabled state
+ *         remains unchanged.
  *
  * @since 0.2.0
  */
@@ -126,6 +128,7 @@ SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
        GSList *l;
        struct sr_probe *probe;
        int ret;
+       gboolean was_enabled;
 
        if (!sdi)
                return SR_ERR_ARG;
@@ -134,8 +137,17 @@ SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
        for (l = sdi->probes; l; l = l->next) {
                probe = l->data;
                if (probe->index == probenum) {
+                       was_enabled = probe->enabled;
                        probe->enabled = state;
                        ret = SR_OK;
+                       if (!state != !was_enabled && sdi->driver
+                                       && sdi->driver->config_probe_set) {
+                               ret = sdi->driver->config_probe_set(
+                                       sdi, probe, SR_PROBE_SET_ENABLED);
+                               /* Roll back change if it wasn't applicable. */
+                               if (ret == SR_ERR_ARG)
+                                       probe->enabled = was_enabled;
+                       }
                        break;
                }
        }
@@ -153,7 +165,9 @@ SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
  * @param[in] probenum Number of probe, starting at 0.
  * @param[in] trigger Trigger string, in the format used by sigrok-cli
  *
- * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
+ * @return SR_OK on success or SR_ERR on failure.  In case of invalid
+ *         arguments, SR_ERR_ARG is returned and the trigger settings
+ *         remain unchanged.
  *
  * @since 0.2.0
  */
@@ -162,6 +176,7 @@ SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
 {
        GSList *l;
        struct sr_probe *probe;
+       char *old_trigger;
        int ret;
 
        if (!sdi)
@@ -171,10 +186,24 @@ SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
        for (l = sdi->probes; l; l = l->next) {
                probe = l->data;
                if (probe->index == probenum) {
-                       /* If the probe already has a trigger, kill it first. */
-                       g_free(probe->trigger);
-                       probe->trigger = g_strdup(trigger);
+                       old_trigger = probe->trigger;
                        ret = SR_OK;
+                       if (g_strcmp0(trigger, old_trigger) == 0)
+                               break;
+                       /* Set new trigger if it has changed. */
+                       probe->trigger = g_strdup(trigger);
+
+                       if (sdi->driver && sdi->driver->config_probe_set) {
+                               ret = sdi->driver->config_probe_set(
+                                       sdi, probe, SR_PROBE_SET_TRIGGER);
+                               /* Roll back change if it wasn't applicable. */
+                               if (ret == SR_ERR_ARG) {
+                                       g_free(probe->trigger);
+                                       probe->trigger = old_trigger;
+                                       break;
+                               }
+                       }
+                       g_free(old_trigger);
                        break;
                }
        }
index 55358ebf3953a86c7a6476dcb33041b940dd7a12..88b6f638e04722903a96ab79164f6f282a36598d 100644 (file)
@@ -633,6 +633,27 @@ SR_API int sr_config_set(const struct sr_dev_inst *sdi,
        return ret;
 }
 
+/**
+ * Apply configuration settings to the device hardware.
+ *
+ * @param sdi The device instance.
+ *
+ * @return SR_OK upon success or SR_ERR in case of error.
+ */
+SR_API int sr_config_commit(const struct sr_dev_inst *sdi)
+{
+       int ret;
+
+       if (!sdi || !sdi->driver)
+               ret = SR_ERR;
+       else if (!sdi->driver->config_commit)
+               ret = SR_OK;
+       else
+               ret = sdi->driver->config_commit(sdi);
+
+       return ret;
+}
+
 /**
  * List all possible values for a configuration key.
  *
index d7d740d822000685f5d56fc4ef24a6da9e26f851..aa8283f1c070c5eca237e46334692caca589f329 100644 (file)
@@ -174,6 +174,14 @@ SR_PRIV int sr_err(const char *format, ...);
 
 /*--- device.c --------------------------------------------------------------*/
 
+/** Values for the changes argument of sr_dev_driver.config_probe_set. */
+enum {
+       /** The enabled state of the probe has been changed. */
+       SR_PROBE_SET_ENABLED = 1 << 0,
+       /** The trigger setup of the probe has been changed. */
+       SR_PROBE_SET_TRIGGER = 1 << 1,
+};
+
 SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
                gboolean enabled, const char *name);
 
index 7fc48cfdede3a1e8f46e5282b463048f44d20470..ab095c9a45630b83207f64bf0c86da386d3fb33c 100644 (file)
@@ -975,6 +975,9 @@ struct sr_dev_driver {
        int (*config_set) (int id, GVariant *data,
                        const struct sr_dev_inst *sdi,
                        const struct sr_probe_group *probe_group);
+       int (*config_probe_set) (const struct sr_dev_inst *sdi,
+                       struct sr_probe *probe, unsigned int changes);
+       int (*config_commit) (const struct sr_dev_inst *sdi);
        int (*config_list) (int info_id, GVariant **data,
                        const struct sr_dev_inst *sdi,
                        const struct sr_probe_group *probe_group);
diff --git a/proto.h b/proto.h
index 57c7f2d98130cc3dd0c23a0b4a8864cb1ef5604b..2b36264e8240066372bf29dc65b713f988e5174b 100644 (file)
--- a/proto.h
+++ b/proto.h
@@ -77,6 +77,7 @@ SR_API int sr_config_get(const struct sr_dev_driver *driver,
 SR_API int sr_config_set(const struct sr_dev_inst *sdi,
                const struct sr_probe_group *probe_group,
                int key, GVariant *data);
+SR_API int sr_config_commit(const struct sr_dev_inst *sdi);
 SR_API int sr_config_list(const struct sr_dev_driver *driver,
                const struct sr_dev_inst *sdi,
                const struct sr_probe_group *probe_group,