2013-12-03 22:25:33 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the libsigrok project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li>
|
|
|
|
*
|
|
|
|
* 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 "libsigrok.h"
|
|
|
|
#include "libsigrok-internal.h"
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2013-12-23 03:38:35 +00:00
|
|
|
#define LOG_PREFIX "scpi_usbtmc"
|
2013-12-03 22:25:33 +00:00
|
|
|
|
2013-12-29 01:36:49 +00:00
|
|
|
#define MAX_READ_LENGTH 2048
|
|
|
|
|
|
|
|
struct usbtmc_scpi {
|
|
|
|
struct sr_usbtmc_dev_inst *usbtmc;
|
|
|
|
char response_buffer[MAX_READ_LENGTH];
|
|
|
|
int response_length;
|
|
|
|
int response_bytes_read;
|
|
|
|
};
|
|
|
|
|
2014-01-17 09:47:42 +00:00
|
|
|
static int scpi_usbtmc_dev_inst_new(void *priv, struct drv_context *drvc,
|
|
|
|
const char *resource, char **params, const char *serialcomm)
|
2014-01-11 23:05:02 +00:00
|
|
|
{
|
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
|
2014-01-17 09:47:42 +00:00
|
|
|
(void)drvc;
|
2014-01-11 23:05:02 +00:00
|
|
|
(void)params;
|
|
|
|
(void)serialcomm;
|
|
|
|
|
|
|
|
if (!(uscpi->usbtmc = sr_usbtmc_dev_inst_new(resource)))
|
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_open(void *priv)
|
2013-12-03 22:25:33 +00:00
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
|
2013-12-03 22:25:33 +00:00
|
|
|
|
|
|
|
if ((usbtmc->fd = open(usbtmc->device, O_RDWR)) < 0)
|
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_source_add(void *priv, int events, int timeout,
|
2013-12-03 22:25:33 +00:00
|
|
|
sr_receive_data_callback_t cb, void *cb_data)
|
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
|
2013-12-03 22:25:33 +00:00
|
|
|
|
|
|
|
return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data);
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_source_remove(void *priv)
|
2013-12-03 22:25:33 +00:00
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
|
2013-12-03 22:25:33 +00:00
|
|
|
|
|
|
|
return sr_source_remove(usbtmc->fd);
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_send(void *priv, const char *command)
|
2013-12-03 22:25:33 +00:00
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
|
2013-12-03 22:25:33 +00:00
|
|
|
int len, out;
|
|
|
|
|
|
|
|
len = strlen(command);
|
|
|
|
out = write(usbtmc->fd, command, len);
|
|
|
|
|
|
|
|
if (out < 0) {
|
|
|
|
sr_err("Write error: %s", strerror(errno));
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out < len) {
|
|
|
|
sr_dbg("Only sent %d/%d bytes of SCPI command: '%s'.", out,
|
|
|
|
len, command);
|
|
|
|
}
|
|
|
|
|
|
|
|
sr_spew("Successfully sent SCPI command: '%s'.", command);
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_read_begin(void *priv)
|
2013-12-03 22:25:33 +00:00
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc;
|
2013-12-03 22:25:33 +00:00
|
|
|
int len;
|
|
|
|
|
2013-12-29 01:36:49 +00:00
|
|
|
len = read(usbtmc->fd, uscpi->response_buffer, MAX_READ_LENGTH);
|
2013-12-03 22:25:33 +00:00
|
|
|
|
|
|
|
if (len < 0) {
|
|
|
|
sr_err("Read error: %s", strerror(errno));
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
2013-12-29 01:36:49 +00:00
|
|
|
uscpi->response_length = len;
|
|
|
|
uscpi->response_bytes_read = 0;
|
2013-12-03 22:25:33 +00:00
|
|
|
|
2014-01-22 04:48:33 +00:00
|
|
|
sr_spew("Read %d bytes from device into buffer", len);
|
|
|
|
|
2013-12-03 22:25:33 +00:00
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen)
|
2013-12-03 22:56:32 +00:00
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
int read_length;
|
2013-12-03 22:56:32 +00:00
|
|
|
|
2014-01-22 04:48:33 +00:00
|
|
|
sr_spew("%d bytes requested", maxlen);
|
2014-01-03 16:50:16 +00:00
|
|
|
|
2014-01-22 04:48:33 +00:00
|
|
|
if (uscpi->response_bytes_read == uscpi->response_length) {
|
|
|
|
sr_spew("Buffer is empty.");
|
|
|
|
if (uscpi->response_length == MAX_READ_LENGTH) {
|
|
|
|
sr_spew("Previous read was of maximum length, reading again.");
|
|
|
|
if (scpi_usbtmc_read_begin(uscpi) != SR_OK)
|
|
|
|
return SR_ERR;
|
|
|
|
} else {
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
}
|
2013-12-03 22:56:32 +00:00
|
|
|
|
2013-12-29 01:36:49 +00:00
|
|
|
read_length = uscpi->response_length - uscpi->response_bytes_read;
|
|
|
|
|
|
|
|
if (read_length > maxlen)
|
|
|
|
read_length = maxlen;
|
|
|
|
|
|
|
|
memcpy(buf, uscpi->response_buffer + uscpi->response_bytes_read, read_length);
|
|
|
|
|
|
|
|
uscpi->response_bytes_read += read_length;
|
|
|
|
|
2014-01-22 04:48:33 +00:00
|
|
|
sr_spew("Returned %d bytes from buffer, %d/%d bytes of buffer now read",
|
|
|
|
read_length, uscpi->response_bytes_read, uscpi->response_length);
|
|
|
|
|
2013-12-29 01:36:49 +00:00
|
|
|
return read_length;
|
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_read_complete(void *priv)
|
2013-12-29 01:36:49 +00:00
|
|
|
{
|
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
|
2014-01-03 16:50:16 +00:00
|
|
|
if (uscpi->response_length == MAX_READ_LENGTH
|
|
|
|
&& uscpi->response_bytes_read == uscpi->response_length)
|
|
|
|
scpi_usbtmc_read_begin(uscpi);
|
|
|
|
|
2013-12-29 01:36:49 +00:00
|
|
|
return (uscpi->response_bytes_read >= uscpi->response_length);
|
2013-12-03 22:56:32 +00:00
|
|
|
}
|
|
|
|
|
2014-01-14 22:16:55 +00:00
|
|
|
static int scpi_usbtmc_close(void *priv)
|
2013-12-03 22:25:33 +00:00
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
2013-12-03 22:25:33 +00:00
|
|
|
|
2014-01-11 23:05:02 +00:00
|
|
|
if (close(uscpi->usbtmc->fd) < 0)
|
2013-12-03 22:25:33 +00:00
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-04 23:16:11 +00:00
|
|
|
static void scpi_usbtmc_free(void *priv)
|
|
|
|
{
|
2013-12-29 01:36:49 +00:00
|
|
|
struct usbtmc_scpi *uscpi = priv;
|
|
|
|
|
2014-01-11 23:05:02 +00:00
|
|
|
sr_usbtmc_dev_inst_free(uscpi->usbtmc);
|
2013-12-04 23:16:11 +00:00
|
|
|
}
|
|
|
|
|
2014-01-11 23:05:02 +00:00
|
|
|
SR_PRIV const struct sr_scpi_dev_inst scpi_usbtmc_dev = {
|
|
|
|
.name = "USBTMC",
|
|
|
|
.prefix = "/dev/usbtmc",
|
|
|
|
.priv_size = sizeof(struct usbtmc_scpi),
|
|
|
|
.dev_inst_new = scpi_usbtmc_dev_inst_new,
|
|
|
|
.open = scpi_usbtmc_open,
|
|
|
|
.source_add = scpi_usbtmc_source_add,
|
|
|
|
.source_remove = scpi_usbtmc_source_remove,
|
|
|
|
.send = scpi_usbtmc_send,
|
|
|
|
.read_begin = scpi_usbtmc_read_begin,
|
|
|
|
.read_data = scpi_usbtmc_read_data,
|
|
|
|
.read_complete = scpi_usbtmc_read_complete,
|
|
|
|
.close = scpi_usbtmc_close,
|
|
|
|
.free = scpi_usbtmc_free,
|
|
|
|
};
|