From: Martin Ling Date: Thu, 19 Mar 2015 21:55:48 +0000 (+0000) Subject: Change API of channel accessor functions to take struct sr_channel *. X-Git-Tag: libsigrok-0.4.0~598 X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=6f1346fbd7bbb6cca97d8a6c02b36156fce24ec2 Change API of channel accessor functions to take struct sr_channel *. --- diff --git a/bindings/cxx/classes.cpp b/bindings/cxx/classes.cpp index a5c9e4cd..c0827905 100644 --- a/bindings/cxx/classes.cpp +++ b/bindings/cxx/classes.cpp @@ -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() diff --git a/include/libsigrok/proto.h b/include/libsigrok/proto.h index ea9b8449..c2c09437 100644 --- a/include/libsigrok/proto.h +++ b/include/libsigrok/proto.h @@ -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); diff --git a/src/device.c b/src/device.c index 78835d4c..d0b9e482 100644 --- a/src/device.c +++ b/src/device.c @@ -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; } /** diff --git a/src/session_file.c b/src/session_file.c index e27ed85c..29f26ee2 100644 --- a/src/session_file.c +++ b/src/session_file.c @@ -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);