]> sigrok.org Git - libsigrok.git/commitdiff
Change API of channel accessor functions to take struct sr_channel *.
authorMartin Ling <redacted>
Thu, 19 Mar 2015 21:55:48 +0000 (21:55 +0000)
committerMartin Ling <redacted>
Thu, 19 Mar 2015 21:57:31 +0000 (21:57 +0000)
bindings/cxx/classes.cpp
include/libsigrok/proto.h
src/device.c
src/session_file.c

index a5c9e4cdfa689a011c8ef1e445391d4b1b9b11b6..c08279051b18377726bde6ed20e80f01ba6e4601 100644 (file)
@@ -652,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()
@@ -668,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()
index ea9b8449cf42d620d4f30934dbe2448a0709600b..c2c094379b78afb90f14b1c18c1a1621fb161d7c 100644 (file)
@@ -54,9 +54,9 @@ SR_API char *sr_log_logdomain_get(void);
 
 /*--- device.c --------------------------------------------------------------*/
 
-SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
-               int channelnum, const char *name);
-SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
+SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
+               const char *name);
+SR_API int sr_dev_channel_enable(struct sr_channel *channel,
                gboolean state);
 SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key);
 SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver);
index 78835d4c9709bdf9c2b02169130875809d1c6013..d0b9e482af0440fee5a34f10ad719194fd1a9fec 100644 (file)
@@ -70,53 +70,37 @@ SR_PRIV struct sr_channel *sr_channel_new(struct sr_dev_inst *sdi,
 }
 
 /**
- * Set the name of the specified channel in the specified device.
+ * Set the name of the specified channel.
  *
  * If the channel already has a different name assigned to it, it will be
  * removed, and the new name will be saved instead.
  *
- * @param sdi The device instance the channel is connected to.
- * @param[in] channelnum The number of the channel whose name to set.
- *                 Note that the channel numbers start at 0.
- * @param[in] name The new name that the specified channel should get. A copy
- *             of the string is made.
+ * @param[in] channel The channel whose name to set.
+ * @param[in] name    The new name that the specified channel should get. A
+ *                    copy of the string is made.
  *
  * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
  *
  * @since 0.3.0
  */
-SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
-               int channelnum, const char *name)
+SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
+               const char *name)
 {
-       GSList *l;
-       struct sr_channel *ch;
-       int ret;
-
-       if (!sdi) {
-               sr_err("%s: sdi was NULL", __func__);
+       if (!channel) {
+               sr_err("%s: channel was NULL", __func__);
                return SR_ERR_ARG;
        }
 
-       ret = SR_ERR_ARG;
-       for (l = sdi->channels; l; l = l->next) {
-               ch = l->data;
-               if (ch->index == channelnum) {
-                       g_free(ch->name);
-                       ch->name = g_strdup(name);
-                       ret = SR_OK;
-                       break;
-               }
-       }
-
-       return ret;
+       g_free(channel->name);
+       channel->name = g_strdup(name);
+       return SR_OK;
 }
 
 /**
- * Enable or disable a channel on the specified device.
+ * Enable or disable a channel.
  *
- * @param sdi The device instance the channel is connected to.
- * @param channelnum The channel number, starting from 0.
- * @param state TRUE to enable the channel, FALSE to disable.
+ * @param[in] channel The channel to enable or disable.
+ * @param[in] state   TRUE to enable the channel, FALSE to disable.
  *
  * @return SR_OK on success or SR_ERR on failure.  In case of invalid
  *         arguments, SR_ERR_ARG is returned and the channel enabled state
@@ -124,37 +108,29 @@ SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
  *
  * @since 0.3.0
  */
-SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
+SR_API int sr_dev_channel_enable(struct sr_channel *channel,
                gboolean state)
 {
-       GSList *l;
-       struct sr_channel *ch;
        int ret;
        gboolean was_enabled;
+       struct sr_dev_inst *sdi;
 
-       if (!sdi)
+       if (!channel)
                return SR_ERR_ARG;
 
-       ret = SR_ERR_ARG;
-       for (l = sdi->channels; l; l = l->next) {
-               ch = l->data;
-               if (ch->index == channelnum) {
-                       was_enabled = ch->enabled;
-                       ch->enabled = state;
-                       ret = SR_OK;
-                       if (!state != !was_enabled && sdi->driver
-                                       && sdi->driver->config_channel_set) {
-                               ret = sdi->driver->config_channel_set(
-                                       sdi, ch, SR_CHANNEL_SET_ENABLED);
-                               /* Roll back change if it wasn't applicable. */
-                               if (ret == SR_ERR_ARG)
-                                       ch->enabled = was_enabled;
-                       }
-                       break;
-               }
+       sdi = channel->sdi;
+       was_enabled = channel->enabled;
+       channel->enabled = state;
+       if (!state != !was_enabled && sdi->driver
+                       && sdi->driver->config_channel_set) {
+               ret = sdi->driver->config_channel_set(
+                       sdi, channel, SR_CHANNEL_SET_ENABLED);
+               /* Roll back change if it wasn't applicable. */
+               if (ret != SR_OK)
+                       return ret;
        }
 
-       return ret;
+       return SR_OK;
 }
 
 /**
index e27ed85c0adf3f256b6a76cefbfab62f8922816a..29f26ee2e4dd895e387a5bdf1afcb56c2803ac6c 100644 (file)
@@ -122,6 +122,7 @@ SR_API int sr_session_load(const char *filename, struct sr_session **session)
        struct zip_file *zf;
        struct zip_stat zs;
        struct sr_dev_inst *sdi;
+       struct sr_channel *ch;
        int ret, i, j;
        uint64_t tmp_u64, total_channels, p;
        char **sections, **keys, *metafile, *val;
@@ -219,10 +220,11 @@ SR_API int sr_session_load(const char *filename, struct sr_session **session)
                                                ret = SR_ERR_DATA;
                                                break;
                                        }
-                                       tmp_u64 = strtoul(keys[j]+5, NULL, 10);
+                                       tmp_u64 = strtoul(keys[j]+5, NULL, 10) - 1;
+                                       ch = g_slist_nth_data(sdi->channels, tmp_u64);
                                        /* sr_session_save() */
-                                       sr_dev_channel_name_set(sdi, tmp_u64 - 1, val);
-                                       sr_dev_channel_enable(sdi, tmp_u64 - 1, TRUE);
+                                       sr_dev_channel_name_set(ch, val);
+                                       sr_dev_channel_enable(ch, TRUE);
                                }
                        }
                        g_strfreev(keys);