sr_dev_open(): Set status to SR_ST_ACTIVE upon success.

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-07 22:36:24 +02:00
parent 6402c37916
commit 7e46362338
37 changed files with 72 additions and 119 deletions

View File

@ -545,11 +545,19 @@ SR_API int sr_dev_clear(const struct sr_dev_driver *driver)
}
/**
* Open the specified device.
* Open the specified device instance.
*
* If the device instance is already open (sdi->status == SR_ST_ACTIVE),
* SR_ERR will be returned and no re-opening of the device will be attempted.
*
* If opening was successful, sdi->status is set to SR_ST_ACTIVE, otherwise
* it will be left unchanged.
*
* @param sdi Device instance to use. Must not be NULL.
*
* @return SR_OK upon success, a negative error code upon errors.
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid arguments.
* @retval SR_ERR Device instance was already active, or other error.
*
* @since 0.2.0
*/
@ -558,7 +566,7 @@ SR_API int sr_dev_open(struct sr_dev_inst *sdi)
int ret;
if (!sdi || !sdi->driver || !sdi->driver->dev_open)
return SR_ERR;
return SR_ERR_ARG;
if (sdi->status == SR_ST_ACTIVE) {
sr_err("%s: Device instance already active, can't re-open.",
@ -566,10 +574,13 @@ SR_API int sr_dev_open(struct sr_dev_inst *sdi)
return SR_ERR;
}
sr_dbg("%s: Opening device.", sdi->driver->name)
sr_dbg("%s: Opening device instance.", sdi->driver->name);
ret = sdi->driver->dev_open(sdi);
if (ret == SR_OK)
sdi->status = SR_ST_ACTIVE;
return ret;
}

View File

@ -140,18 +140,13 @@ static int dev_open(struct sr_dev_inst *sdi)
devc = sdi->priv;
/* Make sure it's an ASIX SIGMA. */
if ((ret = ftdi_usb_open_desc(&devc->ftdic,
USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
sr_err("ftdi_usb_open failed: %s",
ftdi_get_error_string(&devc->ftdic));
return 0;
USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) {
sr_err("Failed to open device (%d): %s.",
ret, ftdi_get_error_string(&devc->ftdic));
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -130,8 +130,6 @@ static int dev_open(struct sr_dev_inst *sdi)
{
(void)sdi;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -157,8 +157,6 @@ static int dev_open(struct sr_dev_inst *sdi)
return SR_ERR;
}
/* We're good to go now */
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -95,9 +95,7 @@ static int dev_open(struct sr_dev_inst *sdi)
usb = sdi->conn;
devc = sdi->priv;
if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
sdi->status = SR_ST_ACTIVE;
else
if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) < 0)
return SR_ERR;
/* Detach kernel drivers which grabbed this device (if any). */
@ -122,7 +120,7 @@ static int dev_open(struct sr_dev_inst *sdi)
}
sr_dbg("Successfully claimed interface 0.");
return ret;
return SR_OK;
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -272,15 +272,12 @@ static int dev_open(struct sr_dev_inst *sdi)
/* Wait 100ms. */
g_usleep(100 * 1000);
sdi->status = SR_ST_ACTIVE;
if (ret == SR_OK)
return SR_OK;
return SR_OK;
err_ftdi_free:
ftdi_free(devc->ftdic); /* Close device (if open), free FTDI context. */
devc->ftdic = NULL;
return ret;
return SR_ERR;
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -182,7 +182,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
static int dev_open(struct sr_dev_inst *sdi)
{
sdi->status = SR_ST_ACTIVE;
(void)sdi;
return SR_OK;
}

View File

@ -560,7 +560,7 @@ SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
struct dev_context *devc;
struct drv_context *drvc;
struct version_info vi;
int ret, i, device_count;
int ret = SR_ERR, i, device_count;
uint8_t revid;
char connection_id[64];
@ -584,9 +584,7 @@ SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
if ((sdi->status == SR_ST_INITIALIZING) ||
(sdi->status == SR_ST_INACTIVE)) {
/*
* Check device by its physical USB bus/port address.
*/
/* Check device by its physical USB bus/port address. */
usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
if (strcmp(sdi->connection_id, connection_id))
/* This is not the one. */
@ -603,6 +601,7 @@ SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(ret));
ret = SR_ERR;
break;
}
@ -611,7 +610,8 @@ SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
sr_err("Failed to detach kernel driver: %s.",
libusb_error_name(ret));
return SR_ERR;
ret = SR_ERR;
break;
}
}
}
@ -637,10 +637,10 @@ SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
sr_err("Expected firmware version %d.x, "
"got %d.%d.", DSLOGIC_REQUIRED_VERSION_MAJOR,
vi.major, vi.minor);
ret = SR_ERR;
break;
}
sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / %s (physical), "
"interface %d, firmware %d.%d.",
usb->bus, usb->address, connection_id,
@ -649,14 +649,14 @@ SR_PRIV int dslogic_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
ret = SR_OK;
break;
}
libusb_free_device_list(devlist, 1);
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR;
return SR_OK;
return ret;
}
SR_PRIV struct dev_context *dslogic_dev_new(void)

View File

@ -302,13 +302,14 @@ static int dev_open(struct sr_dev_inst *sdi)
}
sr_dbg("FTDI chip bitbang mode entered successfully.");
sdi->status = SR_ST_ACTIVE;
return SR_OK;
err_dev_open_close_ftdic:
ftdi_usb_close(devc->ftdic);
err_ftdi_free:
ftdi_free(devc->ftdic);
return SR_ERR;
}

View File

@ -147,7 +147,7 @@ SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
struct dev_context *devc;
struct drv_context *drvc;
struct version_info vi;
int ret, i, device_count;
int ret = SR_ERR, i, device_count;
uint8_t revid;
char connection_id[64];
@ -190,6 +190,7 @@ SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(ret));
ret = SR_ERR;
break;
}
@ -198,7 +199,8 @@ SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
if ((ret = libusb_detach_kernel_driver(usb->devhdl, USB_INTERFACE)) < 0) {
sr_err("Failed to detach kernel driver: %s.",
libusb_error_name(ret));
return SR_ERR;
ret = SR_ERR;
break;
}
}
}
@ -227,7 +229,6 @@ SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
break;
}
sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / %s (physical), "
"interface %d, firmware %d.%d.",
usb->bus, usb->address, connection_id,
@ -236,14 +237,14 @@ SR_PRIV int fx2lafw_dev_open(struct sr_dev_inst *sdi, struct sr_dev_driver *di)
sr_info("Detected REVID=%d, it's a Cypress CY7C68013%s.",
revid, (revid != 1) ? " (FX2)" : "A (FX2LP)");
ret = SR_OK;
break;
}
libusb_free_device_list(devlist, 1);
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR;
return SR_OK;
return ret;
}
SR_PRIV struct dev_context *fx2lafw_dev_new(void)

View File

@ -98,8 +98,6 @@ static int dev_open(struct sr_dev_inst *sdi)
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -137,8 +137,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (hmo_scope_state_get(sdi) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -27,7 +27,7 @@ SR_PRIV int hantek_6xxx_open(struct sr_dev_inst *sdi)
struct sr_usb_dev_inst *usb;
struct libusb_device_descriptor des;
libusb_device **devlist;
int err, i;
int err = SR_ERR, i;
char connection_id[64];
devc = sdi->priv;
@ -61,14 +61,16 @@ SR_PRIV int hantek_6xxx_open(struct sr_dev_inst *sdi)
usb->address = libusb_get_device_address(devlist[i]);
}
sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / "
"%s (physical) interface %d.",
usb->bus, usb->address,
sdi->connection_id, USB_INTERFACE);
err = SR_OK;
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(err));
err = SR_ERR;
}
/* If we made it here, we handled the device (somehow). */
@ -77,10 +79,7 @@ SR_PRIV int hantek_6xxx_open(struct sr_dev_inst *sdi)
libusb_free_device_list(devlist, 1);
if (sdi->status != SR_ST_ACTIVE)
return SR_ERR;
return SR_OK;
return err;
}
SR_PRIV void hantek_6xxx_close(struct sr_dev_inst *sdi)

View File

@ -205,8 +205,6 @@ static int dev_open(struct sr_dev_inst *sdi)
sr_scpi_send(scpi, "TRIG HOLD");
sr_scpi_get_float(scpi, "NPLC?", &devc->nplc);
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -262,8 +262,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (!devc->samples)
goto fail3;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
fail3:

View File

@ -232,8 +232,6 @@ static int dev_open(struct sr_dev_inst *sdi)
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -207,12 +207,11 @@ static int dev_open(struct sr_dev_inst *sdi)
sr_dbg("Received ScanaPLUS device ID successfully: %02x %02x %02x.",
devc->devid[0], devc->devid[1], devc->devid[2]);
sdi->status = SR_ST_ACTIVE;
return SR_OK;
err_dev_open_close_ftdic:
scanaplus_close(devc);
return SR_ERR;
}

View File

@ -171,9 +171,8 @@ static int dev_open(struct sr_dev_inst *sdi)
sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return ret;
return SR_OK;
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -97,9 +97,8 @@ static int dev_open(struct sr_dev_inst *sdi)
sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return ret;
return SR_OK;
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -141,8 +141,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (lecroy_xstream_state_get(sdi) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -180,8 +180,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
/* FIXME: discard serial buffer */
mso_check_trigger(devc->serial, &devc->trigger_state);
sr_dbg("Trigger state: 0x%x.", devc->trigger_state);

View File

@ -203,8 +203,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (sr_modbus_open(modbus) < 0)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
maynuo_m97_set_bit(modbus, PC1, 1);
return SR_OK;

View File

@ -411,12 +411,7 @@ static int dev_open(struct sr_dev_inst *sdi)
devc = sdi->priv;
if (p_ols_open(devc) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
return p_ols_open(devc);
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -133,6 +133,7 @@ SR_PRIV int p_ols_open(struct dev_context *devc)
err_open_close_ftdic:
ftdi_usb_close(devc->ftdic);
return SR_ERR;
}

View File

@ -445,8 +445,6 @@ static int dev_open(struct sr_dev_inst *sdi)
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -162,12 +162,7 @@ static int dev_clear(const struct sr_dev_driver *di)
static int dev_open(struct sr_dev_inst *sdi)
{
if (sr_scpi_open(sdi->conn) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
return sr_scpi_open(sdi->conn);
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -205,8 +205,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (devc->dig_samplerate == 0)
devc->dig_samplerate = samplerates[3];
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -235,7 +235,7 @@ static int logic16_dev_open(struct sr_dev_inst *sdi)
struct sr_usb_dev_inst *usb;
struct libusb_device_descriptor des;
struct drv_context *drvc;
int ret, i, device_count;
int ret = SR_ERR, i, device_count;
char connection_id[64];
di = sdi->driver;
@ -276,6 +276,7 @@ static int logic16_dev_open(struct sr_dev_inst *sdi)
} else {
sr_err("Failed to open device: %s.",
libusb_error_name(ret));
ret = SR_ERR;
break;
}
@ -283,13 +284,16 @@ static int logic16_dev_open(struct sr_dev_inst *sdi)
if (ret == LIBUSB_ERROR_BUSY) {
sr_err("Unable to claim USB interface. Another "
"program or driver has already claimed it.");
ret = SR_ERR;
break;
} else if (ret == LIBUSB_ERROR_NO_DEVICE) {
sr_err("Device has been disconnected.");
ret = SR_ERR;
break;
} else if (ret != 0) {
sr_err("Unable to claim interface: %s.",
libusb_error_name(ret));
ret = SR_ERR;
break;
}
@ -298,15 +302,17 @@ static int logic16_dev_open(struct sr_dev_inst *sdi)
break;
}
sdi->status = SR_ST_ACTIVE;
sr_info("Opened device on %d.%d (logical) / %s (physical), interface %d.",
usb->bus, usb->address, sdi->connection_id, USB_INTERFACE);
ret = SR_OK;
break;
}
libusb_free_device_list(devlist, 1);
if (sdi->status != SR_ST_ACTIVE) {
if (ret != SR_OK) {
if (usb->devhdl) {
libusb_release_interface(usb->devhdl, USB_INTERFACE);
libusb_close(usb->devhdl);

View File

@ -255,8 +255,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (sr_scpi_open(scpi) < 0)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
devc = sdi->priv;
scpi_cmd(sdi, devc->device->commands, SCPI_CMD_REMOTE);
devc->beeper_was_set = FALSE;

View File

@ -303,8 +303,6 @@ static int dev_open(struct sr_dev_inst *sdi)
/* This delay appears to be necessary for reliable operation. */
g_usleep(30 * 1000);
sdi->status = SR_ST_ACTIVE;
devc->active_fpga_config = FPGA_NOCONF;
devc->short_transfer_quirk = FALSE;
devc->state = STATE_IDLE;
@ -317,7 +315,6 @@ static int dev_open(struct sr_dev_inst *sdi)
break;
/* Rinse and repeat. */
sdi->status = SR_ST_INACTIVE;
sr_usb_close(usb);
}

View File

@ -162,7 +162,6 @@ static int dev_open(struct sr_dev_inst *sdi)
sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -98,16 +98,12 @@ static int dev_open(struct sr_dev_inst *sdi)
struct sr_dev_driver *di;
struct drv_context *drvc;
struct sr_usb_dev_inst *usb;
int ret;
di = sdi->driver;
drvc = di->context;
usb = sdi->conn;
if ((ret = sr_usb_open(drvc->sr_ctx->libusb_ctx, usb)) == SR_OK)
sdi->status = SR_ST_ACTIVE;
return ret;
return sr_usb_open(drvc->sr_ctx->libusb_ctx, usb);
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -127,9 +127,8 @@ static int dev_open(struct sr_dev_inst *sdi)
sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return ret;
return SR_OK;
}
static int dev_close(struct sr_dev_inst *sdi)

View File

@ -120,7 +120,6 @@ static int dev_open(struct sr_dev_inst *sdi)
sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
return SR_ERR;
}
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -147,8 +147,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (dlm_scope_state_query(sdi) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
}

View File

@ -264,8 +264,6 @@ static int dev_open(struct sr_dev_inst *sdi)
if (ret != SR_OK)
return ret;
sdi->status = SR_ST_ACTIVE;
ret = libusb_set_configuration(usb->devhdl, USB_CONFIGURATION);
if (ret < 0) {
sr_err("Unable to set USB configuration %d: %s.",

View File

@ -152,23 +152,20 @@ SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
* callback in drivers that use a serial port. The port is opened
* with the SERIAL_RDWR flag.
*
* If the open succeeded, the status field of the given sdi is set
* to SR_ST_ACTIVE.
*
* @retval SR_OK Success.
* @retval SR_ERR_ARG Invalid arguments.
* @retval SR_ERR Serial port open failed.
*/
SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
{
struct sr_serial_dev_inst *serial;
if (!sdi || !sdi->conn)
return SR_ERR_ARG;
serial = sdi->conn;
if (serial_open(serial, SERIAL_RDWR) != SR_OK)
return SR_ERR;
sdi->status = SR_ST_ACTIVE;
return SR_OK;
return serial_open(serial, SERIAL_RDWR);
}
/**