]> sigrok.org Git - libsigrok.git/blobdiff - hardware/rigol-ds1xx2/api.c
Don't accept over 64 probes.
[libsigrok.git] / hardware / rigol-ds1xx2 / api.c
index ce5d57c57ac1db34cccd30e6de0c2465f56f4381..d6f055e64549662c01fa4894a3f84d244c1711b0 100644 (file)
@@ -40,10 +40,13 @@ static const int32_t hwcaps[] = {
        SR_CONF_TRIGGER_SOURCE,
        SR_CONF_TRIGGER_SLOPE,
        SR_CONF_HORIZ_TRIGGERPOS,
-       SR_CONF_VDIV,
-       SR_CONF_COUPLING,
        SR_CONF_NUM_TIMEBASE,
+};
+
+static const int32_t analog_hwcaps[] = {
        SR_CONF_NUM_VDIV,
+       SR_CONF_VDIV,
+       SR_CONF_COUPLING,
 };
 
 static const uint64_t timebases[][2] = {
@@ -146,35 +149,14 @@ static void clear_helper(void *priv)
 {
        struct dev_context *devc;
 
-       for (l = drvc->instances; l; l = l->next) {
-               if (!(sdi = l->data))
-                       continue;
-
-               if (sdi->conn)
-                       sr_serial_dev_inst_free(sdi->conn);
-
-               g_slist_free(sdi->probe_groups);
-
-               if (!(devc = sdi->priv))
-                       continue;
-
-               g_free(devc->device);
-               g_free(devc->coupling[0]);
-               g_free(devc->coupling[1]);
-               g_free(devc->trigger_source);
-               g_free(devc->trigger_slope);
-               g_slist_free(devc->analog_groups[0].probes);
-               g_slist_free(devc->analog_groups[1].probes);
-               g_slist_free(devc->digital_group.probes);
-               close(devc->fd);
-
-               sr_dev_inst_free(sdi);
-       }
-
+       devc = priv;
        g_free(devc->coupling[0]);
        g_free(devc->coupling[1]);
        g_free(devc->trigger_source);
        g_free(devc->trigger_slope);
+       g_slist_free(devc->analog_groups[0].probes);
+       g_slist_free(devc->analog_groups[1].probes);
+       g_slist_free(devc->digital_group.probes);
 }
 
 static int dev_clear(void)
@@ -405,17 +387,37 @@ static int cleanup(void)
 static int config_get(int id, GVariant **data, const struct sr_dev_inst *sdi,
                const struct sr_probe_group *probe_group)
 {
+       struct dev_context *devc;
+       unsigned int i;
 
-       (void)sdi;
-       (void)probe_group;
+       if (!sdi || !(devc = sdi->priv))
+               return SR_ERR_ARG;
+
+       /* If a probe group is specified, it must be a valid one. */
+       if (probe_group) {
+               if (probe_group != &devc->analog_groups[0]
+                               && probe_group != &devc->analog_groups[1]) {
+                       sr_err("Invalid probe group specified.");
+                       return SR_ERR;
+               }
+       }
 
        switch (id) {
        case SR_CONF_NUM_TIMEBASE:
                *data = g_variant_new_int32(NUM_TIMEBASE);
                break;
        case SR_CONF_NUM_VDIV:
-               *data = g_variant_new_int32(NUM_VDIV);
-               break;
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
+               for (i = 0; i < 2; i++) {
+                       if (probe_group == &devc->analog_groups[i]) {
+                               *data = g_variant_new_int32(NUM_VDIV);
+                               return SR_OK;
+                       }
+               }
+               return SR_ERR_NA;
        default:
                return SR_ERR_NA;
        }
@@ -429,17 +431,25 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
        struct dev_context *devc;
        uint64_t tmp_u64, p, q;
        double t_dbl;
-       unsigned int i;
+       unsigned int i, j;
        int ret;
        const char *tmp_str;
 
-       (void)probe_group;
-
-       devc = sdi->priv;
+       if (!(devc = sdi->priv))
+               return SR_ERR_ARG;
 
        if (sdi->status != SR_ST_ACTIVE)
                return SR_ERR_DEV_CLOSED;
 
+       /* If a probe group is specified, it must be a valid one. */
+       if (probe_group) {
+               if (probe_group != &devc->analog_groups[0]
+                               && probe_group != &devc->analog_groups[1]) {
+                       sr_err("Invalid probe group specified.");
+                       return SR_ERR;
+               }
+       }
+
        ret = SR_OK;
        switch (id) {
        case SR_CONF_LIMIT_FRAMES:
@@ -497,35 +507,44 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
                        ret = SR_ERR_ARG;
                break;
        case SR_CONF_VDIV:
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                g_variant_get(data, "(tt)", &p, &q);
-               for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
-                       if (vdivs[i][0] != p || vdivs[i][1] != q)
-                               continue;
-                       devc->vdiv[0] = devc->vdiv[1] = (float)p / q;
-                       set_cfg(sdi, ":CHAN1:SCAL %.3f", devc->vdiv[0]);
-                       ret = set_cfg(sdi, ":CHAN2:SCAL %.3f", devc->vdiv[1]);
-                       break;
+               for (i = 0; i < 2; i++) {
+                       if (probe_group == &devc->analog_groups[i]) {
+                               for (j = 0; j < ARRAY_SIZE(vdivs); j++) {
+                                       if (vdivs[j][0] != p || vdivs[j][1] != q)
+                                               continue;
+                                       devc->vdiv[i] = (float)p / q;
+                                       return set_cfg(sdi, ":CHAN%d:SCAL %.3f", i + 1,
+                                                       devc->vdiv[i]);
+                               }
+                               return SR_ERR_ARG;
+                       }
                }
-               if (i == ARRAY_SIZE(vdivs))
-                       ret = SR_ERR_ARG;
-               break;
+               return SR_ERR_NA;
        case SR_CONF_COUPLING:
-               /* TODO: Not supporting coupling per channel yet. */
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                tmp_str = g_variant_get_string(data, NULL);
-               for (i = 0; i < ARRAY_SIZE(coupling); i++) {
-                       if (!strcmp(tmp_str, coupling[i])) {
-                               g_free(devc->coupling[0]);
-                               g_free(devc->coupling[1]);
-                               devc->coupling[0] = g_strdup(coupling[i]);
-                               devc->coupling[1] = g_strdup(coupling[i]);
-                               set_cfg(sdi, ":CHAN1:COUP %s", devc->coupling[0]);
-                               ret = set_cfg(sdi, ":CHAN2:COUP %s", devc->coupling[1]);
-                               break;
+               for (i = 0; i < 2; i++) {
+                       if (probe_group == &devc->analog_groups[i]) {
+                               for (j = 0; j < ARRAY_SIZE(coupling); j++) {
+                                       if (!strcmp(tmp_str, coupling[j])) {
+                                               g_free(devc->coupling[i]);
+                                               devc->coupling[i] = g_strdup(coupling[j]);
+                                               return set_cfg(sdi, ":CHAN%d:COUP %s", i + 1,
+                                                               devc->coupling[i]);
+                                       }
+                               }
+                               return SR_ERR_ARG;
                        }
                }
-               if (i == ARRAY_SIZE(coupling))
-                       ret = SR_ERR_ARG;
-               break;
+               return SR_ERR_NA;
        default:
                ret = SR_ERR_NA;
                break;
@@ -542,21 +561,63 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        unsigned int i;
        struct dev_context *devc;
 
-       (void)probe_group;
-
-       switch (key) {
-       case SR_CONF_SCAN_OPTIONS:
+       if (key == SR_CONF_SCAN_OPTIONS) {
                *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
                                hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
+               return SR_OK;
+       } else if (key == SR_CONF_DEVICE_OPTIONS && probe_group == NULL) {
+               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                       hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+               return SR_OK;
+       }
+
+       /* Every other option requires a valid device instance. */
+       if (!sdi || !(devc = sdi->priv))
+               return SR_ERR_ARG;
+
+       /* If a probe group is specified, it must be a valid one. */
+       if (probe_group) {
+               if (probe_group != &devc->analog_groups[0]
+                               && probe_group != &devc->analog_groups[1]) {
+                       sr_err("Invalid probe group specified.");
+                       return SR_ERR;
+               }
+       }
+
+       switch (key) {
                break;
        case SR_CONF_DEVICE_OPTIONS:
-               *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
+               if (probe_group == &devc->digital_group) {
+                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                               NULL, 0, sizeof(int32_t));
+                       return SR_OK;
+               } else {
+                       for (i = 0; i < 2; i++) {
+                               if (probe_group == &devc->analog_groups[i]) {
+                                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
+                                               analog_hwcaps, ARRAY_SIZE(analog_hwcaps), sizeof(int32_t));
+                                       return SR_OK;
+                               }
+                       }
+                       return SR_ERR_NA;
+               }
                break;
        case SR_CONF_COUPLING:
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
                break;
        case SR_CONF_VDIV:
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
+               }
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
                for (i = 0; i < ARRAY_SIZE(vdivs); i++) {
                        rational[0] = g_variant_new_uint64(vdivs[i][0]);
@@ -577,10 +638,6 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                *data = g_variant_builder_end(&gvb);
                break;
        case SR_CONF_TRIGGER_SOURCE:
-               if (!sdi || !sdi->priv)
-                       /* Can't know this until we have the exact model. */
-                       return SR_ERR_ARG;
-               devc = sdi->priv;
                *data = g_variant_new_strv(trigger_sources,
                                devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
                break;