]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/victor-dmm/api.c
Drop unneeded std_session_send_df_header() comments.
[libsigrok.git] / src / hardware / victor-dmm / api.c
index 56c53e503c90d98ab60338e56198071c8bd4f789..f94352565c73d59bb41cb15ba55003f5e41cdd2a 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
 #include <glib.h>
 #include <libusb.h>
 #include <stdlib.h>
 #include <string.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
 #define VICTOR_PID 0xd237
 #define VICTOR_VENDOR "Victor"
 #define VICTOR_INTERFACE 0
-#define VICTOR_ENDPOINT LIBUSB_ENDPOINT_IN | 1
+#define VICTOR_ENDPOINT (LIBUSB_ENDPOINT_IN | 1)
 
 SR_PRIV struct sr_dev_driver victor_dmm_driver_info;
-static struct sr_dev_driver *di = &victor_dmm_driver_info;
 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
 
-static const int32_t hwopts[] = {
+static const uint32_t drvopts[] = {
+       SR_CONF_MULTIMETER,
+};
+
+static const uint32_t scanopts[] = {
        SR_CONF_CONN,
 };
 
-static const int32_t hwcaps[] = {
-       SR_CONF_MULTIMETER,
-       SR_CONF_LIMIT_MSEC,
-       SR_CONF_LIMIT_SAMPLES,
+static const uint32_t devopts[] = {
        SR_CONF_CONTINUOUS,
+       SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
+       SR_CONF_LIMIT_MSEC | SR_CONF_SET,
+       SR_CONF_CONN | SR_CONF_GET,
 };
 
-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;
        struct sr_dev_inst *sdi;
-       struct sr_channel *ch;
        struct libusb_device_descriptor des;
        libusb_device **devlist;
        GSList *devices;
-       int ret, devcnt, i;
+       int i;
+       char connection_id[64];
 
        (void)options;
 
-       drvc = di->priv;
+       drvc = di->context;
 
        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;
 
-               devcnt = g_slist_length(drvc->instances);
-               if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
-                               VICTOR_VENDOR, NULL, NULL)))
-                       return NULL;
-               sdi->driver = di;
+               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
 
-               if (!(devc = g_try_malloc0(sizeof(struct dev_context))))
-                       return NULL;
+               sdi = g_malloc0(sizeof(struct sr_dev_inst));
+               sdi->status = SR_ST_INACTIVE;
+               sdi->vendor = g_strdup(VICTOR_VENDOR);
+               sdi->driver = di;
+               sdi->connection_id = g_strdup(connection_id);
+               devc = g_malloc0(sizeof(struct dev_context));
                sdi->priv = devc;
 
-               if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
-                       return NULL;
-               sdi->channels = g_slist_append(NULL, ch);
-
-               if (!(sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
-                               libusb_get_device_address(devlist[i]), NULL)))
-                       return NULL;
+               sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1");
+               sdi->conn = sr_usb_dev_inst_new(libusb_get_bus_number(devlist[i]),
+                               libusb_get_device_address(devlist[i]), NULL);
                sdi->inst_type = SR_INST_USB;
 
                drvc->instances = g_slist_append(drvc->instances, sdi);
@@ -105,19 +101,21 @@ static GSList *scan(GSList *options)
        return devices;
 }
 
-static GSList *dev_list(void)
+static GSList *dev_list(const struct sr_dev_driver *di)
 {
-       return ((struct drv_context *)(di->priv))->instances;
+       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;
        }
@@ -126,8 +124,8 @@ static int dev_open(struct sr_dev_inst *sdi)
 
        libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
        for (i = 0; devlist[i]; i++) {
-               if (libusb_get_bus_number(devlist[i]) != usb->bus
-                               || libusb_get_device_address(devlist[i]) != usb->address)
+               usb_get_port_path(devlist[i], connection_id, sizeof(connection_id));
+               if (strcmp(sdi->connection_id, connection_id))
                        continue;
                if ((ret = libusb_open(devlist[i], &usb->devhdl))) {
                        sr_err("Failed to open device: %s.", libusb_error_name(ret));
@@ -163,9 +161,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;
        }
@@ -184,24 +183,22 @@ static int dev_close(struct sr_dev_inst *sdi)
        return SR_OK;
 }
 
-static int cleanup(void)
+static int cleanup(const struct sr_dev_driver *di)
 {
        int ret;
        struct drv_context *drvc;
 
-       if (!(drvc = di->priv))
+       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);
-       di->priv = NULL;
 
        return ret;
 }
 
-static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
+static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
        struct sr_usb_dev_inst *usb;
@@ -209,7 +206,7 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
 
        (void)cg;
 
-       switch (id) {
+       switch (key) {
        case SR_CONF_CONN:
                if (!sdi || !sdi->conn)
                        return SR_ERR_ARG;
@@ -224,46 +221,42 @@ static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
+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;
 
        (void)cg;
 
        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;
-       ret = SR_OK;
-       switch (id) {
+
+       switch (key) {
        case SR_CONF_LIMIT_MSEC:
                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;
+               return SR_ERR_NA;
        }
 
-       return ret;
+       return SR_OK;
 }
 
-static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
+static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_channel_group *cg)
 {
        (void)sdi;
@@ -271,12 +264,16 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
 
        switch (key) {
        case SR_CONF_SCAN_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                               scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
                break;
        case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+               if (!sdi)
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                       drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
+               else
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
+                                       devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
                break;
        default:
                return SR_ERR_NA;
@@ -285,7 +282,7 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        return SR_OK;
 }
 
-static void receive_transfer(struct libusb_transfer *transfer)
+static void LIBUSB_CALL receive_transfer(struct libusb_transfer *transfer)
 {
        struct dev_context *devc;
        struct sr_dev_inst *sdi;
@@ -329,9 +326,9 @@ static void receive_transfer(struct libusb_transfer *transfer)
 static int handle_events(int fd, int revents, void *cb_data)
 {
        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 sr_dev_driver *di;
        struct timeval tv;
        gint64 now;
 
@@ -340,6 +337,8 @@ 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;
@@ -349,11 +348,8 @@ static int handle_events(int fd, int revents, void *cb_data)
 
        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));
@@ -365,8 +361,9 @@ static int handle_events(int fd, int revents, void *cb_data)
 
 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
 {
+       struct sr_dev_driver *di = sdi->driver;
        struct dev_context *devc;
-       struct drv_context *drvc = di->priv;
+       struct drv_context *drvc = di->context;
        struct sr_usb_dev_inst *usb;
        struct libusb_transfer *transfer;
        int ret;
@@ -375,7 +372,7 @@ 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->priv) {
+       if (!di->context) {
                sr_err("Driver was not initialized.");
                return SR_ERR;
        }
@@ -384,13 +381,12 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
        usb = sdi->conn;
        devc->cb_data = cb_data;
 
-       /* Send header packet to the session bus. */
        std_session_send_df_header(cb_data, LOG_PREFIX);
 
        usb_source_add(sdi->session, drvc->sr_ctx, 100,
                        handle_events, (void *)sdi);
 
-       buf = g_try_malloc(DMM_DATA_SIZE);
+       buf = g_malloc(DMM_DATA_SIZE);
        transfer = libusb_alloc_transfer(0);
        /* Each transfer request gets 100ms to arrive before it's restarted.
         * The device only sends 1 transfer/second no matter how many
@@ -411,9 +407,10 @@ static int dev_acquisition_start(const 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;
        (void)cb_data;
 
-       if (!di->priv) {
+       if (!di->context) {
                sr_err("Driver was not initialized.");
                return SR_ERR;
        }
@@ -444,5 +441,5 @@ SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
        .dev_close = dev_close,
        .dev_acquisition_start = dev_acquisition_start,
        .dev_acquisition_stop = dev_acquisition_stop,
-       .priv = NULL,
+       .context = NULL,
 };