2010-04-02 18:18:27 +00:00
|
|
|
/*
|
2013-04-23 20:24:30 +00:00
|
|
|
* This file is part of the libsigrok project.
|
2010-04-02 18:18:27 +00:00
|
|
|
*
|
2013-03-24 10:21:00 +00:00
|
|
|
* Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
|
2010-04-02 18:18:27 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <glib.h>
|
2012-10-23 22:41:21 +00:00
|
|
|
#include "config.h" /* Needed for HAVE_LIBUSB_1_0 and others. */
|
2012-07-04 22:55:07 +00:00
|
|
|
#include "libsigrok.h"
|
|
|
|
#include "libsigrok-internal.h"
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2014-04-25 16:40:59 +00:00
|
|
|
/** @cond PRIVATE */
|
2013-12-23 03:38:35 +00:00
|
|
|
#define LOG_PREFIX "device"
|
2014-04-25 16:40:59 +00:00
|
|
|
/** @endcond */
|
2012-11-11 11:44:16 +00:00
|
|
|
|
2012-10-21 22:30:12 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* Device handling in libsigrok.
|
|
|
|
*/
|
|
|
|
|
2012-10-21 14:13:36 +00:00
|
|
|
/**
|
|
|
|
* @defgroup grp_devices Devices
|
|
|
|
*
|
|
|
|
* Device handling in libsigrok.
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2013-11-22 19:40:52 +00:00
|
|
|
/** @private
|
2014-03-20 20:58:57 +00:00
|
|
|
* Allocate and initialize new struct sr_channel
|
|
|
|
* @param[in] index @copydoc sr_channel::index
|
|
|
|
* @param[in] type @copydoc sr_channel::type
|
|
|
|
* @param[in] enabled @copydoc sr_channel::enabled
|
|
|
|
* @param[in] name @copydoc sr_channel::name
|
2013-11-22 19:40:52 +00:00
|
|
|
*
|
2014-03-20 20:58:57 +00:00
|
|
|
* @return NULL (failure) or new struct sr_channel*.
|
2013-11-22 19:40:52 +00:00
|
|
|
*/
|
2014-03-24 20:36:04 +00:00
|
|
|
SR_PRIV struct sr_channel *sr_channel_new(int index, int type,
|
2012-07-24 22:20:06 +00:00
|
|
|
gboolean enabled, const char *name)
|
|
|
|
{
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2012-07-24 22:20:06 +00:00
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
if (!(ch = g_try_malloc0(sizeof(struct sr_channel)))) {
|
|
|
|
sr_err("Channel malloc failed.");
|
2012-07-24 22:20:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
ch->index = index;
|
|
|
|
ch->type = type;
|
|
|
|
ch->enabled = enabled;
|
2012-07-24 22:20:06 +00:00
|
|
|
if (name)
|
2014-03-24 20:34:20 +00:00
|
|
|
ch->name = g_strdup(name);
|
2012-07-24 22:20:06 +00:00
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
return ch;
|
2012-07-24 22:20:06 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 15:19:15 +00:00
|
|
|
/**
|
2014-03-24 20:34:20 +00:00
|
|
|
* Set the name of the specified channel in the specified device.
|
2011-12-28 15:19:15 +00:00
|
|
|
*
|
2014-03-24 20:34:20 +00:00
|
|
|
* If the channel already has a different name assigned to it, it will be
|
2011-12-28 15:19:15 +00:00
|
|
|
* removed, and the new name will be saved instead.
|
|
|
|
*
|
2014-03-24 20:34:20 +00:00
|
|
|
* @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
|
2012-07-22 18:05:36 +00:00
|
|
|
* of the string is made.
|
2011-12-28 16:16:33 +00:00
|
|
|
*
|
2012-07-22 18:05:36 +00:00
|
|
|
* @return SR_OK on success, or SR_ERR_ARG on invalid arguments.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2013-11-03 15:08:38 +00:00
|
|
|
* @since 0.2.0
|
2011-12-28 15:19:15 +00:00
|
|
|
*/
|
2014-03-24 15:05:42 +00:00
|
|
|
SR_API int sr_dev_channel_name_set(const struct sr_dev_inst *sdi,
|
2014-03-24 20:34:20 +00:00
|
|
|
int channelnum, const char *name)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-07-22 18:05:36 +00:00
|
|
|
GSList *l;
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2012-07-22 18:05:36 +00:00
|
|
|
int ret;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2012-07-22 18:05:36 +00:00
|
|
|
if (!sdi) {
|
|
|
|
sr_err("%s: sdi was NULL", __func__);
|
2011-12-28 16:16:33 +00:00
|
|
|
return SR_ERR_ARG;
|
2011-12-28 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 18:05:36 +00:00
|
|
|
ret = SR_ERR_ARG;
|
2014-03-24 20:34:20 +00:00
|
|
|
for (l = sdi->channels; l; l = l->next) {
|
|
|
|
ch = l->data;
|
|
|
|
if (ch->index == channelnum) {
|
|
|
|
g_free(ch->name);
|
|
|
|
ch->name = g_strdup(name);
|
2012-07-22 18:05:36 +00:00
|
|
|
ret = SR_OK;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-28 15:19:15 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 18:05:36 +00:00
|
|
|
return ret;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-07-22 10:23:59 +00:00
|
|
|
/**
|
2014-03-24 20:34:20 +00:00
|
|
|
* Enable or disable a channel on the specified device.
|
2012-07-22 10:23:59 +00:00
|
|
|
*
|
2014-03-24 20:34:20 +00:00
|
|
|
* @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.
|
2012-07-22 10:23:59 +00:00
|
|
|
*
|
2014-01-19 19:39:11 +00:00
|
|
|
* @return SR_OK on success or SR_ERR on failure. In case of invalid
|
2014-03-24 20:34:20 +00:00
|
|
|
* arguments, SR_ERR_ARG is returned and the channel enabled state
|
2014-01-19 19:39:11 +00:00
|
|
|
* remains unchanged.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
|
|
|
* @since 0.2.0
|
2012-07-22 10:23:59 +00:00
|
|
|
*/
|
2014-03-24 15:05:42 +00:00
|
|
|
SR_API int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum,
|
2012-07-22 10:23:59 +00:00
|
|
|
gboolean state)
|
|
|
|
{
|
|
|
|
GSList *l;
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2012-07-22 10:23:59 +00:00
|
|
|
int ret;
|
2014-01-19 19:39:11 +00:00
|
|
|
gboolean was_enabled;
|
2012-07-22 10:23:59 +00:00
|
|
|
|
|
|
|
if (!sdi)
|
|
|
|
return SR_ERR_ARG;
|
|
|
|
|
|
|
|
ret = SR_ERR_ARG;
|
2014-03-24 20:34:20 +00:00
|
|
|
for (l = sdi->channels; l; l = l->next) {
|
|
|
|
ch = l->data;
|
|
|
|
if (ch->index == channelnum) {
|
|
|
|
was_enabled = ch->enabled;
|
|
|
|
ch->enabled = state;
|
2012-07-22 10:23:59 +00:00
|
|
|
ret = SR_OK;
|
2014-01-19 19:39:11 +00:00
|
|
|
if (!state != !was_enabled && sdi->driver
|
2014-03-24 15:05:42 +00:00
|
|
|
&& sdi->driver->config_channel_set) {
|
|
|
|
ret = sdi->driver->config_channel_set(
|
2014-03-24 21:39:42 +00:00
|
|
|
sdi, ch, SR_CHANNEL_SET_ENABLED);
|
2014-01-19 19:39:11 +00:00
|
|
|
/* Roll back change if it wasn't applicable. */
|
|
|
|
if (ret == SR_ERR_ARG)
|
2014-03-24 20:34:20 +00:00
|
|
|
ch->enabled = was_enabled;
|
2014-01-19 19:39:11 +00:00
|
|
|
}
|
2012-07-22 10:23:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-28 15:19:15 +00:00
|
|
|
/**
|
2014-03-24 20:34:20 +00:00
|
|
|
* Add a trigger to the specified device (and the specified channel).
|
2012-03-28 19:55:48 +00:00
|
|
|
*
|
2014-03-24 20:34:20 +00:00
|
|
|
* If the specified channel of this device already has a trigger, it will
|
2012-03-28 19:55:48 +00:00
|
|
|
* be silently replaced.
|
2011-12-28 15:19:15 +00:00
|
|
|
*
|
2013-11-22 19:40:52 +00:00
|
|
|
* @param[in,out] sdi Pointer to the device instance; must not be NULL.
|
2014-03-24 20:34:20 +00:00
|
|
|
* @param[in] channelnum Number of channel, starting at 0.
|
2013-11-22 19:40:52 +00:00
|
|
|
* @param[in] trigger Trigger string, in the format used by sigrok-cli
|
2011-12-28 16:16:33 +00:00
|
|
|
*
|
2014-01-19 19:39:11 +00:00
|
|
|
* @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.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2013-11-03 15:08:38 +00:00
|
|
|
* @since 0.2.0
|
2011-12-28 15:19:15 +00:00
|
|
|
*/
|
2014-03-24 20:34:20 +00:00
|
|
|
SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int channelnum,
|
2012-07-21 17:11:49 +00:00
|
|
|
const char *trigger)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-07-21 17:11:49 +00:00
|
|
|
GSList *l;
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2014-01-19 19:39:11 +00:00
|
|
|
char *old_trigger;
|
2012-07-21 17:11:49 +00:00
|
|
|
int ret;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2012-07-21 17:11:49 +00:00
|
|
|
if (!sdi)
|
2011-12-28 16:16:33 +00:00
|
|
|
return SR_ERR_ARG;
|
2011-12-28 15:19:15 +00:00
|
|
|
|
2012-07-21 17:11:49 +00:00
|
|
|
ret = SR_ERR_ARG;
|
2014-03-24 20:34:20 +00:00
|
|
|
for (l = sdi->channels; l; l = l->next) {
|
|
|
|
ch = l->data;
|
|
|
|
if (ch->index == channelnum) {
|
|
|
|
old_trigger = ch->trigger;
|
2012-07-21 17:11:49 +00:00
|
|
|
ret = SR_OK;
|
2014-01-19 19:39:11 +00:00
|
|
|
if (g_strcmp0(trigger, old_trigger) == 0)
|
|
|
|
break;
|
|
|
|
/* Set new trigger if it has changed. */
|
2014-03-24 20:34:20 +00:00
|
|
|
ch->trigger = g_strdup(trigger);
|
2014-01-19 19:39:11 +00:00
|
|
|
|
2014-03-24 15:05:42 +00:00
|
|
|
if (sdi->driver && sdi->driver->config_channel_set) {
|
|
|
|
ret = sdi->driver->config_channel_set(
|
2014-03-24 21:39:42 +00:00
|
|
|
sdi, ch, SR_CHANNEL_SET_TRIGGER);
|
2014-01-19 19:39:11 +00:00
|
|
|
/* Roll back change if it wasn't applicable. */
|
|
|
|
if (ret == SR_ERR_ARG) {
|
2014-03-24 20:34:20 +00:00
|
|
|
g_free(ch->trigger);
|
|
|
|
ch->trigger = old_trigger;
|
2014-01-19 19:39:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_free(old_trigger);
|
2012-07-21 17:11:49 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-12-28 15:19:15 +00:00
|
|
|
}
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2012-07-21 17:11:49 +00:00
|
|
|
return ret;
|
2011-01-31 21:34:14 +00:00
|
|
|
}
|
|
|
|
|
2011-12-28 15:19:15 +00:00
|
|
|
/**
|
2012-10-21 14:52:56 +00:00
|
|
|
* Determine whether the specified device instance has the specified
|
|
|
|
* capability.
|
2011-12-28 15:19:15 +00:00
|
|
|
*
|
2012-10-21 14:52:56 +00:00
|
|
|
* @param sdi Pointer to the device instance to be checked. Must not be NULL.
|
2012-03-21 18:28:43 +00:00
|
|
|
* If the device's 'driver' field is NULL (virtual device), this
|
|
|
|
* function will always return FALSE (virtual devices don't have
|
|
|
|
* a hardware capabilities list).
|
2013-11-22 19:40:52 +00:00
|
|
|
* @param[in] key The option that should be checked for is supported by the
|
2013-01-25 15:20:40 +00:00
|
|
|
* specified device.
|
2011-12-28 15:19:15 +00:00
|
|
|
*
|
2013-11-22 19:40:52 +00:00
|
|
|
* @retval TRUE Device has the specified option
|
|
|
|
* @retval FALSE Device does not have the specified option, invalid input
|
|
|
|
* parameters or other error conditions.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2013-11-03 15:08:38 +00:00
|
|
|
* @since 0.2.0
|
2011-12-28 15:19:15 +00:00
|
|
|
*/
|
2013-01-25 15:20:40 +00:00
|
|
|
SR_API gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
|
2011-01-31 21:34:14 +00:00
|
|
|
{
|
2013-03-25 19:21:10 +00:00
|
|
|
GVariant *gvar;
|
2013-01-25 15:20:40 +00:00
|
|
|
const int *devopts;
|
2013-03-25 19:21:10 +00:00
|
|
|
gsize num_opts, i;
|
|
|
|
int ret;
|
2011-01-31 21:34:14 +00:00
|
|
|
|
2013-03-25 19:21:10 +00:00
|
|
|
if (!sdi || !sdi->driver || !sdi->driver->config_list)
|
2012-03-21 18:28:43 +00:00
|
|
|
return FALSE;
|
2011-12-28 15:19:15 +00:00
|
|
|
|
2013-04-20 00:00:49 +00:00
|
|
|
if (sdi->driver->config_list(SR_CONF_DEVICE_OPTIONS,
|
2014-01-16 01:53:41 +00:00
|
|
|
&gvar, sdi, NULL) != SR_OK)
|
2012-03-21 18:28:43 +00:00
|
|
|
return FALSE;
|
2011-12-28 15:19:15 +00:00
|
|
|
|
2013-03-25 19:21:10 +00:00
|
|
|
ret = FALSE;
|
|
|
|
devopts = g_variant_get_fixed_array(gvar, &num_opts, sizeof(int32_t));
|
|
|
|
for (i = 0; i < num_opts; i++) {
|
|
|
|
if (devopts[i] == key) {
|
|
|
|
ret = TRUE;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-28 15:19:15 +00:00
|
|
|
}
|
2013-03-25 19:21:10 +00:00
|
|
|
g_variant_unref(gvar);
|
2011-02-13 10:27:52 +00:00
|
|
|
|
2013-03-25 19:21:10 +00:00
|
|
|
return ret;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
2012-01-29 12:56:06 +00:00
|
|
|
|
2013-11-22 19:40:52 +00:00
|
|
|
/** @private
|
|
|
|
* Allocate and init new device instance struct.
|
2013-12-16 09:24:32 +00:00
|
|
|
* @param[in] index @copydoc sr_dev_inst::index
|
|
|
|
* @param[in] status @copydoc sr_dev_inst::status
|
|
|
|
* @param[in] vendor @copydoc sr_dev_inst::vendor
|
|
|
|
* @param[in] model @copydoc sr_dev_inst::model
|
|
|
|
* @param[in] version @copydoc sr_dev_inst::version
|
2013-11-22 19:40:52 +00:00
|
|
|
*
|
2013-12-16 09:24:32 +00:00
|
|
|
* @retval NULL Error
|
|
|
|
* @retval struct sr_dev_inst *. Dynamically allocated, free using
|
2013-11-22 19:40:52 +00:00
|
|
|
* sr_dev_inst_free().
|
|
|
|
*/
|
2012-07-24 22:20:06 +00:00
|
|
|
SR_PRIV struct sr_dev_inst *sr_dev_inst_new(int index, int status,
|
|
|
|
const char *vendor, const char *model, const char *version)
|
|
|
|
{
|
|
|
|
struct sr_dev_inst *sdi;
|
|
|
|
|
|
|
|
if (!(sdi = g_try_malloc(sizeof(struct sr_dev_inst)))) {
|
2013-01-29 11:55:00 +00:00
|
|
|
sr_err("Device instance malloc failed.");
|
2012-07-24 22:20:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-07-29 01:01:57 +00:00
|
|
|
sdi->driver = NULL;
|
2012-07-24 22:20:06 +00:00
|
|
|
sdi->index = index;
|
|
|
|
sdi->status = status;
|
|
|
|
sdi->inst_type = -1;
|
|
|
|
sdi->vendor = vendor ? g_strdup(vendor) : NULL;
|
|
|
|
sdi->model = model ? g_strdup(model) : NULL;
|
|
|
|
sdi->version = version ? g_strdup(version) : NULL;
|
2014-03-24 20:34:20 +00:00
|
|
|
sdi->channels = NULL;
|
2014-03-14 20:09:37 +00:00
|
|
|
sdi->channel_groups = NULL;
|
2012-10-01 01:03:24 +00:00
|
|
|
sdi->conn = NULL;
|
2012-07-24 22:20:06 +00:00
|
|
|
sdi->priv = NULL;
|
|
|
|
|
|
|
|
return sdi;
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:40:52 +00:00
|
|
|
/** @private
|
|
|
|
* Free device instance struct created by sr_dev_inst().
|
2013-12-16 09:24:32 +00:00
|
|
|
* @param sdi struct* to free.
|
2013-11-22 19:40:52 +00:00
|
|
|
*/
|
2012-07-24 22:20:06 +00:00
|
|
|
SR_PRIV void sr_dev_inst_free(struct sr_dev_inst *sdi)
|
|
|
|
{
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2012-08-04 10:03:05 +00:00
|
|
|
GSList *l;
|
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
for (l = sdi->channels; l; l = l->next) {
|
|
|
|
ch = l->data;
|
|
|
|
g_free(ch->name);
|
|
|
|
g_free(ch->trigger);
|
|
|
|
g_free(ch);
|
2012-08-04 10:03:05 +00:00
|
|
|
}
|
2014-03-24 20:34:20 +00:00
|
|
|
g_slist_free(sdi->channels);
|
2012-08-04 10:03:05 +00:00
|
|
|
|
2014-03-14 20:09:37 +00:00
|
|
|
if (sdi->channel_groups)
|
|
|
|
g_slist_free(sdi->channel_groups);
|
2013-11-19 00:08:18 +00:00
|
|
|
|
2012-07-24 22:20:06 +00:00
|
|
|
g_free(sdi->vendor);
|
|
|
|
g_free(sdi->model);
|
|
|
|
g_free(sdi->version);
|
|
|
|
g_free(sdi);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBUSB_1_0
|
|
|
|
|
2013-11-22 19:40:52 +00:00
|
|
|
/** @private
|
|
|
|
* Allocate and init struct for USB device instance.
|
2013-12-16 09:24:32 +00:00
|
|
|
* @param[in] bus @copydoc sr_usb_dev_inst::bus
|
|
|
|
* @param[in] address @copydoc sr_usb_dev_inst::address
|
|
|
|
* @param[in] hdl @copydoc sr_usb_dev_inst::devhdl
|
2013-11-22 19:40:52 +00:00
|
|
|
*
|
2013-12-16 09:24:32 +00:00
|
|
|
* @retval NULL Error
|
|
|
|
* @retval other struct sr_usb_dev_inst * for USB device instance.
|
2013-11-22 19:40:52 +00:00
|
|
|
*/
|
2012-07-24 22:20:06 +00:00
|
|
|
SR_PRIV struct sr_usb_dev_inst *sr_usb_dev_inst_new(uint8_t bus,
|
|
|
|
uint8_t address, struct libusb_device_handle *hdl)
|
|
|
|
{
|
|
|
|
struct sr_usb_dev_inst *udi;
|
|
|
|
|
|
|
|
if (!(udi = g_try_malloc(sizeof(struct sr_usb_dev_inst)))) {
|
2013-01-29 11:55:00 +00:00
|
|
|
sr_err("USB device instance malloc failed.");
|
2012-07-24 22:20:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
udi->bus = bus;
|
|
|
|
udi->address = address;
|
|
|
|
udi->devhdl = hdl;
|
|
|
|
|
|
|
|
return udi;
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:40:52 +00:00
|
|
|
/** @private
|
|
|
|
* Free struct * allocated by sr_usb_dev_inst().
|
2013-12-16 09:24:32 +00:00
|
|
|
* @param usb struct* to free. Must not be NULL.
|
2013-11-22 19:40:52 +00:00
|
|
|
*/
|
2012-07-24 22:20:06 +00:00
|
|
|
SR_PRIV void sr_usb_dev_inst_free(struct sr_usb_dev_inst *usb)
|
|
|
|
{
|
2013-05-05 22:40:17 +00:00
|
|
|
g_free(usb);
|
2012-07-24 22:20:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2013-11-13 18:56:13 +00:00
|
|
|
#ifdef HAVE_LIBSERIALPORT
|
|
|
|
|
2013-04-13 16:58:11 +00:00
|
|
|
/**
|
|
|
|
* @private
|
2012-11-11 19:38:47 +00:00
|
|
|
*
|
|
|
|
* Both parameters are copied to newly allocated strings, and freed
|
|
|
|
* automatically by sr_serial_dev_inst_free().
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
2013-11-22 19:40:52 +00:00
|
|
|
* @param[in] port OS-specific serial port specification. Examples:
|
2013-04-13 16:58:11 +00:00
|
|
|
* "/dev/ttyUSB0", "/dev/ttyACM1", "/dev/tty.Modem-0", "COM1".
|
2013-11-22 19:40:52 +00:00
|
|
|
* @param[in] serialcomm A serial communication parameters string, in the form
|
|
|
|
* of \<speed\>/\<data bits\>\<parity\>\<stopbits\>, for example
|
|
|
|
* "9600/8n1" or "600/7o2". This is an optional parameter;
|
|
|
|
* it may be filled in later.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
|
|
|
* @return A pointer to a newly initialized struct sr_serial_dev_inst,
|
|
|
|
* or NULL on error.
|
2012-11-11 19:38:47 +00:00
|
|
|
*/
|
2012-07-24 22:20:06 +00:00
|
|
|
SR_PRIV struct sr_serial_dev_inst *sr_serial_dev_inst_new(const char *port,
|
2012-11-11 19:38:47 +00:00
|
|
|
const char *serialcomm)
|
2012-07-24 22:20:06 +00:00
|
|
|
{
|
|
|
|
struct sr_serial_dev_inst *serial;
|
|
|
|
|
2012-11-11 19:38:47 +00:00
|
|
|
if (!port) {
|
2013-01-29 11:55:00 +00:00
|
|
|
sr_err("Serial port required.");
|
2012-11-11 19:38:47 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(serial = g_try_malloc0(sizeof(struct sr_serial_dev_inst)))) {
|
2013-01-29 11:55:00 +00:00
|
|
|
sr_err("Serial device instance malloc failed.");
|
2012-07-24 22:20:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
serial->port = g_strdup(port);
|
2012-11-11 19:38:47 +00:00
|
|
|
if (serialcomm)
|
|
|
|
serial->serialcomm = g_strdup(serialcomm);
|
2012-07-24 22:20:06 +00:00
|
|
|
|
|
|
|
return serial;
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:40:52 +00:00
|
|
|
/** @private
|
|
|
|
* Free struct sr_serial_dev_inst * allocated by sr_serial_dev_inst().
|
2013-12-16 09:24:32 +00:00
|
|
|
* @param serial struct sr_serial_dev_inst * to free. Must not be NULL.
|
2013-11-22 19:40:52 +00:00
|
|
|
*/
|
2012-07-24 22:20:06 +00:00
|
|
|
SR_PRIV void sr_serial_dev_inst_free(struct sr_serial_dev_inst *serial)
|
|
|
|
{
|
|
|
|
g_free(serial->port);
|
2012-11-11 19:38:47 +00:00
|
|
|
g_free(serial->serialcomm);
|
2012-12-20 03:39:38 +00:00
|
|
|
g_free(serial);
|
2012-07-24 22:20:06 +00:00
|
|
|
}
|
2013-11-13 18:56:13 +00:00
|
|
|
#endif
|
|
|
|
|
2013-12-27 15:18:28 +00:00
|
|
|
/** @private */
|
2013-11-29 00:48:42 +00:00
|
|
|
SR_PRIV struct sr_usbtmc_dev_inst *sr_usbtmc_dev_inst_new(const char *device)
|
|
|
|
{
|
|
|
|
struct sr_usbtmc_dev_inst *usbtmc;
|
|
|
|
|
|
|
|
if (!device) {
|
|
|
|
sr_err("Device name required.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(usbtmc = g_try_malloc0(sizeof(struct sr_usbtmc_dev_inst)))) {
|
|
|
|
sr_err("USBTMC device instance malloc failed.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbtmc->device = g_strdup(device);
|
|
|
|
usbtmc->fd = -1;
|
|
|
|
|
|
|
|
return usbtmc;
|
|
|
|
}
|
|
|
|
|
2013-12-27 15:18:28 +00:00
|
|
|
/** @private */
|
2013-11-29 00:48:42 +00:00
|
|
|
SR_PRIV void sr_usbtmc_dev_inst_free(struct sr_usbtmc_dev_inst *usbtmc)
|
|
|
|
{
|
|
|
|
g_free(usbtmc->device);
|
|
|
|
g_free(usbtmc);
|
|
|
|
}
|
|
|
|
|
2013-05-05 15:14:20 +00:00
|
|
|
/**
|
|
|
|
* Get the list of devices/instances of the specified driver.
|
|
|
|
*
|
|
|
|
* @param driver The driver to use. Must not be NULL.
|
|
|
|
*
|
|
|
|
* @return The list of devices/instances of this driver, or NULL upon errors
|
|
|
|
* or if the list is empty.
|
|
|
|
*
|
2013-11-03 15:08:38 +00:00
|
|
|
* @since 0.2.0
|
2013-05-05 15:14:20 +00:00
|
|
|
*/
|
2013-04-22 13:55:06 +00:00
|
|
|
SR_API GSList *sr_dev_list(const struct sr_dev_driver *driver)
|
2012-08-05 22:59:25 +00:00
|
|
|
{
|
|
|
|
if (driver && driver->dev_list)
|
|
|
|
return driver->dev_list();
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-05-05 15:14:20 +00:00
|
|
|
/**
|
2014-02-26 20:25:07 +00:00
|
|
|
* Clear the list of device instances a driver knows about.
|
2013-05-05 15:14:20 +00:00
|
|
|
*
|
2014-02-26 20:25:07 +00:00
|
|
|
* @param driver The driver to use. This must be a pointer to one of
|
|
|
|
* the entries returned by sr_driver_list(). Must not be NULL.
|
2013-05-05 15:14:20 +00:00
|
|
|
*
|
2014-02-26 20:25:07 +00:00
|
|
|
* @retval SR_OK Success
|
|
|
|
* @retval SR_ERR_ARG Invalid driver
|
2013-05-05 15:14:20 +00:00
|
|
|
*
|
|
|
|
* @since 0.2.0
|
|
|
|
*/
|
2013-04-22 13:55:06 +00:00
|
|
|
SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
|
2012-08-05 22:59:25 +00:00
|
|
|
{
|
2014-02-26 20:25:07 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!driver) {
|
|
|
|
sr_err("Invalid driver.");
|
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (driver->dev_clear)
|
|
|
|
ret = driver->dev_clear();
|
2012-08-05 22:59:25 +00:00
|
|
|
else
|
2014-02-26 20:25:07 +00:00
|
|
|
ret = std_dev_clear(driver, NULL);
|
|
|
|
|
|
|
|
return ret;
|
2012-08-05 22:59:25 +00:00
|
|
|
}
|
|
|
|
|
2013-05-05 15:14:20 +00:00
|
|
|
/**
|
|
|
|
* Open the specified device.
|
|
|
|
*
|
|
|
|
* @param sdi Device instance to use. Must not be NULL.
|
|
|
|
*
|
|
|
|
* @return SR_OK upon success, a negative error code upon errors.
|
|
|
|
*
|
|
|
|
* @since 0.2.0
|
|
|
|
*/
|
2013-04-22 22:21:57 +00:00
|
|
|
SR_API int sr_dev_open(struct sr_dev_inst *sdi)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!sdi || !sdi->driver || !sdi->driver->dev_open)
|
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
ret = sdi->driver->dev_open(sdi);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-05-05 15:14:20 +00:00
|
|
|
/**
|
|
|
|
* Close the specified device.
|
|
|
|
*
|
|
|
|
* @param sdi Device instance to use. Must not be NULL.
|
|
|
|
*
|
|
|
|
* @return SR_OK upon success, a negative error code upon errors.
|
|
|
|
*
|
|
|
|
* @since 0.2.0
|
|
|
|
*/
|
2013-04-22 22:21:57 +00:00
|
|
|
SR_API int sr_dev_close(struct sr_dev_inst *sdi)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!sdi || !sdi->driver || !sdi->driver->dev_close)
|
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
ret = sdi->driver->dev_close(sdi);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-21 14:13:36 +00:00
|
|
|
/** @} */
|