+ if (!devices && resource)
+ devices = sr_scpi_scan_resource(drvc, resource, serialcomm,
+ probe_device);
+
+ /* Tack a copy of the newly found devices onto the driver list. */
+ if (devices)
+ drvc->instances = g_slist_concat(drvc->instances,
+ g_slist_copy(devices));
+
+ return devices;
+}
+
+SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(struct drv_context *drvc,
+ const char *resource, const char *serialcomm)
+{
+ struct sr_scpi_dev_inst *scpi = NULL;
+ const struct sr_scpi_dev_inst *scpi_dev;
+ gchar **params;
+ unsigned i;
+
+ for (i = 0; i < ARRAY_SIZE(scpi_devs); i++) {
+ scpi_dev = scpi_devs[i];
+ if (!strncmp(resource, scpi_dev->prefix, strlen(scpi_dev->prefix))) {
+ sr_dbg("Opening %s device %s.", scpi_dev->name, resource);
+ scpi = g_malloc(sizeof(*scpi));
+ *scpi = *scpi_dev;
+ scpi->priv = g_malloc0(scpi->priv_size);
+ params = g_strsplit(resource, "/", 0);
+ if (scpi->dev_inst_new(scpi->priv, drvc, resource,
+ params, serialcomm) != SR_OK) {
+ sr_scpi_free(scpi);
+ scpi = NULL;
+ }
+ g_strfreev(params);
+ break;
+ }
+ }
+
+ return scpi;
+}
+
+/**
+ * Open SCPI device.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int sr_scpi_open(struct sr_scpi_dev_inst *scpi)
+{
+ return scpi->open(scpi->priv);
+}
+
+/**
+ * Add an event source for an SCPI device.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ * @param events Events to check for.
+ * @param timeout Max time to wait before the callback is called, ignored if 0.
+ * @param cb Callback function to add. Must not be NULL.
+ * @param cb_data Data for the callback function. Can be NULL.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ * SR_ERR_MALLOC upon memory allocation errors.
+ */
+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)
+{
+ return scpi->source_add(scpi->priv, events, timeout, cb, cb_data);
+}
+
+/**
+ * Remove event source for an SCPI device.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ *
+ * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
+ * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
+ * internal errors.
+ */
+SR_PRIV int sr_scpi_source_remove(struct sr_scpi_dev_inst *scpi)
+{
+ return scpi->source_remove(scpi->priv);
+}
+
+/**
+ * Send a SCPI command.
+ *
+ * @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.
+ */
+SR_PRIV int sr_scpi_send(struct sr_scpi_dev_inst *scpi,
+ const char *format, ...)
+{
+ 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;
+}
+
+/**
+ * Begin receiving an SCPI reply.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int sr_scpi_read_begin(struct sr_scpi_dev_inst *scpi)
+{
+ return scpi->read_begin(scpi->priv);
+}
+
+/**
+ * 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_data(struct sr_scpi_dev_inst *scpi,
+ char *buf, int maxlen)
+{
+ return scpi->read_data(scpi->priv, buf, maxlen);
+}
+
+/**
+ * Check whether a complete SCPI response has been received.
+ *
+ * @param scpi Previously initialised SCPI device structure.
+ *
+ * @return 1 if complete, 0 otherwise.
+ */
+SR_PRIV int sr_scpi_read_complete(struct sr_scpi_dev_inst *scpi)
+{
+ return scpi->read_complete(scpi->priv);
+}
+
+/**
+ * Close SCPI device.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV int sr_scpi_close(struct sr_scpi_dev_inst *scpi)
+{
+ return scpi->close(scpi->priv);
+}
+
+/**
+ * Free SCPI device.
+ *
+ * @param scpi Previously initialized SCPI device structure.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+SR_PRIV void sr_scpi_free(struct sr_scpi_dev_inst *scpi)
+{
+ scpi->free(scpi->priv);
+ g_free(scpi->priv);
+ g_free(scpi);