]> sigrok.org Git - libsigrok.git/blobdiff - hardware/common/scpi.c
Centralise duplicated logging helper defines.
[libsigrok.git] / hardware / common / scpi.c
index f8810a59250bfebe12666754bd76d987b7409448..78a06fd1918eed01475f66f14fdd8874ab81467c 100644 (file)
 #include <glib.h>
 #include <string.h>
 
-/* Message logging helpers with subsystem-specific prefix string. */
-#define LOG_PREFIX "scpi: "
-#define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
-#define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
-#define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
-#define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
-#define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
+#define LOG_PREFIX "scpi"
 
 #define SCPI_READ_RETRIES 100
 #define SCPI_READ_RETRY_TIMEOUT 10000
@@ -119,14 +113,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;
 }
 
 /**
@@ -146,6 +181,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.
  *