]> sigrok.org Git - libsigrok.git/commitdiff
hw_dev_close(): Move common checks to wrapper.
authorUwe Hermann <redacted>
Fri, 1 Feb 2013 21:58:54 +0000 (22:58 +0100)
committerUwe Hermann <redacted>
Tue, 5 Feb 2013 17:46:33 +0000 (18:46 +0100)
Check the relevant arguments for != NULL before calling the actual
driver-specific function, so that the driver can safely assume those
arguments are non-NULL. This removes the need to duplicate these
checks in every driver.

hardware/agilent-dmm/api.c
hardware/alsa/api.c
hardware/asix-sigma/asix-sigma.c
hardware/brymen-dmm/api.c
hardware/chronovu-la8/api.c
hardware/colead-slm/api.c
hardware/fluke-dmm/api.c
hardware/fx2lafw/fx2lafw.c
hardware/serial-dmm/api.c
hardware/zeroplus-logic-cube/zeroplus.c
session.c

index 61d4b313ce25cedf206efd895ac38a42c82db7bd..64f6275ada5da29d0f9b341db036a596311a62f6 100644 (file)
@@ -212,10 +212,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("sdi->priv was NULL.");
-               return SR_ERR_BUG;
-       }
+       devc = sdi->priv;
 
        if (devc->serial && devc->serial->fd != -1) {
                serial_close(devc->serial);
index 5751675058a3ec5338e636e47bccc61e28d3b8f2..c546b40036cfa204fbd230b7d2a45c6bcd3cced6 100644 (file)
@@ -103,8 +103,6 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 
        devc = sdi->priv;
 
-       sr_dbg("Closing device.");
-
        if (devc->capture_handle) {
                sr_dbg("Closing PCM device.");
                if ((ret = snd_pcm_close(devc->capture_handle)) < 0) {
index 6d7fcde4b9b7ac95212aca1d4106464bfb40808f..acd3acb9db733153bd4ee79257fbd355c65f56c1 100644 (file)
@@ -752,10 +752,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("%s: sdi->priv was NULL", __func__);
-               return SR_ERR_BUG;
-       }
+       devc = sdi->priv;
 
        /* TODO */
        if (sdi->status == SR_ST_ACTIVE)
index 83c7bc479b3205bf77c16ad2f567751c3b7c8eea..7d60fd031bbf553e1f87c5f8c0fba7e7da2b0300 100644 (file)
@@ -196,10 +196,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("sdi->priv was NULL.");
-               return SR_ERR_BUG;
-       }
+       devc = sdi->priv;
 
        if (devc->serial && devc->serial->fd != -1) {
                serial_close(devc->serial);
index 6bbdcdbbc893197bfeb99433aea8e4ab6ddd9425..bf80fc6bbcb72b27ae4d262ed87c711947fbc5b1 100644 (file)
@@ -240,12 +240,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("%s: sdi->priv was NULL.", __func__);
-               return SR_ERR_BUG;
-       }
-
-       sr_dbg("Closing device.");
+       devc = sdi->priv;
 
        if (sdi->status == SR_ST_ACTIVE) {
                sr_dbg("Status ACTIVE, closing device.");
@@ -256,7 +251,6 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 
        sdi->status = SR_ST_INACTIVE;
 
-       sr_dbg("Freeing sample buffer.");
        g_free(devc->final_buf);
 
        return SR_OK;
index cfeb45bd9a2fe777b1c5ed14356b0350396b48c4..ccd5f6029b7ed08b232eb920f8ad099bc73149c7 100644 (file)
@@ -158,10 +158,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("sdi->priv was NULL.");
-               return SR_ERR_BUG;
-       }
+       devc = sdi->priv;
 
        if (devc->serial && devc->serial->fd != -1) {
                serial_close(devc->serial);
index 3ef69e975639bbf67437c3a59a4f47ad1eec322b..c8280d4a5c4e3f1cadd9504f7df48fa7cf63a110 100644 (file)
@@ -246,10 +246,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("sdi->priv was NULL.");
-               return SR_ERR_BUG;
-       }
+       devc = sdi->priv;
 
        if (devc->serial && devc->serial->fd != -1) {
                serial_close(devc->serial);
index 75b4557c9ebf70ada03b7497e7060b4c5bb45668..e3260e55127eee5f071418cd1ecad76c6887d28d 100644 (file)
@@ -559,6 +559,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
        struct dev_context *devc;
 
        devc = sdi->priv;
+
        if (devc->usb->devhdl == NULL)
                return SR_ERR;
 
index 2b5739f46b095dd3681880427e258d71fdd30edc..2d991dcef251ecbb0996c19285e1a325cf920fd8 100644 (file)
@@ -339,10 +339,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("sdi->priv was NULL.");
-               return SR_ERR_BUG;
-       }
+       devc = sdi->priv;
 
        if (devc->serial && devc->serial->fd != -1) {
                serial_close(devc->serial);
index 3acc977db2e517c01dd96bb8337924c08180aeb7..32bf026884dcacef44fb3a8960ec966927fbb25b 100644 (file)
@@ -483,10 +483,7 @@ static int hw_dev_close(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
 
-       if (!(devc = sdi->priv)) {
-               sr_err("%s: sdi->priv was NULL", __func__);
-               return SR_ERR;
-       }
+       devc = sdi->priv;
 
        if (!devc->usb->devhdl)
                return SR_ERR;
index 7bff474b0aeb982afe448e5e3a73e12dfd5f4ade..8f7c93544d46930b9740793877edb826d7a1686d 100644 (file)
--- a/session.c
+++ b/session.c
@@ -107,10 +107,46 @@ SR_API int sr_session_destroy(void)
        return SR_OK;
 }
 
+/**
+ * Close a device instance.
+ *
+ * @param sdi The device instance to close. Must not be NULL. Also,
+ *            sdi->driver, sdi->driver->priv, and sdi->priv must not be NULL.
+ */
 static void sr_dev_close(struct sr_dev_inst *sdi)
 {
-       if (sdi->driver && sdi->driver->dev_close)
-               sdi->driver->dev_close(sdi);
+       int ret;
+
+       if (!sdi) {
+               sr_err("Invalid device instance, can't close device.");
+               return;
+       }
+
+       /* In the drivers sdi->priv is a 'struct dev_context *devc'. */
+       if (!sdi->priv) {
+               /*
+                * Should be sr_err() in theory, but the 'demo' driver has
+                * NULL for sdi->priv, so we use sr_dbg() until that's fixed.
+                */
+               sr_dbg("Invalid device context, can't close device.");
+               return;
+       }
+
+       if (!sdi->driver) {
+               sr_err("Invalid driver, can't close device.");
+               return;
+       }
+
+       if (!sdi->driver->priv) {
+               sr_err("Driver not initialized, can't close device.");
+               return;
+       }
+
+       sr_spew("Closing '%s' device instance %d.", sdi->driver->name,
+               sdi->index);
+
+       if ((ret = sdi->driver->dev_close(sdi)) < 0)
+               sr_err("Failed to close device instance: %d.", ret);
 }
 
 /**