]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/scpi.c
Make sr_scpi_send() take printf-style arguments.
[libsigrok.git] / hardware / common / scpi.c
index f8810a59250bfebe12666754bd76d987b7409448..10218ba9820379ebcead81f896e59a019b508270 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <glib.h>
 #include <string.h>
+#include <stdarg.h>
 
 /* 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;
 }
 
 /**
@@ -146,6 +170,21 @@ SR_PRIV int sr_scpi_receive(struct sr_scpi_dev_inst *scpi,
        return scpi->receive(scpi->priv, scpi_response);
 }
 
+/**
+ * Read part of a response from SCPI device.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param buf Buffer to store result.
+ * @param maxlen Maximum number of bytes to read.
+ *
+ * @return Number of bytes read, or SR_ERR upon failure.
+ */
+SR_PRIV int sr_scpi_read(struct sr_scpi_dev_inst *scpi,
+                       char *buf, int maxlen)
+{
+       return scpi->read(scpi->priv, buf, maxlen);
+}
+
 /**
  * Close SCPI device.
  *