]> 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 bde477cf312880287c9ffb0befcb180996ddd217..d6f055e64549662c01fa4894a3f84d244c1711b0 100644 (file)
@@ -46,7 +46,7 @@ static const int32_t hwcaps[] = {
 static const int32_t analog_hwcaps[] = {
        SR_CONF_NUM_VDIV,
        SR_CONF_VDIV,
-       SR_CONF_COUPLING
+       SR_CONF_COUPLING,
 };
 
 static const uint64_t timebases[][2] = {
@@ -149,33 +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->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);
-
-               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)
@@ -406,17 +387,32 @@ 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 = sdi->priv;
+       struct dev_context *devc;
        unsigned int i;
 
+       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:
+               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])
-                       {
+                       if (probe_group == &devc->analog_groups[i]) {
                                *data = g_variant_new_int32(NUM_VDIV);
                                return SR_OK;
                        }
@@ -439,11 +435,21 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
        int ret;
        const char *tmp_str;
 
-       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:
@@ -501,12 +507,14 @@ 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 < 2; i++) {
-                       if (probe_group == &devc->analog_groups[i])
-                       {
-                               for (j = 0; j < ARRAY_SIZE(vdivs); j++)
-                               {
+                       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;
@@ -518,14 +526,15 @@ static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
                }
                return SR_ERR_NA;
        case SR_CONF_COUPLING:
+               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 < 2; i++) {
-                       if (probe_group == &devc->analog_groups[i])
-                       {
-                               for (j = 0; j < ARRAY_SIZE(coupling); j++)
-                               {
-                                       if (!strcmp(tmp_str, coupling[j]))
-                                       {
+                       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,
@@ -550,19 +559,39 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
        GVariant *tuple, *rational[2];
        GVariantBuilder gvb;
        unsigned int i;
-       struct dev_context *devc = sdi->priv;
+       struct dev_context *devc;
 
-       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:
-               if (probe_group == NULL) {
-                       *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
-                               hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
-                       return SR_OK;
-               } else if (probe_group == &devc->digital_group) {
+               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;
@@ -576,24 +605,28 @@ static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
                        }
                        return SR_ERR_NA;
                }
+               break;
        case SR_CONF_COUPLING:
-               for (i = 0; i < 2; i++) {
-                       if (probe_group == &devc->analog_groups[i]) {
-                               *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
-                               return SR_OK;
-                       }
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
                }
-               return SR_ERR_NA;
+               *data = g_variant_new_strv(coupling, ARRAY_SIZE(coupling));
+               break;
        case SR_CONF_VDIV:
-               for (i = 0; i < 2; i++) {
-                       if (probe_group == &devc->analog_groups[i]) {
-                               rational[0] = g_variant_new_uint64(vdivs[i][0]);
-                               rational[1] = g_variant_new_uint64(vdivs[i][1]);
-                               *data = g_variant_new_tuple(rational, 2);
-                               return SR_OK;
-                       }
+               if (!probe_group) {
+                       sr_err("No probe group specified.");
+                       return SR_ERR_PROBE_GROUP;
                }
-               return SR_ERR_NA;
+               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]);
+                       rational[1] = g_variant_new_uint64(vdivs[i][1]);
+                       tuple = g_variant_new_tuple(rational, 2);
+                       g_variant_builder_add_value(&gvb, tuple);
+               }
+               *data = g_variant_builder_end(&gvb);
+               break;
        case SR_CONF_TIMEBASE:
                g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
                for (i = 0; i < ARRAY_SIZE(timebases); i++) {
@@ -605,9 +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;
                *data = g_variant_new_strv(trigger_sources,
                                devc->has_digital ? ARRAY_SIZE(trigger_sources) : 4);
                break;