]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/testo/api.c
Remove unnecessary dev_clear() callbacks
[libsigrok.git] / src / hardware / testo / api.c
index 3ceb08698d9ff24c8b7f86cb0fd3fb2d68a15c7c..6e3511c013eb4bd76ec169e792a800a53547a834 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <string.h>
 #include "protocol.h"
 
 #define SERIALCOMM "115200/8n1"
 
 SR_PRIV struct sr_dev_driver testo_driver_info;
-static struct sr_dev_driver *di = &testo_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 scanopts[] = {
        SR_CONF_CONN,
@@ -37,17 +37,18 @@ static const uint32_t devopts[] = {
        SR_CONF_LIMIT_MSEC | SR_CONF_SET,
 };
 
-unsigned char TESTO_x35_REQUEST[] = { 0x12, 0, 0, 0, 1, 1, 0x55, 0xd1, 0xb7 };
-struct testo_model models[] = {
+static const uint8_t TESTO_x35_REQUEST[] = { 0x12, 0, 0, 0, 1, 1, 0x55, 0xd1, 0xb7 };
+
+static const struct testo_model models[] = {
        { "435", 9, TESTO_x35_REQUEST },
 };
 
-static int init(struct sr_context *sr_ctx)
+static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
 {
        return std_init(sr_ctx, di, LOG_PREFIX);
 }
 
-static GSList *scan(GSList *options)
+static GSList *scan(struct sr_dev_driver *di, GSList *options)
 {
        struct drv_context *drvc;
        struct dev_context *devc;
@@ -63,7 +64,7 @@ static GSList *scan(GSList *options)
        char manufacturer[64], product[64], connection_id[64];
 
        devices = NULL;
-       drvc = di->priv;
+       drvc = di->context;
        drvc->instances = NULL;
 
        conn_devices = NULL;
@@ -91,11 +92,7 @@ static GSList *scan(GSList *options)
                                continue;
                }
 
-               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 ((ret = libusb_open(devlist[i], &hdl)) < 0)
                        continue;
@@ -124,8 +121,10 @@ static GSList *scan(GSList *options)
                if (strcmp(product, "testo 435/635/735"))
                        continue;
 
-               sdi = sr_dev_inst_new(SR_ST_INACTIVE, "Testo",
-                               "435/635/735", NULL);
+               sdi = g_malloc0(sizeof(struct sr_dev_inst));
+               sdi->status = SR_ST_INACTIVE;
+               sdi->vendor = g_strdup("Testo");
+               sdi->model = g_strdup("435/635/735");
                sdi->driver = di;
                sdi->inst_type = SR_INST_USB;
                sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
@@ -147,25 +146,21 @@ static GSList *scan(GSList *options)
        return devices;
 }
 
-static GSList *dev_list(void)
-{
-       return ((struct drv_context *)(di->priv))->instances;
-}
-
-static int dev_clear(void)
+static GSList *dev_list(const struct sr_dev_driver *di)
 {
-       return std_dev_clear(di, NULL);
+       return ((struct drv_context *)(di->context))->instances;
 }
 
 static int dev_open(struct sr_dev_inst *sdi)
 {
-       struct drv_context *drvc = di->priv;
+       struct sr_dev_driver *di = sdi->driver;
+       struct drv_context *drvc = di->context;
        struct sr_usb_dev_inst *usb;
        libusb_device **devlist;
        int ret, i;
        char connection_id[64];
 
-       if (!di->priv) {
+       if (!di->context) {
                sr_err("Driver was not initialized.");
                return SR_ERR;
        }
@@ -209,9 +204,10 @@ 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->priv) {
+       if (!di->context) {
                sr_err("Driver was not initialized.");
                return SR_ERR;
        }
@@ -229,21 +225,6 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(void)
-{
-       int ret;
-       struct drv_context *drvc;
-
-       if (!(drvc = di->priv))
-               return SR_OK;
-
-       ret = dev_clear();
-       g_free(drvc);
-       di->priv = NULL;
-
-       return ret;
-}
-
 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
@@ -270,6 +251,7 @@ 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;
        int ret;
@@ -279,7 +261,7 @@ 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->priv) {
+       if (!di->context) {
                sr_err("Driver was not initialized.");
                return SR_ERR;
        }
@@ -291,13 +273,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                devc->limit_msec = g_variant_get_uint64(data);
                now = g_get_monotonic_time() / 1000;
                devc->end_time = now + devc->limit_msec;
-               sr_dbg("Setting time limit to %" PRIu64 "ms.",
-                      devc->limit_msec);
                break;
        case SR_CONF_LIMIT_SAMPLES:
                devc->limit_samples = g_variant_get_uint64(data);
-               sr_dbg("Setting sample limit to %" PRIu64 ".",
-                      devc->limit_samples);
                break;
        default:
                ret = SR_ERR_NA;
@@ -366,13 +344,13 @@ static void receive_data(struct sr_dev_inst *sdi, unsigned char *data, int len)
 
        devc->reply_size = 0;
        if (devc->limit_samples && devc->num_samples >= devc->limit_samples)
-               dev_acquisition_stop(sdi, devc->cb_data);
+               dev_acquisition_stop(sdi);
        else
                testo_request_packet(sdi);
 
 }
 
-SR_PRIV void receive_transfer(struct libusb_transfer *transfer)
+SR_PRIV void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
 {
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
@@ -386,7 +364,7 @@ SR_PRIV void receive_transfer(struct libusb_transfer *transfer)
 
        if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE) {
                /* USB device was unplugged. */
-               dev_acquisition_stop(sdi, devc->cb_data);
+               dev_acquisition_stop(sdi);
        } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
                /* First two bytes in any transfer are FTDI status bytes. */
                if (transfer->actual_length > 2)
@@ -401,7 +379,7 @@ SR_PRIV void receive_transfer(struct libusb_transfer *transfer)
                               libusb_error_name(ret));
                        g_free(transfer->buffer);
                        libusb_free_transfer(transfer);
-                       dev_acquisition_stop(sdi, devc->cb_data);
+                       dev_acquisition_stop(sdi);
                }
        } else {
                /* This was the last transfer we're going to receive, so
@@ -413,9 +391,9 @@ SR_PRIV void receive_transfer(struct libusb_transfer *transfer)
 
 static int handle_events(int fd, int revents, void *cb_data)
 {
+       struct sr_dev_driver *di;
        struct dev_context *devc;
-       struct drv_context *drvc = di->priv;
-       struct sr_datafeed_packet packet;
+       struct drv_context *drvc;
        struct sr_dev_inst *sdi;
        struct timeval tv;
        gint64 now;
@@ -425,20 +403,19 @@ static int handle_events(int fd, int revents, void *cb_data)
 
        sdi = cb_data;
        devc = sdi->priv;
+       di = sdi->driver;
+       drvc = di->context;
 
        if (devc->limit_msec) {
                now = g_get_monotonic_time() / 1000;
                if (now > devc->end_time)
-                       dev_acquisition_stop(sdi, devc->cb_data);
+                       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(sdi, &packet);
+               std_session_send_df_end(sdi, LOG_PREFIX);
        }
 
        memset(&tv, 0, sizeof(struct timeval));
@@ -448,8 +425,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 drv_context *drvc;
        struct dev_context *devc;
        struct sr_usb_dev_inst *usb;
@@ -457,24 +435,22 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        int ret;
        unsigned char *buf;
 
-       drvc = di->priv;
+       drvc = di->context;
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
-       if (!di->priv) {
+       if (!di->context) {
                sr_err("Driver was not initialized.");
                return SR_ERR;
        }
 
        devc = sdi->priv;
        usb = sdi->conn;
-       devc->cb_data = cb_data;
        devc->end_time = 0;
        devc->num_samples = 0;
        devc->reply_size = 0;
 
-       /* 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);
@@ -486,7 +462,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        if (testo_request_packet(sdi) != SR_OK)
                return SR_ERR;
 
-       buf = g_try_malloc(MAX_REPLY_SIZE);
+       buf = g_malloc(MAX_REPLY_SIZE);
        transfer = libusb_alloc_transfer(0);
        libusb_fill_bulk_transfer(transfer, usb->devhdl, EP_IN, buf,
                        MAX_REPLY_SIZE, receive_transfer, (void *)sdi, 100);
@@ -501,11 +477,11 @@ 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)
 {
-       (void)cb_data;
+       struct sr_dev_driver *di = sdi->driver;
 
-       if (!di->priv) {
+       if (!di->context) {
                sr_err("Driver was not initialized.");
                return SR_ERR;
        }
@@ -523,10 +499,9 @@ SR_PRIV struct sr_dev_driver testo_driver_info = {
        .longname = "Testo",
        .api_version = 1,
        .init = init,
-       .cleanup = cleanup,
+       .cleanup = std_cleanup,
        .scan = scan,
        .dev_list = dev_list,
-       .dev_clear = dev_clear,
        .config_get = config_get,
        .config_set = config_set,
        .config_list = config_list,
@@ -534,5 +509,5 @@ SR_PRIV struct sr_dev_driver testo_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };