]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/victor-dmm/api.c
Remove unnecessary driver context checks
[libsigrok.git] / src / hardware / victor-dmm / api.c
index 43b373422d81c8886dbd1bdd758380397df4f980..8f20f599fca7ead98f9cffe0ce06b11d45ba930c 100644 (file)
@@ -17,6 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <glib.h>
 #include <libusb.h>
 #include <stdlib.h>
@@ -32,7 +33,7 @@
 #define VICTOR_ENDPOINT (LIBUSB_ENDPOINT_IN | 1)
 
 SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
+static int dev_acquisition_stop(struct sr_dev_inst *sdi);
 
 static const uint32_t drvopts[] = {
        SR_CONF_MULTIMETER,
@@ -62,7 +63,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        struct libusb_device_descriptor des;
        libusb_device **devlist;
        GSList *devices;
-       int ret, i;
+       int i;
        char connection_id[64];
 
        (void)options;
@@ -72,11 +73,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        devices = NULL;
        libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
        for (i = 0; devlist[i]; i++) {
-               if ((ret = libusb_get_device_descriptor(devlist[i], &des)) != 0) {
-                       sr_warn("Failed to get device descriptor: %s",
-                                       libusb_error_name(ret));
-                       continue;
-               }
+               libusb_get_device_descriptor(devlist[i], &des);
 
                if (des.idVendor != VICTOR_VID || des.idProduct != VICTOR_PID)
                        continue;
@@ -104,11 +101,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options)
        return devices;
 }
 
-static GSList *dev_list(const struct sr_dev_driver *di)
-{
-       return ((struct drv_context *)(di->context))->instances;
-}
-
 static int dev_open(struct sr_dev_inst *sdi)
 {
        struct sr_dev_driver *di = sdi->driver;
@@ -118,11 +110,6 @@ static int dev_open(struct sr_dev_inst *sdi)
        int ret, i;
        char connection_id[64];
 
-       if (!di->context) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        usb = sdi->conn;
 
        libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
@@ -164,14 +151,8 @@ static int dev_open(struct sr_dev_inst *sdi)
 
 static int dev_close(struct sr_dev_inst *sdi)
 {
-       struct sr_dev_driver *di = sdi->driver;
        struct sr_usb_dev_inst *usb;
 
-       if (!di->context) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        usb = sdi->conn;
 
        if (!usb->devhdl)
@@ -186,21 +167,6 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(const struct sr_dev_driver *di)
-{
-       int ret;
-       struct drv_context *drvc;
-
-       if (!(drvc = di->context))
-               /* Can get called on an unused driver, doesn't matter. */
-               return SR_OK;
-
-       ret = std_dev_clear(di, NULL);
-       g_free(drvc);
-
-       return ret;
-}
-
 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
@@ -227,7 +193,6 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
-       struct sr_dev_driver *di = sdi->driver;
        struct dev_context *devc;
        gint64 now;
 
@@ -236,11 +201,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       if (!di->context) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        devc = sdi->priv;
 
        switch (key) {
@@ -295,14 +255,14 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
        devc = sdi->priv;
        if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
                /* USB device was unplugged. */
-               dev_acquisition_stop(sdi, sdi);
+               dev_acquisition_stop(sdi);
        } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
                sr_dbg("Got %d-byte packet.", transfer->actual_length);
                if (transfer->actual_length == DMM_DATA_SIZE) {
                        victor_dmm_receive_data(sdi, transfer->buffer);
                        if (devc->limit_samples) {
                                if (devc->num_samples >= devc->limit_samples)
-                                       dev_acquisition_stop(sdi, sdi);
+                                       dev_acquisition_stop(sdi);
                        }
                }
        }
@@ -316,7 +276,7 @@ static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
                               libusb_error_name(ret));
                        g_free(transfer->buffer);
                        libusb_free_transfer(transfer);
-                       dev_acquisition_stop(sdi, sdi);
+                       dev_acquisition_stop(sdi);
                }
        } else {
                /* This was the last transfer we're going to receive, so
@@ -330,7 +290,6 @@ static int handle_events(int fd, int revents, void *cb_data)
 {
        struct dev_context *devc;
        struct drv_context *drvc;
-       struct sr_datafeed_packet packet;
        struct sr_dev_inst *sdi;
        struct sr_dev_driver *di;
        struct timeval tv;
@@ -347,16 +306,13 @@ static int handle_events(int fd, int revents, void *cb_data)
        if (devc->limit_msec) {
                now = g_get_monotonic_time() / 1000;
                if (now > devc->end_time)
-                       dev_acquisition_stop(sdi, sdi);
+                       dev_acquisition_stop(sdi);
        }
 
        if (sdi->status == SR_ST_STOPPING) {
                usb_source_remove(sdi->session, drvc->sr_ctx);
-
                dev_close(sdi);
-
-               packet.type = SR_DF_END;
-               sr_session_send(cb_data, &packet);
+               std_session_send_df_end(sdi, LOG_PREFIX);
        }
 
        memset(&tv, 0, sizeof(struct timeval));
@@ -366,10 +322,9 @@ static int handle_events(int fd, int revents, void *cb_data)
        return TRUE;
 }
 
-static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_start(const struct sr_dev_inst *sdi)
 {
        struct sr_dev_driver *di = sdi->driver;
-       struct dev_context *devc;
        struct drv_context *drvc = di->context;
        struct sr_usb_dev_inst *usb;
        struct libusb_transfer *transfer;
@@ -379,17 +334,9 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       if (!di->context) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
-       devc = sdi->priv;
        usb = sdi->conn;
-       devc->cb_data = cb_data;
 
-       /* Send header packet to the session bus. */
-       std_session_send_df_header(cb_data, LOG_PREFIX);
+       std_session_send_df_header(sdi, LOG_PREFIX);
 
        usb_source_add(sdi->session, drvc->sr_ctx, 100,
                        handle_events, (void *)sdi);
@@ -402,7 +349,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
         * handling above. */
        libusb_fill_interrupt_transfer(transfer, usb->devhdl,
                        VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
-                       cb_data, 100);
+                       (struct sr_dev_inst *)sdi, 100);
        if ((ret = libusb_submit_transfer(transfer) != 0)) {
                sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
                libusb_free_transfer(transfer);
@@ -413,16 +360,8 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        return SR_OK;
 }
 
-static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
+static int dev_acquisition_stop(struct sr_dev_inst *sdi)
 {
-       struct sr_dev_driver *di = sdi->driver;
-       (void)cb_data;
-
-       if (!di->context) {
-               sr_err("Driver was not initialized.");
-               return SR_ERR;
-       }
-
        if (sdi->status != SR_ST_ACTIVE) {
                sr_err("Device not active, can't stop acquisition.");
                return SR_ERR;
@@ -438,9 +377,9 @@ SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
        .longname = "Victor DMMs",
        .api_version = 1,
        .init = init,
-       .cleanup = cleanup,
+       .cleanup = std_cleanup,
        .scan = scan,
-       .dev_list = dev_list,
+       .dev_list = std_dev_list,
        .dev_clear = NULL,
        .config_get = config_get,
        .config_set = config_set,