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.
This commit is contained in:
Uwe Hermann 2017-07-06 20:24:12 +02:00
parent 89ab9fc39c
commit 093e1cba6b
23 changed files with 29 additions and 77 deletions

View 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;

View File

@ -161,8 +161,6 @@ static int dev_close(struct sr_dev_inst *sdi)
devc = sdi->priv;
/* TODO */
if (sdi->status == SR_ST_ACTIVE)
ftdi_usb_close(&devc->ftdic);
sdi->status = SR_ST_INACTIVE;

View 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);
}
sdi->status = SR_ST_INACTIVE;
return SR_OK;
}

View 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;

View 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;

View 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;

View 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)

View 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;

View 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. */

View 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;

View 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)

View 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,

View 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;

View 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) {

View 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)

View 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;

View 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;

View 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) {

View 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. */

View 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]);

View 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;

View 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;

View File

@ -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;
serial = sdi->conn;
if (serial && sdi->status == SR_ST_ACTIVE) {
serial_close(serial);
sdi->status = SR_ST_INACTIVE;
serial = sdi->conn;
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;
}