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
|
|
|
|
2013-12-23 03:38:35 +00:00
|
|
|
#define LOG_PREFIX "device"
|
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
|
|
|
|
* Allocate and initialize new struct sr_probe
|
2013-12-16 09:24:32 +00:00
|
|
|
* @param[in] index @copydoc sr_probe::index
|
|
|
|
* @param[in] type @copydoc sr_probe::type
|
|
|
|
* @param[in] enabled @copydoc sr_probe::enabled
|
|
|
|
* @param[in] name @copydoc sr_probe::name
|
2013-11-22 19:40:52 +00:00
|
|
|
*
|
|
|
|
* @return NULL (failure) or new struct sr_probe*.
|
|
|
|
*/
|
2012-07-24 22:20:06 +00:00
|
|
|
SR_PRIV struct sr_probe *sr_probe_new(int index, int type,
|
|
|
|
gboolean enabled, const char *name)
|
|
|
|
{
|
|
|
|
struct sr_probe *probe;
|
|
|
|
|
|
|
|
if (!(probe = g_try_malloc0(sizeof(struct sr_probe)))) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("Probe malloc failed.");
|
2012-07-24 22:20:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
probe->index = index;
|
|
|
|
probe->type = type;
|
|
|
|
probe->enabled = enabled;
|
|
|
|
if (name)
|
|
|
|
probe->name = g_strdup(name);
|
|
|
|
|
|
|
|
return probe;
|
|
|
|
}
|
|
|
|
|
2011-12-28 15:19:15 +00:00
|
|
|
/**
|
|
|
|
* Set the name of the specified probe in the specified device.
|
|
|
|
*
|
|
|
|
* If the probe already has a different name assigned to it, it will be
|
|
|
|
* removed, and the new name will be saved instead.
|
|
|
|
*
|
2012-07-22 18:05:36 +00:00
|
|
|
* @param sdi The device instance the probe is connected to.
|
2013-11-22 19:40:52 +00:00
|
|
|
* @param[in] probenum The number of the probe whose name to set.
|
2012-07-22 18:05:36 +00:00
|
|
|
* Note that the probe numbers start at 0.
|
2013-11-22 19:40:52 +00:00
|
|
|
* @param[in] name The new name that the specified probe 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
|
|
|
*/
|
2012-07-22 18:05:36 +00:00
|
|
|
SR_API int sr_dev_probe_name_set(const struct sr_dev_inst *sdi,
|
|
|
|
int probenum, const char *name)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-07-22 18:05:36 +00:00
|
|
|
GSList *l;
|
|
|
|
struct sr_probe *probe;
|
|
|
|
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;
|
|
|
|
for (l = sdi->probes; l; l = l->next) {
|
|
|
|
probe = l->data;
|
|
|
|
if (probe->index == probenum) {
|
|
|
|
g_free(probe->name);
|
|
|
|
probe->name = g_strdup(name);
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Enable or disable a probe on the specified device.
|
|
|
|
*
|
|
|
|
* @param sdi The device instance the probe is connected to.
|
|
|
|
* @param probenum The probe number, starting from 0.
|
|
|
|
* @param state TRUE to enable the probe, FALSE to disable.
|
|
|
|
*
|
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 probe enabled state
|
|
|
|
* remains unchanged.
|
2013-04-13 16:58:11 +00:00
|
|
|
*
|
|
|
|
* @since 0.2.0
|
2012-07-22 10:23:59 +00:00
|
|
|
*/
|
|
|
|
SR_API int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum,
|
|
|
|
gboolean state)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
struct sr_probe *probe;
|
|
|
|
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;
|
|
|
|
for (l = sdi->probes; l; l = l->next) {
|
|
|
|
probe = l->data;
|
|
|
|
if (probe->index == probenum) {
|
2014-01-19 19:39:11 +00:00
|
|
|
was_enabled = probe->enabled;
|
2012-07-22 10:23:59 +00:00
|
|
|
probe->enabled = state;
|
|
|
|
ret = SR_OK;
|
2014-01-19 19:39:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-07-22 10:23:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-28 15:19:15 +00:00
|
|
|
/**
|
2012-03-28 19:55:48 +00:00
|
|
|
* Add a trigger to the specified device (and the specified probe).
|
|
|
|
*
|
|
|
|
* If the specified probe of this device already has a trigger, it will
|
|
|
|
* 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.
|
|
|
|
* @param[in] probenum Number of probe, starting at 0.
|
|
|
|
* @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
|
|
|
*/
|
2012-07-21 17:11:49 +00:00
|
|
|
SR_API int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum,
|
|
|
|
const char *trigger)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-07-21 17:11:49 +00:00
|
|
|
GSList *l;
|
|
|
|
struct sr_probe *probe;
|
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;
|
|
|
|
for (l = sdi->probes; l; l = l->next) {
|
|
|
|
probe = l->data;
|
|
|
|
if (probe->index == probenum) {
|
2014-01-19 19:39:11 +00:00
|
|
|
old_trigger = probe->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. */
|
|
|
|
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);
|
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;
|
|
|
|
sdi->probes = NULL;
|
2013-04-21 20:05:03 +00:00
|
|
|
sdi->probe_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)
|
|
|
|
{
|
2012-08-04 10:03:05 +00:00
|
|
|
struct sr_probe *probe;
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
for (l = sdi->probes; l; l = l->next) {
|
|
|
|
probe = l->data;
|
|
|
|
g_free(probe->name);
|
2013-05-05 22:40:17 +00:00
|
|
|
g_free(probe->trigger);
|
2012-08-04 10:03:05 +00:00
|
|
|
g_free(probe);
|
|
|
|
}
|
2013-05-05 22:40:17 +00:00
|
|
|
g_slist_free(sdi->probes);
|
2012-08-04 10:03:05 +00:00
|
|
|
|
2013-11-19 00:08:18 +00:00
|
|
|
if (sdi->probe_groups)
|
|
|
|
g_slist_free(sdi->probe_groups);
|
|
|
|
|
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
|
|
|
/** @} */
|