+/**
+ * Parse a string representation of a boolean-like value into a gboolean.
+ * Similar to sr_parse_boolstring but rejects strings which do not represent
+ * a boolean-like value.
+ *
+ * @param str String to convert.
+ * @param ret Pointer to a gboolean where the result of the conversion will be
+ * stored.
+ *
+ * @return SR_OK on success, SR_ERR on failure.
+ */
+static int parse_strict_bool(const char *str, gboolean *ret)
+{
+ if (!str)
+ return SR_ERR_ARG;
+
+ if (!g_strcmp0(str, "1") ||
+ !g_ascii_strncasecmp(str, "y", 1) ||
+ !g_ascii_strncasecmp(str, "t", 1) ||
+ !g_ascii_strncasecmp(str, "yes", 3) ||
+ !g_ascii_strncasecmp(str, "true", 4) ||
+ !g_ascii_strncasecmp(str, "on", 2)) {
+ *ret = TRUE;
+ return SR_OK;
+ } else if (!g_strcmp0(str, "0") ||
+ !g_ascii_strncasecmp(str, "n", 1) ||
+ !g_ascii_strncasecmp(str, "f", 1) ||
+ !g_ascii_strncasecmp(str, "no", 2) ||
+ !g_ascii_strncasecmp(str, "false", 5) ||
+ !g_ascii_strncasecmp(str, "off", 3)) {
+ *ret = FALSE;
+ return SR_OK;
+ }
+
+ return SR_ERR;
+}
+
+SR_PRIV extern const struct sr_scpi_dev_inst scpi_serial_dev;
+SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_raw_dev;
+SR_PRIV extern const struct sr_scpi_dev_inst scpi_tcp_rigol_dev;
+SR_PRIV extern const struct sr_scpi_dev_inst scpi_usbtmc_dev;
+SR_PRIV extern const struct sr_scpi_dev_inst scpi_vxi_dev;
+
+static const struct sr_scpi_dev_inst *scpi_devs[] = {
+ &scpi_tcp_raw_dev,
+ &scpi_tcp_rigol_dev,
+ &scpi_usbtmc_dev,
+#if HAVE_RPC
+ &scpi_vxi_dev,
+#endif
+#ifdef HAVE_LIBSERIALPORT
+ &scpi_serial_dev, /* must be last as it matches any resource */
+#endif
+};
+
+SR_PRIV struct sr_scpi_dev_inst *scpi_dev_inst_new(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, 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);
+}
+