]> sigrok.org Git - libsigrok.git/commitdiff
sr_dev_close(): Factor out SR_ERR_DEV_CLOSED check.
authorUwe Hermann <redacted>
Thu, 6 Jul 2017 18:24:12 +0000 (20:24 +0200)
committerUwe Hermann <redacted>
Fri, 7 Jul 2017 19:51:25 +0000 (21:51 +0200)
This ensures consistent checks and log messages across all drivers
and reduces the per-driver boilerplate.

23 files changed:
src/device.c
src/hardware/asix-sigma/api.c
src/hardware/beaglelogic/api.c
src/hardware/brymen-bm86x/api.c
src/hardware/chronovu-la/api.c
src/hardware/gmc-mh-1x-2x/api.c
src/hardware/gwinstek-gds-800/api.c
src/hardware/hameg-hmo/api.c
src/hardware/hp-3457a/api.c
src/hardware/hung-chang-dso-2100/api.c
src/hardware/ikalogic-scanalogic2/protocol.c
src/hardware/ikalogic-scanaplus/api.c
src/hardware/lecroy-xstream/api.c
src/hardware/maynuo-m97/api.c
src/hardware/pipistrello-ols/api.c
src/hardware/rigol-ds/api.c
src/hardware/rohde-schwarz-sme-0x/api.c
src/hardware/scpi-pps/api.c
src/hardware/sysclk-lwla/api.c
src/hardware/testo/protocol.c
src/hardware/yokogawa-dlm/api.c
src/hardware/zeroplus-logic-cube/api.c
src/std.c

index aa499c8d74252ad07a37cf54afbec3b1233196ce..6eff16d16573e45c78f0e836a168ff72ff15d31f 100644 (file)
@@ -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;
index 62cfaa9bc4d5e393b912d55c5490da5fd131abde..676be07f2d0f86e122d461f1241dc2f9e448dde3 100644 (file)
@@ -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;
 
index 09fcf0bdb5577459d49b2f7602b8ba03aaa0ff51..0d022f2b336967e0aea956071d21ca92589c55eb 100644 (file)
@@ -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;
 }
 
index def7b642dda7e5aa8c1a1469c1a5fbf7fea1307c..cafba7da33e0a6b6c833ccc0e2e074486734ea61 100644 (file)
@@ -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;
 
index d85c6c9a281adf9bebaf914402986f117a34bc45..9f793969464261fd113e1651cec8ea572313809f 100644 (file)
@@ -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;
index 71ab57d097a3e1e8b76ac0d4272f1f1431c74ad1..992cbd8124faf3561857019690d431b21e7ec6b5 100644 (file)
@@ -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;
 
index d64d43eb43dd3a73f10ce9efbef66744aae87d59..2ab06957e2ed39e98a2e2a74d7888a7f30bb8b59 100644 (file)
@@ -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)
index 7b5a71db1c42e3fa5075f2b730b5f4d4a3bb9bb8..d4e184811763d20785302f514f70eeb770a07746 100644 (file)
@@ -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;
index df8d2a58d834e801a91e18a244ae099c4160b9ef..195b9804f8ed081f0f820a5eebcc3dd9e213afd0 100644 (file)
@@ -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. */
index 652b3cb3ac4d25418f300b68fccae91244240777..df2cddbba8000654e34890f7be218df35d003324 100644 (file)
@@ -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;
index 45d60db507855837b000e36bf64acc4988e88ea5..c58050bbb102e316c3d580108793405f81415f8c 100644 (file)
@@ -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)
index eb33a4508dbc8884ce159044535a15a747f7135e..1bc17b73528c226979a9b0d353004fa2302dd0b1 100644 (file)
@@ -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,
index 748fbd1e03bf155290783a157262c782acbf31e0..480866474b747791957df86f201b4912dc17c205 100644 (file)
@@ -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;
index aae0d094a4a23a15e123c4011b9f0bf381752b42..970bfe95615c3fb069067fc04d6c332c12d57d0c 100644 (file)
@@ -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) {
index 21b7185fb6bfa0eb1ace1d380b84da58829cbf93..9e274f3b5aecf8207951e8edce6afcb2458ed27e 100644 (file)
@@ -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)
index 40d87bda0f63c26892de3f1a77d9997eea9392bc..9fb2f4066fd148698b60f6fb091d178feb8760b3 100644 (file)
@@ -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;
 
index 95a6d01d6e6999ee2bd5ccd1fc7376f31de5c85e..ff04242cab252dd58f9105d428d45d70669a5304 100644 (file)
@@ -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;
index 0136ca90b58e5c47ee183dfd41f4bf3c6620e180..2746dc4019dc01b5c1f9a56446b536a882a724b8 100644 (file)
@@ -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) {
index bd40b7f64e0fdb7d65c49446f70837d55d00fb41..da2019fba31eb51fbc44b82d1a37cae4309168c5 100644 (file)
@@ -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. */
index 3fc7806f0247df5a617cb0ba6041c9e1136b4b44..8ea254eb17aaf8320390a5e037e25b84619a3d1e 100644 (file)
@@ -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]);
index 891775cc412985c823f1535c170203dade6c2f06..728fce03f5502ec3fe25e5ac0e633083444094e9 100644 (file)
@@ -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;
index 0e31ddf7dcea07898ae6a304f88051cb8a2f7bbf..7803407a21ce9c5d6bbacfd6ff747a34a20412eb 100644 (file)
@@ -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;
index df7a17495ed8dda02952f6943197257842a6d66d..ccb73718c9b26b0fdf129b43439f437c5405e414 100644 (file)
--- 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;
        }