From: Uwe Hermann Date: Tue, 1 Aug 2017 19:12:04 +0000 (+0200) Subject: drivers: Use array-based approach in some places. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=692716f5d1aae4ef06c67b57aaf253f46f538c33;p=libsigrok.git drivers: Use array-based approach in some places. This allows us to use the new array helpers in a few more places. --- diff --git a/src/hardware/hameg-hmo/api.c b/src/hardware/hameg-hmo/api.c index 5e6ad66c..db6c67df 100644 --- a/src/hardware/hameg-hmo/api.c +++ b/src/hardware/hameg-hmo/api.c @@ -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) diff --git a/src/hardware/hameg-hmo/protocol.c b/src/hardware/hameg-hmo/protocol.c index fb7fecb2..6d2ad3c4 100644 --- a/src/hardware/hameg-hmo/protocol.c +++ b/src/hardware/hameg-hmo/protocol.c @@ -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) diff --git a/src/hardware/hameg-hmo/protocol.h b/src/hardware/hameg-hmo/protocol.h index 037b2426..43220dd7 100644 --- a/src/hardware/hameg-hmo/protocol.h +++ b/src/hardware/hameg-hmo/protocol.h @@ -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; diff --git a/src/hardware/lecroy-xstream/api.c b/src/hardware/lecroy-xstream/api.c index 2799f6fb..3a8061d9 100644 --- a/src/hardware/lecroy-xstream/api.c +++ b/src/hardware/lecroy-xstream/api.c @@ -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) diff --git a/src/hardware/lecroy-xstream/protocol.c b/src/hardware/lecroy-xstream/protocol.c index 7aebdc59..777bee34 100644 --- a/src/hardware/lecroy-xstream/protocol.c +++ b/src/hardware/lecroy-xstream/protocol.c @@ -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) diff --git a/src/hardware/lecroy-xstream/protocol.h b/src/hardware/lecroy-xstream/protocol.h index 266504e6..fecab890 100644 --- a/src/hardware/lecroy-xstream/protocol.h +++ b/src/hardware/lecroy-xstream/protocol.h @@ -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; diff --git a/src/hardware/yokogawa-dlm/api.c b/src/hardware/yokogawa-dlm/api.c index ebe73216..7545d00b 100644 --- a/src/hardware/yokogawa-dlm/api.c +++ b/src/hardware/yokogawa-dlm/api.c @@ -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) diff --git a/src/hardware/yokogawa-dlm/protocol.c b/src/hardware/yokogawa-dlm/protocol.c index e40066b0..d22b223e 100644 --- a/src/hardware/yokogawa-dlm/protocol.c +++ b/src/hardware/yokogawa-dlm/protocol.c @@ -24,12 +24,10 @@ 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; } diff --git a/src/hardware/yokogawa-dlm/protocol.h b/src/hardware/yokogawa-dlm/protocol.h index dae0477c..6b27385d 100644 --- a/src/hardware/yokogawa-dlm/protocol.h +++ b/src/hardware/yokogawa-dlm/protocol.h @@ -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];