]> sigrok.org Git - libsigrok.git/commitdiff
drivers: Use array-based approach in some places.
authorUwe Hermann <redacted>
Tue, 1 Aug 2017 19:12:04 +0000 (21:12 +0200)
committerUwe Hermann <redacted>
Thu, 3 Aug 2017 14:35:19 +0000 (16:35 +0200)
This allows us to use the new array helpers in a few more places.

src/hardware/hameg-hmo/api.c
src/hardware/hameg-hmo/protocol.c
src/hardware/hameg-hmo/protocol.h
src/hardware/lecroy-xstream/api.c
src/hardware/lecroy-xstream/protocol.c
src/hardware/lecroy-xstream/protocol.h
src/hardware/yokogawa-dlm/api.c
src/hardware/yokogawa-dlm/protocol.c
src/hardware/yokogawa-dlm/protocol.h

index 5e6ad66c25f3d23d4a2ef1307d0845899d178ee4..db6c67df4dfd7755bd3bb40d22c99e7edac68924 100644 (file)
@@ -276,7 +276,7 @@ static int config_set(uint32_t key, GVariant *data,
                break;
        case SR_CONF_TRIGGER_SOURCE:
                tmp = g_variant_get_string(data, NULL);
-               for (i = 0; (*model->trigger_sources)[i]; i++) {
+               for (i = 0; i < model->num_trigger_sources; i++) {
                        if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
                                continue;
                        state->trigger_source = i;
@@ -342,7 +342,7 @@ static int config_set(uint32_t key, GVariant *data,
                break;
        case SR_CONF_TRIGGER_SLOPE:
                tmp = g_variant_get_string(data, NULL);
-               for (i = 0; (*model->trigger_slopes)[i]; i++) {
+               for (i = 0; i < model->num_trigger_slopes; i++) {
                        if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
                                continue;
                        state->trigger_slope = i;
@@ -360,7 +360,7 @@ static int config_set(uint32_t key, GVariant *data,
 
                tmp = g_variant_get_string(data, NULL);
 
-               for (i = 0; (*model->coupling_options)[i]; i++) {
+               for (i = 0; i < model->num_coupling_options; i++) {
                        if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
                                continue;
                        for (j = 1; j <= model->analog_channels; j++) {
@@ -430,20 +430,17 @@ static int config_list(uint32_t key, GVariant **data,
        case SR_CONF_COUPLING:
                if (cg_type == CG_NONE)
                        return SR_ERR_CHANNEL_GROUP;
-               *data = g_variant_new_strv(*model->coupling_options,
-                          g_strv_length((char **)*model->coupling_options));
+               *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
                break;
        case SR_CONF_TRIGGER_SOURCE:
                if (!model)
                        return SR_ERR_ARG;
-               *data = g_variant_new_strv(*model->trigger_sources,
-                          g_strv_length((char **)*model->trigger_sources));
+               *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
                break;
        case SR_CONF_TRIGGER_SLOPE:
                if (!model)
                        return SR_ERR_ARG;
-               *data = g_variant_new_strv(*model->trigger_slopes,
-                          g_strv_length((char **)*model->trigger_slopes));
+               *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
                break;
        case SR_CONF_TIMEBASE:
                if (!model)
index fb7fecb271f2fcef0c90f5dc80ded8df221176c3..6d2ad3c413a92466e36ba88297dfbfa5725bf386 100644 (file)
@@ -80,28 +80,24 @@ static const char *coupling_options[] = {
        "DC",  // DC with 50 Ohm termination
        "DCL", // DC with 1 MOhm termination
        "GND",
-       NULL,
 };
 
 static const char *scope_trigger_slopes[] = {
        "POS",
        "NEG",
        "EITH",
-       NULL,
 };
 
 static const char *compact2_trigger_sources[] = {
        "CH1", "CH2",
        "LINE", "EXT", "PATT", "BUS1", "BUS2",
        "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
-       NULL,
 };
 
 static const char *compact4_trigger_sources[] = {
        "CH1", "CH2", "CH3", "CH4",
        "LINE", "EXT", "PATT", "BUS1", "BUS2",
        "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
-       NULL,
 };
 
 static const char *compact4_dig16_trigger_sources[] = {
@@ -109,7 +105,6 @@ static const char *compact4_dig16_trigger_sources[] = {
        "LINE", "EXT", "PATT", "BUS1", "BUS2",
        "D0", "D1", "D2", "D3", "D4", "D5", "D6", "D7",
        "D8", "D9", "D10", "D11", "D12", "D13", "D14", "D15",
-       NULL,
 };
 
 static const uint64_t timebases[][2] = {
@@ -199,8 +194,13 @@ static const struct scope_config scope_models[] = {
                .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &compact2_trigger_sources,
+               .num_trigger_sources = ARRAY_SIZE(compact2_trigger_sources),
+
                .trigger_slopes = &scope_trigger_slopes,
+               .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
 
                .timebases = &timebases,
                .num_timebases = ARRAY_SIZE(timebases),
@@ -229,8 +229,13 @@ static const struct scope_config scope_models[] = {
                .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &compact4_trigger_sources,
+               .num_trigger_sources = ARRAY_SIZE(compact4_trigger_sources),
+
                .trigger_slopes = &scope_trigger_slopes,
+               .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
 
                .timebases = &timebases,
                .num_timebases = ARRAY_SIZE(timebases),
@@ -259,8 +264,13 @@ static const struct scope_config scope_models[] = {
                .num_devopts_cg_analog = ARRAY_SIZE(devopts_cg_analog),
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &compact4_dig16_trigger_sources,
+               .num_trigger_sources = ARRAY_SIZE(compact4_dig16_trigger_sources),
+
                .trigger_slopes = &scope_trigger_slopes,
+               .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
 
                .timebases = &timebases,
                .num_timebases = ARRAY_SIZE(timebases),
@@ -316,7 +326,7 @@ static void scope_state_dump(const struct scope_config *config,
 }
 
 static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
-               const char *command, const char *(*array)[], int *result)
+               const char *command, const char *(*array)[], unsigned int n, int *result)
 {
        char *tmp;
        unsigned int i;
@@ -326,7 +336,7 @@ static int scope_state_get_array_option(struct sr_scpi_dev_inst *scpi,
                return SR_ERR;
        }
 
-       for (i = 0; (*array)[i]; i++) {
+       for (i = 0; i < n; i++) {
                if (!g_strcmp0(tmp, (*array)[i])) {
                        *result = i;
                        g_free(tmp);
@@ -420,6 +430,7 @@ static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
                           i + 1);
 
                if (scope_state_get_array_option(scpi, command, config->coupling_options,
+                                        config->num_coupling_options,
                                         &state->analog_channels[i].coupling) != SR_OK)
                        return SR_ERR;
 
@@ -584,12 +595,14 @@ SR_PRIV int hmo_scope_state_get(struct sr_dev_inst *sdi)
 
        if (scope_state_get_array_option(sdi->conn,
                        (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SOURCE],
-                       config->trigger_sources, &state->trigger_source) != SR_OK)
+                       config->trigger_sources, config->num_trigger_sources,
+                       &state->trigger_source) != SR_OK)
                return SR_ERR;
 
        if (scope_state_get_array_option(sdi->conn,
-               (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
-               config->trigger_slopes, &state->trigger_slope) != SR_OK)
+                       (*config->scpi_dialect)[SCPI_CMD_GET_TRIGGER_SLOPE],
+                       config->trigger_slopes, config->num_trigger_slopes,
+                       &state->trigger_slope) != SR_OK)
                return SR_ERR;
 
        if (hmo_update_sample_rate(sdi) != SR_OK)
index 037b2426fcda6829e92d552c2df06e3865dbbdf9..43220dd789a78c6f2ce10a9981af29a3bb34844c 100644 (file)
@@ -56,6 +56,7 @@ struct scope_config {
        const uint8_t num_trigger_sources;
 
        const char *(*trigger_slopes)[];
+       const uint8_t num_trigger_slopes;
 
        const uint64_t (*timebases)[][2];
        const uint8_t num_timebases;
index 2799f6fbee19c377bb274e7906004e3af2d5ad9b..3a8061d9b55b107f3d1e1c36a09fcb721c7dab1e 100644 (file)
@@ -235,7 +235,7 @@ static int config_set(uint32_t key, GVariant *data,
                break;
        case SR_CONF_TRIGGER_SOURCE:
                tmp = g_variant_get_string(data, NULL);
-               for (i = 0; (*model->trigger_sources)[i]; i++) {
+               for (i = 0; i < model->num_trigger_sources; i++) {
                        if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
                                continue;
                        state->trigger_source = i;
@@ -305,7 +305,7 @@ static int config_set(uint32_t key, GVariant *data,
                break;
        case SR_CONF_TRIGGER_SLOPE:
                tmp = g_variant_get_string(data, NULL);
-               for (i = 0; (*model->trigger_slopes)[i]; i++) {
+               for (i = 0; i < model->num_trigger_slopes; i++) {
                        if (g_strcmp0(tmp, (*model->trigger_slopes)[i]) != 0)
                                continue;
                        state->trigger_slope = i;
@@ -320,7 +320,7 @@ static int config_set(uint32_t key, GVariant *data,
        case SR_CONF_COUPLING:
                tmp = g_variant_get_string(data, NULL);
 
-               for (i = 0; (*model->coupling_options)[i]; i++) {
+               for (i = 0; i < model->num_coupling_options; i++) {
                        if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
                                continue;
                        for (j = 1; j <= model->analog_channels; j++) {
@@ -373,20 +373,17 @@ static int config_list(uint32_t key, GVariant **data,
                *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog));
                break;
        case SR_CONF_COUPLING:
-               *data = g_variant_new_strv(*model->coupling_options,
-                          g_strv_length((char **)*model->coupling_options));
+               *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
                break;
        case SR_CONF_TRIGGER_SOURCE:
                if (!model)
                        return SR_ERR_ARG;
-               *data = g_variant_new_strv(*model->trigger_sources,
-                          g_strv_length((char **)*model->trigger_sources));
+               *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
                break;
        case SR_CONF_TRIGGER_SLOPE:
                if (!model)
                        return SR_ERR_ARG;
-               *data = g_variant_new_strv(*model->trigger_slopes,
-                          g_strv_length((char **)*model->trigger_slopes));
+               *data = g_variant_new_strv(*model->trigger_slopes, model->num_trigger_slopes);
                break;
        case SR_CONF_TIMEBASE:
                if (!model)
index 7aebdc59004e8b940de06e39686b079f10362022..777bee3479a5b7c58499e5f3471912914f51fcc4 100644 (file)
@@ -80,15 +80,14 @@ static const char *coupling_options[] = {
        "D1M", // DC with 1 MOhm termination
        "GND",
        "OVL",
-       NULL,
 };
 
 static const char *scope_trigger_slopes[] = {
-       "POS", "NEG", NULL,
+       "POS", "NEG",
 };
 
 static const char *trigger_sources[] = {
-       "C1", "C2", "C3", "C4", "LINE", "EXT", NULL,
+       "C1", "C2", "C3", "C4", "LINE", "EXT",
 };
 
 static const struct sr_rational timebases[] = {
@@ -173,8 +172,13 @@ static const struct scope_config scope_models[] = {
                .analog_names = &scope_analog_channel_names,
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &trigger_sources,
+               .num_trigger_sources = ARRAY_SIZE(trigger_sources),
+
                .trigger_slopes = &scope_trigger_slopes,
+               .num_trigger_slopes = ARRAY_SIZE(scope_trigger_slopes),
 
                .timebases = timebases,
                .num_timebases = ARRAY_SIZE(timebases),
@@ -218,11 +222,11 @@ static void scope_state_dump(const struct scope_config *config,
 }
 
 static int scope_state_get_array_option(const char *resp,
-                                       const char *(*array)[], int *result)
+       const char *(*array)[], unsigned int n, int *result)
 {
        unsigned int i;
 
-       for (i = 0; (*array)[i]; i++) {
+       for (i = 0; i < n; i++) {
                if (!g_strcmp0(resp, (*array)[i])) {
                        *result = i;
                        return SR_OK;
@@ -302,6 +306,7 @@ static int analog_channel_state_get(struct sr_scpi_dev_inst *scpi,
 
 
                if (scope_state_get_array_option(tmp_str, config->coupling_options,
+                                config->num_coupling_options,
                                 &state->analog_channels[i].coupling) != SR_OK)
                        return SR_ERR;
 
@@ -383,7 +388,7 @@ SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi)
                i++;
        }
 
-       if (!trig_source || scope_state_get_array_option(trig_source, config->trigger_sources, &state->trigger_source) != SR_OK)
+       if (!trig_source || scope_state_get_array_option(trig_source, config->trigger_sources, config->num_trigger_sources, &state->trigger_source) != SR_OK)
                return SR_ERR;
 
        g_snprintf(command, sizeof(command), "%s:TRIG_SLOPE?", trig_source);
@@ -391,7 +396,7 @@ SR_PRIV int lecroy_xstream_state_get(struct sr_dev_inst *sdi)
                return SR_ERR;
 
        if (scope_state_get_array_option(tmp_str,
-               config->trigger_slopes, &state->trigger_slope) != SR_OK)
+               config->trigger_slopes, config->num_trigger_slopes, &state->trigger_slope) != SR_OK)
                return SR_ERR;
 
        if (sr_scpi_get_float(sdi->conn, "TRIG_DELAY?", &state->horiz_triggerpos) != SR_OK)
index 266504e6290852f21949ce3cc3ca26a2f5473958..fecab8905147f20854bc0c48d88ff51df03254b9 100644 (file)
@@ -45,6 +45,7 @@ struct scope_config {
        const uint8_t num_trigger_sources;
 
        const char *(*trigger_slopes)[];
+       const uint8_t num_trigger_slopes;
 
        const struct sr_rational *timebases;
        const uint8_t num_timebases;
index ebe73216000f12c293794b846e69efd4875c19e1..7545d00b3766c07e2cb8fcc09c5b7f4a395cacb2 100644 (file)
@@ -311,7 +311,7 @@ static int config_set(uint32_t key, GVariant *data,
                break;
        case SR_CONF_TRIGGER_SOURCE:
                tmp = g_variant_get_string(data, NULL);
-               for (i = 0; (*model->trigger_sources)[i]; i++) {
+               for (i = 0; i < model->num_trigger_sources; i++) {
                        if (g_strcmp0(tmp, (*model->trigger_sources)[i]) != 0)
                                continue;
                        state->trigger_source = i;
@@ -381,7 +381,7 @@ static int config_set(uint32_t key, GVariant *data,
 
                tmp = g_variant_get_string(data, NULL);
 
-               for (i = 0; (*model->coupling_options)[i]; i++) {
+               for (i = 0; i < model->num_coupling_options; i++) {
                        if (strcmp(tmp, (*model->coupling_options)[i]) != 0)
                                continue;
                        for (j = 1; j <= model->analog_channels; j++) {
@@ -444,12 +444,10 @@ static int config_list(uint32_t key, GVariant **data,
                case SR_CONF_TRIGGER_SOURCE:
                        if (!model)
                                return SR_ERR_ARG;
-                       *data = g_variant_new_strv(*model->trigger_sources,
-                                       g_strv_length((char **)*model->trigger_sources));
+                       *data = g_variant_new_strv(*model->trigger_sources, model->num_trigger_sources);
                        return SR_OK;
                case SR_CONF_TRIGGER_SLOPE:
-                       *data = g_variant_new_strv(dlm_trigger_slopes,
-                                       g_strv_length((char **)dlm_trigger_slopes));
+                       *data = g_variant_new_strv(ARRAY_AND_SIZE(dlm_trigger_slopes));
                        return SR_OK;
                case SR_CONF_NUM_HDIV:
                        *data = g_variant_new_uint32(model->num_xdivs);
@@ -474,8 +472,7 @@ static int config_list(uint32_t key, GVariant **data,
        case SR_CONF_COUPLING:
                if (cg_type == CG_NONE)
                        return SR_ERR_CHANNEL_GROUP;
-               *data = g_variant_new_strv(*model->coupling_options,
-                               g_strv_length((char **)*model->coupling_options));
+               *data = g_variant_new_strv(*model->coupling_options, model->num_coupling_options);
                break;
        case SR_CONF_VDIV:
                if (cg_type == CG_NONE)
index e40066b0c0eeb9ed16bfc9e03f012c181ba629c3..d22b223ef18b69f5ffd8036a32bd560ac63379ce 100644 (file)
 
 static const char *coupling_options[] = {
        "AC", "DC", "DC50", "GND",
-       NULL,
 };
 
 static const char *trigger_sources_2ch[] = {
        "1", "2", "LINE", "EXT",
-       NULL,
 };
 
 /* TODO: Is BITx handled correctly or is Dx required? */
@@ -37,13 +35,11 @@ static const char *trigger_sources_4ch[] = {
        "1", "2", "3", "4",
        "LINE", "EXT", "BIT1",
        "BIT2", "BIT3", "BIT4", "BIT5", "BIT6", "BIT7", "BIT8",
-       NULL,
 };
 
 /* Note: Values must correlate to the trigger_slopes values. */
-const char *dlm_trigger_slopes[3] = {
+const char *dlm_trigger_slopes[2] = {
        "r", "f",
-       NULL,
 };
 
 const uint64_t dlm_timebases[36][2] = {
@@ -138,7 +134,10 @@ static const struct scope_config scope_models[] = {
                .digital_names = &scope_digital_channel_names_8,
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &trigger_sources_2ch,
+               .num_trigger_sources = ARRAY_SIZE(trigger_sources_2ch),
 
                .num_xdivs = 10,
                .num_ydivs = 8,
@@ -154,7 +153,10 @@ static const struct scope_config scope_models[] = {
                .digital_names = &scope_digital_channel_names_8,
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &trigger_sources_4ch,
+               .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
 
                .num_xdivs = 10,
                .num_ydivs = 8,
@@ -172,7 +174,10 @@ static const struct scope_config scope_models[] = {
                .digital_names = NULL,
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &trigger_sources_4ch,
+               .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
 
                .num_xdivs = 10,
                .num_ydivs = 8,
@@ -188,7 +193,10 @@ static const struct scope_config scope_models[] = {
                .digital_names = &scope_digital_channel_names_32,
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &trigger_sources_4ch,
+               .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
 
                .num_xdivs = 10,
                .num_ydivs = 8,
@@ -204,7 +212,10 @@ static const struct scope_config scope_models[] = {
                .digital_names = &scope_digital_channel_names_32,
 
                .coupling_options = &coupling_options,
+               .num_coupling_options = ARRAY_SIZE(coupling_options),
+
                .trigger_sources = &trigger_sources_4ch,
+               .num_trigger_sources = ARRAY_SIZE(trigger_sources_4ch),
 
                .num_xdivs = 10,
                .num_ydivs = 8,
@@ -271,13 +282,13 @@ static void scope_state_dump(const struct scope_config *config,
  * @return SR_ERR when value couldn't be found, SR_OK otherwise.
  */
 static int array_option_get(char *value, const char *(*array)[],
-               int *result)
+               unsigned int n, int *result)
 {
        unsigned int i;
 
        *result = -1;
 
-       for (i = 0; (*array)[i]; i++)
+       for (i = 0; i < n; i++)
                if (!g_strcmp0(value, (*array)[i])) {
                        *result = i;
                        break;
@@ -425,6 +436,7 @@ static int analog_channel_state_get(const struct sr_dev_inst *sdi,
                }
 
                if (array_option_get(response, config->coupling_options,
+                               config->num_coupling_options,
                                &state->analog_states[i].coupling) != SR_OK) {
                        g_free(response);
                        return SR_ERR;
@@ -660,7 +672,7 @@ SR_PRIV int dlm_scope_state_query(struct sr_dev_inst *sdi)
        }
 
        if (array_option_get(response, config->trigger_sources,
-                       &state->trigger_source) != SR_OK) {
+                       config->num_trigger_sources, &state->trigger_source) != SR_OK) {
                g_free(response);
                return SR_ERR;
        }
index dae0477c5e90e2b76ff4effad8641d77bed4d01e..6b27385d216dbaa902e87e8014e638b12dcefb0b 100644 (file)
@@ -49,7 +49,7 @@ enum trigger_slopes {
        SLOPE_NEGATIVE
 };
 
-extern const char *dlm_trigger_slopes[3];
+extern const char *dlm_trigger_slopes[2];
 extern const uint64_t dlm_timebases[36][2];
 extern const uint64_t dlm_vdivs[17][2];