X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fmotech-lps-30x%2Fapi.c;h=46407cc5f01cd95b498bad0eb744fd9d4a385cd9;hb=HEAD;hp=464ce7db8ae923ac5a94537bde4d941d6ed347fb;hpb=54d471f4980f1b975188b5e8bdf5aa90acd3cece;p=libsigrok.git diff --git a/src/hardware/motech-lps-30x/api.c b/src/hardware/motech-lps-30x/api.c index 464ce7db..b29346ca 100644 --- a/src/hardware/motech-lps-30x/api.c +++ b/src/hardware/motech-lps-30x/api.c @@ -32,8 +32,6 @@ SR_PRIV int lps_query_status(struct sr_dev_inst *sdi); #define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0" -#define VENDOR_MOTECH "Motech" - static const uint32_t scanopts[] = { SR_CONF_CONN, SR_CONF_SERIALCOMM, @@ -365,13 +363,12 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o sdi = NULL; devc = NULL; - conn = serialcomm = NULL; /* Process and check options. */ + conn = NULL; + serialcomm = SERIALCOMM; if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK) return NULL; - if (!serialcomm) - serialcomm = SERIALCOMM; /* Init serial port. */ serial = sr_serial_dev_inst_new(conn, serialcomm); @@ -380,7 +377,6 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o goto exit_err; /* Query and verify model string. */ - serial_flush(serial); if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK) return NULL; @@ -423,7 +419,7 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o sdi = g_malloc0(sizeof(struct sr_dev_inst)); sdi->status = SR_ST_INACTIVE; - sdi->vendor = g_strdup(VENDOR_MOTECH); + sdi->vendor = g_strdup("Motech"); sdi->model = g_strdup(models[modelid].modelstr); sdi->version = g_strdup(verstr); sdi->inst_type = SR_INST_SERIAL; @@ -442,13 +438,9 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o devc->channel_status[cnt].info = g_slist_append(NULL, ch); - cg = g_malloc(sizeof(struct sr_channel_group)); - snprintf(channel, sizeof(channel), "CG%d", cnt+1); - cg->name = g_strdup(channel); - cg->priv = NULL; + snprintf(channel, sizeof(channel), "CG%d", cnt + 1); + cg = sr_channel_group_new(sdi, channel, NULL); cg->channels = g_slist_append(NULL, ch); - - sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); } /* Query status */ @@ -491,8 +483,8 @@ static int dev_clear(const struct sr_dev_driver *di) return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); } -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; struct sr_channel *ch; @@ -543,17 +535,15 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s return SR_OK; } -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) { struct dev_context *devc; struct sr_channel *ch; gdouble dval; int ch_idx; - const char *sval; gboolean bval; int idx; - gboolean found; devc = sdi->priv; @@ -568,22 +558,15 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd case SR_CONF_LIMIT_SAMPLES: return sr_sw_limits_config_set(&devc->limits, key, data); case SR_CONF_CHANNEL_CONFIG: - sval = g_variant_get_string(data, NULL); - found = FALSE; - for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++) { - if (!strcmp(sval, channel_modes[idx])) { - found = TRUE; - if (devc->tracking_mode == idx) - break; /* Nothing to do! */ - devc->tracking_mode = idx; - if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */ - return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode); - } - if (devc->model->modelid <= LPS_303) /* Only first setting possible for smaller models. */ - break; - } - if (!found) + if ((idx = std_str_idx(data, ARRAY_AND_SIZE(channel_modes))) < 0) return SR_ERR_ARG; + if (devc->model->modelid <= LPS_303 && idx != 0) + break; /* Only first setting possible for smaller models. */ + if (devc->tracking_mode == idx) + break; /* Nothing to do! */ + devc->tracking_mode = idx; + if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */ + return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode); break; default: return SR_ERR_NA; @@ -623,7 +606,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd return SR_ERR_NA; devc->channel_status[ch_idx].output_current_max = dval; return lps_cmd_ok(sdi->conn, "ISET%d %05.4f", ch_idx+1, dval); - break; case SR_CONF_ENABLED: bval = g_variant_get_boolean(data); if (bval == devc->channel_status[ch_idx].output_enabled) /* Nothing to do. */ @@ -647,8 +629,8 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd return SR_OK; } -static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_list(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; struct sr_channel *ch; @@ -662,12 +644,14 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * case SR_CONF_DEVICE_OPTIONS: return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); case SR_CONF_CHANNEL_CONFIG: + if (!devc || !devc->model) + return SR_ERR_ARG; if (devc->model->modelid <= LPS_303) { /* The 1-channel models. */ *data = g_variant_new_strv(channel_modes, 1); } else { /* The other models support all modes. */ - *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes)); + *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes)); } return SR_OK; default: @@ -682,16 +666,18 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * switch (key) { case SR_CONF_DEVICE_OPTIONS: if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */ - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts_cg_ch12, ARRAY_SIZE(devopts_cg_ch12), sizeof(uint32_t)); + *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ch12)); else /* Must be CH3 */ - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts_cg_ch3, ARRAY_SIZE(devopts_cg_ch3), sizeof(uint32_t)); + *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_ch3)); break; case SR_CONF_VOLTAGE_TARGET: + if (!devc || !devc->model) + return SR_ERR_ARG; *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].voltage); break; case SR_CONF_CURRENT_LIMIT: + if (!devc || !devc->model) + return SR_ERR_ARG; *data = std_gvar_min_max_step_array(devc->model->channels[ch_idx].current); break; default: