]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds1xx2/api.c
Add and use std_session_send_df_header().
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
index 64f8c2220d42c7394bed2ed1fc6f7dd507095a63..2fd375fbbdba9702abd489743469d8790d492452 100644 (file)
 #include "libsigrok-internal.h"
 #include "protocol.h"
 
-#define NUM_PROBES 2
-
 static const int hwcaps[] = {
-       SR_HWCAP_OSCILLOSCOPE,
-       SR_HWCAP_LIMIT_SAMPLES,
-       SR_HWCAP_TIMEBASE,
-       SR_HWCAP_TRIGGER_SOURCE,
-       SR_HWCAP_TRIGGER_SLOPE,
-       SR_HWCAP_HORIZ_TRIGGERPOS,
-       SR_HWCAP_VDIV,
-       SR_HWCAP_COUPLING,
+       SR_CONF_OSCILLOSCOPE,
+       SR_CONF_LIMIT_SAMPLES,
+       SR_CONF_TIMEBASE,
+       SR_CONF_TRIGGER_SOURCE,
+       SR_CONF_TRIGGER_SLOPE,
+       SR_CONF_HORIZ_TRIGGERPOS,
+       SR_CONF_VDIV,
+       SR_CONF_COUPLING,
        0,
 };
 
@@ -140,6 +138,7 @@ static int clear_instances(void)
                        continue;
 
                g_free(devc->device);
+               g_slist_free(devc->enabled_probes);
                close(devc->fd);
 
                sr_dev_inst_free(sdi);
@@ -153,17 +152,7 @@ static int clear_instances(void)
 
 static int hw_init(struct sr_context *sr_ctx)
 {
-       struct drv_context *drvc;
-       (void)sr_ctx;
-
-       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)
@@ -189,10 +178,11 @@ static GSList *hw_scan(GSList *options)
 
        (void)options;
 
-       devices = NULL;
        drvc = di->priv;
        drvc->instances = NULL;
 
+       devices = NULL;
+
        dir = g_dir_open("/sys/class/usb/", 0, NULL);
 
        if (dir == NULL)
@@ -283,11 +273,7 @@ static GSList *hw_scan(GSList *options)
 
 static GSList *hw_dev_list(void)
 {
-       struct drv_context *drvc;
-
-       drvc = di->priv;
-
-       return drvc->instances;
+       return ((struct drv_context *)(di->priv))->instances;
 }
 
 static int hw_dev_open(struct sr_dev_inst *sdi)
@@ -325,36 +311,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_TIMEBASES:
-               *data = timebases;
-               break;
-       case SR_DI_TRIGGER_SOURCES:
-               *data = trigger_sources;
-               break;
-       case SR_DI_VDIVS:
-               *data = vdivs;
-               break;
-       case SR_DI_COUPLING:
-               *data = coupling;
-               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;
        uint64_t tmp_u64;
@@ -371,25 +328,25 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
        }
 
        ret = SR_OK;
-       switch (hwcap) {
-       case SR_HWCAP_LIMIT_FRAMES:
+       switch (id) {
+       case SR_CONF_LIMIT_FRAMES:
                devc->limit_frames = *(const uint64_t *)value;
                break;
-       case SR_HWCAP_TRIGGER_SLOPE:
+       case SR_CONF_TRIGGER_SLOPE:
                tmp_u64 = *(const int *)value;
                rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SLOP %s\n",
                                       tmp_u64 ? "POS" : "NEG");
                break;
-       case SR_HWCAP_HORIZ_TRIGGERPOS:
+       case SR_CONF_HORIZ_TRIGGERPOS:
                tmp_float = *(const float *)value;
                rigol_ds1xx2_send_data(devc->fd, ":TIM:OFFS %.9f\n", tmp_float);
                break;
-       case SR_HWCAP_TIMEBASE:
+       case SR_CONF_TIMEBASE:
                tmp_rat = *(const struct sr_rational *)value;
                rigol_ds1xx2_send_data(devc->fd, ":TIM:SCAL %.9f\n",
                                       (float)tmp_rat.p / tmp_rat.q);
                break;
-       case SR_HWCAP_TRIGGER_SOURCE:
+       case SR_CONF_TRIGGER_SOURCE:
                if (!strcmp(value, "CH1"))
                        channel = "CHAN1";
                else if (!strcmp(value, "CH2"))
@@ -404,7 +361,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
                }
                rigol_ds1xx2_send_data(devc->fd, ":TRIG:EDGE:SOUR %s\n", channel);
                break;
-       case SR_HWCAP_VDIV:
+       case SR_CONF_VDIV:
                /* TODO: Not supporting vdiv per channel yet. */
                tmp_rat = *(const struct sr_rational *)value;
                for (i = 0; vdivs[i].p && vdivs[i].q; i++) {
@@ -420,7 +377,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
                if (vdivs[i].p == 0 && vdivs[i].q == 0)
                        ret = SR_ERR_ARG;
                break;
-       case SR_HWCAP_COUPLING:
+       case SR_CONF_COUPLING:
                /* TODO: Not supporting coupling per channel yet. */
                for (i = 0; coupling[i]; i++) {
                        if (!strcmp(value, coupling[i])) {
@@ -434,7 +391,7 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
                        ret = SR_ERR_ARG;
                break;
        default:
-               sr_err("Unknown hardware capability: %d.", hwcap);
+               sr_err("Unknown hardware capability: %d.", id);
                ret = SR_ERR_ARG;
                break;
        }
@@ -442,13 +399,38 @@ static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
        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;
+       case SR_CONF_COUPLING:
+               *data = coupling;
+               break;
+       case SR_CONF_VDIV:
+               *data = vdivs;
+               break;
+       case SR_CONF_TIMEBASE:
+               *data = timebases;
+               break;
+       case SR_CONF_TRIGGER_SOURCE:
+               *data = trigger_sources;
+               break;
+       default:
+               return SR_ERR_ARG;
+       }
+
+       return SR_OK;
+}
+
 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
                                    void *cb_data)
 {
        struct dev_context *devc;
-       struct sr_datafeed_packet packet;
-       struct sr_datafeed_header header;
-       struct sr_datafeed_meta_analog meta;
        char buf[256];
        int len;
 
@@ -461,18 +443,10 @@ static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
        sr_source_add(devc->fd, G_IO_IN, 50, rigol_ds1xx2_receive_data, (void *)sdi);
 
        /* Send header packet to the session bus. */
-       packet.type = SR_DF_HEADER;
-       packet.payload = (unsigned char *)&header;
-       header.feed_version = 1;
-       gettimeofday(&header.starttime, NULL);
-       sr_session_send(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 = NUM_PROBES;
-       sr_session_send(cb_data, &packet);
+       std_session_send_df_header(cb_data, DRIVER_LOG_DOMAIN);
 
+       /* Hardcoded to CH1 only. */
+       devc->enabled_probes = g_slist_append(NULL, sdi->probes->data);
        rigol_ds1xx2_send_data(devc->fd, ":CHAN1:SCAL?\n");
        len = read(devc->fd, buf, sizeof(buf));
        buf[len] = 0;
@@ -515,10 +489,10 @@ SR_PRIV struct sr_dev_driver rigol_ds1xx2_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,