From: Uwe Hermann Date: Fri, 7 Jul 2017 19:11:56 +0000 (+0200) Subject: sr_dev_open(): Factor out SR_ST_ACTIVE check. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=6402c379161ec138e451901c411817a55846a75b;p=libsigrok.git sr_dev_open(): Factor out SR_ST_ACTIVE check. This ensures consistent checks and log messages across all drivers and reduces the per-driver boilerplate. --- diff --git a/src/device.c b/src/device.c index 6eff16d1..e5cbf4ef 100644 --- a/src/device.c +++ b/src/device.c @@ -560,6 +560,14 @@ SR_API int sr_dev_open(struct sr_dev_inst *sdi) if (!sdi || !sdi->driver || !sdi->driver->dev_open) return SR_ERR; + if (sdi->status == SR_ST_ACTIVE) { + sr_err("%s: Device instance already active, can't re-open.", + sdi->driver->name); + return SR_ERR; + } + + sr_dbg("%s: Opening device.", sdi->driver->name) + ret = sdi->driver->dev_open(sdi); return ret; diff --git a/src/hardware/dreamsourcelab-dslogic/protocol.c b/src/hardware/dreamsourcelab-dslogic/protocol.c index 0818e23f..43280cc5 100644 --- a/src/hardware/dreamsourcelab-dslogic/protocol.c +++ b/src/hardware/dreamsourcelab-dslogic/protocol.c @@ -568,10 +568,6 @@ SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di) devc = sdi->priv; usb = sdi->conn; - if (sdi->status == SR_ST_ACTIVE) - /* Device is already in use. */ - return SR_ERR; - device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); if (device_count < 0) { sr_err("Failed to get device list: %s.", diff --git a/src/hardware/fx2lafw/protocol.c b/src/hardware/fx2lafw/protocol.c index a31231fd..422c84ea 100644 --- a/src/hardware/fx2lafw/protocol.c +++ b/src/hardware/fx2lafw/protocol.c @@ -155,10 +155,6 @@ SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di) devc = sdi->priv; usb = sdi->conn; - if (sdi->status == SR_ST_ACTIVE) - /* Device is already in use. */ - return SR_ERR; - device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); if (device_count < 0) { sr_err("Failed to get device list: %s.", diff --git a/src/hardware/hameg-hmo/api.c b/src/hardware/hameg-hmo/api.c index d4e18481..20b5779c 100644 --- a/src/hardware/hameg-hmo/api.c +++ b/src/hardware/hameg-hmo/api.c @@ -131,7 +131,7 @@ static int dev_clear(const struct sr_dev_driver *di) static int dev_open(struct sr_dev_inst *sdi) { - if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK) + if (sr_scpi_open(sdi->conn) != SR_OK) return SR_ERR; if (hmo_scope_state_get(sdi) != SR_OK) diff --git a/src/hardware/hantek-6xxx/protocol.c b/src/hardware/hantek-6xxx/protocol.c index a43f72bb..a08e126c 100644 --- a/src/hardware/hantek-6xxx/protocol.c +++ b/src/hardware/hantek-6xxx/protocol.c @@ -33,10 +33,6 @@ SR_PRIV int hantek_6xxx_open(struct sr_dev_inst *sdi) devc = sdi->priv; usb = sdi->conn; - if (sdi->status == SR_ST_ACTIVE) - /* Already in use. */ - return SR_ERR; - libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { libusb_get_device_descriptor(devlist[i], &des); diff --git a/src/hardware/hantek-dso/protocol.c b/src/hardware/hantek-dso/protocol.c index bf91de45..b1a56ebd 100644 --- a/src/hardware/hantek-dso/protocol.c +++ b/src/hardware/hantek-dso/protocol.c @@ -119,10 +119,6 @@ SR_PRIV int dso_open(struct sr_dev_inst *sdi) devc = sdi->priv; usb = sdi->conn; - if (sdi->status == SR_ST_ACTIVE) - /* already in use */ - return SR_ERR; - libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); for (i = 0; devlist[i]; i++) { libusb_get_device_descriptor(devlist[i], &des); diff --git a/src/hardware/hung-chang-dso-2100/api.c b/src/hardware/hung-chang-dso-2100/api.c index df2cddbb..9f752617 100644 --- a/src/hardware/hung-chang-dso-2100/api.c +++ b/src/hardware/hung-chang-dso-2100/api.c @@ -246,9 +246,6 @@ static int dev_open(struct sr_dev_inst *sdi) struct dev_context *devc = sdi->priv; int i; - if (sdi->status != SR_ST_INACTIVE) - goto fail1; - if (ieee1284_open(sdi->conn, 0, &i) != E1284_OK) goto fail1; diff --git a/src/hardware/lecroy-logicstudio/api.c b/src/hardware/lecroy-logicstudio/api.c index dfa13284..f177cabd 100644 --- a/src/hardware/lecroy-logicstudio/api.c +++ b/src/hardware/lecroy-logicstudio/api.c @@ -191,9 +191,6 @@ static int open_device(struct sr_dev_inst *sdi) size_t i; int r; - if (sdi->status == SR_ST_ACTIVE) - return SR_ERR; - drvc = sdi->driver->context; usb = sdi->conn; diff --git a/src/hardware/lecroy-xstream/api.c b/src/hardware/lecroy-xstream/api.c index 48086647..075fff76 100644 --- a/src/hardware/lecroy-xstream/api.c +++ b/src/hardware/lecroy-xstream/api.c @@ -135,7 +135,7 @@ static int dev_clear(const struct sr_dev_driver *di) static int dev_open(struct sr_dev_inst *sdi) { - if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK) + if (sr_scpi_open(sdi->conn) != SR_OK) return SR_ERR; if (lecroy_xstream_state_get(sdi) != SR_OK) diff --git a/src/hardware/pipistrello-ols/api.c b/src/hardware/pipistrello-ols/api.c index 9e274f3b..5995cdf8 100644 --- a/src/hardware/pipistrello-ols/api.c +++ b/src/hardware/pipistrello-ols/api.c @@ -411,12 +411,12 @@ static int dev_open(struct sr_dev_inst *sdi) devc = sdi->priv; - if (p_ols_open(devc) != SR_OK) { + if (p_ols_open(devc) != SR_OK) return SR_ERR; - } else { - sdi->status = SR_ST_ACTIVE; - return SR_OK; - } + + sdi->status = SR_ST_ACTIVE; + + return SR_OK; } static int dev_close(struct sr_dev_inst *sdi) diff --git a/src/hardware/rohde-schwarz-sme-0x/api.c b/src/hardware/rohde-schwarz-sme-0x/api.c index ff04242c..7d39cbdf 100644 --- a/src/hardware/rohde-schwarz-sme-0x/api.c +++ b/src/hardware/rohde-schwarz-sme-0x/api.c @@ -162,7 +162,7 @@ static int dev_clear(const struct sr_dev_driver *di) static int dev_open(struct sr_dev_inst *sdi) { - if ((sdi->status != SR_ST_ACTIVE) && (sr_scpi_open(sdi->conn) != SR_OK)) + if (sr_scpi_open(sdi->conn) != SR_OK) return SR_ERR; sdi->status = SR_ST_ACTIVE; diff --git a/src/hardware/saleae-logic16/api.c b/src/hardware/saleae-logic16/api.c index a8d32cf8..986b0257 100644 --- a/src/hardware/saleae-logic16/api.c +++ b/src/hardware/saleae-logic16/api.c @@ -242,10 +242,6 @@ static int logic16_dev_open(struct sr_dev_inst *sdi) drvc = di->context; usb = sdi->conn; - if (sdi->status == SR_ST_ACTIVE) - /* Device is already in use. */ - return SR_ERR; - device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); if (device_count < 0) { sr_err("Failed to get device list: %s.", diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c index 2746dc40..50eabc9f 100644 --- a/src/hardware/scpi-pps/api.c +++ b/src/hardware/scpi-pps/api.c @@ -251,9 +251,6 @@ static int dev_open(struct sr_dev_inst *sdi) struct sr_scpi_dev_inst *scpi; GVariant *beeper; - if (sdi->status != SR_ST_INACTIVE) - return SR_ERR; - scpi = sdi->conn; if (sr_scpi_open(scpi) < 0) return SR_ERR; diff --git a/src/hardware/sysclk-lwla/api.c b/src/hardware/sysclk-lwla/api.c index da2019fb..59b36819 100644 --- a/src/hardware/sysclk-lwla/api.c +++ b/src/hardware/sysclk-lwla/api.c @@ -273,11 +273,6 @@ static int dev_open(struct sr_dev_inst *sdi) devc = sdi->priv; usb = sdi->conn; - if (sdi->status != SR_ST_INACTIVE) { - sr_err("Device already open."); - return SR_ERR; - } - /* Try the whole shebang three times, fingers crossed. */ for (i = 0; i < 3; i++) { ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb); diff --git a/src/hardware/testo/protocol.c b/src/hardware/testo/protocol.c index 8ea254eb..def73d42 100644 --- a/src/hardware/testo/protocol.c +++ b/src/hardware/testo/protocol.c @@ -67,7 +67,7 @@ SR_PRIV int testo_probe_channels(struct sr_dev_inst *sdi) usb = sdi->conn; sr_dbg("Probing for channels."); - if (sdi->driver->dev_open(sdi) != SR_OK) + if (sr_dev_open(sdi) != SR_OK) return SR_ERR; if (testo_set_serial_params(usb) != SR_OK) return SR_ERR; diff --git a/src/hardware/yokogawa-dlm/api.c b/src/hardware/yokogawa-dlm/api.c index 728fce03..2faf7917 100644 --- a/src/hardware/yokogawa-dlm/api.c +++ b/src/hardware/yokogawa-dlm/api.c @@ -141,7 +141,7 @@ static int dev_clear(const struct sr_dev_driver *di) static int dev_open(struct sr_dev_inst *sdi) { - if (sdi->status != SR_ST_ACTIVE && sr_scpi_open(sdi->conn) != SR_OK) + if (sr_scpi_open(sdi->conn) != SR_OK) return SR_ERR; if (dlm_scope_state_query(sdi) != SR_OK)