]> sigrok.org Git - libsigrok.git/blobdiff - hardware/victor-dmm/api.c
Add and use std_session_send_df_header().
[libsigrok.git] / hardware / victor-dmm / api.c
index 9ee2b286145f851163af314a2d47a2091a9800ff..871c5de0e7257c2b977c47e092d22a7f0d926fc8 100644 (file)
  */
 
 #include <glib.h>
-#include "libsigrok.h"
-#include "libsigrok-internal.h"
-#include "protocol.h"
 #include <libusb.h>
 #include <stdlib.h>
 #include <string.h>
+#include "libsigrok.h"
+#include "libsigrok-internal.h"
+#include "protocol.h"
 
 #define VICTOR_VID 0x1244
 #define VICTOR_PID 0xd237
@@ -37,18 +37,13 @@ static int hw_dev_close(struct sr_dev_inst *sdi);
 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data);
 
 static const int hwcaps[] = {
-       SR_HWCAP_MULTIMETER,
-       SR_HWCAP_LIMIT_MSEC,
-       SR_HWCAP_LIMIT_SAMPLES,
-       SR_HWCAP_CONTINUOUS,
+       SR_CONF_MULTIMETER,
+       SR_CONF_LIMIT_MSEC,
+       SR_CONF_LIMIT_SAMPLES,
+       SR_CONF_CONTINUOUS,
        0
 };
 
-static const char *probe_names[] = {
-       "P1",
-};
-
-
 /* Properly close and free all devices. */
 static int clear_instances(void)
 {
@@ -77,17 +72,9 @@ static int clear_instances(void)
        return SR_OK;
 }
 
-static int hw_init(void)
+static int hw_init(struct sr_context *sr_ctx)
 {
-       struct drv_context *drvc;
-
-       if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
-               sr_err("Driver context malloc failed.");
-               return SR_ERR_MALLOC;
-       }
-       di->priv = drvc;
-
-       return SR_OK;
+       return std_hw_init(sr_ctx, di, DRIVER_LOG_DOMAIN);
 }
 
 static GSList *hw_scan(GSList *options)
@@ -103,16 +90,13 @@ static GSList *hw_scan(GSList *options)
 
        (void)options;
 
-       if (!(drvc = di->priv)) {
-               sr_err("Driver was not initialized.");
-               return NULL;
-       }
+       drvc = di->priv;
 
        /* USB scan is always authoritative. */
        clear_instances();
 
        devices = NULL;
-       libusb_get_device_list(NULL, &devlist);
+       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",
@@ -124,8 +108,8 @@ static GSList *hw_scan(GSList *options)
                        continue;
 
                devcnt = g_slist_length(drvc->instances);
-               if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE, VICTOR_VENDOR,
-                               NULL, NULL)))
+               if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_INACTIVE,
+                               VICTOR_VENDOR, NULL, NULL)))
                        return NULL;
                sdi->driver = di;
 
@@ -151,19 +135,13 @@ static GSList *hw_scan(GSList *options)
 
 static GSList *hw_dev_list(void)
 {
-       struct drv_context *drvc;
-
-       if (!(drvc = di->priv)) {
-               sr_err("Driver was not initialized.");
-               return NULL;
-       }
-
-       return drvc->instances;
+       return ((struct drv_context *)(di->priv))->instances;
 }
 
 static int hw_dev_open(struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
+       struct drv_context *drvc = di->priv;
        libusb_device **devlist;
        int ret, i;
 
@@ -173,13 +151,13 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
        }
 
        devc = sdi->priv;
-       libusb_get_device_list(NULL, &devlist);
+       libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
        for (i = 0; devlist[i]; i++) {
                if (libusb_get_bus_number(devlist[i]) != devc->usb->bus
                                || libusb_get_device_address(devlist[i]) != devc->usb->address)
                        continue;
                if ((ret = libusb_open(devlist[i], &devc->usb->devhdl))) {
-                       sr_err("Failed to open device: %s", libusb_error_name(ret));
+                       sr_err("Failed to open device: %s.", libusb_error_name(ret));
                        return SR_ERR;
                }
                break;
@@ -193,15 +171,16 @@ static int hw_dev_open(struct sr_dev_inst *sdi)
        /* The device reports as HID class, so the kernel would have
         * claimed it. */
        if (libusb_kernel_driver_active(devc->usb->devhdl, 0) == 1) {
-               if (libusb_detach_kernel_driver(devc->usb->devhdl, 0) < 0) {
-                       sr_err("Failed to detach kernel driver.");
+               if ((ret = libusb_detach_kernel_driver(devc->usb->devhdl, 0)) < 0) {
+                       sr_err("Failed to detach kernel driver: %s.",
+                              libusb_error_name(ret));
                        return SR_ERR;
                }
        }
 
        if ((ret = libusb_claim_interface(devc->usb->devhdl,
                        VICTOR_INTERFACE))) {
-               sr_err("Failed to claim interface: %s", libusb_error_name(ret));
+               sr_err("Failed to claim interface: %s.", libusb_error_name(ret));
                return SR_ERR;
        }
        sdi->status = SR_ST_ACTIVE;
@@ -246,31 +225,7 @@ static int hw_cleanup(void)
        return SR_OK;
 }
 
-static int hw_info_get(int info_id, const void **data,
-                      const struct sr_dev_inst *sdi)
-{
-
-       (void)sdi;
-
-       switch (info_id) {
-               case SR_DI_HWCAPS:
-                       *data = hwcaps;
-                       break;
-               case SR_DI_NUM_PROBES:
-                       *data = GINT_TO_POINTER(1);
-                       break;
-               case SR_DI_PROBE_NAMES:
-                       *data = probe_names;
-                       break;
-               default:
-                       return SR_ERR_ARG;
-       }
-
-       return SR_OK;
-}
-
-static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
-                            const void *value)
+static int config_set(int id, const void *value, const struct sr_dev_inst *sdi)
 {
        struct dev_context *devc;
        gint64 now;
@@ -288,27 +243,43 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
 
        devc = sdi->priv;
        ret = SR_OK;
-       switch (hwcap) {
-               case SR_HWCAP_LIMIT_MSEC:
-                       devc->limit_msec = *(const int64_t *)value;
-                       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_HWCAP_LIMIT_SAMPLES:
-                       devc->limit_samples = *(const uint64_t *)value;
-                       sr_dbg("setting sample limit to %" PRIu64 ".",
-                                       devc->limit_samples);
-                       break;
+       switch (id) {
+       case SR_CONF_LIMIT_MSEC:
+               devc->limit_msec = *(const int64_t *)value;
+               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 = *(const uint64_t *)value;
+               sr_dbg("Setting sample limit to %" PRIu64 ".",
+                      devc->limit_samples);
+               break;
        default:
-               sr_err("Unknown hardware capability: %d.", hwcap);
+               sr_err("Unknown hardware capability: %d.", id);
                ret = SR_ERR_ARG;
        }
 
        return ret;
 }
 
+static int config_list(int key, const void **data, const struct sr_dev_inst *sdi)
+{
+
+       (void)sdi;
+
+       switch (key) {
+       case SR_CONF_DEVICE_OPTIONS:
+               *data = hwcaps;
+               break;
+       default:
+               return SR_ERR_ARG;
+       }
+
+       return SR_OK;
+}
+
 static void receive_transfer(struct libusb_transfer *transfer)
 {
        struct dev_context *devc;
@@ -321,11 +292,9 @@ static void receive_transfer(struct libusb_transfer *transfer)
                /* USB device was unplugged. */
                hw_dev_acquisition_stop(sdi, sdi);
        } else if (transfer->status == LIBUSB_TRANSFER_COMPLETED) {
-               sr_dbg("got %d-byte packet", transfer->actual_length);
-               if (transfer->actual_length == 14) {
-                       devc->num_samples++;
-                       /* TODO */
-
+               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)
                                        hw_dev_acquisition_stop(sdi, sdi);
@@ -338,23 +307,24 @@ static void receive_transfer(struct libusb_transfer *transfer)
        if (sdi->status == SR_ST_ACTIVE) {
                /* Send the same request again. */
                if ((ret = libusb_submit_transfer(transfer) != 0)) {
-                       sr_err("unable to resubmit transfer: %s", libusb_error_name(ret));
-                       libusb_free_transfer(transfer);
+                       sr_err("Unable to resubmit transfer: %s.",
+                              libusb_error_name(ret));
                        g_free(transfer->buffer);
+                       libusb_free_transfer(transfer);
                        hw_dev_acquisition_stop(sdi, sdi);
                }
        } else {
                /* This was the last transfer we're going to receive, so
                 * clean up now. */
-               libusb_free_transfer(transfer);
                g_free(transfer->buffer);
+               libusb_free_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 sr_dev_inst *sdi;
        struct timeval tv;
@@ -384,7 +354,8 @@ static int handle_events(int fd, int revents, void *cb_data)
        }
 
        memset(&tv, 0, sizeof(struct timeval));
-       libusb_handle_events_timeout_completed(NULL, &tv, NULL);
+       libusb_handle_events_timeout_completed(drvc->sr_ctx->libusb_ctx, &tv,
+                                              NULL);
 
        return TRUE;
 }
@@ -392,10 +363,8 @@ static int handle_events(int fd, int revents, void *cb_data)
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
-       struct sr_datafeed_meta_analog meta;
        struct dev_context *devc;
+       struct drv_context *drvc = di->priv;
        const struct libusb_pollfd **pfd;
        struct libusb_transfer *transfer;
        int ret, i;
@@ -406,25 +375,13 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                return SR_ERR;
        }
 
-       sr_dbg("Starting acquisition.");
-
        devc = sdi->priv;
        devc->cb_data = cb_data;
 
        /* Send header packet to the session bus. */
-       sr_dbg("Sending SR_DF_HEADER.");
-       packet.type = SR_DF_HEADER;
-       packet.payload = (uint8_t *)&header;
-       header.feed_version = 1;
-       sr_session_send(devc->cb_data, &packet);
-
-       /* Send metadata about the SR_DF_ANALOG packets to come. */
-       packet.type = SR_DF_META_ANALOG;
-       packet.payload = &meta;
-       meta.num_probes = 1;
-       sr_session_send(devc->cb_data, &packet);
-
-       pfd = libusb_get_pollfds(NULL);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
+
+       pfd = libusb_get_pollfds(drvc->sr_ctx->libusb_ctx);
        for (i = 0; pfd[i]; i++) {
                /* Handle USB events every 100ms, for decent latency. */
                sr_source_add(pfd[i]->fd, pfd[i]->events, 100,
@@ -434,16 +391,17 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        }
        devc->usbfd[i] = -1;
 
-       buf = g_try_malloc(14);
+       buf = g_try_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
         * times you ask, but we want to keep step with the USB events
         * handling above. */
        libusb_fill_interrupt_transfer(transfer, devc->usb->devhdl,
-                       VICTOR_ENDPOINT, buf, 14, receive_transfer, cb_data, 100);
+                       VICTOR_ENDPOINT, buf, DMM_DATA_SIZE, receive_transfer,
+                       cb_data, 100);
        if ((ret = libusb_submit_transfer(transfer) != 0)) {
-               sr_err("unable to submit transfer: %s", libusb_error_name(ret));
+               sr_err("Unable to submit transfer: %s.", libusb_error_name(ret));
                libusb_free_transfer(transfer);
                g_free(buf);
                return SR_ERR;
@@ -454,7 +412,6 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
 
 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
 {
-
        (void)cb_data;
 
        if (!di->priv) {
@@ -481,10 +438,10 @@ SR_PRIV struct sr_dev_driver victor_dmm_driver_info = {
        .scan = hw_scan,
        .dev_list = hw_dev_list,
        .dev_clear = clear_instances,
+       .config_set = config_set,
+       .config_list = config_list,
        .dev_open = hw_dev_open,
        .dev_close = hw_dev_close,
-       .info_get = hw_info_get,
-       .dev_config_set = hw_dev_config_set,
        .dev_acquisition_start = hw_dev_acquisition_start,
        .dev_acquisition_stop = hw_dev_acquisition_stop,
        .priv = NULL,