From: Martin Ling Date: Wed, 4 Dec 2013 10:24:52 +0000 (+0000) Subject: Make sr_scpi_send() take printf-style arguments. X-Git-Tag: libsigrok-0.3.0~492 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=504f40a5749b34f2d0932868188e7d94da929be4;p=libsigrok.git Make sr_scpi_send() take printf-style arguments. --- diff --git a/hardware/common/scpi.c b/hardware/common/scpi.c index 0091ba34..10218ba9 100644 --- a/hardware/common/scpi.c +++ b/hardware/common/scpi.c @@ -22,6 +22,7 @@ #include #include +#include /* Message logging helpers with subsystem-specific prefix string. */ #define LOG_PREFIX "scpi: " @@ -119,14 +120,37 @@ SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi) * Send a SCPI command. * * @param scpi Previously initialized SCPI device structure. - * @param command The SCPI command to send to the device. + * @param format Format string, to be followed by any necessary arguments. * * @return SR_OK on success, SR_ERR on failure. */ SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi, - const char *command) + const char *format, ...) { - return scpi->send(scpi->priv, command); + va_list args1, args2; + char *buf; + int len, ret; + + /* Copy arguments since we need to make two variadic calls. */ + va_start(args1, format); + va_copy(args2, args1); + + /* Get length of buffer required. */ + len = vsnprintf(NULL, 0, format, args1); + va_end(args1); + + /* Allocate buffer and write out command. */ + buf = g_malloc(len + 1); + vsprintf(buf, format, args2); + va_end(args2); + + /* Send command. */ + ret = scpi->send(scpi->priv, buf); + + /* Free command buffer. */ + g_free(buf); + + return ret; } /** diff --git a/libsigrok-internal.h b/libsigrok-internal.h index d48e13f0..0e8cdecb 100644 --- a/libsigrok-internal.h +++ b/libsigrok-internal.h @@ -301,7 +301,7 @@ SR_PRIV int sr_scpi_source_add(struct sr_scpi_dev_inst *scpi, int events, int timeout, sr_receive_data_callback_t cb, void *cb_data); SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi); SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi, - const char *command); + const char *format, ...); SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi, char **scpi_response); SR_PRIV int sr_scpi_read(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen);