Change API of channel accessor functions to take struct sr_channel *.

This commit is contained in:
Martin Ling 2015-03-19 21:55:48 +00:00
parent 837b08660a
commit 6f1346fbd7
4 changed files with 38 additions and 61 deletions

View File

@ -652,8 +652,7 @@ string Channel::name()
void Channel::set_name(string name) void Channel::set_name(string name)
{ {
check(sr_dev_channel_name_set(_parent->_structure, check(sr_dev_channel_name_set(_structure, name.c_str()));
_structure->index, name.c_str()));
} }
const ChannelType *Channel::type() const ChannelType *Channel::type()
@ -668,7 +667,7 @@ bool Channel::enabled()
void Channel::set_enabled(bool value) 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() unsigned int Channel::index()

View File

@ -54,9 +54,9 @@ SR_API char *sr_log_logdomain_get(void);
/*--- device.c --------------------------------------------------------------*/ /*--- device.c --------------------------------------------------------------*/
SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi, SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
int channelnum, const char *name); const char *name);
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); gboolean state);
SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key); 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); SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver);

View 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 * If the channel already has a different name assigned to it, it will be
* removed, and the new name will be saved instead. * removed, and the new name will be saved instead.
* *
* @param sdi The device instance the channel is connected to. * @param[in] channel The channel whose name to set.
* @param[in] channelnum The number of the channel whose name to set. * @param[in] name The new name that the specified channel should get. A
* Note that the channel numbers start at 0. * copy of the string is made.
* @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. * @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
* *
* @since 0.3.0 * @since 0.3.0
*/ */
SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi, SR_API int sr_dev_channel_name_set(struct sr_channel *channel,
int channelnum, const char *name) const char *name)
{ {
GSList *l; if (!channel) {
struct sr_channel *ch; sr_err("%s: channel was NULL", __func__);
int ret;
if (!sdi) {
sr_err("%s: sdi was NULL", __func__);
return SR_ERR_ARG; return SR_ERR_ARG;
} }
ret = SR_ERR_ARG; g_free(channel->name);
for (l = sdi->channels; l; l = l->next) { channel->name = g_strdup(name);
ch = l->data; return SR_OK;
if (ch->index == channelnum) {
g_free(ch->name);
ch->name = g_strdup(name);
ret = SR_OK;
break;
}
}
return ret;
} }
/** /**
* 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[in] channel The channel to enable or disable.
* @param channelnum The channel number, starting from 0. * @param[in] state TRUE to enable the channel, FALSE to disable.
* @param state TRUE to enable the channel, FALSE to disable.
* *
* @return SR_OK on success or SR_ERR on failure. In case of invalid * @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 * 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 * @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) gboolean state)
{ {
GSList *l;
struct sr_channel *ch;
int ret; int ret;
gboolean was_enabled; gboolean was_enabled;
struct sr_dev_inst *sdi;
if (!sdi) if (!channel)
return SR_ERR_ARG; return SR_ERR_ARG;
ret = SR_ERR_ARG; sdi = channel->sdi;
for (l = sdi->channels; l; l = l->next) { was_enabled = channel->enabled;
ch = l->data; channel->enabled = state;
if (ch->index == channelnum) { if (!state != !was_enabled && sdi->driver
was_enabled = ch->enabled; && sdi->driver->config_channel_set) {
ch->enabled = state; ret = sdi->driver->config_channel_set(
ret = SR_OK; sdi, channel, SR_CHANNEL_SET_ENABLED);
if (!state != !was_enabled && sdi->driver /* Roll back change if it wasn't applicable. */
&& sdi->driver->config_channel_set) { if (ret != SR_OK)
ret = sdi->driver->config_channel_set( return ret;
sdi, ch, SR_CHANNEL_SET_ENABLED);
/* Roll back change if it wasn't applicable. */
if (ret == SR_ERR_ARG)
ch->enabled = was_enabled;
}
break;
}
} }
return ret; return SR_OK;
} }
/** /**

View File

@ -122,6 +122,7 @@ SR_API int sr_session_load(const char *filename, struct sr_session **session)
struct zip_file *zf; struct zip_file *zf;
struct zip_stat zs; struct zip_stat zs;
struct sr_dev_inst *sdi; struct sr_dev_inst *sdi;
struct sr_channel *ch;
int ret, i, j; int ret, i, j;
uint64_t tmp_u64, total_channels, p; uint64_t tmp_u64, total_channels, p;
char **sections, **keys, *metafile, *val; 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; ret = SR_ERR_DATA;
break; 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_session_save() */
sr_dev_channel_name_set(sdi, tmp_u64 - 1, val); sr_dev_channel_name_set(ch, val);
sr_dev_channel_enable(sdi, tmp_u64 - 1, TRUE); sr_dev_channel_enable(ch, TRUE);
} }
} }
g_strfreev(keys); g_strfreev(keys);