From: Uwe Hermann Date: Thu, 6 Jul 2017 18:24:12 +0000 (+0200) Subject: sr_dev_close(): Factor out SR_ERR_DEV_CLOSED check. X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=commitdiff_plain;h=093e1cba6b7bf14cfb77fa36f59b0c16e6fca7cc;hp=89ab9fc39c015d541b2fdd4d9d2fb5e00f63be8a sr_dev_close(): Factor out SR_ERR_DEV_CLOSED 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 aa499c8d..6eff16d1 100644 --- a/src/device.c +++ b/src/device.c @@ -581,6 +581,14 @@ SR_API int sr_dev_close(struct sr_dev_inst *sdi) if (!sdi || !sdi->driver || !sdi->driver->dev_close) return SR_ERR; + if (sdi->status != SR_ST_ACTIVE) { + sr_err("%s: Device instance not active, can't close.", + sdi->driver->name); + return SR_ERR_DEV_CLOSED; + } + + sr_dbg("%s: Closing device.", sdi->driver->name) + ret = sdi->driver->dev_close(sdi); return ret; diff --git a/src/hardware/asix-sigma/api.c b/src/hardware/asix-sigma/api.c index 62cfaa9b..676be07f 100644 --- a/src/hardware/asix-sigma/api.c +++ b/src/hardware/asix-sigma/api.c @@ -161,9 +161,7 @@ static int dev_close(struct sr_dev_inst *sdi) devc = sdi->priv; - /* TODO */ - if (sdi->status == SR_ST_ACTIVE) - ftdi_usb_close(&devc->ftdic); + ftdi_usb_close(&devc->ftdic); sdi->status = SR_ST_INACTIVE; diff --git a/src/hardware/beaglelogic/api.c b/src/hardware/beaglelogic/api.c index 09fcf0bd..0d022f2b 100644 --- a/src/hardware/beaglelogic/api.c +++ b/src/hardware/beaglelogic/api.c @@ -166,12 +166,12 @@ static int dev_close(struct sr_dev_inst *sdi) { struct dev_context *devc = sdi->priv; - if (sdi->status == SR_ST_ACTIVE) { - /* Close the memory mapping and the file */ - beaglelogic_munmap(devc); - beaglelogic_close(devc); - } + /* Close the memory mapping and the file */ + beaglelogic_munmap(devc); + beaglelogic_close(devc); + sdi->status = SR_ST_INACTIVE; + return SR_OK; } diff --git a/src/hardware/brymen-bm86x/api.c b/src/hardware/brymen-bm86x/api.c index def7b642..cafba7da 100644 --- a/src/hardware/brymen-bm86x/api.c +++ b/src/hardware/brymen-bm86x/api.c @@ -131,9 +131,6 @@ static int dev_close(struct sr_dev_inst *sdi) struct dev_context *devc; int ret; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - usb = sdi->conn; devc = sdi->priv; diff --git a/src/hardware/chronovu-la/api.c b/src/hardware/chronovu-la/api.c index d85c6c9a..9f793969 100644 --- a/src/hardware/chronovu-la/api.c +++ b/src/hardware/chronovu-la/api.c @@ -288,14 +288,12 @@ static int dev_close(struct sr_dev_inst *sdi) int ret; struct dev_context *devc; - if (sdi->status != SR_ST_ACTIVE) - return SR_OK; - devc = sdi->priv; if (devc->ftdic && (ret = ftdi_usb_close(devc->ftdic)) < 0) sr_err("Failed to close FTDI device (%d): %s.", ret, ftdi_get_error_string(devc->ftdic)); + sdi->status = SR_ST_INACTIVE; return SR_OK; diff --git a/src/hardware/gmc-mh-1x-2x/api.c b/src/hardware/gmc-mh-1x-2x/api.c index 71ab57d0..992cbd81 100644 --- a/src/hardware/gmc-mh-1x-2x/api.c +++ b/src/hardware/gmc-mh-1x-2x/api.c @@ -336,7 +336,6 @@ static int dev_close(struct sr_dev_inst *sdi) std_serial_dev_close(sdi); - sdi->status = SR_ST_INACTIVE; if ((devc = sdi->priv)) devc->model = METRAHIT_NONE; diff --git a/src/hardware/gwinstek-gds-800/api.c b/src/hardware/gwinstek-gds-800/api.c index d64d43eb..2ab06957 100644 --- a/src/hardware/gwinstek-gds-800/api.c +++ b/src/hardware/gwinstek-gds-800/api.c @@ -107,9 +107,6 @@ static int dev_close(struct sr_dev_inst *sdi) { struct sr_scpi_dev_inst *scpi; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - scpi = sdi->conn; if (scpi) { if (sr_scpi_close(scpi) < 0) diff --git a/src/hardware/hameg-hmo/api.c b/src/hardware/hameg-hmo/api.c index 7b5a71db..d4e18481 100644 --- a/src/hardware/hameg-hmo/api.c +++ b/src/hardware/hameg-hmo/api.c @@ -144,9 +144,6 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - if (sdi->status == SR_ST_INACTIVE) - return SR_OK; - sr_scpi_close(sdi->conn); sdi->status = SR_ST_INACTIVE; diff --git a/src/hardware/hp-3457a/api.c b/src/hardware/hp-3457a/api.c index df8d2a58..195b9804 100644 --- a/src/hardware/hp-3457a/api.c +++ b/src/hardware/hp-3457a/api.c @@ -214,9 +214,6 @@ static int dev_close(struct sr_dev_inst *sdi) { struct sr_scpi_dev_inst *scpi = sdi->conn; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - /* Disable scan-advance (preserve relay life). */ sr_scpi_send(scpi, "SADV HOLD"); /* Switch back to auto-triggering. */ diff --git a/src/hardware/hung-chang-dso-2100/api.c b/src/hardware/hung-chang-dso-2100/api.c index 652b3cb3..df2cddbb 100644 --- a/src/hardware/hung-chang-dso-2100/api.c +++ b/src/hardware/hung-chang-dso-2100/api.c @@ -282,13 +282,11 @@ static int dev_close(struct sr_dev_inst *sdi) { struct dev_context *devc = sdi->priv; - if (sdi->status != SR_ST_ACTIVE) - return SR_OK; - g_free(devc->samples); hung_chang_dso_2100_reset_port(sdi->conn); ieee1284_release(sdi->conn); ieee1284_close(sdi->conn); + sdi->status = SR_ST_INACTIVE; return SR_OK; diff --git a/src/hardware/ikalogic-scanalogic2/protocol.c b/src/hardware/ikalogic-scanalogic2/protocol.c index 45d60db5..c58050bb 100644 --- a/src/hardware/ikalogic-scanalogic2/protocol.c +++ b/src/hardware/ikalogic-scanalogic2/protocol.c @@ -41,7 +41,7 @@ static void abort_acquisition(struct sr_dev_inst *sdi) std_session_send_df_end(sdi); - sdi->driver->dev_close(sdi); + sr_dev_close(sdi); } static void buffer_sample_data(const struct sr_dev_inst *sdi) diff --git a/src/hardware/ikalogic-scanaplus/api.c b/src/hardware/ikalogic-scanaplus/api.c index eb33a450..1bc17b73 100644 --- a/src/hardware/ikalogic-scanaplus/api.c +++ b/src/hardware/ikalogic-scanaplus/api.c @@ -218,22 +218,13 @@ err_dev_open_close_ftdic: static int dev_close(struct sr_dev_inst *sdi) { - int ret; struct dev_context *devc; - ret = SR_OK; devc = sdi->priv; - if (sdi->status == SR_ST_ACTIVE) { - sr_dbg("Status ACTIVE, closing device."); - ret = scanaplus_close(devc); - } else { - sr_spew("Status not ACTIVE, nothing to do."); - } - sdi->status = SR_ST_INACTIVE; - return ret; + return scanaplus_close(devc); } static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, diff --git a/src/hardware/lecroy-xstream/api.c b/src/hardware/lecroy-xstream/api.c index 748fbd1e..48086647 100644 --- a/src/hardware/lecroy-xstream/api.c +++ b/src/hardware/lecroy-xstream/api.c @@ -148,9 +148,6 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - if (sdi->status == SR_ST_INACTIVE) - return SR_OK; - sr_scpi_close(sdi->conn); sdi->status = SR_ST_INACTIVE; diff --git a/src/hardware/maynuo-m97/api.c b/src/hardware/maynuo-m97/api.c index aae0d094..970bfe95 100644 --- a/src/hardware/maynuo-m97/api.c +++ b/src/hardware/maynuo-m97/api.c @@ -215,9 +215,6 @@ static int dev_close(struct sr_dev_inst *sdi) struct dev_context *devc; struct sr_modbus_dev_inst *modbus; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - modbus = sdi->conn; if (modbus) { diff --git a/src/hardware/pipistrello-ols/api.c b/src/hardware/pipistrello-ols/api.c index 21b7185f..9e274f3b 100644 --- a/src/hardware/pipistrello-ols/api.c +++ b/src/hardware/pipistrello-ols/api.c @@ -421,22 +421,13 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - int ret; struct dev_context *devc; - ret = SR_OK; devc = sdi->priv; - if (sdi->status == SR_ST_ACTIVE) { - sr_dbg("Status ACTIVE, closing device."); - ret = p_ols_close(devc); - } else { - sr_spew("Status not ACTIVE, nothing to do."); - } - sdi->status = SR_ST_INACTIVE; - return ret; + return p_ols_close(devc); } static int set_trigger(const struct sr_dev_inst *sdi, int stage) diff --git a/src/hardware/rigol-ds/api.c b/src/hardware/rigol-ds/api.c index 40d87bda..9fb2f406 100644 --- a/src/hardware/rigol-ds/api.c +++ b/src/hardware/rigol-ds/api.c @@ -455,9 +455,6 @@ static int dev_close(struct sr_dev_inst *sdi) struct sr_scpi_dev_inst *scpi; struct dev_context *devc; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - scpi = sdi->conn; devc = sdi->priv; diff --git a/src/hardware/rohde-schwarz-sme-0x/api.c b/src/hardware/rohde-schwarz-sme-0x/api.c index 95a6d01d..ff04242c 100644 --- a/src/hardware/rohde-schwarz-sme-0x/api.c +++ b/src/hardware/rohde-schwarz-sme-0x/api.c @@ -172,9 +172,6 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - if (sdi->status == SR_ST_INACTIVE) - return SR_OK; - sr_scpi_close(sdi->conn); sdi->status = SR_ST_INACTIVE; diff --git a/src/hardware/scpi-pps/api.c b/src/hardware/scpi-pps/api.c index 0136ca90..2746dc40 100644 --- a/src/hardware/scpi-pps/api.c +++ b/src/hardware/scpi-pps/api.c @@ -280,9 +280,6 @@ static int dev_close(struct sr_dev_inst *sdi) struct sr_scpi_dev_inst *scpi; struct dev_context *devc; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - devc = sdi->priv; scpi = sdi->conn; if (scpi) { diff --git a/src/hardware/sysclk-lwla/api.c b/src/hardware/sysclk-lwla/api.c index bd40b7f6..da2019fb 100644 --- a/src/hardware/sysclk-lwla/api.c +++ b/src/hardware/sysclk-lwla/api.c @@ -343,10 +343,6 @@ static int dev_close(struct sr_dev_inst *sdi) devc = sdi->priv; usb = sdi->conn; - if (sdi->status == SR_ST_INACTIVE) { - sr_dbg("Device already closed."); - return SR_OK; - } if (devc->acquisition) { sr_err("Cannot close device during acquisition!"); /* Request stop, leak handle, and prepare for the worst. */ diff --git a/src/hardware/testo/protocol.c b/src/hardware/testo/protocol.c index 3fc7806f..8ea254eb 100644 --- a/src/hardware/testo/protocol.c +++ b/src/hardware/testo/protocol.c @@ -108,7 +108,7 @@ SR_PRIV int testo_probe_channels(struct sr_dev_inst *sdi) /* Got a complete packet. */ break; } - sdi->driver->dev_close(sdi); + sr_dev_close(sdi); if (packet[6] > MAX_CHANNELS) { sr_err("Device says it has %d channels!", packet[6]); diff --git a/src/hardware/yokogawa-dlm/api.c b/src/hardware/yokogawa-dlm/api.c index 891775cc..728fce03 100644 --- a/src/hardware/yokogawa-dlm/api.c +++ b/src/hardware/yokogawa-dlm/api.c @@ -154,9 +154,6 @@ static int dev_open(struct sr_dev_inst *sdi) static int dev_close(struct sr_dev_inst *sdi) { - if (sdi->status == SR_ST_INACTIVE) - return SR_OK; - sr_scpi_close(sdi->conn); sdi->status = SR_ST_INACTIVE; diff --git a/src/hardware/zeroplus-logic-cube/api.c b/src/hardware/zeroplus-logic-cube/api.c index 0e31ddf7..7803407a 100644 --- a/src/hardware/zeroplus-logic-cube/api.c +++ b/src/hardware/zeroplus-logic-cube/api.c @@ -127,8 +127,6 @@ const uint64_t samplerates_200[] = { SR_MHZ(200), }; -static int dev_close(struct sr_dev_inst *sdi); - SR_PRIV int zp_set_samplerate(struct dev_context *devc, uint64_t samplerate) { int i; diff --git a/src/std.c b/src/std.c index df7a1749..ccb73718 100644 --- a/src/std.c +++ b/src/std.c @@ -181,17 +181,22 @@ SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi) * to SR_ST_INACTIVE. * * @retval SR_OK Success. + * @retval SR_ERR_ARG Invalid arguments. */ SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi) { struct sr_serial_dev_inst *serial; + sdi->status = SR_ST_INACTIVE; + serial = sdi->conn; - if (serial && sdi->status == SR_ST_ACTIVE) { - serial_close(serial); - sdi->status = SR_ST_INACTIVE; + if (!serial) { + sr_err("%s: Can't close invalid serial port.", sdi->driver->name); + return SR_ERR_ARG; } + serial_close(serial); + return SR_OK; } @@ -220,7 +225,7 @@ SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi) return ret; } - if ((ret = sdi->driver->dev_close(sdi)) < 0) { + if ((ret = sr_dev_close(sdi)) < 0) { sr_err("%s: Failed to close device: %d.", prefix, ret); return ret; }