X-Git-Url: https://sigrok.org/gitweb/?p=libsigrok.git;a=blobdiff_plain;f=src%2Fhardware%2Fmotech-lps-30x%2Fapi.c;h=d5a834f7e972376c093f7426dc1cb0e1f138d322;hp=9849717a7a95a962edb5f87435323c244a34030c;hb=05199c0ac9f15f229d43e50e86c4c1eadc55deac;hpb=c442ffda0fc6fa9bc3c5397a21ef1d04f01a64a1 diff --git a/src/hardware/motech-lps-30x/api.c b/src/hardware/motech-lps-30x/api.c index 9849717a..d5a834f7 100644 --- a/src/hardware/motech-lps-30x/api.c +++ b/src/hardware/motech-lps-30x/api.c @@ -18,18 +18,20 @@ * along with this program. If not, see . */ -/** @file - * Motech LPS-30x series power supply driver - * @internal +/** + * @file + * + * Motech LPS-30x series power supply driver + * + * @internal */ +#include #include #include #include #include "protocol.h" -/* Forward declarations */ -SR_PRIV struct sr_dev_driver motech_lps_301_driver_info; SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen); SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args); SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...); @@ -41,22 +43,21 @@ SR_PRIV int lps_query_status(struct sr_dev_inst *sdi); #define VENDOR_MOTECH "Motech" -/** Driver capabilities generic. */ -static const uint32_t drvopts[] = { - /* Device class */ - SR_CONF_POWER_SUPPLY, -}; - /** Driver scanning options. */ static const uint32_t scanopts[] = { SR_CONF_CONN, SR_CONF_SERIALCOMM, }; +/** Driver capabilities generic. */ +static const uint32_t drvopts[] = { + /* Device class */ + SR_CONF_POWER_SUPPLY, +}; + /** Hardware capabilities generic. */ static const uint32_t devopts[] = { /* Device class */ - SR_CONF_POWER_SUPPLY, /* Acquisition modes. */ SR_CONF_CONTINUOUS, SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, @@ -132,11 +133,6 @@ static const struct lps_modelspec models[] = { }, }; -static int init_lps301(struct sr_dev_driver *di, struct sr_context *sr_ctx) -{ - return std_init(sr_ctx, di, LOG_PREFIX); -} - /** Send command to device with va_list. */ SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args) @@ -150,7 +146,8 @@ SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_l sr_spew("lps_send_va: \"%s\"", buf); - retc = serial_write_blocking(serial, buf, strlen(buf), 0); + retc = serial_write_blocking(serial, buf, strlen(buf), + serial_timeout(serial, strlen(buf))); if (retc < 0) return SR_ERR; @@ -179,7 +176,7 @@ SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...) va_list args; char buf[LINELEN_MAX]; char *bufptr; - int buflen; + int buflen; /* Send command */ va_start(args, fmt); @@ -200,8 +197,9 @@ SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...) return SR_ERR; } -/** Send command and read reply string. - * @param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated. +/** + * Send command and read reply string. + * @param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated. */ SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...) { @@ -209,7 +207,7 @@ SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const va_list args; char buf[LINELEN_MAX]; char *bufptr; - int buflen; + int buflen; reply[0] = '\0'; @@ -240,7 +238,7 @@ SR_PRIV int lps_process_status(struct sr_dev_inst *sdi, int stat) struct dev_context *devc; int tracking_mode; - devc = (struct dev_context *)sdi->priv; + devc = sdi->priv; sr_spew("Status: %d", stat); devc->channel_status[0].cc_mode = (stat & 0x01) != 0; @@ -287,7 +285,7 @@ SR_PRIV int lps_query_status(struct sr_dev_inst *sdi) int stat, ret; struct dev_context *devc; - devc = (struct dev_context *)sdi->priv; + devc = sdi->priv; devc->req_sent_at = g_get_real_time(); @@ -313,9 +311,11 @@ static gint64 calc_timeout_ms(gint64 start_us) return result; } -/** Read message into buf until "OK" received. - * @retval SR_OK Msg received; buf and buflen contain result, if any except OK. - * @retval SR_ERR Error, including timeout. +/** + * Read message into buf until "OK" received. + * + * @retval SR_OK Msg received; buf and buflen contain result, if any except OK. + * @retval SR_ERR Error, including timeout. */ SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen) { @@ -372,12 +372,10 @@ SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *b static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *options) { struct sr_dev_inst *sdi; - struct drv_context *drvc; struct dev_context *devc; struct sr_serial_dev_inst *serial; struct sr_channel *ch; struct sr_channel_group *cg; - GSList *devices; const char *conn, *serialcomm; int cnt, ret; gchar buf[LINELEN_MAX]; @@ -387,12 +385,6 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o sdi = NULL; devc = NULL; conn = serialcomm = NULL; - devices = NULL; - - drvc = drv->context; - drvc->instances = NULL; - - sr_spew("scan() called!"); /* Process and check options. */ if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK) @@ -443,7 +435,7 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o g_strstrip(buf); verstr = buf + 4; } - else /* Bug in device FW 1.17: Querying version string fails while output is active. + else /* Bug in device FW 1.17: Querying version string fails while output is active. Therefore just print an error message, but do not exit with error. */ sr_err("Failed to query for hardware version: %s.", sr_strerror(ret)); @@ -453,16 +445,12 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o sdi->vendor = g_strdup(VENDOR_MOTECH); sdi->model = g_strdup(models[modelid].modelstr); sdi->version = g_strdup(verstr); - sdi->driver = drv; sdi->inst_type = SR_INST_SERIAL; sdi->conn = serial; devc = g_malloc0(sizeof(struct dev_context)); + sr_sw_limits_init(&devc->limits); devc->model = &models[modelid]; - devc->limit_samples = 0; - devc->limit_msec = 0; - devc->num_samples = 0; - devc->elapsed_msec = g_timer_new(); sdi->priv = devc; @@ -482,29 +470,22 @@ static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *o sdi->channel_groups = g_slist_append(sdi->channel_groups, cg); } - drvc->instances = g_slist_append(drvc->instances, sdi); - devices = g_slist_append(devices, sdi); - /* Query status */ if (lps_query_status(sdi) != SR_OK) goto exit_err; serial_close(serial); - if (!devices) - sr_serial_dev_inst_free(serial); - return devices; + return std_scan_complete(drv, g_slist_append(NULL, sdi)); exit_err: sr_info("%s: Error!", __func__); - if (serial) { + if (serial) serial_close(serial); - sr_serial_dev_inst_free(serial); - } + sr_serial_dev_inst_free(serial); g_free(devc); - if (sdi) - sr_dev_inst_free(sdi); + sr_dev_inst_free(sdi); return NULL; } @@ -515,30 +496,18 @@ static GSList *scan_lps301(struct sr_dev_driver *di, GSList *options) return do_scan(LPS_301, di, options); } -static GSList *dev_list_lps301(const struct sr_dev_driver *di) -{ - return ((struct drv_context *)(di->context))->instances; -} - -static void dev_clear_private(struct dev_context *devc) +static void clear_helper(struct dev_context *devc) { int ch_idx; /* Free channel_status.info (list only, data owned by sdi). */ for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++) g_slist_free(devc->channel_status[ch_idx].info); - - g_timer_destroy(devc->elapsed_msec); } -static int dev_clear_lps301(const struct sr_dev_driver *di) +static int dev_clear(const struct sr_dev_driver *di) { - return std_dev_clear(di, (std_dev_clear_callback)dev_clear_private); -} - -static int cleanup(const struct sr_dev_driver *di) -{ - return dev_clear_lps301(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, @@ -557,11 +526,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s /* No channel group: global options. */ switch (key) { case SR_CONF_LIMIT_SAMPLES: - *data = g_variant_new_uint64(devc->limit_samples); - break; case SR_CONF_LIMIT_MSEC: - *data = g_variant_new_uint64(devc->limit_msec); - break; + return sr_sw_limits_config_get(&devc->limits, key, data); case SR_CONF_CHANNEL_CONFIG: *data = g_variant_new_string(channel_modes[devc->tracking_mode]); break; @@ -608,9 +574,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd int idx; gboolean found; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - devc = sdi->priv; /* Cannot change settings while acquisition active, would cause a mess with commands. @@ -622,11 +585,8 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd /* No channel group: global options. */ switch (key) { case SR_CONF_LIMIT_MSEC: - devc->limit_msec = g_variant_get_uint64(data); - break; case SR_CONF_LIMIT_SAMPLES: - devc->limit_samples = g_variant_get_uint64(data); - break; + return sr_sw_limits_config_set(&devc->limits, key, data); case SR_CONF_CHANNEL_CONFIG: sval = g_variant_get_string(data, NULL); found = FALSE; @@ -717,32 +677,14 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * GVariant *gvar; GVariantBuilder gvb; - /* Driver options, no device instance necessary. */ - switch (key) { - case SR_CONF_SCAN_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); - return SR_OK; - case SR_CONF_DEVICE_OPTIONS: - if (sdi != NULL) - break; - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); - return SR_OK; - default: - if (!sdi) - return SR_ERR_ARG; - devc = sdi->priv; - break; - } + devc = (sdi) ? sdi->priv : NULL; /* Device options, independent from channel groups. */ if (!cg) { switch (key) { + case SR_CONF_SCAN_OPTIONS: case SR_CONF_DEVICE_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); - return SR_OK; + return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); case SR_CONF_CHANNEL_CONFIG: if (devc->model->modelid <= LPS_303) { /* The 1-channel models. */ @@ -764,10 +706,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * 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_ch12, ARRAY_SIZE(devopts_ch12), sizeof(uint32_t)); + devopts_ch12, ARRAY_SIZE(devopts_ch12), sizeof(uint32_t)); else /* Must be CH3 */ *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts_ch3, ARRAY_SIZE(devopts_ch3), sizeof(uint32_t)); + devopts_ch3, ARRAY_SIZE(devopts_ch3), sizeof(uint32_t)); break; case SR_CONF_VOLTAGE_TARGET: g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY); @@ -794,14 +736,11 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * return SR_OK; } -static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_start(const struct sr_dev_inst *sdi) { struct dev_context *devc; struct sr_serial_dev_inst *serial; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - devc = sdi->priv; devc->acq_running = TRUE; @@ -809,11 +748,9 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) serial = sdi->conn; serial_source_add(sdi->session, serial, G_IO_IN, 50, motech_lps_30x_receive_data, (void *)sdi); - std_session_send_df_header(cb_data, LOG_PREFIX); + std_session_send_df_header(sdi); - /* Start timer, if required. */ - if (devc->limit_msec) - g_timer_start(devc->elapsed_msec); + sr_sw_limits_acquisition_start(&devc->limits); devc->acq_req = AQ_NONE; /* Do not start polling device here, the read function will do it in 50 ms. */ @@ -821,33 +758,22 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) return SR_OK; } -static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) -{ - struct dev_context *devc; - - /* Stop timer, if required. */ - if (sdi && (devc = sdi->priv) && devc->limit_msec) - g_timer_stop(devc->elapsed_msec); - - return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, - sdi->conn, LOG_PREFIX); -} - -SR_PRIV struct sr_dev_driver motech_lps_301_driver_info = { +static struct sr_dev_driver motech_lps_301_driver_info = { .name = "motech-lps-301", .longname = "Motech LPS-301", .api_version = 1, - .init = init_lps301, - .cleanup = cleanup, + .init = std_init, + .cleanup = std_cleanup, .scan = scan_lps301, - .dev_list = dev_list_lps301, - .dev_clear = dev_clear_lps301, + .dev_list = std_dev_list, + .dev_clear = dev_clear, .config_get = config_get, .config_set = config_set, .config_list = config_list, .dev_open = std_serial_dev_open, .dev_close = std_serial_dev_close, .dev_acquisition_start = dev_acquisition_start, - .dev_acquisition_stop = dev_acquisition_stop, + .dev_acquisition_stop = std_serial_dev_acquisition_stop, .context = NULL, }; +SR_REGISTER_DEV_DRIVER(motech_lps_301_driver_info);