+/**
+ * Send a SCPI command with a variadic argument list without mutex.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ * @param format Format string.
+ * @param args Argument list.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int 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_malloc0(len + 2);
+ vsprintf(buf, format, args);
+ if (buf[len - 1] != '\n')
+ buf[len] = '\n';
+
+ /* Send command. */
+ ret = scpi->send(scpi->priv, buf);
+
+ /* Free command buffer. */
+ g_free(buf);
+
+ return ret;
+}
+
+/**
+ * Send a SCPI command without mutex.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ * @param format Format string, to be followed by any necessary arguments.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int scpi_send(struct sr_scpi_dev_inst *scpi, const char *format,
+ va_list args)
+{
+ return scpi_send_variadic(scpi, format, args);
+}
+
+/**
+ * Send data to SCPI device without mutex.
+ *
+ * TODO: This is only implemented in TcpRaw, but never used.
+ * TODO: Use Mutex at all?
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param buf Buffer with data to send.
+ * @param len Number of bytes to send.
+ *
+ * @return Number of bytes read, or SR_ERR upon failure.
+ */
+static int scpi_write_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
+{
+ return scpi->write_data(scpi->priv, buf, maxlen);
+}
+
+/**
+ * Read part of a response from SCPI device without mutex.
+ *
+ * @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.
+ */
+static int scpi_read_data(struct sr_scpi_dev_inst *scpi, char *buf, int maxlen)
+{
+ return scpi->read_data(scpi->priv, buf, maxlen);
+}
+
+/**
+ * Do a non-blocking read of up to the allocated length, and
+ * check if a timeout has occured, without mutex.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param response Buffer to which the response is appended.
+ * @param abs_timeout_us Absolute timeout in microseconds
+ *
+ * @return read length on success, SR_ERR* on failure.
+ */
+static int scpi_read_response(struct sr_scpi_dev_inst *scpi,
+ GString *response, gint64 abs_timeout_us)
+{
+ int len, space;
+
+ space = response->allocated_len - response->len;
+ len = scpi->read_data(scpi->priv, &response->str[response->len], space);
+
+ if (len < 0) {
+ sr_err("Incompletely read SCPI response.");
+ return SR_ERR;
+ }
+
+ if (len > 0) {
+ g_string_set_size(response, response->len + len);
+ return len;
+ }
+
+ if (g_get_monotonic_time() > abs_timeout_us) {
+ sr_err("Timed out waiting for SCPI response.");
+ return SR_ERR_TIMEOUT;
+ }
+
+ return 0;
+}
+
+/**
+ * Send a SCPI command, receive the reply and store the reply in
+ * scpi_response, without mutex.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ * @param command The SCPI command to send to the device.
+ * @param scpi_response Pointer where to store the SCPI response.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int scpi_get_data(struct sr_scpi_dev_inst *scpi,
+ const char *command, GString **scpi_response)
+{
+ int ret;
+ GString *response;
+ int space;
+ gint64 timeout;
+ va_list empty_va_list;
+
+ /* Optionally send caller provided command. */
+ if (command) {
+ if (scpi_send(scpi, command, empty_va_list) != SR_OK)
+ return SR_ERR;
+ }
+
+ /* Initiate SCPI read operation. */
+ if (sr_scpi_read_begin(scpi) != SR_OK)
+ return SR_ERR;
+
+ /* Keep reading until completion or until timeout. */
+ timeout = g_get_monotonic_time() + scpi->read_timeout_us;
+
+ response = *scpi_response;
+
+ while (!sr_scpi_read_complete(scpi)) {
+ /* Resize the buffer when free space drops below a threshold. */
+ space = response->allocated_len - response->len;
+ if (space < 128) {
+ int oldlen = response->len;
+ g_string_set_size(response, oldlen + 1024);
+ g_string_set_size(response, oldlen);
+ }
+
+ /* Read another chunk of the response. */
+ ret = scpi_read_response(scpi, response, timeout);
+
+ if (ret < 0)
+ return ret;
+ if (ret > 0)
+ timeout = g_get_monotonic_time() + scpi->read_timeout_us;
+ }
+
+ return SR_OK;
+}
+