]> sigrok.org Git - libsigrok.git/blobdiff - src/hardware/sysclk-lwla/api.c
drivers: Factor out std_*_idx*().
[libsigrok.git] / src / hardware / sysclk-lwla / api.c
index 127324f2d1489500dcfc0c2e427f09f9fb664e7a..6055b83d4da6b6a46a65218c82918e7e75c6df43 100644 (file)
@@ -41,12 +41,12 @@ static const int32_t trigger_matches[] = {
        SR_TRIGGER_FALLING,
 };
 
-static const char *const trigger_source_names[] = {
+static const char *trigger_source_names[] = {
        [TRIGGER_CHANNELS] = "CH",
        [TRIGGER_EXT_TRG] = "TRG",
 };
 
-static const char *const signal_edge_names[] = {
+static const char *signal_edge_names[] = {
        [EDGE_POSITIVE] = "r",
        [EDGE_NEGATIVE] = "f",
 };
@@ -67,7 +67,7 @@ static struct sr_dev_inst *dev_inst_new(const struct model_info *model)
 
        sdi = g_malloc0(sizeof(struct sr_dev_inst));
        sdi->status = SR_ST_INACTIVE;
-       sdi->vendor = g_strdup(VENDOR_NAME);
+       sdi->vendor = g_strdup("SysClk");
        sdi->model = g_strdup(model->name);
        sdi->priv = devc;
 
@@ -323,8 +323,8 @@ static int has_devopt(const struct model_info *model, uint32_t key)
        return FALSE;
 }
 
-static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
-                     const struct sr_channel_group *cg)
+static int config_get(uint32_t key, GVariant **data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;
        unsigned int idx;
@@ -382,29 +382,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s
        return SR_OK;
 }
 
-/* Helper for mapping a string-typed configuration value to an index
- * within a table of possible values.
- */
-static int lookup_index(GVariant *value, const char *const *table, int len)
-{
-       const char *entry;
-       int i;
-
-       entry = g_variant_get_string(value, NULL);
-       if (!entry)
-               return -1;
-
-       /* Linear search is fine for very small tables. */
-       for (i = 0; i < len; i++) {
-               if (strcmp(entry, table[i]) == 0)
-                       return i;
-       }
-
-       return -1;
-}
-
-static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
-                     const struct sr_channel_group *cg)
+static int config_set(uint32_t key, GVariant *data,
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        uint64_t value;
        struct dev_context *devc;
@@ -448,20 +427,17 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
                        ? CLOCK_EXT_CLK : CLOCK_INTERNAL;
                break;
        case SR_CONF_CLOCK_EDGE:
-               idx = lookup_index(data, ARRAY_AND_SIZE(signal_edge_names));
-               if (idx < 0)
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edge_names))) < 0)
                        return SR_ERR_ARG;
                devc->cfg_clock_edge = idx;
                break;
        case SR_CONF_TRIGGER_SOURCE:
-               idx = lookup_index(data, ARRAY_AND_SIZE(trigger_source_names));
-               if (idx < 0)
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_source_names))) < 0)
                        return SR_ERR_ARG;
                devc->cfg_trigger_source = idx;
                break;
        case SR_CONF_TRIGGER_SLOPE:
-               idx = lookup_index(data, ARRAY_AND_SIZE(signal_edge_names));
-               if (idx < 0)
+               if ((idx = std_str_idx(data, ARRAY_AND_SIZE(signal_edge_names))) < 0)
                        return SR_ERR_ARG;
                devc->cfg_trigger_slope = idx;
                break;
@@ -474,7 +450,7 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd
 }
 
 static int config_channel_set(const struct sr_dev_inst *sdi,
-                             struct sr_channel *ch, unsigned int changes)
+       struct sr_channel *ch, unsigned int changes)
 {
        uint64_t channel_bit;
        struct dev_context *devc;
@@ -593,8 +569,7 @@ static int config_commit(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)
+       const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
 {
        struct dev_context *devc;