From: Uwe Hermann Date: Fri, 1 Feb 2013 21:58:54 +0000 (+0100) Subject: hw_dev_close(): Move common checks to wrapper. X-Git-Tag: dsupstream~278 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=961009b0c4002717c669a0cdcafb0fcf29f5ea1b;p=libsigrok.git hw_dev_close(): Move common checks to wrapper. 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. --- diff --git a/hardware/agilent-dmm/api.c b/hardware/agilent-dmm/api.c index 61d4b313..64f6275a 100644 --- a/hardware/agilent-dmm/api.c +++ b/hardware/agilent-dmm/api.c @@ -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); diff --git a/hardware/alsa/api.c b/hardware/alsa/api.c index 57516750..c546b400 100644 --- a/hardware/alsa/api.c +++ b/hardware/alsa/api.c @@ -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) { diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index 6d7fcde4..acd3acb9 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -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) diff --git a/hardware/brymen-dmm/api.c b/hardware/brymen-dmm/api.c index 83c7bc47..7d60fd03 100644 --- a/hardware/brymen-dmm/api.c +++ b/hardware/brymen-dmm/api.c @@ -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); diff --git a/hardware/chronovu-la8/api.c b/hardware/chronovu-la8/api.c index 6bbdcdbb..bf80fc6b 100644 --- a/hardware/chronovu-la8/api.c +++ b/hardware/chronovu-la8/api.c @@ -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; diff --git a/hardware/colead-slm/api.c b/hardware/colead-slm/api.c index cfeb45bd..ccd5f602 100644 --- a/hardware/colead-slm/api.c +++ b/hardware/colead-slm/api.c @@ -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); diff --git a/hardware/fluke-dmm/api.c b/hardware/fluke-dmm/api.c index 3ef69e97..c8280d4a 100644 --- a/hardware/fluke-dmm/api.c +++ b/hardware/fluke-dmm/api.c @@ -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); diff --git a/hardware/fx2lafw/fx2lafw.c b/hardware/fx2lafw/fx2lafw.c index 75b4557c..e3260e55 100644 --- a/hardware/fx2lafw/fx2lafw.c +++ b/hardware/fx2lafw/fx2lafw.c @@ -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; diff --git a/hardware/serial-dmm/api.c b/hardware/serial-dmm/api.c index 2b5739f4..2d991dce 100644 --- a/hardware/serial-dmm/api.c +++ b/hardware/serial-dmm/api.c @@ -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); diff --git a/hardware/zeroplus-logic-cube/zeroplus.c b/hardware/zeroplus-logic-cube/zeroplus.c index 3acc977d..32bf0268 100644 --- a/hardware/zeroplus-logic-cube/zeroplus.c +++ b/hardware/zeroplus-logic-cube/zeroplus.c @@ -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; diff --git a/session.c b/session.c index 7bff474b..8f7c9354 100644 --- 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); } /**