]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/scpi.c
teleinfo: Minor cleanups.
[libsigrok.git] / hardware / common / scpi.c
index 0091ba3418eeacce3460a2ebd0148e079038163a..4992b7362a8b76ebf29b83f10caac18fc3b55214 100644 (file)
@@ -119,14 +119,55 @@ 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 args;
+       int ret;
+
+       va_start(args, format);
+       ret = sr_scpi_send_variadic(scpi, format, args);
+       va_end(args);
+
+       return ret;
+}
+
+/**
+ * Send a SCPI command with a variadic argument list.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ * @param format Format string.
+ * @param args Argument list.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int sr_scpi_send_variadic(struct sr_scpi_dev_inst *scpi,
+                        const char *format, va_list args)
+{
+       va_list args_copy;
+       char *buf;
+       int len, ret;
+
+       /* Get length of buffer required. */
+       va_copy(args_copy, args);
+       len = vsnprintf(NULL, 0, format, args_copy);
+       va_end(args_copy);
+
+       /* Allocate buffer and write out command. */
+       buf = g_malloc(len + 1);
+       vsprintf(buf, format, args);
+
+       /* Send command. */
+       ret = scpi->send(scpi->priv, buf);
+
+       /* Free command buffer. */
+       g_free(buf);
+
+       return ret;
 }
 
 /**