X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fcommon%2Fscpi_usbtmc.c;h=3c98c06bc8c87bfa2eeb38cf962f821c271cd675;hb=c1bcb8cc633cc69ce4a872f59ec2b686bc00bd04;hp=5aa0dbbd335d323dd20615984a59f9294069f7c5;hpb=05c644ea081f5973fcbb2429318b808b931edfe3;p=libsigrok.git diff --git a/hardware/common/scpi_usbtmc.c b/hardware/common/scpi_usbtmc.c index 5aa0dbbd..3c98c06b 100644 --- a/hardware/common/scpi_usbtmc.c +++ b/hardware/common/scpi_usbtmc.c @@ -37,7 +37,21 @@ struct usbtmc_scpi { int response_bytes_read; }; -SR_PRIV int scpi_usbtmc_open(void *priv) +static int scpi_usbtmc_dev_inst_new(void *priv, const char *resource, + char **params, const char *serialcomm) +{ + struct usbtmc_scpi *uscpi = priv; + + (void)params; + (void)serialcomm; + + if (!(uscpi->usbtmc = sr_usbtmc_dev_inst_new(resource))) + return SR_ERR; + + return SR_OK; +} + +static int scpi_usbtmc_open(void *priv) { struct usbtmc_scpi *uscpi = priv; struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; @@ -48,7 +62,7 @@ SR_PRIV int scpi_usbtmc_open(void *priv) return SR_OK; } -SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout, +static int scpi_usbtmc_source_add(void *priv, int events, int timeout, sr_receive_data_callback_t cb, void *cb_data) { struct usbtmc_scpi *uscpi = priv; @@ -57,7 +71,7 @@ SR_PRIV int scpi_usbtmc_source_add(void *priv, int events, int timeout, return sr_source_add(usbtmc->fd, events, timeout, cb, cb_data); } -SR_PRIV int scpi_usbtmc_source_remove(void *priv) +static int scpi_usbtmc_source_remove(void *priv) { struct usbtmc_scpi *uscpi = priv; struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; @@ -65,7 +79,7 @@ SR_PRIV int scpi_usbtmc_source_remove(void *priv) return sr_source_remove(usbtmc->fd); } -SR_PRIV int scpi_usbtmc_send(void *priv, const char *command) +static int scpi_usbtmc_send(void *priv, const char *command) { struct usbtmc_scpi *uscpi = priv; struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; @@ -89,7 +103,7 @@ SR_PRIV int scpi_usbtmc_send(void *priv, const char *command) return SR_OK; } -SR_PRIV int scpi_usbtmc_read_begin(void *priv) +static int scpi_usbtmc_read_begin(void *priv) { struct usbtmc_scpi *uscpi = priv; struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; @@ -108,11 +122,16 @@ SR_PRIV int scpi_usbtmc_read_begin(void *priv) return SR_OK; } -SR_PRIV int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen) +static int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen) { struct usbtmc_scpi *uscpi = priv; int read_length; + if (uscpi->response_length == MAX_READ_LENGTH + && uscpi->response_bytes_read == uscpi->response_length) + if (scpi_usbtmc_read_begin(uscpi) != SR_OK) + return SR_ERR; + if (uscpi->response_bytes_read >= uscpi->response_length) return SR_ERR; @@ -128,19 +147,22 @@ SR_PRIV int scpi_usbtmc_read_data(void *priv, char *buf, int maxlen) return read_length; } -SR_PRIV int scpi_usbtmc_read_complete(void *priv) +static int scpi_usbtmc_read_complete(void *priv) { struct usbtmc_scpi *uscpi = priv; + if (uscpi->response_length == MAX_READ_LENGTH + && uscpi->response_bytes_read == uscpi->response_length) + scpi_usbtmc_read_begin(uscpi); + return (uscpi->response_bytes_read >= uscpi->response_length); } -SR_PRIV int scpi_usbtmc_close(void *priv) +static int scpi_usbtmc_close(void *priv) { struct usbtmc_scpi *uscpi = priv; - struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; - if (close(usbtmc->fd) < 0) + if (close(uscpi->usbtmc->fd) < 0) return SR_ERR; return SR_OK; @@ -149,37 +171,22 @@ SR_PRIV int scpi_usbtmc_close(void *priv) static void scpi_usbtmc_free(void *priv) { struct usbtmc_scpi *uscpi = priv; - struct sr_usbtmc_dev_inst *usbtmc = uscpi->usbtmc; - g_free(uscpi); - sr_usbtmc_dev_inst_free(usbtmc); + sr_usbtmc_dev_inst_free(uscpi->usbtmc); } -SR_PRIV struct sr_scpi_dev_inst *scpi_usbtmc_dev_inst_new(const char *device) -{ - struct sr_scpi_dev_inst *scpi; - struct usbtmc_scpi *uscpi; - struct sr_usbtmc_dev_inst *usbtmc; - - if (!(usbtmc = sr_usbtmc_dev_inst_new(device))) - return NULL; - - uscpi = g_malloc(sizeof(struct usbtmc_scpi)); - - uscpi->usbtmc = usbtmc; - - scpi = g_malloc(sizeof(struct sr_scpi_dev_inst)); - - scpi->open = scpi_usbtmc_open; - scpi->source_add = scpi_usbtmc_source_add; - scpi->source_remove = scpi_usbtmc_source_remove; - scpi->send = scpi_usbtmc_send; - scpi->read_begin = scpi_usbtmc_read_begin; - scpi->read_data = scpi_usbtmc_read_data; - scpi->read_complete = scpi_usbtmc_read_complete; - scpi->close = scpi_usbtmc_close; - scpi->free = scpi_usbtmc_free; - scpi->priv = uscpi; - - return scpi; -} +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, +};